From 4b575a8dab798fc213c56d21891e6d6e7446bbc5 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Wed, 7 Dec 2022 23:18:23 +0800 Subject: [PATCH 0001/1533] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 0b4048d509..3d9a605052 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -657,6 +657,39 @@ object Solution { } ``` +## rust + +```rust +impl Solution { + pub fn delete_node( + root: Option>>, + key: i32, + ) -> Option>> { + root.as_ref()?; + + let mut node = root.as_ref().unwrap().borrow_mut(); + match node.val.cmp(&key) { + std::cmp::Ordering::Less => node.right = Self::delete_node(node.right.clone(), key), + std::cmp::Ordering::Equal => match (node.left.clone(), node.right.clone()) { + (None, None) => return None, + (None, Some(r)) => return Some(r), + (Some(l), None) => return Some(l), + (Some(l), Some(r)) => { + let mut cur = Some(r.clone()); + while let Some(n) = cur.clone().unwrap().borrow().left.clone() { + cur = Some(n); + } + cur.unwrap().borrow_mut().left = Some(l); + return Some(r); + } + }, + std::cmp::Ordering::Greater => node.left = Self::delete_node(node.left.clone(), key), + } + root.clone() + } +} +``` +

From d97f27621c96972c11b0e48b5498c411ac1e85f5 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:44:18 +0800 Subject: [PATCH 0002/1533] =?UTF-8?q?Update=20problems/0450.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 3d9a605052..424491b725 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -685,7 +685,8 @@ impl Solution { }, std::cmp::Ordering::Greater => node.left = Self::delete_node(node.left.clone(), key), } - root.clone() + drop(node); + root } } ``` From 7a4057066a52a68b60e1c19670a5f21b5ffa716c Mon Sep 17 00:00:00 2001 From: StriveDD Date: Thu, 2 Mar 2023 11:15:07 +0800 Subject: [PATCH 0003/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0841.=E9=92=A5?= =?UTF-8?q?=E5=8C=99=E5=92=8C=E6=88=BF=E9=97=B4=E7=9A=84Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84BFS=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...31\345\222\214\346\210\277\351\227\264.md" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index a973ab56c4..00263cac9a 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -273,6 +273,48 @@ class Solution { return true; } } + +// 广度优先搜索 +class Solution { + public boolean canVisitAllRooms(List> rooms) { + boolean[] visited = new boolean[rooms.size()]; // 用一个 visited 数据记录房间是否被访问 + visited[0] = true; + Queue queue = new ArrayDeque<>(); + queue.add(0); // 第 0 个房间标记为已访问 + while (!queue.isEmpty()) { + int curKey = queue.poll(); + for (int key: rooms.get(curKey)) { + if (visited[key]) continue; + visited[key] = true; + queue.add(key); + } + } + for (boolean key: visited) + if (!key) return false; + return true; + } +} + +// 广度优先遍历(时间优化) +class Solution { + public boolean canVisitAllRooms(List> rooms) { + int count = 1; // 用来记录可以被访问的房间数目,因为初始状态下 0 号房间可以被访问,所以置为 1 + boolean[] visited = new boolean[rooms.size()]; // 用一个 visited 数据记录房间是否被访问 + visited[0] = true; // 第 0 个房间标记为已访问 + Queue queue = new ArrayDeque<>(); + queue.add(0); + while (!queue.isEmpty()) { + int curKey = queue.poll(); + for (int key: rooms.get(curKey)) { + if (visited[key]) continue; + ++count; // 每访问一个访问房间就让 count 加 1 + visited[key] = true; + queue.add(key); + } + } + return count == rooms.size(); // 如果 count 等于房间数目,表示能进入所有房间,反之不能 + } +} ``` ### python3 @@ -417,3 +459,4 @@ function canVisitAllRooms(rooms: number[][]): boolean { + From 5dd403e69ab53c363799e0182e6443e2fde99d7a Mon Sep 17 00:00:00 2001 From: StriveDD Date: Thu, 2 Mar 2023 11:21:03 +0800 Subject: [PATCH 0004/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0127.=20=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8E=A5=E9=BE=99=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E5=8F=8C=E5=90=91BFS=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\350\257\215\346\216\245\351\276\231.md" | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 7ffd6a21e6..b4078913f2 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -16,7 +16,7 @@ * 转换过程中的中间单词必须是字典 wordList 中的单词。 * 给你两个单词 beginWord 和 endWord 和一个字典 wordList ,找到从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0。 -  + 示例 1: * 输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] @@ -134,7 +134,71 @@ public int ladderLength(String beginWord, String endWord, List wordList) } ``` +## Java 双向BFS + +```java +class Solution { + // 判断单词之间是否之差了一个字母 + public boolean isValid(String currentWord, String chooseWord) { + int count = 0; + for (int i = 0; i < currentWord.length(); i++) + if (currentWord.charAt(i) != chooseWord.charAt(i)) ++count; + return count == 1; + } + + public int ladderLength(String beginWord, String endWord, List wordList) { + if (!wordList.contains(endWord)) return 0; // 如果 endWord 不在 wordList 中,那么无法成功转换,返回 0 + + // ansLeft 记录从 beginWord 开始 BFS 时能组成的单词数目 + // ansRight 记录从 endWord 开始 BFS 时能组成的单词数目 + int ansLeft = 0, ansRight = 0; + + // queueLeft 表示从 beginWord 开始 BFS 时使用的队列 + // queueRight 表示从 endWord 开始 BFS 时使用的队列 + Queue queueLeft = new ArrayDeque<>(), queueRight = new ArrayDeque<>(); + queueLeft.add(beginWord); + queueRight.add(endWord); + + // 从 beginWord 开始 BFS 时把遍历到的节点存入 hashSetLeft 中 + // 从 endWord 开始 BFS 时把遍历到的节点存入 hashSetRight 中 + Set hashSetLeft = new HashSet<>(), hashSetRight = new HashSet<>(); + hashSetLeft.add(beginWord); + hashSetRight.add(endWord); + + // 只要有一个队列为空,说明 beginWord 无法转换到 endWord + while (!queueLeft.isEmpty() && !queueRight.isEmpty()) { + ++ansLeft; + int size = queueLeft.size(); + for (int i = 0; i < size; i++) { + String currentWord = queueLeft.poll(); + // 只要 hashSetRight 中存在 currentWord,说明从 currentWord 可以转换到 endWord + if (hashSetRight.contains(currentWord)) return ansRight + ansLeft; + for (String chooseWord : wordList) { + if (hashSetLeft.contains(chooseWord) || !isValid(currentWord, chooseWord)) continue; + hashSetLeft.add(chooseWord); + queueLeft.add(chooseWord); + } + } + ++ansRight; + size = queueRight.size(); + for (int i = 0; i < size; i++) { + String currentWord = queueRight.poll(); + // 只要 hashSetLeft 中存在 currentWord,说明从 currentWord 可以转换到 beginWord + if (hashSetLeft.contains(currentWord)) return ansLeft + ansRight; + for (String chooseWord : wordList) { + if (hashSetRight.contains(chooseWord) || !isValid(currentWord, chooseWord)) continue; + hashSetRight.add(chooseWord); + queueRight.add(chooseWord); + } + } + } + return 0; + } +} +``` + ## Python + ``` class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: @@ -301,3 +365,4 @@ function diffonechar(word1: string, word2: string): boolean { + From 453fa147c9adf88abc038f68d3b3f36acf696f89 Mon Sep 17 00:00:00 2001 From: StriveDD Date: Thu, 2 Mar 2023 14:44:36 +0800 Subject: [PATCH 0005/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A00827.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=BA=E5=B7=A5=E5=B2=9B=E7=9A=84Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\272\345\267\245\345\262\233.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 1b49e9ee66..45b2ef5ab9 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -219,7 +219,71 @@ public: }; ``` +# 其他语言版本 + +## Java + +```Java +class Solution { + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向 + + /** + * @param grid 矩阵数组 + * @param row 当前遍历的节点的行号 + * @param col 当前遍历的节点的列号 + * @param mark 当前区域的标记 + * @return 返回当前区域内 1 的数量 + */ + public int dfs(int[][] grid, int row, int col, int mark) { + int ans = 0; + grid[row][col] = mark; + for (int[] current: position) { + int curRow = row + current[0], curCol = col + current[1]; + if (curRow < 0 || curRow >= grid.length || curCol < 0 || curCol >= grid.length) continue; // 越界 + if (grid[curRow][curCol] == 1) + ans += 1 + dfs(grid, curRow, curCol, mark); + } + return ans; + } + + public int largestIsland(int[][] grid) { + int ans = Integer.MIN_VALUE, size = grid.length, mark = 2; + Map getSize = new HashMap<>(); + for (int row = 0; row < size; row++) { + for (int col = 0; col < size; col++) { + if (grid[row][col] == 1) { + int areaSize = 1 + dfs(grid, row, col, mark); + getSize.put(mark++, areaSize); + } + } + } + for (int row = 0; row < size; row++) { + for (int col = 0; col < size; col++) { + // 当前位置如果不是 0 那么直接跳过,因为我们只能把 0 变成 1 + if (grid[row][col] != 0) continue; + Set hashSet = new HashSet<>(); // 防止同一个区域被重复计算 + // 计算从当前位置开始获取的 1 的数量,初始化 1 是因为把当前位置的 0 转换成了 1 + int curSize = 1; + for (int[] current: position) { + int curRow = row + current[0], curCol = col + current[1]; + if (curRow < 0 || curRow >= grid.length || curCol < 0 || curCol >= grid.length) continue; + int curMark = grid[curRow][curCol]; // 获取对应位置的标记 + // 如果标记存在 hashSet 中说明该标记被记录过一次,如果不存在 getSize 中说明该标记是无效标记(此时 curMark = 0) + if (hashSet.contains(curMark) || !getSize.containsKey(curMark)) continue; + hashSet.add(curMark); + curSize += getSize.get(curMark); + } + ans = Math.max(ans, curSize); + } + } + // 当 ans == Integer.MIN_VALUE 说明矩阵数组中不存在 0,全都是有效区域,返回数组大小即可 + return ans == Integer.MIN_VALUE ? size * size : ans; + } +} +``` +

+ From 987bad6d74d57b065ae5aaf55196a0f790aa5d2c Mon Sep 17 00:00:00 2001 From: StriveDD Date: Thu, 2 Mar 2023 14:50:46 +0800 Subject: [PATCH 0006/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00127.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8E=A5=E9=BE=99=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=8C=E5=90=91BFS=E4=BB=A3=E7=A0=81=EF=BC=8C0827.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=BA=E5=B7=A5=E5=B2=9B=E7=9A=84Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=EF=BC=8C0841.=E9=92=A5=E5=8C=99=E5=92=8C=E6=88=BF?= =?UTF-8?q?=E9=97=B4=E7=9A=84Java=E7=89=88=E6=9C=AC=E7=9A=84BFS=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" | 3 +-- ...2\245\345\214\231\345\222\214\346\210\277\351\227\264.md" | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index b4078913f2..20ad518295 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -134,9 +134,8 @@ public int ladderLength(String beginWord, String endWord, List wordList) } ``` -## Java 双向BFS - ```java +// Java 双向BFS class Solution { // 判断单词之间是否之差了一个字母 public boolean isValid(String currentWord, String chooseWord) { diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index 00263cac9a..faf2b97f5d 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -273,7 +273,9 @@ class Solution { return true; } } +``` +```Java // 广度优先搜索 class Solution { public boolean canVisitAllRooms(List> rooms) { @@ -294,7 +296,9 @@ class Solution { return true; } } +``` +```java // 广度优先遍历(时间优化) class Solution { public boolean canVisitAllRooms(List> rooms) { @@ -459,4 +463,3 @@ function canVisitAllRooms(rooms: number[][]): boolean { - From 2a9b627a90574695b2e5689c721428e27b5f1dcc Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 3 Mar 2023 09:46:38 +0800 Subject: [PATCH 0007/1533] Update --- problems/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 problems/.DS_Store diff --git a/problems/.DS_Store b/problems/.DS_Store deleted file mode 100644 index 32266f382acd7b944a23d2f5f05301bbed6d4e0f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKu}Z{15S`7Xh^TO-<^Doyn|MS!8_z;f zVC$RRxm?z$r5rK?vu`uAlgGYm1 zMLXL^w*hCgdN+$V`92QzXMC=vDCb;Xa{*8E#yp+f@nHS>C1gr@Cz5G$oj%rFb#~6y z&W3!?%%`Rbr~<0MqAP%&&6X^C)KL{s1yq4r0scN%IAbPQd32u+4DJX3j1hK*IiDrC z#tLSFl}BV?#+L$psgWaw@#TnHS(gb`9(_3($Bg?3vyl^uaqNg&x13DoQAbrk6$lmR z_{%Q$|IMGz|6!3{sRF9Nzf!=&c|Y%AOVV3go8w+<;R84uud6)PC78HYjJ4c~*Wk_& Yw>$x6f|W-^VEQBAWY9qs_)`Tw0S;+s82|tP From 54847a181cfd59d8e938aa619daf72307a50ac39 Mon Sep 17 00:00:00 2001 From: GODVvVZzz <2662446324@qq.com> Date: Fri, 3 Mar 2023 10:57:00 +0800 Subject: [PATCH 0008/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97-=E6=9F=A5=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 039f3596df..f47925fe32 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -117,7 +117,7 @@ for (int i = 3; i <= n ; i++) { 其实可以模拟一下哈,拆分j的情况,在遍历j的过程中dp[i - j]其实都计算过了。 -例如 i= 10,j = 5,i-j = 5,如果把j查分为 2 和 3,其实在j = 2 的时候,i-j= 8 ,拆分i-j的时候就可以拆出来一个3了。 +例如 i= 10,j = 5,i-j = 5,如果把j拆分为 2 和 3,其实在j = 2 的时候,i-j= 8 ,拆分i-j的时候就可以拆出来一个3了。 **或者也可以理解j是拆分i的第一个整数**。 From 419e67ca91122f505f937e774d41bfd1a8c60246 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Fri, 3 Mar 2023 23:26:04 +0800 Subject: [PATCH 0009/1533] =?UTF-8?q?update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86=EF=BC=9A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=20python=20=E4=BB=A3=E7=A0=81=E4=B8=AD=E4=B8=80=E7=BB=B4?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 45dd289a21..dfb327ec1f 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -302,7 +302,7 @@ class Solution: target = sum(nums) if target % 2 == 1: return False target //= 2 - dp = [0] * (len(nums) + 1) + dp = [0] * (target + 1) for i in range(len(nums)): for j in range(target, nums[i] - 1, -1): dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) From 511381119d8228de09c2fb57a3b4c3ce0163bee5 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 4 Mar 2023 18:30:51 +0800 Subject: [PATCH 0010/1533] =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=8F=98=E9=87=8F?= =?UTF-8?q?=EF=BC=8C=E4=B8=8A=E9=9D=A2=E9=83=BD=E6=98=AFresSet=E4=B8=8B?= =?UTF-8?q?=E9=9D=A2=E5=86=99=E6=88=90=E4=BA=86setRes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\260\347\273\204\347\232\204\344\272\244\351\233\206.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index ed6a5d97c8..347d10947d 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -143,9 +143,9 @@ class Solution { return resSet.stream().mapToInt(x -> x).toArray(); //方法2:另外申请一个数组存放setRes中的元素,最后返回数组 - int[] arr = new int[setRes.size()]; + int[] arr = new int[resSet.size()]; int j = 0; - for(int i : setRes){ + for(int i : resSet){ arr[j++] = i; } From 77aef4b18badbd6e04e0d78d834af7fea2613027 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Sun, 5 Mar 2023 10:19:41 +0800 Subject: [PATCH 0011/1533] =?UTF-8?q?Update=20=E5=9B=9E=E6=BA=AF=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6?= =?UTF-8?q?=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\347\247\215\345\206\231\346\263\225.md" | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 7386215681..2265a89b88 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -525,10 +525,41 @@ function permuteUnique(nums: number[]): number[][] { }; ``` -Go: - - +Rust: + +**90.子集II**: + +```rust +use std::collections::HashSet; +impl Solution { + pub fn subsets_with_dup(mut nums: Vec) -> Vec> { + let mut res = vec![]; + let mut path = vec![]; + nums.sort(); + Self::backtracking(&nums, &mut path, &mut res, 0); + res + } + pub fn backtracking( + nums: &Vec, + path: &mut Vec, + res: &mut Vec>, + start_index: usize, + ) { + res.push(path.clone()); + let mut helper_set = HashSet::new(); + for i in start_index..nums.len() { + if helper_set.contains(&nums[i]) { + continue; + } + helper_set.insert(nums[i]); + path.push(nums[i]); + Self::backtracking(nums, path, res, i + 1); + path.pop(); + } + } +} +```

From a30546d0777b41eb415d056f6f15dd4cc88e878e Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Sun, 5 Mar 2023 10:38:54 +0800 Subject: [PATCH 0012/1533] =?UTF-8?q?Update=20=E5=9B=9E=E6=BA=AF=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6?= =?UTF-8?q?=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\347\247\215\345\206\231\346\263\225.md" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 2265a89b88..08ba64dc05 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -561,6 +561,49 @@ impl Solution { } ``` +**40. 组合总和 II** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn backtracking( + candidates: &Vec, + target: i32, + sum: i32, + path: &mut Vec, + res: &mut Vec>, + start_index: usize, + ) { + if sum > target { + return; + } + if sum == target { + res.push(path.clone()); + } + let mut helper_set = HashSet::new(); + for i in start_index..candidates.len() { + if sum + candidates[i] <= target { + if helper_set.contains(&candidates[i]) { + continue; + } + helper_set.insert(candidates[i]); + path.push(candidates[i]); + Self::backtracking(candidates, target, sum + candidates[i], path, res, i + 1); + path.pop(); + } + } + } + + pub fn combination_sum2(mut candidates: Vec, target: i32) -> Vec> { + let mut res = vec![]; + let mut path = vec![]; + candidates.sort(); + Self::backtracking(&candidates, target, 0, &mut path, &mut res, 0); + res + } +} +``` +

From 4a203a3787e507cd28016779df9ffbb26e745a8b Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Sun, 5 Mar 2023 11:11:41 +0800 Subject: [PATCH 0013/1533] =?UTF-8?q?Update=20=E5=9B=9E=E6=BA=AF=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6?= =?UTF-8?q?=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\347\247\215\345\206\231\346\263\225.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 08ba64dc05..e60bd44abf 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -604,6 +604,44 @@ impl Solution { } ``` +**47. 全排列 II** + +```rust +use std::collections::HashSet; +impl Solution { + pub fn permute_unique(mut nums: Vec) -> Vec> { + let mut res = vec![]; + let mut path = vec![]; + let mut used = vec![false; nums.len()]; + Self::backtracking(&mut res, &mut path, &nums, &mut used); + res + } + pub fn backtracking( + res: &mut Vec>, + path: &mut Vec, + nums: &Vec, + used: &mut Vec, + ) { + if path.len() == nums.len() { + res.push(path.clone()); + return; + } + let mut helper_set = HashSet::new(); + for i in 0..nums.len() { + if used[i] || helper_set.contains(&nums[i]) { + continue; + } + helper_set.insert(nums[i]); + path.push(nums[i]); + used[i] = true; + Self::backtracking(res, path, nums, used); + used[i] = false; + path.pop(); + } + } +} +``` +

From 1d44a17aaa733ef95a7eafde6f155cb5ea1a1929 Mon Sep 17 00:00:00 2001 From: ruyubai1 <104716559+ruyubai1@users.noreply.github.com> Date: Sun, 5 Mar 2023 18:52:47 +0100 Subject: [PATCH 0014/1533] =?UTF-8?q?python=E4=B8=80=E7=BB=B4=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=88=9D=E5=A7=8B=E5=8C=96=E9=95=BF=E5=BA=A6=E5=BA=94?= =?UTF-8?q?=E4=B8=BAtarget+1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 45dd289a21..dfb327ec1f 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -302,7 +302,7 @@ class Solution: target = sum(nums) if target % 2 == 1: return False target //= 2 - dp = [0] * (len(nums) + 1) + dp = [0] * (target + 1) for i in range(len(nums)): for j in range(target, nums[i] - 1, -1): dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) From 5e08d1b8108b9c99cf1c2e8bb1ae12b5f6bcf822 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Sun, 5 Mar 2023 16:21:15 -0500 Subject: [PATCH 0015/1533] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改python版本为:fast先走n+1步 (旧版本是fast走n步,然后判断fast.next!=None),这样与上面的讲解一致 --- ...5\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index b0641b5f08..4e4474cabd 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -129,10 +129,10 @@ class Solution: head_dummy.next = head slow, fast = head_dummy, head_dummy - while(n!=0): #fast先往前走n步 + while(n>=0): #fast先往前走n+1步 fast = fast.next n -= 1 - while(fast.next!=None): + while(fast!=None): slow = slow.next fast = fast.next #fast 走到结尾后,slow的下一个节点为倒数第N个节点 From 98a8a8b8b1c8068d1cc291b0770369318d867338 Mon Sep 17 00:00:00 2001 From: Du Zongwei <894588765@qq.com> Date: Mon, 6 Mar 2023 09:48:49 +0800 Subject: [PATCH 0016/1533] =?UTF-8?q?modified=20ACM=E4=B8=8B=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E4=BA=8C=E5=8F=89=E6=A0=91=20Python?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\273\272\344\272\214\345\217\211\346\240\221.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" index 42bf7af0ba..b64464031f 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" @@ -305,11 +305,12 @@ def construct_binary_tree(nums: []) -> TreeNode: Tree.append(node) if i == 0: root = node + # 直接判断2*i+2 Date: Mon, 6 Mar 2023 10:52:35 +0800 Subject: [PATCH 0017/1533] =?UTF-8?q?Update=200455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5.\345\210\206\345\217\221\351\245\274\345\271\262.md" | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 2437582cc6..63525b0394 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -219,19 +219,17 @@ func findContentChildren(g []int, s []int) int { ### Rust ```rust -pub fn find_content_children(children: Vec, cookie: Vec) -> i32 { - let mut children = children; - let mut cookies = cookie; +pub fn find_content_children(mut children: Vec, mut cookie: Vec) -> i32 { children.sort(); cookies.sort(); - let (mut child, mut cookie) = (0usize, 0usize); + let (mut child, mut cookie) = (0, 0); while child < children.len() && cookie < cookies.len() { // 优先选择最小饼干喂饱孩子 if children[child] <= cookies[cookie] { child += 1; } - cookie += 1 + cookie += 1; } child as i32 } From 2f1cd225eea3fff9f76c2054f7b3472b7ed24cfd Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 6 Mar 2023 11:41:46 +0800 Subject: [PATCH 0018/1533] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\212\250\345\272\217\345\210\227.md" | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index efb9c6b65a..d4daccc586 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -462,21 +462,19 @@ var wiggleMaxLength = function(nums) { ```Rust impl Solution { pub fn wiggle_max_length(nums: Vec) -> i32 { - let len = nums.len() as usize; - if len <= 1 { - return len as i32; + if nums.len() == 1 { + return 1; } - let mut preDiff = 0; - let mut curDiff = 0; - let mut result = 1; - for i in 0..len-1 { - curDiff = nums[i+1] - nums[i]; - if (preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0) { - result += 1; - preDiff = curDiff; + let mut res = 1; + let mut pre_diff = 0; + for i in 0..nums.len() - 1 { + let cur_diff = nums[i + 1] - nums[i]; + if (pre_diff <= 0 && cur_diff > 0) || (pre_diff >= 0 && cur_diff < 0) { + res += 1; + pre_diff = cur_diff; } } - result + res } } ``` From 30b5b629a288a380f21dcda11d6f8be5d9e17f10 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 6 Mar 2023 12:24:23 +0800 Subject: [PATCH 0019/1533] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\212\250\345\272\217\345\210\227.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index d4daccc586..0199d83b5d 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -479,6 +479,30 @@ impl Solution { } ``` +**动态规划** + +```rust +impl Solution { + pub fn wiggle_max_length(nums: Vec) -> i32 { + if nums.len() == 1 { + return 1; + } + let (mut down, mut up) = (1, 1); + for i in 1..nums.len() { + // i - 1 为峰顶 + if nums[i] < nums[i - 1] { + down = down.max(up + 1); + } + // i - 1 为峰谷 + if nums[i] > nums[i - 1] { + up = up.max(down + 1); + } + } + down.max(up) + } +} +``` + ### C **贪心** From 84326d9d6154626f907cddedc59d37f35acb44a6 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 6 Mar 2023 01:35:47 -0500 Subject: [PATCH 0020/1533] =?UTF-8?q?Update=200349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入python使用数组方法 --- ...73\204\347\232\204\344\272\244\351\233\206.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 347d10947d..b7365e6e5d 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -169,6 +169,21 @@ class Solution: val_dict[num] = 0 return ans + +class Solution: # 使用数组方法 + def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: + count1 = [0]*1001 + count2 = [0]*1001 + result = [] + for i in range(len(nums1)): + count1[nums1[i]]+=1 + for j in range(len(nums2)): + count2[nums2[j]]+=1 + for k in range(1001): + if count1[k]*count2[k]>0: + result.append(k) + return result + ``` From 3a16650abe53b59bb9ab99c931993ef1f253d22c Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 6 Mar 2023 01:49:19 -0500 Subject: [PATCH 0021/1533] =?UTF-8?q?Update=200202.=E5=BF=AB=E4=B9=90?= =?UTF-8?q?=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python的另一种写法 - 通过字符串来计算各位平方和 --- .../0202.\345\277\253\344\271\220\346\225\260.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 1a7d53a343..2687574fdc 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -132,6 +132,19 @@ class Solution: else: record.add(n) +# python的另一种写法 - 通过字符串来计算各位平方和 +class Solution: + def isHappy(self, n: int) -> bool: + record = [] + while n not in record: + record.append(n) + newn = 0 + nn = str(n) + for i in nn: + newn+=int(i)**2 + if newn==1: return True + n = newn + return False ``` Go: From 3bc935ccc716f316040e0da5f5370cfcc6ce708c Mon Sep 17 00:00:00 2001 From: StriveDD Date: Mon, 6 Mar 2023 16:32:25 +0800 Subject: [PATCH 0022/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00130.=E8=A2=AB?= =?UTF-8?q?=E5=9B=B4=E7=BB=95=E7=9A=84=E5=8C=BA=E5=9F=9F=E7=9A=84Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E7=9A=84=20BFS=E3=80=81DFS=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C0797.=E6=89=80=E6=9C=89=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E7=9A=84=E8=B7=AF=E5=BE=84=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=20DFS=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\232\204\345\214\272\345\237\237.md" | 214 ++++++++++++++++++ ...75\347\232\204\350\267\257\345\276\204.md" | 30 +++ 2 files changed, 244 insertions(+) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index c2aa569693..7afa71b40f 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -84,7 +84,221 @@ public: ## 其他语言版本 +### Java + +```Java +// 广度优先遍历 +// 使用 visited 数组进行标记 +class Solution { + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向 + + public void solve(char[][] board) { + // rowSize:行的长度,colSize:列的长度 + int rowSize = board.length, colSize = board[0].length; + boolean[][] visited = new boolean[rowSize][colSize]; + Queue queue = new ArrayDeque<>(); + // 从左侧边,和右侧边遍历 + for (int row = 0; row < rowSize; row++) { + if (board[row][0] == 'O') { + visited[row][0] = true; + queue.add(new int[]{row, 0}); + } + if (board[row][colSize - 1] == 'O') { + visited[row][colSize - 1] = true; + queue.add(new int[]{row, colSize - 1}); + } + } + // 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角 + // 所以在遍历上边和下边时可以不用遍历四个角 + for (int col = 1; col < colSize - 1; col++) { + if (board[0][col] == 'O') { + visited[0][col] = true; + queue.add(new int[]{0, col}); + } + if (board[rowSize - 1][col] == 'O') { + visited[rowSize - 1][col] = true; + queue.add(new int[]{rowSize - 1, col}); + } + } + // 广度优先遍历,把没有被 'X' 包围的 'O' 进行标记 + while (!queue.isEmpty()) { + int[] current = queue.poll(); + for (int[] pos: position) { + int row = current[0] + pos[0], col = current[1] + pos[1]; + // 如果范围越界、位置已被访问过、该位置的值不是 'O',就直接跳过 + if (row < 0 || row >= rowSize || col < 0 || col >= colSize) continue; + if (visited[row][col] || board[row][col] != 'O') continue; + visited[row][col] = true; + queue.add(new int[]{row, col}); + } + } + // 遍历数组,把没有被标记的 'O' 修改成 'X' + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (board[row][col] == 'O' && !visited[row][col]) board[row][col] = 'X'; + } + } + } +} +``` +```Java +// 广度优先遍历 +// 直接修改 board 的值为其他特殊值 +class Solution { + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向 + + public void solve(char[][] board) { + // rowSize:行的长度,colSize:列的长度 + int rowSize = board.length, colSize = board[0].length; + Queue queue = new ArrayDeque<>(); + // 从左侧边,和右侧边遍历 + for (int row = 0; row < rowSize; row++) { + if (board[row][0] == 'O') + queue.add(new int[]{row, 0}); + if (board[row][colSize - 1] == 'O') + queue.add(new int[]{row, colSize - 1}); + } + // 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角 + // 所以在遍历上边和下边时可以不用遍历四个角 + for (int col = 1; col < colSize - 1; col++) { + if (board[0][col] == 'O') + queue.add(new int[]{0, col}); + if (board[rowSize - 1][col] == 'O') + queue.add(new int[]{rowSize - 1, col}); + } + // 广度优先遍历,把没有被 'X' 包围的 'O' 修改成特殊值 + while (!queue.isEmpty()) { + int[] current = queue.poll(); + board[current[0]][current[1]] = 'A'; + for (int[] pos: position) { + int row = current[0] + pos[0], col = current[1] + pos[1]; + // 如果范围越界、该位置的值不是 'O',就直接跳过 + if (row < 0 || row >= rowSize || col < 0 || col >= colSize) continue; + if (board[row][col] != 'O') continue; + queue.add(new int[]{row, col}); + } + } + // 遍历数组,把 'O' 修改成 'X',特殊值修改成 'O' + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (board[row][col] == 'A') board[row][col] = 'O'; + else if (board[row][col] == 'O') board[row][col] = 'X'; + } + } + } +} +``` +```Java +// 深度优先遍历 +// 使用 visited 数组进行标记 +class Solution { + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向 + + public void dfs(char[][] board, int row, int col, boolean[][] visited) { + for (int[] pos: position) { + int nextRow = row + pos[0], nextCol = col + pos[1]; + // 位置越界 + if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length) + continue; + // 位置已被访问过、新位置值不是 'O' + if (visited[nextRow][nextCol] || board[nextRow][nextCol] != 'O') continue; + visited[nextRow][nextCol] = true; + dfs(board, nextRow, nextCol, visited); + } + } + + public void solve(char[][] board) { + int rowSize = board.length, colSize = board[0].length; + boolean[][] visited = new boolean[rowSize][colSize]; + // 从左侧遍、右侧遍遍历 + for (int row = 0; row < rowSize; row++) { + if (board[row][0] == 'O' && !visited[row][0]) { + visited[row][0] = true; + dfs(board, row, 0, visited); + } + if (board[row][colSize - 1] == 'O' && !visited[row][colSize - 1]) { + visited[row][colSize - 1] = true; + dfs(board, row, colSize - 1, visited); + } + } + // 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角 + // 所以在遍历上边和下边时可以不用遍历四个角 + for (int col = 1; col < colSize - 1; col++) { + if (board[0][col] == 'O' && !visited[0][col]) { + visited[0][col] = true; + dfs(board, 0, col, visited); + } + if (board[rowSize - 1][col] == 'O' && !visited[rowSize - 1][col]) { + visited[rowSize - 1][col] = true; + dfs(board, rowSize - 1, col, visited); + } + } + // 遍历数组,把没有被标记的 'O' 修改成 'X' + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (board[row][col] == 'O' && !visited[row][col]) board[row][col] = 'X'; + } + } + } +} +``` +```Java +// 深度优先遍历 +// // 直接修改 board 的值为其他特殊值 +class Solution { + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向 + + public void dfs(char[][] board, int row, int col) { + for (int[] pos: position) { + int nextRow = row + pos[0], nextCol = col + pos[1]; + // 位置越界 + if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length) + continue; + // 新位置值不是 'O' + if (board[nextRow][nextCol] != 'O') continue; + board[nextRow][nextCol] = 'A'; // 修改为特殊值 + dfs(board, nextRow, nextCol); + } + } + + public void solve(char[][] board) { + int rowSize = board.length, colSize = board[0].length; + // 从左侧遍、右侧遍遍历 + for (int row = 0; row < rowSize; row++) { + if (board[row][0] == 'O') { + board[row][0] = 'A'; + dfs(board, row, 0); + } + if (board[row][colSize - 1] == 'O') { + board[row][colSize - 1] = 'A'; + dfs(board, row, colSize - 1); + } + } + // 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角 + // 所以在遍历上边和下边时可以不用遍历四个角 + for (int col = 1; col < colSize - 1; col++) { + if (board[0][col] == 'O') { + board[0][col] = 'A'; + dfs(board, 0, col); + } + if (board[rowSize - 1][col] == 'O') { + board[rowSize - 1][col] = 'A'; + dfs(board, rowSize - 1, col); + } + } + // 遍历数组,把 'O' 修改成 'X',特殊值修改成 'O' + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (board[row][col] == 'O') board[row][col] = 'X'; + else if (board[row][col] == 'A') board[row][col] = 'O'; + } + } + } +} +``` +

+ diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index eb3f7fb422..89643e0490 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -161,6 +161,35 @@ public: ## Java +```Java +// 深度优先遍历 +class Solution { + List> ans; // 用来存放满足条件的路径 + List cnt; // 用来保存 dfs 过程中的节点值 + + public void dfs(int[][] graph, int node) { + if (node == graph.length - 1) { // 如果当前节点是 n - 1,那么就保存这条路径 + ans.add(new ArrayList<>(cnt)); + return; + } + for (int index = 0; index < graph[node].length; index++) { + int nextNode = graph[node][index]; + cnt.add(nextNode); + dfs(graph, nextNode); + cnt.remove(cnt.size() - 1); // 回溯 + } + } + + public List> allPathsSourceTarget(int[][] graph) { + ans = new ArrayList<>(); + cnt = new ArrayList<>(); + cnt.add(0); // 注意,0 号节点要加入 cnt 数组中 + dfs(graph, 0); + return ans; + } +} +``` + ## Python ```python class Solution: @@ -192,3 +221,4 @@ class Solution: + From aea8bee9543fd699068fd413167a682d6dd967a8 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Mon, 6 Mar 2023 19:50:04 -0600 Subject: [PATCH 0023/1533] =?UTF-8?q?=E5=88=A0=E9=99=A4typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index eec4183d2f..2e3f4fee63 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -162,7 +162,7 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 有哪些来源可以推出dp[j]呢? -只要搞到nums[i]),凑成dp[j]就有dp[j - nums[i]] 种方法。 +只要搞到nums[i],凑成dp[j]就有dp[j - nums[i]] 种方法。 例如:dp[j],j 为5, From 202013fd817e303e14d5f43a324b55b9c9810ead Mon Sep 17 00:00:00 2001 From: Jeremy Feng <44312563+jeremy-feng@users.noreply.github.com> Date: Tue, 7 Mar 2023 17:46:03 +0800 Subject: [PATCH 0024/1533] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- "problems/0070.\347\210\254\346\245\274\346\242\257.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 14aeef0142..d4a7881ddd 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -72,7 +72,7 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 3. dp数组如何初始化 -在回顾一下dp[i]的定义:爬到第i层楼梯,有dp[i]中方法。 +再回顾一下dp[i]的定义:爬到第i层楼梯,有dp[i]中方法。 那么i为0,dp[i]应该是多少呢,这个可以有很多解释,但基本都是直接奔着答案去解释的。 From 200e14cbdedff221fe93387e709c8b349986ac60 Mon Sep 17 00:00:00 2001 From: GODVvVZzz <2662446324@qq.com> Date: Wed, 8 Mar 2023 20:07:45 +0800 Subject: [PATCH 0025/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9Java=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\350\203\214\345\214\205\347\211\210\346\234\254.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 3093c83325..41c2e6162d 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -128,12 +128,12 @@ Java: class Solution { public int climbStairs(int n) { int[] dp = new int[n + 1]; - int[] weight = {1,2}; + int m = 2; dp[0] = 1; - for (int i = 0; i <= n; i++) { - for (int j = 0; j < weight.length; j++) { - if (i >= weight[j]) dp[i] += dp[i - weight[j]]; + for (int i = 1; i <= n; i++) { // 遍历背包 + for (int j = 1; j <= m; j++) { //遍历物品 + if (i >= j) dp[i] += dp[i - j]; } } @@ -227,3 +227,4 @@ function climbStairs(n: number): number { + From 77eeed96bcfb0375e09d1a8f2d4e7a6afe9cd615 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 9 Mar 2023 00:13:05 +0800 Subject: [PATCH 0026/1533] =?UTF-8?q?update=200704.=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=9F=A5=E6=89=BE:=20=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...704.\344\272\214\345\210\206\346\237\245\346\211\276.md" | 6 ++++++ 1 file changed, 6 insertions(+) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index a02bf7a201..2d5525baa8 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -86,6 +86,9 @@ public: }; ``` +* 时间复杂度:O(log n) +* 空间复杂度:O(1) + ### 二分法第二种写法 @@ -124,6 +127,9 @@ public: } }; ``` +* 时间复杂度:O(log n) +* 空间复杂度:O(1) + ## 总结 From 337bf59ee6f703345d8e3371ec7e6ea6b8a013a5 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 9 Mar 2023 00:20:33 +0800 Subject: [PATCH 0027/1533] =?UTF-8?q?update=200059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index d9a656b9f6..b4dad9c3de 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -117,6 +117,9 @@ public: }; ``` +* 时间复杂度 O(n^2): 模拟遍历二维矩阵的时间 +* 空间复杂度 O(1) + ## 类似题目 * 54.螺旋矩阵 From 60d585f4eeb2018483b1f80b8697e9ea80ff871e Mon Sep 17 00:00:00 2001 From: dongyunpeng Date: Thu, 9 Mar 2023 01:36:30 +0800 Subject: [PATCH 0028/1533] =?UTF-8?q?=E5=AE=8C=E5=96=840707.=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8C++=E7=89=88=E6=9C=AC=EF=BC=8C?= =?UTF-8?q?=E5=9C=A8delete=E6=8C=87=E9=92=88=E4=B9=8B=E5=90=8E=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=E4=B8=BAnull=EF=BC=8C=E5=9C=A8=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E4=B8=AD=20=E8=AF=B4=E6=98=8E=E4=BA=86=E9=87=8E=E6=8C=87?= =?UTF-8?q?=E9=92=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 6 ++++++ 1 file changed, 6 insertions(+) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 43ed262296..de1e7eb452 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -133,6 +133,11 @@ public: LinkedNode* tmp = cur->next; cur->next = cur->next->next; delete tmp; + //delete命令指示释放了tmp指针原本所指的那部分内存, + //被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后, + //如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针 + //如果之后的程序不小心使用了tmp,会指向难以预想的内存空间 + tmp=nullptr; _size--; } @@ -1447,3 +1452,4 @@ impl MyLinkedList { + From 4164ddbb69da491b05f201a39b7b4c6233fad929 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Fri, 10 Mar 2023 11:11:54 +0800 Subject: [PATCH 0029/1533] =?UTF-8?q?Update=200122.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...200\344\275\263\346\227\266\346\234\272II.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index d094da488e..a863afe7b4 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -274,6 +274,7 @@ const maxProfit = (prices) => { ### TypeScript: +贪心 ```typescript function maxProfit(prices: number[]): number { let resProfit: number = 0; @@ -284,6 +285,21 @@ function maxProfit(prices: number[]): number { }; ``` +动态规划 +```typescript +function maxProfit(prices: number[]): number { + const dp = Array(prices.length) + .fill(0) + .map(() => Array(2).fill(0)) + dp[0][0] = -prices[0] + for (let i = 1; i < prices.length; i++) { + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]) + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]) + } + return dp[prices.length - 1][1] +} +``` + ### Rust 贪心: From 17cb4b45c7c53cf50ba91815b0f8feb9a5aa4e5a Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 10 Mar 2023 14:02:32 +0800 Subject: [PATCH 0030/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=9B=BE=E5=BA=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\226\207\345\255\220\344\270\262.md" | 4 +- ...27\346\257\215\347\273\204\345\220\210.md" | 4 +- ...4N\344\270\252\350\212\202\347\202\271.md" | 3 +- ...10\347\232\204\346\213\254\345\217\267.md" | 9 +- ...22\345\205\245\344\275\215\347\275\256.md" | 47 +++-- ...7.\350\247\243\346\225\260\347\213\254.md" | 50 +++--- ...04\345\220\210\346\200\273\345\222\214.md" | 66 ++++--- ...\345\220\210\346\200\273\345\222\214II.md" | 4 +- ...2.\346\216\245\351\233\250\346\260\264.md" | 99 +++++----- ...\350\267\203\346\270\270\346\210\217II.md" | 7 +- ...6.\345\205\250\346\216\222\345\210\227.md" | 2 +- ...\345\205\250\346\216\222\345\210\227II.md" | 32 ++-- "problems/0051.N\347\232\207\345\220\216.md" | 4 +- .../0052.N\347\232\207\345\220\216II.md" | 4 +- ...01\350\247\204\345\210\222\357\274\211.md" | 2 +- ...72\346\227\213\347\237\251\351\230\265.md" | 3 +- ...63\350\267\203\346\270\270\346\210\217.md" | 3 +- ...10\345\271\266\345\214\272\351\227\264.md" | 2 +- ...15\345\220\214\350\267\257\345\276\204.md" | 46 +++-- ...\345\220\214\350\267\257\345\276\204II.md" | 50 +++--- ...0.\347\210\254\346\245\274\346\242\257.md" | 3 +- ...26\350\276\221\350\267\235\347\246\273.md" | 4 +- "problems/0077.\347\273\204\345\220\210.md" | 95 +++++----- ...04\345\220\210\344\274\230\345\214\226.md" | 2 +- "problems/0078.\345\255\220\351\233\206.md" | 4 +- "problems/0090.\345\255\220\351\233\206II.md" | 2 +- ...\345\216\237IP\345\234\260\345\235\200.md" | 6 +- ...11\346\220\234\347\264\242\346\240\221.md" | 22 ++- ...11\346\220\234\347\264\242\346\240\221.md" | 4 +- ...60\344\272\214\345\217\211\346\240\221.md" | 4 +- ...02\345\272\217\351\201\215\345\216\206.md" | 170 ++++++++++-------- ...00\345\244\247\346\267\261\345\272\246.md" | 8 +- ...40\344\272\214\345\217\211\346\240\221.md" | 84 ++++----- ...11\346\220\234\347\264\242\346\240\221.md" | 3 +- ...41\344\272\214\345\217\211\346\240\221.md" | 41 +++-- ...00\345\260\217\346\267\261\345\272\246.md" | 7 +- ...57\345\276\204\346\200\273\345\222\214.md" | 117 +++++++----- ...00\344\275\263\346\227\266\346\234\272.md" | 3 +- ...\344\275\263\346\227\266\346\234\272II.md" | 3 +- ...344\275\263\346\227\266\346\234\272III.md" | 3 +- ...\345\233\236\346\226\207\344\270\262II.md" | 2 +- ...06\345\217\221\347\263\226\346\236\234.md" | 6 +- ...25\350\257\215\346\213\206\345\210\206.md" | 3 +- ...\345\275\242\351\223\276\350\241\250II.md" | 19 +- ...\344\275\263\346\227\266\346\234\272IV.md" | 2 +- ...23\345\256\266\345\212\253\350\210\215.md" | 2 +- ...76\350\241\250\345\205\203\347\264\240.md" | 53 ++++-- ...73\350\275\254\351\223\276\350\241\250.md" | 3 +- ...04\345\255\220\346\225\260\347\273\204.md" | 2 +- ...\345\256\266\345\212\253\350\210\215II.md" | 6 +- ...345\220\210\346\200\273\345\222\214III.md" | 21 ++- ...02\347\202\271\344\270\252\346\225\260.md" | 6 +- ...54\344\272\214\345\217\211\346\240\221.md" | 6 +- ...54\345\205\261\347\245\226\345\205\210.md" | 3 +- ...54\345\205\261\347\245\226\345\205\210.md" | 9 +- ...00\346\234\211\350\267\257\345\276\204.md" | 4 +- ...50\345\271\263\346\226\271\346\225\260.md" | 3 +- ...07\345\255\220\345\272\217\345\210\227.md" | 2 +- ...53\345\206\267\345\206\273\346\234\237.md" | 5 +- ...66\351\222\261\345\205\221\346\215\242.md" | 2 +- ...11\346\216\222\350\241\214\347\250\213.md" | 6 +- ...345\256\266\345\212\253\350\210\215III.md" | 3 +- ...64\346\225\260\346\213\206\345\210\206.md" | 2 +- ...04\347\232\204\344\272\244\351\233\206.md" | 2 +- ...06\345\212\250\345\272\217\345\210\227.md" | 4 +- ...10\346\200\273\345\222\214\342\205\243.md" | 2 +- ...55\345\255\220\345\272\217\345\210\227.md" | 9 +- ...66\345\255\220\344\271\213\345\222\214.md" | 6 +- ...15\345\273\272\351\230\237\345\210\227.md" | 2 +- ...11\345\222\214\345\255\220\351\233\206.md" | 3 +- ...55\347\232\204\350\212\202\347\202\271.md" | 3 +- ...25\347\210\206\346\260\224\347\220\203.md" | 2 +- ...4.\344\270\200\345\222\214\351\233\266.md" | 5 +- ...36\345\255\220\345\272\217\345\210\227.md" | 7 +- ...4.\347\233\256\346\240\207\345\222\214.md" | 2 +- ...55\347\232\204\344\274\227\346\225\260.md" | 4 +- ...13\350\247\222\347\232\204\345\200\274.md" | 5 +- ...07\345\255\220\345\272\217\345\210\227.md" | 20 ++- ...\351\222\261\345\205\221\346\215\242II.md" | 2 +- ...17\347\273\235\345\257\271\345\267\256.md" | 4 +- ...72\347\264\257\345\212\240\346\240\221.md" | 6 +- ...40\351\231\244\346\223\215\344\275\234.md" | 2 +- ...66\344\272\214\345\217\211\346\240\221.md" | 2 +- ...36\346\226\207\345\255\220\344\270\262.md" | 4 +- ...47\344\272\214\345\217\211\346\240\221.md" | 2 +- ...11\346\220\234\347\264\242\346\240\221.md" | 35 ++-- ...27\347\232\204\344\270\252\346\225\260.md" | 2 +- ...22\345\242\236\345\272\217\345\210\227.md" | 3 +- ...55\347\232\204\346\220\234\347\264\242.md" | 5 +- ...22\345\205\245\346\223\215\344\275\234.md" | 3 +- ...14\345\210\206\346\237\245\346\211\276.md" | 5 +- ...76\350\256\241\351\223\276\350\241\250.md" | 6 +- ...15\345\255\220\346\225\260\347\273\204.md" | 6 +- ...17\346\227\245\346\270\251\345\272\246.md" | 90 +++++----- ...27\346\257\215\345\214\272\351\227\264.md" | 3 +- ...47\344\272\214\345\217\211\346\240\221.md" | 57 +++--- ...70\344\272\244\347\232\204\347\272\277.md" | 3 +- ...\347\232\204\351\207\215\351\207\217II.md" | 2 +- ...61\345\255\220\345\272\217\345\210\227.md" | 5 +- ...57\345\244\232\345\244\247\357\274\237.md" | 15 +- ...06\350\256\272\345\237\272\347\241\200.md" | 40 +++-- ...55\344\273\243\351\201\215\345\216\206.md" | 2 +- ...50\350\277\231\351\207\214\357\274\201.md" | 6 +- ...57\345\244\232\345\244\247\357\274\237.md" | 14 +- ...43\347\240\201\351\243\216\346\240\274.md" | 2 +- ...50\350\277\231\351\207\214\357\274\201.md" | 6 +- ...10\350\200\227\344\271\210\357\274\237.md" | 12 +- ...17\345\221\230\347\256\200\345\216\206.md" | 2 +- ...02\345\272\246\345\210\206\346\236\220.md" | 9 +- ...15\346\235\202\345\272\246\357\274\201.md" | 4 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 11 +- ...50\346\234\253\346\200\273\347\273\223.md" | 16 +- ...50\346\234\253\346\200\273\347\273\223.md" | 11 +- ...50\346\234\253\346\200\273\347\273\223.md" | 3 +- ...50\346\234\253\346\200\273\347\273\223.md" | 5 +- ...50\346\234\253\346\200\273\347\273\223.md" | 10 +- ...50\346\234\253\346\200\273\347\273\223.md" | 13 +- ...50\346\234\253\346\200\273\347\273\223.md" | 10 +- ...50\346\234\253\346\200\273\347\273\223.md" | 5 +- ...50\346\234\253\346\200\273\347\273\223.md" | 13 +- ...50\346\234\253\346\200\273\347\273\223.md" | 8 +- ...06\350\256\272\345\237\272\347\241\200.md" | 34 ++-- ...36\346\272\257\346\200\273\347\273\223.md" | 86 +++++---- ...00\347\247\215\345\206\231\346\263\225.md" | 5 +- ...06\350\256\272\345\237\272\347\241\200.md" | 4 +- ...06\350\256\272\345\237\272\347\241\200.md" | 6 +- ...06\350\256\272\345\237\272\347\241\200.md" | 7 +- ...06\350\256\262\350\247\243\357\274\211.md" | 15 +- ...05\346\200\273\347\273\223\347\257\207.md" | 2 +- ...47\241\20001\350\203\214\345\214\205-1.md" | 72 ++++---- ...47\241\20001\350\203\214\345\214\205-2.md" | 2 +- ...14\345\205\250\350\203\214\345\214\205.md" | 6 +- ...06\350\256\272\345\237\272\347\241\200.md" | 21 ++- 134 files changed, 1169 insertions(+), 829 deletions(-) diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index 4d48f18458..b1987b87b3 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -108,7 +108,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://img-blog.csdnimg.cn/20210121171032473.jpg) +![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -142,7 +142,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://img-blog.csdnimg.cn/20210121171059951.jpg) +![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951.jpg) **注意因为dp[i][j]的定义,所以j一定是大于等于i的,那么在填充dp[i][j]的时候一定是只填充右上半部分**。 diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index df038806a5..d113549716 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -13,7 +13,7 @@ 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 -![17.电话号码的字母组合](https://img-blog.csdnimg.cn/2020102916424043.png) +![17.电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/2020102916424043.png) 示例: * 输入:"23" @@ -66,7 +66,7 @@ const string letterMap[10] = { 例如:输入:"23",抽象为树形结构,如图所示: -![17. 电话号码的字母组合](https://img-blog.csdnimg.cn/20201123200304469.png) +![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123200304469.png) 图中可以看出遍历的深度,就是输入"23"的长度,而叶子节点就是我们要收集的结果,输出["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。 diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index b0641b5f08..48c6dd5de4 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -17,7 +17,8 @@ 示例 1: -![19.删除链表的倒数第N个节点](https://img-blog.csdnimg.cn/20210510085957392.png) + +![19.删除链表的倒数第N个节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20210510085957392.png) 输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index 737fab86eb..a25129c52a 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -80,14 +80,17 @@ cd a/b/c/../../ 先来分析一下 这里有三种不匹配的情况, + 1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。 -![括号匹配1](https://img-blog.csdnimg.cn/2020080915505387.png) +![括号匹配1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020080915505387.png) 2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。 -![括号匹配2](https://img-blog.csdnimg.cn/20200809155107397.png) +![括号匹配2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155107397.png) 3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。 -![括号匹配3](https://img-blog.csdnimg.cn/20200809155115779.png) +![括号匹配3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155115779.png) + + 我们的代码只要覆盖了这三种不匹配的情况,就不会出问题,可以看出 动手之前分析好题目的重要性。 diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 4d9ee74fb2..58340c21a1 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -1,3 +1,4 @@ +

@@ -7,6 +8,7 @@ + # 35.搜索插入位置 [力扣题目链接](https://leetcode.cn/problems/search-insert-position/) @@ -16,18 +18,22 @@ 你可以假设数组中无重复元素。 示例 1: + * 输入: [1,3,5,6], 5 * 输出: 2 -示例 2: +示例 2: + * 输入: [1,3,5,6], 2 * 输出: 1 示例 3: + * 输入: [1,3,5,6], 7 * 输出: 4 示例 4: + * 输入: [1,3,5,6], 0 * 输出: 0 @@ -37,7 +43,7 @@ 这道题目,要在数组中插入目标值,无非是这四种情况。 -![35_搜索插入位置3](https://img-blog.csdnimg.cn/20201216232148471.png) +![35_搜索插入位置3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232148471.png) * 目标值在数组所有元素之前 * 目标值等于数组中某一个元素 @@ -78,13 +84,14 @@ public: 效率如下: -![35_搜索插入位置](https://img-blog.csdnimg.cn/20201216232127268.png) +![35_搜索插入位置](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232127268.png) ### 二分法 -既然暴力解法的时间复杂度是$O(n)$,就要尝试一下使用二分查找法。 +既然暴力解法的时间复杂度是O(n),就要尝试一下使用二分查找法。 -![35_搜索插入位置4](https://img-blog.csdnimg.cn/202012162326354.png) + +![35_搜索插入位置4](https://code-thinking-1253855093.file.myqcloud.com/pics/202012162326354.png) 大家注意这道题目的前提是数组是有序数组,这也是使用二分查找的基础条件。 @@ -94,7 +101,7 @@ public: 大体讲解一下二分法的思路,这里来举一个例子,例如在这个数组中,使用二分法寻找元素为5的位置,并返回其下标。 -![35_搜索插入位置5](https://img-blog.csdnimg.cn/20201216232659199.png) +![35_搜索插入位置5](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232659199.png) 二分查找涉及的很多的边界条件,逻辑比较简单,就是写不好。 @@ -145,7 +152,7 @@ public: * 空间复杂度:O(1) 效率如下: -![35_搜索插入位置2](https://img-blog.csdnimg.cn/2020121623272877.png) +![35_搜索插入位置2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020121623272877.png) ### 二分法第二种写法 @@ -199,7 +206,7 @@ public: ## 其他语言版本 -### Java +### Java ```java class Solution { @@ -226,11 +233,12 @@ class Solution { } } ``` + ```java //第二种二分法:左闭右开 public int searchInsert(int[] nums, int target) { int left = 0; - int right = nums.length; + int right = nums.length; while (left < right) { //左闭右开 [left, right) int middle = left + ((right - left) >> 1); if (nums[middle] > target) { @@ -290,7 +298,8 @@ impl Solution { } ``` -### Python +### Python + ```python class Solution: def searchInsert(self, nums: List[int], target: int) -> int: @@ -308,7 +317,8 @@ class Solution: return right + 1 ``` -### JavaScript +### JavaScript + ```js var searchInsert = function (nums, target) { let l = 0, r = nums.length - 1, ans = nums.length; @@ -350,7 +360,7 @@ function searchInsert(nums: number[], target: number): number { }; ``` -### Swift +### Swift ```swift // 暴力法 @@ -383,7 +393,9 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int { return right + 1 } ``` + ### Scala + ```scala object Solution { def searchInsert(nums: Array[Int], target: Int): Int = { @@ -404,7 +416,7 @@ object Solution { } ``` -### PHP +### PHP ```php // 二分法(1):[左闭右闭] @@ -429,11 +441,13 @@ function searchInsert($nums, $target) return $r + 1; } ``` + ### C + ```c //版本一 [left, right]左闭右闭区间 int searchInsert(int* nums, int numsSize, int target){ - //左闭右开区间 [0 , numsSize-1] + //左闭右开区间 [0 , numsSize-1] int left =0; int mid =0; int right = numsSize - 1; @@ -451,14 +465,15 @@ int searchInsert(int* nums, int numsSize, int target){ } } //数组中未找到target元素 - //target在数组所有元素之后,[left, right]是右闭区间,需要返回 right +1 + //target在数组所有元素之后,[left, right]是右闭区间,需要返回 right +1 return right + 1; } ``` + ```c //版本二 [left, right]左闭右开区间 int searchInsert(int* nums, int numsSize, int target){ - //左闭右开区间 [0 , numsSize) + //左闭右开区间 [0 , numsSize) int left =0; int mid =0; int right = numsSize; diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index 18a96d581b..6edd3c5b5e 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ 如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) # 37. 解数独 @@ -14,20 +16,21 @@ 编写一个程序,通过填充空格来解决数独问题。 一个数独的解法需遵循如下规则: -数字 1-9 在每一行只能出现一次。 -数字 1-9 在每一列只能出现一次。 -数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 -空白格用 '.' 表示。 +数字 1-9 在每一行只能出现一次。 +数字 1-9 在每一列只能出现一次。 +数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 +空白格用 '.' 表示。 -![解数独](https://img-blog.csdnimg.cn/202011171912586.png) +![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/202011171912586.png) 一个数独。 -![解数独](https://img-blog.csdnimg.cn/20201117191340669.png) +![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117191340669.png) 答案被标成红色。 提示: + * 给定的数独序列只包含数字 1-9 和字符 '.' 。 * 你可以假设给定的数独只有唯一解。 * 给定数独永远是 9x9 形式的。 @@ -54,7 +57,7 @@ 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://img-blog.csdnimg.cn/2020111720451790.png) +![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131816104.png) ### 回溯三部曲 @@ -85,7 +88,7 @@ bool backtracking(vector>& board) * 递归单层搜索逻辑 -![37.解数独](https://img-blog.csdnimg.cn/2020111720451790.png) +![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131822254.png) 在树形图中可以看出我们需要的是一个二维的递归(也就是两个for循环嵌套着递归) @@ -171,8 +174,8 @@ bool backtracking(vector>& board) { board[i][j] = '.'; // 回溯,撤销k } } - return false; // 9个数都试完了,都不行,那么就返回false - } + return false; // 9个数都试完了,都不行,那么就返回false + } } } return true; // 遍历完没有返回false,说明找到了合适棋盘位置了 @@ -223,7 +226,8 @@ public: ## 其他语言版本 -### Java +### Java + ```java class Solution { public void solveSudoku(char[][] board) { @@ -291,7 +295,8 @@ class Solution { } ``` -### Python +### Python + ```python class Solution: def solveSudoku(self, board: List[List[str]]) -> None: @@ -306,7 +311,7 @@ class Solution: for j in range(len(board[0])): # 遍历列 # 若空格内已有数字,跳过 if board[i][j] != '.': continue - for k in range(1, 10): + for k in range(1, 10): if self.is_valid(i, j, k, board): board[i][j] = str(k) if self.backtracking(board): return True @@ -334,7 +339,7 @@ class Solution: return True ``` -### Go +### Go ```go func solveSudoku(board [][]byte) { @@ -392,7 +397,8 @@ func isvalid(row, col int, k byte, board [][]byte) bool { -### Javascript +### Javascript + ```Javascript var solveSudoku = function(board) { function isValid(row, col, val, board) { @@ -433,7 +439,7 @@ var solveSudoku = function(board) { if (backTracking()) { return true } - + board[i][j] = `.` } } @@ -444,7 +450,7 @@ var solveSudoku = function(board) { } backTracking(board) return board - + }; ``` @@ -543,7 +549,7 @@ impl Solution { } ``` -### C +### C ```C bool isValid(char** board, int row, int col, int k) { @@ -660,9 +666,10 @@ func solveSudoku(_ board: inout [[Character]]) { ### Scala 详细写法: + ```scala object Solution { - + def solveSudoku(board: Array[Array[Char]]): Unit = { backtracking(board) } @@ -692,7 +699,7 @@ object Solution { return false } } - + // 列 for (j <- 0 until 9) { if (board(x)(j) == value) { @@ -717,9 +724,10 @@ object Solution { ``` 遵循Scala至简原则写法: + ```scala object Solution { - + def solveSudoku(board: Array[Array[Char]]): Unit = { backtracking(board) } diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index a3c62c53dd..c4ee5ca6ca 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -1,3 +1,4 @@ +

@@ -5,40 +6,43 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 39. 组合总和 [力扣题目链接](https://leetcode.cn/problems/combination-sum/) -给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 +给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 -candidates 中的数字可以无限制重复被选取。 +candidates 中的数字可以无限制重复被选取。 说明: -* 所有数字(包括 target)都是正整数。 -* 解集不能包含重复的组合。  +* 所有数字(包括 target)都是正整数。 +* 解集不能包含重复的组合。 示例 1: + * 输入:candidates = [2,3,6,7], target = 7, * 所求解集为: -[ + [ [7], [2,2,3] -] + ] + +示例 2: -示例 2: * 输入:candidates = [2,3,5], target = 8, * 所求解集为: -[ -  [2,2,2,2], -  [2,3,3], -  [3,5] -] + [ + [2,2,2,2], + [2,3,3], + [3,5] + ] # 算法公开课 **《代码随想录》算法视频公开课:[Leetcode:39. 组合总和讲解](https://www.bilibili.com/video/BV1KT4y1M7HJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - + # 思路 @@ -48,7 +52,7 @@ candidates 中的数字可以无限制重复被选取。 本题搜索的过程抽象成树形结构如下: -![39.组合总和](https://img-blog.csdnimg.cn/20201223170730367.png) +![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) 注意图中叶子节点的返回条件,因为本题没有组合数量要求,仅仅是总和的限制,所以递归没有层数的限制,只要选取的元素总和超过target,就返回! 而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。 @@ -83,7 +87,7 @@ void backtracking(vector& candidates, int target, int sum, int startIndex) 在如下树形结构中: -![39.组合总和](https://img-blog.csdnimg.cn/20201223170730367.png) +![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135337214.png) 从叶子节点可以清晰看到,终止只有两种情况,sum大于target和sum等于target。 @@ -156,7 +160,7 @@ public: 在这个树形结构中: -![39.组合总和](https://img-blog.csdnimg.cn/20201223170730367.png) +![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135342472.png) 以及上面的版本一的代码大家可以看到,对于sum已经大于target的情况,其实是依然进入了下一层递归,只是下一层递归结束判断的时候,会判断sum > target的话就返回。 @@ -169,7 +173,7 @@ public: 如图: -![39.组合总和1](https://img-blog.csdnimg.cn/20201223170809182.png) +![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170809182.png) for循环剪枝代码如下: @@ -235,7 +239,8 @@ public: # 其他语言版本 -## Java +## Java + ```Java // 剪枝优化 class Solution { @@ -264,8 +269,10 @@ class Solution { } ``` -## Python +## Python + **回溯** + ```python class Solution: def __init__(self): @@ -287,9 +294,9 @@ class Solution: self.paths.append(self.path[:]) # 因为是shallow copy,所以不能直接传入self.path return if sum_ > target: - return - - # 单层递归逻辑 + return + + # 单层递归逻辑 for i in range(start_index, len(candidates)): sum_ += candidates[i] self.path.append(candidates[i]) @@ -297,7 +304,9 @@ class Solution: sum_ -= candidates[i] # 回溯 self.path.pop() # 回溯 ``` + **剪枝回溯** + ```python class Solution: def __init__(self): @@ -321,11 +330,11 @@ class Solution: if sum_ == target: self.paths.append(self.path[:]) # 因为是shallow copy,所以不能直接传入self.path return - # 单层递归逻辑 + # 单层递归逻辑 # 如果本层 sum + condidates[i] > target,就提前结束遍历,剪枝 for i in range(start_index, len(candidates)): - if sum_ + candidates[i] > target: - return + if sum_ + candidates[i] > target: + return sum_ += candidates[i] self.path.append(candidates[i]) self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i-1 @@ -333,7 +342,8 @@ class Solution: self.path.pop() # 回溯 ``` -## Go +## Go + 主要在于递归中传递下一个数字 ```go @@ -423,7 +433,7 @@ function combinationSum(candidates: number[], target: number): number[][] { ```Rust impl Solution { pub fn backtracking(result: &mut Vec>, path: &mut Vec, candidates: &Vec, target: i32, mut sum: i32, start_index: usize) { - if sum == target { + if sum == target { result.push(path.to_vec()); return; } @@ -447,7 +457,7 @@ impl Solution { } ``` -## C +## C ```c int* path; diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 415fdb605f..83708df720 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -82,7 +82,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 选择过程树形结构如图所示: -![40.组合总和II](https://img-blog.csdnimg.cn/20201123202736384.png) +![40.组合总和II](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000918.png) 可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。 @@ -132,7 +132,7 @@ if (sum == target) { 这块比较抽象,如图: -![40.组合总和II1](https://img-blog.csdnimg.cn/20201123202817973.png) +![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000954.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 8ea81234cc..db66095da2 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -1,3 +1,4 @@ +

@@ -5,9 +6,10 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ > 这个图就是大厂面试经典题目,接雨水! 最常青藤的一道题,面试官百出不厌! -# 42. 接雨水 +# 42. 接雨水 [力扣题目链接](https://leetcode.cn/problems/trapping-rain-water/) @@ -15,7 +17,7 @@ 示例 1: -![](https://code-thinking-1253855093.cos.ap-guangzhou.myqcloud.com/pics/20210713205038.png) +![](https://code-thinking-1253855093.cos.ap-guangzhou.myqcloud.com/pics/20210713205038.png) * 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1] * 输出:6 @@ -27,26 +29,27 @@ * 输出:9 -# 思路 +# 思路 接雨水问题在面试中还是常见题目的,有必要好好讲一讲。 本文深度讲解如下三种方法: + * 双指针法 * 动态规划 * 单调栈 -## 暴力解法 +## 暴力解法 本题暴力解法也是也是使用双指针。 首先要明确,要按照行来计算,还是按照列来计算。 按照行来计算如图: -![42.接雨水2](https://img-blog.csdnimg.cn/20210402091118927.png) +![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091118927.png) 按照列来计算如图: -![42.接雨水1](https://img-blog.csdnimg.cn/20210402091208445.png) +![42.接雨水1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091208445.png) 一些同学在实现的时候,很容易一会按照行来计算一会按照列来计算,这样就会越写越乱。 @@ -58,7 +61,7 @@ 这句话可以有点绕,来举一个理解,例如求列4的雨水高度,如图: -![42.接雨水3](https://img-blog.csdnimg.cn/20210223092732301.png) +![42.接雨水3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092732301.png) 列4 左侧最高的柱子是列3,高度为2(以下用lHeight表示)。 @@ -72,7 +75,7 @@ 此时求出了列4的雨水体积。 -一样的方法,只要从头遍历一遍所有的列,然后求出每一列雨水的体积,相加之后就是总雨水的体积了。 +一样的方法,只要从头遍历一遍所有的列,然后求出每一列雨水的体积,相加之后就是总雨水的体积了。 首先从头遍历所有的列,并且**要注意第一个柱子和最后一个柱子不接雨水**,代码如下: @@ -132,7 +135,7 @@ public: 因为每次遍历列的时候,还要向两边寻找最高的列,所以时间复杂度为O(n^2),空间复杂度为O(1)。 -力扣后面修改了后台测试数据,所以以上暴力解法超时了。 +力扣后面修改了后台测试数据,所以以上暴力解法超时了。 ## 双指针优化 @@ -181,9 +184,9 @@ public: }; ``` -## 单调栈解法 +## 单调栈解法 -关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 +关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 单调栈就是保持栈内元素有序。和[栈与队列:单调队列](https://programmercarl.com/0239.滑动窗口最大值.html)一样,需要我们自己维持顺序,没有现成的容器可以用。 @@ -197,7 +200,7 @@ public: 1. 首先单调栈是按照行方向来计算雨水,如图: -![42.接雨水2](https://img-blog.csdnimg.cn/20210223092629946.png) +![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092629946.png) 知道这一点,后面的就可以理解了。 @@ -211,11 +214,11 @@ public: 如图: -![42.接雨水4](https://img-blog.csdnimg.cn/2021022309321229.png) +![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229.png) 关于单调栈的顺序给大家一个总结: [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 中求一个元素右边第一个更大元素,单调栈就是递增的,[84.柱状图中最大的矩形](https://programmercarl.com/0084.柱状图中最大的矩形.html)求一个元素右边第一个更小元素,单调栈就是递减的。 -3. 遇到相同高度的柱子怎么办。 +3. 遇到相同高度的柱子怎么办。 遇到相同的元素,更新栈内下标,就是将栈里元素(旧下标)弹出,将新元素(新下标)加入栈中。 @@ -225,7 +228,7 @@ public: 如图所示: -![42.接雨水5](https://img-blog.csdnimg.cn/20210223094619398.png) +![42.接雨水5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223094619398.png) 4. 栈里要保存什么数值 @@ -233,7 +236,7 @@ public: 长就是通过柱子的高度来计算,宽是通过柱子之间的下标来计算, -那么栈里有没有必要存一个pair类型的元素,保存柱子的高度和下标呢。 +那么栈里有没有必要存一个pair类型的元素,保存柱子的高度和下标呢。 其实不用,栈里就存放下标就行,想要知道对应的高度,通过height[stack.top()] 就知道弹出的下标对应的高度了。 @@ -245,17 +248,17 @@ stack st; // 存着下标,计算的时候用下标对应的柱子高度 明确了如上几点,我们再来看处理逻辑。 -### 单调栈处理逻辑 +### 单调栈处理逻辑 -以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 +以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 -以下逻辑主要就是三种情况 +以下逻辑主要就是三种情况 -* 情况一:当前遍历的元素(柱子)高度小于栈顶元素的高度 height[i] < height[st.top()] -* 情况二:当前遍历的元素(柱子)高度等于栈顶元素的高度 height[i] == height[st.top()] -* 情况三:当前遍历的元素(柱子)高度大于栈顶元素的高度 height[i] > height[st.top()] +* 情况一:当前遍历的元素(柱子)高度小于栈顶元素的高度 height[i] < height[st.top()] +* 情况二:当前遍历的元素(柱子)高度等于栈顶元素的高度 height[i] == height[st.top()] +* 情况三:当前遍历的元素(柱子)高度大于栈顶元素的高度 height[i] > height[st.top()] -先将下标0的柱子加入到栈中,`st.push(0);`。 栈中存放我们遍历过的元素,所以先将下标0加进来。 +先将下标0的柱子加入到栈中,`st.push(0);`。 栈中存放我们遍历过的元素,所以先将下标0加进来。 然后开始从下标1开始遍历所有的柱子,`for (int i = 1; i < height.size(); i++)`。 @@ -278,9 +281,9 @@ if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况 } ``` -如果当前遍历的元素(柱子)高度大于栈顶元素的高度,此时就出现凹槽了,如图所示: +如果当前遍历的元素(柱子)高度大于栈顶元素的高度,此时就出现凹槽了,如图所示: -![42.接雨水4](https://img-blog.csdnimg.cn/2021022309321229.png) +![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229-20230310123027977.png) 取栈顶元素,将栈顶元素弹出,这个就是凹槽的底部,也就是中间位置,下标记为mid,对应的高度为height[mid](就是图中的高度1)。 @@ -290,7 +293,7 @@ if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况 此时大家应该可以发现其实就是**栈顶和栈顶的下一个元素以及要入栈的元素,三个元素来接水!** -那么雨水高度是 min(凹槽左边高度, 凹槽右边高度) - 凹槽底部高度,代码为:`int h = min(height[st.top()], height[i]) - height[mid];` +那么雨水高度是 min(凹槽左边高度, 凹槽右边高度) - 凹槽底部高度,代码为:`int h = min(height[st.top()], height[i]) - height[mid];` 雨水的宽度是 凹槽右边的下标 - 凹槽左边的下标 - 1(因为只求中间宽度),代码为:`int w = i - st.top() - 1 ;` @@ -373,11 +376,12 @@ public: 精简之后的代码,大家就看不出去三种情况的处理了,貌似好像只处理的情况三,其实是把情况一和情况二融合了。 这样的代码不太利于理解。 -## 其他语言版本 +## 其他语言版本 -### Java: +### Java: 暴力解法: + ```java class Solution { public int trap(int[] height) { @@ -385,7 +389,7 @@ class Solution { for (int i = 0; i < height.length; i++) { // 第一个柱子和最后一个柱子不接雨水 if (i==0 || i== height.length - 1) continue; - + int rHeight = height[i]; // 记录右边柱子的最高高度 int lHeight = height[i]; // 记录左边柱子的最高高度 for (int r = i+1; r < height.length; r++) { @@ -404,6 +408,7 @@ class Solution { ``` 双指针: + ```java class Solution { public int trap(int[] height) { @@ -411,15 +416,15 @@ class Solution { if (length <= 2) return 0; int[] maxLeft = new int[length]; int[] maxRight = new int[length]; - + // 记录每个柱子左边柱子最大高度 maxLeft[0] = height[0]; for (int i = 1; i< length; i++) maxLeft[i] = Math.max(height[i], maxLeft[i-1]); - + // 记录每个柱子右边柱子最大高度 maxRight[length - 1] = height[length - 1]; for(int i = length - 2; i >= 0; i--) maxRight[i] = Math.max(height[i], maxRight[i+1]); - + // 求和 int sum = 0; for (int i = 0; i < length; i++) { @@ -432,13 +437,14 @@ class Solution { ``` 单调栈法 + ```java class Solution { public int trap(int[] height){ int size = height.length; if (size <= 2) return 0; - + // in the stack, we push the index of array // using height[] to access the real height Stack stack = new Stack(); @@ -458,7 +464,7 @@ class Solution { int heightAtIdx = height[index]; while (!stack.isEmpty() && (heightAtIdx > height[stackTop])){ int mid = stack.pop(); - + if (!stack.isEmpty()){ int left = stack.peek(); @@ -472,7 +478,7 @@ class Solution { stack.push(index); } } - + return sum; } } @@ -481,6 +487,7 @@ class Solution { ### Python: 暴力解法: + ```Python class Solution: def trap(self, height: List[int]) -> int: @@ -495,32 +502,35 @@ class Solution: for k in range(i+2,len(height)): if height[k] > rHight: rHight = height[k] - res1 = min(lHight,rHight) - height[i] + res1 = min(lHight,rHight) - height[i] if res1 > 0: res += res1 return res ``` 双指针: + ```python class Solution: def trap(self, height: List[int]) -> int: leftheight, rightheight = [0]*len(height), [0]*len(height) - + leftheight[0]=height[0] for i in range(1,len(height)): leftheight[i]=max(leftheight[i-1],height[i]) rightheight[-1]=height[-1] for i in range(len(height)-2,-1,-1): rightheight[i]=max(rightheight[i+1],height[i]) - + result = 0 for i in range(0,len(height)): summ = min(leftheight[i],rightheight[i])-height[i] result += summ return result ``` + 单调栈 + ```Python class Solution: def trap(self, height: List[int]) -> int: @@ -565,8 +575,8 @@ class Solution: result += h * w stack.append(i) return result - -# 单调栈压缩版 + +# 单调栈压缩版 class Solution: def trap(self, height: List[int]) -> int: stack = [0] @@ -586,7 +596,7 @@ class Solution: ``` -### Go +### Go ```go func trap(height []int) int { @@ -601,7 +611,7 @@ func trap(height []int) int { } left++ } else { - if height[right] > rightMax { + if height[right] > rightMax { rightMax = height[right] // //设置右边最高柱子 } else { res += rightMax - height[right] // //左边必定有柱子挡水,所以,遇到所有值小于等于rightMax的,全部加入水池 @@ -652,6 +662,7 @@ func min(a,b int)int{ ``` 单调栈解法 + ```go func trap(height []int) int { if len(height) <= 2 { @@ -896,12 +907,12 @@ int trap(int* height, int heightSize) { while (left < right) { //两个指针重合就结束 leftMax = fmax(leftMax, height[left]); rightMax = fmax(rightMax, height[right]); - if (leftMax < rightMax) { + if (leftMax < rightMax) { ans += leftMax - height[left]; //这里考虑的是下标为left的“底”能装多少水 ++left;//指针的移动次序是这个方法的关键 //这里左指针右移是因为左“墙”较矮,左边这一片实际情况下的盛水量是受制于这个矮的左“墙”的 //而较高的右边在实际情况下的限制条件可能不是当前的左“墙”,比如限制条件可能是右“墙”,就能装更高的水, - } + } else { ans += rightMax - height[right]; //同理,考虑下标为right的元素 --right; diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index d4f2e6ea78..22151fdc9a 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -46,7 +46,8 @@ 如图: -![45.跳跃游戏II](https://img-blog.csdnimg.cn/20201201232309103.png) + +![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103.png) **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** @@ -96,11 +97,11 @@ public: 因为当移动下标指向nums.size - 2时: * 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图: -![45.跳跃游戏II2](https://img-blog.csdnimg.cn/20201201232445286.png) +![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png) * 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图: -![45.跳跃游戏II1](https://img-blog.csdnimg.cn/20201201232338693.png) +![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693.png) 代码如下: diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index 86bc704d4b..e08aec940b 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -66,7 +66,7 @@ void backtracking (vector& nums, vector& used) * 递归终止条件 -![46.全排列](https://img-blog.csdnimg.cn/20201209174225145.png) +![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209174225145.png) 可以看出叶子节点,就是收割结果的地方。 diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index 3eb99948ea..b4f7a4d8cb 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 47.全排列 II [力扣题目链接](https://leetcode.cn/problems/permutations-ii/) @@ -12,17 +14,20 @@ 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。 示例 1: + * 输入:nums = [1,1,2] * 输出: -[[1,1,2], - [1,2,1], - [2,1,1]] + [[1,1,2], + [1,2,1], + [2,1,1]] 示例 2: + * 输入:nums = [1,2,3] * 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 提示: + * 1 <= nums.length <= 8 * -10 <= nums[i] <= 10 @@ -45,7 +50,7 @@ 我以示例中的 [1,1,2]为例 (为了方便举例,已经排序)抽象为一棵树,去重过程如图: -![47.全排列II1](https://img-blog.csdnimg.cn/20201124201331223.png) +![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201331223.png) 图中我们对同一树层,前一位(也就是nums[i-1])如果使用过,那么就进行去重。 @@ -68,7 +73,7 @@ private: } for (int i = 0; i < nums.size(); i++) { // used[i - 1] == true,说明同一树枝nums[i - 1]使用过 - // used[i - 1] == false,说明同一树层nums[i - 1]使用过 + // used[i - 1] == false,说明同一树层nums[i - 1]使用过 // 如果同一树层nums[i - 1]使用过则直接跳过 if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) { continue; @@ -123,23 +128,26 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2](https://img-blog.csdnimg.cn/20201124201406192.png) +![47.全排列II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201406192.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://img-blog.csdnimg.cn/20201124201431571.png) +![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201431571.png) 大家应该很清晰的看到,树层上对前一位去重非常彻底,效率很高,树枝上对前一位去重虽然最后可以得到答案,但是做了很多无用搜索。 ## 总结 这道题其实还是用了我们之前讲过的去重思路,但有意思的是,去重的代码中,这么写: + ```cpp if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) { continue; } ``` + 和这么写: + ```cpp if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { continue; @@ -154,7 +162,7 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { ## 其他语言版本 -### java +### java ```java class Solution { @@ -196,7 +204,7 @@ class Solution { } ``` -### python +### python ```python class Solution: @@ -224,7 +232,7 @@ class Solution: return res ``` -### Go +### Go ```go var ( @@ -264,7 +272,6 @@ func dfs(nums []int, cur int) { ### Javascript ```javascript - var permuteUnique = function (nums) { nums.sort((a, b) => { return a - b @@ -392,6 +399,7 @@ impl Solution { ``` ### C + ```c //临时数组 int *path; @@ -483,7 +491,7 @@ object Solution { // 当前索引为0,不存在和前一个数字相等可以进入回溯 // 当前索引值和上一个索引不相等,可以回溯 // 前一个索引对应的值没有被选,可以回溯 - // 因为Scala没有continue,只能将逻辑反过来写 + // 因为Scala没有continue,只能将逻辑反过来写 if (i == 0 || (i > 0 && num(i) != num(i - 1)) || used(i-1) == false) { used(i) = true path.append(num(i)) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 624452fd2c..bd1d1c9bec 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -47,7 +47,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, 下面我用一个 3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://img-blog.csdnimg.cn/20210130182532303.jpg) +![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303.jpg) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 @@ -87,7 +87,7 @@ void backtracking(int n, int row, vector& chessboard) { * 递归终止条件 在如下树形结构中: -![51.N皇后](https://img-blog.csdnimg.cn/20210130182532303.jpg) +![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303-20230310122134167.jpg) 可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。 diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index 1c88fb3c7b..90b920bac4 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -13,7 +13,9 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 上图为 8 皇后问题的一种解法。 -![51n皇后](https://img-blog.csdnimg.cn/20200821152118456.png) + + +![51n皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20200821152118456.png) 给定一个整数 n,返回 n 皇后不同的解决方案的数量。 diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 506c000fd2..c7d1b2fd7b 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -51,7 +51,7 @@ dp[0]应该是多少呢? 5. 举例推导dp数组 以示例一为例,输入:nums = [-2,1,-3,4,-1,2,1,-5,4],对应的dp状态如下: -![53.最大子序和(动态规划)](https://img-blog.csdnimg.cn/20210303104129101.png) +![53.最大子序和(动态规划)](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303104129101.png) **注意最后的结果可不是dp[nums.size() - 1]!** ,而是dp[6]。 diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 858741d0b0..a38e8237b9 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -37,7 +37,8 @@ 由外向内一圈一圈这么画下去,如下所示: -![螺旋矩阵](https://img-blog.csdnimg.cn/2020121623550681.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922102236.png) 这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。 diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index 7b02075bdb..a898263de9 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -46,7 +46,8 @@ 如图: -![55.跳跃游戏](https://img-blog.csdnimg.cn/20201124154758229.png) + +![55.跳跃游戏](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124154758229-20230310135019977.png) i每次移动只能在cover的范围内移动,每移动一个元素,cover得到该元素数值(新的覆盖范围)的补充,让i继续移动下去。 diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 3c980c2be3..d467ab1a29 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -37,7 +37,7 @@ 这么说有点抽象,看图:(**注意图中区间都是按照左边界排序之后了**) -![56.合并区间](https://img-blog.csdnimg.cn/20201223200632791.png) +![56.合并区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223200632791.png) 知道如何判断重复之后,剩下的就是合并了,如何去模拟合并区间呢? diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 40655f0c28..bf4363692b 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -1,14 +1,16 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 62.不同路径 [力扣题目链接](https://leetcode.cn/problems/unique-paths/) -一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。 +一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。 @@ -16,30 +18,35 @@ 示例 1: -![](https://img-blog.csdnimg.cn/20210110174033215.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110174033215.png) * 输入:m = 3, n = 7 * 输出:28 示例 2: + * 输入:m = 2, n = 3 * 输出:3 解释: 从左上角开始,总共有 3 条路径可以到达右下角。 + 1. 向右 -> 向右 -> 向下 2. 向右 -> 向下 -> 向右 3. 向下 -> 向右 -> 向右 示例 3: + * 输入:m = 7, n = 3 * 输出:28 示例 4: + * 输入:m = 3, n = 3 * 输出:6 提示: + * 1 <= m, n <= 100 * 题目数据保证答案小于等于 2 * 10^9 @@ -57,7 +64,7 @@ 如图举例: -![62.不同路径](https://img-blog.csdnimg.cn/20201209113602700.png) +![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700.png) 此时问题就可以转化为求二叉树叶子节点的个数,代码如下: @@ -126,7 +133,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图所示: -![62.不同路径1](https://img-blog.csdnimg.cn/20201209113631392.png) +![62.不同路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113631392.png) 以上动规五部曲分析完毕,C++代码如下: @@ -175,7 +182,7 @@ public: 在这个图中,可以看出一共m,n的话,无论怎么走,走到终点都需要 m + n - 2 步。 -![62.不同路径](https://img-blog.csdnimg.cn/20201209113602700.png) +![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700-20230310120944078.png) 在这m + n - 2 步中,一定有 m - 1 步是要向下走的,不用管什么时候向下走。 @@ -185,7 +192,7 @@ public: 那么答案,如图所示: -![62.不同路径2](https://img-blog.csdnimg.cn/20201209113725324.png) +![62.不同路径2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113725324.png) **求组合的时候,要防止两个int相乘溢出!** 所以不能把算式的分子都算出来,分母都算出来再做除法。 @@ -245,7 +252,8 @@ public: ## 其他语言版本 -### Java +### Java + ```java /** * 1. 确定dp数组下标含义 dp[i][j] 到每一个坐标可能的路径种类 @@ -278,7 +286,8 @@ public: ``` -### Python +### Python + ```python class Solution: # 动态规划 def uniquePaths(self, m: int, n: int) -> int: @@ -289,7 +298,8 @@ class Solution: # 动态规划 return dp[m - 1][n - 1] ``` -### Go +### Go + ```Go func uniquePaths(m int, n int) int { dp := make([][]int, m) @@ -309,19 +319,20 @@ func uniquePaths(m int, n int) int { } ``` -### Javascript +### Javascript + ```Javascript var uniquePaths = function(m, n) { const dp = Array(m).fill().map(item => Array(n)) - + for (let i = 0; i < m; ++i) { dp[i][0] = 1 } - + for (let i = 0; i < n; ++i) { dp[0][i] = 1 } - + for (let i = 1; i < m; ++i) { for (let j = 1; j < n; ++j) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1] @@ -330,7 +341,9 @@ var uniquePaths = function(m, n) { return dp[m - 1][n - 1] }; ``` + >版本二:直接将dp数值值初始化为1 + ```javascript /** * @param {number} m @@ -414,9 +427,9 @@ int **initDP(int m, int n) { } //从0,0到i,0只有一种走法,所以dp[i][0]都是1,同理dp[0][j]也是1 - for(i = 0; i < m; ++i) + for(i = 0; i < m; ++i) dp[i][0] = 1; - for(j = 0; j < n; ++j) + for(j = 0; j < n; ++j) dp[0][j] = 1; return dp; } @@ -440,6 +453,7 @@ int uniquePaths(int m, int n){ ``` 滚动数组解法: + ```c int uniquePaths(int m, int n){ int i, j; @@ -455,7 +469,7 @@ int uniquePaths(int m, int n){ dp[i] += dp[i - 1]; } } - return dp[n - 1]; + return dp[n - 1]; } ``` diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index c4ce3f954f..85130ab4e4 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -1,9 +1,11 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 63. 不同路径 II [力扣题目链接](https://leetcode.cn/problems/unique-paths-ii/) @@ -14,32 +16,33 @@ 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径? -![](https://img-blog.csdnimg.cn/20210111204901338.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204901338.png) 网格中的障碍物和空位置分别用 1 和 0 来表示。 示例 1: -![](https://img-blog.csdnimg.cn/20210111204939971.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204939971.png) * 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] * 输出:2 -解释: + 解释: * 3x3 网格的正中间有一个障碍物。 * 从左上角到右下角一共有 2 条不同的路径: - 1. 向右 -> 向右 -> 向下 -> 向下 - 2. 向下 -> 向下 -> 向右 -> 向右 + 1. 向右 -> 向右 -> 向下 -> 向下 + 2. 向下 -> 向下 -> 向右 -> 向右 示例 2: -![](https://img-blog.csdnimg.cn/20210111205857918.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111205857918.png) * 输入:obstacleGrid = [[0,1],[0,0]] * 输出:1 提示: -* m == obstacleGrid.length -* n == obstacleGrid[i].length + +* m == obstacleGrid.length +* n == obstacleGrid[i].length * 1 <= m, n <= 100 * obstacleGrid[i][j] 为 0 或 1 @@ -92,7 +95,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图: -![63.不同路径II](https://img-blog.csdnimg.cn/20210104114513928.png) +![63.不同路径II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114513928.png) 下标(0, j)的初始化情况同理。 @@ -126,13 +129,13 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://img-blog.csdnimg.cn/20210104114548983.png) +![63.不同路径II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114548983.png) 对应的dp table 如图: -![63.不同路径II2](https://img-blog.csdnimg.cn/20210104114610256.png) +![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256.png) -如果这个图看不同,建议在理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下​!​ +如果这个图看不同,建议在理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下! 动规五部分分析完毕,对应C++代码如下: @@ -163,6 +166,7 @@ public: 同样我们给出空间优化版本: + ```CPP class Solution { public: @@ -208,7 +212,7 @@ public: ## 其他语言版本 -### Java +### Java ```java class Solution { @@ -246,11 +250,11 @@ class Solution { int m = obstacleGrid.length; int n = obstacleGrid[0].length; int[] dp = new int[n]; - + for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) { dp[j] = 1; } - + for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { if (obstacleGrid[i][j] == 1) { @@ -316,7 +320,7 @@ class Solution: if obstacleGrid[0][j] == 1: break curr[j] = 1 - + for i in range(1, m): # 从第二行开始 for j in range(n): # 从第一列开始,因为第一列可能有障碍物 # 有障碍物处无法通行,状态就设成0 @@ -328,7 +332,7 @@ class Solution: curr[j] = curr[j] + curr[j - 1] # 隐含的状态更新 # dp[i][0] = dp[i - 1][0] - + return curr[n - 1] ``` @@ -369,26 +373,27 @@ func uniquePathsWithObstacles(obstacleGrid [][]int) int { ``` ### Javascript + ```Javascript var uniquePathsWithObstacles = function(obstacleGrid) { const m = obstacleGrid.length const n = obstacleGrid[0].length const dp = Array(m).fill().map(item => Array(n).fill(0)) - + for (let i = 0; i < m && obstacleGrid[i][0] === 0; ++i) { dp[i][0] = 1 } - + for (let i = 0; i < n && obstacleGrid[0][i] === 0; ++i) { dp[0][i] = 1 } - + for (let i = 1; i < m; ++i) { for (let j = 1; j < n; ++j) { dp[i][j] = obstacleGrid[i][j] === 1 ? 0 : dp[i - 1][j] + dp[i][j - 1] } } - + return dp[m - 1][n - 1] }; @@ -545,9 +550,10 @@ int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obst ``` 空间优化版本: + ```c int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obstacleGridColSize){ - int m = obstacleGridSize; + int m = obstacleGridSize; int n = obstacleGridColSize[0]; int *dp = (int*)malloc(sizeof(int) * n); int i, j; diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 14aeef0142..ed9be618d2 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -102,7 +102,8 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 举例当n为5的时候,dp table(dp数组)应该是这样的 -![70.爬楼梯](https://img-blog.csdnimg.cn/20210105202546299.png) + +![70.爬楼梯](https://code-thinking-1253855093.file.myqcloud.com/pics/20210105202546299.png) 如果代码出问题了,就把dp table 打印出来,看看究竟是不是和自己推导的一样。 diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 8a877d2088..0b49e3c656 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -169,7 +169,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 可以看出dp[i][j]是依赖左方,上方和左上方元素的,如图: -![72.编辑距离](https://img-blog.csdnimg.cn/20210114162113131.jpg) +![72.编辑距离](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162113131.jpg) 所以在dp矩阵中一定是从左到右从上到下去遍历。 @@ -193,7 +193,7 @@ for (int i = 1; i <= word1.size(); i++) { 以示例1为例,输入:`word1 = "horse", word2 = "ros"`为例,dp矩阵状态图如下: -![72.编辑距离1](https://img-blog.csdnimg.cn/20210114162132300.jpg) +![72.编辑距离1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162132300.jpg) 以上动规五部分析完毕,C++代码如下: diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 77ea5e80eb..9c6d481ddb 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -1,3 +1,4 @@ +

@@ -7,31 +8,32 @@ + # 第77题. 组合 [力扣题目链接](https://leetcode.cn/problems/combinations/ ) -给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 +给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 -示例: -输入: n = 4, k = 2 -输出: -[ - [2,4], - [3,4], - [2,3], - [1,2], - [1,3], - [1,4], -] +示例: +输入: n = 4, k = 2 +输出: +[ + [2,4], + [3,4], + [2,3], + [1,2], + [1,3], + [1,4], +] -# 算法公开课 +# 算法公开课 **《代码随想录》算法视频公开课:[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv),[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +# 思路 本题是回溯法的经典题目。 @@ -39,6 +41,7 @@ 直接的解法当然是使用for循环,例如示例中k为2,很容易想到 用两个for循环,这样就可以输出 和示例中一样的结果。 代码如下: + ```CPP int n = 4; for (int i = 1; i <= n; i++) { @@ -66,7 +69,7 @@ for (int i = 1; i <= n; i++) { **此时就会发现虽然想暴力搜索,但是用for循环嵌套连暴力都写不出来!** -咋整? +咋整? 回溯搜索法来了,虽然回溯法也是暴力,但至少能写出来,不像for循环嵌套k层让人绝望。 @@ -86,7 +89,7 @@ for (int i = 1; i <= n; i++) { 那么我把组合问题抽象为如下树形结构: -![77.组合](https://img-blog.csdnimg.cn/20201123195223940.png) +![77.组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195223940.png) 可以看出这棵树,一开始集合是 1,2,3,4, 从左向右取数,取过的数,不再重复取。 @@ -94,7 +97,7 @@ for (int i = 1; i <= n; i++) { **每次从集合中选取元素,可选择的范围随着选择的进行而收缩,调整可选择的范围**。 -**图中可以发现n相当于树的宽度,k相当于树的深度**。 +**图中可以发现n相当于树的宽度,k相当于树的深度**。 那么如何在这个树上遍历,然后收集到我们要的结果集呢? @@ -107,7 +110,7 @@ for (int i = 1; i <= n; i++) { ## 回溯法三部曲 -* 递归函数的返回值以及参数 +* 递归函数的返回值以及参数 在这里要定义两个全局变量,一个用来存放符合条件单一结果,一个用来存放符合条件结果的集合。 @@ -124,25 +127,25 @@ vector path; // 用来存放符合条件结果 然后还需要一个参数,为int型变量startIndex,这个参数用来记录本层递归的中,集合从哪里开始遍历(集合就是[1,...,n] )。 -为什么要有这个startIndex呢? +为什么要有这个startIndex呢? **建议在[77.组合视频讲解](https://www.bilibili.com/video/BV1ti4y1L7cv)中,07:36的时候开始听,startIndex 就是防止出现重复的组合**。 从下图中红线部分可以看出,在集合[1,2,3,4]取1之后,下一层递归,就要在[2,3,4]中取数了,那么下一层递归如何知道从[2,3,4]中取数呢,靠的就是startIndex。 -![77.组合2](https://img-blog.csdnimg.cn/20201123195328976.png) +![77.组合2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195328976.png) -所以需要startIndex来记录下一层递归,搜索的起始位置。 +所以需要startIndex来记录下一层递归,搜索的起始位置。 那么整体代码如下: ```cpp vector> result; // 存放符合条件结果的集合 vector path; // 用来存放符合条件单一结果 -void backtracking(int n, int k, int startIndex) +void backtracking(int n, int k, int startIndex) ``` -* 回溯函数终止条件 +* 回溯函数终止条件 什么时候到达所谓的叶子节点了呢? @@ -150,7 +153,7 @@ path这个数组的大小如果达到k,说明我们找到了一个子集大小 如图红色部分: -![77.组合3](https://img-blog.csdnimg.cn/20201123195407907.png) +![77.组合3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195407907.png) 此时用result二维数组,把path保存起来,并终止本层递归。 @@ -163,21 +166,21 @@ if (path.size() == k) { } ``` -* 单层搜索的过程 +* 单层搜索的过程 回溯法的搜索过程就是一个树型结构的遍历过程,在如下图中,可以看出for循环用来横向遍历,递归的过程是纵向遍历。 -![77.组合1](https://img-blog.csdnimg.cn/20201123195242899.png) +![77.组合1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195242899.png) 如此我们才遍历完图中的这棵树。 -for循环每次从startIndex开始遍历,然后用path保存取到的节点i。 +for循环每次从startIndex开始遍历,然后用path保存取到的节点i。 代码如下: ```CPP for (int i = startIndex; i <= n; i++) { // 控制树的横向遍历 - path.push_back(i); // 处理节点 + path.push_back(i); // 处理节点 backtracking(n, k, i + 1); // 递归:控制树的纵向遍历,注意下一层搜索要从i+1开始 path.pop_back(); // 回溯,撤销处理的节点 } @@ -201,7 +204,7 @@ private: return; } for (int i = startIndex; i <= n; i++) { - path.push_back(i); // 处理节点 + path.push_back(i); // 处理节点 backtracking(n, k, i + 1); // 递归 path.pop_back(); // 回溯,撤销处理的节点 } @@ -216,9 +219,10 @@ public: }; ``` -还记得我们在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中给出的回溯法模板么? +还记得我们在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中给出的回溯法模板么? 如下: + ``` void backtracking(参数) { if (终止条件) { @@ -234,15 +238,15 @@ void backtracking(参数) { } ``` -**对比一下本题的代码,是不是发现有点像!** 所以有了这个模板,就有解题的大体方向,不至于毫无头绪。 +**对比一下本题的代码,是不是发现有点像!** 所以有了这个模板,就有解题的大体方向,不至于毫无头绪。 -## 总结 +## 总结 组合问题是回溯法解决的经典问题,我们开始的时候给大家列举一个很形象的例子,就是n为100,k为50的话,直接想法就需要50层for循环。 从而引出了回溯法就是解决这种k层for循环嵌套的问题。 -然后进一步把回溯法的搜索过程抽象为树形结构,可以直观的看出搜索的过程。 +然后进一步把回溯法的搜索过程抽象为树形结构,可以直观的看出搜索的过程。 接着用回溯法三部曲,逐步分析了函数参数、终止条件和单层搜索的过程。 @@ -266,7 +270,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://img-blog.csdnimg.cn/20210130194335207.png) +![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207-20230310134409532.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 @@ -275,6 +279,7 @@ for (int i = startIndex; i <= n; i++) { **如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了**。 注意代码中i,就是for循环里选择的起始位置。 + ``` for (int i = startIndex; i <= n; i++) { ``` @@ -342,6 +347,7 @@ public: ### Java: + ```java class Solution { List> result = new ArrayList<>(); @@ -370,7 +376,7 @@ class Solution { } ``` -### Python +### Python ```python class Solution(object): @@ -417,6 +423,7 @@ class Solution: ``` 剪枝: + ```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: @@ -434,7 +441,8 @@ class Solution: return res ``` -### Go +### Go + ```Go var ( path []int @@ -452,7 +460,7 @@ func dfs(n int, k int, start int) { tmp := make([]int, k) copy(tmp, path) res = append(res, tmp) - return + return } for i := start; i <= n; i++ { // 从start开始,不往回走,避免出现重复组合 if n - i + 1 < k - len(path) { // 剪枝 @@ -465,9 +473,10 @@ func dfs(n int, k int, start int) { } ``` -### javascript +### javascript 剪枝: + ```javascript let result = [] let path = [] @@ -536,6 +545,7 @@ impl Solution { ``` 剪枝 + ```Rust impl Solution { fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, start_index: i32) { @@ -561,6 +571,7 @@ impl Solution { ``` ### C + ```c int* path; int pathTop; @@ -615,6 +626,7 @@ int** combine(int n, int k, int* returnSize, int** returnColumnSizes){ ``` 剪枝: + ```c int* path; int pathTop; @@ -701,13 +713,14 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { ### Scala 暴力: + ```scala object Solution { import scala.collection.mutable // 导包 def combine(n: Int, k: Int): List[List[Int]] = { var result = mutable.ListBuffer[List[Int]]() // 存放结果集 var path = mutable.ListBuffer[Int]() //存放符合条件的结果 - + def backtracking(n: Int, k: Int, startIndex: Int): Unit = { if (path.size == k) { // 如果path的size == k就达到题目要求,添加到结果集,并返回 @@ -720,7 +733,7 @@ object Solution { path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字 } } - + backtracking(n, k, 1) // 执行回溯 result.toList // 最终返回result的List形式,return关键字可以省略 } @@ -743,7 +756,7 @@ object Solution { return } // 剪枝优化 - for (i <- startIndex to (n - (k - path.size) + 1)) { + for (i <- startIndex to (n - (k - path.size) + 1)) { path.append(i) // 先把数字添加进去 backtracking(n, k, i + 1) // 进行下一步回溯 path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字 diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index 066e1b3329..9736549c57 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -66,7 +66,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://img-blog.csdnimg.cn/20210130194335207.png) +![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index b0c51d4496..f26d0821e9 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -48,7 +48,7 @@ 以示例中nums = [1,2,3]为例把求子集抽象为树型结构,如下: -![78.子集](https://img-blog.csdnimg.cn/202011232041348.png) +![78.子集](https://code-thinking.cdn.bcebos.com/pics/78.%E5%AD%90%E9%9B%86.png) 从图中红线部分,可以看出**遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合**。 @@ -72,7 +72,7 @@ void backtracking(vector& nums, int startIndex) { 从图中可以看出: -![78.子集](https://img-blog.csdnimg.cn/202011232041348.png) +![78.子集](https://code-thinking.cdn.bcebos.com/pics/78.%E5%AD%90%E9%9B%86.png) 剩余集合为空的时候,就是叶子节点。 diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index a02161aa92..1a9f8fda3e 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -42,7 +42,7 @@ 用示例中的[1, 2, 2] 来举例,如图所示: (**注意去重需要先对集合排序**) -![90.子集II](https://img-blog.csdnimg.cn/20201124195411977.png) +![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124195411977.png) 从图中可以看出,同一树层上重复取2 就要过滤掉,同一树枝上就可以重复取2,因为同一树枝上元素的集合才是唯一子集! diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 1f99a7a5d0..9d4d59180e 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -59,7 +59,8 @@ 切割问题可以抽象为树型结构,如图: -![93.复原IP地址](https://img-blog.csdnimg.cn/20201123203735933.png) + +![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933.png) ## 回溯三部曲 @@ -110,7 +111,8 @@ if (pointNum == 3) { // 逗点数量为3时,分隔结束 如果不合法就结束本层循环,如图中剪掉的分支: -![93.复原IP地址](https://img-blog.csdnimg.cn/20201123203735933.png) + +![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933-20230310132314109.png) 然后就是递归和回溯的过程: diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 99a4b8dda3..9f41906d27 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,9 +1,11 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 96.不同的二叉搜索树 [力扣题目链接](https://leetcode.cn/problems/unique-binary-search-trees/) @@ -12,7 +14,7 @@ 示例: -![](https://img-blog.csdnimg.cn/20210113161941835.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png) # 算法公开课 @@ -27,11 +29,11 @@ 了解了二叉搜索树之后,我们应该先举几个例子,画画图,看看有没有什么规律,如图: -![96.不同的二叉搜索树](https://img-blog.csdnimg.cn/20210107093106367.png) +![96.不同的二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093106367.png) n为1的时候有一棵树,n为2有两棵树,这个是很直观的。 -![96.不同的二叉搜索树1](https://img-blog.csdnimg.cn/20210107093129889.png) +![96.不同的二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093129889.png) 来看看n为3的时候,有哪几种情况。 @@ -65,7 +67,7 @@ dp[3],就是 元素1为头结点搜索树的数量 + 元素2为头结点搜索 如图所示: -![96.不同的二叉搜索树2](https://img-blog.csdnimg.cn/20210107093226241.png) +![96.不同的二叉搜索树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093226241.png) 此时我们已经找到递推关系了,那么可以用动规五部曲再系统分析一遍。 @@ -118,7 +120,7 @@ for (int i = 1; i <= n; i++) { n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://img-blog.csdnimg.cn/20210107093253987.png) +![96.不同的二叉搜索树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093253987.png) 当然如果自己画图举例的话,基本举例到n为3就可以了,n为4的时候,画图已经比较麻烦了。 @@ -168,7 +170,8 @@ public: ## 其他语言版本 -### Java +### Java + ```Java class Solution { public int numTrees(int n) { @@ -190,6 +193,7 @@ class Solution { ``` ### Python + ```python class Solution: def numTrees(self, n: int) -> int: @@ -202,6 +206,7 @@ class Solution: ``` ### Go + ```Go func numTrees(n int)int{ dp := make([]int, n+1) @@ -216,6 +221,7 @@ func numTrees(n int)int{ ``` ### Javascript + ```Javascript const numTrees =(n) => { let dp = new Array(n+1).fill(0); @@ -241,7 +247,7 @@ function numTrees(n: number): number { dp[0]: -1; 无意义; dp[1]: 1; ... - dp[i]: 2 * dp[i - 1] + + dp[i]: 2 * dp[i - 1] + (dp[1] * dp[i - 2] + dp[2] * dp[i - 3] + ... + dp[i - 2] * dp[1]); 从1加到i-2 */ const dp: number[] = []; @@ -282,7 +288,7 @@ impl Solution { int *initDP(int n) { int *dp = (int *)malloc(sizeof(int) * (n + 1)); int i; - for(i = 0; i <= n; ++i) + for(i = 0; i <= n; ++i) dp[i] = 0; return dp; } diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 93c2272cf6..95afe6805c 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -18,7 +18,7 @@ * 节点的右子树只包含大于当前节点的数。 * 所有左子树和右子树自身必须也是二叉搜索树。 -![98.验证二叉搜索树](https://img-blog.csdnimg.cn/20210203144334501.png) +![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png) # 视频讲解 @@ -104,7 +104,7 @@ if (root->val > root->left->val && root->val < root->right->val) { 例如: [10,5,15,null,null,6,20] 这个case: -![二叉搜索树](https://img-blog.csdnimg.cn/20200812191501419.png) +![二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000824.png) 节点10大于左节点5,小于右节点15,但右子树里出现了一个6 这就不符合了! diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 1c2812fa64..b75e9ff2da 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -11,7 +11,7 @@ 给定一个二叉树,检查它是否是镜像对称的。 -![101. 对称二叉树](https://img-blog.csdnimg.cn/20210203144607387.png) +![101. 对称二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144607387.png) # 思路 @@ -25,7 +25,7 @@ 比较的是两个子树的里侧和外侧的元素是否相等。如图所示: -![101. 对称二叉树1](https://img-blog.csdnimg.cn/20210203144624414.png) +![101. 对称二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144624414.png) 那么遍历的顺序应该是什么样的呢? diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 5c6ce488cb..26f0ae2d17 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1,9 +1,11 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 二叉树层序遍历登场! 《代码随想录》算法视频公开课:[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频在看本篇题解,更有助于大家对本题的理解。 @@ -35,7 +37,7 @@ 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。 -![102.二叉树的层序遍历](https://img-blog.csdnimg.cn/20210203144842988.png) +![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png) 思路: @@ -87,6 +89,7 @@ public: } }; ``` + ```CPP # 递归法 class Solution { @@ -108,7 +111,7 @@ public: }; ``` -java: +java: ```Java // 102.二叉树的层序遍历 @@ -168,7 +171,6 @@ python3代码: ```python - class Solution: """二叉树层序遍历迭代解法""" @@ -176,10 +178,10 @@ class Solution: results = [] if not root: return results - + from collections import deque que = deque([root]) - + while que: size = len(que) result = [] @@ -194,6 +196,7 @@ class Solution: return results ``` + ```python # 递归法 class Solution: @@ -209,7 +212,7 @@ class Solution: return res ``` -go: +go: ```go /** @@ -252,9 +255,9 @@ func levelOrder(root *TreeNode) [][]int { } queue := list.New() queue.PushBack(root) - + var tmpArr []int - + for queue.Len() > 0 { length := queue.Len() //保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) for i := 0; i < length; i++ { @@ -270,7 +273,7 @@ func levelOrder(root *TreeNode) [][]int { res = append(res, tmpArr) //放入结果集 tmpArr = []int{} //清空层的数据 } - + return res } @@ -279,7 +282,7 @@ func levelOrder(root *TreeNode) [][]int { */ func levelOrder(root *TreeNode) (res [][]int) { if root == nil { - return + return } curLevel := []*TreeNode{root} // 存放当前层节点 @@ -318,7 +321,7 @@ var levelOrder = function(root) { while(queue.length !== 0) { // 记录当前层级节点数 let length = queue.length; - //存放每一层的节点 + //存放每一层的节点 let curLevel = []; for(let i = 0;i < length; i++) { let node = queue.shift(); @@ -387,7 +390,9 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] { return result } ``` + Scala: + ```scala // 102.二叉树的层序遍历 object Solution { @@ -455,7 +460,7 @@ impl Solution { 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) -![107.二叉树的层次遍历II](https://img-blog.csdnimg.cn/20210203151058308.png) +![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png) 思路: @@ -488,6 +493,7 @@ public: } }; ``` + python代码: ```python @@ -498,10 +504,10 @@ class Solution: results = [] if not root: return results - + from collections import deque que = deque([root]) - + while que: result = [] for _ in range(len(que)): @@ -572,7 +578,7 @@ class Solution { public List> levelOrderBottom(TreeNode root) { // 利用链表可以进行 O(1) 头部插入, 这样最后答案不需要再反转 LinkedList> ans = new LinkedList<>(); - + Queue q = new LinkedList<>(); if (root != null) q.offer(root); @@ -584,9 +590,9 @@ class Solution { for (int i = 0; i < size; i ++) { TreeNode node = q.poll(); - + temp.add(node.val); - + if (node.left != null) q.offer(node.left); if (node.right != null) q.offer(node.right); @@ -616,7 +622,7 @@ func levelOrderBottom(root *TreeNode) [][]int { return res } queue.PushBack(root) - + for queue.Len() > 0 { length := queue.Len() tmp := []int{} @@ -632,12 +638,12 @@ func levelOrderBottom(root *TreeNode) [][]int { } res=append(res, tmp) } - + //反转结果集 for i:=0; i [[Int]] { Scala: + ```scala // 107.二叉树的层次遍历II object Solution { @@ -781,7 +788,7 @@ impl Solution { 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 -![199.二叉树的右视图](https://img-blog.csdnimg.cn/20210203151307377.png) +![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png) 思路: @@ -810,6 +817,7 @@ public: } }; ``` + python代码: ```python @@ -817,7 +825,7 @@ class Solution: def rightSideView(self, root: TreeNode) -> List[int]: if not root: return [] - + # deque来自collections模块,不在力扣平台时,需要手动写入 # 'from collections import deque' 导入 # deque相比list的好处是,list的pop(0)是O(n)复杂度,deque的popleft()是O(1)复杂度 @@ -837,15 +845,15 @@ class Solution: quene.append(node.left) if node.right: quene.append(node.right) - + return out_list - + # 执行用时:36 ms, 在所有 Python3 提交中击败了89.47%的用户 # 内存消耗:14.6 MB, 在所有 Python3 提交中击败了96.65%的用户 ``` -Java: +Java: ```java // 199.二叉树的右视图 @@ -889,10 +897,9 @@ public class N0199 { } ``` -go: +go: ```GO - /** 199. 二叉树的右视图 */ @@ -932,7 +939,7 @@ var rightSideView = function(root) { //二叉树右视图 只需要把每一层最后一个节点存储到res数组 let res = [], queue = []; queue.push(root); - + while(queue.length && root!==null) { // 记录当前层级节点个数 let length = queue.length; @@ -946,7 +953,7 @@ var rightSideView = function(root) { node.right && queue.push(node.right); } } - + return res; }; ``` @@ -997,6 +1004,7 @@ func rightSideView(_ root: TreeNode?) -> [Int] { ``` Scala: + ```scala // 199.二叉树的右视图 object Solution { @@ -1060,7 +1068,7 @@ impl Solution { 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。 -![637.二叉树的层平均值](https://img-blog.csdnimg.cn/20210203151350500.png) +![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png) 思路: @@ -1103,10 +1111,10 @@ class Solution: results = [] if not root: return results - + from collections import deque que = deque([root]) - + while que: size = len(que) sum_ = 0 @@ -1124,8 +1132,7 @@ class Solution: java: -```java - +```java // 637. 二叉树的层平均值 public class N0637 { @@ -1210,7 +1217,7 @@ var averageOfLevels = function(root) { //层级平均值 let res = [], queue = []; queue.push(root); - + while(queue.length && root!==null) { //每一层节点个数 let length = queue.length; @@ -1225,7 +1232,7 @@ var averageOfLevels = function(root) { //每一层的平均值存入数组res res.push(sum/length); } - + return res; }; ``` @@ -1280,7 +1287,9 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] { return result } ``` + Scala: + ```scala // 637.二叉树的层平均值 object Solution { @@ -1346,7 +1355,7 @@ impl Solution { 例如,给定一个 3叉树 : -![429. N叉树的层序遍历](https://img-blog.csdnimg.cn/20210203151439168.png) +![429. N叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151439168.png) 返回其层序遍历: @@ -1399,10 +1408,10 @@ class Solution: results = [] if not root: return results - + from collections import deque que = deque([root]) - + while que: result = [] for _ in range(len(que)): @@ -1426,16 +1435,16 @@ class Solution: def traversal(root,depth): if len(result)==depth:result.append([]) result[depth].append(root.val) - if root.children: + if root.children: for i in range(len(root.children)):traversal(root.children[i],depth+1) - + traversal(root,0) - return result + return result ``` -java: -```java +java: +```java // 429. N 叉树的层序遍历 public class N0429 { /** @@ -1520,7 +1529,7 @@ func levelOrder(root *Node) [][]int { } res = append(res, tmp) } - + return res } ``` @@ -1532,7 +1541,7 @@ var levelOrder = function(root) { //每一层可能有2个以上,所以不再使用node.left node.right let res = [], queue = []; queue.push(root); - + while(queue.length && root!==null) { //记录每一层节点个数还是和二叉树一致 let length = queue.length; @@ -1541,7 +1550,7 @@ var levelOrder = function(root) { while(length--) { let node = queue.shift(); curLevel.push(node.val); - + //这里不再是 ndoe.left node.right 而是循坏node.children for(let item of node.children){ item && queue.push(item); @@ -1549,7 +1558,7 @@ var levelOrder = function(root) { } res.push(curLevel); } - + return res; }; ``` @@ -1602,6 +1611,7 @@ func levelOrder(_ root: Node?) -> [[Int]] { ``` Scala: + ```scala // 429.N叉树的层序遍历 object Solution { @@ -1683,7 +1693,7 @@ impl Solution { 您需要在二叉树的每一行中找到最大的值。 -![515.在每个树行中找最大值](https://img-blog.csdnimg.cn/20210203151532153.png) +![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png) 思路: @@ -1714,6 +1724,7 @@ public: } }; ``` + python代码: ```python @@ -1734,6 +1745,7 @@ class Solution: out_list.append(max(in_list)) return out_list ``` + java代码: ```java @@ -1754,7 +1766,7 @@ class Solution { if(node.right != null) queue.offer(node.right); } result.add(max); - } + } return result; } } @@ -1811,8 +1823,8 @@ var largestValues = function(root) { //使用层序遍历 let res = [], queue = []; queue.push(root); - - while(root !== null && queue.length) { + + while(root !== null && queue.length) { //设置max初始值就是队列的第一个元素 let max = queue[0].val; let length = queue.length; @@ -1825,7 +1837,7 @@ var largestValues = function(root) { //把每一层的最大值放到res数组 res.push(max); } - + return res; }; ``` @@ -1884,6 +1896,7 @@ func largestValues(_ root: TreeNode?) -> [Int] { ``` Scala: + ```scala // 515.在每个树行中找最大值 object Solution { @@ -1959,9 +1972,9 @@ struct Node { 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。 -初始状态下,所有 next 指针都被设置为 NULL。 +初始状态下,所有 next 指针都被设置为 NULL。 -![116.填充每个节点的下一个右侧节点指针](https://img-blog.csdnimg.cn/20210203152044855.jpg) +![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg) 思路: @@ -2009,24 +2022,24 @@ class Solution { public Node connect(Node root) { Queue tmpQueue = new LinkedList(); if (root != null) tmpQueue.add(root); - + while (tmpQueue.size() != 0){ int size = tmpQueue.size(); - + Node cur = tmpQueue.poll(); if (cur.left != null) tmpQueue.add(cur.left); if (cur.right != null) tmpQueue.add(cur.right); - + for (int index = 1; index < size; index++){ Node next = tmpQueue.poll(); if (next.left != null) tmpQueue.add(next.left); if (next.right != null) tmpQueue.add(next.right); - + cur.next = next; cur = next; } } - + return root; } } @@ -2067,6 +2080,7 @@ class Solution: first = first.left # 从本层扩展到下一层 return root ``` + go: ```GO @@ -2108,8 +2122,8 @@ func connect(root *Node) *Node { ``` JavaScript: -```javascript +```javascript /** * // Definition for a Node. * function Node(val, left, right, next) { @@ -2142,6 +2156,7 @@ var connect = function(root) { }; ``` + TypeScript: ```typescript @@ -2200,6 +2215,7 @@ func connect(_ root: Node?) -> Node? { ``` Scala: + ```scala // 116.填充每个节点的下一个右侧节点指针 object Solution { @@ -2228,6 +2244,7 @@ object Solution { } } ``` + # 117.填充每个节点的下一个右侧节点指针II [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) @@ -2284,7 +2301,7 @@ class Solution { int size = queue.size(); Node node = null; Node nodePre = null; - + for (int i = 0; i < size; i++) { if (i == 0) { nodePre = queue.poll(); // 取出本层头一个节点 @@ -2307,6 +2324,7 @@ class Solution { } } ``` + python代码: ```python @@ -2329,6 +2347,7 @@ class Solution: return root ``` + go: ```GO @@ -2369,6 +2388,7 @@ func connect(root *Node) *Node { ``` JavaScript: + ```javascript /** * // Definition for a Node. @@ -2401,6 +2421,7 @@ var connect = function(root) { return root; }; ``` + TypeScript: ```typescript @@ -2459,6 +2480,7 @@ func connect(_ root: Node?) -> Node? { ``` Scala: + ```scala // 117.填充每个节点的下一个右侧节点指针II object Solution { @@ -2487,6 +2509,7 @@ object Solution { } } ``` + # 104.二叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) @@ -2501,7 +2524,7 @@ object Solution { 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://img-blog.csdnimg.cn/20210203153031914.png) +![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310134849764.png) 返回它的最大深度 3 。 @@ -2511,7 +2534,7 @@ object Solution { 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://img-blog.csdnimg.cn/20200810193056585.png) +![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585-20230310134854803.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 @@ -2540,7 +2563,8 @@ public: }; ``` -Java: +Java: + ```Java class Solution { public int maxDepth(TreeNode root) { @@ -2566,12 +2590,13 @@ class Solution { ``` Python: + ```python 3 class Solution: def maxDepth(self, root: TreeNode) -> int: if root == None: return 0 - + queue_ = [root] depth = 0 while queue_: @@ -2583,7 +2608,7 @@ class Solution: if cur.left: queue_.append(cur.left) if cur.right: queue_.append(cur.right) depth += 1 - + return depth ``` @@ -2623,6 +2648,7 @@ func maxDepth(root *TreeNode) int { ``` JavaScript: + ```javascript /** * Definition for a binary tree node. @@ -2700,6 +2726,7 @@ func maxDepth(_ root: TreeNode?) -> Int { ``` Scala: + ```scala // 104.二叉树的最大深度 object Solution { @@ -2789,7 +2816,8 @@ public: }; ``` -Java: +Java: + ```java class Solution { public int minDepth(TreeNode root){ @@ -2838,7 +2866,7 @@ class Solution: queue_ = [(root,1)] while queue_: cur, depth = queue_.pop(0) - + if cur.left == None and cur.right == None: return depth #先左子节点,由于左子节点没有孩子,则就是这一层了 @@ -2884,12 +2912,13 @@ func minDepth(root *TreeNode) int { } ans++//记录层数 } - + return ans+1 } ``` JavaScript: + ```javascript /** * Definition for a binary tree node. @@ -2972,6 +3001,7 @@ func minDepth(_ root: TreeNode?) -> Int { ``` Scala: + ```scala // 111.二叉树的最小深度 object Solution { diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index e54221db8c..b95e39c864 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -18,7 +18,8 @@ 示例: 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://img-blog.csdnimg.cn/20210203153031914.png) + +![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310121809902.png) 返回它的最大深度 3 。 @@ -169,7 +170,8 @@ public: 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://img-blog.csdnimg.cn/20200810193056585.png) + +![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 @@ -213,7 +215,7 @@ public: 例如,给定一个 3叉树 : -![559.n叉树的最大深度](https://img-blog.csdnimg.cn/2021020315313214.png) +![559.n叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315313214.png) 我们应返回其最大深度,3。 diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index 9c563619da..adb374f957 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ 看完本文,可以一起解决如下两道题目 * 106.从中序与后序遍历序列构造二叉树 @@ -21,11 +23,11 @@ 例如,给出 -* 中序遍历 inorder = [9,3,15,20,7] +* 中序遍历 inorder = [9,3,15,20,7] * 后序遍历 postorder = [9,15,7,20,3] -返回如下的二叉树: + 返回如下的二叉树: -![106. 从中序与后序遍历序列构造二叉树1](https://img-blog.csdnimg.cn/20210203154316774.png) +![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png) # 视频讲解 @@ -40,7 +42,7 @@ 流程如图: -![106.从中序与后序遍历序列构造二叉树](https://img-blog.csdnimg.cn/20210203154249860.png) +![106.从中序与后序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154249860.png) 那么代码应该怎么写呢? @@ -280,6 +282,7 @@ public: 下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割) ### C++优化版本 + ```CPP class Solution { private: @@ -397,7 +400,7 @@ public: }; ``` -## Python +## Python # 105.从前序与中序遍历序列构造二叉树 @@ -411,11 +414,11 @@ public: 例如,给出 -前序遍历 preorder = [3,9,20,15,7] +前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: -![105. 从前序与中序遍历序列构造二叉树](https://img-blog.csdnimg.cn/20210203154626672.png) +![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png) ## 思路 @@ -558,7 +561,7 @@ public: 举一个例子: -![106.从中序与后序遍历序列构造二叉树2](https://img-blog.csdnimg.cn/20210203154720326.png) +![106.从中序与后序遍历序列构造二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154720326.png) tree1 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 @@ -601,7 +604,7 @@ class Solution { return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开 } - + public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) { // 参数里的范围都是前闭后开 if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树 @@ -642,7 +645,7 @@ class Solution { int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置 TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点 int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数 - root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1, + root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1, inorder, inBegin, rootIndex); root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd, inorder, rootIndex + 1, inEnd); @@ -652,18 +655,19 @@ class Solution { } ``` -## Python +## Python + ```python class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: # 第一步: 特殊情况讨论: 树为空. 或者说是递归终止条件 if not postorder: - return + return # 第二步: 后序遍历的最后一个就是当前的中间节点 root_val = postorder[-1] root = TreeNode(root_val) - + # 第三步: 找切割点. root_index = inorder.index(root_val) @@ -672,7 +676,7 @@ class Solution: right_inorder = inorder[root_index + 1:] # 第五步: 切割postorder数组. 得到postorder数组的左,右半边. - # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. + # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. left_postorder = postorder[:len(left_inorder)] right_postorder = postorder[len(left_inorder): len(postorder) - 1] @@ -682,7 +686,7 @@ class Solution: root.right = self.buildTree(right_inorder, right_postorder) # 第七步: 返回答案 - return root + return root ``` 105.从前序与中序遍历序列构造二叉树 @@ -691,22 +695,22 @@ class Solution: class Solution: def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode: # 第一步: 特殊情况讨论: 树为空. 或者说是递归终止条件 - if not preorder: + if not preorder: return None - # 第二步: 前序遍历的第一个就是当前的中间节点. + # 第二步: 前序遍历的第一个就是当前的中间节点. root_val = preorder[0] root = TreeNode(root_val) - # 第三步: 找切割点. + # 第三步: 找切割点. separator_idx = inorder.index(root_val) - # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. + # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. inorder_left = inorder[:separator_idx] inorder_right = inorder[separator_idx + 1:] # 第五步: 切割preorder数组. 得到preorder数组的左,右半边. - # ⭐️ 重点1: 中序数组大小一定跟前序数组大小是相同的. + # ⭐️ 重点1: 中序数组大小一定跟前序数组大小是相同的. preorder_left = preorder[1:1 + len(inorder_left)] preorder_right = preorder[1 + len(inorder_left):] @@ -723,22 +727,22 @@ class Solution: class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode: # 第一步: 特殊情况讨论: 树为空. (递归终止条件) - if not postorder: + if not postorder: return None - # 第二步: 后序遍历的最后一个就是当前的中间节点. + # 第二步: 后序遍历的最后一个就是当前的中间节点. root_val = postorder[-1] root = TreeNode(root_val) - # 第三步: 找切割点. + # 第三步: 找切割点. separator_idx = inorder.index(root_val) - # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. + # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. inorder_left = inorder[:separator_idx] inorder_right = inorder[separator_idx + 1:] # 第五步: 切割postorder数组. 得到postorder数组的左,右半边. - # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. + # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. postorder_left = postorder[:len(inorder_left)] postorder_right = postorder[len(inorder_left): len(postorder) - 1] @@ -746,7 +750,7 @@ class Solution: root.left = self.buildTree(inorder_left, postorder_left) root.right = self.buildTree(inorder_right, postorder_right) - return root + return root ``` ## Go @@ -786,7 +790,7 @@ func rebuild(inorder []int, postorder []int, rootIdx int, l, r int) *TreeNode { rootIn := hash[rootV] // 找到根节点在对应的中序数组中的位置 root := &TreeNode{Val : rootV} // 构造根节点 // 重建左节点和右节点 - root.Left = rebuild(inorder, postorder, rootIdx-(r-rootIn)-1, l, rootIn-1) + root.Left = rebuild(inorder, postorder, rootIdx-(r-rootIn)-1, l, rootIn-1) root.Right = rebuild(inorder, postorder, rootIdx-1, rootIn+1, r) return root } @@ -830,7 +834,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode { -## JavaScript +## JavaScript ```javascript var buildTree = function(inorder, postorder) { @@ -1031,7 +1035,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in // 4.根据中序遍历数组左右数组的各子大小切割前序遍历数组。也分为左右数组 int* leftPreorder = preorder+1; - int* rightPreorder = preorder + 1 + leftNum; + int* rightPreorder = preorder + 1 + leftNum; // 5.递归进入左右数组,将返回的结果作为根结点的左右孩子 root->left = buildTree(leftPreorder, leftNum, leftInorder, leftNum); @@ -1056,26 +1060,26 @@ class Solution { inorderBegin: 0, inorderEnd: inorder.count) } - + func helper(preorder: [Int], preorderBegin: Int, preorderEnd: Int, inorder: [Int], inorderBegin: Int, inorderEnd: Int) -> TreeNode? { if preorderBegin == preorderEnd { return nil } - + // 前序遍历数组的第一个元素作为分割点 let rootValue = preorder[preorderBegin] let root = TreeNode(rootValue) - - + + if preorderEnd - preorderBegin == 1 { return root } - + var index = 0 // 从中序遍历数组中找到根节点的下标 if let ind = inorder.firstIndex(of: rootValue) { index = ind } - + // 递归 root.left = helper(preorder: preorder, preorderBegin: preorderBegin + 1, @@ -1102,28 +1106,28 @@ class Solution_0106 { if postorderEnd - postorderBegin < 1 { return nil } - + // 后序遍历数组的最后一个元素作为分割点 let rootValue = postorder[postorderEnd - 1] let root = TreeNode(rootValue) - + if postorderEnd - postorderBegin == 1 { return root } - + // 从中序遍历数组中找到根节点的下标 var delimiterIndex = 0 if let index = inorder.firstIndex(of: rootValue) { delimiterIndex = index } - + root.left = buildTree(inorder: inorder, inorderBegin: inorderBegin, inorderEnd: delimiterIndex, postorder: postorder, postorderBegin: postorderBegin, postorderEnd: postorderBegin + (delimiterIndex - inorderBegin)) - + root.right = buildTree(inorder: inorder, inorderBegin: delimiterIndex + 1, inorderEnd: inorderEnd, diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index f0455a834f..89778421d1 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -17,7 +17,8 @@ 示例: -![108.将有序数组转换为二叉搜索树](https://img-blog.csdnimg.cn/20201022164420763.png) + +![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png) # 算法公开课 diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index 492e25d9a5..804c95eb27 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ > 求高度还是求深度,你搞懂了不? # 110.平衡二叉树 @@ -13,13 +15,13 @@ 给定一个二叉树,判断它是否是高度平衡的二叉树。 -本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。 +本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。 示例 1: 给定二叉树 [3,9,20,null,null,15,7] -![110.平衡二叉树](https://img-blog.csdnimg.cn/2021020315542230.png) +![110.平衡二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315542230.png) 返回 true 。 @@ -27,7 +29,7 @@ 给定二叉树 [1,2,2,3,3,null,null,4,4] -![110.平衡二叉树1](https://img-blog.csdnimg.cn/20210203155447919.png) +![110.平衡二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155447919.png) 返回 false 。 @@ -45,7 +47,7 @@ 但leetcode中强调的深度和高度很明显是按照节点来计算的,如图: -![110.平衡二叉树2](https://img-blog.csdnimg.cn/20210203155515650.png) +![110.平衡二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155515650.png) 关于根节点的深度究竟是1 还是 0,不同的地方有不一样的标准,leetcode的题目中都是以节点为一度,即根节点深度是1。但维基百科上定义用边为一度,即根节点的深度是0,我们暂时以leetcode为准(毕竟要在这上面刷题)。 @@ -125,7 +127,7 @@ public: 1. 明确递归函数的参数和返回值 -参数:当前传入节点。 +参数:当前传入节点。 返回值:以当前传入节点为根节点的树的高度。 那么如何标记左右子树是否差值大于1呢? @@ -496,9 +498,10 @@ class Solution { } ``` -### Python +### Python 递归法: + ```python # Definition for a binary tree node. # class TreeNode: @@ -512,7 +515,7 @@ class Solution: return True else: return False - + def get_height(self, root: TreeNode) -> int: # Base Case if not root: @@ -531,6 +534,7 @@ class Solution: ``` 迭代法: + ```python class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: @@ -557,9 +561,10 @@ class Solution: ### Go + ```Go func isBalanced(root *TreeNode) bool { - h := getHeight(root) + h := getHeight(root) if h == -1 { return false } @@ -588,7 +593,9 @@ func max(a, b int) int { ``` ### JavaScript -递归法: + +递归法: + ```javascript var isBalanced = function(root) { //还是用递归三部曲 + 后序遍历 左右中 当前左子树右子树高度相差大于1就返回-1 @@ -614,6 +621,7 @@ var isBalanced = function(root) { ``` 迭代法: + ```javascript // 获取当前节点的高度 var getHeight = function (curNode) { @@ -644,7 +652,7 @@ var isBalanced = function (root) { let queue = [root]; while (queue.length) { let node = queue[queue.length - 1]; // 取出栈顶 - queue.pop(); + queue.pop(); if (Math.abs(getHeight(node.left) - getHeight(node.right)) > 1) { return false; } @@ -676,6 +684,7 @@ function isBalanced(root: TreeNode | null): boolean { ### C 递归法: + ```c int getDepth(struct TreeNode* node) { //如果结点不存在,返回0 @@ -706,6 +715,7 @@ bool isBalanced(struct TreeNode* root) { ``` 迭代法: + ```c //计算结点深度 int getDepth(struct TreeNode* node) { @@ -717,7 +727,7 @@ int getDepth(struct TreeNode* node) { stack[stackTop++] = node; int result = 0; int depth = 0; - + //当栈中有元素时,进行迭代遍历 while(stackTop) { //取出栈顶元素 @@ -741,7 +751,7 @@ int getDepth(struct TreeNode* node) { tempNode = stack[--stackTop]; depth--; } - } + } return result; } @@ -750,11 +760,11 @@ bool isBalanced(struct TreeNode* root){ //开辟栈空间 struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 10000); int stackTop = 0; - + //若根节点不存在,返回True if(!root) return 1; - + //将根节点入栈 stack[stackTop++] = root; //当栈中有元素时,进行遍历 @@ -764,7 +774,7 @@ bool isBalanced(struct TreeNode* root){ //计算左右子树的深度 int diff = getDepth(node->right) - getDepth(node->left); //若深度的绝对值大于1,返回False - if(diff > 1 || diff < -1) + if(diff > 1 || diff < -1) return 0; //如果栈顶结点有左右结点,将左右结点入栈 if(node->left) @@ -780,6 +790,7 @@ bool isBalanced(struct TreeNode* root){ ### Swift: >递归 + ```swift func isBalanced(_ root: TreeNode?) -> Bool { // -1 已经不是平衡二叉树 diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index cbe7b7ecad..de36c6f25a 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -21,7 +21,8 @@ 给定二叉树 [3,9,20,null,null,15,7], -![111.二叉树的最小深度1](https://img-blog.csdnimg.cn/2021020315582586.png) + +![111.二叉树的最小深度1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315582586.png) 返回它的最小深度 2. @@ -45,7 +46,7 @@ 本题还有一个误区,在处理节点的过程中,最大深度很容易理解,最小深度就不那么好理解,如图: -![111.二叉树的最小深度](https://img-blog.csdnimg.cn/20210203155800503.png) +![111.二叉树的最小深度](https://code-thinking.cdn.bcebos.com/pics/111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.png) 这就重新审题了,题目中说的是:**最小深度是从根节点到最近叶子节点的最短路径上的节点数量。**,注意是**叶子节点**。 @@ -87,7 +88,7 @@ return result; 这个代码就犯了此图中的误区: -![111.二叉树的最小深度](https://img-blog.csdnimg.cn/20210203155800503.png) +![111.二叉树的最小深度](https://code-thinking.cdn.bcebos.com/pics/111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.png) 如果这么求的话,没有左孩子的分支会算为最短深度。 diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index b1f0133663..e412d38e4e 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -1,18 +1,20 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 112. 路径总和 [力扣题目链接](https://leetcode.cn/problems/path-sum/) 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。 -说明: 叶子节点是指没有子节点的节点。 +说明: 叶子节点是指没有子节点的节点。 -示例:  +示例: 给定如下二叉树,以及目标和 sum = 22, ![112.路径总和1](https://img-blog.csdnimg.cn/20210203160355234.png) @@ -53,7 +55,7 @@ 如图所示: -![112.路径总和](https://img-blog.csdnimg.cn/2021020316051216.png) +![112.路径总和](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020316051216.png) 图中可以看出,遍历的路线,并不要遍历整棵树,所以递归函数需要返回值,可以用bool类型表示。 @@ -222,13 +224,13 @@ public: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。 -说明: 叶子节点是指没有子节点的节点。 +说明: 叶子节点是指没有子节点的节点。 示例: -给定如下二叉树,以及目标和 sum = 22, +给定如下二叉树,以及目标和 sum = 22, -![113.路径总和ii1.png](https://img-blog.csdnimg.cn/20210203160854654.png) +![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png) ## 思路 @@ -237,7 +239,7 @@ public: 如图: -![113.路径总和ii](https://img-blog.csdnimg.cn/20210203160922745.png) +![113.路径总和ii](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160922745.png) 为了尽可能的把细节体现出来,我写出如下代码(**这份代码并不简洁,但是逻辑非常清晰**) @@ -303,6 +305,7 @@ public: ## java ### 0112.路径总和 + ```java class solution { public boolean haspathsum(treenode root, int targetsum) { @@ -333,9 +336,9 @@ class solution { // lc112 简洁方法 class solution { public boolean haspathsum(treenode root, int targetsum) { - + if (root == null) return false; // 为空退出 - + // 叶子节点判断是否符合 if (root.left == null && root.right == null) return root.val == targetsum; @@ -344,7 +347,9 @@ class solution { } } ``` + 迭代 + ```java class solution { public boolean haspathsum(treenode root, int targetsum) { @@ -363,7 +368,7 @@ class solution { // 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true if(node.left == null && node.right == null && sum == targetsum) { return true; - } + } // 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来 if(node.right != null){ stack1.push(node.right); @@ -377,7 +382,7 @@ class solution { } } return false; - } + } } ``` @@ -447,6 +452,7 @@ class Solution { ### 0112.路径总和 **递归** + ```python class solution: def haspathsum(self, root: treenode, targetsum: int) -> bool: @@ -472,55 +478,57 @@ class solution: ``` **迭代 - 层序遍历** + ```python class solution: def haspathsum(self, root: treenode, targetsum: int) -> bool: - if not root: + if not root: return false stack = [] # [(当前节点,路径数值), ...] stack.append((root, root.val)) - while stack: + while stack: cur_node, path_sum = stack.pop() - if not cur_node.left and not cur_node.right and path_sum == targetsum: + if not cur_node.left and not cur_node.right and path_sum == targetsum: return true - if cur_node.right: - stack.append((cur_node.right, path_sum + cur_node.right.val)) + if cur_node.right: + stack.append((cur_node.right, path_sum + cur_node.right.val)) - if cur_node.left: + if cur_node.left: stack.append((cur_node.left, path_sum + cur_node.left.val)) return false ``` -### 0113.路径总和-ii +### 0113.路径总和-ii **递归** + ```python class solution: def pathsum(self, root: treenode, targetsum: int) -> list[list[int]]: - def traversal(cur_node, remain): + def traversal(cur_node, remain): if not cur_node.left and not cur_node.right: - if remain == 0: + if remain == 0: result.append(path[:]) return - if cur_node.left: + if cur_node.left: path.append(cur_node.left.val) traversal(cur_node.left, remain-cur_node.left.val) path.pop() - if cur_node.right: + if cur_node.right: path.append(cur_node.right.val) traversal(cur_node.right, remain-cur_node.right.val) path.pop() result, path = [], [] - if not root: + if not root: return [] path.append(root.val) traversal(root, targetsum - root.val) @@ -528,6 +536,7 @@ class solution: ``` **迭代法,用第二个队列保存目前的总和与路径** + ```python class Solution: def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: @@ -569,7 +578,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { if root == nil { return false } - + targetSum -= root.Val // 将targetSum在遍历每层的时候都减去本层节点的值 if root.Left == nil && root.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果 return true @@ -602,10 +611,10 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { targetSum -= node.Val // 将targetSum在遍历每层的时候都减去本层节点的值 *currPath = append(*currPath, node.Val) // 把当前节点放到路径记录里 - + if node.Left == nil && node.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果 // 不能直接将currPath放到result里面, 因为currPath是共享的, 每次遍历子树时都会被修改 - pathCopy := make([]int, len(*currPath)) + pathCopy := make([]int, len(*currPath)) for i, element := range *currPath { pathCopy[i] = element } @@ -623,6 +632,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { ### 0112.路径总和 **递归** + ```javascript /** * @param {treenode} root @@ -639,7 +649,7 @@ let haspathsum = function (root, targetsum) { // 左(空节点不遍历).遇到叶子节点返回true,则直接返回true if (node.left && traversal(node.left, cnt - node.left.val)) return true; - // 右(空节点不遍历) + // 右(空节点不遍历) if (node.right && traversal(node.right, cnt - node.right.val)) return true; return false; }; @@ -652,7 +662,9 @@ let haspathsum = function (root, targetsum) { // return haspathsum(root.left, targetsum - root.val) || haspathsum(root.right, targetsum - root.val); }; ``` + **迭代** + ```javascript let hasPathSum = function(root, targetSum) { if(root === null) return false; @@ -681,9 +693,10 @@ let hasPathSum = function(root, targetSum) { }; ``` -### 0113.路径总和-ii +### 0113.路径总和-ii **递归** + ```javascript let pathsum = function (root, targetsum) { // 递归法 @@ -715,7 +728,9 @@ let pathsum = function (root, targetsum) { return res; }; ``` + **递归 精简版** + ```javascript var pathsum = function(root, targetsum) { //递归方法 @@ -739,7 +754,9 @@ var pathsum = function(root, targetsum) { return resPath; }; ``` + **迭代** + ```javascript let pathSum = function(root, targetSum) { if(root === null) return []; @@ -905,7 +922,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { guard let root = root else { return false } - + return traversal(root, targetSum - root.val) } @@ -913,52 +930,54 @@ func traversal(_ cur: TreeNode?, _ count: Int) -> Bool { if cur?.left == nil && cur?.right == nil && count == 0 { return true } - + if cur?.left == nil && cur?.right == nil { return false } - + if let leftNode = cur?.left { if traversal(leftNode, count - leftNode.val) { return true } } - + if let rightNode = cur?.right { if traversal(rightNode, count - rightNode.val) { return true } } - + return false } ``` + **迭代** + ```swift func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { guard let root = root else { return false } - + var stack = Array<(TreeNode, Int)>() stack.append((root, root.val)) - + while !stack.isEmpty { let node = stack.removeLast() - + if node.0.left == nil && node.0.right == nil && targetSum == node.1 { return true } - + if let rightNode = node.0.right { stack.append((rightNode, node.1 + rightNode.val)) } - + if let leftNode = node.0.left { stack.append((leftNode, node.1 + leftNode.val)) } } - + return false } ``` @@ -989,12 +1008,12 @@ func traversal(_ cur: TreeNode?, count: Int) { result.append(path) return } - + // 遇到叶子节点而没有找到合适的边,直接返回 if cur?.left == nil && cur?.right == nil{ return } - + if let leftNode = cur?.left { path.append(leftNode.val) count -= leftNode.val @@ -1002,7 +1021,7 @@ func traversal(_ cur: TreeNode?, count: Int) { count += leftNode.val// 回溯 path.removeLast()// 回溯 } - + if let rightNode = cur?.right { path.append(rightNode.val) count -= rightNode.val @@ -1015,8 +1034,10 @@ func traversal(_ cur: TreeNode?, count: Int) { ``` ## C + > 0112.路径总和 -递归法: +> 递归法: + ```c bool hasPathSum(struct TreeNode* root, int targetSum){ // 递归结束条件:若当前节点不存在,返回false @@ -1025,13 +1046,14 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ // 若当前节点为叶子节点,且targetSum-root的值为0。(当前路径上的节点值的和满足条件)返回true if(!root->right && !root->left && targetSum == root->val) return true; - + // 查看左子树和右子树的所有节点是否满足条件 return hasPathSum(root->right, targetSum - root->val) || hasPathSum(root->left, targetSum - root->val); } ``` 迭代法: + ```c // 存储一个节点以及当前的和 struct Pair { @@ -1056,7 +1078,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ // 若栈顶元素为叶子节点,且和为targetSum时,返回true if(!topPair.node->left && !topPair.node->right && topPair.sum == targetSum) return true; - + // 若当前栈顶节点有左右孩子,计算和并入栈 if(topPair.node->left) { struct Pair newPair = {topPair.node->left, topPair.sum + topPair.node->left->val}; @@ -1070,7 +1092,9 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ return false; } ``` + > 0113.路径总和 II + ```c int** ret; int* path; @@ -1139,6 +1163,7 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur ### 0112.路径总和 **递归:** + ```scala object Solution { def hasPathSum(root: TreeNode, targetSum: Int): Boolean = { @@ -1163,6 +1188,7 @@ object Solution { ``` **迭代:** + ```scala object Solution { import scala.collection.mutable @@ -1187,6 +1213,7 @@ object Solution { ### 0113.路径总和 II **递归:** + ```scala object Solution { import scala.collection.mutable.ListBuffer diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index 8736c9f375..5b398f3fc7 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -127,7 +127,8 @@ dp[0][1]表示第0天不持有股票,不持有股票那么现金就是0,所 以示例1,输入:[7,1,5,3,6,4]为例,dp数组状态如下: -![121.买卖股票的最佳时机](https://img-blog.csdnimg.cn/20210224225642465.png) + +![121.买卖股票的最佳时机](https://code-thinking-1253855093.file.myqcloud.com/pics/20210224225642465.png) dp[5][1]就是最终结果。 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index d094da488e..29242be3db 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -62,7 +62,8 @@ 如图: -![122.买卖股票的最佳时机II](https://img-blog.csdnimg.cn/2020112917480858.png) + +![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858-20230310134659477.png) 一些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。 diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 3cd7ab26f8..33157238ac 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -116,7 +116,8 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]); 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://img-blog.csdnimg.cn/20201228181724295.png) + +![123.买卖股票的最佳时机III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201228181724295-20230310134201291.png) 大家可以看到红色框为最后两次卖出的状态。 diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" index 24cf43ae31..eb0e39fb5a 100644 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -163,7 +163,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 以输入:"aabc" 为例: -![132.分割回文串II](https://img-blog.csdnimg.cn/20210124182218844.jpg) +![132.分割回文串II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210124182218844.jpg) 以上分析完毕,代码如下: diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index 1a2fe92a5d..181f24e98e 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -53,7 +53,8 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果](https://img-blog.csdnimg.cn/20201117114916878.png) + +![135.分发糖果](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117114916878.png) 再确定左孩子大于右孩子的情况(从后向前遍历) @@ -78,7 +79,8 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果1](https://img-blog.csdnimg.cn/20201117115658791.png) + +![135.分发糖果1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117115658791.png) 所以该过程代码如下: diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 2148971d51..edb2c65ad6 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -181,7 +181,8 @@ dp[0]表示如果字符串为空的话,说明出现在字典里。 以输入: s = "leetcode", wordDict = ["leet", "code"]为例,dp状态如图: -![139.单词拆分](https://img-blog.csdnimg.cn/20210202162652727.jpg) + +![139.单词拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202162652727.jpg) dp[s.size()]就是最终结果。 diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index 7f944345f2..8a48c7d5f6 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -1,3 +1,4 @@ +

@@ -6,6 +7,7 @@ + > 找到有没有环已经很不容易了,还要让我找到环的入口? @@ -14,13 +16,13 @@ [力扣题目链接](https://leetcode.cn/problems/linked-list-cycle-ii/) 题意: -给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 +给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 为了表示给定链表中的环,使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。 **说明**:不允许修改给定的链表。 -![循环链表](https://img-blog.csdnimg.cn/20200816110112704.png) +![循环链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20200816110112704.png) ## 思路 @@ -48,7 +50,7 @@ 会发现最终都是这种情况, 如下图: -![142环形链表1](https://img-blog.csdnimg.cn/20210318162236720.png) +![142环形链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318162236720.png) fast和slow各自再走一步, fast和slow就相遇了 @@ -149,20 +151,20 @@ public: 即文章[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)中如下的地方: -![142环形链表5](https://img-blog.csdnimg.cn/20210318165123581.png) +![142环形链表5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318165123581.png) 首先slow进环的时候,fast一定是先进环来了。 如果slow进环入口,fast也在环入口,那么把这个环展开成直线,就是如下图的样子: -![142环形链表3](https://img-blog.csdnimg.cn/2021031816503266.png) +![142环形链表3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816503266.png) 可以看出如果slow 和 fast同时在环入口开始走,一定会在环入口3相遇,slow走了一圈,fast走了两圈。 重点来了,slow进环的时候,fast一定是在环的任意一个位置,如图: -![142环形链表4](https://img-blog.csdnimg.cn/2021031816515727.png) +![142环形链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816515727.png) 那么fast指针走到环入口3的时候,已经走了k + n 个节点,slow相应的应该走了(k + n) / 2 个节点。 @@ -187,6 +189,7 @@ public: Java: + ```java public class Solution { public ListNode detectCycle(ListNode head) { @@ -235,6 +238,7 @@ class Solution: ``` Go: + ```go func detectCycle(head *ListNode) *ListNode { slow, fast := head, head @@ -267,7 +271,7 @@ var detectCycle = function(head) { let slow =head.next, fast = head.next.next; while(fast && fast.next && fast!== slow) { slow = slow.next; - fast = fast.next.next; + fast = fast.next.next; } if(!fast || !fast.next ) return null; slow = head; @@ -374,6 +378,7 @@ ListNode *detectCycle(ListNode *head) { ``` Scala: + ```scala object Solution { def detectCycle(head: ListNode): ListNode = { diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 695fac35a2..4fdd7bf40c 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -129,7 +129,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://img-blog.csdnimg.cn/20201229100358221.png) +![188.买卖股票的最佳时机IV](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229100358221.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 6002cd3a48..fdb2dabf5f 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -85,7 +85,7 @@ for (int i = 2; i < nums.size(); i++) { 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://img-blog.csdnimg.cn/20210221170954115.jpg) +![198.打家劫舍](https://code-thinking-1253855093.file.myqcloud.com/pics/20210221170954115.jpg) 红框dp[nums.size() - 1]为结果。 diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 883876673f..99bd35805a 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ > 链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点再进行删除操作,接下来看一看哪种方式更方便。 # 203.移除链表元素 @@ -13,17 +15,17 @@ 题意:删除链表中等于给定值 val 的所有节点。 -示例 1: -输入:head = [1,2,6,3,4,5,6], val = 6 -输出:[1,2,3,4,5] +示例 1: +输入:head = [1,2,6,3,4,5,6], val = 6 +输出:[1,2,3,4,5] -示例 2: -输入:head = [], val = 1 -输出:[] +示例 2: +输入:head = [], val = 1 +输出:[] -示例 3: -输入:head = [7,7,7,7], val = 7 -输出:[] +示例 3: +输入:head = [7,7,7,7], val = 7 +输出:[] # 思路 @@ -32,11 +34,11 @@ 这里以链表 1 4 2 4 来举例,移除元素4。 -![203_链表删除元素1](https://img-blog.csdnimg.cn/20210316095351161.png) +![203_链表删除元素1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095351161.png) 如果使用C,C++编程语言的话,不要忘了还要从内存中删除这两个移除的节点, 清理节点内存之后如图: -![203_链表删除元素2](https://img-blog.csdnimg.cn/20210316095418280.png) +![203_链表删除元素2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095418280.png) **当然如果使用java ,python的话就不用手动管理内存了。** @@ -47,23 +49,23 @@ 那么因为单链表的特殊性,只能指向下一个节点,刚刚删除的是链表的中第二个,和第四个节点,那么如果删除的是头结点又该怎么办呢? 这里就涉及如下链表操作的两种方式: + * **直接使用原来的链表来进行删除操作。** * **设置一个虚拟头结点在进行删除操作。** 来看第一种操作:直接使用原来的链表来进行移除。 -![203_链表删除元素3](https://img-blog.csdnimg.cn/2021031609544922.png) +![203_链表删除元素3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031609544922.png) 移除头结点和移除其他节点的操作是不一样的,因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点。 所以头结点如何移除呢,其实只要将头结点向后移动一位就可以,这样就从链表中移除了一个头结点。 -![203_链表删除元素4](https://img-blog.csdnimg.cn/20210316095512470.png) - +![203_链表删除元素4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095512470.png) 依然别忘将原头结点从内存中删掉。 -![203_链表删除元素5](https://img-blog.csdnimg.cn/20210316095543775.png) +![203_链表删除元素5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095543775.png) 这样移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。 @@ -74,7 +76,7 @@ 来看看如何设置一个虚拟头。依然还是在这个链表中,移除元素1。 -![203_链表删除元素6](https://img-blog.csdnimg.cn/20210316095619221.png) +![203_链表删除元素6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095619221.png) 这里来给链表添加一个虚拟头结点为新的头结点,此时要移除这个旧头结点元素1。 @@ -146,8 +148,10 @@ public: ## 其他语言版本 + C: 用原来的链表操作: + ```c struct ListNode* removeElements(struct ListNode* head, int val){ struct ListNode* temp; @@ -168,7 +172,7 @@ struct ListNode* removeElements(struct ListNode* head, int val){ // 将cur->next设置为cur->next->next并删除cur->next cur->next = temp->next; free(temp); - } + } // 若cur->next不等于val,则将cur后移一位 else cur = cur->next; @@ -178,7 +182,9 @@ struct ListNode* removeElements(struct ListNode* head, int val){ return head; } ``` + 设置一个虚拟头结点: + ```c /** * Definition for singly-linked list. @@ -212,6 +218,7 @@ struct ListNode* removeElements(struct ListNode* head, int val){ ``` Java: + ```java /** * 添加虚节点方式 @@ -292,6 +299,7 @@ public ListNode removeElements(ListNode head, int val) { ``` Python: + ```python # Definition for singly-linked list. # class ListNode: @@ -335,7 +343,7 @@ func removeElements(head *ListNode, val int) *ListNode { } ``` -javaScript: +javaScript: ```js /** @@ -442,6 +450,7 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? { ``` PHP: + ```php /** * Definition for singly-linked list. @@ -469,6 +478,7 @@ func removeElements(head *ListNode, val int) *ListNode { ``` RUST: + ```rust // Definition for singly-linked list. // #[derive(PartialEq, Eq, Clone, Debug)] @@ -476,7 +486,7 @@ RUST: // pub val: i32, // pub next: Option> // } -// +// // impl ListNode { // #[inline] // fn new(val: i32) -> Self { @@ -504,7 +514,9 @@ impl Solution { } } ``` + Scala: + ```scala /** * Definition for singly-linked list. @@ -535,7 +547,9 @@ object Solution { } } ``` + Kotlin: + ```kotlin /** * Example: @@ -569,6 +583,7 @@ class Solution { } } ``` +

diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index b87c7cba5c..8bf61c3f8f 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -25,7 +25,8 @@ 其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: -![206_反转链表](https://img-blog.csdnimg.cn/20210218090901207.png) + +![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png) 之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index a25fc2f582..5a8f91aff3 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -103,7 +103,7 @@ public: 解题的关键在于 窗口的起始位置如何移动,如图所示: -![leetcode_209](https://img-blog.csdnimg.cn/20210312160441942.png) +![leetcode_209](https://code-thinking-1253855093.file.myqcloud.com/pics/20210312160441942.png) 可以发现**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)暴力解法降为O(n)。** diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index e595d2fdb2..0627eedb23 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -39,15 +39,15 @@ * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://img-blog.csdnimg.cn/20210129160748643.jpg) +![213.打家劫舍II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160748643-20230310134000692.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://img-blog.csdnimg.cn/20210129160821374.jpg) +![213.打家劫舍II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160821374-20230310134003961.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://img-blog.csdnimg.cn/20210129160842491.jpg) +![213.打家劫舍II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160842491-20230310134008133.jpg) **注意我这里用的是"考虑"**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素! 对于情况三,取nums[1] 和 nums[3]就是最大的。 diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index 5ca7bb9094..f631c3cdc8 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -1,3 +1,4 @@ +

@@ -7,17 +8,19 @@ + > 别看本篇选的是组合总和III,而不是组合总和,本题和上一篇77.组合相比难度刚刚好! # 216.组合总和III [力扣题目链接](https://leetcode.cn/problems/combination-sum-iii/) -找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。 +找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。 说明: + * 所有数字都是正整数。 -* 解集不能包含重复的组合。  +* 解集不能包含重复的组合。 示例 1: 输入: k = 3, n = 7 @@ -46,7 +49,7 @@ 选取过程如图: -![216.组合总和III](https://img-blog.csdnimg.cn/20201123195717975.png) +![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975.png) 图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。 @@ -80,6 +83,7 @@ vector> result; vector path; void backtracking(int targetSum, int k, int sum, int startIndex) ``` + 其实这里sum这个参数也可以省略,每次targetSum减去选取的元素数值,然后判断如果targetSum为0了,说明收集到符合条件的结果了,我这里为了直观便于理解,还是加一个sum参数。 还要强调一下,回溯法中递归函数参数很难一次性确定下来,一般先写逻辑,需要啥参数了,填什么参数。 @@ -108,7 +112,7 @@ if (path.size() == k) { 本题和[77. 组合](https://programmercarl.com/0077.组合.html)区别之一就是集合固定的就是9个数[1,...,9],所以for循环固定i<=9 如图: -![216.组合总和III](https://img-blog.csdnimg.cn/20201123195717975.png) +![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975-20230310113546003.png) 处理过程就是 path收集每次选取的元素,相当于树型结构里的边,sum来统计path里元素的总和。 @@ -166,7 +170,7 @@ public: 这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。 如图: -![216.组合总和III1](https://img-blog.csdnimg.cn/2020112319580476.png) +![216.组合总和III1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112319580476.png) 已选元素总和如果已经大于n(图中数值为4)了,那么往后遍历就没有意义了,直接剪掉。 @@ -181,7 +185,6 @@ if (sum > targetSum) { // 剪枝操作 当然这个剪枝也可以放在 调用递归之前,即放在这里,只不过要记得 要回溯操作给做了。 ```CPP - for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) { // 剪枝 sum += i; // 处理 path.push_back(i); // 处理 @@ -250,6 +253,7 @@ public: ## Java 模板方法 + ```java class Solution { List> result = new ArrayList<>(); @@ -317,6 +321,7 @@ class Solution { ``` 其他方法 + ```java class Solution { List> res = new ArrayList<>(); @@ -429,12 +434,12 @@ var combinationSum3 = function(k, n) { const dfs = (path,index) => { // 剪枝操作 if (sum > n){ - return + return } if (path.length == k) { if(sum == n){ res.push([...path]); - return + return } } for (let i = index; i <= 9 - (k-path.length) + 1;i++) { diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index 10ac826496..c346b6ff19 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -152,7 +152,7 @@ public: 我来举一个典型的例子如题: - + 完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。 @@ -161,10 +161,10 @@ public: 对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。 完全二叉树(一)如图: -![222.完全二叉树的节点个数](https://img-blog.csdnimg.cn/20201124092543662.png) +![222.完全二叉树的节点个数](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092543662.png) 完全二叉树(二)如图: -![222.完全二叉树的节点个数1](https://img-blog.csdnimg.cn/20201124092634138.png) +![222.完全二叉树的节点个数1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092634138.png) 可以看出如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量。 diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 1ea9a3e6c1..16a5be577d 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -11,7 +11,8 @@ 翻转一棵二叉树。 -![226.翻转二叉树](https://img-blog.csdnimg.cn/20210203192644329.png) + +![226.翻转二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192644329.png) 这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,权当一个乐子哈) @@ -33,7 +34,8 @@ 如果要从整个树来看,翻转还真的挺复杂,整个树以中间分割线进行翻转,如图: -![226.翻转二叉树1](https://img-blog.csdnimg.cn/20210203192724351.png) + +![226.翻转二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192724351.png) 可以发现想要翻转它,其实就把每一个节点的左右孩子交换一下就可以了。 diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 1c21f2be28..8353303aa3 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -15,7 +15,8 @@ 例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5] -![235. 二叉搜索树的最近公共祖先](https://img-blog.csdnimg.cn/20201018172243602.png) + +![235. 二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201018172243602.png) 示例 1: diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 7b163ee503..33201def08 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -17,7 +17,8 @@ 例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4] -![236. 二叉树的最近公共祖先](https://img-blog.csdnimg.cn/20201016173414722.png) + +![236. 二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201016173414722.png) 示例 1: 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 @@ -130,7 +131,7 @@ left与right的逻辑处理; // 中 如图: -![236.二叉树的最近公共祖先](https://img-blog.csdnimg.cn/2021020415105872.png) +![236.二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415105872.png) 就像图中一样直接返回7,多美滋滋。 @@ -163,7 +164,7 @@ TreeNode* right = lowestCommonAncestor(root->right, p, q); 如图: -![236.二叉树的最近公共祖先1](https://img-blog.csdnimg.cn/20210204151125844.png) +![236.二叉树的最近公共祖先1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151125844.png) 图中节点10的左子树返回null,右子树返回目标值7,那么此时节点10的处理逻辑就是把右子树的返回值(最近公共祖先7)返回上去! @@ -184,7 +185,7 @@ else { // (left == NULL && right == NULL) 那么寻找最小公共祖先,完整流程图如下: -![236.二叉树的最近公共祖先2](https://img-blog.csdnimg.cn/202102041512582.png) +![236.二叉树的最近公共祖先2](https://code-thinking-1253855093.file.myqcloud.com/pics/202102041512582.png) **从图中,大家可以看到,我们是如何回溯遍历整棵二叉树,将结果返回给头结点的!** diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 68434479f5..8c542ceaa4 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -16,7 +16,7 @@ 说明: 叶子节点是指没有子节点的节点。 示例: -![257.二叉树的所有路径1](https://img-blog.csdnimg.cn/2021020415161576.png) +![257.二叉树的所有路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415161576.png) # 思路 @@ -28,7 +28,7 @@ 前序遍历以及回溯的过程如图: -![257.二叉树的所有路径](https://img-blog.csdnimg.cn/20210204151702443.png) +![257.二叉树的所有路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151702443.png) 我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。** diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index 0654e49442..c329156b83 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -94,7 +94,8 @@ for (int i = 0; i <= n; i++) { // 遍历背包 已输入n为5例,dp状态图如下: -![279.完全平方数](https://img-blog.csdnimg.cn/20210202112617341.jpg) + +![279.完全平方数](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202112617341.jpg) dp[0] = 0 dp[1] = min(dp[0] + 1) = 1 diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index f4fe1c31c3..478837cc1b 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -82,7 +82,7 @@ for (int i = 1; i < nums.size(); i++) { 输入:[0,1,0,3,2],dp数组的变化如下: -![300.最长上升子序列](https://img-blog.csdnimg.cn/20210110170945618.jpg) +![300.最长上升子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110170945618.jpg) 如果代码写出来,但一直AC不了,那么就把dp数组打印出来,看看对不对! diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index b4825894b8..d62e91c74a 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -45,7 +45,7 @@ dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]。 * 状态三:今天卖出股票 * 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天! -![](https://img-blog.csdnimg.cn/518d5baaf33f4b2698064f8efb42edbf.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/518d5baaf33f4b2698064f8efb42edbf.png) j的状态为: @@ -133,7 +133,8 @@ dp[i][3] = dp[i - 1][2]; 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://img-blog.csdnimg.cn/2021032317451040.png) + +![309.最佳买卖股票时机含冷冻期](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032317451040.png) 最后结果是取 状态二,状态三,和状态四的最大值,不少同学会把状态四忘了,状态四是冷冻期,最后一天如果是冷冻期也可能是最大值。 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 c718ef4757..3be72565be 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" @@ -106,7 +106,7 @@ dp[0] = 0; 以输入:coins = [1, 2, 5], amount = 5为例 -![322.零钱兑换](https://img-blog.csdnimg.cn/20210201111833906.jpg) +![322.零钱兑换](https://code-thinking-1253855093.file.myqcloud.com/pics/20210201111833906.jpg) dp[amount]为最终结果。 diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 17ae964161..9bd5df7aad 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -57,7 +57,7 @@ 对于死循环,我来举一个有重复机场的例子: -![332.重新安排行程](https://img-blog.csdnimg.cn/20201115180537865.png) +![332.重新安排行程](https://code-thinking-1253855093.file.myqcloud.com/pics/20201115180537865.png) 为什么要举这个例子呢,就是告诉大家,出发机场和到达机场也会重复的,**如果在解题的过程中没有对集合元素处理好,就会死循环。** @@ -111,7 +111,7 @@ void backtracking(参数) { 本题以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![332.重新安排行程1](https://img-blog.csdnimg.cn/2020111518065555.png) +![332.重新安排行程1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555-20230310121223600.png) 开始回溯三部曲讲解: @@ -137,7 +137,7 @@ bool backtracking(int ticketNum, vector& result) { 因为我们只需要找到一个行程,就是在树形结构中唯一的一条通向叶子节点的路线,如图: -![332.重新安排行程1](https://img-blog.csdnimg.cn/2020111518065555.png) +![332.重新安排行程1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555-20230310121240991.png) 所以找到了这个叶子节点了直接返回,这个递归函数的返回值问题我们在讲解二叉树的系列的时候,在这篇[二叉树:递归函数究竟什么时候需要返回值,什么时候不要返回值?](https://programmercarl.com/0112.路径总和.html)详细介绍过。 diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 97cf234fba..f75c4c88ad 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -13,7 +13,8 @@ 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 -![337.打家劫舍III](https://img-blog.csdnimg.cn/20210223173849619.png) + +![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png) ## 思路 diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 4170f7e6db..c2fbbdde68 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -129,7 +129,7 @@ for (int i = 3; i <= n ; i++) { 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://img-blog.csdnimg.cn/20210104173021581.png) +![343.整数拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104173021581.png) 以上动规五部曲分析完毕,C++代码如下: diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index ed6a5d97c8..0d25cba50d 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -16,7 +16,7 @@ 题意:给定两个数组,编写一个函数来计算它们的交集。 -![349. 两个数组的交集](https://img-blog.csdnimg.cn/20200818193523911.png) +![349. 两个数组的交集](https://code-thinking-1253855093.file.myqcloud.com/pics/20200818193523911.png) **说明:** 输出结果中的每个元素一定是唯一的。 diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index efb9c6b65a..5eb61ed075 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -41,7 +41,7 @@ 用示例二来举例,如图所示: -![376.摆动序列](https://img-blog.csdnimg.cn/20201124174327597.png) +![376.摆动序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124174327597.png) **局部最优:删除单调坡度上的节点(不包括单调坡度两端的节点),那么这个坡度就可以有两个局部峰值**。 @@ -103,7 +103,7 @@ 那么为了规则统一,针对序列[2,5],可以假设为[2,2,5],这样它就有坡度了即preDiff = 0,如图: -![376.摆动序列1](https://img-blog.csdnimg.cn/20201124174357612.png) +![376.摆动序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124174357612.png) 针对以上情形,result初始为1(默认最右面有一个峰值),此时curDiff > 0 && preDiff <= 0,那么result++(计算了左面的峰值),最后得到的result就是2(峰值个数为2即摆动序列长度为2) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index 0b7a2fdd2d..f228718857 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -105,7 +105,7 @@ dp[i](考虑nums[j])可以由 dp[i - nums[j]](不考虑nums[j]) 推导 我们再来用示例中的例子推导一下: -![377.组合总和Ⅳ](https://img-blog.csdnimg.cn/20210131174250148.jpg) +![377.组合总和Ⅳ](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000625.png) 如果代码运行处的结果不是想要的结果,就把dp[i]都打出来,看看和我们推导的一不一样。 diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 03c1baae63..2fa647d200 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -77,7 +77,8 @@ if (s[i - 1] != t[j - 1]),此时相当于t要删除元素,t如果把当前 因为这样的定义在dp二维矩阵中可以留出初始化的区间,如图: -![392.判断子序列](https://img-blog.csdnimg.cn/20210303173115966.png) + +![392.判断子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303173115966.png) 如果要是定义的dp[i][j]是以下标i为结尾的字符串s和以下标j为结尾的字符串t,初始化就比较麻烦了。 @@ -94,13 +95,15 @@ vector> dp(s.size() + 1, vector(t.size() + 1, 0)); 如图所示: -![392.判断子序列1](https://img-blog.csdnimg.cn/20210303172354155.jpg) + +![392.判断子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303172354155.jpg) 5. 举例推导dp数组 以示例一为例,输入:s = "abc", t = "ahbgdc",dp状态转移图如下: -![392.判断子序列2](https://img-blog.csdnimg.cn/2021030317364166.jpg) + +![392.判断子序列2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021030317364166.jpg) dp[i][j]表示以下标i-1为结尾的字符串s和以下标j-1为结尾的字符串t 相同子序列的长度,所以如果dp[s.size()][t.size()] 与 字符串s的长度相同说明:s与t的最长相同子序列就是s,那么s 就是 t 的子序列。 diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 1ff74bc96b..cf5441c4c4 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -13,7 +13,8 @@ 示例: -![404.左叶子之和1](https://img-blog.csdnimg.cn/20210204151927654.png) + +![404.左叶子之和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151927654.png) ## 视频讲解 @@ -27,8 +28,7 @@ 大家思考一下如下图中二叉树,左叶子之和究竟是多少? -![404.左叶子之和](https://img-blog.csdnimg.cn/20210204151949672.png) - +![404.左叶子之和](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151949672.png) **其实是0,因为这棵树根本没有左叶子!** 但看这个图的左叶子之和是多少? diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 73a7affc53..48c498e1ad 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -59,7 +59,7 @@ 以图中{5,2} 为例: -![406.根据身高重建队列](https://img-blog.csdnimg.cn/20201216201851982.png) +![406.根据身高重建队列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216201851982.png) 按照身高排序之后,优先按身高高的people的k来插入,后序插入节点也不会影响前面已经插入的节点,最终按照k的规则完成了队列。 diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 45dd289a21..08d005f57a 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -148,7 +148,8 @@ dp[j]的数值一定是小于等于j的。 用例1,输入[1,5,11,5] 为例,如图: -![416.分割等和子集2](https://img-blog.csdnimg.cn/20210110104240545.png) + +![416.分割等和子集2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110104240545.png) 最后dp[11] == 11,说明可以将这个数组分割成两个子集,使得两个子集的元素和相等。 diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 0655f8c5d0..2602b52873 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -21,7 +21,8 @@ 示例: -![450.删除二叉搜索树中的节点](https://img-blog.csdnimg.cn/20201020171048265.png) + +![450.删除二叉搜索树中的节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20201020171048265.png) # 算法公开课 diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 86bfa49070..6b018f978a 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -74,7 +74,7 @@ 以题目示例: [[10,16],[2,8],[1,6],[7,12]]为例,如图:(方便起见,已经排序) -![452.用最少数量的箭引爆气球](https://img-blog.csdnimg.cn/20201123101929791.png) +![452.用最少数量的箭引爆气球](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123101929791.png) 可以看出首先第一组重叠气球,一定是需要一个箭,气球3,的左边界大于了 第一组重叠气球的最小右边界,所以再需要一支箭来射气球3了。 diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index b40380dbea..8d79d701ac 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -52,7 +52,8 @@ 其实本题并不是多重背包,再来看一下这个图,捋清几种背包的关系 -![416.分割等和子集1](https://img-blog.csdnimg.cn/20210117171307407.png) + +![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407-20230310132423205.png) 多重背包是每个物品,数量不同的情况。 @@ -128,7 +129,7 @@ for (string str : strs) { // 遍历物品 最后dp数组的状态如下所示: -![474.一和零](https://img-blog.csdnimg.cn/20210120111201512.jpg) +![474.一和零](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120111201512.jpg) 以上动规五部曲分析完毕,C++代码如下: diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 6aa81539ec..436dbf01c8 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -46,7 +46,10 @@ 为了有鲜明的对比,我用[4, 7, 6, 7]这个数组来举例,抽象为树形结构如图: -![491. 递增子序列1](https://img-blog.csdnimg.cn/20201124200229824.png) + +![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124200229824.png) + + ### 回溯三部曲 @@ -78,7 +81,7 @@ if (path.size() > 1) { * 单层搜索逻辑 -![491. 递增子序列1](https://img-blog.csdnimg.cn/20201124200229824.png) +![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124200229824-20230310131640070.png) 在图中可以看出,**同一父节点下的同层上使用过的元素就不能再使用了** 那么单层搜索代码如下: diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 9adf8b0518..3dcac737ba 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -219,7 +219,7 @@ bagSize = (S + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![494.目标和](https://img-blog.csdnimg.cn/20210125120743274.jpg) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210125120743274.jpg) C++代码如下: diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 4214e232cd..b7ef606fe6 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -25,7 +25,7 @@ 给定 BST [1,null,2,2], -![501. 二叉搜索树中的众数](https://img-blog.csdnimg.cn/20201014221532206.png) +![501. 二叉搜索树中的众数](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014221532206.png) 返回[2]. @@ -146,7 +146,7 @@ public: 如图: -![501.二叉搜索树中的众数1](https://img-blog.csdnimg.cn/20210204152758889.png) +![501.二叉搜索树中的众数1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204152758889.png) 中序遍历代码如下: diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index d08948822e..5ff254845d 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -11,13 +11,14 @@ 给定一个二叉树,在树的最后一行找到最左边的值。 + 示例 1: -![513.找树左下角的值](https://img-blog.csdnimg.cn/20210204152956836.png) +![513.找树左下角的值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204152956836.png) 示例 2: -![513.找树左下角的值1](https://img-blog.csdnimg.cn/20210204153017586.png) +![513.找树左下角的值1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153017586.png) ## 视频讲解 diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index c8f10c242e..d28a33ccd0 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -1,10 +1,12 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 516.最长回文子序列 + +# 516.最长回文子序列 [力扣题目链接](https://leetcode.cn/problems/longest-palindromic-subsequence/) @@ -52,7 +54,7 @@ 如果s[i]与s[j]相同,那么dp[i][j] = dp[i + 1][j - 1] + 2; 如图: -![516.最长回文子序列](https://img-blog.csdnimg.cn/20210127151350563.jpg) +![516.最长回文子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151350563.jpg) (如果这里看不懂,回忆一下dp[i][j]的定义) @@ -64,7 +66,7 @@ 那么dp[i][j]一定是取最大的,即:dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); -![516.最长回文子序列1](https://img-blog.csdnimg.cn/20210127151420476.jpg) +![516.最长回文子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151420476.jpg) 代码如下: @@ -91,7 +93,7 @@ for (int i = 0; i < s.size(); i++) dp[i][i] = 1; 4. 确定遍历顺序 -从递归公式中,可以看出,dp[i][j] 依赖于 dp[i + 1][j - 1] ,dp[i + 1][j] 和 dp[i][j - 1],如图: +从递归公式中,可以看出,dp[i][j] 依赖于 dp[i + 1][j - 1] ,dp[i + 1][j] 和 dp[i][j - 1],如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230102172155.png) @@ -117,7 +119,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 输入s:"cbbd" 为例,dp数组状态如图: -![516.最长回文子序列3](https://img-blog.csdnimg.cn/20210127151521432.jpg) +![516.最长回文子序列3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151521432.jpg) 红色框即:dp[0][s.size() - 1]; 为最终结果。 @@ -147,6 +149,7 @@ public: Java: + ```java public class Solution { public int longestPalindromeSubseq(String s) { @@ -169,6 +172,7 @@ public class Solution { Python: + ```python class Solution: def longestPalindromeSubseq(self, s: str) -> int: @@ -185,6 +189,7 @@ class Solution: ``` Go: + ```Go func longestPalindromeSubseq(s string) int { size := len(s) @@ -213,11 +218,12 @@ func longestPalindromeSubseq(s string) int { ``` Javascript: + ```javascript const longestPalindromeSubseq = (s) => { const strLen = s.length; let dp = Array.from(Array(strLen), () => Array(strLen).fill(0)); - + for(let i = 0; i < strLen; i++) { dp[i][i] = 1; } @@ -241,7 +247,7 @@ TypeScript: ```typescript function longestPalindromeSubseq(s: string): number { /** - dp[i][j]:[i,j]区间内,最长回文子序列的长度 + dp[i][j]:[i,j]区间内,最长回文子序列的长度 */ const length: number = s.length; const dp: number[][] = new Array(length).fill(0) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 162bbe680e..3fd03e1607 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -158,7 +158,7 @@ for (int j = 0; j <= amount; j++) { // 遍历背包容量 输入: amount = 5, coins = [1, 2, 5] ,dp状态图如下: -![518.零钱兑换II](https://img-blog.csdnimg.cn/20210120181331461.jpg) +![518.零钱兑换II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120181331461.jpg) 最后红色框dp[amount]为最终结果。 diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index d59ec09c0e..203add397d 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -15,7 +15,7 @@ 示例: -![530二叉搜索树的最小绝对差](https://img-blog.csdnimg.cn/20201014223400123.png) +![530二叉搜索树的最小绝对差](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014223400123.png) 提示:树中至少有 2 个节点。 @@ -72,7 +72,7 @@ public: 如图: -![530.二叉搜索树的最小绝对差](https://img-blog.csdnimg.cn/20210204153247458.png) +![530.二叉搜索树的最小绝对差](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153247458.png) 一些同学不知道在递归中如何记录前一个节点的指针,其实实现起来是很简单的,大家只要看过一次,写过一次,就掌握了。 diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index 8d4b97f18f..29b2184059 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -19,7 +19,8 @@ 示例 1: -![538.把二叉搜索树转换为累加树](https://img-blog.csdnimg.cn/20201023160751832.png) + +![538.把二叉搜索树转换为累加树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201023160751832.png) * 输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] * 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] @@ -67,7 +68,8 @@ 遍历顺序如图所示: -![538.把二叉搜索树转换为累加树](https://img-blog.csdnimg.cn/20210204153440666.png) + +![538.把二叉搜索树转换为累加树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153440666.png) 本题依然需要一个pre指针记录当前遍历节点cur的前一个节点,这样才方便做累加。 diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 68c6de1609..eb046a9d04 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -78,7 +78,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 以word1:"sea",word2:"eat"为例,推导dp数组状态图如下: -![583.两个字符串的删除操作1](https://img-blog.csdnimg.cn/20210714101750205.png) +![583.两个字符串的删除操作1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210714101750205.png) 以上分析完毕,代码如下: diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 058deb8521..f98948f0f3 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -15,7 +15,7 @@ 示例 1: -![617.合并二叉树](https://img-blog.csdnimg.cn/20210204153634809.png) +![617.合并二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000854.png) 注意: 合并必须从两个树的根节点开始。 diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index df55d24da3..90e6da9fbf 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -102,7 +102,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://img-blog.csdnimg.cn/20210121171032473.jpg) +![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473-20230310132134822.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -132,7 +132,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://img-blog.csdnimg.cn/20210121171059951.jpg) +![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951-20230310132153163.jpg) 图中有6个true,所以就是有6个回文子串。 diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 0f473228ad..64b38b488e 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -19,7 +19,7 @@ 示例 : -![654.最大二叉树](https://img-blog.csdnimg.cn/20210204154534796.png) +![654.最大二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204154534796.png) 提示: diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 1fd6fce098..18d8a0cc8d 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ > 如果不对递归有深刻的理解,本题有点难 > 单纯移除一个节点那还不够,要修剪! @@ -12,13 +14,13 @@ [力扣题目链接](https://leetcode.cn/problems/trim-a-binary-search-tree/) -给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。 +给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。 -![669.修剪二叉搜索树](https://img-blog.csdnimg.cn/20201014173115788.png) +![669.修剪二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173115788.png) -![669.修剪二叉搜索树1](https://img-blog.csdnimg.cn/20201014173219142.png) +![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173219142.png) -# 算法公开课 +# 算法公开课 **《代码随想录》算法视频公开课:[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 @@ -50,7 +52,7 @@ public: 我们在重新关注一下第二个示例,如图: -![669.修剪二叉搜索树](https://img-blog.csdnimg.cn/20210204155302751.png) +![669.修剪二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155302751.png) **所以以上的代码是不可行的!** @@ -60,7 +62,7 @@ public: 在上图中我们发现节点0并不符合区间要求,那么将节点0的右孩子 节点2 直接赋给 节点3的左孩子就可以了(就是把节点0从二叉树中移除),如图: -![669.修剪二叉搜索树1](https://img-blog.csdnimg.cn/20210204155327203.png) +![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155327203.png) 理解了最关键部分了我们再递归三部曲: @@ -127,9 +129,10 @@ return root; 在回顾一下上面的代码,针对下图中二叉树的情况: -![669.修剪二叉搜索树1](https://img-blog.csdnimg.cn/20210204155327203.png) +![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155327203-20230310120126738.png) 如下代码相当于把节点0的右孩子(节点2)返回给上一层, + ``` if (root->val < low) { TreeNode* right = trimBST(root->right, low, high); // 寻找符合区间[low, high]的节点 @@ -243,7 +246,7 @@ public: # 其他语言版本 -## Java +## Java ```Java class Solution { @@ -266,8 +269,10 @@ class Solution { ``` -## Python +## Python + **递归** + ```python # Definition for a binary tree node. # class TreeNode: @@ -287,7 +292,7 @@ class Solution: if root.val < low: # 若当前root节点小于左界:只考虑其右子树,用于替代更新后的其本身,抛弃其左子树整体 return self.trimBST(root.right, low, high) - + if high < root.val: # 若当前root节点大于右界:只考虑其左子树,用于替代更新后的其本身,抛弃其右子树整体 return self.trimBST(root.left, low, high) @@ -300,6 +305,7 @@ class Solution: ``` **迭代** + ```python class Solution: def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: @@ -325,10 +331,9 @@ class Solution: return root ``` -## Go +## Go ```go - // 递归 func trimBST(root *TreeNode, low int, high int) *TreeNode { if root == nil { @@ -384,6 +389,7 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode { ## JavaScript版本 迭代: + ```js var trimBST = function(root, low, high) { if(root === null) { @@ -416,8 +422,9 @@ var trimBST = function(root, low, high) { ``` 递归: + ```js -var trimBST = function (root,low,high) { +var trimBST = function (root,low,high) { if(root === null) { return null; } @@ -486,6 +493,7 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n ## Scala 递归法: + ```scala object Solution { def trimBST(root: TreeNode, low: Int, high: Int): TreeNode = { @@ -502,6 +510,7 @@ object Solution { ## rust // 递归 + ```rust impl Solution { pub fn trim_bst( diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" index 6418be8a55..da80a4e039 100644 --- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" +++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" @@ -180,7 +180,7 @@ for (int i = 0; i < nums.size(); i++) { 输入:[1,3,5,4,7] -![673.最长递增子序列的个数](https://img-blog.csdnimg.cn/20210112104555234.jpg) +![673.最长递增子序列的个数](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000656.png) **如果代码写出来了,怎么改都通过不了,那么把dp和count打印出来看看对不对!** diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 3cb680600f..79a8311d78 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -82,7 +82,8 @@ for (int i = 1; i < nums.size(); i++) { 已输入nums = [1,3,5,4,7]为例,dp数组状态如下: -![674.最长连续递增序列](https://img-blog.csdnimg.cn/20210204103529742.jpg) + +![674.最长连续递增序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204103529742.jpg) **注意这里要取dp[i]里的最大值,所以dp[2]才是结果!** diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 675a0e4a23..40724f48a8 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -13,7 +13,8 @@ 例如, -![700.二叉搜索树中的搜索](https://img-blog.csdnimg.cn/20210204155522476.png) + +![700.二叉搜索树中的搜索](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155522476.png) 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。 @@ -125,7 +126,7 @@ public: 中间节点如果大于3就向左走,如果小于3就向右走,如图: -![二叉搜索树](https://img-blog.csdnimg.cn/20200812190213280.png) +![二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20200812190213280.png) 所以迭代法代码如下: diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 4bdb8d607f..4e834201bd 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -13,7 +13,8 @@ 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。 -![701.二叉搜索树中的插入操作](https://img-blog.csdnimg.cn/20201019173259554.png) + +![701.二叉搜索树中的插入操作](https://code-thinking-1253855093.file.myqcloud.com/pics/20201019173259554.png) 提示: diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index a02bf7a201..97b123bc03 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -59,7 +59,7 @@ 例如在数组:1,2,3,4,7,9,10中查找元素2,如图所示: -![704.二分查找](https://img-blog.csdnimg.cn/20210311153055723.jpg) +![704.二分查找](https://code-thinking-1253855093.file.myqcloud.com/pics/20210311153055723.jpg) 代码如下:(详细注释) @@ -98,7 +98,8 @@ public: 在数组:1,2,3,4,7,9,10中查找元素2,如图所示:(**注意和方法一的区别**) -![704.二分查找1](https://img-blog.csdnimg.cn/20210311153123632.jpg) + +![704.二分查找1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210311153123632.jpg) 代码如下:(详细注释) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 43ed262296..95560fb138 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -22,7 +22,7 @@ * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。 -![707示例](https://img-blog.csdnimg.cn/20200814200558953.png) +![707示例](https://code-thinking-1253855093.file.myqcloud.com/pics/20200814200558953.png) # 思路 @@ -33,10 +33,10 @@ 如果对链表的虚拟头结点不清楚,可以看这篇文章:[链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html) 删除链表节点: -![链表-删除节点](https://img-blog.csdnimg.cn/20200806195114541.png) +![链表-删除节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195114541.png) 添加链表节点: -![链表-添加节点](https://img-blog.csdnimg.cn/20200806195134331.png) +![链表-添加节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195134331.png) 这道题目设计链表的五个接口: * 获取链表第index个节点的数值 diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 0658f9c8bb..08be67326e 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -93,7 +93,8 @@ for (int i = 1; i <= nums1.size(); i++) { 拿示例1中,A: [1,2,3,2,1],B: [3,2,1,4,7]为例,画一个dp数组的状态变化,如下: -![718.最长重复子数组](https://img-blog.csdnimg.cn/2021011215282060.jpg) + +![718.最长重复子数组](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011215282060.jpg) 以上五部曲分析完毕,C++代码如下: @@ -124,7 +125,8 @@ public: 在如下图中: -![718.最长重复子数组](https://img-blog.csdnimg.cn/2021011215282060.jpg) + +![718.最长重复子数组](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011215282060-20230310134554486.jpg) 我们可以看出dp[i][j]都是由dp[i - 1][j - 1]推出。那么压缩为一维数组,也就是dp[j]都是由dp[j - 1]推出。 diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index 5aa370052f..263a28f40c 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -1,3 +1,4 @@ +

@@ -5,15 +6,16 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 739. 每日温度 [力扣题目链接](https://leetcode.cn/problems/daily-temperatures/) -请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。 +请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。 -例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。 +例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。 -提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。 +提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。 ## 思路 @@ -32,7 +34,7 @@ **单调栈的本质是空间换时间**,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素高的元素,优点是整个数组只需要遍历一次。 -**更直白来说,就是用一个栈来记录我们遍历过的元素**,因为我们遍历数组的时候,我们不知道之前都遍历了哪些元素,以至于遍历一个元素找不到是不是之前遍历过一个更小的,所以我们需要用一个容器(这里用单调栈)来记录我们遍历过的元素。 +**更直白来说,就是用一个栈来记录我们遍历过的元素**,因为我们遍历数组的时候,我们不知道之前都遍历了哪些元素,以至于遍历一个元素找不到是不是之前遍历过一个更小的,所以我们需要用一个容器(这里用单调栈)来记录我们遍历过的元素。 在使用单调栈的时候首先要明确如下几点: @@ -59,79 +61,79 @@ **把这三种情况分析清楚了,也就理解透彻了**。 -接下来我们用temperatures = [73, 74, 75, 71, 71, 72, 76, 73]为例来逐步分析,输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。 +接下来我们用temperatures = [73, 74, 75, 71, 71, 72, 76, 73]为例来逐步分析,输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。 -------- +------- 首先先将第一个遍历元素加入单调栈 -![739.每日温度1](https://img-blog.csdnimg.cn/20210219124434172.jpg) +![739.每日温度1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124434172.jpg) ---------- +--------- 加入T[1] = 74,因为T[1] > T[0](当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况)。 我们要保持一个递增单调栈(从栈头到栈底),所以将T[0]弹出,T[1]加入,此时result数组可以记录了,result[0] = 1,即T[0]右面第一个比T[0]大的元素是T[1]。 -![739.每日温度2](https://img-blog.csdnimg.cn/20210219124504299.jpg) +![739.每日温度2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124504299.jpg) ------------ +----------- 加入T[2],同理,T[1]弹出 -![739.每日温度3](https://img-blog.csdnimg.cn/20210219124527361.jpg) +![739.每日温度3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124527361.jpg) -------- +------- 加入T[3],T[3] < T[2] (当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况),加T[3]加入单调栈。 -![739.每日温度4](https://img-blog.csdnimg.cn/20210219124610761.jpg) +![739.每日温度4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124610761.jpg) ---------- +--------- 加入T[4],T[4] == T[3] (当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况),此时依然要加入栈,不用计算距离,因为我们要求的是右面第一个大于本元素的位置,而不是大于等于! -![739.每日温度5](https://img-blog.csdnimg.cn/20210219124633444.jpg) +![739.每日温度5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124633444.jpg) ---------- +--------- 加入T[5],T[5] > T[4] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[4]弹出,同时计算距离,更新result -![739.每日温度6](https://img-blog.csdnimg.cn/20210219124700567.jpg) +![739.每日温度6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124700567.jpg) ----------- +---------- T[4]弹出之后, T[5] > T[3] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[3]继续弹出,同时计算距离,更新result -![739.每日温度7](https://img-blog.csdnimg.cn/20210219124726613.jpg) +![739.每日温度7](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124726613.jpg) -------- +------- 直到发现T[5]小于T[st.top()],终止弹出,将T[5]加入单调栈 -![739.每日温度8](https://img-blog.csdnimg.cn/20210219124807715.jpg) +![739.每日温度8](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124807715.jpg) -------- +------- 加入T[6],同理,需要将栈里的T[5],T[2]弹出 -![739.每日温度9](https://img-blog.csdnimg.cn/2021021912483374.jpg) +![739.每日温度9](https://code-thinking-1253855093.file.myqcloud.com/pics/2021021912483374.jpg) -------- +------- 同理,继续弹出 -![739.每日温度10](https://img-blog.csdnimg.cn/2021021912490098.jpg) +![739.每日温度10](https://code-thinking-1253855093.file.myqcloud.com/pics/2021021912490098.jpg) ------- +------ 此时栈里只剩下了T[6] -![739.每日温度11](https://img-blog.csdnimg.cn/20210219124930156.jpg) +![739.每日温度11](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124930156.jpg) ------------ 加入T[7], T[7] < T[6] 直接入栈,这就是最后的情况,result数组也更新完了。 -![739.每日温度12](https://img-blog.csdnimg.cn/20210219124957216.jpg) +![739.每日温度12](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124957216.jpg) 此时有同学可能就疑惑了,那result[6] , result[7]怎么没更新啊,元素也一直在栈里。 @@ -144,7 +146,7 @@ T[4]弹出之后, T[5] > T[3] (当前遍历的元素T[i]大于栈顶元素T[ * 情况二:当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况 * 情况三:当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况 -通过以上过程,大家可以自己再模拟一遍,就会发现:只有单调栈递增(从栈口到栈底顺序),就是求右边第一个比自己大的,单调栈递减的话,就是求右边第一个比自己小的。 +通过以上过程,大家可以自己再模拟一遍,就会发现:只有单调栈递增(从栈口到栈底顺序),就是求右边第一个比自己大的,单调栈递减的话,就是求右边第一个比自己小的。 C++代码如下: @@ -198,6 +200,7 @@ public: } }; ``` + * 时间复杂度:O(n) * 空间复杂度:O(n) @@ -210,26 +213,26 @@ public: Java: -```java +```java class Solution { - // 版本 1 + // 版本 1 public int[] dailyTemperatures(int[] temperatures) { - + int lens=temperatures.length; int []res=new int[lens]; - + /** 如果当前遍历的元素 大于栈顶元素,表示 栈顶元素的 右边的最大的元素就是 当前遍历的元素, - 所以弹出 栈顶元素,并记录 - 如果栈不空的话,还要考虑新的栈顶与当前元素的大小关系 + 所以弹出 栈顶元素,并记录 + 如果栈不空的话,还要考虑新的栈顶与当前元素的大小关系 否则的话,可以直接入栈。 注意,单调栈里 加入的元素是 下标。 */ Deque stack=new LinkedList<>(); stack.push(0); for(int i=1;i stack=new LinkedList<>(); for(int i=0;itemperatures[stack.peek()]){ res[stack.peek()]=i-stack.peek(); stack.pop(); @@ -263,10 +266,12 @@ class Solution { return res; } } - + } ``` + Python: + ```python class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: @@ -282,9 +287,10 @@ class Solution: answer[stack[-1]]=i-stack[-1] stack.pop() stack.append(i) - + return answer ``` + Go: > 暴力法 @@ -341,6 +347,7 @@ func dailyTemperatures(temperatures []int) []int { ``` > 单调栈法(精简版本) + ```go // 单调递减栈 func dailyTemperatures(num []int) []int { @@ -362,6 +369,7 @@ func dailyTemperatures(num []int) []int { ``` JavaScript: + ```javascript // 版本一 var dailyTemperatures = function(temperatures) { diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 6667e7403e..077cb54ac4 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -41,7 +41,8 @@ 如图: -![763.划分字母区间](https://img-blog.csdnimg.cn/20201222191924417.png) + +![763.划分字母区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201222191924417.png) 明白原理之后,代码并不复杂,如下: diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index 92212ebbea..14563a51fb 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 968.监控二叉树 [力扣题目链接](https://leetcode.cn/problems/binary-tree-cameras/) @@ -17,7 +19,7 @@ 示例 1: -![](https://img-blog.csdnimg.cn/20201229175736596.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229175736596.png) * 输入:[0,0,null,0,0] * 输出:1 @@ -25,7 +27,7 @@ 示例 2: -![](https://img-blog.csdnimg.cn/2020122917584449.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/2020122917584449.png) * 输入:[0,0,null,0,null,0,null,null,0] * 输出:2 @@ -139,7 +141,7 @@ if (cur == NULL) return 2; 如图: -![968.监控二叉树2](https://img-blog.csdnimg.cn/20201229203710729.png) +![968.监控二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229203710729.png) 代码如下: @@ -163,6 +165,7 @@ if (left == 2 && right == 2) return 0; 此时摄像头的数量要加一,并且return 1,代表中间节点放摄像头。 代码如下: + ```CPP if (left == 0 || right == 0) { result++; @@ -186,7 +189,7 @@ if (left == 1 || right == 1) return 2; **从这个代码中,可以看出,如果left == 1, right == 0 怎么办?其实这种条件在情况2中已经判断过了**,如图: -![968.监控二叉树1](https://img-blog.csdnimg.cn/2020122920362355.png) +![968.监控二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020122920362355.png) 这种情况也是大多数同学容易迷惑的情况。 @@ -194,7 +197,7 @@ if (left == 1 || right == 1) return 2; 以上都处理完了,递归结束之后,可能头结点 还有一个无覆盖的情况,如图: -![968.监控二叉树3](https://img-blog.csdnimg.cn/20201229203742446.png) +![968.监控二叉树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229203742446.png) 所以递归结束之后,还要判断根节点,如果没有覆盖,result++,代码如下: @@ -311,7 +314,8 @@ public: ## 其他语言版本 -### Java +### Java + ```java class Solution { int res=0; @@ -324,32 +328,32 @@ class Solution { } /** 节点的状态值: - 0 表示无覆盖 + 0 表示无覆盖 1 表示 有摄像头 - 2 表示有覆盖 + 2 表示有覆盖 后序遍历,根据左右节点的情况,来判读 自己的状态 */ public int minCame(TreeNode root){ if(root==null){ - // 空节点默认为 有覆盖状态,避免在叶子节点上放摄像头 + // 空节点默认为 有覆盖状态,避免在叶子节点上放摄像头 return 2; } int left=minCame(root.left); int right=minCame(root.right); - + // 如果左右节点都覆盖了的话, 那么本节点的状态就应该是无覆盖,没有摄像头 if(left==2&&right==2){ - //(2,2) + //(2,2) return 0; }else if(left==0||right==0){ // 左右节点都是无覆盖状态,那 根节点此时应该放一个摄像头 - // (0,0) (0,1) (0,2) (1,0) (2,0) + // (0,0) (0,1) (0,2) (1,0) (2,0) // 状态值为 1 摄像头数 ++; res++; return 1; }else{ // 左右节点的 状态为 (1,1) (1,2) (2,1) 也就是左右节点至少存在 1个摄像头, - // 那么本节点就是处于被覆盖状态 + // 那么本节点就是处于被覆盖状态 return 2; } } @@ -357,7 +361,8 @@ class Solution { ``` -### Python +### Python + ```python class Solution: def minCameraCover(self, root: TreeNode) -> int: @@ -367,19 +372,19 @@ class Solution: # 0: 该节点未覆盖 # 1: 该节点有摄像头 # 2: 该节点有覆盖 - + result = 0 # 从下往上遍历:后序(左右中) def traversal(curr: TreeNode) -> int: nonlocal result - + if not curr: return 2 left = traversal(curr.left) right = traversal(curr.right) # Case 1: # 左右节点都有覆盖 - if left == 2 and right == 2: + if left == 2 and right == 2: return 0 # Case 2: @@ -388,7 +393,7 @@ class Solution: # left == 0 && right == 1 左节点有无覆盖,右节点摄像头 # left == 0 && right == 2 左节点无覆盖,右节点覆盖 # left == 2 && right == 0 左节点覆盖,右节点无覆盖 - elif left == 0 or right == 0: + elif left == 0 or right == 0: result += 1 return 1 @@ -398,16 +403,17 @@ class Solution: # left == 1 && right == 1 左右节点都有摄像头 elif left == 1 or right == 1: return 2 - + # 其他情况前段代码均已覆盖 if traversal(root) == 0: result += 1 - + return result ``` -### Go +### Go + ```go const inf = math.MaxInt64 / 2 @@ -437,7 +443,8 @@ func min(a, b int) int { ``` -### Javascript +### Javascript + ```Javascript var minCameraCover = function(root) { let result = 0 @@ -470,7 +477,7 @@ var minCameraCover = function(root) { } return result - + }; ``` @@ -501,7 +508,7 @@ function minCameraCover(root: TreeNode | null): number { }; ``` -### C +### C ```c /* @@ -576,7 +583,9 @@ object Solution { } } ``` + ### Rust + ```Rust /// 版本一 impl Solution { diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 6742c8d90a..d1e1e30a8c 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -14,7 +14,8 @@ 以这种方法绘制线条,并返回我们可以绘制的最大连线数。 -![1035.不相交的线](https://img-blog.csdnimg.cn/2021032116363533.png) + +![1035.不相交的线](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032116363533.png) ## 思路 diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 8e40dcd59f..210ce73738 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -115,7 +115,7 @@ for (int i = 0; i < stones.size(); i++) { // 遍历物品 举例,输入:[2,4,1,1],此时target = (2 + 4 + 1 + 1)/2 = 4 ,dp数组状态图如下: -![1049.最后一块石头的重量II](https://img-blog.csdnimg.cn/20210121115805904.jpg) +![1049.最后一块石头的重量II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121115805904.jpg) 最后dp[target]里是容量为target的背包所能背的最大重量。 diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index f2e6e7e2b2..8e5f7eb252 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -91,7 +91,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 从递推公式,可以看出,有三个方向可以推出dp[i][j],如图: -![1143.最长公共子序列](https://img-blog.csdnimg.cn/20210204115139616.jpg) +![1143.最长公共子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204115139616.jpg) 那么为了在递推的过程中,这三个方向都是经过计算的数值,所以要从前向后,从上到下来遍历这个矩阵。 @@ -99,7 +99,8 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 以输入:text1 = "abcde", text2 = "ace" 为例,dp状态如图: -![1143.最长公共子序列1](https://img-blog.csdnimg.cn/20210210150215918.jpg) + +![1143.最长公共子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210210150215918.jpg) 最后红框dp[text1.size()][text2.size()]为最终结果 diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index c2854ca84f..a488c0bad4 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -1,18 +1,21 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 程序提交之后为什么会超时?O(n)的算法会超时,n究竟是多大? + 一些同学可能对计算机运行的速度还没有概念,就是感觉计算机运行速度应该会很快,那么在leetcode上做算法题目的时候为什么会超时呢? 计算机究竟1s可以执行多少次操作呢? 接下来探讨一下这个问题。 # 超时是怎么回事 -![程序超时](https://img-blog.csdnimg.cn/20200729112716117.png) +![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -52,6 +55,7 @@ 尽管有很多因素影响,但是还是可以对自己程序的运行时间有一个大体的评估的。 引用算法4里面的一段话: + * 火箭科学家需要大致知道一枚试射火箭的着陆点是在大海里还是在城市中; * 医学研究者需要知道一次药物测试是会杀死还是会治愈实验对象; @@ -103,6 +107,7 @@ void function3(long long n) { ``` 来看一下这三个函数随着n的规模变化,耗时会产生多大的变化,先测function1 ,就把 function2 和 function3 注释掉 + ```CPP int main() { long long n; // 数据规模 @@ -126,11 +131,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://img-blog.csdnimg.cn/20200729200018460.png) +![程序超时2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729200018460.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下O(n^2) 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://img-blog.csdnimg.cn/2020072919590970.png) +![程序超时3](https://code-thinking-1253855093.file.myqcloud.com/pics/2020072919590970.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -138,7 +143,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 O(n)少一个数量级,因为logn的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://img-blog.csdnimg.cn/20200729195729407.png) +![程序超时4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729195729407.png) O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -146,7 +151,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 **整体测试数据整理如下:** -![程序超时1](https://img-blog.csdnimg.cn/20201208231559175.png) +![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175.png) 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 9541baafca..3afedc8249 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,18 +1,20 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 二叉树理论基础篇 《代码随想录》算法视频公开课:[关于二叉树,你该了解这些!](https://www.bilibili.com/video/BV1Hy4y1t7ij),相信结合视频在看本篇题解,更有助于大家对本题的理解。 -题目分类大纲如下: +题目分类大纲如下: -二叉树大纲 +二叉树大纲 说到二叉树,大家对于二叉树其实都很熟悉了,本文呢我也不想教科书式的把二叉树的基础内容再啰嗦一遍,所以以下我讲的都是一些比较重点的内容。 @@ -28,7 +30,7 @@ 如图所示: - + 这棵二叉树为满二叉树,也可以说深度为k,有2^k-1个节点的二叉树。 @@ -37,13 +39,13 @@ 什么是完全二叉树? -完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1)  个节点。 +完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1) 个节点。 **大家要自己看完全二叉树的定义,很多同学对完全二叉树其实不是真正的懂了。** 我来举一个典型的例子如题: - + 相信不少同学最后一个二叉树是不是完全二叉树都中招了。 @@ -60,16 +62,16 @@ 下面这两棵树都是搜索树 - + ### 平衡二叉搜索树 -平衡二叉搜索树:又被称为AVL(Adelson-Velsky and Landis)树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。 +平衡二叉搜索树:又被称为AVL(Adelson-Velsky and Landis)树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。 如图: - + 最后一棵 不是平衡二叉树,因为它的左右两个子树的高度差的绝对值超过了1。 @@ -88,13 +90,13 @@ 链式存储如图: - + 链式存储是大家很熟悉的一种方式,那么我们来看看如何顺序存储呢? 其实就是用数组来存储二叉树,顺序存储的方式如图: - + 用数组来存储二叉树如何遍历的呢? @@ -113,6 +115,7 @@ 我这里把二叉树的几种遍历方式列出来,大家就可以一一串起来了。 二叉树主要有两种遍历方式: + 1. 深度优先遍历:先往深走,遇到叶子节点再往回走。 2. 广度优先遍历:一层一层的去遍历。 @@ -121,11 +124,11 @@ 那么从深度优先遍历和广度优先遍历进一步拓展,才有如下遍历方式: * 深度优先遍历 - * 前序遍历(递归法,迭代法) - * 中序遍历(递归法,迭代法) - * 后序遍历(递归法,迭代法) + * 前序遍历(递归法,迭代法) + * 中序遍历(递归法,迭代法) + * 后序遍历(递归法,迭代法) * 广度优先遍历 - * 层次遍历(迭代法) + * 层次遍历(迭代法) 在深度优先遍历中:有三个顺序,前中后序遍历, 有同学总分不清这三个顺序,经常搞混,我这里教大家一个技巧。 @@ -140,7 +143,7 @@ 大家可以对着如下图,看看自己理解的前后中序有没有问题。 - + 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 @@ -206,8 +209,9 @@ public class TreeNode { Python: + ```python -class TreeNode: +class TreeNode: def __init__(self, value): self.value = value self.left = None @@ -215,6 +219,7 @@ class TreeNode: ``` Go: + ```go type TreeNode struct { Val int @@ -224,6 +229,7 @@ type TreeNode struct { ``` JavaScript: + ```javascript function TreeNode(val, left, right) { this.val = (val===undefined ? 0 : val) @@ -263,7 +269,9 @@ class TreeNode { } } ``` + Scala: + ```scala class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { var value: Int = _value diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 0814866fde..35cf4077a5 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -116,7 +116,7 @@ public: 再来看后序遍历,先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: -![前序到后序](https://img-blog.csdnimg.cn/20200808200338924.png) +![前序到后序](https://code-thinking-1253855093.file.myqcloud.com/pics/20200808200338924.png) **所以后序遍历只需要前序遍历的代码稍作修改就可以了,代码如下:** diff --git "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index 246294ede0..c479dddc67 100644 --- "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -31,7 +31,7 @@ 同样的同理再看一下快速排序,都知道快速排序是O(nlog n),但是当数据已经有序情况下,快速排序的时间复杂度是O(n^2) 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是O(n^2)**。 **但是我们依然说快速排序是O(nlog n)的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示: -![时间复杂度4,一般情况下的时间复杂度](https://img-blog.csdnimg.cn/20200728185745611.png) +![时间复杂度4,一般情况下的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728185745611.png) 我们主要关心的还是一般情况下的数据形式。 @@ -42,7 +42,7 @@ 如下图中可以看出不同算法的时间复杂度在不同数据输入规模下的差异。 -![时间复杂度,不同数据规模的差异](https://img-blog.csdnimg.cn/20200728191447384.png) +![时间复杂度,不同数据规模的差异](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447384.png) 在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。 @@ -108,7 +108,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 为什么可以这么做呢?如下图所示: -![时间复杂度1.png](https://img-blog.csdnimg.cn/20200728191447349.png) +![时间复杂度1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447349.png) 假如有两个算法的时间复杂度,分别是log以2为底n的对数和log以10为底n的对数,那么这里如果还记得高中数学的话,应该不能理解`以2为底n的对数 = 以2为底10的对数 * 以10为底n的对数`。 diff --git "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 20a48e191e..8b0934c582 100644 --- "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -8,7 +8,7 @@ ## 超时是怎么回事 -![程序超时](https://img-blog.csdnimg.cn/20200729112716117.png) +![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117-20230310124308704.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -48,6 +48,7 @@ 尽管有很多因素影响,但是还是可以对自己程序的运行时间有一个大体的评估的。 引用算法4里面的一段话: + * 火箭科学家需要大致知道一枚试射火箭的着陆点是在大海里还是在城市中; * 医学研究者需要知道一次药物测试是会杀死还是会治愈实验对象; @@ -99,6 +100,7 @@ void function3(long long n) { ``` 来看一下这三个函数随着n的规模变化,耗时会产生多大的变化,先测function1 ,就把 function2 和 function3 注释掉 + ```CPP int main() { long long n; // 数据规模 @@ -122,11 +124,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://img-blog.csdnimg.cn/20200729200018460.png) +![程序超时2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729200018460-20230310124315093.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下$O(n^2)$ 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://img-blog.csdnimg.cn/2020072919590970.png) +![程序超时3](https://code-thinking-1253855093.file.myqcloud.com/pics/2020072919590970-20230310124318532.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -134,7 +136,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 $O(n)$少一个数量级,因为$\log n$的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://img-blog.csdnimg.cn/20200729195729407.png) +![程序超时4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729195729407-20230310124322232.png) $O(n\log n)$的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -142,7 +144,7 @@ $O(n\log n)$的算法,1s内大概计算机可以运行 2 * (10^7)次计算, **整体测试数据整理如下:** -![程序超时1](https://img-blog.csdnimg.cn/20201208231559175.png) +![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175-20230310124325152.png) 至于 $O(\log n)$ 和 $O(n^3)$ 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 @@ -204,6 +206,7 @@ int main() { Java版本 + ```Java import java.util.Scanner; @@ -274,4 +277,5 @@ public class TimeComplexity { ----------------------- +
diff --git "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" index 196f40272c..b508346080 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" @@ -57,7 +57,7 @@ 我做了一下总结如图: -![编程风格](https://img-blog.csdnimg.cn/20201119173039835.png) +![编程风格](https://code-thinking-1253855093.file.myqcloud.com/pics/20201119173039835.png) ### 水平留白(代码空格) diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index 72cfab542c..9d00a0f2a0 100644 --- "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -38,7 +38,7 @@ 同样的同理再看一下快速排序,都知道快速排序是O(nlogn),但是当数据已经有序情况下,快速排序的时间复杂度是O(n^2) 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是O(n^2)**。 **但是我们依然说快速排序是O(nlogn)的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示: -![时间复杂度4,一般情况下的时间复杂度](https://img-blog.csdnimg.cn/20200728185745611.png) +![时间复杂度4,一般情况下的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728185745611-20230310123844306.png) 我们主要关心的还是一般情况下的数据形式。 @@ -49,7 +49,7 @@ 如下图中可以看出不同算法的时间复杂度在不同数据输入规模下的差异。 -![时间复杂度,不同数据规模的差异](https://img-blog.csdnimg.cn/20200728191447384.png) +![时间复杂度,不同数据规模的差异](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447384-20230310124015324.png) 在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。 @@ -115,7 +115,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 为什么可以这么做呢?如下图所示: -![时间复杂度1.png](https://img-blog.csdnimg.cn/20200728191447349.png) +![时间复杂度1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447349-20230310124032001.png) 假如有两个算法的时间复杂度,分别是log以2为底n的对数和log以10为底n的对数,那么这里如果还记得高中数学的话,应该不难理解`以2为底n的对数 = 以2为底10的对数 * 以10为底n的对数`。 diff --git "a/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" index f4aa4b6ea8..f70564e4a2 100644 --- "a/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" @@ -1,5 +1,4 @@ - # 刷了这么多题,你了解自己代码的内存消耗么? 理解代码的内存消耗,最关键是要知道自己所用编程语言的内存管理。 @@ -20,7 +19,7 @@ 如果我们写C++的程序,就要知道栈和堆的概念,程序运行时所需的内存空间分为 固定部分,和可变部分,如下: -![C++内存空间](https://img-blog.csdnimg.cn/20210309165950660.png) +![C++内存空间](https://code-thinking-1253855093.file.myqcloud.com/pics/20210309165950660.png) 固定部分的内存消耗 是不会随着代码运行产生变化的, 可变部分则是会产生变化的 @@ -42,7 +41,7 @@ 想要算出自己程序会占用多少内存就一定要了解自己定义的数据类型的大小,如下: -![C++数据类型的大小](https://img-blog.csdnimg.cn/20200804193045440.png) +![C++数据类型的大小](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193045440.png) 注意图中有两个不一样的地方,为什么64位的指针就占用了8个字节,而32位的指针占用4个字节呢? @@ -85,9 +84,11 @@ int main() { cout << sizeof(st) << endl; } ``` + 看一下和自己想的结果一样么, 我们来逐一分析一下。 其输出的结果依次为: + ``` 4 1 @@ -108,7 +109,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第一种就是内存对齐的情况,如图: -![内存对齐](https://img-blog.csdnimg.cn/20200804193307347.png) +![内存对齐](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193307347.png) 一字节的char占用了四个字节,空了三个字节的内存地址,int数据从地址4开始。 @@ -116,7 +117,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第二种是没有内存对齐的情况如图: -![非内存对齐](https://img-blog.csdnimg.cn/20200804193353926.png) +![非内存对齐](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193353926.png) char型的数据和int型的数据挨在一起,该int数据从地址1开始,那么CPU想要读这个数据的话来看看需要几步操作: @@ -143,4 +144,5 @@ char型的数据和int型的数据挨在一起,该int数据从地址1开始, ----------------------- +
diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" index e64f547ac8..3e25b895f0 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" @@ -103,7 +103,7 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 最后福利,把我的简历模板贡献出来!如下图所示。 -![简历模板](https://img-blog.csdnimg.cn/20200803175538158.png) +![简历模板](https://code-thinking-1253855093.file.myqcloud.com/pics/20200803175538158.png) 这里是简历模板中Markdown的代码:[https://github.com/youngyangyang04/Markdown-Resume-Template](https://github.com/youngyangyang04/Markdown-Resume-Template) ,可以fork到自己Github仓库上,按照这个模板来修改自己的简历。 diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" index 142358da7b..aacc456857 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" @@ -28,7 +28,8 @@ int fibonacci(int i) { 可以看出上面的代码每次递归都是O(1)的操作。再来看递归了多少次,这里将i为5作为输入的递归过程 抽象成一棵递归树,如图: -![递归空间复杂度分析](https://img-blog.csdnimg.cn/20210305093200104.png) + +![递归空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305093200104.png) 从图中,可以看出f(5)是由f(4)和f(3)相加而来,那么f(4)是由f(3)和f(2)相加而来 以此类推。 @@ -194,7 +195,8 @@ int main() 在看递归的深度是多少呢?如图所示: -![递归空间复杂度分析](https://img-blog.csdnimg.cn/20210305094749554.png) + +![递归空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305094749554.png) 递归第n个斐波那契数的话,递归调用栈的深度就是n。 @@ -211,7 +213,8 @@ int fibonacci(int i) { 最后对各种求斐波那契数列方法的性能做一下分析,如题: -![递归的空间复杂度分析](https://img-blog.csdnimg.cn/20210305095227356.png) + +![递归的空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305095227356.png) 可以看出,求斐波那契数的时候,使用递归算法并不一定是在性能上是最优的,但递归确实简化的代码层面的复杂度。 diff --git "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" index b2db92f541..9f17e6fac7 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" @@ -69,7 +69,7 @@ int function3(int x, int n) { 我们来分析一下,首先看递归了多少次呢,可以把递归抽象出一棵满二叉树。刚刚同学写的这个算法,可以用一棵满二叉树来表示(为了方便表示,选择n为偶数16),如图: -![递归算法的时间复杂度](https://img-blog.csdnimg.cn/20201209193909426.png) +![递归算法的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209193909426.png) 当前这棵二叉树就是求x的n次方,n为16的情况,n为16的时候,进行了多少次乘法运算呢? @@ -79,7 +79,7 @@ int function3(int x, int n) { 这么如果是求x的n次方,这个递归树有多少个节点呢,如下图所示:(m为深度,从0开始) -![递归求时间复杂度](https://img-blog.csdnimg.cn/20200728195531892.png) +![递归求时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728195531892.png) **时间复杂度忽略掉常数项`-1`之后,这个递归算法的时间复杂度依然是O(n)**。对,你没看错,依然是O(n)的时间复杂度! diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 5f59b04022..57416775fb 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -254,5 +254,5 @@ traversal(cur->left, tmp, result); * Github:[leetcode-master](https://github.com/youngyangyang04/leetcode-master) * 知乎:[代码随想录](https://www.zhihu.com/people/sun-xiu-yang-64) -![](https://img-blog.csdnimg.cn/2021013018121150.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/2021013018121150.png)
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 5310f9ecf0..76bd331bd4 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,5 +1,4 @@ - # 本周小结!(回溯算法系列二) > 例行每周小结 @@ -32,7 +31,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)第一个树形结构没有画出startIndex的作用,**这里这里纠正一下,准确的树形结构如图所示:** -![39.组合总和](https://img-blog.csdnimg.cn/20201123202227835.png) +![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123202227835.png) ## 周二 @@ -46,7 +45,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://img-blog.csdnimg.cn/20201123202817973.png) +![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123202817973.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: @@ -80,7 +79,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; **本题的树形结构中,和代码的逻辑有一个小出入,已经判断不是回文的子串就不会进入递归了,纠正如下:** -![131.分割回文串](https://img-blog.csdnimg.cn/20201123203228309.png) +![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203228309.png) ## 周四 @@ -91,7 +90,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形图如下: -![93.复原IP地址](https://img-blog.csdnimg.cn/20201123203735933.png) +![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933-20230310133532452.png) 在本文的树形结构图中,我已经把详细的分析思路都画了出来,相信大家看了之后一定会思路清晰不少! @@ -113,7 +112,7 @@ if (s.size() > 12) return result; // 剪枝 如图: -![78.子集](https://img-blog.csdnimg.cn/202011232041348.png) +![78.子集](https://code-thinking-1253855093.file.myqcloud.com/pics/202011232041348.png) 认清这个本质之后,今天的题目就是一道模板题了。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index af08097b64..ec36d1213d 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,5 +1,4 @@ - # 本周小结!(回溯算法系列三) ## 周一 @@ -12,14 +11,14 @@ 树形结构如下: -![90.子集II](https://img-blog.csdnimg.cn/2020111217110449.png) +![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449-20230310133150714.png) ## 周二 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://img-blog.csdnimg.cn/20201112170832333.png) +![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170832333-20230310133155209.png) [回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)留言区大家有很多疑问,主要还是和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混合在了一起。 @@ -34,7 +33,7 @@ 可以看出元素1在[1,2]中已经使用过了,但是在[2,1]中还要在使用一次1,所以处理排列问题就不用使用startIndex了。 如图: -![46.全排列](https://img-blog.csdnimg.cn/20201112170304979.png) +![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170304979-20230310133201250.png) **大家此时可以感受出排列问题的不同:** @@ -47,7 +46,7 @@ 树形结构如下: -![47.全排列II1](https://img-blog.csdnimg.cn/20201112171930470.png) +![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112171930470-20230310133206398.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -55,11 +54,11 @@ 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://img-blog.csdnimg.cn/20201112172230434.png) +![47.全排列II2.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172230434-20230310133211392.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://img-blog.csdnimg.cn/20201112172327967.png) +![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172327967-20230310133216389.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** @@ -72,14 +71,17 @@ **所以这块就说一说我个人理解,对内容持开放态度,集思广益,欢迎大家来讨论!** 子集问题分析: + * 时间复杂度:$O(n × 2^n)$,因为每一个元素的状态无外乎取与不取,所以时间复杂度为$O(2^n)$,构造每一组子集都需要填进数组,又有需要$O(n)$,最终时间复杂度:$O(n × 2^n)$。 * 空间复杂度:$O(n)$,递归深度为n,所以系统栈所用空间为$O(n)$,每一层递归所用的空间都是常数级别,注意代码里的result和path都是全局变量,就算是放在参数里,传的也是引用,并不会新申请内存空间,最终空间复杂度为$O(n)$。 排列问题分析: + * 时间复杂度:$O(n!)$,这个可以从排列的树形图中很明显发现,每一层节点为n,第二层每一个分支都延伸了n-1个分支,再往下又是n-2个分支,所以一直到叶子节点一共就是 n * n-1 * n-2 * ..... 1 = n!。每个叶子节点都会有一个构造全排列填进数组的操作(对应的代码:`result.push_back(path)`),该操作的复杂度为$O(n)$。所以,最终时间复杂度为:n * n!,简化为$O(n!)$。 * 空间复杂度:$O(n)$,和子集问题同理。 组合问题分析: + * 时间复杂度:$O(n × 2^n)$,组合问题其实就是一种子集的问题,所以组合问题最坏的情况,也不会超过子集问题的时间复杂度。 * 空间复杂度:$O(n)$,和子集问题同理。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 10e2d1bb50..2918246a77 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,5 +1,4 @@ - # 本周小结!(贪心算法系列二) ## 周一 @@ -16,7 +15,7 @@ 如图: -![122.买卖股票的最佳时机II](https://img-blog.csdnimg.cn/2020112917480858.png) +![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858.png) ## 周二 @@ -32,7 +31,7 @@ 如图: -![55.跳跃游戏](https://img-blog.csdnimg.cn/20201124154758229.png) +![55.跳跃游戏](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124154758229.png) ## 周三 @@ -45,7 +44,7 @@ 如图: -![45.跳跃游戏II](https://img-blog.csdnimg.cn/20201201232309103.png) +![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103-20230310133110942.png) 注意:**图中的移动下标是到当前这步覆盖的最远距离(下标2的位置),此时没有到终点,只能增加第二步来扩大覆盖范围**。 @@ -56,10 +55,10 @@ 而版本二就比较统一的,超过范围,步数就加一,但在移动下标的范围了做了文章。 即如果覆盖最远距离下标是倒数第二点:直接加一就行,默认一定可以到终点。如图: -![45.跳跃游戏II2](https://img-blog.csdnimg.cn/20201201232445286.png) +![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286-20230310133115650.png) 如果覆盖最远距离下标不是倒数第二点,说明本次覆盖已经到终点了。如图: -![45.跳跃游戏II1](https://img-blog.csdnimg.cn/20201201232338693.png) +![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693-20230310133120115.png) 有的录友认为版本一好理解,有的录友认为版本二好理解,其实掌握一种就可以了,也不用非要比拼一下代码的简洁性,简洁程度都差不多了。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" index 5e5f696d4c..203e448655 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -76,7 +76,8 @@ 文中从计算机硬件出发,分析计算机的计算性能,然后亲自做实验,整理出数据如下: -![程序超时1](https://img-blog.csdnimg.cn/20201208231559175.png) + +![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175-20230310133304038.png) **大家有一个数量级上的概念就可以了!** diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 4d12f92a88..fe4cc4a2ab 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -37,7 +37,8 @@ 先贪心一边,局部最优:只要右边评分比左边大,右边的孩子就多一个糖果,全局最优:相邻的孩子中,评分高的右孩子获得比左边孩子更多的糖果 如图: -![135.分发糖果](https://img-blog.csdnimg.cn/20201117114916878.png) + +![135.分发糖果](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117114916878-20230310133332759.png) 接着在贪心另一边,左孩子大于右孩子,左孩子的糖果就要比右孩子多。 @@ -49,7 +50,7 @@ 局部最优可以推出全局最优。 如图: -![135.分发糖果1](https://img-blog.csdnimg.cn/20201117115658791.png) +![135.分发糖果1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117115658791-20230310133346127.png) ## 周三 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 1bc3924751..7436295af3 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,5 +1,4 @@ - # 本周小结!(贪心算法系列四) ## 周一 @@ -10,7 +9,7 @@ 如图: -![452.用最少数量的箭引爆气球](https://img-blog.csdnimg.cn/20201123101929791.png) +![452.用最少数量的箭引爆气球](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123101929791-20230310133845522.png) 模拟射气球的过程,很多同学真的要去模拟了,实时把气球从数组中移走,这么写的话就复杂了,从前向后遍历重复的只要跳过就可以的。 @@ -22,7 +21,7 @@ 如图: -![435.无重叠区间](https://img-blog.csdnimg.cn/20201221201553618.png) +![435.无重叠区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201221201553618.png) 细心的同学就发现了,此题和 [贪心算法:用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像。 @@ -31,6 +30,7 @@ 把[贪心算法:用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)代码稍做修改,就可以AC本题。 修改后的C++代码如下: + ```CPP class Solution { public: @@ -71,7 +71,7 @@ public: 如图: -![763.划分字母区间](https://img-blog.csdnimg.cn/20201222191924417.png) +![763.划分字母区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201222191924417-20230310133855435.png) ## 周四 @@ -86,7 +86,7 @@ public: 如图: -![56.合并区间](https://img-blog.csdnimg.cn/20201223200632791.png) +![56.合并区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223200632791-20230310133859587.png) ## 总结 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 039f3596df..fb4978642a 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -30,7 +30,7 @@ for (int i = 1; i < m; i++) { } ``` -![62.不同路径1](https://img-blog.csdnimg.cn/20201209113631392.png) +![62.不同路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113631392-20230310133703294.png) ## 周二 @@ -44,7 +44,7 @@ dp[i][j]定义依然是:表示从(0 ,0)出发,到(i, j) 有dp[i][j]条 如图: -![63.不同路径II](https://img-blog.csdnimg.cn/20210104114513928.png) +![63.不同路径II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114513928-20230310133707783.png) 这里难住了不少同学,代码如下: @@ -69,11 +69,11 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://img-blog.csdnimg.cn/20210104114548983.png) +![63.不同路径II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114548983-20230310133711888.png) 对应的dp table 如图: -![63.不同路径II2](https://img-blog.csdnimg.cn/20210104114610256.png) +![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256-20230310133715981.png) ## 周三 @@ -107,9 +107,10 @@ for (int i = 3; i <= n ; i++) { } } ``` + 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://img-blog.csdnimg.cn/20210104173021581.png) +![343.整数拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104173021581-20230310133720552.png) @@ -141,7 +142,7 @@ dp数组如何初始化:只需要初始化dp[0]就可以了,推导的基础 n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://img-blog.csdnimg.cn/20210107093253987.png) +![96.不同的二叉搜索树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093253987-20230310133724531.png) ## 总结 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index e9427142d6..7bae5ca9ee 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,4 +1,6 @@ + # 本周小结!(动态规划系列三) + 本周我们正式开始讲解背包问题,也是动规里非常重要的一类问题。 背包问题其实有很多细节,如果了解个大概,然后也能一气呵成把代码写出来,但稍稍变变花样可能会陷入迷茫了。 @@ -15,7 +17,7 @@ 关于其他几种常用的背包,大家看这张图就了然于胸了: -![416.分割等和子集1](https://img-blog.csdnimg.cn/20210117171307407.png) +![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407-20230310133624872.png) 本文用动规五部曲详细讲解了01背包的二维dp数组的实现方法,大家其实可以发现最简单的是推导公式了,推导公式估计看一遍就记下来了,但难就难在确定初始化和遍历顺序上。 @@ -61,14 +63,14 @@ for(int i = 1; i < weight.size(); i++) { // 遍历物品 物品为: | | 重量 | 价值 | -| --- | --- | --- | +| ----- | ---- | ---- | | 物品0 | 1 | 15 | | 物品1 | 3 | 20 | | 物品2 | 4 | 30 | 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://img-blog.csdnimg.cn/20210118163425129.jpg) +![动态规划-背包问题4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210118163425129-20230310133630224.jpg) 最终结果就是dp[2][4]。 @@ -120,7 +122,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://img-blog.csdnimg.cn/20210110103614769.png) +![动态规划-背包问题9](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103614769-20230310133634873.png) ## 周三 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 83bddaff3b..e785af1228 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -35,7 +35,7 @@ bagSize = (S + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![494.目标和](https://img-blog.csdnimg.cn/20210125120743274.jpg) +![494.目标和](https://code-thinking-1253855093.file.myqcloud.com/pics/20210125120743274-20230310132918821.jpg) ## 周二 @@ -72,7 +72,8 @@ dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1); 最后dp数组的状态如下所示: -![474.一和零](https://img-blog.csdnimg.cn/20210120111201512.jpg) + +![474.一和零](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120111201512-20230310132936011.jpg) ## 周三 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 21cc53ad09..8fd292ed9a 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -29,7 +29,7 @@ dp[1] = max(nums[0], nums[1]); 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://img-blog.csdnimg.cn/20210221170954115.jpg) +![198.打家劫舍](https://code-thinking-1253855093.file.myqcloud.com/pics/20210221170954115-20230310133425353.jpg) 红框dp[nums.size() - 1]为结果。 @@ -41,15 +41,15 @@ dp[1] = max(nums[0], nums[1]); * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://img-blog.csdnimg.cn/20210129160748643.jpg) +![213.打家劫舍II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160748643.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://img-blog.csdnimg.cn/20210129160821374.jpg) +![213.打家劫舍II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160821374.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://img-blog.csdnimg.cn/20210129160842491.jpg) +![213.打家劫舍II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160842491.jpg) 需要注意的是,**“考虑” 不等于 “偷”**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素!对于情况三,取nums[1] 和 nums[3]就是最大的。 @@ -135,9 +135,11 @@ dp数组含义:下标为0记录不偷该节点所得到的的最大金钱, 2. 确定终止条件 在遍历的过程中,如果遇到空间点的话,很明显,无论偷还是不偷都是0,所以就返回 + ``` if (cur == NULL) return vector{0, 0}; ``` + 3. 确定遍历顺序 采用后序遍历,代码如下: @@ -175,7 +177,7 @@ return {val2, val1}; 以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**) -![337.打家劫舍III](https://img-blog.csdnimg.cn/20210129181331613.jpg) +![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129181331613.jpg) **最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。 @@ -196,6 +198,7 @@ return {val2, val1}; 这里我给出了三种解法: 暴力解法代码: + ```CPP class Solution { public: diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index c912b92d4a..b814e1b39e 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -76,7 +76,7 @@ dp[0][4] = 0; 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://img-blog.csdnimg.cn/20201228181724295.png) +![123.买卖股票的最佳时机III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201228181724295.png) 可以看到红色框为最后两次卖出的状态。 @@ -142,7 +142,8 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://img-blog.csdnimg.cn/20201229100358221.png) + +![188.买卖股票的最佳时机IV](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229100358221-20230310133805763.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 @@ -194,7 +195,8 @@ vector> dp(n, vector(3, 0)); 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://img-blog.csdnimg.cn/20201229163725348.png) + +![309.最佳买卖股票时机含冷冻期](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229163725348.png) 最后两个状态 不持有股票(能购买) 和 不持有股票(冷冻期)都有可能最后结果,取最大的。 diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 41647d48e1..123fb53124 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,3 +1,4 @@ +

@@ -6,6 +7,7 @@ + ## 哈希表 首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 @@ -16,7 +18,7 @@ 哈希表中关键码就是数组的索引下标,然后通过下标直接访问数组中的元素,如下图所示: -![哈希表1](https://img-blog.csdnimg.cn/20210104234805168.png) +![哈希表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104234805168.png) 那么哈希表能解决什么问题呢,**一般哈希表都是用来快速判断一个元素是否出现集合里。** @@ -34,7 +36,7 @@ 哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。 -![哈希表2](https://img-blog.csdnimg.cn/2021010423484818.png) +![哈希表2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021010423484818.png) 如果hashCode得到的数值大于 哈希表的大小了,也就是大于tableSize了,怎么办呢? @@ -50,7 +52,7 @@ 如图所示,小李和小王都映射到了索引下标 1 的位置,**这一现象叫做哈希碰撞**。 -![哈希表3](https://img-blog.csdnimg.cn/2021010423494884.png) +![哈希表3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021010423494884.png) 一般哈希碰撞有两种解决方法, 拉链法和线性探测法。 @@ -58,7 +60,7 @@ 刚刚小李和小王在索引1的位置发生了冲突,发生冲突的元素都被存储在链表中。 这样我们就可以通过索引找到小李和小王了 -![哈希表4](https://img-blog.csdnimg.cn/20210104235015226.png) +![哈希表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235015226.png) (数据规模是dataSize, 哈希表的大小为tableSize) @@ -70,7 +72,7 @@ 例如冲突的位置,放了小李,那么就向下找一个空位放置小王的信息。所以要求tableSize一定要大于dataSize ,要不然哈希表上就没有空置的位置来存放 冲突的数据了。如图所示: -![哈希表5](https://img-blog.csdnimg.cn/20210104235109950.png) +![哈希表5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235109950.png) 其实关于哈希碰撞还有非常多的细节,感兴趣的同学可以再好好研究一下,这里我就不再赘述了。 @@ -86,19 +88,19 @@ 在C++中,set 和 map 分别提供以下三种数据结构,其底层实现以及优劣如下表所示: -|集合 |底层实现 | 是否有序 |数值是否可以重复 | 能否更改数值|查询效率 |增删效率| -|---|---| --- |---| --- | --- | ---| -|std::set |红黑树 |有序 |否 |否 | O(log n)|O(log n) | -|std::multiset | 红黑树|有序 |是 | 否| O(logn) |O(logn) | -|std::unordered_set |哈希表 |无序 |否 |否 |O(1) | O(1)| +| 集合 | 底层实现 | 是否有序 | 数值是否可以重复 | 能否更改数值 | 查询效率 | 增删效率 | +| ------------------ | -------- | -------- | ---------------- | ------------ | -------- | -------- | +| std::set | 红黑树 | 有序 | 否 | 否 | O(log n) | O(log n) | +| std::multiset | 红黑树 | 有序 | 是 | 否 | O(logn) | O(logn) | +| std::unordered_set | 哈希表 | 无序 | 否 | 否 | O(1) | O(1) | std::unordered_set底层实现为哈希表,std::set 和std::multiset 的底层实现是红黑树,红黑树是一种平衡二叉搜索树,所以key值是有序的,但key不可以修改,改动key值会导致整棵树的错乱,所以只能删除和增加。 -|映射 |底层实现 | 是否有序 |数值是否可以重复 | 能否更改数值|查询效率 |增删效率| -|---|---| --- |---| --- | --- | ---| -|std::map |红黑树 |key有序 |key不可重复 |key不可修改 | O(logn)|O(logn) | -|std::multimap | 红黑树|key有序 | key可重复 | key不可修改|O(log n) |O(log n) | -|std::unordered_map |哈希表 | key无序 |key不可重复 |key不可修改 |O(1) | O(1)| +| 映射 | 底层实现 | 是否有序 | 数值是否可以重复 | 能否更改数值 | 查询效率 | 增删效率 | +| ------------------ | -------- | -------- | ---------------- | ------------ | -------- | -------- | +| std::map | 红黑树 | key有序 | key不可重复 | key不可修改 | O(logn) | O(logn) | +| std::multimap | 红黑树 | key有序 | key可重复 | key不可修改 | O(log n) | O(log n) | +| std::unordered_map | 哈希表 | key无序 | key不可重复 | key不可修改 | O(1) | O(1) | std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底层实现是红黑树。同理,std::map 和std::multimap 的key也是有序的(这个问题也经常作为面试题,考察对语言容器底层的理解)。 @@ -114,7 +116,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 实际上功能都是一样一样的, 但是unordered_set在C++11的时候被引入标准库了,而hash_set并没有,所以建议还是使用unordered_set比较好,这就好比一个是官方认证的,hash_set,hash_map 是C++11标准之前民间高手自发造的轮子。 -![哈希表6](https://img-blog.csdnimg.cn/20210104235134572.png) +![哈希表6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235134572.png) ## 总结 diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index 58fb42f841..21d78bc211 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -1,12 +1,14 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ > 20张树形结构图、14道精选回溯题目,21篇回溯法精讲文章,由浅入深,一气呵成,这是全网最强回溯算法总结! -# 回溯法理论基础 +# 回溯法理论基础 转眼间[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)里已经分享连续讲解了21天的回溯算法,是时候做一个大总结了,本篇高能,需要花费很大的精力来看! @@ -49,9 +51,9 @@ void backtracking(参数) { **事实证明这个模板会伴随整个回溯法系列!** -# 组合问题 +# 组合问题 -## 组合问题 +## 组合问题 在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)中,我们开始用回溯法解决第一道题目:组合问题。 @@ -61,17 +63,17 @@ void backtracking(参数) { 本题我把回溯问题抽象为树形结构,如题: -![77.组合1](https://img-blog.csdnimg.cn/20201118152928844.png) +![77.组合1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118152928844.png) 可以直观的看出其搜索的过程:**for循环横向遍历,递归纵向遍历,回溯不断调整结果集**,这个理念贯穿整个回溯法系列,也是我做了很多回溯的题目,不断摸索其规律才总结出来的。 -对于回溯法的整体框架,网上搜的文章这块都说不清楚,按照天上掉下来的代码对着讲解,不知道究竟是怎么来的,也不知道为什么要这么写。 +对于回溯法的整体框架,网上搜的文章这块都说不清楚,按照天上掉下来的代码对着讲解,不知道究竟是怎么来的,也不知道为什么要这么写。 **所以,录友们刚开始学回溯法,起跑姿势就很标准了!** 优化回溯算法只有剪枝一种方法,在[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中把回溯法代码做了剪枝优化,树形结构如图: -![77.组合4](https://img-blog.csdnimg.cn/20201118153133458.png) +![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118153133458.png) 大家可以一目了然剪的究竟是哪里。 @@ -87,11 +89,11 @@ void backtracking(参数) { 在[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中,相当于 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)加了一个元素总和的限制。 树形结构如图: -![216.组合总和III](https://img-blog.csdnimg.cn/20201118201921245.png) +![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118201921245.png) 整体思路还是一样的,本题的剪枝会好想一些,即:**已选元素总和如果已经大于n(题中要求的和)了,那么往后遍历就没有意义了,直接剪掉**,如图: -![216.组合总和III1](https://img-blog.csdnimg.cn/20201118202038240.png) +![216.组合总和III1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202038240.png) 在本题中,依然还可以有一个剪枝,就是[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中提到的,对for循环选择的起始范围的剪枝。 @@ -113,7 +115,7 @@ void backtracking(参数) { 树形结构如下: -![39.组合总和](https://img-blog.csdnimg.cn/20201118152521990.png) +![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118152521990.png) 最后还给出了本题的剪枝优化,如下: @@ -123,7 +125,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 优化后树形结构如下: -![39.组合总和1](https://img-blog.csdnimg.cn/20201118202115929.png) +![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202115929.png) ### 组合总和(三) @@ -138,7 +140,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://img-blog.csdnimg.cn/2020111820220675.png) +![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111820220675.png) 我在图中将used的变化用橘黄色标注上,**可以看出在candidates[i] == candidates[i - 1]相同的情况下:** @@ -159,7 +161,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![17. 电话号码的字母组合](https://img-blog.csdnimg.cn/20201118202335724.png) +![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202335724.png) 如果大家在现场面试的时候,一定要注意各种输入异常的情况,例如本题输入1 * #按键。 @@ -187,10 +189,10 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![131.分割回文串](https://img-blog.csdnimg.cn/20201118202448642.png) +![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202448642.png) -# 子集问题 +# 子集问题 ## 子集问题(一) @@ -198,7 +200,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 如图: -![78.子集](https://img-blog.csdnimg.cn/20201118202544339.png) +![78.子集](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202544339.png) 认清这个本质之后,今天的题目就是一道模板题了。 @@ -225,23 +227,23 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![90.子集II](https://img-blog.csdnimg.cn/2020111217110449.png) +![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449.png) ## 递增子序列 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://img-blog.csdnimg.cn/20201112170832333.png) +![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170832333.png) 很多同学都会把这道题目和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混在一起。 -**[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)也可以使用set针对同一父节点本层去重,但子集问题一定要排序,为什么呢?** +**[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)也可以使用set针对同一父节点本层去重,但子集问题一定要排序,为什么呢?** 我用没有排序的集合{2,1,2,2}来举个例子画一个图,如下: -![90.子集II2](https://img-blog.csdnimg.cn/2020111316440479.png) +![90.子集II2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111316440479.png) **相信这个图胜过千言万语的解释了**。 @@ -257,7 +259,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 如图: -![46.全排列](https://img-blog.csdnimg.cn/20201112170304979.png) +![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170304979.png) **大家此时可以感受出排列问题的不同:** @@ -270,7 +272,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![47.全排列II1](https://img-blog.csdnimg.cn/20201112171930470.png) +![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112171930470.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -278,21 +280,21 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://img-blog.csdnimg.cn/20201112172230434.png) +![47.全排列II2.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172230434.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://img-blog.csdnimg.cn/20201112172327967.png) +![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172327967.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** 本题used数组即是记录path里都放了哪些元素,同时也用来去重,一举两得。 -# 去重问题 +# 去重问题 以上我都是统一使用used数组来去重的,其实使用set也可以用来去重! -在[本周小结!(回溯算法系列三)续集](https://programmercarl.com/回溯算法去重问题的另一种写法.html)中给出了子集、组合、排列问题使用set来去重的解法以及具体代码,并纠正一些同学的常见错误写法。 +在[本周小结!(回溯算法系列三)续集](https://programmercarl.com/回溯算法去重问题的另一种写法.html)中给出了子集、组合、排列问题使用set来去重的解法以及具体代码,并纠正一些同学的常见错误写法。 同时详细分析了 使用used数组去重 和 使用set去重 两种写法的性能差异: @@ -304,7 +306,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 **使用set去重,不仅时间复杂度高了,空间复杂度也高了**,在[本周小结!(回溯算法系列三)](https://programmercarl.com/周总结/20201112回溯周末总结.html)中分析过,组合,子集,排列问题的空间复杂度都是O(n),但如果使用set去重,空间复杂度就变成了O(n^2),因为每一层递归都有一个set集合,系统栈空间是n,每一个空间都有set集合。 -那有同学可能疑惑 用used数组也是占用O(n)的空间啊? +那有同学可能疑惑 用used数组也是占用O(n)的空间啊? used数组可是全局变量,每层与每层之间公用一个used数组,所以空间复杂度是O(n + n),最终空间复杂度还是O(n)。 @@ -316,7 +318,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![](https://img-blog.csdnimg.cn/2020111518065555.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555.png) 本题可以算是一道hard的题目了,关于本题的难点我在文中已经详细列出。 @@ -325,19 +327,19 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 本题其实是一道深度优先搜索的题目,但是我完全使用回溯法的思路来讲解这道题题目,**算是给大家拓展一下思维方式,其实深搜和回溯也是分不开的,毕竟最终都是用递归**。 -# 棋盘问题 +# 棋盘问题 -## N皇后问题 +## N皇后问题 在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。 下面我用一个3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://img-blog.csdnimg.cn/20201118225433127.png) +![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118225433127.png) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 -那么我们用皇后们的约束条件,来回溯搜索这棵树,**只要搜索到了树的叶子节点,说明就找到了皇后们的合理位置了**。 +那么我们用皇后们的约束条件,来回溯搜索这棵树,**只要搜索到了树的叶子节点,说明就找到了皇后们的合理位置了**。 如果从来没有接触过N皇后问题的同学看着这样的题会感觉无从下手,可能知道要用回溯法,但也不知道该怎么去搜。 @@ -361,7 +363,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://img-blog.csdnimg.cn/2020111720451790.png) +![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790.png) 解数独可以说是非常难的题目了,如果还一直停留在一维递归的逻辑中,这道题目可以让大家瞬间崩溃。 @@ -380,22 +382,27 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 以下在计算空间复杂度的时候我都把系统栈(不是数据结构里的栈)所占空间算进去。 子集问题分析: + * 时间复杂度:O(2^n),因为每一个元素的状态无外乎取与不取,所以时间复杂度为O(2^n) * 空间复杂度:O(n),递归深度为n,所以系统栈所用空间为O(n),每一层递归所用的空间都是常数级别,注意代码里的result和path都是全局变量,就算是放在参数里,传的也是引用,并不会新申请内存空间,最终空间复杂度为O(n) 排列问题分析: + * 时间复杂度:O(n!),这个可以从排列的树形图中很明显发现,每一层节点为n,第二层每一个分支都延伸了n-1个分支,再往下又是n-2个分支,所以一直到叶子节点一共就是 n * n-1 * n-2 * ..... 1 = n!。 * 空间复杂度:O(n),和子集问题同理。 组合问题分析: + * 时间复杂度:O(2^n),组合问题其实就是一种子集的问题,所以组合问题最坏的情况,也不会超过子集问题的时间复杂度。 * 空间复杂度:O(n),和子集问题同理。 -N皇后问题分析: +N皇后问题分析: + * 时间复杂度:O(n!) ,其实如果看树形图的话,直觉上是O(n^n),但皇后之间不能见面所以在搜索的过程中是有剪枝的,最差也就是O(n!),n!表示n * (n-1) * .... * 1。 * 空间复杂度:O(n),和子集问题同理。 解数独问题分析: + * 时间复杂度:O(9^m) , m是'.'的数目。 * 空间复杂度:O(n^2),递归的深度是n^2 @@ -403,7 +410,7 @@ N皇后问题分析: **一般说道回溯算法的复杂度,都说是指数级别的时间复杂度,这也算是一个概括吧!** -# 总结 +# 总结 **[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)历时21天,14道经典题目分析,20张树形图,21篇回溯法精讲文章,从组合到切割,从子集到排列,从棋盘问题到最后的复杂度分析**,至此收尾了。 @@ -412,11 +419,12 @@ N皇后问题分析: 可以说方方面面都详细介绍到了。 例如: + * 如何理解回溯法的搜索过程? -* 什么时候用startIndex,什么时候不用? -* 如何去重?如何理解“树枝去重”与“树层去重”? +* 什么时候用startIndex,什么时候不用? +* 如何去重?如何理解“树枝去重”与“树层去重”? * 去重的几种方法? -* 如何理解二维递归? +* 如何理解二维递归? **这里的每一个问题,网上几乎找不到能讲清楚的文章,这也是直击回溯算法本质的问题**。 @@ -424,11 +432,11 @@ N皇后问题分析: 此时回溯算法系列就要正式告一段落了。 -**录友们可以回顾一下这21天,每天的打卡,每天在交流群里和大家探讨代码,最终换来的都是不知不觉的成长**。 +**录友们可以回顾一下这21天,每天的打卡,每天在交流群里和大家探讨代码,最终换来的都是不知不觉的成长**。 同样也感谢录友们的坚持,这也是我持续写作的动力,**正是因为大家的积极参与,我才知道这件事件是非常有意义的**。 -回溯专题汇聚为一张图: +回溯专题汇聚为一张图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211030124742.png) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 7386215681..69a28ad951 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -16,7 +16,8 @@ 我用没有排序的集合{2,1,2,2}来举例子画一个图,如图: -![90.子集II2](https://img-blog.csdnimg.cn/2020111316440479.png) + +![90.子集II2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111316440479-20230310121930316.png) 图中,大家就很明显的看到,子集重复了。 @@ -96,7 +97,7 @@ private: 如图: -![90.子集II1](https://img-blog.csdnimg.cn/202011131625054.png) +![90.子集II1](https://code-thinking-1253855093.file.myqcloud.com/pics/202011131625054.png) 可以看出一旦把unordered_set uset放在类成员位置,它控制的就是整棵树,包括树枝。 diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index aba21fa0b7..f11dbaef52 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -8,7 +8,7 @@ ## 题目分类大纲如下: -回溯算法大纲 +回溯算法大纲 可以配合我的B站视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) 一起学习! @@ -110,7 +110,7 @@ if (终止条件) { 如图: -![回溯算法理论基础](https://img-blog.csdnimg.cn/20210130173631174.png) +![回溯算法理论基础](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130173631174.png) 注意图中,我特意举例集合大小和孩子的数量是相等的! diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index c49e9ec89e..67b7b20d1b 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -82,7 +82,8 @@ int main() { 如图: -![数组内存](https://img-blog.csdnimg.cn/20210310150641186.png) + +![数组内存](https://code-thinking-1253855093.file.myqcloud.com/pics/20210310150641186.png) **所以可以看出在C++中二维数组在地址空间上是连续的**。 @@ -112,7 +113,8 @@ public static void test_arr() { 所以Java的二维数组可能是如下排列的方式: -![算法通关数组3](https://img-blog.csdnimg.cn/20201214111631844.png) + +![算法通关数组3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201214111631844.png) 这里面试中数组相关的理论知识就介绍完了。 diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index ee4506f4ca..0075deb6c2 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -10,7 +10,7 @@ 如图所示: -![栈与队列理论1](https://img-blog.csdnimg.cn/20210104235346563.png) +![栈与队列理论1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235346563.png) 那么我这里再列出四个关于栈的问题,大家可以思考一下。以下是以C++为例,使用其他编程语言的同学也对应思考一下,自己使用的编程语言里栈和队列是什么样的。 @@ -44,7 +44,8 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 来说一说栈,栈先进后出,如图所示: -![栈与队列理论2](https://img-blog.csdnimg.cn/20210104235434905.png) + +![栈与队列理论2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235434905.png) 栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则,所以栈不提供走访功能,也不提供迭代器(iterator)。 不像是set 或者map 提供迭代器iterator来遍历所有元素。 @@ -56,8 +57,8 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 从下图中可以看出,栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。 -![栈与队列理论3](https://img-blog.csdnimg.cn/20210104235459376.png) +![栈与队列理论3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235459376.png) **我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。** diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 315b6da12a..94d94e3849 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 贪心算法:根据身高重建队列(续集) 在讲解[贪心算法:根据身高重建队列](https://programmercarl.com/0406.根据身高重建队列.html)中,我们提到了使用vector(C++中的动态数组)来进行insert操作是费时的。 @@ -12,6 +14,7 @@ 这里专门写一篇文章来详细说一说这个问题。 使用vector的代码如下: + ```CPP // 版本一,使用vector(动态数组) class Solution { @@ -32,12 +35,14 @@ public: }; ``` + 耗时如下: -![vectorinsert](https://img-blog.csdnimg.cn/20201218203611181.png) +![vectorinsert](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218203611181.png) 其直观上来看数组的insert操作是O(n)的,整体代码的时间复杂度是O(n^2)。 这么一分析好像和版本二链表实现的时间复杂度是一样的啊,为什么提交之后效率会差距这么大呢? + ```CPP // 版本二,使用list(链表) class Solution { @@ -65,7 +70,7 @@ public: 耗时如下: -![使用链表](https://img-blog.csdnimg.cn/20201218200756257.png) +![使用链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218200756257.png) 大家都知道对于普通数组,一旦定义了大小就不能改变,例如int a[10];,这个数组a至多只能放10个元素,改不了的。 @@ -76,6 +81,7 @@ public: **首先vector的底层实现也是普通数组**。 vector的大小有两个维度一个是size一个是capicity,size就是我们平时用来遍历vector时候用的,例如: + ``` for (int i = 0; i < vec.size(); i++) { @@ -91,7 +97,7 @@ for (int i = 0; i < vec.size(); i++) { 就是重新申请一个二倍于原数组大小的数组,然后把数据都拷贝过去,并释放原数组内存。(对,就是这么原始粗暴的方法!) 举一个例子,如图: -![vector原理](https://img-blog.csdnimg.cn/20201218185902217.png) +![vector原理](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218185902217.png) 原vector中的size和capicity相同都是3,初始化为1 2 3,此时要push_back一个元素4。 @@ -131,9 +137,10 @@ public: } }; ``` + 耗时如下: -![vector手动模拟insert](https://img-blog.csdnimg.cn/20201218200626718.png) +![vector手动模拟insert](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218200626718.png) 这份代码就是不让vector动态扩容,全程我们自己模拟insert的操作,大家也可以直观的看出是一个O(n^2)的方法了。 diff --git "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" index 9f61b2afa7..c4e8cd9cff 100644 --- "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" @@ -13,7 +13,7 @@ 关于这几种常见的背包,其关系如下: -![416.分割等和子集1](https://img-blog.csdnimg.cn/20210117171307407.png) +![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000726.png) 通过这个图,可以很清晰分清这几种常见背包之间的关系。 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 0b2d7ae685..ff0b6aba28 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -1,10 +1,12 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 动态规划:01背包理论基础 + +# 动态规划:01背包理论基础 **《代码随想录》算法视频公开课:[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -20,7 +22,7 @@ 如果这几种背包,分不清,我这里画了一个图,如下: -![416.分割等和子集1](https://img-blog.csdnimg.cn/20210117171307407.png) +![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407.png) 至于背包九讲其其他背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 @@ -39,7 +41,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 -![动态规划-背包问题](https://img-blog.csdnimg.cn/20210117175428387.jpg) +![动态规划-背包问题](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117175428387.jpg) 这是标准的背包问题,以至于很多同学看了这个自然就会想到背包,甚至都不知道暴力的解法应该怎么解了。 @@ -56,7 +58,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 物品为: | | 重量 | 价值 | -| --- | --- | --- | +| ----- | ---- | ---- | | 物品0 | 1 | 15 | | 物品1 | 3 | 20 | | 物品2 | 4 | 30 | @@ -75,7 +77,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 只看这个二维数组的定义,大家一定会有点懵,看下面这个图: -![动态规划-背包问题1](https://img-blog.csdnimg.cn/20210110103003361.png) +![动态规划-背包问题1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103003361.png) **要时刻记着这个dp数组的含义,下面的一些步骤都围绕这dp数组的含义进行的**,如果哪里看懵了,就来回顾一下i代表什么,j又代表什么。 @@ -96,20 +98,21 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: -![动态规划-背包问题2](https://img-blog.csdnimg.cn/2021011010304192.png) +![动态规划-背包问题2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010304192.png) 在看其他情况。 状态转移方程 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); 可以看出i 是由 i-1 推导出来,那么i为0的时候就一定要初始化。 -dp[0][j],即:i为0,存放编号0的物品的时候,各个容量的背包所能存放的最大价值。 +dp[0][j],即:i为0,存放编号0的物品的时候,各个容量的背包所能存放的最大价值。 那么很明显当 j < weight[0]的时候,dp[0][j] 应该是 0,因为背包容量比编号0的物品重量还小。 当j >= weight[0]时,dp[0][j] 应该是value[0],因为背包容量放足够放编号0物品。 代码初始化如下: -``` + +``` for (int j = 0 ; j < weight[0]; j++) { // 当然这一步,如果把dp数组预先初始化为0了,这一步就可以省略,但很多同学应该没有想清楚这一点。 dp[0][j] = 0; } @@ -122,7 +125,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 此时dp数组初始化情况如图所示: -![动态规划-背包问题7](https://img-blog.csdnimg.cn/20210110103109140.png) +![动态规划-背包问题7](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103109140.png) dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? @@ -134,7 +137,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 如图: -![动态规划-背包问题10](https://code-thinking.cdn.bcebos.com/pics/动态规划-背包问题10.jpg) +![动态规划-背包问题10](https://code-thinking-1253855093.file.myqcloud.com/pics/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%9810.jpg) 最后初始化代码如下: @@ -154,7 +157,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 在如下图中,可以看出,有两个遍历的维度:物品与背包重量 -![动态规划-背包问题3](https://img-blog.csdnimg.cn/2021011010314055.png) +![动态规划-背包问题3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010314055.png) 那么问题来了,**先遍历 物品还是先遍历背包重量呢?** @@ -166,7 +169,7 @@ for (int j = weight[0]; j <= bagweight; j++) { // weight数组的大小 就是物品个数 for(int i = 1; i < weight.size(); i++) { // 遍历物品 for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 - if (j < weight[i]) dp[i][j] = dp[i - 1][j]; + if (j < weight[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); } @@ -195,11 +198,11 @@ dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); 递归公式 dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括正上方向),那么先遍历物品,再遍历背包的过程如图所示: -![动态规划-背包问题5](https://img-blog.csdnimg.cn/202101101032124.png) +![动态规划-背包问题5](https://code-thinking-1253855093.file.myqcloud.com/pics/202101101032124.png) 再来看看先遍历背包,再遍历物品呢,如图: -![动态规划-背包问题6](https://img-blog.csdnimg.cn/20210110103244701.png) +![动态规划-背包问题6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103244701.png) **大家可以看出,虽然两个for循环遍历的次序不同,但是dp[i][j]所需要的数据就是左上角,根本不影响dp[i][j]公式的推导!** @@ -211,7 +214,7 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://img-blog.csdnimg.cn/20210118163425129.jpg) +![动态规划-背包问题4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210118163425129.jpg) 最终结果就是dp[2][4]。 @@ -272,7 +275,7 @@ int main() { ## 其他语言版本 -### java +### java ```java public class BagProblem { @@ -336,33 +339,34 @@ public class BagProblem { ``` ### python + ```python -def test_2_wei_bag_problem1(bag_size, weight, value) -> int: +def test_2_wei_bag_problem1(bag_size, weight, value) -> int: rows, cols = len(weight), bag_size + 1 dp = [[0 for _ in range(cols)] for _ in range(rows)] - - # 初始化dp数组. - for i in range(rows): + + # 初始化dp数组. + for i in range(rows): dp[i][0] = 0 first_item_weight, first_item_value = weight[0], value[0] - for j in range(1, cols): - if first_item_weight <= j: + for j in range(1, cols): + if first_item_weight <= j: dp[0][j] = first_item_value - # 更新dp数组: 先遍历物品, 再遍历背包. - for i in range(1, len(weight)): + # 更新dp数组: 先遍历物品, 再遍历背包. + for i in range(1, len(weight)): cur_weight, cur_val = weight[i], value[i] - for j in range(1, cols): - if cur_weight > j: # 说明背包装不下当前物品. - dp[i][j] = dp[i - 1][j] # 所以不装当前物品. - else: + for j in range(1, cols): + if cur_weight > j: # 说明背包装不下当前物品. + dp[i][j] = dp[i - 1][j] # 所以不装当前物品. + else: # 定义dp数组: dp[i][j] 前i个物品里,放进容量为j的背包,价值总和最大是多少。 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight]+ cur_val) print(dp) -if __name__ == "__main__": +if __name__ == "__main__": bag_size = 4 weight = [1, 3, 4] value = [15, 20, 30] @@ -370,7 +374,8 @@ if __name__ == "__main__": ``` -### go +### go + ```go func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { // 定义dp数组 @@ -391,7 +396,7 @@ func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { } else { dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) } - } + } } return dp[len(weight)-1][bagweight] } @@ -410,7 +415,7 @@ func main() { } ``` -### javascript +### javascript ```js function testWeightBagProblem (weight, value, size) { @@ -445,6 +450,7 @@ test(); ### C + ```c #include #include @@ -458,7 +464,7 @@ void backPack(int* weights, int weightSize, int* costs, int costSize, int bagWei // 开辟dp数组 int dp[weightSize][bagWeight + 1]; memset(dp, 0, sizeof(int) * weightSize * (bagWeight + 1)); - + int i, j; // 当背包容量大于物品0的重量时,将物品0放入到背包中 for(j = weights[0]; j <= bagWeight; ++j) { diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index c9414fd6cf..baa4107f58 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -150,7 +150,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://img-blog.csdnimg.cn/20210110103614769.png) +![动态规划-背包问题9](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103614769.png) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 2a2f61ec38..d0b2ce0b8c 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -68,7 +68,8 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 dp状态图如下: -![动态规划-完全背包](https://img-blog.csdnimg.cn/20210126104510106.jpg) + +![动态规划-完全背包](https://code-thinking-1253855093.file.myqcloud.com/pics/20210126104510106.jpg) 相信很多同学看网上的文章,关于完全背包介绍基本就到为止了。 @@ -91,7 +92,8 @@ dp状态图如下: 遍历物品在外层循环,遍历背包容量在内层循环,状态如图: -![动态规划-完全背包1](https://img-blog.csdnimg.cn/20210126104529605.jpg) + +![动态规划-完全背包1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210126104529605.jpg) 遍历背包容量在外层循环,遍历物品在内层循环,状态如图: diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 44afc39626..a09974df7c 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,3 +1,4 @@ +

@@ -5,6 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 关于链表,你该了解这些! 什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。 @@ -12,7 +14,7 @@ 链表的入口节点称为链表的头结点也就是head。 如图所示: -![链表1](https://img-blog.csdnimg.cn/20200806194529815.png) +![链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194529815.png) # 链表的类型 @@ -31,7 +33,7 @@ 双链表 既可以向前查询也可以向后查询。 如图所示: -![链表2](https://img-blog.csdnimg.cn/20200806194559317.png) +![链表2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194559317.png) ## 循环链表 @@ -39,7 +41,7 @@ 循环链表可以用来解决约瑟夫环问题。 -![链表4](https://img-blog.csdnimg.cn/20200806194629603.png) +![链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194629603.png) # 链表的存储方式 @@ -54,7 +56,7 @@ 如图所示: -![链表3](https://img-blog.csdnimg.cn/20200806194613920.png) +![链表3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194613920.png) 这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。 @@ -104,7 +106,7 @@ head->val = 5; 删除D节点,如图所示: -![链表-删除节点](https://img-blog.csdnimg.cn/20200806195114541.png) +![链表-删除节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195114541-20230310121459257.png) 只要将C节点的next指针 指向E节点就可以了。 @@ -118,7 +120,7 @@ head->val = 5; 如图所示: -![链表-添加节点](https://img-blog.csdnimg.cn/20200806195134331.png) +![链表-添加节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195134331-20230310121503147.png) 可以看出链表的增添和删除都是O(1)操作,也不会影响到其他节点。 @@ -128,7 +130,7 @@ head->val = 5; 再把链表的特性和数组的特性进行一个对比,如图所示: -![链表-链表与数据性能对比](https://img-blog.csdnimg.cn/20200806195200276.png) +![链表-链表与数据性能对比](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195200276.png) 数组在定义的时候,长度就是固定的,如果想改动数组的长度,就需要重新定义一个新的数组。 @@ -143,6 +145,7 @@ head->val = 5; Java: + ```java public class ListNode { // 结点的值 @@ -195,6 +198,7 @@ class ListNode { ``` Python: + ```python class ListNode: def __init__(self, val, next=None): @@ -203,6 +207,7 @@ class ListNode: ``` Go: + ```go type ListNode struct { Val int @@ -211,6 +216,7 @@ type ListNode struct { ``` Scala: + ```scala class ListNode(_x: Int = 0, _next: ListNode = null) { var next: ListNode = _next @@ -219,6 +225,7 @@ class ListNode(_x: Int = 0, _next: ListNode = null) { ``` Rust: + ```rust #[derive(PartialEq, Eq, Clone, Debug)] pub struct ListNode { From 3dda10b12f589a78fb026ef469c788415901b7cd Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 10 Mar 2023 14:11:11 +0800 Subject: [PATCH 0031/1533] Update --- ...\347\273\264dp\346\225\260\347\273\204.md" | 145 ------------------ 1 file changed, 145 deletions(-) delete mode 100644 "problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214IV\344\272\214\347\273\264dp\346\225\260\347\273\204.md" diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214IV\344\272\214\347\273\264dp\346\225\260\347\273\204.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214IV\344\272\214\347\273\264dp\346\225\260\347\273\204.md" deleted file mode 100644 index 276329c56a..0000000000 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214IV\344\272\214\347\273\264dp\346\225\260\347\273\204.md" +++ /dev/null @@ -1,145 +0,0 @@ -# 完全背包的排列问题模拟 - -#### Problem - -1. 排列问题是完全背包中十分棘手的问题。 -2. 其在迭代过程中需要先迭代背包容量,再迭代物品个数,使得其在代码理解上较难入手。 - -#### Contribution - -本文档以力扣上[组合总和IV](https://leetcode.cn/problems/combination-sum-iv/)为例,提供一个二维dp的代码例子,并提供模拟过程以便于理解 - -#### Code - -```cpp -int combinationSum4(vector& nums, int target) { - // 定义背包容量为target,物品个数为nums.size()的dp数组 - // dp[i][j]表示将第0-i个物品添加入排列中,和为j的排列方式 - vector> dp (nums.size(), vector(target+1,0)); - - // 表示有0,1,...,n个物品可选择的情况下,和为0的选择方法为1:什么都不取 - for(int i = 0; i < nums.size(); i++) dp[i][0] = 1; - - // 必须按列遍历,因为右边数组需要知道左边数组最低部的信息(排列问题) - // 后面的模拟可以更清楚的表现这么操作的原因 - for(int i = 1; i <= target; i++){ - for(int j = 0; j < nums.size(); j++){ - // 只有nums[j]可以取的情况 - if(j == 0){ - if(nums[j] > i) dp[j][i] = 0; - // 如果背包容量放不下 那么此时没有排列方式 - else dp[j][i] = dp[nums.size()-1][i-nums[j]]; - // 如果背包容量放的下 全排列方式为dp[最底层][容量-该物品容量]排列方式后面放一个nums[j] - } - // 有多个nums数可以取 - else{ - // 如果背包容量放不下 那么沿用0-j-1个物品的排列方式 - if(nums[j] > i) dp[j][i] = dp[j-1][i]; - // 如果背包容量放得下 在dp[最底层][容量-该物品容量]排列方式后面放一个nums[j]后面放个nums[j] - // INT_MAX避免溢出 - else if(i >= nums[j] && dp[j-1][i] < INT_MAX - dp[nums.size()-1][i-nums[j]]) - dp[j][i] = dp[j-1][i] + dp[nums.size()-1][i-nums[j]]; - } - } - } - // 打印dp数组 - for(int i = 0; i < nums.size(); i++){ - for(int j = 0; j <= target; j++){ - cout< Date: Fri, 10 Mar 2023 14:52:24 +0800 Subject: [PATCH 0032/1533] =?UTF-8?q?Update=200055.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\263\350\267\203\346\270\270\346\210\217.md" | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index a898263de9..6f6dec26de 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -178,16 +178,16 @@ var canJump = function(nums) { ```Rust impl Solution { - fn max(a: usize, b: usize) -> usize { - if a > b { a } else { b } - } pub fn can_jump(nums: Vec) -> bool { - let mut cover = 0; - if (nums.len() == 1) { return true; } - let mut i = 0; + if nums.len() == 1 { + return true; + } + let (mut i, mut cover) = (0, 0); while i <= cover { - cover = Self::max(i + nums[i] as usize, cover); - if cover >= nums.len() - 1 { return true; } + cover = (i + nums[i] as usize).max(cover); + if cover >= nums.len() - 1 { + return true; + } i += 1; } false From 296ef893b4a15d4459e7400bd2bd699b3173bbbc Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Fri, 10 Mar 2023 13:12:53 -0500 Subject: [PATCH 0033/1533] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python解法:使用双指针法移除空格 --- ...14\347\232\204\345\215\225\350\257\215.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index dc12ae2336..eb78cc9d18 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -516,6 +516,48 @@ class Solution: return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions, ``` +```python +class Solution: # 使用双指针法移除空格 + def reverseWords(self, s: str) -> str: + + def removeextraspace(s): + start = 0; end = len(s)-1 + while s[start]==' ': + start+=1 + while s[end]==' ': + end-=1 + news = list(s[start:end+1]) + slow = fast = 0 + while fast0 and news[fast]==news[fast-1]==' ': + fast+=1 + news[slow]=news[fast] + slow+=1; fast+=1 + #return "".join(news[:slow]) + return news[:slow] + + def reversestr(s): + left,right = 0,len(s)-1 + news = list(s) + while left Date: Sun, 12 Mar 2023 11:25:46 +0800 Subject: [PATCH 0034/1533] =?UTF-8?q?update=200203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 6 ++++++ 1 file changed, 6 insertions(+) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 99bd35805a..7e3955a5ef 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -118,6 +118,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + **设置一个虚拟头结点在进行移除节点操作:** ```CPP @@ -144,6 +147,9 @@ public: ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + From 9321c3c6773f4cd244d86d5f8052042c30ba5be0 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 12 Mar 2023 11:34:01 +0800 Subject: [PATCH 0035/1533] =?UTF-8?q?update=200707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 95560fb138..f6f16e30c7 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -152,6 +152,9 @@ private: }; ``` +* 时间复杂度: 涉及 `index` 的相关操作为 O(index), 其余为 O(1) +* 空间复杂度: O(n) + ## 其他语言版本 From 2d7a9a16c513f8e49ed9cd51d7a54743588ec722 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 12 Mar 2023 11:38:20 +0800 Subject: [PATCH 0036/1533] =?UTF-8?q?update=200206.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....\347\277\273\350\275\254\351\223\276\350\241\250.md" | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index 8bf61c3f8f..d558e783fe 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -68,6 +68,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + ## 递归法 递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。 @@ -97,6 +100,9 @@ public: }; ``` +* 时间复杂度: O(n), 要递归处理链表的每个节点 +* 空间复杂度: O(n), 递归调用了 n 层栈空间 + 我们可以发现,上面的递归写法和双指针法实质上都是从前往后翻转指针指向,其实还有另外一种与双指针法不同思路的递归写法:从后往前翻转指针指向。 具体代码如下(带详细注释): @@ -120,6 +126,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 其他语言版本 From 358818c7a499809056fee400f460cc80b0c20ceb Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 12 Mar 2023 11:41:09 +0800 Subject: [PATCH 0037/1533] =?UTF-8?q?update=200019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index c408fefcb8..a11ff8bace 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -87,6 +87,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + ## 其他语言版本 From 3aabfed8bd708b8524cab5eac65b3358eeceb9bf Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 12 Mar 2023 11:46:51 +0800 Subject: [PATCH 0038/1533] =?UTF-8?q?update=200142.=E7=8E=AF=E5=BD=A2?= =?UTF-8?q?=E9=93=BE=E8=A1=A8II=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index 8a48c7d5f6..46df477705 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -145,6 +145,9 @@ public: }; ``` +* 时间复杂度: O(n),快慢指针相遇前,指针走的次数小于链表长度,快慢指针相遇后,两个index指针走的次数也小于链表长度,总体为走的次数小于 2n +* 空间复杂度: O(1) + ## 补充 在推理过程中,大家可能有一个疑问就是:**为什么第一次在环中相遇,slow的 步数 是 x+y 而不是 x + 若干环的长度 + y 呢?** From 0ccbc1ea535240a2118296eaf1d5414c41a865b4 Mon Sep 17 00:00:00 2001 From: jeffreyjia Date: Sun, 12 Mar 2023 13:07:31 +0800 Subject: [PATCH 0039/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md=20=E6=8F=90=E4=BE=9BGo=E7=89=88=E6=9C=AC=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E6=96=B0=E6=80=9D=E8=B7=AF(dp[i]=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E4=BB=8Ei=E5=B1=82=E8=B5=B7=E8=B7=B3=E6=89=80?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=94=AF=E4=BB=98=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E8=B4=B9=E7=94=A8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++++ .idea/leetcode-master.iml | 9 +++++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ ...71\347\210\254\346\245\274\346\242\257.md" | 27 +++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/leetcode-master.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..35410cacdc --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml new file mode 100644 index 0000000000..5e764c4f0b --- /dev/null +++ b/.idea/leetcode-master.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..7c250acde5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..94a25f7f4c --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 44b6406c8a..d6b3a17735 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -284,6 +284,33 @@ func min(a, b int) int { return b } ``` +``` GO +第二种思路: dp[i]表示从i层起跳所需要支付的最小费用 +递推公式: +i Date: Sun, 12 Mar 2023 13:10:21 +0800 Subject: [PATCH 0040/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..3d725761b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.idea \ No newline at end of file From d8df97845c165669713c5606b5e6a5397e356d5e Mon Sep 17 00:00:00 2001 From: jeffreyjia Date: Sun, 12 Mar 2023 13:24:03 +0800 Subject: [PATCH 0041/1533] =?UTF-8?q?=E5=BF=BD=E7=95=A5idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 -------- .idea/leetcode-master.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 4 files changed, 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/leetcode-master.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410cacdc..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml deleted file mode 100644 index 5e764c4f0b..0000000000 --- a/.idea/leetcode-master.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7c250acde5..0000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f4c..0000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 50cdae9a91df693b122f5bf8e89aa8fc5164a0d8 Mon Sep 17 00:00:00 2001 From: jeffreyjia Date: Sun, 12 Mar 2023 13:25:18 +0800 Subject: [PATCH 0042/1533] =?UTF-8?q?=E5=88=A0=E9=99=A4.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 -- .idea/.gitignore | 8 ++++++++ .idea/leetcode-master.iml | 9 +++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 31 insertions(+), 2 deletions(-) delete mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/leetcode-master.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 3d725761b0..0000000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.DS_Store -.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..35410cacdc --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml new file mode 100644 index 0000000000..5e764c4f0b --- /dev/null +++ b/.idea/leetcode-master.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..7c250acde5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..94a25f7f4c --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From d34e4a8378ae8c62cdb8a3a00462561c70ec0b4a Mon Sep 17 00:00:00 2001 From: jeffreyjia Date: Sun, 12 Mar 2023 13:25:53 +0800 Subject: [PATCH 0043/1533] =?UTF-8?q?=E5=BF=BD=E7=95=A5idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 -------- .idea/leetcode-master.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 4 files changed, 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/leetcode-master.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410cacdc..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml deleted file mode 100644 index 5e764c4f0b..0000000000 --- a/.idea/leetcode-master.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7c250acde5..0000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f4c..0000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From abc26c5e5ab377379179dba63f93f7e2eef50353 Mon Sep 17 00:00:00 2001 From: StriveDD Date: Mon, 13 Mar 2023 21:26:29 +0800 Subject: [PATCH 0044/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A01020.=E9=A3=9E?= =?UTF-8?q?=E6=8A=B5=E7=9A=84=E6=95=B0=E9=87=8FJava=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\232\204\346\225\260\351\207\217.md" | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 3da373765a..ad848e7379 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -144,6 +144,130 @@ public: } }; ``` +## 其他语言版本 + +**Java**: + +深度优先遍历版本: + +```java +class Solution { + // 四个方向 + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + + // 深度优先遍历,把可以通向边缘部分的 1 全部标记成 true + public void dfs(int[][] grid, int row, int col, boolean[][] visited) { + for (int[] current: position) { + int newRow = row + current[0], newCol = col + current[1]; + // 下标越界直接跳过 + if (newRow < 0 || newRow >= grid.length || newCol < 0 || newCol >= grid[0].length) continue; + // 当前位置不是 1 或者已经被访问了就直接跳过 + if (grid[newRow][newCol] != 1 || visited[newRow][newCol]) continue; + visited[newRow][newCol] = true; + dfs(grid, newRow, newCol, visited); + } + } + + public int numEnclaves(int[][] grid) { + int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案 + boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组 + // 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历 + for (int row = 0; row < rowSize; row++) { + if (grid[row][0] == 1 && !visited[row][0]) { + visited[row][0] = true; + dfs(grid, row, 0, visited); + } + if (grid[row][colSize - 1] == 1 && !visited[row][colSize - 1]) { + visited[row][colSize - 1] = true; + dfs(grid, row, colSize - 1, visited); + } + } + // 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了 + for (int col = 1; col < colSize - 1; col++) { + if (grid[0][col] == 1 && !visited[0][col]) { + visited[0][col] = true; + dfs(grid, 0, col, visited); + } + if (grid[rowSize - 1][col] == 1 && !visited[rowSize - 1][col]) { + visited[rowSize - 1][col] = true; + dfs(grid, rowSize - 1, col, visited); + } + } + // 查找没有标记过的 1,记录到 ans 中 + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (grid[row][col] == 1 && !visited[row][col]) ++ans; + } + } + return ans; + } +} +``` + +广度优先遍历版本: + +```java +class Solution { + // 四个方向 + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + + // 广度优先遍历,把可以通向边缘部分的 1 全部标记成 true + public void bfs(int[][] grid, Queue queue, boolean[][] visited) { + while (!queue.isEmpty()) { + int[] curPos = queue.poll(); + for (int[] current: position) { + int row = curPos[0] + current[0], col = curPos[1] + current[1]; + // 下标越界直接跳过 + if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) + continue; + // 当前位置不是 1 或者已经被访问了就直接跳过 + if (visited[row][col] || grid[row][col] == 0) continue; + visited[row][col] = true; + queue.add(new int[]{row, col}); + } + } + } + + public int numEnclaves(int[][] grid) { + int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案 + boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组 + Queue queue = new ArrayDeque<>(); + // 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历 + for (int row = 0; row < rowSize; row++) { + if (grid[row][0] == 1) { + visited[row][0] = true; + queue.add(new int[]{row, 0}); + } + if (grid[row][colSize - 1] == 1) { + visited[row][colSize - 1] = true; + queue.add(new int[]{row, colSize - 1}); + } + } + // 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了 + for (int col = 1; col < colSize - 1; col++) { + if (grid[0][col] == 1) { + visited[0][col] = true; + queue.add(new int[]{0, col}); + } + if (grid[rowSize - 1][col] == 1 && !visited[rowSize - 1][col]) { + visited[rowSize - 1][col] = true; + queue.add(new int[]{rowSize - 1, col}); + } + } + bfs(grid, queue, visited); + // 查找没有标记过的 1,记录到 ans 中 + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (grid[row][col] == 1 && !visited[row][col]) ++ans; + } + } + return ans; + } +} +``` + + + ## 类似题目 * 1254. 统计封闭岛屿的数目 @@ -153,3 +277,4 @@ public: + From 2b73437e7283230b340d72fe208b2c39c9d6a3c7 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Tue, 14 Mar 2023 11:35:23 +0800 Subject: [PATCH 0045/1533] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\203\346\270\270\346\210\217II.md" | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 22151fdc9a..911834e130 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -379,43 +379,46 @@ object Solution { ```Rust //版本一 impl Solution { - fn max(a: i32, b:i32) -> i32 { - if a > b { a } else { b } - } pub fn jump(nums: Vec) -> i32 { - if nums.len() == 0 { return 0; } - let mut cur_distance: i32 = 0; - let mut ans: i32 = 0; - let mut next_distance: i32 = 0; - for i in 0..nums.len() { - next_distance = Self::max(nums[i] + i as i32, next_distance); - if i as i32 == cur_distance { - if cur_distance != (nums.len() - 1) as i32 { + if nums.len() == 1 { + return 0; + } + let mut cur_distance = 0; + let mut ans = 0; + let mut next_distance = 0; + for (n, &i) in nums.iter().enumerate() { + next_distance = (n as i32 + i).max(next_distance); + if i == cur_distance { + if cur_distance < n as i32 - 1 { ans += 1; cur_distance = next_distance; - if next_distance == (nums.len() - 1) as i32 { break; } + if next_distance >= n as i32 - 1 { + break; + }; + } else { + break; } - else { break; } } } ans } } + ``` ```Rust //版本二 impl Solution { - fn max(a: i32, b:i32) -> i32 { - if a > b { a } else { b } - } pub fn jump(nums: Vec) -> i32 { - let mut cur_distance: i32 = 0; - let mut ans: i32 = 0; - let mut next_distance: i32 = 0; - for i in 0..nums.len() - 1 { - next_distance = Self::max(nums[i] + i as i32, next_distance); - if i as i32 == cur_distance { + if nums.len() == 1 { + return 0; + } + let mut cur_distance = 0; + let mut ans = 0; + let mut next_distance = 0; + for (n, &i) in nums.iter().enumerate() { + next_distance = (n as i32 + i).max(next_distance); + if i == cur_distance { cur_distance = next_distance; ans += 1; } @@ -423,6 +426,7 @@ impl Solution { ans } } + ``` From 05028415306e7678ef1121e7c93dd6611897d68e Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Tue, 14 Mar 2023 11:58:01 +0800 Subject: [PATCH 0046/1533] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\263\350\267\203\346\270\270\346\210\217II.md" | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 911834e130..9b13d31d35 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -386,13 +386,13 @@ impl Solution { let mut cur_distance = 0; let mut ans = 0; let mut next_distance = 0; - for (n, &i) in nums.iter().enumerate() { - next_distance = (n as i32 + i).max(next_distance); + for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) { + next_distance = (n as usize + i).max(next_distance); if i == cur_distance { - if cur_distance < n as i32 - 1 { + if cur_distance < nums.len() - 1 { ans += 1; cur_distance = next_distance; - if next_distance >= n as i32 - 1 { + if next_distance >= nums.len() - 1 { break; }; } else { @@ -403,7 +403,6 @@ impl Solution { ans } } - ``` ```Rust @@ -416,8 +415,8 @@ impl Solution { let mut cur_distance = 0; let mut ans = 0; let mut next_distance = 0; - for (n, &i) in nums.iter().enumerate() { - next_distance = (n as i32 + i).max(next_distance); + for (i, &n) in nums.iter().enumerate().take(nums.len() - 1) { + next_distance = (n as usize + i).max(next_distance); if i == cur_distance { cur_distance = next_distance; ans += 1; @@ -426,7 +425,6 @@ impl Solution { ans } } - ``` From 411ef134a7e194e281affa0be45efe9bbcad4a25 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Tue, 14 Mar 2023 12:33:05 +0800 Subject: [PATCH 0047/1533] =?UTF-8?q?Update=201005.K=E6=AC=A1=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\346\225\260\347\273\204\345\222\214.md" | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index cdf42511b1..439bdfde9b 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -231,23 +231,18 @@ var largestSumAfterKNegations = function(nums, k) { ```Rust impl Solution { - pub fn largest_sum_after_k_negations(nums: Vec, k: i32) -> i32 { - let mut nums = nums; - let mut k = k; - let len = nums.len(); - nums.sort_by(|a, b| b.abs().cmp(&a.abs())); - for i in 0..len { - if nums[i] < 0 && k > 0 { - nums[i] *= -1; + pub fn largest_sum_after_k_negations(mut nums: Vec, mut k: i32) -> i32 { + nums.sort_by_key(|b| std::cmp::Reverse(b.abs())); + for v in nums.iter_mut() { + if *v < 0 && k > 0 { + *v *= -1; k -= 1; } } - if k % 2 == 1 { nums[len - 1] *= -1; } - let mut result = 0; - for num in nums { - result += num; + if k % 2 == 1 { + *nums.last_mut().unwrap() *= -1; } - result + nums.iter().sum() } } ``` From d1eccfba1a51880bece8edba08894000591eb3e0 Mon Sep 17 00:00:00 2001 From: StriveDD Date: Tue, 14 Mar 2023 19:05:39 +0800 Subject: [PATCH 0048/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00417.=E5=A4=AA?= =?UTF-8?q?=E5=B9=B3=E6=B4=8B=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98.md=E7=9A=84Java=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 124 +++++++++++++++++- ...60\347\232\204\346\225\260\351\207\217.md" | 3 +- 2 files changed, 123 insertions(+), 4 deletions(-) diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index f936399b44..0ec8ebf9e7 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -230,17 +230,137 @@ for (int j = 0; j < m; j++) { dfs (heights, pacific, 0, j); // 遍历最左列,接触太平洋 dfs (heights, atlantic, n - 1, j); // 遍历最右列,接触大西洋 } -``` +``` 那么本题整体的时间复杂度其实是: 2 * n * m + n * m ,所以最终时间复杂度为 O(n * m) 。 空间复杂度为:O(n * m) 这个就不难理解了。开了几个 n * m 的数组。 - ## 其他语言版本 +### Java + +深度优先遍历: + +```Java +class Solution { + // 四个位置 + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + + /** + * @param heights 题目给定的二维数组 + * @param row 当前位置的行号 + * @param col 当前位置的列号 + * @param sign 记录是哪一条河,两条河中可以一个为 0,一个为 1 + * @param visited 记录这个位置可以到哪条河 + */ + public void dfs(int[][] heights, int row, int col, int sign, boolean[][][] visited) { + for (int[] current: position) { + int curRow = row + current[0], curCol = col + current[1]; + // 越界 + if (curRow < 0 || curRow >= heights.length || curCol < 0 || curCol >= heights[0].length) + continue; + // 高度不合适或者已经被访问过了 + if (heights[curRow][curCol] < heights[row][col] || visited[curRow][curCol][sign]) continue; + visited[curRow][curCol][sign] = true; + dfs(heights, curRow, curCol, sign, visited); + } + } + + public List> pacificAtlantic(int[][] heights) { + int rowSize = heights.length, colSize = heights[0].length; + List> ans = new ArrayList<>(); + // 记录 [row, col] 位置是否可以到某条河,可以为 true,反之为 false; + // 假设太平洋的标记为 1,大西洋为 0 + boolean[][][] visited = new boolean[rowSize][colSize][2]; + for (int row = 0; row < rowSize; row++) { + visited[row][colSize - 1][0] = true; + visited[row][0][1] = true; + dfs(heights, row, colSize - 1, 0, visited); + dfs(heights, row, 0, 1, visited); + } + for (int col = 0; col < colSize; col++) { + visited[rowSize - 1][col][0] = true; + visited[0][col][1] = true; + dfs(heights, rowSize - 1, col, 0, visited); + dfs(heights, 0, col, 1, visited); + } + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + // 如果该位置即可以到太平洋又可以到大西洋,就放入答案数组 + if (visited[row][col][0] && visited[row][col][1]) + ans.add(List.of(row, col)); + } + } + return ans; + } +} +``` + +广度优先遍历: + +```Java +class Solution { + // 四个位置 + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + + /** + * @param heights 题目给定的二维数组 + * @param queue 记录可以到达边界的节点 + * @param visited 记录这个位置可以到哪条河 + */ + public void bfs(int[][] heights, Queue queue, boolean[][][] visited) { + while (!queue.isEmpty()) { + int[] curPos = queue.poll(); + for (int[] current: position) { + int row = curPos[0] + current[0], col = curPos[1] + current[1], sign = curPos[2]; + // 越界 + if (row < 0 || row >= heights.length || col < 0 || col >= heights[0].length) continue; + // 高度不合适或者已经被访问过了 + if (heights[row][col] < heights[curPos[0]][curPos[1]] || visited[row][col][sign]) continue; + visited[row][col][sign] = true; + queue.add(new int[]{row, col, sign}); + } + } + } + + public List> pacificAtlantic(int[][] heights) { + int rowSize = heights.length, colSize = heights[0].length; + List> ans = new ArrayList<>(); + boolean[][][] visited = new boolean[rowSize][colSize][2]; + // 队列,保存的数据为 [行号, 列号, 标记] + // 假设太平洋的标记为 1,大西洋为 0 + Queue queue = new ArrayDeque<>(); + for (int row = 0; row < rowSize; row++) { + visited[row][colSize - 1][0] = true; + visited[row][0][1] = true; + queue.add(new int[]{row, colSize - 1, 0}); + queue.add(new int[]{row, 0, 1}); + } + for (int col = 0; col < colSize; col++) { + visited[rowSize - 1][col][0] = true; + visited[0][col][1] = true; + queue.add(new int[]{rowSize - 1, col, 0}); + queue.add(new int[]{0, col, 1}); + } + bfs(heights, queue, visited); + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + // 如果该位置即可以到太平洋又可以到大西洋,就放入答案数组 + if (visited[row][col][0] && visited[row][col][1]) + ans.add(List.of(row, col)); + } + } + return ans; + } +} +``` + + +

+ diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index ad848e7379..d244e60c71 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -146,7 +146,7 @@ public: ``` ## 其他语言版本 -**Java**: +### Java 深度优先遍历版本: @@ -277,4 +277,3 @@ class Solution { - From d002d8769cfae23d0f5656bf9f36ba10590371d7 Mon Sep 17 00:00:00 2001 From: StriveDD Date: Wed, 15 Mar 2023 20:36:34 +0800 Subject: [PATCH 0049/1533] =?UTF-8?q?=E6=8F=90=E4=BA=A41020.=E9=A3=9E?= =?UTF-8?q?=E6=8A=B5=E7=9A=84=E6=95=B0=E9=87=8F.md=E7=9A=84Python=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81=EF=BC=8C0417.=E5=A4=AA=E5=B9=B3?= =?UTF-8?q?=E6=B4=8B=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98.md=E7=9A=84Python=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 95 ++++++++++++++- ...60\347\232\204\346\225\260\351\207\217.md" | 113 +++++++++++++++++- 2 files changed, 202 insertions(+), 6 deletions(-) diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 0ec8ebf9e7..ec22936577 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -356,6 +356,100 @@ class Solution { } ``` +### Python + +深度优先遍历 + +```Python3 +class Solution: + def __init__(self): + self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向 + + # heights:题目给定的二维数组, row:当前位置的行号, col:当前位置的列号 + # sign:记录是哪一条河,两条河中可以一个为 0,一个为 1 + # visited:记录这个位置可以到哪条河 + def dfs(self, heights: List[List[int]], row: int, col: int, sign: int, visited: List[List[List[int]]]): + for current in self.position: + curRow, curCol = row + current[0], col + current[1] + # 索引下标越界 + if curRow < 0 or curRow >= len(heights) or curCol < 0 or curCol >= len(heights[0]): continue + # 不满足条件或者已经被访问过 + if heights[curRow][curCol] < heights[row][col] or visited[curRow][curCol][sign]: continue + visited[curRow][curCol][sign] = True + self.dfs(heights, curRow, curCol, sign, visited) + + def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: + rowSize, colSize = len(heights), len(heights[0]) + # visited 记录 [row, col] 位置是否可以到某条河,可以为 true,反之为 false; + # 假设太平洋的标记为 1,大西洋为 0 + # ans 用来保存满足条件的答案 + ans, visited = [], [[[False for _ in range(2)] for _ in range(colSize)] for _ in range(rowSize)] + for row in range(rowSize): + visited[row][0][1] = True + visited[row][colSize - 1][0] = True + self.dfs(heights, row, 0, 1, visited) + self.dfs(heights, row, colSize - 1, 0, visited) + for col in range(0, colSize): + visited[0][col][1] = True + visited[rowSize - 1][col][0] = True + self.dfs(heights, 0, col, 1, visited) + self.dfs(heights, rowSize - 1, col, 0, visited) + for row in range(rowSize): + for col in range(colSize): + # 如果该位置即可以到太平洋又可以到大西洋,就放入答案数组 + if visited[row][col][0] and visited[row][col][1]: + ans.append([row, col]) + return ans +``` + +广度优先遍历 + +```Python3 +class Solution: + def __init__(self): + self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] + + # heights:题目给定的二维数组,visited:记录这个位置可以到哪条河 + def bfs(self, heights: List[List[int]], queue: deque, visited: List[List[List[int]]]): + while queue: + curPos = queue.popleft() + for current in self.position: + row, col, sign = curPos[0] + current[0], curPos[1] + current[1], curPos[2] + # 越界 + if row < 0 or row >= len(heights) or col < 0 or col >= len(heights[0]): continue + # 不满足条件或已经访问过 + if heights[row][col] < heights[curPos[0]][curPos[1]] or visited[row][col][sign]: continue + visited[row][col][sign] = True + queue.append([row, col, sign]) + + def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: + rowSize, colSize = len(heights), len(heights[0]) + # visited 记录 [row, col] 位置是否可以到某条河,可以为 true,反之为 false; + # 假设太平洋的标记为 1,大西洋为 0 + # ans 用来保存满足条件的答案 + ans, visited = [], [[[False for _ in range(2)] for _ in range(colSize)] for _ in range(rowSize)] + # 队列,保存的数据为 [行号, 列号, 标记] + # 假设太平洋的标记为 1,大西洋为 0 + queue = deque() + for row in range(rowSize): + visited[row][0][1] = True + visited[row][colSize - 1][0] = True + queue.append([row, 0, 1]) + queue.append([row, colSize - 1, 0]) + for col in range(0, colSize): + visited[0][col][1] = True + visited[rowSize - 1][col][0] = True + queue.append([0, col, 1]) + queue.append([rowSize - 1, col, 0]) + self.bfs(heights, queue, visited) # 广度优先遍历 + for row in range(rowSize): + for col in range(colSize): + # 如果该位置即可以到太平洋又可以到大西洋,就放入答案数组 + if visited[row][col][0] and visited[row][col][1]: + ans.append([row, col]) + return ans +``` + @@ -363,4 +457,3 @@ class Solution { - diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index d244e60c71..f97678e872 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -170,7 +170,8 @@ class Solution { public int numEnclaves(int[][] grid) { int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案 - boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组 + // 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 true,反之为 false + boolean[][] visited = new boolean[rowSize][colSize]; // 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历 for (int row = 0; row < rowSize; row++) { if (grid[row][0] == 1 && !visited[row][0]) { @@ -230,9 +231,10 @@ class Solution { public int numEnclaves(int[][] grid) { int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案 - boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组 + // 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 true,反之为 false + boolean[][] visited = new boolean[rowSize][colSize]; Queue queue = new ArrayDeque<>(); - // 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历 + // 搜索左侧边界和右侧边界查找 1 存入队列 for (int row = 0; row < rowSize; row++) { if (grid[row][0] == 1) { visited[row][0] = true; @@ -243,7 +245,7 @@ class Solution { queue.add(new int[]{row, colSize - 1}); } } - // 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了 + // 搜索上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了 for (int col = 1; col < colSize - 1; col++) { if (grid[0][col] == 1) { visited[0][col] = true; @@ -254,7 +256,7 @@ class Solution { queue.add(new int[]{rowSize - 1, col}); } } - bfs(grid, queue, visited); + bfs(grid, queue, visited); // 广度优先遍历 // 查找没有标记过的 1,记录到 ans 中 for (int row = 0; row < rowSize; row++) { for (int col = 0; col < colSize; col++) { @@ -266,6 +268,106 @@ class Solution { } ``` +### Python + +深度优先遍历 + +```Python3 +class Solution: + def __init__(self): + self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向 + + # 深度优先遍历,把可以通向边缘部分的 1 全部标记成 true + def dfs(self, grid: List[List[int]], row: int, col: int, visited: List[List[bool]]) -> None: + for current in self.position: + newRow, newCol = row + current[0], col + current[1] + # 索引下标越界 + if newRow < 0 or newRow >= len(grid) or newCol < 0 or newCol >= len(grid[0]): + continue + # 当前位置值不是 1 或者已经被访问过了 + if grid[newRow][newCol] == 0 or visited[newRow][newCol]: continue + visited[newRow][newCol] = True + self.dfs(grid, newRow, newCol, visited) + + def numEnclaves(self, grid: List[List[int]]) -> int: + rowSize, colSize, ans = len(grid), len(grid[0]), 0 + # 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 True,反之为 False + visited = [[False for _ in range(colSize)] for _ in range(rowSize)] + # 搜索左边界和右边界,对值为 1 的位置进行深度优先遍历 + for row in range(rowSize): + if grid[row][0] == 1: + visited[row][0] = True + self.dfs(grid, row, 0, visited) + if grid[row][colSize - 1] == 1: + visited[row][colSize - 1] = True + self.dfs(grid, row, colSize - 1, visited) + # 搜索上边界和下边界,对值为 1 的位置进行深度优先遍历,但是四个角不需要,因为上面遍历过了 + for col in range(1, colSize - 1): + if grid[0][col] == 1: + visited[0][col] = True + self.dfs(grid, 0, col, visited) + if grid[rowSize - 1][col] == 1: + visited[rowSize - 1][col] = True + self.dfs(grid, rowSize - 1, col, visited) + # 找出矩阵中值为 1 但是没有被标记过的位置,记录答案 + for row in range(rowSize): + for col in range(colSize): + if grid[row][col] == 1 and not visited[row][col]: + ans += 1 + return ans +``` + +广度优先遍历 + +```Python3 +class Solution: + def __init__(self): + self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向 + + # 广度优先遍历,把可以通向边缘部分的 1 全部标记成 true + def bfs(self, grid: List[List[int]], queue: deque, visited: List[List[bool]]) -> None: + while queue: + curPos = queue.popleft() + for current in self.position: + row, col = curPos[0] + current[0], curPos[1] + current[1] + # 索引下标越界 + if row < 0 or row >= len(grid) or col < 0 or col >= len(grid[0]): continue + # 当前位置值不是 1 或者已经被访问过了 + if grid[row][col] == 0 or visited[row][col]: continue + visited[row][col] = True + queue.append([row, col]) + + + def numEnclaves(self, grid: List[List[int]]) -> int: + rowSize, colSize, ans = len(grid), len(grid[0]), 0 + # 标记数组记录每个值为 1 的位置是否可以到达边界,可以为 True,反之为 False + visited = [[False for _ in range(colSize)] for _ in range(rowSize)] + queue = deque() # 队列 + # 搜索左侧边界和右侧边界查找 1 存入队列 + for row in range(rowSize): + if grid[row][0] == 1: + visited[row][0] = True + queue.append([row, 0]) + if grid[row][colSize - 1] == 1: + visited[row][colSize - 1] = True + queue.append([row, colSize - 1]) + # 搜索上边界和下边界查找 1 存入队列,但是四个角不用遍历,因为上面已经遍历到了 + for col in range(1, colSize - 1): + if grid[0][col] == 1: + visited[0][col] = True + queue.append([0, col]) + if grid[rowSize - 1][col] == 1: + visited[rowSize - 1][col] = True + queue.append([rowSize - 1, col]) + self.bfs(grid, queue, visited) # 广度优先遍历 + # 找出矩阵中值为 1 但是没有被标记过的位置,记录答案 + for row in range(rowSize): + for col in range(colSize): + if grid[row][col] == 1 and not visited[row][col]: + ans += 1 + return ans +``` + ## 类似题目 @@ -277,3 +379,4 @@ class Solution { + From ef00e2c35dfe1ecb2223fe4a32b75cfa91ec042c Mon Sep 17 00:00:00 2001 From: ZashJie <1030298236@qq.com> Date: Thu, 16 Mar 2023 12:20:42 +0800 Subject: [PATCH 0050/1533] modify_01 --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index ff0b6aba28..c45fc3d302 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -87,7 +87,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 那么可以有两个方向推出来dp[i][j], -* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以被背包内的价值依然和前面相同。) +* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以背包内的价值依然和前面相同。) * **放物品i**:由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]的时候不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 所以递归公式: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); From 2175be827dbe4215f3ccf55048e62b158a936a0e Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 16 Mar 2023 22:24:32 +0800 Subject: [PATCH 0051/1533] =?UTF-8?q?update=200242.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D:=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 9f84a5cd78..1006ea3552 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -85,6 +85,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + ## 其他语言版本 From 9a4086cd3628b0199e7e4b51eefe7dfab35688b3 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Thu, 16 Mar 2023 19:55:49 -0400 Subject: [PATCH 0052/1533] =?UTF-8?q?Update=200222.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA?= =?UTF-8?q?=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加完全二叉树写法2,更易理解且减少非完全二叉树节点的迭代次数 --- ...212\202\347\202\271\344\270\252\346\225\260.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index c346b6ff19..d89a9bcea6 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -379,6 +379,20 @@ class Solution: return (2 << leftDepth) - 1 #注意(2<<1) 相当于2^2,所以leftDepth初始为0 return self.countNodes(root.left) + self.countNodes(root.right) + 1 ``` +完全二叉树写法2 +```python +class Solution: # 利用完全二叉树特性 + def countNodes(self, root: TreeNode) -> int: + if not root: return 0 + count = 1 + left = root.left; right = root.right + while left and right: + count+=1 + left = left.left; right = right.right + if not left and not right: # 如果同时到底说明是满二叉树,反之则不是 + return 2**count-1 + return 1+self.countNodes(root.left)+self.countNodes(root.right) +``` ## Go From c3588f445f13ec540e7c44e031924d946922a588 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:09:35 +0800 Subject: [PATCH 0053/1533] =?UTF-8?q?docs:=20=E5=88=86=E5=8F=91=E9=A5=BC?= =?UTF-8?q?=E5=B9=B2=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E8=AE=B2=E8=A7=A3?= =?UTF-8?q?=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\217\221\351\245\274\345\271\262.md" | 153 +++++++++--------- 1 file changed, 79 insertions(+), 74 deletions(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 63525b0394..3688b3fd95 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -4,31 +4,35 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- # 455.分发饼干 [力扣题目链接](https://leetcode.cn/problems/assign-cookies/) 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。 -对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。 +对每个孩子 i,都有一个胃口值  g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。 + +示例  1: -示例 1: -* 输入: g = [1,2,3], s = [1,1] -* 输出: 1 -解释:你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。所以你应该输出1。 +- 输入: g = [1,2,3], s = [1,1] +- 输出: 1 + 解释:你有三个孩子和两块小饼干,3 个孩子的胃口值分别是:1,2,3。虽然你有两块小饼干,由于他们的尺寸都是 1,你只能让胃口值是 1 的孩子满足。所以你应该输出 1。 -示例 2: -* 输入: g = [1,2], s = [1,2,3] -* 输出: 2 -* 解释:你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。你拥有的饼干数量和尺寸都足以让所有孩子满足。所以你应该输出2. +示例  2: +- 输入: g = [1,2], s = [1,2,3] +- 输出: 2 +- 解释:你有两个孩子和三块小饼干,2 个孩子的胃口值分别是 1,2。你拥有的饼干数量和尺寸都足以让所有孩子满足。所以你应该输出 2. 提示: -* 1 <= g.length <= 3 * 10^4 -* 0 <= s.length <= 3 * 10^4 -* 1 <= g[i], s[j] <= 2^31 - 1 +- 1 <= g.length <= 3 \* 10^4 +- 0 <= s.length <= 3 \* 10^4 +- 1 <= g[i], s[j] <= 2^31 - 1 + +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -46,14 +50,12 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203105634.png) - -这个例子可以看出饼干9只有喂给胃口为7的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。 - +这个例子可以看出饼干 9 只有喂给胃口为 7 的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。 C++代码整体如下: ```CPP -// 版本一 +// 版本一 // 时间复杂度:O(nlogn) // 空间复杂度:O(1) class Solution { @@ -63,8 +65,8 @@ public: sort(s.begin(), s.end()); int index = s.size() - 1; // 饼干数组的下标 int result = 0; - for (int i = g.size() - 1; i >= 0; i--) { // 遍历胃口 - if (index >= 0 && s[index] >= g[i]) { // 遍历饼干 + for (int i = g.size() - 1; i >= 0; i--) { // 遍历胃口 + if (index >= 0 && s[index] >= g[i]) { // 遍历饼干 result++; index--; } @@ -74,27 +76,25 @@ public: }; ``` -从代码中可以看出我用了一个index来控制饼干数组的遍历,遍历饼干并没有再起一个for循环,而是采用自减的方式,这也是常用的技巧。 - -有的同学看到要遍历两个数组,就想到用两个for循环,那样逻辑其实就复杂了。 +从代码中可以看出我用了一个 index 来控制饼干数组的遍历,遍历饼干并没有再起一个 for 循环,而是采用自减的方式,这也是常用的技巧。 +有的同学看到要遍历两个数组,就想到用两个 for 循环,那样逻辑其实就复杂了。 -### 注意事项 +### 注意事项 -注意版本一的代码中,可以看出来,是先遍历的胃口,在遍历的饼干,那么可不可以 先遍历 饼干,在遍历胃口呢? +注意版本一的代码中,可以看出来,是先遍历的胃口,在遍历的饼干,那么可不可以 先遍历 饼干,在遍历胃口呢? -其实是不可以的。 +其实是不可以的。 -外面的for 是里的下标i 是固定移动的,而if里面的下标 index 是符合条件才移动的。 +外面的 for 是里的下标 i 是固定移动的,而 if 里面的下标 index 是符合条件才移动的。 -如果 for 控制的是饼干, if 控制胃口,就是出现如下情况 : +如果 for 控制的是饼干, if 控制胃口,就是出现如下情况 : ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230112102848.png) -if 里的 index 指向 胃口 10, for里的i指向饼干9,因为 饼干9 满足不了 胃口10,所以 i 持续向前移动,而index 走不到` s[index] >= g[i]` 的逻辑,所以index不会移动,那么当i 持续向前移动,最后所有的饼干都匹配不上。 - -所以 一定要for 控制 胃口,里面的if控制饼干。 +if 里的 index 指向 胃口 10, for 里的 i 指向饼干 9,因为 饼干 9 满足不了 胃口 10,所以 i 持续向前移动,而 index 走不到` s[index] >= g[i]` 的逻辑,所以 index 不会移动,那么当 i 持续向前移动,最后所有的饼干都匹配不上。 +所以 一定要 for 控制 胃口,里面的 if 控制饼干。 ### 其他思路 @@ -117,11 +117,11 @@ public: return index; } }; -``` +``` -细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。 +细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。 -理由在上面 “注意事项”中 已经讲过。 +理由在上面 “注意事项”中 已经讲过。 ## 总结 @@ -131,8 +131,8 @@ public: ## 其他语言版本 - ### Java + ```java class Solution { // 思路1:优先考虑饼干,小饼干先喂饱小胃口 @@ -151,6 +151,7 @@ class Solution { } } ``` + ```java class Solution { // 思路2:优先考虑胃口,先喂饱大胃口 @@ -172,6 +173,7 @@ class Solution { ``` ### Python + ```python class Solution: # 思路1:优先考虑小胃口 @@ -184,6 +186,7 @@ class Solution: res += 1 return res ``` + ```python class Solution: # 思路2:优先考虑大胃口 @@ -199,6 +202,7 @@ class Solution: ``` ### Go + ```golang //排序后,局部最优 func findContentChildren(g []int, s []int) int { @@ -218,6 +222,7 @@ func findContentChildren(g []int, s []int) int { ``` ### Rust + ```rust pub fn find_content_children(mut children: Vec, mut cookie: Vec) -> i32 { children.sort(); @@ -236,21 +241,21 @@ pub fn find_content_children(mut children: Vec, mut cookie: Vec) -> i3 ``` ### Javascript + ```js -var findContentChildren = function(g, s) { - g = g.sort((a, b) => a - b) - s = s.sort((a, b) => a - b) - let result = 0 - let index = s.length - 1 - for(let i = g.length - 1; i >= 0; i--) { - if(index >= 0 && s[index] >= g[i]) { - result++ - index-- - } +var findContentChildren = function (g, s) { + g = g.sort((a, b) => a - b); + s = s.sort((a, b) => a - b); + let result = 0; + let index = s.length - 1; + for (let i = g.length - 1; i >= 0; i--) { + if (index >= 0 && s[index] >= g[i]) { + result++; + index--; } - return result + } + return result; }; - ``` ### TypeScript @@ -258,41 +263,41 @@ var findContentChildren = function(g, s) { ```typescript // 大饼干尽量喂胃口大的 function findContentChildren(g: number[], s: number[]): number { - g.sort((a, b) => a - b); - s.sort((a, b) => a - b); - const childLength: number = g.length, - cookieLength: number = s.length; - let curChild: number = childLength - 1, - curCookie: number = cookieLength - 1; - let resCount: number = 0; - while (curChild >= 0 && curCookie >= 0) { - if (g[curChild] <= s[curCookie]) { - curCookie--; - resCount++; - } - curChild--; + g.sort((a, b) => a - b); + s.sort((a, b) => a - b); + const childLength: number = g.length, + cookieLength: number = s.length; + let curChild: number = childLength - 1, + curCookie: number = cookieLength - 1; + let resCount: number = 0; + while (curChild >= 0 && curCookie >= 0) { + if (g[curChild] <= s[curCookie]) { + curCookie--; + resCount++; } - return resCount; -}; + curChild--; + } + return resCount; +} ``` ```typescript // 小饼干先喂饱小胃口的 function findContentChildren(g: number[], s: number[]): number { - g.sort((a, b) => a - b); - s.sort((a, b) => a - b); - const childLength: number = g.length, - cookieLength: number = s.length; - let curChild: number = 0, - curCookie: number = 0; - while (curChild < childLength && curCookie < cookieLength) { - if (g[curChild] <= s[curCookie]) { - curChild++; - } - curCookie++; + g.sort((a, b) => a - b); + s.sort((a, b) => a - b); + const childLength: number = g.length, + cookieLength: number = s.length; + let curChild: number = 0, + curCookie: number = 0; + while (curChild < childLength && curCookie < cookieLength) { + if (g[curChild] <= s[curCookie]) { + curChild++; } - return curChild; -}; + curCookie++; + } + return curChild; +} ``` ### C From 5678f566a8985e145c22f8297abc38744cc98463 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:11:35 +0800 Subject: [PATCH 0054/1533] =?UTF-8?q?docs:=20=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E8=AE=B2=E8=A7=A3?= =?UTF-8?q?=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\212\250\345\272\217\345\210\227.md" | 215 +++++++++--------- 1 file changed, 111 insertions(+), 104 deletions(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 925d726270..469c19fd8d 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -4,7 +4,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- > 本周讲解了[贪心理论基础](https://programmercarl.com/贪心算法理论基础.html),以及第一道贪心的题目:[贪心算法:分发饼干](https://programmercarl.com/0455.分发饼干.html),可能会给大家一种贪心算法比较简单的错觉,好了,接下来几天的题目难度要上来了,哈哈。 # 376. 摆动序列 @@ -13,25 +12,32 @@ 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。 -例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的。相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。 +例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3)  是正负交替出现的。相反, [1,4,7,2,5]  和  [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。 给定一个整数序列,返回作为摆动序列的最长子序列的长度。 通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。 示例 1: -* 输入: [1,7,4,9,2,5] -* 输出: 6 -* 解释: 整个序列均为摆动序列。 + +- 输入: [1,7,4,9,2,5] +- 输出: 6 +- 解释: 整个序列均为摆动序列。 示例 2: -* 输入: [1,17,5,10,13,15,10,5,16,8] -* 输出: 7 -* 解释: 这个序列包含几个长度为 7 摆动序列,其中一个可为[1,17,10,13,10,16,8]。 + +- 输入: [1,17,5,10,13,15,10,5,16,8] +- 输出: 7 +- 解释: 这个序列包含几个长度为 7 摆动序列,其中一个可为[1,17,10,13,10,16,8]。 示例 3: -* 输入: [1,2,3,4,5,6,7,8,9] -* 输出: 2 -## 思路1(贪心解法) +- 输入: [1,2,3,4,5,6,7,8,9] +- 输出: 2 + +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + +## 思路 1(贪心解法) 本题要求通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。 @@ -53,63 +59,61 @@ **实际操作上,其实连删除的操作都不用做,因为题目要求的是最长摆动子序列的长度,所以只需要统计数组的峰值数量就可以了(相当于是删除单一坡度上的节点,然后统计长度)** -**这就是贪心所贪的地方,让峰值尽可能的保持峰值,然后删除单一坡度上的节点** +**这就是贪心所贪的地方,让峰值尽可能的保持峰值,然后删除单一坡度上的节点** -在计算是否有峰值的时候,大家知道遍历的下标i ,计算prediff(nums[i] - nums[i-1]) 和 curdiff(nums[i+1] - nums[i]),如果`prediff < 0 && curdiff > 0` 或者 `prediff > 0 && curdiff < 0` 此时就有波动就需要统计。 +在计算是否有峰值的时候,大家知道遍历的下标 i ,计算 prediff(nums[i] - nums[i-1]) 和 curdiff(nums[i+1] - nums[i]),如果`prediff < 0 && curdiff > 0` 或者 `prediff > 0 && curdiff < 0` 此时就有波动就需要统计。 这是我们思考本题的一个大题思路,但本题要考虑三种情况: -1. 情况一:上下坡中有平坡 -2. 情况二:数组首尾两端 +1. 情况一:上下坡中有平坡 +2. 情况二:数组首尾两端 3. 情况三:单调坡中有平坡 ### 情况一:上下坡中有平坡 -例如 [1,2,2,2,1]这样的数组,如图: +例如 [1,2,2,2,1]这样的数组,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106170449.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106170449.png) -它的摇摆序列长度是多少呢? **其实是长度是3**,也就是我们在删除的时候 要不删除左面的三个2,要不就删除右边的三个2。 +它的摇摆序列长度是多少呢? **其实是长度是 3**,也就是我们在删除的时候 要不删除左面的三个 2,要不就删除右边的三个 2。 -如图,可以统一规则,删除左边的三个2: +如图,可以统一规则,删除左边的三个 2: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106172613.png) -在图中,当i指向第一个2的时候,`prediff > 0 && curdiff = 0` ,当 i 指向最后一个2的时候 `prediff = 0 && curdiff < 0`。 - -如果我们采用,删左面三个2的规则,那么 当 `prediff = 0 && curdiff < 0` 也要记录一个峰值,因为他是把之前相同的元素都删掉留下的峰值。 - -所以我们记录峰值的条件应该是: `(preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)`,为什么这里允许 prediff == 0 ,就是为了 上面我说的这种情况。 +在图中,当 i 指向第一个 2 的时候,`prediff > 0 && curdiff = 0` ,当 i 指向最后一个 2 的时候 `prediff = 0 && curdiff < 0`。 +如果我们采用,删左面三个 2 的规则,那么 当 `prediff = 0 && curdiff < 0` 也要记录一个峰值,因为他是把之前相同的元素都删掉留下的峰值。 -### 情况二:数组首尾两端 +所以我们记录峰值的条件应该是: `(preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)`,为什么这里允许 prediff == 0 ,就是为了 上面我说的这种情况。 +### 情况二:数组首尾两端 -所以本题统计峰值的时候,数组最左面和最右面如果统计呢? +所以本题统计峰值的时候,数组最左面和最右面如果统计呢? -题目中说了,如果只有两个不同的元素,那摆动序列也是2。 +题目中说了,如果只有两个不同的元素,那摆动序列也是 2。 -例如序列[2,5],如果靠统计差值来计算峰值个数就需要考虑数组最左面和最右面的特殊情况。 +例如序列[2,5],如果靠统计差值来计算峰值个数就需要考虑数组最左面和最右面的特殊情况。 因为我们在计算 prediff(nums[i] - nums[i-1]) 和 curdiff(nums[i+1] - nums[i])的时候,至少需要三个数字才能计算,而数组只有两个数字。 -这里我们可以写死,就是 如果只有两个元素,且元素不同,那么结果为2。 +这里我们可以写死,就是 如果只有两个元素,且元素不同,那么结果为 2。 -不写死的话,如果和我们的判断规则结合在一起呢? +不写死的话,如果和我们的判断规则结合在一起呢? -可以假设,数组最前面还有一个数字,那这个数字应该是什么呢? +可以假设,数组最前面还有一个数字,那这个数字应该是什么呢? -之前我们在 讨论 情况一:相同数字连续 的时候, prediff = 0 ,curdiff < 0 或者 >0 也记为波谷。 +之前我们在 讨论 情况一:相同数字连续 的时候, prediff = 0 ,curdiff < 0 或者 >0 也记为波谷。 -那么为了规则统一,针对序列[2,5],可以假设为[2,2,5],这样它就有坡度了即preDiff = 0,如图: +那么为了规则统一,针对序列[2,5],可以假设为[2,2,5],这样它就有坡度了即 preDiff = 0,如图: ![376.摆动序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124174357612.png) -针对以上情形,result初始为1(默认最右面有一个峰值),此时curDiff > 0 && preDiff <= 0,那么result++(计算了左面的峰值),最后得到的result就是2(峰值个数为2即摆动序列长度为2) +针对以上情形,result 初始为 1(默认最右面有一个峰值),此时 curDiff > 0 && preDiff <= 0,那么 result++(计算了左面的峰值),最后得到的 result 就是 2(峰值个数为 2 即摆动序列长度为 2) 经过以上分析后,我们可以写出如下代码: -```CPP +```CPP // 版本一 class Solution { public: @@ -129,33 +133,34 @@ public: return result; } }; -``` -* 时间复杂度:O(n) -* 空间复杂度:O(1) +``` -此时大家是不是发现 以上代码提交也不能通过本题? +- 时间复杂度:O(n) +- 空间复杂度:O(1) + +此时大家是不是发现 以上代码提交也不能通过本题? 所以此时我们要讨论情况三! -### 情况三:单调坡度有平坡 +### 情况三:单调坡度有平坡 -在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: +在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108171505.png) -图中,我们可以看出,版本一的代码在三个地方记录峰值,但其实结果因为是2,因为 单调中的平坡 不能算峰值(即摆动)。 +图中,我们可以看出,版本一的代码在三个地方记录峰值,但其实结果因为是 2,因为 单调中的平坡 不能算峰值(即摆动)。 -之所以版本一会出问题,是因为我们实时更新了 prediff。 +之所以版本一会出问题,是因为我们实时更新了 prediff。 -那么我们应该什么时候更新prediff呢? +那么我们应该什么时候更新 prediff 呢? -我们只需要在 这个坡度 摆动变化的时候,更新prediff就行,这样prediff在 单调区间有平坡的时候 就不会发生变化,造成我们的误判。 +我们只需要在 这个坡度 摆动变化的时候,更新 prediff 就行,这样 prediff 在 单调区间有平坡的时候 就不会发生变化,造成我们的误判。 所以本题的最终代码为: ```CPP -// 版本二 +// 版本二 class Solution { public: int wiggleMaxLength(vector& nums) { @@ -168,7 +173,7 @@ public: // 出现峰值 if ((preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)) { result++; - preDiff = curDiff; // 注意这里,只在摆动变化的时候更新prediff + preDiff = curDiff; // 注意这里,只在摆动变化的时候更新prediff } } return result; @@ -176,25 +181,25 @@ public: }; ``` -其实本题看起来好像简单,但需要考虑的情况还是很复杂的,而且很难一次性想到位。 +其实本题看起来好像简单,但需要考虑的情况还是很复杂的,而且很难一次性想到位。 -**本题异常情况的本质,就是要考虑平坡**, 平坡分两种,一个是 上下中间有平坡,一个是单调有平坡,如图: +**本题异常情况的本质,就是要考虑平坡**, 平坡分两种,一个是 上下中间有平坡,一个是单调有平坡,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108174452.png) -## 思路2(动态规划) +## 思路 2(动态规划) 考虑用动态规划的思想来解决这个问题。 -很容易可以发现,对于我们当前考虑的这个数,要么是作为山峰(即nums[i] > nums[i-1]),要么是作为山谷(即nums[i] < nums[i - 1])。 +很容易可以发现,对于我们当前考虑的这个数,要么是作为山峰(即 nums[i] > nums[i-1]),要么是作为山谷(即 nums[i] < nums[i - 1])。 -* 设dp状态`dp[i][0]`,表示考虑前i个数,第i个数作为山峰的摆动子序列的最长长度 -* 设dp状态`dp[i][1]`,表示考虑前i个数,第i个数作为山谷的摆动子序列的最长长度 +- 设 dp 状态`dp[i][0]`,表示考虑前 i 个数,第 i 个数作为山峰的摆动子序列的最长长度 +- 设 dp 状态`dp[i][1]`,表示考虑前 i 个数,第 i 个数作为山谷的摆动子序列的最长长度 则转移方程为: -* `dp[i][0] = max(dp[i][0], dp[j][1] + 1)`,其中`0 < j < i`且`nums[j] < nums[i]`,表示将nums[i]接到前面某个山谷后面,作为山峰。 -* `dp[i][1] = max(dp[i][1], dp[j][0] + 1)`,其中`0 < j < i`且`nums[j] > nums[i]`,表示将nums[i]接到前面某个山峰后面,作为山谷。 +- `dp[i][0] = max(dp[i][0], dp[j][1] + 1)`,其中`0 < j < i`且`nums[j] < nums[i]`,表示将 nums[i]接到前面某个山谷后面,作为山峰。 +- `dp[i][1] = max(dp[i][1], dp[j][0] + 1)`,其中`0 < j < i`且`nums[j] > nums[i]`,表示将 nums[i]接到前面某个山峰后面,作为山谷。 初始状态: @@ -223,28 +228,25 @@ public: }; ``` -* 时间复杂度:O(n^2) -* 空间复杂度:O(n) +- 时间复杂度:O(n^2) +- 空间复杂度:O(n) **进阶** 可以用两棵线段树来维护区间的最大值 -* 每次更新`dp[i][0]`,则在`tree1`的`nums[i]`位置值更新为`dp[i][0]` -* 每次更新`dp[i][1]`,则在`tree2`的`nums[i]`位置值更新为`dp[i][1]` -* 则dp转移方程中就没有必要j从0遍历到i-1,可以直接在线段树中查询指定区间的值即可。 +- 每次更新`dp[i][0]`,则在`tree1`的`nums[i]`位置值更新为`dp[i][0]` +- 每次更新`dp[i][1]`,则在`tree2`的`nums[i]`位置值更新为`dp[i][1]` +- 则 dp 转移方程中就没有必要 j 从 0 遍历到 i-1,可以直接在线段树中查询指定区间的值即可。 时间复杂度:O(nlog n) 空间复杂度:O(n) - - - ## 其他语言版本 +### Java -### Java ```Java class Solution { public int wiggleMaxLength(int[] nums) { @@ -270,6 +272,7 @@ class Solution { } } ``` + ```java // DP class Solution { @@ -300,7 +303,7 @@ class Solution { } ``` -### Python +### Python **贪心** @@ -332,7 +335,7 @@ class Solution: # nums[i] 为波谷 if nums[j] > nums[i]: dp[i][1] = max(dp[i][1], dp[j][0] + 1) - # nums[i] 为波峰 + # nums[i] 为波峰 if nums[j] < nums[i]: dp[i][0] = max(dp[i][0], dp[j][1] + 1) return max(dp[-1][0], dp[-1][1]) @@ -357,9 +360,10 @@ class Solution: return max(up, down) ``` -### Go +### Go **贪心** + ```go func wiggleMaxLength(nums []int) int { n := len(nums) @@ -383,6 +387,7 @@ func wiggleMaxLength(nums []int) int { ``` **动态规划** + ```go func wiggleMaxLength(nums []int) int { n := len(nums) @@ -419,8 +424,10 @@ func max(a, b int) int { } ``` -### Javascript +### Javascript + **贪心** + ```Javascript var wiggleMaxLength = function(nums) { if(nums.length <= 1) return nums.length @@ -437,10 +444,12 @@ var wiggleMaxLength = function(nums) { return result }; ``` + **动态规划** + ```Javascript var wiggleMaxLength = function(nums) { - if (nums.length === 1) return 1; + if (nums.length === 1) return 1; // 考虑前i个数,当第i个值作为峰谷时的情况(则第i-1是峰顶) let down = 1; // 考虑前i个数,当第i个值作为峰顶时的情况(则第i-1是峰谷) @@ -458,7 +467,9 @@ var wiggleMaxLength = function(nums) { ``` ### Rust + **贪心** + ```Rust impl Solution { pub fn wiggle_max_length(nums: Vec) -> i32 { @@ -504,11 +515,12 @@ impl Solution { ``` ### C + **贪心** ```c int wiggleMaxLength(int* nums, int numsSize){ - if(numsSize <= 1) + if(numsSize <= 1) return numsSize; int length = 1; @@ -568,54 +580,49 @@ int wiggleMaxLength(int* nums, int numsSize){ } ``` - - ### TypeScript **贪心** ```typescript function wiggleMaxLength(nums: number[]): number { - let length: number = nums.length; - if (length <= 1) return length; - let preDiff: number = 0; - let curDiff: number = 0; - let count: number = 1; - for (let i = 1; i < length; i++) { - curDiff = nums[i] - nums[i - 1]; - if ( - (preDiff <= 0 && curDiff > 0) || - (preDiff >= 0 && curDiff < 0) - ) { - preDiff = curDiff; - count++; - } + let length: number = nums.length; + if (length <= 1) return length; + let preDiff: number = 0; + let curDiff: number = 0; + let count: number = 1; + for (let i = 1; i < length; i++) { + curDiff = nums[i] - nums[i - 1]; + if ((preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)) { + preDiff = curDiff; + count++; } - return count; -}; + } + return count; +} ``` **动态规划** ```typescript function wiggleMaxLength(nums: number[]): number { - const length: number = nums.length; - if (length <= 1) return length; - const dp: number[][] = new Array(length).fill(0).map(_ => []); - dp[0][0] = 1; // 第一个数作为波峰 - dp[0][1] = 1; // 第一个数作为波谷 - for (let i = 1; i < length; i++) { - dp[i][0] = 1; - dp[i][1] = 1; - for (let j = 0; j < i; j++) { - if (nums[j] < nums[i]) dp[i][0] = Math.max(dp[i][0], dp[j][1] + 1); - } - for (let j = 0; j < i; j++) { - if (nums[j] > nums[i]) dp[i][1] = Math.max(dp[i][1], dp[j][0] + 1); - } + const length: number = nums.length; + if (length <= 1) return length; + const dp: number[][] = new Array(length).fill(0).map((_) => []); + dp[0][0] = 1; // 第一个数作为波峰 + dp[0][1] = 1; // 第一个数作为波谷 + for (let i = 1; i < length; i++) { + dp[i][0] = 1; + dp[i][1] = 1; + for (let j = 0; j < i; j++) { + if (nums[j] < nums[i]) dp[i][0] = Math.max(dp[i][0], dp[j][1] + 1); } - return Math.max(dp[length - 1][0], dp[length - 1][1]); -}; + for (let j = 0; j < i; j++) { + if (nums[j] > nums[i]) dp[i][1] = Math.max(dp[i][1], dp[j][0] + 1); + } + } + return Math.max(dp[length - 1][0], dp[length - 1][1]); +} ``` ### Scala From ef8500d4cfa9f2669dec76157a01896d018b441e Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:14:43 +0800 Subject: [PATCH 0055/1533] =?UTF-8?q?docs:=20=E6=9C=80=E5=A4=A7=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=92=8C=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E8=AE=B2=E8=A7=A3=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\345\255\220\345\272\217\345\222\214.md" | 116 +++++++++--------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 14017f9899..1de68041a3 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -4,7 +4,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- # 53. 最大子序和 [力扣题目链接](https://leetcode.cn/problems/maximum-subarray/) @@ -12,17 +11,21 @@ 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: -* 输入: [-2,1,-3,4,-1,2,1,-5,4] -* 输出: 6 -* 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 +- 输入: [-2,1,-3,4,-1,2,1,-5,4] +- 输出: 6 +- 解释:  连续子数组  [4,-1,2,1] 的和最大,为  6。 + +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 暴力解法 -暴力解法的思路,第一层for 就是设置起始位置,第二层for循环遍历数组寻找最大值 +暴力解法的思路,第一层 for 就是设置起始位置,第二层 for 循环遍历数组寻找最大值 -* 时间复杂度:O(n^2) -* 空间复杂度:O(1) +- 时间复杂度:O(n^2) +- 空间复杂度:O(1) ```CPP class Solution { @@ -42,13 +45,13 @@ public: }; ``` -以上暴力的解法C++勉强可以过,其他语言就不确定了。 +以上暴力的解法 C++勉强可以过,其他语言就不确定了。 ## 贪心解法 **贪心贪的是哪里呢?** -如果 -2 1 在一起,计算起点的时候,一定是从1开始计算,因为负数只会拉低总和,这就是贪心贪的地方! +如果 -2 1 在一起,计算起点的时候,一定是从 1 开始计算,因为负数只会拉低总和,这就是贪心贪的地方! 局部最优:当前“连续和”为负数的时候立刻放弃,从下一个元素重新计算“连续和”,因为负数加上下一个元素 “连续和”只会越来越小。 @@ -56,29 +59,27 @@ public: **局部最优的情况下,并记录最大的“连续和”,可以推出全局最优**。 - -从代码角度上来讲:遍历nums,从头开始用count累积,如果count一旦加上nums[i]变为负数,那么就应该从nums[i+1]开始从0累积count了,因为已经变为负数的count,只会拖累总和。 +从代码角度上来讲:遍历 nums,从头开始用 count 累积,如果 count 一旦加上 nums[i]变为负数,那么就应该从 nums[i+1]开始从 0 累积 count 了,因为已经变为负数的 count,只会拖累总和。 **这相当于是暴力解法中的不断调整最大子序和区间的起始位置**。 - **那有同学问了,区间终止位置不用调整么? 如何才能得到最大“连续和”呢?** -区间的终止位置,其实就是如果count取到最大值了,及时记录下来了。例如如下代码: +区间的终止位置,其实就是如果 count 取到最大值了,及时记录下来了。例如如下代码: ``` if (count > result) result = count; ``` -**这样相当于是用result记录最大子序和区间和(变相的算是调整了终止位置)**。 +**这样相当于是用 result 记录最大子序和区间和(变相的算是调整了终止位置)**。 如动画所示: ![53.最大子序和](https://code-thinking.cdn.bcebos.com/gifs/53.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.gif) -红色的起始位置就是贪心每次取count为正数的时候,开始一个区间的统计。 +红色的起始位置就是贪心每次取 count 为正数的时候,开始一个区间的统计。 -那么不难写出如下C++代码(关键地方已经注释) +那么不难写出如下 C++代码(关键地方已经注释) ```CPP class Solution { @@ -98,38 +99,34 @@ public: }; ``` -* 时间复杂度:O(n) -* 空间复杂度:O(1) +- 时间复杂度:O(n) +- 空间复杂度:O(1) 当然题目没有说如果数组为空,应该返回什么,所以数组为空的话返回啥都可以了。 +## 常见误区 -## 常见误区 - -误区一: - -不少同学认为 如果输入用例都是-1,或者 都是负数,这个贪心算法跑出来的结果是0, 这是**又一次证明脑洞模拟不靠谱的经典案例**,建议大家把代码运行一下试一试,就知道了,也会理解 为什么 result 要初始化为最小负数了。 +误区一: +不少同学认为 如果输入用例都是-1,或者 都是负数,这个贪心算法跑出来的结果是 0, 这是**又一次证明脑洞模拟不靠谱的经典案例**,建议大家把代码运行一下试一试,就知道了,也会理解 为什么 result 要初始化为最小负数了。 误区二: -大家在使用贪心算法求解本题,经常陷入的误区,就是分不清,是遇到 负数就选择起始位置,还是连续和为负选择起始位置。 +大家在使用贪心算法求解本题,经常陷入的误区,就是分不清,是遇到 负数就选择起始位置,还是连续和为负选择起始位置。 -在动画演示用,大家可以发现, 4,遇到 -1 的时候,我们依然累加了,为什么呢? +在动画演示用,大家可以发现, 4,遇到 -1 的时候,我们依然累加了,为什么呢? -因为和为3,只要连续和还是正数就会 对后面的元素 起到增大总和的作用。 所以只要连续和为正数我们就保留。 - -这里也会有录友疑惑,那 4 + -1 之后 不就变小了吗? 会不会错过 4 成为最大连续和的可能性? - -其实并不会,因为还有一个变量result 一直在更新 最大的连续和,只要有更大的连续和出现,result就更新了,那么result已经把4更新了,后面 连续和变成3,也不会对最后结果有影响。 +因为和为 3,只要连续和还是正数就会 对后面的元素 起到增大总和的作用。 所以只要连续和为正数我们就保留。 +这里也会有录友疑惑,那 4 + -1 之后 不就变小了吗? 会不会错过 4 成为最大连续和的可能性? +其实并不会,因为还有一个变量 result 一直在更新 最大的连续和,只要有更大的连续和出现,result 就更新了,那么 result 已经把 4 更新了,后面 连续和变成 3,也不会对最后结果有影响。 ## 动态规划 -当然本题还可以用动态规划来做,当前[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png)主要讲解贪心系列,后续到动态规划系列的时候会详细讲解本题的dp方法。 +当然本题还可以用动态规划来做,当前[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png)主要讲解贪心系列,后续到动态规划系列的时候会详细讲解本题的 dp 方法。 -那么先给出我的dp代码如下,有时间的录友可以提前做一做: +那么先给出我的 dp 代码如下,有时间的录友可以提前做一做: ```CPP class Solution { @@ -148,8 +145,8 @@ public: }; ``` -* 时间复杂度:O(n) -* 空间复杂度:O(n) +- 时间复杂度:O(n) +- 空间复杂度:O(n) ## 总结 @@ -159,8 +156,8 @@ public: ## 其他语言版本 - ### Java + ```java class Solution { public int maxSubArray(int[] nums) { @@ -201,6 +198,7 @@ class Solution { ``` ### Python + ```python class Solution: def maxSubArray(self, nums: List[int]) -> int: @@ -233,6 +231,7 @@ func maxSubArray(nums []int) int { ``` ### Rust + ```rust pub fn max_sub_array(nums: Vec) -> i32 { let mut max_sum = i32::MIN; @@ -247,6 +246,7 @@ pub fn max_sub_array(nums: Vec) -> i32 { ``` ### Javascript: + ```Javascript var maxSubArray = function(nums) { let result = -Infinity @@ -264,14 +264,15 @@ var maxSubArray = function(nums) { }; ``` - ### C: + 贪心: + ```c int maxSubArray(int* nums, int numsSize){ int maxVal = INT_MIN; int subArrSum = 0; - + int i; for(i = 0; i < numsSize; ++i) { subArrSum += nums[i]; @@ -286,6 +287,7 @@ int maxSubArray(int* nums, int numsSize){ ``` 动态规划: + ```c /** * 解题思路:动态规划: @@ -324,15 +326,15 @@ int maxSubArray(int* nums, int numsSize){ ```typescript function maxSubArray(nums: number[]): number { - let curSum: number = 0; - let resMax: number = -Infinity; - for (let i = 0, length = nums.length; i < length; i++) { - curSum += nums[i]; - resMax = Math.max(curSum, resMax); - if (curSum < 0) curSum = 0; - } - return resMax; -}; + let curSum: number = 0; + let resMax: number = -Infinity; + for (let i = 0, length = nums.length; i < length; i++) { + curSum += nums[i]; + resMax = Math.max(curSum, resMax); + if (curSum < 0) curSum = 0; + } + return resMax; +} ``` **动态规划** @@ -340,17 +342,17 @@ function maxSubArray(nums: number[]): number { ```typescript // 动态规划 function maxSubArray(nums: number[]): number { - const length = nums.length; - if (length === 0) return 0; - const dp: number[] = []; - dp[0] = nums[0]; - let resMax: number = nums[0]; - for (let i = 1; i < length; i++) { - dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); - resMax = Math.max(resMax, dp[i]); - } - return resMax; -}; + const length = nums.length; + if (length === 0) return 0; + const dp: number[] = []; + dp[0] = nums[0]; + let resMax: number = nums[0]; + for (let i = 1; i < length; i++) { + dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); + resMax = Math.max(resMax, dp[i]); + } + return resMax; +} ``` ### Scala From ef5a6f8567a5924308fb4fe081665f8f5577c5fb Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:17:32 +0800 Subject: [PATCH 0056/1533] =?UTF-8?q?docs:=20=E8=82=A1=E7=A5=A8=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA=E4=BA=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E9=93=BE=E6=8E=A5=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272II.md" | 131 ++++++++++-------- 1 file changed, 76 insertions(+), 55 deletions(-) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index 29242be3db..533a14a4d3 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -4,43 +4,49 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -# 122.买卖股票的最佳时机II +# 122.买卖股票的最佳时机 II [力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/) -给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 +给定一个数组,它的第  i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 - 示例 1: -* 输入: [7,1,5,3,6,4] -* 输出: 7 -* 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。 + +- 输入: [7,1,5,3,6,4] +- 输出: 7 +- 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。 示例 2: -* 输入: [1,2,3,4,5] -* 输出: 4 -* 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 -示例 3: -* 输入: [7,6,4,3,1] -* 输出: 0 -* 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 +- 输入: [1,2,3,4,5] +- 输出: 4 +- 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 + +示例  3: + +- 输入: [7,6,4,3,1] +- 输出: 0 +- 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 提示: -* 1 <= prices.length <= 3 * 10 ^ 4 -* 0 <= prices[i] <= 10 ^ 4 + +- 1 <= prices.length <= 3 \* 10 ^ 4 +- 0 <= prices[i] <= 10 ^ 4 + +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 本题首先要清楚两点: -* 只有一只股票! -* 当前只有买股票或者卖股票的操作 +- 只有一只股票! +- 当前只有买股票或者卖股票的操作 想获得利润至少要两天为一个交易单元。 @@ -52,17 +58,16 @@ 如何分解呢? -假如第0天买入,第3天卖出,那么利润为:prices[3] - prices[0]。 +假如第 0 天买入,第 3 天卖出,那么利润为:prices[3] - prices[0]。 相当于(prices[3] - prices[2]) + (prices[2] - prices[1]) + (prices[1] - prices[0])。 -**此时就是把利润分解为每天为单位的维度,而不是从0天到第3天整体去考虑!** +**此时就是把利润分解为每天为单位的维度,而不是从 0 天到第 3 天整体去考虑!** -那么根据prices可以得到每天的利润序列:(prices[i] - prices[i - 1]).....(prices[1] - prices[0])。 +那么根据 prices 可以得到每天的利润序列:(prices[i] - prices[i - 1]).....(prices[1] - prices[0])。 如图: - ![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858-20230310134659477.png) 一些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。 @@ -77,7 +82,7 @@ 局部最优可以推出全局最优,找不出反例,试一试贪心! -对应C++代码如下: +对应 C++代码如下: ```CPP class Solution { @@ -92,12 +97,12 @@ public: }; ``` -* 时间复杂度:O(n) -* 空间复杂度:O(1) +- 时间复杂度:O(n) +- 空间复杂度:O(1) ### 动态规划 -动态规划将在下一个系列详细讲解,本题解先给出我的C++代码(带详细注释),感兴趣的同学可以自己先学习一下。 +动态规划将在下一个系列详细讲解,本题解先给出我的 C++代码(带详细注释),感兴趣的同学可以自己先学习一下。 ```CPP class Solution { @@ -119,8 +124,8 @@ public: }; ``` -* 时间复杂度:$O(n)$ -* 空间复杂度:$O(n)$ +- 时间复杂度:$O(n)$ +- 空间复杂度:$O(n)$ ## 总结 @@ -134,9 +139,10 @@ public: ## 其他语言版本 -### Java: +### Java: 贪心: + ```java // 贪心思路 class Solution { @@ -151,6 +157,7 @@ class Solution { ``` 动态规划: + ```java class Solution { // 动态规划 public int maxProfit(int[] prices) { @@ -172,8 +179,10 @@ class Solution { // 动态规划 } ``` -### Python: +### Python: + 贪心: + ```python class Solution: def maxProfit(self, prices: List[int]) -> int: @@ -184,6 +193,7 @@ class Solution: ``` 动态规划: + ```python class Solution: def maxProfit(self, prices: List[int]) -> int: @@ -200,6 +210,7 @@ class Solution: ### Go: 贪心算法 + ```go func maxProfit(prices []int) int { var sum int @@ -212,7 +223,9 @@ func maxProfit(prices []int) int { return sum } ``` + 动态规划 + ```go func maxProfit(prices []int) int { dp := make([][]int, len(prices)) @@ -226,7 +239,7 @@ func maxProfit(prices []int) int { dp[i][1] = max(dp[i-1][0] - prices[i], dp[i-1][1]) } return dp[len(prices)-1][0] - + } func max(a, b int) int { if a > b { @@ -239,6 +252,7 @@ func max(a, b int) int { ### Javascript: 贪心 + ```Javascript var maxProfit = function(prices) { let result = 0 @@ -249,27 +263,28 @@ var maxProfit = function(prices) { }; ``` -动态规划 +动态规划 + ```javascript const maxProfit = (prices) => { - let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); - // dp[i][0] 表示第i天持有股票所得现金。 - // dp[i][1] 表示第i天不持有股票所得最多现金 - dp[0][0] = 0 - prices[0]; - dp[0][1] = 0; - for(let i = 1; i < prices.length; i++) { - // 如果第i天持有股票即dp[i][0], 那么可以由两个状态推出来 - // 第i-1天就持有股票,那么就保持现状,所得现金就是昨天持有股票的所得现金 即:dp[i - 1][0] - // 第i天买入股票,所得现金就是昨天不持有股票的所得现金减去 今天的股票价格 即:dp[i - 1][1] - prices[i] - dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] - prices[i]); - - // 在来看看如果第i天不持有股票即dp[i][1]的情况, 依然可以由两个状态推出来 - // 第i-1天就不持有股票,那么就保持现状,所得现金就是昨天不持有股票的所得现金 即:dp[i - 1][1] - // 第i天卖出股票,所得现金就是按照今天股票佳价格卖出后所得现金即:prices[i] + dp[i - 1][0] - dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]); - } + let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); + // dp[i][0] 表示第i天持有股票所得现金。 + // dp[i][1] 表示第i天不持有股票所得最多现金 + dp[0][0] = 0 - prices[0]; + dp[0][1] = 0; + for (let i = 1; i < prices.length; i++) { + // 如果第i天持有股票即dp[i][0], 那么可以由两个状态推出来 + // 第i-1天就持有股票,那么就保持现状,所得现金就是昨天持有股票的所得现金 即:dp[i - 1][0] + // 第i天买入股票,所得现金就是昨天不持有股票的所得现金减去 今天的股票价格 即:dp[i - 1][1] - prices[i] + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + + // 在来看看如果第i天不持有股票即dp[i][1]的情况, 依然可以由两个状态推出来 + // 第i-1天就不持有股票,那么就保持现状,所得现金就是昨天不持有股票的所得现金 即:dp[i - 1][1] + // 第i天卖出股票,所得现金就是按照今天股票佳价格卖出后所得现金即:prices[i] + dp[i - 1][0] + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]); + } - return dp[prices.length -1][1]; + return dp[prices.length - 1][1]; }; ``` @@ -277,17 +292,18 @@ const maxProfit = (prices) => { ```typescript function maxProfit(prices: number[]): number { - let resProfit: number = 0; - for (let i = 1, length = prices.length; i < length; i++) { - resProfit += Math.max(prices[i] - prices[i - 1], 0); - } - return resProfit; -}; + let resProfit: number = 0; + for (let i = 1, length = prices.length; i < length; i++) { + resProfit += Math.max(prices[i] - prices[i - 1], 0); + } + return resProfit; +} ``` ### Rust 贪心: + ```Rust impl Solution { fn max(a: i32, b: i32) -> i32 { @@ -304,6 +320,7 @@ impl Solution { ``` 动态规划: + ```Rust impl Solution { fn max(a: i32, b: i32) -> i32 { @@ -323,7 +340,9 @@ impl Solution { ``` ### C: + 贪心: + ```c int maxProfit(int* prices, int pricesSize){ int result = 0; @@ -339,6 +358,7 @@ int maxProfit(int* prices, int pricesSize){ ``` 动态规划: + ```c #define max(a, b) (((a) > (b)) ? (a) : (b)) @@ -363,6 +383,7 @@ int maxProfit(int* prices, int pricesSize){ ### Scala 贪心: + ```scala object Solution { def maxProfit(prices: Array[Int]): Int = { From 29646b86b90026072dfa903877e16123164d60fa Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:19:09 +0800 Subject: [PATCH 0057/1533] =?UTF-8?q?docs:=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...63\350\267\203\346\270\270\346\210\217.md" | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index a898263de9..6c75b04cf2 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -4,7 +4,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- # 55. 跳跃游戏 [力扣题目链接](https://leetcode.cn/problems/jump-game/) @@ -15,20 +14,25 @@ 判断你是否能够到达最后一个位置。 -示例 1: -* 输入: [2,3,1,1,4] -* 输出: true -* 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置。 +示例  1: + +- 输入: [2,3,1,1,4] +- 输出: true +- 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置。 + +示例  2: -示例 2: -* 输入: [3,2,1,0,4] -* 输出: false -* 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 +- 输入: [3,2,1,0,4] +- 输出: false +- 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 -刚看到本题一开始可能想:当前位置元素如果是3,我究竟是跳一步呢,还是两步呢,还是三步呢,究竟跳几步才是最优呢? +刚看到本题一开始可能想:当前位置元素如果是 3,我究竟是跳一步呢,还是两步呢,还是三步呢,究竟跳几步才是最优呢? 其实跳几步无所谓,关键在于可跳的覆盖范围! @@ -46,14 +50,13 @@ 如图: - ![55.跳跃游戏](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124154758229-20230310135019977.png) -i每次移动只能在cover的范围内移动,每移动一个元素,cover得到该元素数值(新的覆盖范围)的补充,让i继续移动下去。 +i 每次移动只能在 cover 的范围内移动,每移动一个元素,cover 得到该元素数值(新的覆盖范围)的补充,让 i 继续移动下去。 -而cover每次只取 max(该元素数值补充后的范围, cover本身范围)。 +而 cover 每次只取 max(该元素数值补充后的范围, cover 本身范围)。 -如果cover大于等于了终点下标,直接return true就可以了。 +如果 cover 大于等于了终点下标,直接 return true 就可以了。 C++代码如下: @@ -71,6 +74,7 @@ public: } }; ``` + ## 总结 这道题目关键点在于:不用拘泥于每次究竟跳几步,而是看覆盖范围,覆盖范围内一定是可以跳过来的,不用管是怎么跳的。 @@ -83,8 +87,8 @@ public: ## 其他语言版本 +### Java -### Java ```Java class Solution { public boolean canJump(int[] nums) { @@ -106,6 +110,7 @@ class Solution { ``` ### Python + ```python class Solution: def canJump(self, nums: List[int]) -> bool: @@ -156,9 +161,7 @@ func max(a, b int ) int { } ``` - - -### Javascript +### Javascript ```Javascript var canJump = function(nums) { @@ -196,6 +199,7 @@ impl Solution { ``` ### C + ```c #define max(a, b) (((a) > (b)) ? (a) : (b)) @@ -217,23 +221,23 @@ bool canJump(int* nums, int numsSize){ } ``` - ### TypeScript ```typescript function canJump(nums: number[]): boolean { - let farthestIndex: number = 0; - let cur: number = 0; - while (cur <= farthestIndex) { - farthestIndex = Math.max(farthestIndex, cur + nums[cur]); - if (farthestIndex >= nums.length - 1) return true; - cur++; - } - return false; -}; + let farthestIndex: number = 0; + let cur: number = 0; + while (cur <= farthestIndex) { + farthestIndex = Math.max(farthestIndex, cur + nums[cur]); + if (farthestIndex >= nums.length - 1) return true; + cur++; + } + return false; +} ``` ### Scala + ```scala object Solution { def canJump(nums: Array[Int]): Boolean = { @@ -250,7 +254,6 @@ object Solution { } ``` -

From 6f8d5eb7a04e178b99d23b4b4c443bbdfd84840c Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:21:12 +0800 Subject: [PATCH 0058/1533] =?UTF-8?q?docs:=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E4=BA=8C=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\203\346\270\270\346\210\217II.md" | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 22151fdc9a..eb622cd3d4 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -4,10 +4,9 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心里准备! -> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心里准备! - -# 45.跳跃游戏II +# 45.跳跃游戏 II [力扣题目链接](https://leetcode.cn/problems/jump-game-ii/) @@ -18,13 +17,17 @@ 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 示例: -* 输入: [2,3,1,1,4] -* 输出: 2 -* 解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。 + +- 输入: [2,3,1,1,4] +- 输出: 2 +- 解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳  1  步,然后跳  3  步到达数组的最后一个位置。 说明: 假设你总是可以到达数组的最后一个位置。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -46,7 +49,6 @@ 如图: - ![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103.png) **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** @@ -57,8 +59,8 @@ 这里还是有个特殊情况需要考虑,当移动下标达到了当前覆盖的最远距离下标时 -* 如果当前覆盖最远距离下标不是是集合终点,步数就加一,还需要继续走。 -* 如果当前覆盖最远距离下标就是是集合终点,步数不用加一,因为不能再往后走了。 +- 如果当前覆盖最远距离下标不是是集合终点,步数就加一,还需要继续走。 +- 如果当前覆盖最远距离下标就是是集合终点,步数不用加一,因为不能再往后走了。 C++代码如下:(详细注释) @@ -92,14 +94,14 @@ public: **针对于方法一的特殊情况,可以统一处理**,即:移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不考虑是不是终点的情况。 -想要达到这样的效果,只要让移动下标,最大只能移动到nums.size - 2的地方就可以了。 +想要达到这样的效果,只要让移动下标,最大只能移动到 nums.size - 2 的地方就可以了。 -因为当移动下标指向nums.size - 2时: +因为当移动下标指向 nums.size - 2 时: -* 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图: -![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png) +- 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即 ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图: + ![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png) -* 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图: +- 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图: ![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693.png) @@ -127,7 +129,7 @@ public: 可以看出版本二的代码相对于版本一简化了不少! -**其精髓在于控制移动下标i只移动到nums.size() - 2的位置**,所以移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不用考虑别的了。 +**其精髓在于控制移动下标 i 只移动到 nums.size() - 2 的位置**,所以移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不用考虑别的了。 ## 总结 @@ -137,11 +139,10 @@ public: 理解本题的关键在于:**以最小的步数增加最大的覆盖范围,直到覆盖范围覆盖了终点**,这个范围内最小步数一定可以跳到,不用管具体是怎么跳的,不纠结于一步究竟跳一个单位还是两个单位。 - ## 其他语言版本 +### Java -### Java ```Java // 版本一 class Solution { @@ -207,7 +208,7 @@ class Solution: nextDistance = 0 for i in range(len(nums)): nextDistance = max(i + nums[i], nextDistance) - if i == curDistance: + if i == curDistance: if curDistance != len(nums) - 1: ans += 1 curDistance = nextDistance @@ -230,9 +231,10 @@ class Solution: step += 1 return step ``` + ```python # 动态规划做法 -class Solution: +class Solution: def jump(self, nums: List[int]) -> int: result = [10**4+1]*len(nums) result[0]=0 @@ -244,7 +246,6 @@ class Solution: ``` - ### Go ```go @@ -331,21 +332,21 @@ var jump = function(nums) { ```typescript function jump(nums: number[]): number { - const length: number = nums.length; - let curFarthestIndex: number = 0, - nextFarthestIndex: number = 0; - let curIndex: number = 0; - let stepNum: number = 0; - while (curIndex < length - 1) { - nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]); - if (curIndex === curFarthestIndex) { - curFarthestIndex = nextFarthestIndex; - stepNum++; - } - curIndex++; + const length: number = nums.length; + let curFarthestIndex: number = 0, + nextFarthestIndex: number = 0; + let curIndex: number = 0; + let stepNum: number = 0; + while (curIndex < length - 1) { + nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]); + if (curIndex === curFarthestIndex) { + curFarthestIndex = nextFarthestIndex; + stepNum++; } - return stepNum; -}; + curIndex++; + } + return stepNum; +} ``` ### Scala @@ -425,7 +426,6 @@ impl Solution { } ``` -

From a8d3d3cf75ff188b192993486059c9048f31d0e4 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:22:58 +0800 Subject: [PATCH 0059/1533] =?UTF-8?q?docs:k=E6=AC=A1=E5=8F=96=E5=8F=8D?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\226\347\232\204\346\225\260\347\273\204\345\222\214.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index cdf42511b1..fb2a1cb701 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -34,6 +34,10 @@ * 1 <= K <= 10000 * -100 <= A[i] <= 100 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 本题思路其实比较好想了,如何可以让数组和最大呢? From 8919bcc03a39ad6366d04a3315713300c00dff53 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:24:02 +0800 Subject: [PATCH 0060/1533] =?UTF-8?q?docs:=E5=8A=A0=E6=B2=B9=E7=AB=99?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0134.\345\212\240\346\262\271\347\253\231.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index 77e199fd35..f432bf0b1f 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -45,6 +45,10 @@ * 解释: 你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 暴力方法 From 74b4c506aa7a922f173ebe30daef96e17a97153c Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:26:03 +0800 Subject: [PATCH 0061/1533] =?UTF-8?q?docs:=E5=88=86=E5=8F=91=E7=B3=96?= =?UTF-8?q?=E6=9E=9C=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index 181f24e98e..ef79868a1a 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -28,6 +28,10 @@ * 输出: 4 * 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 From 6f1111447cca7dafb2261836ff1735de819a4f9a Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:27:20 +0800 Subject: [PATCH 0062/1533] =?UTF-8?q?docs:=E6=9F=A0=E6=AA=AC=E6=B0=B4?= =?UTF-8?q?=E6=89=BE=E9=9B=B6=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...37\240\346\252\254\346\260\264\346\211\276\351\233\266.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index fc336eba82..cd52adb099 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -50,6 +50,10 @@ * 0 <= bills.length <= 10000 * bills[i] 不是 5 就是 10 或是 20  +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 这是前几天的leetcode每日一题,感觉不错,给大家讲一下。 From 5cb8eb074673c8e2f977adf83787145a84cee079 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:28:54 +0800 Subject: [PATCH 0063/1533] =?UTF-8?q?docs:=E8=BA=AB=E9=AB=98=E9=87=8D?= =?UTF-8?q?=E5=BB=BA=E9=98=9F=E5=88=97=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\230\351\207\215\345\273\272\351\230\237\345\210\227.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 48c498e1ad..3f82d0bc20 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -37,6 +37,10 @@ 题目数据确保队列可以被重建 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 本题有两个维度,h和k,看到这种题目一定要想如何确定一个维度,然后再按照另一个维度重新排列。 From 3f904211ca06133b2493ac5594187e1f925112fb Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:30:37 +0800 Subject: [PATCH 0064/1533] =?UTF-8?q?docs:=20=E6=9C=80=E5=B0=8F=E7=9A=84?= =?UTF-8?q?=E7=AE=AD=E5=BC=95=E7=88=86=E6=B0=94=E7=90=83=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...56\255\345\274\225\347\210\206\346\260\224\347\220\203.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 6b018f978a..53eee25c77 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -42,6 +42,10 @@ * points[i].length == 2 * -2^31 <= xstart < xend <= 2^31 - 1 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 如何使用最少的弓箭呢? From 403f524b3fb2f969074a10fe3516cefd563dec3b Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:31:35 +0800 Subject: [PATCH 0065/1533] =?UTF-8?q?docs:=20=E6=97=A0=E9=87=8D=E5=8F=A0?= =?UTF-8?q?=E5=8C=BA=E9=97=B4=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\240\351\207\215\345\217\240\345\214\272\351\227\264.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index e5675e04aa..02db203414 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -30,6 +30,10 @@ * 输出: 0 * 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,依然是判断重叠区间 | LeetCode:435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 **相信很多同学看到这道题目都冥冥之中感觉要排序,但是究竟是按照右边界排序,还是按照左边界排序呢?** From b0692bfa0d0a368e0d3acf2f6f6541a8534b2d65 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:32:37 +0800 Subject: [PATCH 0066/1533] =?UTF-8?q?docs:=20=E5=88=92=E5=88=86=E5=AD=97?= =?UTF-8?q?=E6=AF=8D=E5=8C=BA=E9=97=B4=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\206\345\255\227\346\257\215\345\214\272\351\227\264.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 077cb54ac4..ad680ef92d 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -24,6 +24,10 @@ * S的长度在[1, 500]之间。 * S只包含小写字母 'a' 到 'z' 。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 一想到分割字符串就想到了回溯,但本题其实不用回溯去暴力搜索。 From f3b8bfaf77975d7b396d464c606a25c120e3ede9 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:33:41 +0800 Subject: [PATCH 0067/1533] =?UTF-8?q?docs:=20=E5=90=88=E5=B9=B6=E5=8C=BA?= =?UTF-8?q?=E9=97=B4=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index d467ab1a29..17d2cc0a1b 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -22,6 +22,9 @@ * 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。 * 注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 1d2d187c3a93b390742b533302bda036c5df27c7 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:35:28 +0800 Subject: [PATCH 0068/1533] =?UTF-8?q?docs:=20=E5=8D=95=E8=B0=83=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97=E6=B7=BB=E5=8A=A0=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\222\345\242\236\347\232\204\346\225\260\345\255\227.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index fda378aa46..f92652cd7f 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -26,6 +26,10 @@ 说明: N 是在 [0, 10^9] 范围内的一个整数。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 暴力解法 From 7ad80ab57f282ac94eef6aa42cecb98774601b46 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:37:27 +0800 Subject: [PATCH 0069/1533] =?UTF-8?q?docs:=20=E7=9B=91=E6=8E=A7=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E6=B7=BB=E5=8A=A0=E8=A7=86=E9=A2=91=E9=93=BE?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\221\346\216\247\344\272\214\345\217\211\346\240\221.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index 14563a51fb..267e7f6ede 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -38,6 +38,10 @@ * 给定树的节点数的范围是 [1, 1000]。 * 每个节点的值都是 0。 +# 视频讲解 + +**《代码随想录》算法视频公开课:[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 From 33f6bc9a36aeca424dd7a5c1cb5ec5f186679ee9 Mon Sep 17 00:00:00 2001 From: liangzhensheng <1250564179@qq.com> Date: Fri, 17 Mar 2023 11:58:35 +0800 Subject: [PATCH 0070/1533] =?UTF-8?q?docs:=2001=E8=83=8C=E5=8C=85=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=AD=97=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index ff0b6aba28..c45fc3d302 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -87,7 +87,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 那么可以有两个方向推出来dp[i][j], -* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以被背包内的价值依然和前面相同。) +* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以背包内的价值依然和前面相同。) * **放物品i**:由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]的时候不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 所以递归公式: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); From 29e1a07cc3a51cb61c5aec933d5d914eb105741d Mon Sep 17 00:00:00 2001 From: sharky <1821984081@qq.com> Date: Fri, 17 Mar 2023 12:22:40 +0800 Subject: [PATCH 0071/1533] =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6=E4=B8=80?= =?UTF-8?q?=E7=A7=8D=E5=86=99=E6=B3=95=EF=BC=8C40.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CII=E7=9A=84java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\347\247\215\345\206\231\346\263\225.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index b3bd74da28..cbbd831f8f 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -317,7 +317,38 @@ class Solution { } } ``` +**40.组合总和II** +```java +class Solution { + List> result = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort( candidates ); + if( candidates[0] > target ) return result; + backtracking(candidates,target,0,0); + return result; + } + public void backtracking(int[] candidates,int target,int sum,int startIndex){ + if( sum > target )return; + if( sum == target ){ + result.add( new ArrayList<>(path) ); + } + HashSet hashSet = new HashSet<>(); + for( int i = startIndex; i < candidates.length; i++){ + if( hashSet.contains(candidates[i]) ){ + continue; + } + hashSet.add(candidates[i]); + path.add(candidates[i]); + sum += candidates[i]; + backtracking(candidates,target,sum,i+1); + path.removeLast(); + sum -= candidates[i]; + } + } +} +``` Python: **90.子集II** From 39b1b10a8aa80b3a8fb0bc6363a7e761de4211db Mon Sep 17 00:00:00 2001 From: zsq <1377821797@qq.com> Date: Sat, 18 Mar 2023 22:16:17 +0800 Subject: [PATCH 0072/1533] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改README语法错误 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a9b2f7efe9..43f4df15bd 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ **这里每一篇题解,都是精品,值得仔细琢磨**。 -我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)的贡献的代码,当然我也会严格把控代码质量。 +我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。 **所以也欢迎大家参与进来,完善题解的各个语言版本,拥抱开源,让更多小伙伴们受益**。 From 8a7432ef2174ce8864f3e79816df2399815cd28d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 18 Mar 2023 23:49:44 +0800 Subject: [PATCH 0073/1533] =?UTF-8?q?update=200349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86:=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\260\347\273\204\347\232\204\344\272\244\351\233\206.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 905bf4b268..0da0e30c37 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -72,6 +72,9 @@ public: }; ``` +* 时间复杂度: O(mn) +* 空间复杂度: O(n) + ## 拓展 那有同学可能问了,遇到哈希问题我直接都用set不就得了,用什么数组啊。 @@ -110,6 +113,8 @@ public: }; ``` +* 时间复杂度: O(m + n) +* 空间复杂度: O(n) ## 其他语言版本 From 46a34392d5308b9606436d6722bd43053da4da73 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 00:02:30 +0800 Subject: [PATCH 0074/1533] =?UTF-8?q?update=200202.=E5=BF=AB=E4=B9=90?= =?UTF-8?q?=E6=95=B0=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0202.\345\277\253\344\271\220\346\225\260.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 2687574fdc..5ac29e8639 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -75,6 +75,8 @@ public: }; ``` +* 时间复杂度: O(logn) +* 空间复杂度: O(logn) From 3b79659bc0c3303ccf48219ff9398184b622b88c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 00:04:13 +0800 Subject: [PATCH 0075/1533] =?UTF-8?q?update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index a0acdcbe0e..b3abb991ca 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -109,6 +109,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 总结 本题其实有四个重点: From 79dfddde054abed5c766aa70ed56f54ee3b1c3ef Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Sat, 18 Mar 2023 15:43:20 -0400 Subject: [PATCH 0076/1533] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加112-python-简洁版解法 --- ...112.\350\267\257\345\276\204\346\200\273\345\222\214.md" | 6 ++++++ 1 file changed, 6 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index e412d38e4e..bb46f04add 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -475,6 +475,12 @@ class solution: return false # 别忘记处理空treenode else: return isornot(root, targetsum - root.val) + +class Solution: # 简洁版 + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: + if not root: return False + if root.left==root.right==None and root.val == targetSum: return True + return self.hasPathSum(root.left,targetSum-root.val) or self.hasPathSum(root.right,targetSum-root.val) ``` **迭代 - 层序遍历** From 87dbf5efc5e12ea3a305be097c2967ac3792e403 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Sat, 18 Mar 2023 16:34:16 -0400 Subject: [PATCH 0077/1533] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加:0113 - python - 迭代法 - 前序遍历解法 --- ...57\345\276\204\346\200\273\345\222\214.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index e412d38e4e..29e5a547d9 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -560,6 +560,26 @@ class Solution: return result ``` +**迭代法,前序遍历** + +```python +class Solution: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: + if not root: return [] + stack, path_stack,result = [[root,root.val]],[[root.val]],[] + while stack: + cur,cursum = stack.pop() + path = path_stack.pop() + if cur.left==cur.right==None: + if cursum==targetSum: result.append(path) + if cur.right: + stack.append([cur.right,cursum+cur.right.val]) + path_stack.append(path+[cur.right.val]) + if cur.left: + stack.append([cur.left,cursum+cur.left.val]) + path_stack.append(path+[cur.left.val]) + return result +``` ## go ### 112. 路径总和 From 08a984d61c5b855b5052d8617dc23cf170d61c01 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:13:34 +0800 Subject: [PATCH 0078/1533] =?UTF-8?q?update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 07c3f71194..c2a5710fa2 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -83,6 +83,9 @@ public: ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n^2),最坏情况下A和B的值各不相同,相加产生的数字个数为 n^2 + From 94f79ada2d4c234a7b8a67cddda169ef3c591ccf Mon Sep 17 00:00:00 2001 From: coffeelize <46947638+coffeelize@users.noreply.github.com> Date: Sun, 19 Mar 2023 11:13:54 +0800 Subject: [PATCH 0079/1533] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 还有 --> 还要 --- .../0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index a0acdcbe0e..a8f8bf173a 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -41,7 +41,7 @@ 那么我们就应该想到使用哈希法了。 -因为本地,我们不仅要知道元素有没有遍历过,还有知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。 +因为本地,我们不仅要知道元素有没有遍历过,还要知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。 再来看一下使用数组和set来做哈希法的局限。 From c91ee8fdc2b7ff02953a758b0b9c7da4c031d7d0 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:16:15 +0800 Subject: [PATCH 0080/1533] =?UTF-8?q?update=200383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0383.\350\265\216\351\207\221\344\277\241.md" | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index 4e6ba03369..d9a184b643 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -39,8 +39,6 @@ canConstruct("aa", "aab") -> true 那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下: ```CPP -// 时间复杂度: O(n^2) -// 空间复杂度:O(1) class Solution { public: bool canConstruct(string ransomNote, string magazine) { @@ -62,6 +60,9 @@ public: }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(1) + 这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。 @@ -78,8 +79,6 @@ public: 代码如下: ```CPP -// 时间复杂度: O(n) -// 空间复杂度:O(1) class Solution { public: bool canConstruct(string ransomNote, string magazine) { @@ -105,6 +104,10 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + + ## 其他语言版本 From 015935e4fa757f2fee1551e26336aa0fd520d844 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:24:04 +0800 Subject: [PATCH 0081/1533] =?UTF-8?q?update=200015.=E4=B8=89=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5.\344\270\211\346\225\260\344\271\213\345\222\214.md" | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 6675e4088a..26c9eaa284 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -83,6 +83,10 @@ public: }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n),额外的 set 开销 + + ## 双指针 **其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。 @@ -158,6 +162,10 @@ public: }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(1) + + ## 去重逻辑的思考 ### a的去重 From cd537cb91aa0c2b879a9b3b8cf0d2f5ab0552495 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:26:02 +0800 Subject: [PATCH 0082/1533] =?UTF-8?q?update=200018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 8e87fcbd2f..5f4c2ec920 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -121,6 +121,10 @@ public: ``` +* 时间复杂度: O(n^3) +* 空间复杂度: O(1) + + ## 补充 二级剪枝的部分: From 633cb549fb21c4f45a366a973dcc30768a97a03f Mon Sep 17 00:00:00 2001 From: Javie Deng Date: Mon, 20 Mar 2023 12:36:06 +0800 Subject: [PATCH 0083/1533] =?UTF-8?q?=E4=B8=BA0226.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E6=B7=BB=E5=8A=A0=E4=BA=86C#?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E7=89=88=EF=BC=88=E5=8C=85=E5=90=AB=E9=80=92?= =?UTF-8?q?=E5=BD=92=E5=92=8C=E8=BF=AD=E4=BB=A3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\344\272\214\345\217\211\346\240\221.md" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 16a5be577d..611e971aac 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -914,6 +914,53 @@ impl Solution { } ``` +### C# + +```csharp +//递归 +public class Solution { + public TreeNode InvertTree(TreeNode root) { + if (root == null) return root; + + swap(root); + InvertTree(root.left); + InvertTree(root.right); + return root; + } + + public void swap(TreeNode node) { + TreeNode temp = node.left; + node.left = node.right; + node.right = temp; + } +} +``` + +```csharp +//迭代 +public class Solution { + public TreeNode InvertTree(TreeNode root) { + if (root == null) return null; + Stack stack=new Stack(); + stack.Push(root); + while(stack.Count>0) + { + TreeNode node = stack.Pop(); + swap(node); + if(node.right!=null) stack.Push(node.right); + if(node.left!=null) stack.Push(node.left); + } + return root; + } + + public void swap(TreeNode node) { + TreeNode temp = node.left; + node.left = node.right; + node.right = temp; + } +} +``` +

From da2acd7dcf6c0a3fde44bb3801395eb7050f19f3 Mon Sep 17 00:00:00 2001 From: Javie Deng Date: Mon, 20 Mar 2023 13:05:00 +0800 Subject: [PATCH 0084/1533] =?UTF-8?q?"=E4=B8=BA0226.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E6=B7=BB=E5=8A=A0=E4=BA=86C#?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E7=89=88=EF=BC=88=E5=8C=85=E5=90=AB=E9=80=92?= =?UTF-8?q?=E5=BD=92=E5=92=8C=E8=BF=AD=E4=BB=A3=EF=BC=89"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\273\350\275\254\344\272\214\345\217\211\346\240\221.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 611e971aac..c1f0f2c136 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -928,7 +928,7 @@ public class Solution { return root; } - public void swap(TreeNode node) { + public void swap(TreeNode node) { TreeNode temp = node.left; node.left = node.right; node.right = temp; @@ -953,7 +953,7 @@ public class Solution { return root; } - public void swap(TreeNode node) { + public void swap(TreeNode node) { TreeNode temp = node.left; node.left = node.right; node.right = temp; From deaf08a9ad225a113fd7c0efd40f161d93ca460c Mon Sep 17 00:00:00 2001 From: zj <294839611@qq.com> Date: Tue, 21 Mar 2023 16:15:58 +0800 Subject: [PATCH 0085/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C?= =?UTF-8?q?=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C?= =?UTF-8?q?=E5=8C=85-1.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-1.md" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index ff0b6aba28..1b04b057f4 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -338,6 +338,64 @@ public class BagProblem { ``` +```java +import java.util.Arrays; + +public class BagProblem { + public static void main(String[] args) { + int[] weight = {1,3,4}; + int[] value = {15,20,30}; + int bagSize = 4; + testWeightBagProblem(weight,value,bagSize); + } + + /** + * 初始化 dp 数组做了简化(给物品增加冗余维)。这样初始化dp数组,默认全为0即可。 + * dp[i][j] 表示从下标为[0 - i-1]的物品里任意取,放进容量为j的背包,价值总和最大是多少。 + * 其实是模仿背包重量从 0 开始,背包容量 j 为 0 的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为 0。 + * 可选物品也可以从无开始,也就是没有物品可选,即dp[0][j],这样无论背包容量为多少,背包价值总和一定为 0。 + * @param weight 物品的重量 + * @param value 物品的价值 + * @param bagSize 背包的容量 + */ + public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){ + + // 创建dp数组 + int goods = weight.length; // 获取物品的数量 + int[][] dp = new int[goods + 1][bagSize + 1]; // 给物品增加冗余维,i = 0 表示没有物品可选 + + // 初始化dp数组,默认全为0即可 + // 填充dp数组 + for (int i = 1; i <= goods; i++) { + for (int j = 1; j <= bagSize; j++) { + if (j < weight[i - 1]) { // i - 1 对应物品 i + /** + * 当前背包的容量都没有当前物品i大的时候,是不放物品i的 + * 那么前i-1个物品能放下的最大价值就是当前情况的最大价值 + */ + dp[i][j] = dp[i - 1][j]; + } else { + /** + * 当前背包的容量可以放下物品i + * 那么此时分两种情况: + * 1、不放物品i + * 2、放物品i + * 比较这两种情况下,哪种背包中物品的最大价值最大 + */ + dp[i][j] = Math.max(dp[i - 1][j] , dp[i - 1][j - weight[i - 1]] + value[i - 1]); // i - 1 对应物品 i + } + } + } + + // 打印dp数组 + for(int[] arr : dp){ + System.out.println(Arrays.toString(arr)); + } + } +} + +``` + ### python ```python From 06ebd5f204f8a7012c1e4518ce778fa06750d7e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Thu, 23 Mar 2023 00:08:57 +0800 Subject: [PATCH 0086/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原先是先判断map中是否有这个key,后面更新时再get(key),经过测试,直接使用getOrDefault()方法效率会更高一些,没有当前key时返回0,值为0就是没有当前key --- ...33\346\225\260\347\233\270\345\212\240II.md" | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index c2a5710fa2..411b60e872 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -97,26 +97,25 @@ Java: ```Java class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { - Map map = new HashMap<>(); - int temp; int res = 0; + Map map = new HashMap(); //统计两个数组中的元素之和,同时统计出现的次数,放入map for (int i : nums1) { for (int j : nums2) { - temp = i + j; - if (map.containsKey(temp)) { - map.put(temp, map.get(temp) + 1); + int tmp = map.getOrDefault(i + j, 0); + if (tmp == 0) { + map.put(i + j, 1); } else { - map.put(temp, 1); + map.replace(i + j, tmp + 1); } } } //统计剩余的两个元素的和,在map中找是否存在相加为0的情况,同时记录次数 for (int i : nums3) { for (int j : nums4) { - temp = i + j; - if (map.containsKey(0 - temp)) { - res += map.get(0 - temp); + int tmp = map.getOrDefault(0 - i - j, 0); + if (tmp != 0) { + res += tmp; } } } From 37d12a168cfa59e0ae44203f5aa8234d588f23fd Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Wed, 22 Mar 2023 18:22:15 -0400 Subject: [PATCH 0087/1533] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python - 双指针法,不用数组 (同Carl写法) - 更快 --- ...17\347\273\235\345\257\271\345\267\256.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 203add397d..fa1430dec1 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -221,8 +221,27 @@ class Solution: for i in range(len(res)-1): // 统计有序数组的最小差值 r = min(abs(res[i]-res[i+1]),r) return r + + +class Solution: # 双指针法,不用数组 (同Carl写法) - 更快 + def getMinimumDifference(self, root: Optional[TreeNode]) -> int: + global pre,minval + pre = None + minval = 10**5 + self.traversal(root) + return minval + + def traversal(self,root): + global pre,minval + if not root: return None + self.traversal(root.left) + if pre and root.val-pre.val Date: Thu, 23 Mar 2023 10:57:32 +0800 Subject: [PATCH 0088/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=860056.?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=8C=BA=E9=97=B4=E7=9A=84=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\345\271\266\345\214\272\351\227\264.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index d467ab1a29..0daf5f9b2c 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -174,6 +174,34 @@ func max(a, b int) int { return b } ``` +```go +// 版本2 +func merge(intervals [][]int) [][]int { + if len(intervals) == 1 { + return intervals + } + sort.Slice(intervals, func(i, j int) bool { + return intervals[i][0] < intervals[j][0] + }) + res := make([][]int, 0) + res = append(res, intervals[0]) + for i := 1; i < len(intervals); i++ { + if intervals[i][0] <= res[len(res)-1][1]{ + res[len(res)-1][1] = max56(res[len(res)-1][1],intervals[i][1]) + } else { + res = append(res, intervals[i]) + } + } + return res +} +func max56(a, b int) int { + if a > b { + return a + } + return b +} +``` + ### Javascript ```javascript From f51f31290f69bf467bdb444d8c85a94b0c30616a Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 23 Mar 2023 11:10:20 +0800 Subject: [PATCH 0089/1533] =?UTF-8?q?Update=200406.=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...215\345\273\272\351\230\237\345\210\227.md" | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 48c498e1ad..2e6818db05 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -291,19 +291,19 @@ var reconstructQueue = function(people) { ```Rust impl Solution { - pub fn reconstruct_queue(people: Vec>) -> Vec> { - let mut people = people; + pub fn reconstruct_queue(mut people: Vec>) -> Vec> { + let mut queue = vec![]; people.sort_by(|a, b| { - if a[0] == b[0] { return a[1].cmp(&b[1]); } + if a[0] == b[0] { + return a[1].cmp(&b[1]); + } b[0].cmp(&a[0]) }); - let mut que: Vec> = Vec::new(); - que.push(people[0].clone()); - for i in 1..people.len() { - let position = people[i][1]; - que.insert(position as usize, people[i].clone()); + queue.push(people[0].clone()); + for v in people.iter().skip(1) { + queue.insert(v[1] as usize, v.clone()); } - que + queue } } ``` From b934f47c83340b91b8a6291d96420978da0ec0e6 Mon Sep 17 00:00:00 2001 From: milu-tao <91822069+milu-tao@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:14:39 +0800 Subject: [PATCH 0090/1533] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. result 在遍历过程中 ++ 就行,没必要另外计算 2.题目已经表明:s字符串的长度规则是:1 <= s.length <= 1000,所以前面不需要进行判断字符串长度 --- ...36\346\226\207\345\255\220\344\270\262.md" | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 90e6da9fbf..339247f58a 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -238,33 +238,24 @@ Java: ```java class Solution { public int countSubstrings(String s) { - int len, ans = 0; - if (s == null || (len = s.length()) < 1) return 0; - //dp[i][j]:s字符串下标i到下标j的字串是否是一个回文串,即s[i, j] + char[] chars = s.toCharArray(); + int len = chars.length; boolean[][] dp = new boolean[len][len]; - for (int j = 0; j < len; j++) { - for (int i = 0; i <= j; i++) { - //当两端字母一样时,才可以两端收缩进一步判断 - if (s.charAt(i) == s.charAt(j)) { - //i++,j--,即两端收缩之后i,j指针指向同一个字符或者i超过j了,必然是一个回文串 - if (j - i < 3) { + int result = 0; + for (int i = len - 1; i >= 0; i--) { + for (int j = i; j < len; j++) { + if (chars[i] == chars[j]) { + if (j - i <= 1) { // 情况一 和 情况二 + result++; + dp[i][j] = true; + } else if (dp[i + 1][j - 1]) { //情况三 + result++; dp[i][j] = true; - } else { - //否则通过收缩之后的字串判断 - dp[i][j] = dp[i + 1][j - 1]; } - } else {//两端字符不一样,不是回文串 - dp[i][j] = false; } } } - //遍历每一个字串,统计回文串个数 - for (int i = 0; i < len; i++) { - for (int j = 0; j < len; j++) { - if (dp[i][j]) ans++; - } - } - return ans; + return result; } } ``` From 4b65ece9ed30a88962782f1742d2d07d53783010 Mon Sep 17 00:00:00 2001 From: "Mr.D" Date: Sun, 26 Mar 2023 16:52:18 +0800 Subject: [PATCH 0091/1533] Add another Rust solution --- ...44\346\225\260\344\271\213\345\222\214.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index b3abb991ca..eea3ba7a44 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -216,6 +216,26 @@ impl Solution { } } ``` +Rust + +``` +use std::collections::HashMap; + +impl Solution { + pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut hm: HashMap = HashMap::new(); + for i in 0..nums.len() { + let j = target - nums[i]; + if hm.contains_key(&j) { + return vec![*hm.get(&j).unwrap(), i as i32] + } else { + hm.insert(nums[i], i as i32); + } + } + vec![-1, -1] + } +} +``` Javascript From c73c1a2b107780ce0aeb78c578112d065b887aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BA=A6=E8=B6=85=E7=A6=B9?= <1677425105@qq.com> Date: Sun, 26 Mar 2023 18:36:13 +0800 Subject: [PATCH 0092/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index ff0b6aba28..c45fc3d302 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -87,7 +87,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 那么可以有两个方向推出来dp[i][j], -* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以被背包内的价值依然和前面相同。) +* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以背包内的价值依然和前面相同。) * **放物品i**:由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]的时候不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 所以递归公式: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); From 62a9b6c3722a44f8bf71ea360d6331a2f9918294 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:07:22 +0800 Subject: [PATCH 0093/1533] =?UTF-8?q?update=200344.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 6ffbac26a0..775cfc5872 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -130,6 +130,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + From 22ab6281e44ca025aaca2e5a94ba3e07f2021c1a Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:17:07 +0800 Subject: [PATCH 0094/1533] =?UTF-8?q?update=200541.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2II=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\275\254\345\255\227\347\254\246\344\270\262II.md" | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index 516f78da3c..179395b3ac 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -65,6 +65,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + @@ -96,6 +99,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1)或O(n), 取决于使用的语言中字符串是否可以修改. + 另一种思路的解法 @@ -116,6 +122,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + ## 其他语言版本 From fe386a051dc73b3fd40bc79b70119df2e068ed9b Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:29:13 +0800 Subject: [PATCH 0095/1533] =?UTF-8?q?update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index eb78cc9d18..1b00666546 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -114,6 +114,7 @@ void removeExtraSpaces(string& s) { } ``` + 有的同学可能发现用erase来移除空格,在leetcode上性能也还行。主要是以下几点;: 1. leetcode上的测试集里,字符串的长度不够长,如果足够长,性能差距会非常明显。 @@ -197,6 +198,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) 或 O(n),取决于语言中字符串是否可变 + ## 其他语言版本 From cc1a327fb1d9f9e3ead9ba8f122b20ff4d112baf Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:36:32 +0800 Subject: [PATCH 0096/1533] =?UTF-8?q?update=20=E5=89=91=E6=8C=87Offer58-II?= =?UTF-8?q?.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 49480abfae..f368514f4b 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -66,6 +66,9 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度:O(1) + 是不是发现这代码也太简单了,哈哈。 # 总结 From 167cb84ee3b4ae514d898e33090d2453a516f02e Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 15:12:08 +0800 Subject: [PATCH 0097/1533] =?UTF-8?q?update=200028.=E5=AE=9E=E7=8E=B0strSt?= =?UTF-8?q?r=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0028.\345\256\236\347\216\260strStr.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 2757130c8a..263c168995 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -444,6 +444,8 @@ public: }; ``` +* 时间复杂度: O(n + m) +* 空间复杂度: O(m), 只需要保存字符串needle的前缀表 # 前缀表(不减一)C++实现 @@ -540,6 +542,9 @@ public: } }; ``` +* 时间复杂度: O(n + m) +* 空间复杂度: O(m) + # 总结 From dbcd875d3bcc0ccbf6b331bbfd07e799e272688b Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 16:22:07 +0800 Subject: [PATCH 0098/1533] =?UTF-8?q?update=200459.=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 6 ++++++ 1 file changed, 6 insertions(+) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 5d87021903..98c02a2544 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -73,6 +73,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) 不过这种解法还有一个问题,就是 我们最终还是要判断 一个字符串(s + s)是否出现过 s 的过程,大家可能直接用contains,find 之类的库函数。 却忽略了实现这些函数的时间复杂度(暴力解法是m * n,一般库函数实现为 O(m + n))。 @@ -185,6 +187,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) 前缀表(不减一)的C++代码实现: @@ -219,6 +223,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) ## 其他语言版本 From 334f4a27de2ecc00134c6cf287f658758f856e6f Mon Sep 17 00:00:00 2001 From: hoxiansen <1365171821@qq.com> Date: Tue, 28 Mar 2023 09:40:19 +0000 Subject: [PATCH 0099/1533] =?UTF-8?q?Update=200518.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2II.md=20=E8=A1=A5=E5=85=85=E4=BA=8C=E7=BB=B4d?= =?UTF-8?q?p=E6=95=B0=E7=BB=84=E7=9A=84Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\351\222\261\345\205\221\346\215\242II.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 3fd03e1607..eb5a844ce4 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -215,6 +215,27 @@ class Solution { } } ``` +```Java +// 二维dp数组版本,方便理解 +class Solution { + public int change(int amount, int[] coins) { + int[][] dp = new int[coins.length][amount + 1]; + // 只有一种硬币的情况 + for (int i = 0; i <= amount; i += coins[0]) { + dp[0][i] = 1; + } + for (int i = 1; i < coins.length; i++) { + for (int j = 0; j <= amount; j++) { + // 第i种硬币使用0~k次,求和 + for (int k = 0; k * coins[i] <= j; k++) { + dp[i][j] += dp[i - 1][j - k * coins[i]]; + } + } + } + return dp[coins.length - 1][amount]; + } +} +``` Python: From b18aba58bcfa66b31fb9d40ffb6805f75f5bd3ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E6=97=A0=E7=BC=BA?= Date: Tue, 28 Mar 2023 22:22:14 +0800 Subject: [PATCH 0100/1533] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将爬楼梯文件中的中字更正为了种 --- "problems/0070.\347\210\254\346\245\274\346\242\257.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index d824622389..a06ee91e1d 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -72,7 +72,7 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 3. dp数组如何初始化 -再回顾一下dp[i]的定义:爬到第i层楼梯,有dp[i]中方法。 +再回顾一下dp[i]的定义:爬到第i层楼梯,有dp[i]种方法。 那么i为0,dp[i]应该是多少呢,这个可以有很多解释,但基本都是直接奔着答案去解释的。 From dad45d599b9368e7ddeeea9b62bc53bea122fc24 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 29 Mar 2023 11:34:59 +0800 Subject: [PATCH 0101/1533] Update --- README.md | 2 +- ...5\344\273\243\347\220\206\350\247\243).md" | 145 ------------------ ...15\346\235\202\345\272\246\357\274\201.md" | 16 +- ...06\350\256\272\345\237\272\347\241\200.md" | 5 +- 4 files changed, 11 insertions(+), 157 deletions(-) delete mode 100644 "problems/0377-\347\273\204\345\220\210\346\200\273\345\222\214IV(\345\256\214\345\205\250\350\203\214\345\214\205\347\232\204\346\216\222\345\210\227\351\227\256\351\242\230\344\272\214\347\273\264\350\277\255\344\273\243\347\220\206\350\247\243).md" diff --git a/README.md b/README.md index a9b2f7efe9..f3e5812ca8 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ 如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 -目前「代码随想录」刷题攻略更新了:**200多篇文章,精讲了200道经典算法题目,共60w字的详细图解,部分难点题目还搭配了20分钟左右的视频讲解**。 +目前「代码随想录」刷题攻略更新了:**200多篇文章,精讲了200道经典算法题目,共60w字的详细图解,大部分题目都搭配了20分钟左右的视频讲解**,视频质量很好,口碑很好,大家可以去看看,视频列表:[代码随想录视频讲解](https://www.bilibili.com/video/BV1fA4y1o715)。 **这里每一篇题解,都是精品,值得仔细琢磨**。 diff --git "a/problems/0377-\347\273\204\345\220\210\346\200\273\345\222\214IV(\345\256\214\345\205\250\350\203\214\345\214\205\347\232\204\346\216\222\345\210\227\351\227\256\351\242\230\344\272\214\347\273\264\350\277\255\344\273\243\347\220\206\350\247\243).md" "b/problems/0377-\347\273\204\345\220\210\346\200\273\345\222\214IV(\345\256\214\345\205\250\350\203\214\345\214\205\347\232\204\346\216\222\345\210\227\351\227\256\351\242\230\344\272\214\347\273\264\350\277\255\344\273\243\347\220\206\350\247\243).md" deleted file mode 100644 index 276329c56a..0000000000 --- "a/problems/0377-\347\273\204\345\220\210\346\200\273\345\222\214IV(\345\256\214\345\205\250\350\203\214\345\214\205\347\232\204\346\216\222\345\210\227\351\227\256\351\242\230\344\272\214\347\273\264\350\277\255\344\273\243\347\220\206\350\247\243).md" +++ /dev/null @@ -1,145 +0,0 @@ -# 完全背包的排列问题模拟 - -#### Problem - -1. 排列问题是完全背包中十分棘手的问题。 -2. 其在迭代过程中需要先迭代背包容量,再迭代物品个数,使得其在代码理解上较难入手。 - -#### Contribution - -本文档以力扣上[组合总和IV](https://leetcode.cn/problems/combination-sum-iv/)为例,提供一个二维dp的代码例子,并提供模拟过程以便于理解 - -#### Code - -```cpp -int combinationSum4(vector& nums, int target) { - // 定义背包容量为target,物品个数为nums.size()的dp数组 - // dp[i][j]表示将第0-i个物品添加入排列中,和为j的排列方式 - vector> dp (nums.size(), vector(target+1,0)); - - // 表示有0,1,...,n个物品可选择的情况下,和为0的选择方法为1:什么都不取 - for(int i = 0; i < nums.size(); i++) dp[i][0] = 1; - - // 必须按列遍历,因为右边数组需要知道左边数组最低部的信息(排列问题) - // 后面的模拟可以更清楚的表现这么操作的原因 - for(int i = 1; i <= target; i++){ - for(int j = 0; j < nums.size(); j++){ - // 只有nums[j]可以取的情况 - if(j == 0){ - if(nums[j] > i) dp[j][i] = 0; - // 如果背包容量放不下 那么此时没有排列方式 - else dp[j][i] = dp[nums.size()-1][i-nums[j]]; - // 如果背包容量放的下 全排列方式为dp[最底层][容量-该物品容量]排列方式后面放一个nums[j] - } - // 有多个nums数可以取 - else{ - // 如果背包容量放不下 那么沿用0-j-1个物品的排列方式 - if(nums[j] > i) dp[j][i] = dp[j-1][i]; - // 如果背包容量放得下 在dp[最底层][容量-该物品容量]排列方式后面放一个nums[j]后面放个nums[j] - // INT_MAX避免溢出 - else if(i >= nums[j] && dp[j-1][i] < INT_MAX - dp[nums.size()-1][i-nums[j]]) - dp[j][i] = dp[j-1][i] + dp[nums.size()-1][i-nums[j]]; - } - } - } - // 打印dp数组 - for(int i = 0; i < nums.size(); i++){ - for(int j = 0; j <= target; j++){ - cout< Date: Wed, 29 Mar 2023 11:52:19 +0800 Subject: [PATCH 0102/1533] =?UTF-8?q?Update=20=E6=A0=B9=E6=8D=AE=E8=BA=AB?= =?UTF-8?q?=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97=EF=BC=88vector?= =?UTF-8?q?=E5=8E=9F=E7=90=86=E8=AE=B2=E8=A7=A3=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\262\350\247\243\357\274\211.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 94d94e3849..4f8cab8288 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -177,6 +177,35 @@ Java: Python: +Rust: + +```rust +// 版本二,使用list(链表) +use std::collections::LinkedList; +impl Solution{ + pub fn reconstruct_queue(mut people: Vec>) -> Vec> { + let mut queue = LinkedList::new(); + people.sort_by(|a, b| { + if a[0] == b[0] { + return a[1].cmp(&b[1]); + } + b[0].cmp(&a[0]) + }); + queue.push_back(people[0].clone()); + for v in people.iter().skip(1) { + if queue.len() > v[1] as usize { + let mut back_link = queue.split_off(v[1] as usize); + queue.push_back(v.clone()); + queue.append(&mut back_link); + } else { + queue.push_back(v.clone()); + } + } + queue.into_iter().collect() + } +} +``` + Go: From c835ec07b6fd76303e11ebf082df70fceb3891e3 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 16:44:08 +0800 Subject: [PATCH 0103/1533] =?UTF-8?q?Update=2024.=20=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9=20about=20rust=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index 4dc051aa74..12912382f0 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -410,7 +410,7 @@ impl Solution { // 递归 impl Solution { pub fn swap_pairs(head: Option>) -> Option> { - if head == None || head.as_ref().unwrap().next == None { + if head.is_none() || head.as_ref().unwrap().next.is_none() { return head; } From 59c86a7023989e3cb3195615f30a0a99e39fa556 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 18:15:26 +0800 Subject: [PATCH 0104/1533] =?UTF-8?q?Update=200844.=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\227\347\254\246\344\270\262.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" index 6534f18c90..40d5caa1c6 100644 --- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -535,6 +535,35 @@ function getIndexAfterDel(s: string, startIndex: number): number { } ``` +### Rust + +> 双指针 + +```rust +impl Solution { + pub fn backspace_compare(s: String, t: String) -> bool { + let (s, t) = ( + s.chars().collect::>(), + t.chars().collect::>(), + ); + Self::get_string(s) == Self::get_string(t) + } + + pub fn get_string(mut chars: Vec) -> Vec { + let mut slow = 0; + for i in 0..chars.len() { + if chars[i] == '#' { + slow = (slow as u32).saturating_sub(1) as usize; + } else { + chars[slow] = chars[i]; + slow += 1; + } + } + chars.truncate(slow); + chars + } +} +``` From e4951bc9dba7ebaa5314d41a52b0a5d97b8a6cea Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 18:34:11 +0800 Subject: [PATCH 0105/1533] =?UTF-8?q?Update=200844.=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\227\347\254\246\344\270\262.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" index 40d5caa1c6..ac56f6f990 100644 --- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -565,7 +565,27 @@ impl Solution { } ``` +> 双栈法 +```rust +impl Solution { + pub fn backspace_compare(s: String, t: String) -> bool { + Self::get_string(s) == Self::get_string(t) + } + + pub fn get_string(string: String) -> String { + let mut s = String::new(); + for c in string.chars() { + if c != '#' { + s.push(c); + } else if !s.is_empty() { + s.pop(); + } + } + s + } +} +```

From cff523c993e88c7e8de553225af7112113ae6754 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 19:58:07 +0800 Subject: [PATCH 0106/1533] =?UTF-8?q?Update=200452.=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86?= =?UTF-8?q?=E6=B0=94=E7=90=83.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\210\206\346\260\224\347\220\203.md" | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 6b018f978a..3066d5292a 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -270,26 +270,18 @@ int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){ ### Rust ```Rust -use std::cmp; impl Solution { pub fn find_min_arrow_shots(mut points: Vec>) -> i32 { - if points.is_empty() { - return 0; - } points.sort_by_key(|point| point[0]); - - let size = points.len(); - let mut count = 1; - - for i in 1..size { - if points[i][0] > points[i-1][1] { - count += 1; - } else { - points[i][1] = cmp::min(points[i][1], points[i-1][1]); + let mut result = 1; + for i in 1..points.len() { + if points[i][0] > points[i - 1][1] { + result += 1; + } else { + points[i][1] = points[i][1].min(points[i - 1][1]) } } - - return count; + result } } ``` From d1ccf8d3320eba0bfe0fb6dfdb88a4951c0729bb Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 20:00:07 +0800 Subject: [PATCH 0107/1533] =?UTF-8?q?Update=200452.=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86?= =?UTF-8?q?=E6=B0=94=E7=90=83.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 3066d5292a..79f9d59ad3 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -272,6 +272,9 @@ int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){ ```Rust impl Solution { pub fn find_min_arrow_shots(mut points: Vec>) -> i32 { + if points.is_empty() { + return 0; + } points.sort_by_key(|point| point[0]); let mut result = 1; for i in 1..points.len() { From 7e02650599bd98ef92f017f2838c09c17235a502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E6=97=A0=E7=BC=BA?= Date: Wed, 29 Mar 2023 20:21:27 +0800 Subject: [PATCH 0108/1533] =?UTF-8?q?update=20=E4=BD=BF=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化文档中的语言描述 --- ...\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index d6b3a17735..d7258d4576 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -92,7 +92,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。 这里就要说明本题力扣为什么改题意,而且修改题意之后 就清晰很多的原因了。 -新题目描述中明确说了 “你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。” 也就是说 从 到达 第 0 个台阶是不花费的,但从 第0 个台阶 往上跳的话,需要花费 cost[0]。 +新题目描述中明确说了 “你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。” 也就是说 到达 第 0 个台阶是不花费的,但从 第0 个台阶 往上跳的话,需要花费 cost[0]。 所以初始化 dp[0] = 0,dp[1] = 0; From 5daf942cd584259762907c1e796ad535d5aa9ce6 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Wed, 29 Mar 2023 22:47:54 -0400 Subject: [PATCH 0109/1533] =?UTF-8?q?Update=200225.=E7=94=A8=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增一個java solution用的是卡哥的邏輯 --- ...27\345\256\236\347\216\260\346\240\210.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 16b9e47cae..66d807c1b1 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -324,6 +324,43 @@ class MyStack { } } +``` +优化,使用一个 Queue 实现,但用卡哥的逻辑实现 +``` +class MyStack { + Queue queue; + + public MyStack() { + queue = new LinkedList<>(); + } + + public void push(int x) { + queue.add(x); + } + + public int pop() { + rePosition(); + return queue.poll(); + } + + public int top() { + rePosition(); + int result = queue.poll(); + queue.add(result); + return result; + } + + public boolean empty() { + return queue.isEmpty(); + } + + public void rePosition(){ + int size = queue.size(); + size--; + while(size-->0) + queue.add(queue.poll()); + } +} ``` Python: From d2811139ae718d265b37653ae9ebfd4f07701c9d Mon Sep 17 00:00:00 2001 From: Jeremy Feng <44312563+jeremy-feng@users.noreply.github.com> Date: Fri, 31 Mar 2023 00:35:33 +0800 Subject: [PATCH 0110/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- ...\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index d0b2ce0b8c..f80d467138 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -41,7 +41,7 @@ * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -首先在回顾一下01背包的核心代码 +首先再回顾一下01背包的核心代码 ```cpp for(int i = 0; i < weight.size(); i++) { // 遍历物品 for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量 From 96502b5f09b432ebb87f126fb56db83a5504abb4 Mon Sep 17 00:00:00 2001 From: Jeremy Feng <44312563+jeremy-feng@users.noreply.github.com> Date: Fri, 31 Mar 2023 00:39:08 +0800 Subject: [PATCH 0111/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- ...\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index f80d467138..e927aa2061 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -173,7 +173,7 @@ int main() { 别急,下一篇就是了!哈哈 -最后,**又可以出一道面试题了,就是纯完全背包,要求先用二维dp数组实现,然后再用一维dp数组实现,最后在问,两个for循环的先后是否可以颠倒?为什么?** +最后,**又可以出一道面试题了,就是纯完全背包,要求先用二维dp数组实现,然后再用一维dp数组实现,最后再问,两个for循环的先后是否可以颠倒?为什么?** 这个简单的完全背包问题,估计就可以难住不少候选人了。 From f7988e06dc26ffb93d93f41c8454b4d4504f5828 Mon Sep 17 00:00:00 2001 From: ayao98 <91120769+ayao98@users.noreply.github.com> Date: Fri, 31 Mar 2023 15:50:58 +0800 Subject: [PATCH 0112/1533] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit c++语言部分,函数名和节点命名大小写有错误 --- ...00\345\244\247\346\267\261\345\272\246.md" | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index b95e39c864..36578fd343 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -50,7 +50,7 @@ 代码如下: ```CPP -int getdepth(treenode* node) +int getdepth(TreeNode* node) ``` 2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。 @@ -76,14 +76,14 @@ return depth; ```CPP class solution { public: - int getdepth(treenode* node) { + int getdepth(TreeNode* node) { if (node == NULL) return 0; int leftdepth = getdepth(node->left); // 左 int rightdepth = getdepth(node->right); // 右 int depth = 1 + max(leftdepth, rightdepth); // 中 return depth; } - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { return getdepth(root); } }; @@ -93,9 +93,9 @@ public: ```CPP class solution { public: - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { if (root == null) return 0; - return 1 + max(maxdepth(root->left), maxdepth(root->right)); + return 1 + max(maxDepth(root->left), maxDepth(root->right)); } }; @@ -110,7 +110,7 @@ public: class solution { public: int result; - void getdepth(treenode* node, int depth) { + void getdepth(TreeNode* node, int depth) { result = depth > result ? depth : result; // 中 if (node->left == NULL && node->right == NULL) return ; @@ -127,7 +127,7 @@ public: } return ; } - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { result = 0; if (root == NULL) return result; getdepth(root, 1); @@ -144,7 +144,7 @@ public: class solution { public: int result; - void getdepth(treenode* node, int depth) { + void getdepth(TreeNode* node, int depth) { result = depth > result ? depth : result; // 中 if (node->left == NULL && node->right == NULL) return ; if (node->left) { // 左 @@ -155,7 +155,7 @@ public: } return ; } - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { result = 0; if (root == 0) return result; getdepth(root, 1); @@ -182,16 +182,16 @@ c++代码如下: ```CPP class solution { public: - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { if (root == NULL) return 0; int depth = 0; - queue que; + queue que; que.push(root); while(!que.empty()) { int size = que.size(); depth++; // 记录深度 for (int i = 0; i < size; i++) { - treenode* node = que.front(); + TreeNode* node = que.front(); que.pop(); if (node->left) que.push(node->left); if (node->right) que.push(node->right); @@ -230,11 +230,11 @@ c++代码: ```CPP class solution { public: - int maxdepth(node* root) { + int maxDepth(Node* root) { if (root == 0) return 0; int depth = 0; for (int i = 0; i < root->children.size(); i++) { - depth = max (depth, maxdepth(root->children[i])); + depth = max (depth, maxDepth(root->children[i])); } return depth + 1; } @@ -247,15 +247,15 @@ public: ```CPP class solution { public: - int maxdepth(node* root) { - queue que; + int maxDepth(Node* root) { + queue que; if (root != NULL) que.push(root); int depth = 0; while (!que.empty()) { int size = que.size(); depth++; // 记录深度 for (int i = 0; i < size; i++) { - node* node = que.front(); + Node* node = que.front(); que.pop(); for (int j = 0; j < node->children.size(); j++) { if (node->children[j]) que.push(node->children[j]); From 6af02057b674d64c19c733c490ae9090e30e498e Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Fri, 31 Mar 2023 18:04:59 +0800 Subject: [PATCH 0113/1533] =?UTF-8?q?Update=200435.=E6=97=A0=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\215\345\217\240\345\214\272\351\227\264.md" | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 02db203414..37ef819d5b 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -399,18 +399,20 @@ object Solution { ```Rust impl Solution { pub fn erase_overlap_intervals(intervals: Vec>) -> i32 { - if intervals.len() == 0 { return 0; } - let mut intervals = intervals; - intervals.sort_by(|a, b| a[1].cmp(&b[1])); + if intervals.is_empty() { + return 0; + } + intervals.sort_by_key(|interval| interval[1]); let mut count = 1; let mut end = intervals[0][1]; - for i in 1..intervals.len() { - if end <= intervals[i][0] { - end = intervals[i][1]; + for v in intervals.iter().skip(1) { + if end <= v[0] { + end = v[1]; count += 1; } } - intervals.len() as i32 - count + + (intervals.len() - count) as i32 } } ``` From 2208d96581e13b19e4d41610f259b04ad3a75856 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Fri, 31 Mar 2023 18:37:51 +0800 Subject: [PATCH 0114/1533] =?UTF-8?q?Update=200763.=E5=88=92=E5=88=86?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\345\214\272\351\227\264.md" | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index ad680ef92d..fbcafdc8cb 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -412,28 +412,22 @@ object Solution { ### Rust ```Rust -use std::collections::HashMap; impl Solution { - fn max (a: usize, b: usize) -> usize { - if a > b { a } else { b } - } pub fn partition_labels(s: String) -> Vec { - let s = s.chars().collect::>(); - let mut hash: HashMap = HashMap::new(); - for i in 0..s.len() { - hash.insert(s[i], i); + let mut hash = vec![0; 26]; + for (i, &c) in s.as_bytes().iter().enumerate() { + hash[(c - b'a') as usize] = i; } - let mut result: Vec = Vec::new(); - let mut left: usize = 0; - let mut right: usize = 0; - for i in 0..s.len() { - right = Self::max(right, hash[&s[i]]); + let mut res = vec![]; + let (mut left, mut right) = (0, 0); + for (i, &c) in s.as_bytes().iter().enumerate() { + right = right.max(hash[(c - b'a') as usize]); if i == right { - result.push((right - left + 1) as i32); + res.push((right - left + 1) as i32); left = i + 1; } } - result + res } } ``` From 24d59de1c0ef5580b122516404ea5b5bdd9c0d44 Mon Sep 17 00:00:00 2001 From: GODVvVZzz <2662446324@qq.com> Date: Fri, 31 Mar 2023 20:59:03 +0800 Subject: [PATCH 0115/1533] =?UTF-8?q?Modify=20376=E6=91=86=E5=8A=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md=20=E9=94=99=E5=88=AB=E5=AD=97-=E6=9E=9C->?= =?UTF-8?q?=E4=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 469c19fd8d..ff388b5579 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -89,7 +89,7 @@ ### 情况二:数组首尾两端 -所以本题统计峰值的时候,数组最左面和最右面如果统计呢? +所以本题统计峰值的时候,数组最左面和最右面如何统计呢? 题目中说了,如果只有两个不同的元素,那摆动序列也是 2。 @@ -655,3 +655,4 @@ object Solution { + From 7e80792a63f6d0b63745f2bb5c401b1a749afc1a Mon Sep 17 00:00:00 2001 From: YuZou Date: Fri, 31 Mar 2023 22:20:36 +0800 Subject: [PATCH 0116/1533] =?UTF-8?q?update=200045.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8FII.md=20=E4=BF=AE=E6=94=B9=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E8=AF=AD=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\263\350\267\203\346\270\270\346\210\217II.md" | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index c6e4e9b5b5..2f6e8b6728 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -76,11 +76,9 @@ public: for (int i = 0; i < nums.size(); i++) { nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标 if (i == curDistance) { // 遇到当前覆盖最远距离下标 - if (curDistance < nums.size() - 1) { // 如果当前覆盖最远距离下标不是终点 - ans++; // 需要走下一步 - curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了) - if (nextDistance >= nums.size() - 1) break; // 下一步的覆盖范围已经可以达到终点,结束循环 - } else break; // 当前覆盖最远距到达集合终点,不用做ans++操作了,直接结束 + ans++; // 需要走下一步 + curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了) + if (nextDistance >= nums.size() - 1) break; // 当前覆盖最远距到达集合终点,不用做ans++操作了,直接结束 } } return ans; From 6943358d8f75b4c189bcedaa5b79d5c0b0a93c2a Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Fri, 31 Mar 2023 21:58:27 -0400 Subject: [PATCH 0117/1533] =?UTF-8?q?Update=200538.=E6=8A=8A=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E7=B4=AF=E5=8A=A0=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 python 迭代法 --- ...72\347\264\257\345\212\240\346\240\221.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index 29b2184059..ad5310e150 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -234,6 +234,26 @@ class Solution: return root ``` +**迭代** +```python +class Solution: + def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if not root: return root + stack = [] + result = [] + cur = root + pre = 0 + while cur or stack: + if cur: + stack.append(cur) + cur = cur.right + else: + cur = stack.pop() + cur.val+= pre + pre = cur.val + cur =cur.left + return root +``` ## Go From ddda474eb4d73d894205cd5845b0c41f1b34226b Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Sat, 1 Apr 2023 11:42:50 +0800 Subject: [PATCH 0118/1533] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\345\271\266\345\214\272\351\227\264.md" | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 17d2cc0a1b..d03a590d6a 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -280,24 +280,22 @@ object Solution { ```Rust impl Solution { - fn max(a: i32, b: i32) -> i32 { - if a > b { a } else { b } - } - - pub fn merge(intervals: Vec>) -> Vec> { - let mut intervals = intervals; - let mut result = Vec::new(); - if intervals.len() == 0 { return result; } - intervals.sort_by(|a, b| a[0].cmp(&b[0])); - result.push(intervals[0].clone()); - for i in 1..intervals.len() { - if result.last_mut().unwrap()[1] >= intervals[i][0] { - result.last_mut().unwrap()[1] = Self::max(result.last_mut().unwrap()[1], intervals[i][1]); + pub fn merge(mut intervals: Vec>) -> Vec> { + let mut res = vec![]; + if intervals.is_empty() { + return res; + } + intervals.sort_by_key(|a| a[0]); + res.push(intervals[0].clone()); + for interval in intervals.into_iter().skip(1) { + let res_last_ele = res.last_mut().unwrap(); + if res_last_ele[1] >= interval[0] { + res_last_ele[1] = interval[1].max(res_last_ele[1]); } else { - result.push(intervals[i].clone()); + res.push(interval); } } - result + res } } ``` From cd1a680c26c25e928b7a56f3e98ce8d00f7e1577 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Sat, 1 Apr 2023 12:22:37 +0800 Subject: [PATCH 0119/1533] =?UTF-8?q?Update=200738.=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...236\347\232\204\346\225\260\345\255\227.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index f92652cd7f..1cf8a0a61e 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -280,18 +280,20 @@ object Solution { ```Rust impl Solution { pub fn monotone_increasing_digits(n: i32) -> i32 { - let mut str_num = n.to_string().chars().map(|x| x.to_digit(10).unwrap() as i32).collect::>(); - let mut flag = str_num.len(); - for i in (1..str_num.len()).rev() { - if str_num[i - 1] > str_num[i] { + let mut n_bytes = n.to_string().into_bytes(); + let mut flag = n_bytes.len(); + for i in (1..n_bytes.len()).rev() { + if n_bytes[i - 1] > n_bytes[i] { flag = i; - str_num[i - 1] -= 1; + n_bytes[i - 1] -= 1; } } - for i in flag..str_num.len() { - str_num[i] = 9; + for v in n_bytes.iter_mut().skip(flag) { + *v = 57; } - str_num.iter().fold(0, |acc, x| acc * 10 + x) + n_bytes + .into_iter() + .fold(0, |acc, x| acc * 10 + x as i32 - 48) } } ``` From 02f9c26f696774c9db2aa7bef4ac25f9d4b28803 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 1 Apr 2023 12:42:01 +0800 Subject: [PATCH 0120/1533] =?UTF-8?q?updaye=200232.=E7=94=A8=E6=A0=88?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\210\345\256\236\347\216\260\351\230\237\345\210\227.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index efeb10463e..4a57ee966b 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -112,6 +112,10 @@ public: ``` +* 时间复杂度: push和empty为O(1), pop和peek为O(n) +* 空间复杂度: O(n) + + ## 拓展 可以看出peek()的实现,直接复用了pop(), 要不然,对stOut判空的逻辑又要重写一遍。 From 1b6e26be2bf4f2dd584b3b59e4781a9d79cd4b48 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 1 Apr 2023 12:45:02 +0800 Subject: [PATCH 0121/1533] =?UTF-8?q?update=200225.=E7=94=A8=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\237\345\210\227\345\256\236\347\216\260\346\240\210.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 16b9e47cae..bad2faec27 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -111,6 +111,8 @@ public: } }; ``` +* 时间复杂度: push为O(n),其他为O(1) +* 空间复杂度: O(n) # 优化 @@ -156,6 +158,9 @@ public: } }; ``` +* 时间复杂度: push为O(n),其他为O(1) +* 空间复杂度: O(n) + # 其他语言版本 From 916718824bd6fe521ee2164b29f71d11633828a7 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 1 Apr 2023 12:47:36 +0800 Subject: [PATCH 0122/1533] =?UTF-8?q?update=200020.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E6=8B=AC=E5=8F=B7=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index a25129c52a..213d61b795 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -135,6 +135,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + 技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。 From 2633b1a346e106e8d14ee6404f1cf50cfb83a0af Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 1 Apr 2023 12:50:29 +0800 Subject: [PATCH 0123/1533] =?UTF-8?q?update=201047.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9:=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\270\351\202\273\351\207\215\345\244\215\351\241\271.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 810f7292ce..694f1a92dc 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -77,6 +77,9 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + 当然可以拿字符串直接作为栈,这样省去了栈还要转为字符串的操作。 @@ -99,6 +102,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1),返回值不计空间复杂度 ## 题外话 From 6cd25d1fc5f98a02c7ef1ad52ad416c39d7357a4 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 1 Apr 2023 12:53:40 +0800 Subject: [PATCH 0124/1533] =?UTF-8?q?update=200150.=E9=80=86=E6=B3=A2?= =?UTF-8?q?=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC:=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index f0323bc4ff..09cc4f96e3 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -113,6 +113,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 题外话 From 1662ac347d28f35073066dba2761cd9d0c4bcf35 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 1 Apr 2023 12:55:37 +0800 Subject: [PATCH 0125/1533] =?UTF-8?q?update=200239.=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 540ab5d7f5..f1c4b76ccf 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -184,6 +184,9 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(k) + 再来看一下时间复杂度,使用单调队列的时间复杂度是 O(n)。 From b28ba70ee1464fbd915c5d655bd6865a7d2d66e0 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 1 Apr 2023 12:57:24 +0800 Subject: [PATCH 0126/1533] =?UTF-8?q?update=20=200347.=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\252\351\253\230\351\242\221\345\205\203\347\264\240.md" | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index d8eabef4a5..0d268d9bdc 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -79,8 +79,6 @@ ```CPP -// 时间复杂度:O(nlogk) -// 空间复杂度:O(n) class Solution { public: // 小顶堆 @@ -120,6 +118,10 @@ public: } }; ``` + +* 时间复杂度: O(nlogk) +* 空间复杂度: O(n) + # 拓展 大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。 From d7d7e6c733716a7eb516dea3c8b7660e8d0ac9ce Mon Sep 17 00:00:00 2001 From: ray Date: Sat, 1 Apr 2023 20:54:58 +0800 Subject: [PATCH 0127/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=20=E5=86=99?= =?UTF-8?q?=E6=B3=95=E6=9B=B4Python=E4=B8=80=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...31\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 7e3955a5ef..b52f16ea23 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -316,8 +316,8 @@ class Solution: def removeElements(self, head: ListNode, val: int) -> ListNode: dummy_head = ListNode(next=head) #添加一个虚拟节点 cur = dummy_head - while(cur.next!=None): - if(cur.next.val == val): + while cur.next: + if cur.next.val == val: cur.next = cur.next.next #删除cur.next节点 else: cur = cur.next From befa14402bbf12eb147235e190bb775a42169ba9 Mon Sep 17 00:00:00 2001 From: jyu <1025368039@qq.com> Date: Sat, 1 Apr 2023 21:45:43 +0800 Subject: [PATCH 0128/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A0151=20C=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\347\232\204\345\215\225\350\257\215.md" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 1b00666546..8fa7c77c9e 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -970,6 +970,49 @@ pub fn remove_extra_spaces(s: &mut Vec) { } } ``` +C: + +```C +// 翻转字符串中指定范围的字符 +void reverse(char* s, int start, int end) { + for (int i = start, j = end; i < j; i++, j--) { + int tmp = s[i]; + s[i] = s[j]; + s[j] = tmp; + } +} + +// 删除字符串两端和中间多余的空格 +void removeExtraSpace(char* s) { + int start = 0; // 指向字符串开头的指针 + int end = strlen(s) - 1; // 指向字符串结尾的指针 + while (s[start] == ' ') start++; // 移动指针 start,直到找到第一个非空格字符 + while (s[end] == ' ') end--; // 移动指针 end,直到找到第一个非空格字符 + int slow = 0; // 指向新字符串的下一个写入位置的指针 + for (int i = start; i <= end; i++) { // 遍历整个字符串 + if (s[i] == ' ' && s[i+1] == ' ') { // 如果当前字符是空格,并且下一个字符也是空格,则跳过 + continue; + } + s[slow] = s[i]; // 否则,将当前字符复制到新字符串的 slow 位置 + slow++; // 将 slow 指针向后移动 + } + s[slow] = '\0'; // 在新字符串的末尾添加一个空字符 +} + +// 翻转字符串中的单词 +char * reverseWords(char * s){ + removeExtraSpace(s); // 先删除字符串两端和中间的多余空格 + reverse(s, 0, strlen(s) - 1); // 翻转整个字符串 + int slow = 0; // 指向每个单词的开头位置的指针 + for (int i = 0; i <= strlen(s); i++) { // 遍历整个字符串 + if (s[i] ==' ' || s[i] == '\0') { // 如果当前字符是空格或空字符,说明一个单词结束了 + reverse(s, slow, i-1); // 翻转单词 + slow = i + 1; // 将 slow 指针指向下一个单词的开头位置 + } + } + return s; // 返回处理后的字符串 +} +```

From c1f17275e5879b94e0dcbc321f9dd434068ce516 Mon Sep 17 00:00:00 2001 From: mercer5 <602502833@qq.com> Date: Sun, 2 Apr 2023 11:40:30 +0800 Subject: [PATCH 0129/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00739=E6=AF=8F?= =?UTF-8?q?=E6=97=A5=E6=B8=A9=E5=BA=A6=20python=20=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\217\346\227\245\346\270\251\345\272\246.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index 263a28f40c..e521fbc6e6 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -271,6 +271,7 @@ class Solution { ``` Python: +> 未精简版本 ```python class Solution: @@ -291,6 +292,22 @@ class Solution: return answer ``` +> 精简版本 + +```python +class Solution: + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: + # 单调栈 + answer = [0]*len(temperatures) + stack = [] + for i in range(len(temperatures)): + while len(stack)>0 and temperatures[i] > temperatures[stack[-1]]: + answer[stack[-1]] = i - stack[-1] + stack.pop() + stack.append(i) + return answer +``` + Go: > 暴力法 From a490e39206f4fd5d89165bbf0b38f982736e5309 Mon Sep 17 00:00:00 2001 From: mercer5 <602502833@qq.com> Date: Sun, 2 Apr 2023 11:50:29 +0800 Subject: [PATCH 0130/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90739=E6=AF=8F?= =?UTF-8?q?=E6=97=A5=E6=B8=A9=E5=BA=A6=20python=20=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" | 1 - 1 file changed, 1 deletion(-) diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index e521fbc6e6..749dc97235 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -297,7 +297,6 @@ class Solution: ```python class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: - # 单调栈 answer = [0]*len(temperatures) stack = [] for i in range(len(temperatures)): From 34e1b0ab5ce4a839ac9bf40b9e7ceb582fd51119 Mon Sep 17 00:00:00 2001 From: ayao98 <91120769+ayao98@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:48:51 +0800 Subject: [PATCH 0131/1533] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...112.\350\267\257\345\276\204\346\200\273\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 9b6027d1e4..5958de93e6 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -250,7 +250,7 @@ private: vector> result; vector path; // 递归函数不需要返回值,因为我们要遍历整个树 - void traversal(treenode* cur, int count) { + void traversal(TreeNode* cur, int count) { if (!cur->left && !cur->right && count == 0) { // 遇到了叶子节点且找到了和为sum的路径 result.push_back(path); return; @@ -276,10 +276,10 @@ private: } public: - vector> pathsum(treenode* root, int sum) { + vector> pathSum(TreeNode* root, int sum) { result.clear(); path.clear(); - if (root == null) return result; + if (root == NULL) return result; path.push_back(root->val); // 把根节点放进路径 traversal(root, sum - root->val); return result; From 9e27f3dd9b40dabef0afd7e085fb6dec0e745474 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Tue, 4 Apr 2023 22:51:45 -0400 Subject: [PATCH 0132/1533] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加: python - 使用used数组 - 神似之前几题写法 --- ...11\346\216\222\350\241\214\347\250\213.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 9bd5df7aad..95e7d3ed5c 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -380,7 +380,33 @@ class Solution: backtracking("JFK") return path ``` + +python - 使用used数组 - 神似之前几题写法 +```python +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + global used,path,results + used = [0]*len(tickets) + path = ['JFK'] + results = [] + tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的 + self.backtracking(tickets,'JFK') + return results[0] + def backtracking(self,tickets,cur): + if sum(used) == len(tickets): + results.append(path[:]) + return True # 只要找到就返回 + for i in range(len(tickets)): + if tickets[i][0]==cur and used[i]==0: + used[i]=1 + path.append(tickets[i][1]) + state = self.backtracking(tickets,tickets[i][1]) + path.pop() + used[i]=0 + if state: return True # 只要找到就返回,不继续搜索了 +``` + ### GO ```go type pair struct { From 1d74967090ff2adf96d84ba7f4dbd81d0ed53e68 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Thu, 6 Apr 2023 13:40:06 -0500 Subject: [PATCH 0133/1533] fix typos --- .../0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 469c19fd8d..26baf2f966 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -99,7 +99,7 @@ 这里我们可以写死,就是 如果只有两个元素,且元素不同,那么结果为 2。 -不写死的话,如果和我们的判断规则结合在一起呢? +不写死的话,如何和我们的判断规则结合在一起呢? 可以假设,数组最前面还有一个数字,那这个数字应该是什么呢? From 700b5c388bb1c3cb0a0d70ee9d0e0b54d5235a6e Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:48:47 -0400 Subject: [PATCH 0134/1533] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python - 贪心版本三 - 类似‘55-跳跃游戏’写法 --- ...\263\350\267\203\346\270\270\346\210\217II.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index c6e4e9b5b5..d12664aca8 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -231,6 +231,21 @@ class Solution: step += 1 return step ``` +```python +# 贪心版本三 - 类似‘55-跳跃游戏’写法 +class Solution: + def jump(self, nums) -> int: + if len(nums)==1: return 0 + i = 0 + count = 0 + cover = 0 + while i<=cover: + for i in range(i,cover+1): + cover = max(nums[i]+i,cover) + if cover>=len(nums)-1: return count+1 + count+=1 + +``` ```python # 动态规划做法 From bf8c539a635ce317ab9dd9598f06ca30bdebb677 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Thu, 6 Apr 2023 23:39:05 -0400 Subject: [PATCH 0135/1533] =?UTF-8?q?Update=200452.=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86?= =?UTF-8?q?=E6=B0=94=E7=90=83.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 python 不改变原数组 写法 --- ...4\225\347\210\206\346\260\224\347\220\203.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 53eee25c77..67070ba45a 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -177,7 +177,23 @@ class Solution: points[i][1] = min(points[i - 1][1], points[i][1]) # 更新重叠气球最小右边界 return result ``` +```python +class Solution: # 不改变原数组 + def findMinArrowShots(self, points: List[List[int]]) -> int: + points.sort(key = lambda x: x[0]) + sl,sr = points[0][0],points[0][1] + count = 1 + for i in points: + if i[0]>sr: + count+=1 + sl,sr = i[0],i[1] + else: + sl = max(sl,i[0]) + sr = min(sr,i[1]) + return count + +``` ### Go ```go func findMinArrowShots(points [][]int) int { From 4b0982e681e72081752843e8df83c0ceec1b543a Mon Sep 17 00:00:00 2001 From: Liu Cheng <32453247+Falldio@users.noreply.github.com> Date: Fri, 7 Apr 2023 14:29:56 +0800 Subject: [PATCH 0136/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00132.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2II=E7=9A=84Golang=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\233\236\346\226\207\344\270\262II.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" index eb0e39fb5a..9b164dfbb5 100644 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -289,7 +289,45 @@ class Solution: ## Go ```go +func minCut(s string) int { + isValid := make([][]bool, len(s)) + for i := 0; i < len(isValid); i++ { + isValid[i] = make([]bool, len(s)) + isValid[i][i] = true + } + for i := len(s) - 1; i >= 0; i-- { + for j := i + 1; j < len(s); j++ { + if s[i] == s[j] && (isValid[i + 1][j - 1] || j - i == 1) { + isValid[i][j] = true + } + } + } + dp := make([]int, len(s)) + for i := 0; i < len(s); i++ { + dp[i] = math.MaxInt + } + for i := 0; i < len(s); i++ { + if isValid[0][i] { + dp[i] = 0 + continue + } + for j := 0; j < i; j++ { + if isValid[j + 1][i] { + dp[i] = min(dp[i], dp[j] + 1) + } + } + } + return dp[len(s) - 1] +} + +func min(i, j int) int { + if i < j { + return i + } else { + return j + } +} ``` ## JavaScript From dee25873fc8f594dbc0209453399669df5fdf61a Mon Sep 17 00:00:00 2001 From: Levi <3573897471@qq.com> Date: Fri, 7 Apr 2023 17:05:31 +0800 Subject: [PATCH 0137/1533] =?UTF-8?q?=E8=A1=A5=E5=85=850111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=85=B3=E4=BA=8E=E5=89=8D=E5=BA=8F=E9=81=8D?= =?UTF-8?q?=E5=8E=86=EF=BC=88=E4=B8=AD=E5=B7=A6=E5=8F=B3=EF=BC=89=E7=9A=84?= =?UTF-8?q?=E4=B8=AD=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\234\200\345\260\217\346\267\261\345\272\246.md" | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index de36c6f25a..47569b05c6 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -170,11 +170,14 @@ class Solution { private: int result; void getdepth(TreeNode* node, int depth) { - if (node->left == NULL && node->right == NULL) { - result = min(depth, result); + // 函数递归终止条件 + if (root == nullptr) { return; } - // 中 只不过中没有处理的逻辑 + // 中,处理逻辑:判断是不是叶子结点 + if (root -> left == nullptr && root->right == nullptr) { + res = min(res, depth); + } if (node->left) { // 左 getdepth(node->left, depth + 1); } @@ -186,7 +189,9 @@ private: public: int minDepth(TreeNode* root) { - if (root == NULL) return 0; + if (root == nullptr) { + return 0; + } result = INT_MAX; getdepth(root, 1); return result; From e68a8a379be1defd7952a0d87fcac4350363de13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E6=97=A0=E7=BC=BA?= Date: Sun, 9 Apr 2023 14:54:51 +0800 Subject: [PATCH 0138/1533] =?UTF-8?q?update=20=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更正文档错别字 --- .../0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 85130ab4e4..6221042038 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -135,7 +135,7 @@ for (int i = 1; i < m; i++) { ![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256.png) -如果这个图看不同,建议在理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下! +如果这个图看不懂,建议再理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下! 动规五部分分析完毕,对应C++代码如下: From e1e695e78d3fbba5ebf01fe1be69c6569f9a035a Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Sun, 9 Apr 2023 23:11:11 +0800 Subject: [PATCH 0139/1533] =?UTF-8?q?Update=200509.=E6=96=90=E6=B3=A2?= =?UTF-8?q?=E9=82=A3=E5=A5=91=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\351\202\243\345\245\221\346\225\260.md" | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 08175058bb..479b36a0f8 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -347,26 +347,32 @@ int fib(int n){ ### Rust 动态规划: ```Rust -pub fn fib(n: i32) -> i32 { - let n = n as usize; - let mut dp = vec![0; 31]; - dp[1] = 1; - for i in 2..=n { - dp[i] = dp[i - 1] + dp[i - 2]; +impl Solution { + pub fn fib(n: i32) -> i32 { + if n <= 1 { + return n; + } + let n = n as usize; + let mut dp = vec![0; n + 1]; + dp[1] = 1; + for i in 2..=n { + dp[i] = dp[i - 2] + dp[i - 1]; + } + dp[n] } - dp[n] } ``` 递归实现: ```Rust -pub fn fib(n: i32) -> i32 { - //若n小于等于1,返回n - f n <= 1 { - return n; +impl Solution { + pub fn fib(n: i32) -> i32 { + if n <= 1 { + n + } else { + Self::fib(n - 1) + Self::fib(n - 2) + } } - //否则返回fib(n-1) + fib(n-2) - return fib(n - 1) + fib(n - 2); } ``` From 185f6df9d8dbad3c67724bdaa97b6fa307b5b9e1 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Sun, 9 Apr 2023 17:25:58 -0400 Subject: [PATCH 0140/1533] =?UTF-8?q?Update=2020210204=E5=8A=A8=E8=A7=84?= =?UTF-8?q?=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字: 原为:先遍历物品,在遍历背包 改为:先遍历物品,再遍历背包 --- ...47\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 9a64c1529d..fb570bd402 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -146,7 +146,7 @@ public: **这也体现了刷题顺序的重要性**。 -先遍历背包,在遍历物品: +先遍历背包,再遍历物品: ```CPP // 版本一 @@ -165,7 +165,7 @@ public: }; ``` -先遍历物品,在遍历背包: +先遍历物品,再遍历背包: ```CPP // 版本二 From 2be03e614ddaa09b11b7bae3de7fddd99b959a92 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Sun, 9 Apr 2023 18:07:43 -0400 Subject: [PATCH 0141/1533] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加:python - 和视频中写法一致(和最上面C++写法一致) --- ...5\215\225\350\257\215\346\213\206\345\210\206.md" | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index edb2c65ad6..230942ef02 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -351,7 +351,17 @@ class Solution: dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j]) return dp[len(s)] ``` - +```python +class Solution: # 和视频中写法一致(和最上面C++写法一致) + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + dp = [False]*(len(s)+1) + dp[0]=True + for j in range(1,len(s)+1): + for i in range(j): + word = s[i:j] + if word in wordDict and dp[i]: dp[j]=True + return dp[-1] +``` From 962744515d4e67a417dc0f87ffda90710b974a76 Mon Sep 17 00:00:00 2001 From: "435962415@qq.com" Date: Mon, 10 Apr 2023 11:59:08 +0800 Subject: [PATCH 0142/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=860188.?= =?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?= =?UTF-8?q?=E6=97=B6=E6=9C=BAIV=E7=9A=84=E8=A7=A3=E6=B3=95=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272IV.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 4fdd7bf40c..77d1c961ed 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -323,6 +323,42 @@ func max(a, b int) int { } ``` +版本二: 三维 dp数组 +```go +func maxProfit(k int, prices []int) int { + length := len(prices) + if length == 0 { + return 0 + } + // [天数][交易次数][是否持有股票] + // 1表示不持有/卖出, 0表示持有/买入 + dp := make([][][]int, length) + for i := 0; i < length; i++ { + dp[i] = make([][]int, k+1) + for j := 0; j <= k; j++ { + dp[i][j] = make([]int, 2) + } + } + for j := 0; j <= k; j++ { + dp[0][j][0] = -prices[0] + } + for i := 1; i < length; i++ { + for j := 1; j <= k; j++ { + dp[i][j][0] = max188(dp[i-1][j][0], dp[i-1][j-1][1]-prices[i]) + dp[i][j][1] = max188(dp[i-1][j][1], dp[i-1][j][0]+prices[i]) + } + } + return dp[length-1][k][1] +} + +func max188(a, b int) int { + if a > b { + return a + } + return b +} +``` + Javascript: ```javascript From 801e03ab3112da8e3e1666fafded4aa998836723 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 10 Apr 2023 00:48:03 -0400 Subject: [PATCH 0143/1533] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python - 二维dp数组写法 --- ...6\211\223\345\256\266\345\212\253\350\210\215.md" | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index fdb2dabf5f..20e18c0870 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -150,7 +150,17 @@ class Solution: dp[i] = max(dp[i-2]+nums[i], dp[i-1]) return dp[-1] ``` - +```python +class Solution: # 二维dp数组写法 + def rob(self, nums: List[int]) -> int: + dp = [[0,0] for _ in range(len(nums))] + dp[0][1] = nums[0] + for i in range(1,len(nums)): + dp[i][0] = max(dp[i-1][1],dp[i-1][0]) + dp[i][1] = dp[i-1][0]+nums[i] + print(dp) + return max(dp[-1]) +``` Go: ```Go func rob(nums []int) int { From 34aabac5b93af8dcb2e56a48c8f1b9049f2a28be Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 10 Apr 2023 00:58:56 -0400 Subject: [PATCH 0144/1533] =?UTF-8?q?Update=200213.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python - 二维dp数组写法 --- ...\223\345\256\266\345\212\253\350\210\215II.md" | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index 0627eedb23..5d315d5cd5 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -142,7 +142,20 @@ class Solution: dp[i]=max(dp[i-1],dp[i-2]+nums[i]) return dp[-1] ``` - +```python +class Solution: # 二维dp数组写法 + def rob(self, nums: List[int]) -> int: + if len(nums)<3: return max(nums) + return max(self.default(nums[:-1]),self.default(nums[1:])) + def default(self,nums): + dp = [[0,0] for _ in range(len(nums))] + dp[0][1] = nums[0] + for i in range(1,len(nums)): + dp[i][0] = max(dp[i-1]) + dp[i][1] = dp[i-1][0] + nums[i] + return max(dp[-1]) + +``` Go: ```go From abbcbe1ed0b52b7232957bf9152ad0e92104114d Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 10 Apr 2023 15:28:58 +0800 Subject: [PATCH 0145/1533] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...070.\347\210\254\346\245\274\346\242\257.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index a06ee91e1d..c46e3b4669 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -470,6 +470,23 @@ impl Solution { } ``` +dp 数组 + +```rust +impl Solution { + pub fn climb_stairs(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![0; n + 1]; + dp[0] = 1; + dp[1] = 1; + for i in 2..=n { + dp[i] = dp[i - 1] + dp[i - 2]; + } + dp[n] + } +} +``` +

From 03f037afebff9684e9d64283b1d8d5ec690e684a Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 10 Apr 2023 15:36:23 +0800 Subject: [PATCH 0146/1533] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0070.\347\210\254\346\245\274\346\242\257.md" | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index c46e3b4669..793d3e67f7 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -454,19 +454,16 @@ public class Solution { ```rust impl Solution { pub fn climb_stairs(n: i32) -> i32 { - if n <= 2 { + if n <= 1 { return n; } - let mut a = 1; - let mut b = 2; - let mut f = 0; - for i in 2..n { + let (mut a, mut b, mut f) = (1, 1, 0); + for _ in 2..=n { f = a + b; a = b; b = f; } - return f; - } + f } ``` From dbcec85e977fce41e5459ef3d108b29abc98883b Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:22:10 +0800 Subject: [PATCH 0147/1533] =?UTF-8?q?update=200455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5.\345\210\206\345\217\221\351\245\274\345\271\262.md" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 3688b3fd95..efadb43309 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -56,8 +56,6 @@ C++代码整体如下: ```CPP // 版本一 -// 时间复杂度:O(nlogn) -// 空间复杂度:O(1) class Solution { public: int findContentChildren(vector& g, vector& s) { @@ -75,6 +73,9 @@ public: } }; ``` +* 时间复杂度:O(nlogn) +* 空间复杂度:O(1) + 从代码中可以看出我用了一个 index 来控制饼干数组的遍历,遍历饼干并没有再起一个 for 循环,而是采用自减的方式,这也是常用的技巧。 @@ -118,6 +119,9 @@ public: } }; ``` +* 时间复杂度:O(nlogn) +* 空间复杂度:O(1) + 细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。 From 80006ad61592e0268fd4a8857a8a8b687278c5b3 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:28:09 +0800 Subject: [PATCH 0148/1533] =?UTF-8?q?update=200053.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\200\345\244\247\345\255\220\345\272\217\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 1de68041a3..48f8be29c1 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -24,8 +24,6 @@ 暴力解法的思路,第一层 for 就是设置起始位置,第二层 for 循环遍历数组寻找最大值 -- 时间复杂度:O(n^2) -- 空间复杂度:O(1) ```CPP class Solution { @@ -44,6 +42,9 @@ public: } }; ``` +* 时间复杂度:O(n^2) +* 空间复杂度:O(1) + 以上暴力的解法 C++勉强可以过,其他语言就不确定了。 @@ -98,7 +99,6 @@ public: } }; ``` - - 时间复杂度:O(n) - 空间复杂度:O(1) From 85f54a718d022e6a3cb5883fb3d9aff5745a3362 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:30:07 +0800 Subject: [PATCH 0149/1533] =?UTF-8?q?update=20=20=200055.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index ce9a21ec57..c187ef8f10 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -75,6 +75,10 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + + ## 总结 这道题目关键点在于:不用拘泥于每次究竟跳几步,而是看覆盖范围,覆盖范围内一定是可以跳过来的,不用管是怎么跳的。 From 4afa9c2a51f3745b1dc6497fd522a607f3174136 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:32:31 +0800 Subject: [PATCH 0150/1533] =?UTF-8?q?update=20=20=200045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\267\263\350\267\203\346\270\270\346\210\217II.md" | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index c6e4e9b5b5..558b32b9d7 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -88,6 +88,10 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + + ## 方法二 依然是贪心,思路和方法一差不多,代码可以简洁一些。 @@ -127,6 +131,11 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + + + 可以看出版本二的代码相对于版本一简化了不少! **其精髓在于控制移动下标 i 只移动到 nums.size() - 2 的位置**,所以移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不用考虑别的了。 From 38c74c90d793b9637feceec6350e269d9e460a16 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:34:25 +0800 Subject: [PATCH 0151/1533] =?UTF-8?q?update=20=20=201005.K=E6=AC=A1?= =?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\226\347\232\204\346\225\260\347\273\204\345\222\214.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 4a08085701..27a575c717 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -85,6 +85,10 @@ public: }; ``` +* 时间复杂度: O(nlogn) +* 空间复杂度: O(1) + + ## 总结 贪心的题目如果简单起来,会让人简单到开始怀疑:本来不就应该这么做么?这也算是算法?我认为这不是贪心? From 25bf0859a1e7b68ddfcb83456e9828244e0ee703 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:36:37 +0800 Subject: [PATCH 0152/1533] =?UTF-8?q?update=20=20=200135.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index ef79868a1a..1ba1563fb3 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -121,6 +121,11 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + + + ## 总结 这在leetcode上是一道困难的题目,其难点就在于贪心的策略,如果在考虑局部的时候想两边兼顾,就会顾此失彼。 From 4c7464fa93ca388699b37935e5b56e68b1f6fed2 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:37:48 +0800 Subject: [PATCH 0153/1533] =?UTF-8?q?update=20=200860.=E6=9F=A0=E6=AA=AC?= =?UTF-8?q?=E6=B0=B4=E6=89=BE=E9=9B=B6=20=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index cd52adb099..d96e08797b 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -115,6 +115,9 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + ## 总结 From ff77937816b26b6aa88a41fb6b1684837ca1a31d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:41:40 +0800 Subject: [PATCH 0154/1533] =?UTF-8?q?update=20=20=200056.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=8C=BA=E9=97=B4=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index ec65910859..72dc69c57f 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -73,6 +73,10 @@ public: }; ``` +* 时间复杂度: O(nlogn) +* 空间复杂度: O(logn),排序需要的空间开销 + + ## 其他语言版本 From 528c1d4f9e9678b143706178f76d91c0de6947c4 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 00:49:38 +0800 Subject: [PATCH 0155/1533] =?UTF-8?q?update=20=20=200968.=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\221\346\216\247\344\272\214\345\217\211\346\240\221.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index 267e7f6ede..fdec56cafa 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -303,6 +303,11 @@ public: ``` +* 时间复杂度: O(n),需要遍历二叉树上的每个节点 +* 空间复杂度: O(n) + + + 大家可能会惊讶,居然可以这么简短,**其实就是在版本一的基础上,使用else把一些情况直接覆盖掉了**。 在网上关于这道题解可以搜到很多这种神级别的代码,但都没讲不清楚,如果直接看代码的话,指定越看越晕,**所以建议大家对着版本一的代码一步一步来,版本二中看不中用!**。 From 4e1590e26db6363af09e0feef0109d27d48b3958 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:14:10 +0800 Subject: [PATCH 0156/1533] =?UTF-8?q?update=20=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0474.\344\270\200\345\222\214\351\233\266.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 8d79d701ac..6a178a254b 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -156,6 +156,11 @@ public: }; ``` +* 时间复杂度: O(kmn),k 为strs的长度 +* 空间复杂度: O(mn) + + + ## 总结 不少同学刷过这道题,可能没有总结这究竟是什么背包。 From f701133a9a8b418904cf02583e90d3c7fcbdd743 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:17:04 +0800 Subject: [PATCH 0157/1533] =?UTF-8?q?update=20=20=200518.=E9=9B=B6?= =?UTF-8?q?=E9=92=B1=E5=85=91=E6=8D=A2II=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...18.\351\233\266\351\222\261\345\205\221\346\215\242II.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index eb5a844ce4..c208754f77 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -179,6 +179,11 @@ public: } }; ``` + +* 时间复杂度: O(mn),其中 m 是amount,n 是 coins 的长度 +* 空间复杂度: O(m) + + 是不是发现代码如此精简,哈哈 ## 总结 From 6e62049cd3f06e5bf156240087d90bdfb5910569 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:20:07 +0800 Subject: [PATCH 0158/1533] =?UTF-8?q?update=20=200377.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C=E2=85=A3=20=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\204\345\220\210\346\200\273\345\222\214\342\205\243.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index f228718857..ee65972371 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -130,6 +130,11 @@ public: ``` +* 时间复杂度: O(target * n),其中 n 为 nums 的长度 +* 空间复杂度: O(target) + + + C++测试用例有两个数相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 但java就不用考虑这个限制,java里的int也是四个字节吧,也有可能leetcode后台对不同语言的测试数据不一样。 From 84b84e5bf11158c3ab4ac2356c8290060b7ebf45 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:46:51 +0800 Subject: [PATCH 0159/1533] =?UTF-8?q?update=20=20=200070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88?= =?UTF-8?q?=E6=9C=AC=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\250\350\203\214\345\214\205\347\211\210\346\234\254.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 41c2e6162d..8f8bc9a649 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -101,6 +101,11 @@ public: }; ``` +* 时间复杂度: O(nm) +* 空间复杂度: O(n) + + + 代码中m表示最多可以爬m个台阶,代码中把m改成2就是本题70.爬楼梯可以AC的代码了。 ## 总结 From 9fef5cbd00bde88126d4407c913d4b5f448d5308 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:51:41 +0800 Subject: [PATCH 0160/1533] =?UTF-8?q?=20update=20=20=200322.=E9=9B=B6?= =?UTF-8?q?=E9=92=B1=E5=85=91=E6=8D=A2=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22.\351\233\266\351\222\261\345\205\221\346\215\242.md" | 7 +++++++ 1 file changed, 7 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 3be72565be..0e3947dad3 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" @@ -133,6 +133,11 @@ public: }; ``` +* 时间复杂度: O(n * amount),其中 n 为 coins 的长度 +* 空间复杂度: O(amount) + + + 对于遍历方式遍历背包放在外循环,遍历物品放在内循环也是可以的,我就直接给出代码了 ```CPP @@ -154,6 +159,8 @@ public: } }; ``` +* 同上 + ## 总结 From b6fcdc160d85cd5d541c851f514e7ea332ffd4b4 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 11:57:47 +0800 Subject: [PATCH 0161/1533] =?UTF-8?q?=20update=20=20=200279.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E5=B9=B3=E6=96=B9=E6=95=B0=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\214\345\205\250\345\271\263\346\226\271\346\225\260.md" | 6 ++++++ 1 file changed, 6 insertions(+) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index c329156b83..f5b23d2636 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -127,6 +127,10 @@ public: }; ``` +* 时间复杂度: O(n * √n) +* 空间复杂度: O(n) + + 同样我在给出先遍历物品,在遍历背包的代码,一样的可以AC的。 ```CPP @@ -145,6 +149,8 @@ public: } }; ``` +* 同上 + ## 总结 From ecf8251c6ad8c3524ebbe282a62a682acc91824b Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 13:58:53 +0800 Subject: [PATCH 0162/1533] =?UTF-8?q?update=20=20=200198.=E6=89=93?= =?UTF-8?q?=E5=AE=B6=E5=8A=AB=E8=88=8D=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index fdb2dabf5f..c25f3b8677 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -108,6 +108,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 总结 打家劫舍是DP解决的经典题目,这道题也是打家劫舍入门级题目,后面我们还会变种方式来打劫的。 From 6754a955688dd80870593da89fa26d889074e748 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:00:01 +0800 Subject: [PATCH 0163/1533] =?UTF-8?q?update=20=200213.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DII=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13.\346\211\223\345\256\266\345\212\253\350\210\215II.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index 0627eedb23..becad06920 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -82,6 +82,11 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + + + ## 总结 成环之后还是难了一些的, 不少题解没有把“考虑房间”和“偷房间”说清楚。 From 56b35274356fe0cecda151e5d1aa77a9f8b750b3 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:07:01 +0800 Subject: [PATCH 0164/1533] =?UTF-8?q?update=20=200188.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?IV=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 4fdd7bf40c..f6744a2b36 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -156,6 +156,11 @@ public: }; ``` +* 时间复杂度: O(n * k),其中 n 为 prices 的长度 +* 空间复杂度: O(n * k) + + + 当然有的解法是定义一个三维数组dp[i][j][k],第i天,第j次买卖,k表示买还是卖的状态,从定义上来讲是比较直观。 但感觉三维数组操作起来有些麻烦,我是直接用二维数组来模拟三维数组的情况,代码看起来也清爽一些。 From 5bc0fa5c96684068bec235ca353e297b199f5414 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:08:42 +0800 Subject: [PATCH 0165/1533] =?UTF-8?q?=20update=20=20=200300.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\212\345\215\207\345\255\220\345\272\217\345\210\227.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 478837cc1b..e8cb0b5f18 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -106,6 +106,10 @@ public: } }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n) + + ## 总结 From 1ed179e233bc18574a3f4e7b46cbbed9ab686667 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:11:04 +0800 Subject: [PATCH 0166/1533] =?UTF-8?q?update=20=20=201143.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...05\254\345\205\261\345\255\220\345\272\217\345\210\227.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 8e5f7eb252..730e9ad1f7 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -124,6 +124,10 @@ public: } }; ``` +* 时间复杂度: O(n * m),其中 n 和 m 分别为 text1 和 text2 的长度 +* 空间复杂度: O(n * m) + + ## 其他语言版本 From 4ded4b5c820f3098ab54a3dbd6cda23e0c5e70ff Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:12:41 +0800 Subject: [PATCH 0167/1533] =?UTF-8?q?update=20=20=201035.=E4=B8=8D?= =?UTF-8?q?=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\215\347\233\270\344\272\244\347\232\204\347\272\277.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index d1e1e30a8c..7b60abdda3 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -62,6 +62,10 @@ public: } }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 总结 From f579a9f82e89f18338b7af64f94e76a63fa7f96d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:14:03 +0800 Subject: [PATCH 0168/1533] =?UTF-8?q?update=20=20=200115.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\214\347\232\204\345\255\220\345\272\217\345\210\227.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index daa708bc84..6127f19045 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -149,6 +149,11 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + + ## 其他语言版本 From 7d6476c16a421ef03c56ee101e1fd16b1d47ea9c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:15:40 +0800 Subject: [PATCH 0169/1533] =?UTF-8?q?update=20=200583.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\210\240\351\231\244\346\223\215\344\275\234.md" | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index eb046a9d04..561ad2f238 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -104,6 +104,10 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ### 动态规划二 @@ -127,6 +131,10 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 其他语言版本 From 541ac6ef3cfe7da2312df2cedf9df63edce2b325 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:16:43 +0800 Subject: [PATCH 0170/1533] =?UTF-8?q?update=20=20=200072.=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E8=B7=9D=E7=A6=BB=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 0b49e3c656..cc4ab00cdc 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -218,6 +218,10 @@ public: } }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 其他语言版本 From a26fe0fdf4559c535ac3f04d4e43e87b86b01196 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Tue, 11 Apr 2023 14:17:50 +0800 Subject: [PATCH 0171/1533] =?UTF-8?q?update=20=200516.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97=20=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\236\346\226\207\345\255\220\345\272\217\345\210\227.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index d28a33ccd0..fcdd57b0f4 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -144,6 +144,11 @@ public: } }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n^2) + + + ## 其他语言版本 From 8f58ab445a4f7a50a0a9d3ab9c2041dba0c1f2d9 Mon Sep 17 00:00:00 2001 From: Erincrying <1016158928@qq.com> Date: Tue, 11 Apr 2023 20:20:33 +0800 Subject: [PATCH 0172/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?=EF=BC=8Cjs=E6=96=B9=E6=B3=95=EF=BC=8C=E9=87=87=E7=94=A8map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\257\215\345\274\202\344\275\215\350\257\215.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 1006ea3552..ba802bbdbb 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -205,6 +205,19 @@ var isAnagram = function(s, t) { } return true; }; + +var isAnagram = function(s, t) { + if(s.length !== t.length) return false; + let char_count = new Map(); + for(let item of s) { + char_count.set(item, (char_count.get(item) || 0) + 1) ; + } + for(let item of t) { + if(!char_count.get(item)) return false; + char_count.set(item, char_count.get(item)-1); + } + return true; +}; ``` TypeScript: From eef44eb9e82c6f8c605e4e2575a3200dafe0014d Mon Sep 17 00:00:00 2001 From: Erincrying <1016158928@qq.com> Date: Tue, 11 Apr 2023 21:26:44 +0800 Subject: [PATCH 0173/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B01002.=20=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6=EF=BC=8Cjs?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E9=87=87=E7=94=A8map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\347\224\250\345\255\227\347\254\246.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index fd18866029..9ec3c6c4e8 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -252,6 +252,36 @@ var commonChars = function (words) { } return res }; + +// 方法二:map() +var commonChars = function(words) { + let min_count = new Map() + // 统计字符串中字符出现的最小频率,以第一个字符串初始化 + for(let str of words[0]) { + min_count.set(str, ((min_count.get(str) || 0) + 1)) + } + // 从第二个单词开始统计字符出现次数 + for(let i = 1; i < words.length; i++) { + let char_count = new Map() + for(let str of words[i]) { // 遍历字母 + char_count.set(str, (char_count.get(str) || 0) + 1) + } + // 比较出最小的字符次数 + for(let value of min_count) { // 注意这里遍历min_count!而不是单词 + min_count.set(value[0], Math.min((min_count.get(value[0]) || 0), (char_count.get(value[0]) || 0))) + } + } + // 遍历map + let res = [] + min_count.forEach((value, key) => { + if(value) { + for(let i=0; i Date: Thu, 13 Apr 2023 12:20:18 +0800 Subject: [PATCH 0174/1533] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\210\254\346\245\274\346\242\257.md" | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index d7258d4576..98a58c1256 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -351,17 +351,29 @@ function minCostClimbingStairs(cost: number[]): number { ### Rust ```Rust -use std::cmp::min; impl Solution { pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { - let len = cost.len(); - let mut dp = vec![0; len]; - dp[0] = cost[0]; - dp[1] = cost[1]; - for i in 2..len { - dp[i] = min(dp[i-1], dp[i-2]) + cost[i]; + let mut dp = vec![0; cost.len() + 1]; + for i in 2..=cost.len() { + dp[i] = (dp[i - 1] + cost[i - 1]).min(dp[i - 2] + cost[i - 2]); + } + dp[cost.len()] + } +} +``` + +不使用 dp 数组 + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let (mut dp_before, mut dp_after) = (0, 0); + for i in 2..=cost.len() { + let dpi = (dp_before + cost[i - 2]).min(dp_after + cost[i - 1]); + dp_before = dp_after; + dp_after = dpi; } - min(dp[len-1], dp[len-2]) + dp_after } } ``` From e9c4d54f537a1c42f71b5651bfc4baa0cd1a51e8 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 12:28:51 +0800 Subject: [PATCH 0175/1533] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\210\254\346\245\274\346\242\257.md" | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 98a58c1256..3d01485874 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -330,22 +330,27 @@ var minCostClimbingStairs = function(cost) { ```typescript function minCostClimbingStairs(cost: number[]): number { - /** - dp[i]: 走到第i阶需要花费的最少金钱 - dp[0]: 0; - dp[1]: 0; - ... - dp[i]: min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); - */ - const dp = []; - const length = cost.length; - dp[0] = 0; - dp[1] = 0; - for (let i = 2; i <= length; i++) { - dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); - } - return dp[length]; -}; + const dp = [0, 0] + for (let i = 2; i <= cost.length; i++) { + dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + } + return dp[cost.length] +} +``` + +不使用 dp 数组 + +```typescript +function minCostClimbingStairs(cost: number[]): number { + let dp_before = 0, + dp_after = 0 + for (let i = 2; i <= cost.length; i++) { + let dpi = Math.min(dp_before + cost[i - 2], dp_after + cost[i - 1]) + dp_before = dp_after + dp_after = dpi + } + return dp_after +} ``` ### Rust From 1ad26f69cdffc20bec0c95669e929dab0f7d3b2c Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 12:37:03 +0800 Subject: [PATCH 0176/1533] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\210\254\346\245\274\346\242\257.md" | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 3d01485874..fb5261f321 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -312,17 +312,30 @@ func min(a, b int) int { ``` -### Javascript +### JavaScript + ```Javascript var minCostClimbingStairs = function(cost) { - const n = cost.length; - const dp = new Array(n + 1); - dp[0] = dp[1] = 0; - for (let i = 2; i <= n; ++i) { - dp[i] = Math.min(dp[i -1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + const dp = [0, 0] + for (let i = 2; i <= cost.length; ++i) { + dp[i] = Math.min(dp[i -1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + } + return dp[cost.length] +}; +``` + +不使用 dp 数组 + +```JavaScript +var minCostClimbingStairs = function(cost) { + let dpBefore = 0, + dpAfter = 0 + for(let i = 2;i <= cost.length;i++){ + let dpi = Math.min(dpBefore + cost[i - 2],dpAfter + cost[i - 1]) + dpBefore = dpAfter + dpAfter = dpi } - - return dp[n] + return dpAfter }; ``` @@ -342,14 +355,14 @@ function minCostClimbingStairs(cost: number[]): number { ```typescript function minCostClimbingStairs(cost: number[]): number { - let dp_before = 0, - dp_after = 0 + let dpBefore = 0, + dpAfter = 0 for (let i = 2; i <= cost.length; i++) { - let dpi = Math.min(dp_before + cost[i - 2], dp_after + cost[i - 1]) - dp_before = dp_after - dp_after = dpi + let dpi = Math.min(dpBefore + cost[i - 2], dpAfter + cost[i - 1]) + dpBefore = dpAfter + dpAfter = dpi } - return dp_after + return dpAfter } ``` From ac16522a48d90d45d2f87f50a2c336fc383f4134 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 12:37:46 +0800 Subject: [PATCH 0177/1533] =?UTF-8?q?Update=20problems/0746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index fb5261f321..561441fc4c 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -314,7 +314,7 @@ func min(a, b int) int { ### JavaScript -```Javascript +```JavaScript var minCostClimbingStairs = function(cost) { const dp = [0, 0] for (let i = 2; i <= cost.length; ++i) { From 057d6b8f89ea5bbef1bb460ce40d9093abd1c3e0 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 13:34:46 +0800 Subject: [PATCH 0178/1533] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\210\254\346\245\274\346\242\257.md" | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 561441fc4c..f11439c03c 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -399,18 +399,29 @@ impl Solution { ### C ```c -int minCostClimbingStairs(int* cost, int costSize){ - //开辟dp数组,大小为costSize - int *dp = (int *)malloc(sizeof(int) * costSize); - //初始化dp[0] = cost[0], dp[1] = cost[1] - dp[0] = cost[0], dp[1] = cost[1]; - - int i; - for(i = 2; i < costSize; ++i) { - dp[i] = (dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]) + cost[i]; - } - //选出倒数2层楼梯中较小的 - return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]; +#include +int minCostClimbingStairs(int *cost, int costSize) { + int dp[costSize + 1]; + dp[0] = dp[1] = 0; + for (int i = 2; i <= costSize; i++) { + dp[i] = fmin(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]); + } + return dp[costSize]; +} +``` + +不使用 dp 数组 + +```c +#include +int minCostClimbingStairs(int *cost, int costSize) { + int dpBefore = 0, dpAfter = 0; + for (int i = 2; i <= costSize; i++) { + int dpi = fmin(dpBefore + cost[i - 2], dpAfter + cost[i - 1]); + dpBefore = dpAfter; + dpAfter = dpi; + } + return dpAfter; } ``` From f7680ab01d27b57e97dfe85796ac7c3c28b06d41 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 17:01:45 +0800 Subject: [PATCH 0179/1533] =?UTF-8?q?Update=200062.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\220\214\350\267\257\345\276\204.md" | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index bf4363692b..2ca157263e 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -395,21 +395,14 @@ function uniquePaths(m: number, n: number): number { ```Rust impl Solution { pub fn unique_paths(m: i32, n: i32) -> i32 { - let m = m as usize; - let n = n as usize; - let mut dp = vec![vec![0; n]; m]; - for i in 0..m { - dp[i][0] = 1; - } - for j in 0..n { - dp[0][j] = 1; - } - for i in 1..m { - for j in 1..n { - dp[i][j] = dp[i-1][j] + dp[i][j-1]; - } + let (m, n) = (m as usize, n as usize); + let mut dp = vec![vec![1; n]; m]; + for i in 1..m { + for j in 1..n { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } - dp[m-1][n-1] + } + dp[m - 1][n - 1] } } ``` From 872402e5a4e2e227e889fd3103786c503f864abd Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 18:05:44 +0800 Subject: [PATCH 0180/1533] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\214\350\267\257\345\276\204II.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 85130ab4e4..8ab90e6d4f 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -493,6 +493,34 @@ impl Solution { } ``` +空间优化: + +```rust +impl Solution { + pub fn unique_paths_with_obstacles(obstacle_grid: Vec>) -> i32 { + let mut dp = vec![0; obstacle_grid[0].len()]; + for (i, &v) in obstacle_grid[0].iter().enumerate() { + if v == 0 { + dp[i] = 1; + } else { + break; + } + } + for rows in obstacle_grid.iter().skip(1) { + for j in 0..rows.len() { + if rows[j] == 1 { + dp[j] = 0; + continue; + } else if j != 0 { + dp[j] += dp[j - 1]; + } + } + } + dp.pop().unwrap() + } +} +``` + ### C ```c From ced650c7bb9d43447e8445fff31469455440057e Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 18:06:59 +0800 Subject: [PATCH 0181/1533] =?UTF-8?q?Update=20problems/0063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" | 1 - 1 file changed, 1 deletion(-) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 8ab90e6d4f..8ff8c33f15 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -510,7 +510,6 @@ impl Solution { for j in 0..rows.len() { if rows[j] == 1 { dp[j] = 0; - continue; } else if j != 0 { dp[j] += dp[j - 1]; } From bd22ad9257b90dbeb2db60f5cdd6c591f6288e37 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 18:51:26 +0800 Subject: [PATCH 0182/1533] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\225\260\346\213\206\345\210\206.md" | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index c2fbbdde68..4df6c41f4d 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -319,6 +319,29 @@ pub fn integer_break(n: i32) -> i32 { } ``` +贪心: + +```rust +impl Solution { + pub fn integer_break(mut n: i32) -> i32 { + match n { + 2 => 1, + 3 => 2, + 4 => 4, + 5.. => { + let mut res = 1; + while n > 4 { + res *= 3; + n -= 3; + } + res * n + } + _ => panic!("Error"), + } + } +} +``` + ### TypeScript ```typescript @@ -344,27 +367,6 @@ function integerBreak(n: number): number { }; ``` -### Rust - -```Rust -impl Solution { - fn max(a: i32, b: i32) -> i32{ - if a > b { a } else { b } - } - pub fn integer_break(n: i32) -> i32 { - let n = n as usize; - let mut dp = vec![0; n + 1]; - dp[2] = 1; - for i in 3..=n { - for j in 1..i - 1 { - dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32)); - } - } - dp[n] - } -} -``` - ### C ```c From 7111943ac00f9cea2c4899f0211b2d0fcadeef62 Mon Sep 17 00:00:00 2001 From: BanTanger <88583317+BanTanger@users.noreply.github.com> Date: Thu, 13 Apr 2023 22:18:21 +0800 Subject: [PATCH 0183/1533] =?UTF-8?q?Update=200347.=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前的 java 代码调用的 api 过于难记忆,不利于白板书写,于是将优先级队列存储数据结构从 map.setEntry 改为 int[] 数组,方便大家记忆理解书写 --- ...30\351\242\221\345\205\203\347\264\240.md" | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 0d268d9bdc..6c8b51b1ad 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -188,7 +188,33 @@ class Solution { } } ``` - +简化版代码: +```java +class Solution { + public int[] topKFrequent(int[] nums, int k) { + // 优先级队列,为了避免复杂 api 操作,pq 存储数组 + // lambda 表达式设置优先级队列从大到小存储 o1 - o2 为从大到小,o2 - o1 反之 + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]); + int[] res = new int[k]; // 答案数组为 k 个元素 + Map map = new HashMap<>(); // 记录元素出现次数 + for(int num : nums) map.put(num, map.getOrDefault(num, 0) + 1); + for(var x : map.entrySet()) { // entrySet 获取 k-v Set 集合 + // 将 kv 转化成数组 + int[] tmp = new int[2]; + tmp[0] = x.getKey(); + tmp[1] = x.getValue(); + pq.offer(tmp); + if(pq.size() > k) { + pq.poll(); + } + } + for(int i = 0; i < k; i ++) { + res[i] = pq.poll()[0]; // 获取优先队列里的元素 + } + return res; + } +} +``` Python: ```python From 341b0aa672e9ca2d8a5b1cea2bc5ba307e795eda Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 17 Apr 2023 10:37:39 +0800 Subject: [PATCH 0184/1533] =?UTF-8?q?Update=20problems/0746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index f11439c03c..31e7bd48c4 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -318,7 +318,7 @@ func min(a, b int) int { var minCostClimbingStairs = function(cost) { const dp = [0, 0] for (let i = 2; i <= cost.length; ++i) { - dp[i] = Math.min(dp[i -1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) } return dp[cost.length] }; From 9aaf02cc20156d385853f0d3f8c9eebc983bc9d7 Mon Sep 17 00:00:00 2001 From: sharky <1821984081@qq.com> Date: Tue, 18 Apr 2023 15:15:28 +0800 Subject: [PATCH 0185/1533] =?UTF-8?q?1005=E3=80=81K=E6=AC=A1=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8C=EF=BC=8C=E6=9A=B4=E5=8A=9B=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=EF=BC=8Cjava?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204\346\225\260\347\273\204\345\222\214.md" | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index cdf42511b1..18b07b890e 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -145,7 +145,23 @@ class Solution { } } ``` - +```java +//暴力解法 +class Solution { + public int largestSumAfterKNegations(int[] nums, int k) { + int count = 0; + //循环k次 + for( int i = 0; i < k; i++ ){ + Arrays.sort( nums ); + nums[0] = -nums[0];//把最小值换成其相反数 + } + for ( int num : nums ){ + count += num;//累加 + } + return count; + } +} +``` ### Python ```python class Solution: From b7ea55c93b68391ecd87c39c06e2811f0ed6b98f Mon Sep 17 00:00:00 2001 From: Winson Huang Date: Tue, 18 Apr 2023 21:07:54 +0800 Subject: [PATCH 0186/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 Java 语言版本代码,让 HashMap 相关操作更简洁 --- ...33\233\346\225\260\347\233\270\345\212\240II.md" | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 411b60e872..abfc7c238f 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -102,21 +102,14 @@ class Solution { //统计两个数组中的元素之和,同时统计出现的次数,放入map for (int i : nums1) { for (int j : nums2) { - int tmp = map.getOrDefault(i + j, 0); - if (tmp == 0) { - map.put(i + j, 1); - } else { - map.replace(i + j, tmp + 1); - } + int sum = i + j; + map.put(sum, map.getOrDefault(sum, 0) + 1); } } //统计剩余的两个元素的和,在map中找是否存在相加为0的情况,同时记录次数 for (int i : nums3) { for (int j : nums4) { - int tmp = map.getOrDefault(0 - i - j, 0); - if (tmp != 0) { - res += tmp; - } + res += map.getOrDefault(0 - i - j, 0); } } return res; From 747563607292b4fc84452c4f45c2d0504531a171 Mon Sep 17 00:00:00 2001 From: Winson Huang Date: Tue, 18 Apr 2023 21:19:42 +0800 Subject: [PATCH 0187/1533] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为 Java 语言版本的代码添加长度判断 --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index d9a184b643..a3f87d4afa 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -117,6 +117,10 @@ Java: ```Java class Solution { public boolean canConstruct(String ransomNote, String magazine) { + // shortcut + if (ransomNote.length() > magazine.length()) { + return false; + } // 定义一个哈希映射数组 int[] record = new int[26]; From 3f2a816f3096c6ccccba41c81b3975ca80f6d081 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 18 Apr 2023 18:23:24 -0400 Subject: [PATCH 0188/1533] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java 統一迭代法 --- ...57\345\276\204\346\200\273\345\222\214.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 5958de93e6..b145788794 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -385,6 +385,42 @@ class solution { } } ``` +```Java 統一迭代法 + public boolean hasPathSum(TreeNode root, int targetSum) { + Stack treeNodeStack = new Stack<>(); + Stack sumStack = new Stack<>(); + + if(root == null) + return false; + treeNodeStack.add(root); + sumStack.add(root.val); + + while(!treeNodeStack.isEmpty()){ + TreeNode curr = treeNodeStack.peek(); + int tempsum = sumStack.pop(); + if(curr != null){ + treeNodeStack.pop(); + treeNodeStack.add(curr); + treeNodeStack.add(null); + sumStack.add(tempsum); + if(curr.right != null){ + treeNodeStack.add(curr.right); + sumStack.add(tempsum + curr.right.val); + } + if(curr.left != null){ + treeNodeStack.add(curr.left); + sumStack.add(tempsum + curr.left.val); + } + }else{ + treeNodeStack.pop(); + TreeNode temp = treeNodeStack.pop(); + if(temp.left == null && temp.right == null && tempsum == targetSum) + return true; + } + } + return false; + } +``` ### 0113.路径总和-ii From 0cfe55180439c77995abefaa02c5b61108203f3a Mon Sep 17 00:00:00 2001 From: asxy <17375702582@163.com> Date: Thu, 20 Apr 2023 11:14:43 +0800 Subject: [PATCH 0189/1533] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 回文子串添加java动态规划简单版本的代码 --- ...36\346\226\207\345\255\220\344\270\262.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 90e6da9fbf..521c8c268e 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -267,6 +267,27 @@ class Solution { return ans; } } + +``` + +动态规划:简洁版 +```java +class Solution { + public int countSubstrings(String s) { + boolean[][] dp = new boolean[s.length()][s.length()]; + + int res = 0; + for (int i = s.length() - 1; i >= 0; i--) { + for (int j = i; j < s.length(); j++) { + if (s.charAt(i) == s.charAt(j) && (j - i <= 1 || dp[i + 1][j - 1])) { + res++; + dp[i][j] = true; + } + } + } + return res; + } +} ``` 中心扩散法: From fca305039005936bc9678c01d7573d6a2f8552ba Mon Sep 17 00:00:00 2001 From: asxy Date: Thu, 20 Apr 2023 11:47:39 +0800 Subject: [PATCH 0190/1533] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加单调栈精简java代码 --- ...47\347\232\204\347\237\251\345\275\242.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index eb06414334..f9a8350818 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -307,6 +307,33 @@ class Solution { } } ``` +单调栈精简 +```java +class Solution { + public int largestRectangleArea(int[] heights) { + int[] newHeight = new int[heights.length + 2]; + System.arraycopy(heights, 0, newHeight, 1, heights.length); + newHeight[heights.length+1] = 0; + newHeight[0] = 0; + + Stack stack = new Stack<>(); + stack.push(0); + + int res = 0; + for (int i = 1; i < newHeight.length; i++) { + while (newHeight[i] < newHeight[stack.peek()]) { + int mid = stack.pop(); + int w = i - stack.peek() - 1; + int h = newHeight[mid]; + res = Math.max(res, w * h); + } + stack.push(i); + + } + return res; + } +} +``` Python3: From f7bdc58b2802e8b81483a5c2f8023499a3d525c4 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:18:03 -0500 Subject: [PATCH 0191/1533] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 python 代码,dummy node --- ...76\350\256\241\351\223\276\350\241\250.md" | 71 ++++++++++--------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 5c78a12a7f..c72b73277a 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -486,15 +486,10 @@ class MyLinkedList { Python: ```python # 单链表 -class Node(object): - def __init__(self, x=0): - self.val = x - self.next = None - -class MyLinkedList(object): +class MyLinkedList1: def __init__(self): - self.head = Node() + self.dummy_head = Node()# 添加虚拟头指针,便于操作 self.size = 0 # 设置一个链表长度的属性,便于后续操作,注意每次增和删的时候都要更新 def get(self, index): @@ -504,7 +499,7 @@ class MyLinkedList(object): """ if index < 0 or index >= self.size: return -1 - cur = self.head.next + cur = self.dummy_head.next while(index): cur = cur.next index -= 1 @@ -516,8 +511,8 @@ class MyLinkedList(object): :rtype: None """ new_node = Node(val) - new_node.next = self.head.next - self.head.next = new_node + new_node.next = self.dummy_head.next + self.dummy_head.next = new_node self.size += 1 def addAtTail(self, val): @@ -526,7 +521,7 @@ class MyLinkedList(object): :rtype: None """ new_node = Node(val) - cur = self.head + cur = self.dummy_head while(cur.next): cur = cur.next cur.next = new_node @@ -548,12 +543,12 @@ class MyLinkedList(object): return node = Node(val) - pre = self.head + cur = self.dummy_head while(index): - pre = pre.next + cur = cur.next index -= 1 - node.next = pre.next - pre.next = node + node.next = cur.next + cur.next = node self.size += 1 def deleteAtIndex(self, index): @@ -563,7 +558,7 @@ class MyLinkedList(object): """ if index < 0 or index >= self.size: return - pre = self.head + pre = self.dummy_head while(index): pre = pre.next index -= 1 @@ -574,11 +569,10 @@ class MyLinkedList(object): # 相对于单链表, Node新增了prev属性 class Node: - def __init__(self, val): + def __init__(self, val=0, next = None, prev = None): self.val = val - self.prev = None - self.next = None - + self.next = next + self.prev = prev class MyLinkedList: @@ -601,6 +595,20 @@ class MyLinkedList: node = node.next return node + + def _update(self, prev: Node, next: Node, val: int) -> None: + """ + 更新节点 + :param prev: 相对于更新的前一个节点 + :param next: 相对于更新的后一个节点 + :param val: 要添加的节点值 + """ + # 计数累加 + self._count += 1 + node = Node(val) + prev.next, next.prev = node, node + node.prev, node.next = prev, next + def get(self, index: int) -> int: """ Get the value of the index-th node in the linked list. If the index is invalid, return -1. @@ -634,19 +642,6 @@ class MyLinkedList: node = self._get_node(index) self._update(node.prev, node, val) - def _update(self, prev: Node, next: Node, val: int) -> None: - """ - 更新节点 - :param prev: 相对于更新的前一个节点 - :param next: 相对于更新的后一个节点 - :param val: 要添加的节点值 - """ - # 计数累加 - self._count += 1 - node = Node(val) - prev.next, next.prev = node, node - node.prev, node.next = prev, next - def deleteAtIndex(self, index: int) -> None: """ Delete the index-th node in the linked list, if the index is valid. @@ -656,6 +651,16 @@ class MyLinkedList: # 计数-1 self._count -= 1 node.prev.next, node.next.prev = node.next, node.prev + + + +# Your MyLinkedList object will be instantiated and called as such: +# obj = MyLinkedList() +# param_1 = obj.get(index) +# obj.addAtHead(val) +# obj.addAtTail(val) +# obj.addAtIndex(index,val) +# obj.deleteAtIndex(index) ``` Go: From ad2acdb14ac6d633a0ad6e3b8f92d3e1f6c32881 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 20 Apr 2023 23:44:00 -0400 Subject: [PATCH 0192/1533] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add java iteration method for leetcode 113 (DFS统一迭代法) --- ...57\345\276\204\346\200\273\345\222\214.md" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 5958de93e6..d8ea0a18f0 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -446,6 +446,52 @@ class Solution { } } ``` +```java +// 解法3 DFS统一迭代法 +class Solution { + public List> pathSum(TreeNode root, int targetSum) { + List> result = new ArrayList<>(); + Stack nodeStack = new Stack<>(); + Stack sumStack = new Stack<>(); + Stack> pathStack = new Stack<>(); + if(root == null) + return result; + nodeStack.add(root); + sumStack.add(root.val); + pathStack.add(new ArrayList<>()); + + while(!nodeStack.isEmpty()){ + TreeNode currNode = nodeStack.peek(); + int currSum = sumStack.pop(); + ArrayList currPath = pathStack.pop(); + if(currNode != null){ + nodeStack.pop(); + nodeStack.add(currNode); + nodeStack.add(null); + sumStack.add(currSum); + currPath.add(currNode.val); + pathStack.add(new ArrayList(currPath)); + if(currNode.right != null){ + nodeStack.add(currNode.right); + sumStack.add(currSum + currNode.right.val); + pathStack.add(new ArrayList(currPath)); + } + if(currNode.left != null){ + nodeStack.add(currNode.left); + sumStack.add(currSum + currNode.left.val); + pathStack.add(new ArrayList(currPath)); + } + }else{ + nodeStack.pop(); + TreeNode temp = nodeStack.pop(); + if(temp.left == null && temp.right == null && currSum == targetSum) + result.add(new ArrayList(currPath)); + } + } + return result; + } +} +``` ## python From 7a41318056f7aa10ef7d4653dd1c3fb988b64448 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 21 Apr 2023 22:27:05 +0800 Subject: [PATCH 0193/1533] Update --- .../0047.\345\205\250\346\216\222\345\210\227II.md" | 13 +++++++++++++ ...\267\263\350\267\203\346\270\270\346\210\217.md" | 2 +- ...\220\210\345\271\266\345\214\272\351\227\264.md" | 1 - ...\267\257\345\276\204\346\200\273\345\222\214.md" | 2 +- ...\210\206\345\217\221\351\245\274\345\271\262.md" | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index b4f7a4d8cb..b1908fb491 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -158,6 +158,19 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { 所以我通过举[1,1,1]的例子,把这两个去重的逻辑分别抽象成树形结构,大家可以一目了然:为什么两种写法都可以以及哪一种效率更高! +这里可能大家又有疑惑,既然 `used[i - 1] == false`也行而`used[i - 1] == true`也行,那为什么还要写这个条件呢? + +直接这样写 不就完事了? + +```cpp +if (i > 0 && nums[i] == nums[i - 1]) { + continue; +} +``` + +其实并不行,一定要加上 `used[i - 1] == false`或者`used[i - 1] == true`,因为 used[i - 1] 要一直是 true 或者一直是false 才可以,而不是 一会是true 一会又是false。 所以这个条件要写上。 + + 是不是豁然开朗了!! ## 其他语言版本 diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index a898263de9..fa76bc2737 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -46,8 +46,8 @@ 如图: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203105634.png) -![55.跳跃游戏](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124154758229-20230310135019977.png) i每次移动只能在cover的范围内移动,每移动一个元素,cover得到该元素数值(新的覆盖范围)的补充,让i继续移动下去。 diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index d467ab1a29..08a3cb319f 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -106,7 +106,6 @@ class Solution { } } -} ``` ```java // 版本2 diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index e412d38e4e..43a623b5e3 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -17,7 +17,7 @@ 示例: 给定如下二叉树,以及目标和 sum = 22, -![112.路径总和1](https://img-blog.csdnimg.cn/20210203160355234.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230407210247.png) 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 63525b0394..e525175b9d 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -44,7 +44,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203105634.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230405225628.png) 这个例子可以看出饼干9只有喂给胃口为7的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。 From 252e330a6b20fa13e81e4412c91f40547e0ba1b1 Mon Sep 17 00:00:00 2001 From: HOUSHENGREN <48871516+HOUSHENGREN@users.noreply.github.com> Date: Sun, 23 Apr 2023 14:36:27 +0800 Subject: [PATCH 0194/1533] =?UTF-8?q?Update=20=E5=88=B7=E5=8A=9B=E6=89=A3?= =?UTF-8?q?=E7=94=A8=E4=B8=8D=E7=94=A8=E5=BA=93=E5=87=BD=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix:文字错误 --- ...\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" "b/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" index ae0940bfdc..04fce85611 100644 --- "a/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" +++ "b/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" @@ -24,5 +24,5 @@ 例如for循环里套一个字符串的insert,erase之类的操作,你说时间复杂度是多少呢,很明显是O(n^2)的时间复杂度了。 -在刷题的时候本着我说的标准来使用库函数,详细对大家回有所帮助! +在刷题的时候本着我说的标准来使用库函数,相信对大家回有所帮助! From b8f6df60c432fd85d9417e0ce138a90b4603527e Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Sun, 23 Apr 2023 02:40:34 -0400 Subject: [PATCH 0195/1533] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增一java寫法 和卡哥的思路一樣的 --- ...40\344\272\214\345\217\211\346\240\221.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index adb374f957..8fc973e0f0 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -622,7 +622,42 @@ class Solution { } } ``` +```java +class Solution { + public TreeNode buildTree(int[] inorder, int[] postorder) { + if(postorder.length == 0 || inorder.length == 0) + return null; + return buildHelper(inorder, 0, inorder.length, postorder, 0, postorder.length); + + } + private TreeNode buildHelper(int[] inorder, int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd){ + if(postorderStart == postorderEnd) + return null; + int rootVal = postorder[postorderEnd - 1]; + TreeNode root = new TreeNode(rootVal); + int middleIndex; + for (middleIndex = inorderStart; middleIndex < inorderEnd; middleIndex++){ + if(inorder[middleIndex] == rootVal) + break; + } + int leftInorderStart = inorderStart; + int leftInorderEnd = middleIndex; + int rightInorderStart = middleIndex + 1; + int rightInorderEnd = inorderEnd; + + + int leftPostorderStart = postorderStart; + int leftPostorderEnd = postorderStart + (middleIndex - inorderStart); + int rightPostorderStart = leftPostorderEnd; + int rightPostorderEnd = postorderEnd - 1; + root.left = buildHelper(inorder, leftInorderStart, leftInorderEnd, postorder, leftPostorderStart, leftPostorderEnd); + root.right = buildHelper(inorder, rightInorderStart, rightInorderEnd, postorder, rightPostorderStart, rightPostorderEnd); + + return root; + } +} +``` 105.从前序与中序遍历序列构造二叉树 ```java From 9558af957bd2077773a11fb4ebae7e432838c08a Mon Sep 17 00:00:00 2001 From: Charlie <1753524606@qq.com> Date: Sun, 23 Apr 2023 14:55:28 +0800 Subject: [PATCH 0196/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200028.=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0strStr=20Rust=E7=89=88=E6=9C=AC=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0028.\345\256\236\347\216\260strStr.md" | 4 ---- 1 file changed, 4 deletions(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 263c168995..2a81db8842 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -1314,7 +1314,6 @@ impl Solution { pub fn str_str(haystack: String, needle: String) -> i32 { let (haystack_len, needle_len) = (haystack.len(), needle.len()); - if haystack_len == 0 { return 0; } if haystack_len < needle_len { return -1;} let (haystack, needle) = (haystack.chars().collect::>(), needle.chars().collect::>()); let mut next: Vec = vec![0; haystack_len]; @@ -1355,9 +1354,6 @@ impl Solution { next } pub fn str_str(haystack: String, needle: String) -> i32 { - if needle.is_empty() { - return 0; - } if haystack.len() < needle.len() { return -1; } From f17c74ceec294a90f864a1a9a3836a03b4a752bb Mon Sep 17 00:00:00 2001 From: HOUSHENGREN <48871516+HOUSHENGREN@users.noreply.github.com> Date: Sun, 23 Apr 2023 15:14:25 +0800 Subject: [PATCH 0197/1533] =?UTF-8?q?Update=20=E5=88=B7=E5=8A=9B=E6=89=A3?= =?UTF-8?q?=E7=94=A8=E4=B8=8D=E7=94=A8=E5=BA=93=E5=87=BD=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat:修改文案 --- ...\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" "b/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" index 04fce85611..7d0e34757b 100644 --- "a/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" +++ "b/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" @@ -24,5 +24,5 @@ 例如for循环里套一个字符串的insert,erase之类的操作,你说时间复杂度是多少呢,很明显是O(n^2)的时间复杂度了。 -在刷题的时候本着我说的标准来使用库函数,相信对大家回有所帮助! +在刷题的时候本着我说的标准来使用库函数,相信对大家会有所帮助! From ad871dae77cfed61609d3bffd14e2fd994ae7d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Sun, 23 Apr 2023 18:22:29 +0800 Subject: [PATCH 0198/1533] =?UTF-8?q?Update=200225.=E7=94=A8=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为使用两个 Queue 实现添加新方法,方法将q1作为主要的队列,其元素排列顺序和出栈顺序相同,q2仅作为临时放置,push方法中在加入元素时先将q1中的元素依次出栈压入q2,然后将新加入的元素压入q1,再将q2中的元素依次出栈压入q1,其他方法可直接使用Queue中已有的方法。 --- ...27\345\256\236\347\216\260\346\240\210.md" | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index bad2faec27..29ef093375 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -166,7 +166,7 @@ public: Java: -使用两个 Queue 实现 +使用两个 Queue 实现方法1 ```java class MyStack { @@ -208,6 +208,42 @@ class MyStack { } ``` +使用两个 Queue 实现方法2 +```java +class MyStack { + //q1作为主要的队列,其元素排列顺序和出栈顺序相同 + Queue q1 = new ArrayDeque<>(); + //q2仅作为临时放置 + Queue q2 = new ArrayDeque<>(); + + public MyStack() { + + } + //在加入元素时先将q1中的元素依次出栈压入q2,然后将新加入的元素压入q1,再将q2中的元素依次出栈压入q1 + public void push(int x) { + while (q1.size() > 0) { + q2.add(q1.poll()); + } + q1.add(x); + while (q2.size() > 0) { + q1.add(q2.poll()); + } + } + + public int pop() { + return q1.poll(); + } + + public int top() { + return q1.peek(); + } + + public boolean empty() { + return q1.isEmpty(); + } +} +``` + 使用两个 Deque 实现 ```java class MyStack { From dd290cf33ba44e857021985197122a2982d76874 Mon Sep 17 00:00:00 2001 From: Binbin Date: Mon, 24 Apr 2023 14:28:47 +0800 Subject: [PATCH 0199/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?.md=20=E9=87=8C=E7=9A=84=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后者 -> 或者 --- ...\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 36578fd343..96169b3268 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -38,7 +38,7 @@ 本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。 * 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始) -* 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始) +* 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始) **而根节点的高度就是二叉树的最大深度**,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。 From ce73cef055f17879a9ef8f58f8cabffd48ed7edb Mon Sep 17 00:00:00 2001 From: jimowo <1252480844@qq.com> Date: Mon, 24 Apr 2023 16:16:16 +0800 Subject: [PATCH 0200/1533] =?UTF-8?q?=E7=AE=97=E6=B3=95=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\345\256\266\345\212\253\350\210\215.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index c25f3b8677..6e7f5ab6c6 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -136,6 +136,29 @@ class Solution { return dp[nums.length - 1]; } } + +// 空间优化 dp数组只存与计算相关的两次数据 +class Solution { + public int rob(int[] nums) { + if (nums.length == 1) { + return nums[0]; + } + // 初始化dp数组 + // 优化空间 dp数组只用2格空间 只记录与当前计算相关的前两个结果 + int[] dp = new int[2]; + dp[0] = nums[0]; + dp[1] = nums[0] > nums[1] ? nums[0] : nums[1]; + int res = 0; + // 遍历 + for (int i = 2; i < nums.length; i++) { + res = (dp[0] + nums[i]) > dp[1] ? (dp[0] + nums[i]) : dp[1]; + dp[0] = dp[1]; + dp[1] = res; + } + // 输出结果 + return dp[1]; + } +} ``` Python: @@ -220,3 +243,4 @@ function rob(nums: number[]): number { + From bb8e7312b64b13f98ca6c6ae88260a4f57375acd Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 25 Apr 2023 03:25:45 -0400 Subject: [PATCH 0201/1533] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java 統一迭代法的寫法 有通過. AC --- ...11\346\220\234\347\264\242\346\240\221.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 95afe6805c..ccb6330558 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -259,6 +259,36 @@ public: ## Java +```Java +//使用統一迭代法 +class Solution { + public boolean isValidBST(TreeNode root) { + Stack stack = new Stack<>(); + TreeNode pre = null; + if(root != null) + stack.add(root); + while(!stack.isEmpty()){ + TreeNode curr = stack.peek(); + if(curr != null){ + stack.pop(); + if(curr.right != null) + stack.add(curr.right); + stack.add(curr); + stack.add(null); + if(curr.left != null) + stack.add(curr.left); + }else{ + stack.pop(); + TreeNode temp = stack.pop(); + if(pre != null && pre.val >= temp.val) + return false; + pre = temp; + } + } + return true; + } +} +``` ```Java class Solution { // 递归 From 75c7f940d112893dd6a1f89f844b710a87b202e1 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 25 Apr 2023 04:03:17 -0400 Subject: [PATCH 0202/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=20=E7=B5=B1?= =?UTF-8?q?=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95-=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java 統一迭代法-中序遍历 --- ...17\347\273\235\345\257\271\345\267\256.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index fa1430dec1..cd24c6faf1 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -174,6 +174,39 @@ class Solution { } } ``` +統一迭代法-中序遍历 +```Java +class Solution { + public int getMinimumDifference(TreeNode root) { + Stack stack = new Stack<>(); + TreeNode pre = null; + int result = Integer.MAX_VALUE; + + if(root != null) + stack.add(root); + while(!stack.isEmpty()){ + TreeNode curr = stack.peek(); + if(curr != null){ + stack.pop(); + if(curr.right != null) + stack.add(curr.right); + stack.add(curr); + stack.add(null); + if(curr.left != null) + stack.add(curr.left); + }else{ + stack.pop(); + TreeNode temp = stack.pop(); + if(pre != null) + result = Math.min(result, temp.val - pre.val); + pre = temp; + } + } + return result; + } +} +``` + 迭代法-中序遍历 ```java From c5c7dfb7fe0c0e7aa5c2115bed085d417d0726c0 Mon Sep 17 00:00:00 2001 From: zbb Date: Tue, 25 Apr 2023 19:45:48 +0800 Subject: [PATCH 0203/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200101.=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91.md=20=E9=87=8C=E7=9A=84?= =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\271\347\247\260\344\272\214\345\217\211\346\240\221.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index b75e9ff2da..1594196bc9 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -88,7 +88,7 @@ else if (left->val != right->val) return false; // 注意这里我没有 * 比较二叉树外侧是否对称:传入的是左节点的左孩子,右节点的右孩子。 -* 比较内测是否对称,传入左节点的右孩子,右节点的左孩子。 +* 比较内侧是否对称,传入左节点的右孩子,右节点的左孩子。 * 如果左右都对称就返回true ,有一侧不对称就返回false 。 代码如下: @@ -157,7 +157,7 @@ public: **这个代码就很简洁了,但隐藏了很多逻辑,条理不清晰,而且递归三部曲,在这里完全体现不出来。** -**所以建议大家做题的时候,一定要想清楚逻辑,每一步做什么。把道题目所有情况想到位,相应的代码写出来之后,再去追求简洁代码的效果。** +**所以建议大家做题的时候,一定要想清楚逻辑,每一步做什么。把题目所有情况想到位,相应的代码写出来之后,再去追求简洁代码的效果。** ## 迭代法 From 7851e51c7345590977d3881424354d6be0dd8eae Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 15:35:27 +0800 Subject: [PATCH 0204/1533] =?UTF-8?q?update=20=200077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 9c6d481ddb..3f222a17c2 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -218,6 +218,10 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) + + 还记得我们在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中给出的回溯法模板么? From e533cb9cd4a5048a933055d5dd02ad7dbf8a1d20 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 15:37:21 +0800 Subject: [PATCH 0205/1533] =?UTF-8?q?update=20=20=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index 9736549c57..0c816bc169 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -130,6 +130,10 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) + + # 总结 From 9a29e5c3be75185c6013aaa0cb252374fb80598d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 15:41:40 +0800 Subject: [PATCH 0206/1533] =?UTF-8?q?update=20=20=200216.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index f631c3cdc8..f08d77eac1 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -235,6 +235,8 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) # 总结 From 025854e1678ba1e77a271b35a60159c1ad92ffbb Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:02:29 +0800 Subject: [PATCH 0207/1533] =?UTF-8?q?update=20=20=200017.=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?= =?UTF-8?q?=E5=90=88=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index d113549716..d506bb889e 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -183,6 +183,8 @@ public: } }; ``` +* 时间复杂度: O(3^m * 4^n),其中 m 是对应四个字母的数字个数,n 是对应三个字母的数字个数 +* 空间复杂度: O(3^m * 4^n) 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方) From d3290c7e773f66514625e8d678ff7695ae3f0f99 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:07:21 +0800 Subject: [PATCH 0208/1533] =?UTF-8?q?update=20=20=200039.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index c4ee5ca6ca..e1e51923db 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -214,6 +214,8 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n),注意这只是复杂度的上界,因为剪枝的存在,真实的时间复杂度远小于此 +* 空间复杂度: O(target) # 总结 From d68a1f633e532621ebd87578ae7c8daae4e5c36c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:08:26 +0800 Subject: [PATCH 0209/1533] =?UTF-8?q?update=20=20=200040.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 83708df720..b708650ad0 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -214,6 +214,8 @@ public: }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) ## 补充 From 65f059ac7a7168a8848bb7106f1050ce28afa862 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:27:02 +0800 Subject: [PATCH 0210/1533] =?UTF-8?q?update=20=200131.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E4=B8=B2=20=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 30bba455fb..dfec7853d1 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -209,6 +209,9 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n^2) + # 优化 上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在: From 33d5a6878e73800cae191d98276729c1ebb77860 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:33:52 +0800 Subject: [PATCH 0211/1533] =?UTF-8?q?=20update=20=20=200078.=E5=AD=90?= =?UTF-8?q?=E9=9B=86=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0078.\345\255\220\351\233\206.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index f26d0821e9..07418fbc12 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -149,6 +149,8 @@ public: }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) 在注释中,可以发现可以不写终止条件,因为本来我们就要遍历整棵树。 From 3accc7602597a2d6c7af017326d8caf9cb30be16 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:34:55 +0800 Subject: [PATCH 0212/1533] =?UTF-8?q?update=20=20=200090.=E5=AD=90?= =?UTF-8?q?=E9=9B=86II=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0090.\345\255\220\351\233\206II.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 1a9f8fda3e..63f75d294f 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -83,6 +83,9 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) + 使用set去重的版本。 ```CPP From 09975d3d380c3151538270fe8cac25d39a476281 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:37:53 +0800 Subject: [PATCH 0213/1533] =?UTF-8?q?update=20=20=200491.=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 436dbf01c8..d6c6b9c956 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -139,6 +139,8 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) ## 优化 From 9d581e86fe858577263597ec5b0a1aa5dda24c2e Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:38:59 +0800 Subject: [PATCH 0214/1533] =?UTF-8?q?update=20=200046.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0046.\345\205\250\346\216\222\345\210\227.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index e08aec940b..fb70be4141 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -136,6 +136,8 @@ public: } }; ``` +* 时间复杂度: O(n!) +* 空间复杂度: O(n) ## 总结 From 1a9cb629ae87aa43a7eac4dea0a6732492a8a242 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:39:37 +0800 Subject: [PATCH 0215/1533] =?UTF-8?q?update=20=200047.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97II=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0047.\345\205\250\346\216\222\345\210\227II.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index b1908fb491..3ff3fb8fcd 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -99,6 +99,8 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) ## 拓展 From 2a7f4d5d4b4107c0aa85960e0e46adb56d665def Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:44:28 +0800 Subject: [PATCH 0216/1533] =?UTF-8?q?update=20=20=200051.N=E7=9A=87?= =?UTF-8?q?=E5=90=8E=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index bd1d1c9bec..54580cf76a 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -208,6 +208,9 @@ public: } }; ``` +* 时间复杂度: O(n!) +* 空间复杂度: O(n) + 可以看出,除了验证棋盘合法性的代码,省下来部分就是按照回溯法模板来的。 From 7f699b59167263645112138b8ff4a99e8c606846 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 17:15:12 +0800 Subject: [PATCH 0217/1533] =?UTF-8?q?update=20=20=200093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 9d4d59180e..161fb96e31 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -244,6 +244,8 @@ public: }; ``` +* 时间复杂度: O(3^4),IP地址最多包含4个数字,每个数字最多有3种可能的分割方式,则搜索树的最大深度为4,每个节点最多有3个子节点。 +* 空间复杂度: O(n) # 总结 From 42cdaa2e9a2639dab6931a5733aabf6ed92df62a Mon Sep 17 00:00:00 2001 From: blockChain-Fans <33158355+1055373165@users.noreply.github.com> Date: Thu, 27 Apr 2023 23:34:50 +0800 Subject: [PATCH 0218/1533] =?UTF-8?q?Update=200035.=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 返回值应该是 right+1 把 --- ...\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 58340c21a1..efc875777b 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -274,7 +274,7 @@ func searchInsert(nums []int, target int) int { left = mid + 1 } } - return len(nums) + return right+1 } ``` From 31d2755d18a216384495f25654cd4510c360dadf Mon Sep 17 00:00:00 2001 From: xiaodi007 <334830452@qq.com> Date: Fri, 28 Apr 2023 11:17:08 +0800 Subject: [PATCH 0219/1533] fix 0343 py code --- .../0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index c2fbbdde68..812bf67bec 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -254,7 +254,7 @@ class Solution: # 假设对正整数 i 拆分出的第一个正整数是 j(1 <= j < i),则有以下两种方案: # 1) 将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j * (i-j) # 2) 将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j * dp[i-j] - for j in range(1, i / 2 + 1): + for j in range(1, i // 2 + 1): dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j])) return dp[n] ``` From 14c25f2eeff8e024f822485c4fbdfddcb0c3710e Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 28 Apr 2023 18:30:16 +0800 Subject: [PATCH 0220/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md=20about=20?= =?UTF-8?q?rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-1.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index c45fc3d302..c25252488a 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -573,6 +573,39 @@ object Solution { } ``` +### Rust + +```rust +pub struct Solution; + +impl Solution { + pub fn wei_bag_problem1(weight: Vec, value: Vec, bag_size: usize) -> usize { + let mut dp = vec![vec![0; bag_size + 1]; weight.len()]; + for j in weight[0]..=weight.len() { + dp[0][j] = value[0]; + } + + for i in 1..weight.len() { + for j in 0..=bag_size { + match j < weight[i] { + true => dp[i][j] = dp[i - 1][j], + false => dp[i][j] = dp[i - 1][j].max(dp[i - 1][j - weight[i]] + value[i]), + } + } + } + dp[weight.len() - 1][bag_size] + } +} + +#[test] +fn test_wei_bag_problem1() { + println!( + "{}", + Solution::wei_bag_problem1(vec![1, 3, 4], vec![15, 20, 30], 4) + ); +} +``` +

From e6ad637e6475c73766f401b052a1c523d171d059 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 28 Apr 2023 18:50:56 +0800 Subject: [PATCH 0221/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md=20about=20?= =?UTF-8?q?rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-2.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index baa4107f58..1ce90440b6 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -406,6 +406,34 @@ object Solution { } ``` +### Rust + +```rust +pub struct Solution; + +impl Solution { + pub fn wei_bag_problem2(weight: Vec, value: Vec, bag_size: usize) -> usize { + let mut dp = vec![0; bag_size + 1]; + for i in 0..weight.len() { + for j in (weight[i]..=bag_size).rev() { + if j >= weight[i] { + dp[j] = dp[j].max(dp[j - weight[i]] + value[i]); + } + } + } + dp[dp.len() - 1] + } +} + +#[test] +fn test_wei_bag_problem2() { + println!( + "{}", + Solution::wei_bag_problem1(vec![1, 3, 4], vec![15, 20, 30], 4) + ); +} +``` +

From d2650cc436980c2553003e3c7bdba2e8579aa53b Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 28 Apr 2023 18:51:38 +0800 Subject: [PATCH 0222/1533] =?UTF-8?q?Update=20problems/=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 1ce90440b6..3b798334a6 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -429,7 +429,7 @@ impl Solution { fn test_wei_bag_problem2() { println!( "{}", - Solution::wei_bag_problem1(vec![1, 3, 4], vec![15, 20, 30], 4) + Solution::wei_bag_problem2(vec![1, 3, 4], vec![15, 20, 30], 4) ); } ``` From 3ff604055147700bd66ad8c83b43ee6f073e4265 Mon Sep 17 00:00:00 2001 From: lzxzz <1042183935@qq.com> Date: Sat, 29 Apr 2023 09:49:25 +0800 Subject: [PATCH 0223/1533] optimize --- ...02\273\351\207\215\345\244\215\351\241\271.md" | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 694f1a92dc..486d198b4d 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -264,14 +264,15 @@ javaScript: ```js var removeDuplicates = function(s) { - const stack = []; - for(const x of s) { - let c = null; - if(stack.length && x === (c = stack.pop())) continue; - c && stack.push(c); - stack.push(x); + const result = [] + for(const i of s){ + if(i === result[result.length-1]){ + result.pop() + }else{ + result.push(i) + } } - return stack.join(""); + return result.join('') }; ``` From 76d347076b1ff05769c6bbb8844027363608e017 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 29 Apr 2023 10:27:48 +0800 Subject: [PATCH 0224/1533] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 去除不必要的方法 --- ...11\345\222\214\345\255\220\351\233\206.md" | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 8726bf95b8..8115e18ea9 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -406,24 +406,21 @@ var canPartition = function(nums) { ```Rust impl Solution { - fn max(a: usize, b: usize) -> usize { - if a > b { a } else { b } - } pub fn can_partition(nums: Vec) -> bool { - let nums = nums.iter().map(|x| *x as usize).collect::>(); - let mut sum = 0; - let mut dp: Vec = vec![0; 10001]; - for i in 0..nums.len() { - sum += nums[i]; + let sum = nums.iter().sum::() as usize; + if sum % 2 == 1 { + return false; } - if sum % 2 == 1 { return false; } let target = sum / 2; - for i in 0..nums.len() { - for j in (nums[i]..=target).rev() { - dp[j] = Self::max(dp[j], dp[j - nums[i]] + nums[i]); + let mut dp = vec![0; target + 1]; + for n in nums { + for j in (n as usize..=target).rev() { + dp[j] = dp[j].max(dp[j - n as usize] + n) } } - if dp[target] == target { return true; } + if dp[target] == target as i32 { + return true; + } false } } From da4116bcb60b3bf6efa7f44e8e20877e987fadfe Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Sat, 29 Apr 2023 05:45:01 -0400 Subject: [PATCH 0225/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20Java=20=E7=B5=B1?= =?UTF-8?q?=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 Java 統一迭代法 --- ...55\347\232\204\344\274\227\346\225\260.md" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index b7ef606fe6..e467bf8a23 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -472,6 +472,59 @@ class Solution { } } ``` +統一迭代法 +```Java +class Solution { + public int[] findMode(TreeNode root) { + int count = 0; + int maxCount = 0; + TreeNode pre = null; + LinkedList res = new LinkedList<>(); + Stack stack = new Stack<>(); + + if(root != null) + stack.add(root); + + while(!stack.isEmpty()){ + TreeNode curr = stack.peek(); + if(curr != null){ + stack.pop(); + if(curr.right != null) + stack.add(curr.right); + stack.add(curr); + stack.add(null); + if(curr.left != null) + stack.add(curr.left); + }else{ + stack.pop(); + TreeNode temp = stack.pop(); + if(pre == null) + count = 1; + else if(pre != null && pre.val == temp.val) + count++; + else + count = 1; + pre = temp; + if(count == maxCount) + res.add(temp.val); + if(count > maxCount){ + maxCount = count; + res.clear(); + res.add(temp.val); + } + } + } + int[] result = new int[res.size()]; + int i = 0; + for (int x : res){ + result[i] = x; + i++; + } + return result; + } +} +``` + ## Python From 513707ca713b2b199f8202e953b1a8dd2c40d447 Mon Sep 17 00:00:00 2001 From: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com> Date: Sun, 30 Apr 2023 14:24:26 +0800 Subject: [PATCH 0226/1533] =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E7=BB=93=E6=9E=84=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...51\223\276\350\241\250\347\233\270\344\272\244.md" | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 30f5c467c4..9e0c29f5da 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -101,8 +101,8 @@ public: ## 其他语言版本 +Java: -### Java ```Java public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { @@ -150,9 +150,9 @@ public class Solution { } ``` -### Python -```python +Python: +```python class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: lenA, lenB = 0, 0 @@ -179,7 +179,7 @@ class Solution: return None ``` -### Go +Go: ```go func getIntersectionNode(headA, headB *ListNode) *ListNode { @@ -240,7 +240,7 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -### javaScript +JavaScript: ```js var getListLen = function(head) { @@ -352,6 +352,7 @@ ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ``` Scala: + ```scala object Solution { def getIntersectionNode(headA: ListNode, headB: ListNode): ListNode = { From 113d1192a92b50ad8154b4b7717cdb5d9186d7a0 Mon Sep 17 00:00:00 2001 From: Mrzhugq <84071063+Mrzhugq@users.noreply.github.com> Date: Sun, 30 Apr 2023 16:45:31 +0800 Subject: [PATCH 0227/1533] =?UTF-8?q?Update=200035.=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 195行把空间复杂度写成时间复杂度了 --- ...64\242\346\217\222\345\205\245\344\275\215\347\275\256.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 58340c21a1..a37d67f569 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -191,8 +191,8 @@ public: }; ``` -* 时间复杂度:$O(\log n)$ -* 时间复杂度:$O(1)$ +* 时间复杂度:O(log n) +* 空间复杂度:O(1) ## 总结 From 18a16f9faffb7b796d174fb150e3dd601f6f82f4 Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Sun, 30 Apr 2023 20:24:32 +0000 Subject: [PATCH 0228/1533] Ignore .DS_Store files. --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 1 + 2 files changed, 1 insertion(+) delete mode 100644 .DS_Store create mode 100644 .gitignore diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index da03c1c1b207c3ee04079084ecb15dcfff97ef95..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5T317Q;OJwLXQhx3-<3)^bl)(0V4`psf{T$m}X1UTBHSVD*@yHz(SRn%3w1`N_?k*PTX z=s*u5cO3N-(d77z49It9K_3Pnz=e13&kcl&V-IdW@S|a|_{35R>BXgGloVwt;#QN%@Vis8VsHj`Xe9O(bW6^; z=;?2>^71?{rY9N=vv=!y_4uZF|F|A+0!!OY6cR47cXj`MRB4$Rg_Eiph0d}LrjcXRUo z-}}7&|CmHQVt^R Date: Sun, 30 Apr 2023 13:40:16 -0700 Subject: [PATCH 0229/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 3afedc8249..dbf42ca4e4 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -195,15 +195,16 @@ Java: ```java public class TreeNode { int val; - TreeNode left; - TreeNode right; - TreeNode() {} - TreeNode(int val) { this.val = val; } - TreeNode(int val, TreeNode left, TreeNode right) { - this.val = val; - this.left = left; - this.right = right; - } + TreeNode left; + TreeNode right; + + TreeNode() {} + TreeNode(int val) { this.val = val; } + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } } ``` @@ -212,10 +213,10 @@ Python: ```python class TreeNode: - def __init__(self, value): - self.value = value - self.left = None - self.right = None + def __init__(self, val, left = None, right = None): + self.val = val + self.left = left + self.right = right ``` Go: From 9617a8b3fedc3996645977df31104159864b3462 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 1 May 2023 15:46:15 -0500 Subject: [PATCH 0230/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 75 +++++++++++-------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 26f0ae2d17..e777c2a4aa 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -171,47 +171,60 @@ python3代码: ```python +# 利用长度法 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +from collections import deque class Solution: - """二叉树层序遍历迭代解法""" - - def levelOrder(self, root: TreeNode) -> List[List[int]]: - results = [] + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: if not root: - return results - - from collections import deque - que = deque([root]) - - while que: - size = len(que) - result = [] - for _ in range(size): - cur = que.popleft() - result.append(cur.val) + return [] + queue = deque([root]) + result = [] + while queue: + level = [] + for _ in range(len(queue)): + cur = queue.popleft() + level.append(cur.val) if cur.left: - que.append(cur.left) + queue.append(cur.left) if cur.right: - que.append(cur.right) - results.append(result) - - return results + queue.append(cur.right) + result.append(level) + return result ``` - ```python # 递归法 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def levelOrder(self, root: TreeNode) -> List[List[int]]: - res = [] - def helper(root, depth): - if not root: return [] - if len(res) == depth: res.append([]) # start the current depth - res[depth].append(root.val) # fulfil the current depth - if root.left: helper(root.left, depth + 1) # process child nodes for the next depth - if root.right: helper(root.right, depth + 1) - helper(root, 0) - return res + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + levels = [] + self.helper(root, 0, levels) + return levels + + def helper(self, node, level, levels): + if not node: + return + if len(levels) == level: + levels.append([]) + levels[level].append(node.val) + self.helper(node.left, level + 1, levels) + self.helper(node.right, level + 1, levels) + + ``` + + go: ```go From d6b1f3e9538c67b5592a0d93aee4df70a652ab96 Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Tue, 2 May 2023 23:28:44 -0400 Subject: [PATCH 0231/1533] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix bug. --- ...\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 79a8311d78..1e79b797d6 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -212,7 +212,7 @@ class Solution: return 0 result = 1 dp = [1] * len(nums) - for i in range(len(nums)-1): + for i in range(len(nums)): if nums[i+1] > nums[i]: #连续记录 dp[i+1] = dp[i] + 1 result = max(result, dp[i+1]) From f80761a43833de34dc355d48f729c64ecef63afb Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Tue, 2 May 2023 23:34:57 -0400 Subject: [PATCH 0232/1533] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit result should be initialized to 1. --- ...\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index e8cb0b5f18..01d349496b 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -149,7 +149,7 @@ class Solution: if len(nums) <= 1: return len(nums) dp = [1] * len(nums) - result = 0 + result = 1 for i in range(1, len(nums)): for j in range(0, i): if nums[i] > nums[j]: From c76ed37fc487bc7da34403bbb484e9e42b582efb Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 2 May 2023 23:55:58 -0400 Subject: [PATCH 0233/1533] adding java iteration method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java 迭代方法 --- ...11\346\220\234\347\264\242\346\240\221.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 18d8a0cc8d..5739f7622d 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -248,6 +248,8 @@ public: ## Java +**递归** + ```Java class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { @@ -269,6 +271,46 @@ class Solution { ``` +**迭代** + +```Java +class Solution { + //iteration + public TreeNode trimBST(TreeNode root, int low, int high) { + if(root == null) + return null; + while(root != null && (root.val < low || root.val > high)){ + if(root.val < low) + root = root.right; + else + root = root.left; + } + + TreeNode curr = root; + + //deal with root's left sub-tree, and deal with the value smaller than low. + while(curr != null){ + while(curr.left != null && curr.left.val < low){ + curr.left = curr.left.right; + } + curr = curr.left; + } + //go back to root; + curr = root; + + //deal with root's righg sub-tree, and deal with the value bigger than high. + while(curr != null){ + while(curr.right != null && curr.right.val > high){ + curr.right = curr.right.left; + } + curr = curr.right; + } + return root; + } +} + +```` + ## Python **递归** From 424d5c224f229aaee353b3bbae8996b8e5dcbef2 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 15:06:11 -0500 Subject: [PATCH 0234/1533] =?UTF-8?q?Update=200027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\273\351\231\244\345\205\203\347\264\240.md" | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 91150c74b9..6dc6404e48 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -198,6 +198,7 @@ Python: ``` python 3 +(版本一)快慢指针法 class Solution: def removeElement(self, nums: List[int], val: int) -> int: # 快慢指针 @@ -213,7 +214,21 @@ class Solution: return slow ``` - +``` python 3 +(版本二)暴力递归法 +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + i, l = 0, len(nums) + while i < l: + if nums[i] == val: # 找到等于目标值的节点 + for j in range(i+1, l): # 移除该元素,并将后面元素向前平移 + nums[j - 1] = nums[j] + l -= 1 + i -= 1 + i += 1 + return l + +``` Go: From 53fbfc8339a6b61cb708ec87356e764db0f2b1ba Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 15:08:17 -0500 Subject: [PATCH 0235/1533] =?UTF-8?q?Update=200027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 6dc6404e48..908011532a 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -215,7 +215,7 @@ class Solution: ``` ``` python 3 -(版本二)暴力递归法 +(版本二)暴力法 class Solution: def removeElement(self, nums: List[int], val: int) -> int: i, l = 0, len(nums) From 934dd4e3131485802edd5d7c45fd60dd4f8f2827 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 15:19:23 -0500 Subject: [PATCH 0236/1533] =?UTF-8?q?Update=200977.=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\345\271\263\346\226\271.md" | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 57f8de02af..4fbdd1cdac 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -140,22 +140,37 @@ class Solution { Python: ```Python +(版本一)双指针法 class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: - n = len(nums) - i,j,k = 0,n - 1,n - 1 - ans = [-1] * n - while i <= j: - lm = nums[i] ** 2 - rm = nums[j] ** 2 - if lm > rm: - ans[k] = lm - i += 1 + l, r, i = 0, len(nums)-1, len(nums)-1 + res = [float('inf')] * len(nums) # 需要提前定义列表,存放结果 + while l <= r: + if nums[l] ** 2 < nums[r] ** 2: # 左右边界进行对比,找出最大值 + res[i] = nums[r] ** 2 + r -= 1 # 右指针往左移动 else: - ans[k] = rm - j -= 1 - k -= 1 - return ans + res[i] = nums[l] ** 2 + l += 1 # 左指针往右移动 + i -= 1 # 存放结果的指针需要往前平移一位 + return res +``` + +```Python +(版本二)暴力排序法 +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + for i in range(len(nums)): + nums[i] *= nums[i] + nums.sort() + return nums +``` + +```Python +(版本三)暴力排序法+列表推导法 +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + return sorted(x*x for x in nums) ``` Go: From 5f65e3ba24617dc81b199bcbb356f47517f7fbee Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 15:32:36 -0500 Subject: [PATCH 0237/1533] =?UTF-8?q?Update=200209.=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\346\225\260\347\273\204.md" | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 5a8f91aff3..d7ae478033 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -173,18 +173,44 @@ class Solution { Python: ```python +(版本一)滑动窗口法 class Solution: def minSubArrayLen(self, s: int, nums: List[int]) -> int: - res = float("inf") # 定义一个无限大的数 - Sum = 0 # 滑动窗口数值之和 - i = 0 # 滑动窗口起始位置 - for j in range(len(nums)): - Sum += nums[j] - while Sum >= s: - res = min(res, j-i+1) - Sum -= nums[i] - i += 1 - return 0 if res == float("inf") else res + l = len(nums) + left = 0 + right = 0 + min_len = float('inf') + cur_sum = 0 #当前的累加值 + + while right < l: + cur_sum += nums[right] + + while cur_sum >= s: # 当前累加值大于目标值 + min_len = min(min_len, right - left + 1) + cur_sum -= nums[left] + left += 1 + + right += 1 + + return min_len if min_len != float('inf') else 0 +``` + +```python +(版本二)暴力法 +class Solution: + def minSubArrayLen(self, s: int, nums: List[int]) -> int: + l = len(nums) + min_len = float('inf') + + for i in range(l): + cur_sum = 0 + for j in range(i, l): + cur_sum += nums[j] + if cur_sum >= s: + min_len = min(min_len, j - i + 1) + break + + return min_len if min_len != float('inf') else 0 ``` Go: From f45b1f1d28a668e2c90ad66ac0d2141d59fc73ae Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 16:19:13 -0500 Subject: [PATCH 0238/1533] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index b52f16ea23..6a0de2826c 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -307,21 +307,27 @@ public ListNode removeElements(ListNode head, int val) { Python: ```python +(版本一)虚拟头节点法 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: - def removeElements(self, head: ListNode, val: int) -> ListNode: - dummy_head = ListNode(next=head) #添加一个虚拟节点 - cur = dummy_head - while cur.next: - if cur.next.val == val: - cur.next = cur.next.next #删除cur.next节点 + def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: + # 创建虚拟头部节点以简化删除过程 + dummy_head = ListNode(next = head) + + # 遍历列表并删除值为val的节点 + current = dummy_head + while current.next: + if current.next.val == val: + current.next = current.next.next else: - cur = cur.next + current = current.next + return dummy_head.next + ``` Go: From 70d8379ff7fe99edbbd2d006b9b3d9a5068c66fc Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 16:26:16 -0500 Subject: [PATCH 0239/1533] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\351\223\276\350\241\250.md" | 268 +++++++++--------- 1 file changed, 131 insertions(+), 137 deletions(-) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index c72b73277a..aa04d0e1af 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -485,172 +485,166 @@ class MyLinkedList { Python: ```python -# 单链表 -class MyLinkedList1: - +(版本一)单链表法 +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class MyLinkedList: def __init__(self): - self.dummy_head = Node()# 添加虚拟头指针,便于操作 - self.size = 0 # 设置一个链表长度的属性,便于后续操作,注意每次增和删的时候都要更新 - - def get(self, index): - """ - :type index: int - :rtype: int - """ + self.dummy_head = ListNode() + self.size = 0 + + def get(self, index: int) -> int: if index < 0 or index >= self.size: return -1 - cur = self.dummy_head.next - while(index): - cur = cur.next - index -= 1 - return cur.val - - def addAtHead(self, val): - """ - :type val: int - :rtype: None - """ - new_node = Node(val) - new_node.next = self.dummy_head.next - self.dummy_head.next = new_node + + current = self.dummy_head.next + for i in range(index): + current = current.next + + return current.val + + def addAtHead(self, val: int) -> None: + self.dummy_head.next = ListNode(val, self.dummy_head.next) self.size += 1 - def addAtTail(self, val): - """ - :type val: int - :rtype: None - """ - new_node = Node(val) - cur = self.dummy_head - while(cur.next): - cur = cur.next - cur.next = new_node + def addAtTail(self, val: int) -> None: + current = self.dummy_head + while current.next: + current = current.next + current.next = ListNode(val) self.size += 1 - def addAtIndex(self, index, val): - """ - :type index: int - :type val: int - :rtype: None - """ - if index < 0: - self.addAtHead(val) - return - elif index == self.size: - self.addAtTail(val) - return - elif index > self.size: + def addAtIndex(self, index: int, val: int) -> None: + if index < 0 or index > self.size: return - - node = Node(val) - cur = self.dummy_head - while(index): - cur = cur.next - index -= 1 - node.next = cur.next - cur.next = node - self.size += 1 - def deleteAtIndex(self, index): - """ - :type index: int - :rtype: None - """ + current = self.dummy_head + for i in range(index): + current = current.next + current.next = ListNode(val, current.next) + self.size += 1 + + def deleteAtIndex(self, index: int) -> None: if index < 0 or index >= self.size: return - pre = self.dummy_head - while(index): - pre = pre.next - index -= 1 - pre.next = pre.next.next + + current = self.dummy_head + for i in range(index): + current = current.next + current.next = current.next.next self.size -= 1 - -# 双链表 -# 相对于单链表, Node新增了prev属性 -class Node: - - def __init__(self, val=0, next = None, prev = None): + + +# Your MyLinkedList object will be instantiated and called as such: +# obj = MyLinkedList() +# param_1 = obj.get(index) +# obj.addAtHead(val) +# obj.addAtTail(val) +# obj.addAtIndex(index,val) +# obj.deleteAtIndex(index) +``` + + +```python +(版本二)双链表法 +class ListNode: + def __init__(self, val=0, prev=None, next=None): self.val = val - self.next = next self.prev = prev + self.next = next class MyLinkedList: - def __init__(self): - self._head, self._tail = Node(0), Node(0) # 虚拟节点 - self._head.next, self._tail.prev = self._tail, self._head - self._count = 0 # 添加的节点数 - - def _get_node(self, index: int) -> Node: - # 当index小于_count//2时, 使用_head查找更快, 反之_tail更快 - if index >= self._count // 2: - # 使用prev往前找 - node = self._tail - for _ in range(self._count - index): - node = node.prev - else: - # 使用next往后找 - node = self._head - for _ in range(index + 1): - node = node.next - return node - - - def _update(self, prev: Node, next: Node, val: int) -> None: - """ - 更新节点 - :param prev: 相对于更新的前一个节点 - :param next: 相对于更新的后一个节点 - :param val: 要添加的节点值 - """ - # 计数累加 - self._count += 1 - node = Node(val) - prev.next, next.prev = node, node - node.prev, node.next = prev, next + self.head = None + self.tail = None + self.size = 0 def get(self, index: int) -> int: - """ - Get the value of the index-th node in the linked list. If the index is invalid, return -1. - """ - if 0 <= index < self._count: - node = self._get_node(index) - return node.val - else: + if index < 0 or index >= self.size: return -1 + + if index < self.size // 2: + current = self.head + for i in range(index): + current = current.next + else: + current = self.tail + for i in range(self.size - index - 1): + current = current.prev + + return current.val def addAtHead(self, val: int) -> None: - """ - Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. - """ - self._update(self._head, self._head.next, val) + new_node = ListNode(val, None, self.head) + if self.head: + self.head.prev = new_node + else: + self.tail = new_node + self.head = new_node + self.size += 1 def addAtTail(self, val: int) -> None: - """ - Append a node of value val to the last element of the linked list. - """ - self._update(self._tail.prev, self._tail, val) + new_node = ListNode(val, self.tail, None) + if self.tail: + self.tail.next = new_node + else: + self.head = new_node + self.tail = new_node + self.size += 1 def addAtIndex(self, index: int, val: int) -> None: - """ - Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. - """ - if index < 0: - index = 0 - elif index > self._count: + if index < 0 or index > self.size: return - node = self._get_node(index) - self._update(node.prev, node, val) + + if index == 0: + self.addAtHead(val) + elif index == self.size: + self.addAtTail(val) + else: + if index < self.size // 2: + current = self.head + for i in range(index - 1): + current = current.next + else: + current = self.tail + for i in range(self.size - index): + current = current.prev + new_node = ListNode(val, current, current.next) + current.next.prev = new_node + current.next = new_node + self.size += 1 def deleteAtIndex(self, index: int) -> None: - """ - Delete the index-th node in the linked list, if the index is valid. - """ - if 0 <= index < self._count: - node = self._get_node(index) - # 计数-1 - self._count -= 1 - node.prev.next, node.next.prev = node.next, node.prev + if index < 0 or index >= self.size: + return + + if index == 0: + self.head = self.head.next + if self.head: + self.head.prev = None + else: + self.tail = None + elif index == self.size - 1: + self.tail = self.tail.prev + if self.tail: + self.tail.next = None + else: + self.head = None + else: + if index < self.size // 2: + current = self.head + for i in range(index): + current = current.next + else: + current = self.tail + for i in range(self.size - index - 1): + current = current.prev + current.prev.next = current.next + current.next.prev = current.prev + self.size -= 1 From 926008cf1390c596c7225f0f988723fe687a9408 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Wed, 3 May 2023 18:08:11 -0400 Subject: [PATCH 0240/1533] adding java iteraion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java統一迭代法 --- ...72\347\264\257\345\212\240\346\240\221.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index ad5310e150..e60d9a84e8 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -177,6 +177,8 @@ public: ## Java +**递归** + ```Java class Solution { int sum; @@ -198,6 +200,42 @@ class Solution { } } ``` +**迭代** + +```Java +class Solution { + //DFS iteraion統一迭代法 + public TreeNode convertBST(TreeNode root) { + int pre = 0; + Stack stack = new Stack<>(); + if(root == null) //edge case check + return null; + + stack.add(root); + + while(!stack.isEmpty()){ + TreeNode curr = stack.peek(); + //curr != null的狀況,只負責存node到stack中 + if(curr != null){ + stack.pop(); + if(curr.left != null) //左 + stack.add(curr.left); + stack.add(curr); //中 + stack.add(null); + if(curr.right != null) //右 + stack.add(curr.right); + }else{ + //curr == null的狀況,只負責做單層邏輯 + stack.pop(); + TreeNode temp = stack.pop(); + temp.val += pre; + pre = temp.val; + } + } + return root; + } +} +``` ## Python **递归** From 55ea26c5bdac70997c6ad9833448cdf3c1a7e0d1 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 17:18:48 -0500 Subject: [PATCH 0241/1533] =?UTF-8?q?Update=200206.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E9=93=BE=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\350\275\254\351\223\276\350\241\250.md" | 40 +++++-------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index d558e783fe..0425e18280 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -193,9 +193,9 @@ class Solution { } ``` -Python迭代法: +Python ```python -#双指针 +(版本一)双指针法 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): @@ -205,7 +205,7 @@ class Solution: def reverseList(self, head: ListNode) -> ListNode: cur = head pre = None - while(cur!=None): + while cur: temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next cur.next = pre #反转 #更新pre、cur指针 @@ -217,6 +217,7 @@ class Solution: Python递归法: ```python +(版本二)递归法 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): @@ -224,36 +225,17 @@ Python递归法: # self.next = next class Solution: def reverseList(self, head: ListNode) -> ListNode: - - def reverse(pre,cur): - if not cur: - return pre - - tmp = cur.next - cur.next = pre - - return reverse(cur,tmp) - - return reverse(None,head) + return self.reverse(head, None) + def reverse(self, cur: ListNode, pre: ListNode) -> ListNode: + if cur == None: + return pre + temp = cur.next + cur.next = pre + return self.reverse(temp, cur) ``` -Python递归法从后向前: -```python -# Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: - if not head or not head.next: return head - p = self.reverseList(head.next) - head.next.next = head - head.next = None - return p -``` Go: From d92104209fecd89626df38414d03c78371f251e5 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 17:20:34 -0500 Subject: [PATCH 0242/1533] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index 4dc051aa74..2c171dde91 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -186,21 +186,20 @@ Python: class Solution: def swapPairs(self, head: ListNode) -> ListNode: - res = ListNode(next=head) - pre = res + dummy_head = ListNode(next=head) + current = dummy_head - # 必须有pre的下一个和下下个才能交换,否则说明已经交换结束了 - while pre.next and pre.next.next: - cur = pre.next - post = pre.next.next + # 必须有cur的下一个和下下个才能交换,否则说明已经交换结束了 + while current.next and current.next.next: + temp = current.next # 防止节点修改 + temp1 = current.next.next.next - # pre,cur,post对应最左,中间的,最右边的节点 - cur.next = post.next - post.next = cur - pre.next = post + current.next = current.next.next + current.next.next = temp + temp.next = temp1 + current = current.next.next + return dummy_head.next - pre = pre.next.next - return res.next ``` Go: From 190477400ebf0842343588c4e9e15c5eab412eb4 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 17:22:46 -0500 Subject: [PATCH 0243/1533] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index a11ff8bace..c6f5bfc781 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -127,21 +127,29 @@ Python: # def __init__(self, val=0, next=None): # self.val = val # self.next = next + class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: - head_dummy = ListNode() - head_dummy.next = head - - slow, fast = head_dummy, head_dummy - while(n>=0): #fast先往前走n+1步 + # 创建一个虚拟节点,并将其下一个指针设置为链表的头部 + dummy_head = ListNode(0, head) + + # 创建两个指针,慢指针和快指针,并将它们初始化为虚拟节点 + slow = fast = dummy_head + + # 快指针比慢指针快 n+1 步 + for i in range(n+1): fast = fast.next - n -= 1 - while(fast!=None): + + # 移动两个指针,直到快速指针到达链表的末尾 + while fast: slow = slow.next fast = fast.next - #fast 走到结尾后,slow的下一个节点为倒数第N个节点 - slow.next = slow.next.next #删除 - return head_dummy.next + + # 通过更新第 (n-1) 个节点的 next 指针删除第 n 个节点 + slow.next = slow.next.next + + return dummy_head.next + ``` Go: ```Go From e4072d9a00b12c6de6949091378bdd8d95b5296c Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 17:25:36 -0500 Subject: [PATCH 0244/1533] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=980?= =?UTF-8?q?2.07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\347\233\270\344\272\244.md" | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 30f5c467c4..4bcbd1f920 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -152,7 +152,7 @@ public class Solution { ### Python ```python - +(版本一)求长度,同时出发 class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: lenA, lenB = 0, 0 @@ -178,7 +178,68 @@ class Solution: curB = curB.next return None ``` +```python +(版本二)求长度,同时出发 (代码复用) +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + lenA = self.getLength(headA) + lenB = self.getLength(headB) + + # 通过移动较长的链表,使两链表长度相等 + if lenA > lenB: + headA = self.moveForward(headA, lenA - lenB) + else: + headB = self.moveForward(headB, lenB - lenA) + + # 将两个头向前移动,直到它们相交 + while headA and headB: + if headA == headB: + return headA + headA = headA.next + headB = headB.next + + return None + + def getLength(self, head: ListNode) -> int: + length = 0 + while head: + length += 1 + head = head.next + return length + + def moveForward(self, head: ListNode, steps: int) -> ListNode: + while steps > 0: + head = head.next + steps -= 1 + return head +``` +```python +(版本三)等比例法 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + # 处理边缘情况 + if not headA or not headB: + return None + + # 在每个链表的头部初始化两个指针 + pointerA = headA + pointerB = headB + + # 遍历两个链表直到指针相交 + while pointerA != pointerB: + # 将指针向前移动一个节点 + pointerA = pointerA.next if pointerA else headB + pointerB = pointerB.next if pointerB else headA + + # 如果相交,指针将位于交点节点,如果没有交点,值为None + return pointerA +``` ### Go ```go From b2bfb8016642f388389250df57bad7e9cce82a3f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 17:27:27 -0500 Subject: [PATCH 0245/1533] =?UTF-8?q?Update=200142.=E7=8E=AF=E5=BD=A2?= =?UTF-8?q?=E9=93=BE=E8=A1=A8II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\275\242\351\223\276\350\241\250II.md" | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index 46df477705..e80a715a6c 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -221,25 +221,55 @@ public class Solution { Python: ```python +(版本一)快慢指针法 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + + class Solution: def detectCycle(self, head: ListNode) -> ListNode: - slow, fast = head, head + slow = head + fast = head + while fast and fast.next: slow = slow.next fast = fast.next.next - # 如果相遇 + + # If there is a cycle, the slow and fast pointers will eventually meet if slow == fast: - p = head - q = slow - while p!=q: - p = p.next - q = q.next - #你也可以return q - return p - + # Move one of the pointers back to the start of the list + slow = head + while slow != fast: + slow = slow.next + fast = fast.next + return slow + # If there is no cycle, return None return None ``` +```python +(版本二)集合法 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: ListNode) -> ListNode: + visited = set() + + while head: + if head in visited: + return head + visited.add(head) + head = head.next + + return None +``` Go: ```go From dd58553088514af1a1a019fb9e6d4b2e41b0aeb5 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 19:26:39 -0500 Subject: [PATCH 0246/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\275\222\351\201\215\345\216\206.md" | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 5a9a670add..8d5a5985e3 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -174,50 +174,45 @@ class Solution { Python: ```python # 前序遍历-递归-LC144_二叉树的前序遍历 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right + class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: - # 保存结果 - result = [] - - def traversal(root: TreeNode): - if root == None: - return - result.append(root.val) # 前序 - traversal(root.left) # 左 - traversal(root.right) # 右 - - traversal(root) - return result + if not root: + return [] + + left = self.preorderTraversal(root.left) + right = self.preorderTraversal(root.right) + + return [root.val] + left + right + # 中序遍历-递归-LC94_二叉树的中序遍历 class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: - result = [] + if root is None: + return [] - def traversal(root: TreeNode): - if root == None: - return - traversal(root.left) # 左 - result.append(root.val) # 中序 - traversal(root.right) # 右 + left = self.inorderTraversal(root.left) + right = self.inorderTraversal(root.right) - traversal(root) - return result + return left + [root.val] + right # 后序遍历-递归-LC145_二叉树的后序遍历 class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: - result = [] + if not root: + return [] - def traversal(root: TreeNode): - if root == None: - return - traversal(root.left) # 左 - traversal(root.right) # 右 - result.append(root.val) # 后序 + left = self.postorderTraversal(root.left) + right = self.postorderTraversal(root.right) - traversal(root) - return result + return left + right + [root.val] ``` Go: From dd1da7fc548c7ab7c1e0964f0ef50410be53e551 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 19:27:58 -0500 Subject: [PATCH 0247/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 8d5a5985e3..92f342f0db 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -191,7 +191,8 @@ class Solution: return [root.val] + left + right - +``` +```python # 中序遍历-递归-LC94_二叉树的中序遍历 class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: @@ -202,6 +203,9 @@ class Solution: right = self.inorderTraversal(root.right) return left + [root.val] + right +``` +```python + # 后序遍历-递归-LC145_二叉树的后序遍历 class Solution: From ce5b335b7258b7febb2dc831d68bfd3bb6161905 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 19:30:53 -0500 Subject: [PATCH 0248/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\350\277\255\344\273\243\351\201\215\345\216\206.md" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 35cf4077a5..8b2414652c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -258,7 +258,9 @@ class Solution: if node.left: stack.append(node.left) return result - +``` +```python + # 中序遍历-迭代-LC94_二叉树的中序遍历 class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: @@ -279,7 +281,9 @@ class Solution: # 取栈顶元素右结点 cur = cur.right return result - + ``` + ```python + # 后序遍历-迭代-LC145_二叉树的后序遍历 class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: From 0521f762d90b1af39fa4f134d219e2d208988e3b Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 19:48:57 -0500 Subject: [PATCH 0249/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index e777c2a4aa..29deee11a5 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -178,12 +178,11 @@ python3代码: # self.val = val # self.left = left # self.right = right -from collections import deque class Solution: def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: if not root: return [] - queue = deque([root]) + queue = collections.deque([root]) result = [] while queue: level = [] From 42f85c8a8f8c637dbb484ca0d0c9ab88973c3a0b Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 19:56:26 -0500 Subject: [PATCH 0250/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 29deee11a5..a4164b2ca3 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -512,27 +512,29 @@ python代码: class Solution: """二叉树层序遍历II迭代解法""" +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: def levelOrderBottom(self, root: TreeNode) -> List[List[int]]: - results = [] if not root: - return results - - from collections import deque - que = deque([root]) - - while que: - result = [] - for _ in range(len(que)): - cur = que.popleft() - result.append(cur.val) + return [] + queue = collections.deque([root]) + result = [] + while queue: + level = [] + for _ in range(len(queue)): + cur = queue.popleft() + level.append(cur.val) if cur.left: - que.append(cur.left) + queue.append(cur.left) if cur.right: - que.append(cur.right) - results.append(result) - - results.reverse() - return results + queue.append(cur.right) + result.append(level) + return result[::-1] ``` Java: From 52092d0153828a14179b41d041e99121a9b4f1f2 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:01:07 -0500 Subject: [PATCH 0251/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index a4164b2ca3..fdfde82277 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -835,35 +835,35 @@ public: python代码: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def rightSideView(self, root: TreeNode) -> List[int]: if not root: return [] - - # deque来自collections模块,不在力扣平台时,需要手动写入 - # 'from collections import deque' 导入 - # deque相比list的好处是,list的pop(0)是O(n)复杂度,deque的popleft()是O(1)复杂度 - - quene = deque([root]) - out_list = [] - - while quene: - # 每次都取最后一个node就可以了 - node = quene[-1] - out_list.append(node.val) - - # 执行这个遍历的目的是获取下一层所有的node - for _ in range(len(quene)): - node = quene.popleft() + + queue = collections.deque([root]) + right_view = [] + + while queue: + level_size = len(queue) + + for i in range(level_size): + node = queue.popleft() + + if i == level_size - 1: + right_view.append(node.val) + if node.left: - quene.append(node.left) + queue.append(node.left) if node.right: - quene.append(node.right) - - return out_list - -# 执行用时:36 ms, 在所有 Python3 提交中击败了89.47%的用户 -# 内存消耗:14.6 MB, 在所有 Python3 提交中击败了96.65%的用户 + queue.append(node.right) + + return right_view ``` From ae3fc9f57688e6369f593e6dab8e8f1d55a8c919 Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Wed, 3 May 2023 21:02:58 -0400 Subject: [PATCH 0252/1533] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=92=8C=EF=BC=88=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目已定义1 <= nums.length. --- ...\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" | 2 -- 1 file changed, 2 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index c7d1b2fd7b..bd49091269 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -139,8 +139,6 @@ Python: ```python class Solution: def maxSubArray(self, nums: List[int]) -> int: - if len(nums) == 0: - return 0 dp = [0] * len(nums) dp[0] = nums[0] result = dp[0] From 95616fa9a8a147f3255ead5523af0a4de51844b9 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:03:09 -0500 Subject: [PATCH 0253/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index fdfde82277..f4a5c466e3 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1121,27 +1121,38 @@ python代码: class Solution: """二叉树层平均值迭代解法""" +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: def averageOfLevels(self, root: TreeNode) -> List[float]: - results = [] if not root: - return results - - from collections import deque - que = deque([root]) - - while que: - size = len(que) - sum_ = 0 - for _ in range(size): - cur = que.popleft() - sum_ += cur.val - if cur.left: - que.append(cur.left) - if cur.right: - que.append(cur.right) - results.append(sum_ / size) + return [] - return results + queue = collections.deque([root]) + averages = [] + + while queue: + size = len(queue) + level_sum = 0 + + for i in range(size): + node = queue.popleft() + + + level_sum += node.val + + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + + averages.append(level_sum / size) + + return averages ``` java: From 7be18e2dd372a17288c8cb215b52dc5a06c1a240 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:04:54 -0500 Subject: [PATCH 0254/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index f4a5c466e3..ed2c683d7a 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1426,28 +1426,36 @@ public: python代码: ```python -class Solution: - """N叉树的层序遍历迭代法""" +""" +# Definition for a Node. +class Node: + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" +class Solution: def levelOrder(self, root: 'Node') -> List[List[int]]: - results = [] if not root: - return results - - from collections import deque - que = deque([root]) - - while que: - result = [] - for _ in range(len(que)): - cur = que.popleft() - result.append(cur.val) - # cur.children 是 Node 对象组成的列表,也可能为 None - if cur.children: - que.extend(cur.children) - results.append(result) - - return results + return [] + + result = [] + queue = collections.deque([root]) + + while queue: + level_size = len(queue) + level = [] + + for _ in range(level_size): + node = queue.popleft() + level.append(node.val) + + for child in node.children: + queue.append(child) + + result.append(level) + + return result ``` ```python From b794dccc5b40c1fa25dc2413edd0bbfda9e20aac Mon Sep 17 00:00:00 2001 From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com> Date: Wed, 3 May 2023 21:05:55 -0400 Subject: [PATCH 0255/1533] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 1e79b797d6..79a8311d78 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -212,7 +212,7 @@ class Solution: return 0 result = 1 dp = [1] * len(nums) - for i in range(len(nums)): + for i in range(len(nums)-1): if nums[i+1] > nums[i]: #连续记录 dp[i+1] = dp[i] + 1 result = max(result, dp[i+1]) From 994db141aa831866a5551d7c01c5d676252b9876 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:06:39 -0500 Subject: [PATCH 0256/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index ed2c683d7a..6235ad21c0 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1761,22 +1761,37 @@ public: python代码: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def largestValues(self, root: TreeNode) -> List[int]: - if root is None: + if not root: return [] - queue = [root] - out_list = [] + + result = [] + queue = collections.deque([root]) + while queue: - length = len(queue) - in_list = [] - for _ in range(length): - curnode = queue.pop(0) - in_list.append(curnode.val) - if curnode.left: queue.append(curnode.left) - if curnode.right: queue.append(curnode.right) - out_list.append(max(in_list)) - return out_list + level_size = len(queue) + max_val = float('-inf') + + for _ in range(level_size): + node = queue.popleft() + max_val = max(max_val, node.val) + + if node.left: + queue.append(node.left) + + if node.right: + queue.append(node.right) + + result.append(max_val) + + return result ``` java代码: From ffab57f2c330f35a624bd7c9ce7dc9d444f0ff79 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:09:12 -0500 Subject: [PATCH 0257/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 6235ad21c0..47969b25fb 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -2096,36 +2096,40 @@ class Solution { python代码: ```python -# 层序遍历解法 +""" +# Definition for a Node. +class Node: + def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): + self.val = val + self.left = left + self.right = right + self.next = next +""" class Solution: def connect(self, root: 'Node') -> 'Node': if not root: - return None - queue = [root] + return root + + queue = collections.deque([root]) + while queue: - n = len(queue) - for i in range(n): - node = queue.pop(0) + level_size = len(queue) + prev = None + + for i in range(level_size): + node = queue.popleft() + + if prev: + prev.next = node + + prev = node + if node.left: queue.append(node.left) + if node.right: queue.append(node.right) - if i == n - 1: - break - node.next = queue[0] - return root - -# 链表解法 -class Solution: - def connect(self, root: 'Node') -> 'Node': - first = root - while first: - cur = first - while cur: # 遍历每一层的节点 - if cur.left: cur.left.next = cur.right # 找左节点的next - if cur.right and cur.next: cur.right.next = cur.next.left # 找右节点的next - cur = cur.next # cur同层移动到下一节点 - first = first.left # 从本层扩展到下一层 + return root ``` From 65085c4e9bcf8c478fe67caad7dd0dceaa5911e0 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:10:51 -0500 Subject: [PATCH 0258/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 47969b25fb..89e7a1265e 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -2381,21 +2381,41 @@ python代码: ```python # 层序遍历解法 +""" +# Definition for a Node. +class Node: + def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): + self.val = val + self.left = left + self.right = right + self.next = next +""" + class Solution: def connect(self, root: 'Node') -> 'Node': if not root: - return None - queue = [root] - while queue: # 遍历每一层 - length = len(queue) - tail = None # 每一层维护一个尾节点 - for i in range(length): # 遍历当前层 - curnode = queue.pop(0) - if tail: - tail.next = curnode # 让尾节点指向当前节点 - tail = curnode # 让当前节点成为尾节点 - if curnode.left : queue.append(curnode.left) - if curnode.right: queue.append(curnode.right) + return root + + queue = collections.deque([root]) + + while queue: + level_size = len(queue) + prev = None + + for i in range(level_size): + node = queue.popleft() + + if prev: + prev.next = node + + prev = node + + if node.left: + queue.append(node.left) + + if node.right: + queue.append(node.right) + return root ``` From 61173277185bf7948ffb576212cc31fd383778e3 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:12:16 -0500 Subject: [PATCH 0259/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 89e7a1265e..13694207ac 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -2664,24 +2664,31 @@ class Solution { Python: ```python 3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def maxDepth(self, root: TreeNode) -> int: - if root == None: + if not root: return 0 - - queue_ = [root] + depth = 0 - while queue_: - length = len(queue_) - for i in range(length): - cur = queue_.pop(0) - sub.append(cur.val) - #子节点入队列 - if cur.left: queue_.append(cur.left) - if cur.right: queue_.append(cur.right) + queue = collections.deque([root]) + + while queue: depth += 1 - + for _ in range(len(queue)): + node = queue.popleft() + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + return depth + ``` Go: From de7c67c35a5d3d0e423072d0b7cb02876c628786 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:13:38 -0500 Subject: [PATCH 0260/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 13694207ac..c2ad950814 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -2938,23 +2938,26 @@ Python 3: # self.right = right class Solution: def minDepth(self, root: TreeNode) -> int: - if root == None: + if not root: return 0 + depth = 0 + queue = collections.deque([root]) + + while queue: + depth += 1 + for _ in range(len(queue)): + node = queue.popleft() + + if not node.left and not node.right: + return depth + + if node.left: + queue.append(node.left) + + if node.right: + queue.append(node.right) - #根节点的深度为1 - queue_ = [(root,1)] - while queue_: - cur, depth = queue_.pop(0) - - if cur.left == None and cur.right == None: - return depth - #先左子节点,由于左子节点没有孩子,则就是这一层了 - if cur.left: - queue_.append((cur.left,depth + 1)) - if cur.right: - queue_.append((cur.right,depth + 1)) - - return 0 + return depth ``` Go: From 54bcab13e37ee1e609b567411b968bdb5a86abac Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:25:30 -0500 Subject: [PATCH 0261/1533] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\344\272\214\345\217\211\346\240\221.md" | 161 +++++++++++++----- 1 file changed, 119 insertions(+), 42 deletions(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 16a5be577d..63baa409dd 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -314,81 +314,158 @@ class Solution { 递归法:前序遍历: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return None - root.left, root.right = root.right, root.left #中 - self.invertTree(root.left) #左 - self.invertTree(root.right) #右 + root.left, root.right = root.right, root.left + self.invertTree(root.left) + self.invertTree(root.right) + return root +``` + +迭代法:前序遍历: +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def invertTree(self, root: TreeNode) -> TreeNode: + if not root: + return None + stack = [root] + while stack: + node = stack.pop() + node.left, node.right = node.right, node.left + if node.left: + stack.append(node.left) + if node.right: + stack.append(node.right) + return root +``` + + +递归法:中序遍历: +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def invertTree(self, root: TreeNode) -> TreeNode: + if not root: + return None + self.invertTree(root.left) + root.left, root.right = root.right, root.left + self.invertTree(root.left) + return root +``` + +迭代法:中序遍历: +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def invertTree(self, root: TreeNode) -> TreeNode: + if not root: + return None + stack = [root] + while stack: + node = stack.pop() + if node.left: + stack.append(node.left) + node.left, node.right = node.right, node.left + if node.left: + stack.append(node.left) return root ``` + 递归法:后序遍历: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def invertTree(self, root: TreeNode) -> TreeNode: - if root is None: + if not root: return None self.invertTree(root.left) self.invertTree(root.right) root.left, root.right = root.right, root.left - return root + return root ``` -迭代法:深度优先遍历(前序遍历): +迭代法:后序遍历: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: - return root - st = [] - st.append(root) - while st: - node = st.pop() - node.left, node.right = node.right, node.left #中 - if node.right: - st.append(node.right) #右 + return None + stack = [root] + while stack: + node = stack.pop() if node.left: - st.append(node.left) #左 + stack.append(node.left) + if node.right: + stack.append(node.right) + node.left, node.right = node.right, node.left + return root ``` + + + + 迭代法:广度优先遍历(层序遍历): ```python -import collections +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def invertTree(self, root: TreeNode) -> TreeNode: - queue = collections.deque() #使用deque() - if root: - queue.append(root) + if not root: + return None + + queue = collections.deque([root]) while queue: - size = len(queue) - for i in range(size): + for i in range(len(queue)): node = queue.popleft() - node.left, node.right = node.right, node.left #节点处理 - if node.left: - queue.append(node.left) - if node.right: - queue.append(node.right) - return root -``` -迭代法:广度优先遍历(层序遍历),和之前的层序遍历写法一致: -```python -class Solution: - def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: - if not root: return root - from collections import deque - que=deque([root]) - while que: - size=len(que) - for i in range(size): - cur=que.popleft() - cur.left, cur.right = cur.right, cur.left - if cur.left: que.append(cur.left) - if cur.right: que.append(cur.right) + node.left, node.right = node.right, node.left + if node.left: queue.append(node.left) + if node.right: queue.append(node.right) return root + ``` + ### Go 递归版本的前序遍历 From 918d0ec0faf27870212b11c4820d4ae956ba8da3 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 20:40:43 -0500 Subject: [PATCH 0262/1533] =?UTF-8?q?Update=200101.=E5=AF=B9=E7=A7=B0?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\344\272\214\345\217\211\346\240\221.md" | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index b75e9ff2da..81ca79a2e2 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -442,25 +442,31 @@ class Solution: 层次遍历 ```python class Solution: - def isSymmetric(self, root: Optional[TreeNode]) -> bool: + def isSymmetric(self, root: TreeNode) -> bool: if not root: return True - - que = [root] - while que: - this_level_length = len(que) - for i in range(this_level_length // 2): - # 要么其中一个是None但另外一个不是 - if (not que[i] and que[this_level_length - 1 - i]) or (que[i] and not que[this_level_length - 1 - i]): - return False - # 要么两个都不是None - if que[i] and que[i].val != que[this_level_length - 1 - i].val: - return False - for i in range(this_level_length): - if not que[i]: continue - que.append(que[i].left) - que.append(que[i].right) - que = que[this_level_length:] + + queue = collections.deque([root.left, root.right]) + + while queue: + level_size = len(queue) + + if level_size % 2 != 0: + return False + + level_vals = [] + for i in range(level_size): + node = queue.popleft() + if node: + level_vals.append(node.val) + queue.append(node.left) + queue.append(node.right) + else: + level_vals.append(None) + + if level_vals != level_vals[::-1]: + return False + return True ``` From aac7378cb62ed2ec36fc1012f0057844297bda67 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 21:40:49 -0500 Subject: [PATCH 0263/1533] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 36578fd343..2294c1d943 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -419,26 +419,33 @@ class solution: return 1 + max(self.maxdepth(root.left), self.maxdepth(root.right)) ``` -迭代法: +层序遍历迭代法: ```python -import collections -class solution: - def maxdepth(self, root: treenode) -> int: +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def maxDepth(self, root: TreeNode) -> int: if not root: return 0 - depth = 0 #记录深度 - queue = collections.deque() - queue.append(root) + + depth = 0 + queue = collections.deque([root]) + while queue: - size = len(queue) depth += 1 - for i in range(size): + for _ in range(len(queue)): node = queue.popleft() if node.left: queue.append(node.left) if node.right: queue.append(node.right) + return depth + ``` ### 559.n叉树的最大深度 From 5da51519b5692ef2f450ba38b6299452a00f3b22 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 21:48:04 -0500 Subject: [PATCH 0264/1533] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\200\345\244\247\346\267\261\345\272\246.md" | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 2294c1d943..c4d94d4d98 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -452,14 +452,17 @@ class Solution: 递归法: ```python -class solution: - def maxdepth(self, root: 'node') -> int: +class Solution: + def maxDepth(self, root: 'Node') -> int: if not root: return 0 - depth = 0 - for i in range(len(root.children)): - depth = max(depth, self.maxdepth(root.children[i])) - return depth + 1 + + max_depth = 1 + + for child in root.children: + max_depth = max(max_depth, self.maxDepth(child) + 1) + + return max_depth ``` 迭代法: From be27cc547daf4c82a048b71f201c93ed3b2b3e2e Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 21:57:13 -0500 Subject: [PATCH 0265/1533] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index c4d94d4d98..c55ddb3f3d 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -467,22 +467,31 @@ class Solution: 迭代法: ```python -import collections -class solution: - def maxdepth(self, root: 'node') -> int: - queue = collections.deque() - if root: - queue.append(root) - depth = 0 #记录深度 +""" +# Definition for a Node. +class Node: + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" + +class Solution: + def maxDepth(self, root: TreeNode) -> int: + if not root: + return 0 + + depth = 0 + queue = collections.deque([root]) + while queue: - size = len(queue) depth += 1 - for i in range(size): + for _ in range(len(queue)): node = queue.popleft() - for j in range(len(node.children)): - if node.children[j]: - queue.append(node.children[j]) + for child in node.children: + queue.append(child) + return depth + ``` 使用栈来模拟后序遍历依然可以 From 1b1b51750db1afcb812ba1250ea4fdc3e0395425 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 3 May 2023 22:04:25 -0500 Subject: [PATCH 0266/1533] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index c55ddb3f3d..b6208169a4 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -494,30 +494,32 @@ class Solution: ``` -使用栈来模拟后序遍历依然可以 +使用栈 ```python -class solution: - def maxdepth(self, root: 'node') -> int: - st = [] - if root: - st.append(root) - depth = 0 - result = 0 - while st: - node = st.pop() - if node != none: - st.append(node) #中 - st.append(none) - depth += 1 - for i in range(len(node.children)): #处理孩子 - if node.children[i]: - st.append(node.children[i]) - - else: - node = st.pop() - depth -= 1 - result = max(result, depth) - return result +""" +# Definition for a Node. +class Node: + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" + +class Solution: + def maxDepth(self, root: 'Node') -> int: + if not root: + return 0 + + max_depth = 0 + + stack = [(root, 1)] + + while stack: + node, depth = stack.pop() + max_depth = max(max_depth, depth) + for child in node.children: + stack.append((child, depth + 1)) + + return max_depth ``` From 75d137eb1c0464de0321f611ed66125e322936e7 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 4 May 2023 00:23:53 -0500 Subject: [PATCH 0267/1533] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\260\217\346\267\261\345\272\246.md" | 59 ++++++++++++------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index de36c6f25a..bda12ff08b 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -300,44 +300,63 @@ class Solution { 递归法: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def minDepth(self, root: TreeNode) -> int: if not root: return 0 + if not root.left and not root.right: return 1 - - min_depth = 10**9 + + left_depth = float('inf') + right_depth = float('inf') + if root.left: - min_depth = min(self.minDepth(root.left), min_depth) # 获得左子树的最小高度 + left_depth = self.minDepth(root.left) if root.right: - min_depth = min(self.minDepth(root.right), min_depth) # 获得右子树的最小高度 - return min_depth + 1 + right_depth = self.minDepth(root.right) + + return 1 + min(left_depth, right_depth) + ``` 迭代法: ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def minDepth(self, root: TreeNode) -> int: if not root: return 0 - que = deque() - que.append(root) - res = 1 - - while que: - for _ in range(len(que)): - node = que.popleft() - # 当左右孩子都为空的时候,说明是最低点的一层了,退出 + depth = 0 + queue = collections.deque([root]) + + while queue: + depth += 1 + for _ in range(len(queue)): + node = queue.popleft() + if not node.left and not node.right: - return res - if node.left is not None: - que.append(node.left) - if node.right is not None: - que.append(node.right) - res += 1 - return res + return depth + + if node.left: + queue.append(node.left) + + if node.right: + queue.append(node.right) + + return depth ``` From d15c54f43fe0df608d207f269e8bb589c5cebd3d Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 4 May 2023 00:26:20 -0500 Subject: [PATCH 0268/1533] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\260\217\346\267\261\345\272\246.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index bda12ff08b..0c086f1bbb 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -359,6 +359,39 @@ class Solution: return depth ``` +迭代法: + +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right + +class Solution: + def minDepth(self, root: TreeNode) -> int: + if not root: + return 0 + + queue = collections.deque([(root, 1)]) + + while queue: + node, depth = queue.popleft() + + # Check if the node is a leaf node + if not node.left and not node.right: + return depth + + # Add left and right child to the queue + if node.left: + queue.append((node.left, depth+1)) + if node.right: + queue.append((node.right, depth+1)) + + return 0 + +``` ## Go From 3d59f732e0479c12657f5a6020b4ed5784222780 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 4 May 2023 01:35:13 -0500 Subject: [PATCH 0269/1533] =?UTF-8?q?Update=200222.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA?= =?UTF-8?q?=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...212\202\347\202\271\344\270\252\346\225\260.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index d89a9bcea6..795a6f3777 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -393,6 +393,20 @@ class Solution: # 利用完全二叉树特性 return 2**count-1 return 1+self.countNodes(root.left)+self.countNodes(root.right) ``` +完全二叉树写法3 +```python +class Solution: # 利用完全二叉树特性 + def countNodes(self, root: TreeNode) -> int: + if not root: return 0 + count = 0 + left = root.left; right = root.right + while left and right: + count+=1 + left = left.left; right = right.right + if not left and not right: # 如果同时到底说明是满二叉树,反之则不是 + return (2< Date: Thu, 4 May 2023 21:49:12 -0500 Subject: [PATCH 0270/1533] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...241\344\272\214\345\217\211\346\240\221.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index 804c95eb27..a3bc77fb11 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -532,6 +532,24 @@ class Solution: else: return 1 + max(left_height, right_height) ``` +递归法精简版: + +```python +class Solution: + def isBalanced(self, root: TreeNode) -> bool: + return self.height(root) != -1 + def height(self, node: TreeNode) -> int: + if not node: + return 0 + left = self.height(node.left) + if left == -1: + return -1 + right = self.height(node.right) + if right == -1 or abs(left - right) > 1: + return -1 + return max(left, right) + 1 +``` + 迭代法: From debaa5e4a0cb16a58f226e04b0865ce1b423f122 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 4 May 2023 22:54:49 -0500 Subject: [PATCH 0271/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\234\211\350\267\257\345\276\204.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 8c542ceaa4..c396f4a0fb 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -468,6 +468,71 @@ class Solution { ``` --- ## Python: + + +递归法+回溯(版本一) +```Python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +import copy +from typing import List, Optional + +class Solution: + def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]: + if not root: + return [] + result = [] + self.generate_paths(root, [], result) + return result + + def generate_paths(self, node: TreeNode, path: List[int], result: List[str]) -> None: + path.append(node.val) + if not node.left and not node.right: + result.append('->'.join(map(str, path))) + if node.left: + self.generate_paths(node.left, copy.copy(path), result) + if node.right: + self.generate_paths(node.right, copy.copy(path), result) + path.pop() + + +``` +递归法+回溯(版本二) +```Python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +import copy +from typing import List, Optional + +class Solution: + def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]: + if not root: + return [] + result = [] + self.generate_paths(root, [], result) + return result + + def generate_paths(self, node: TreeNode, path: List[int], result: List[str]) -> None: + if not node: + return + path.append(node.val) + if not node.left and not node.right: + result.append('->'.join(map(str, path))) + else: + self.generate_paths(node.left, copy.copy(path), result) + self.generate_paths(node.right, copy.copy(path), result) + path.pop() + +``` + 递归法+隐形回溯 ```Python # Definition for a binary tree node. From 3b0000a280b2e6a5b1bb033556cbc77e793736fb Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 4 May 2023 23:26:06 -0500 Subject: [PATCH 0272/1533] =?UTF-8?q?Update=200404.=E5=B7=A6=E5=8F=B6?= =?UTF-8?q?=E5=AD=90=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...66\345\255\220\344\271\213\345\222\214.md" | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index cf5441c4c4..617978b743 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -250,19 +250,27 @@ class Solution { **递归后序遍历** ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def sumOfLeftLeaves(self, root: TreeNode) -> int: - if not root: + def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int: + if not root: return 0 - left_left_leaves_sum = self.sumOfLeftLeaves(root.left) # 左 - right_left_leaves_sum = self.sumOfLeftLeaves(root.right) # 右 - - cur_left_leaf_val = 0 - if root.left and not root.left.left and not root.left.right: - cur_left_leaf_val = root.left.val + # 检查根节点的左子节点是否为叶节点 + if root.left and not root.left.left and not root.left.right: + left_val = root.left.val + else: + left_val = self.sumOfLeftLeaves(root.left) - return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum # 中 + # 递归地计算右子树左叶节点的和 + right_val = self.sumOfLeftLeaves(root.right) + + return left_val + right_val ``` **迭代** From efc06ad7fdebe84477904b05c2e97b0adce6bedc Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Fri, 5 May 2023 13:57:17 -0500 Subject: [PATCH 0273/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\244\247\345\205\203\347\264\240II.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index bf65120923..3fd4b3b6db 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -164,6 +164,7 @@ class Solution { Python: ```python +# 方法 1: class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: dp = [-1] * len(nums) @@ -174,6 +175,26 @@ class Solution: stack.pop() stack.append(i%len(nums)) return dp + +# 方法 2: +class Solution: + def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: + stack = [] + # 创建答案数组 + ans = [-1] * len(nums1) + for i in range(len(nums2)): + while len(stack) > 0 and nums2[i] > nums2[stack[-1]]: + # 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常 + if nums2[stack[-1]] in nums1: + # 锁定 num1 检索的 index + index = nums1.index(nums2[stack[-1]]) + # 更新答案数组 + ans[index] = nums2[i] + # 弹出小元素 + # 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了 + stack.pop() + stack.append(i) + return ans ``` Go: ```go From 4224c17e54e164141ca2f6a08978402ae115fea5 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 19:18:26 -0500 Subject: [PATCH 0274/1533] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=980?= =?UTF-8?q?2.07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\347\233\270\344\272\244.md" | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 4bcbd1f920..dda7f2ad00 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -214,7 +214,41 @@ class Solution: return head ``` ```python -(版本三)等比例法 +(版本三)求长度,同时出发 (代码复用 + 精简) +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + dis = self.getLength(headA) - self.getLength(headB) + + # 通过移动较长的链表,使两链表长度相等 + if dis > 0: + headA = self.moveForward(headA, dis) + else: + headB = self.moveForward(headB, abs(dis)) + + # 将两个头向前移动,直到它们相交 + while headA and headB: + if headA == headB: + return headA + headA = headA.next + headB = headB.next + + return None + + def getLength(self, head: ListNode) -> int: + length = 0 + while head: + length += 1 + head = head.next + return length + + def moveForward(self, head: ListNode, steps: int) -> ListNode: + while steps > 0: + head = head.next + steps -= 1 + return head +``` +```python +(版本四)等比例法 # Definition for singly-linked list. # class ListNode: # def __init__(self, x): From d590d268d717b0241c5d900f70e9356bf3c88903 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 19:50:20 -0500 Subject: [PATCH 0275/1533] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\274\202\344\275\215\350\257\215.md" | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 1006ea3552..0e5683c794 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -122,6 +122,21 @@ class Solution { ``` Python: +(版本一) 使用数组 +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + record = [0] * 26 + for i in range(len(s)): + record[ord(s[i]) - ord("a")] += 1 + for i in range(len(t)): + record[ord(t[i]) - ord("a")] -= 1 + for i in range(26): + if record[i] != 0: + return False + return True +``` +(版本二) 使用数组 ```python class Solution: def isAnagram(self, s: str, t: str) -> bool: @@ -138,12 +153,13 @@ class Solution: return True ``` -Python写法二(没有使用数组作为哈希表,只是介绍defaultdict这样一种解题思路): +(版本三) 使用defaultdict ```python +from collections import defaultdict + class Solution: def isAnagram(self, s: str, t: str) -> bool: - from collections import defaultdict s_dict = defaultdict(int) t_dict = defaultdict(int) @@ -156,17 +172,40 @@ class Solution: return s_dict == t_dict ``` -Python写法三(没有使用数组作为哈希表,只是介绍Counter这种更方便的解题思路): +(版本四) 使用字典 + +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + hash_table_s = {} + hash_table_t = {} + + for i in range(len(s)): + hash_table_s[s[i]] = hash_table_s.get(s[i], 0) + 1 + hash_table_t[t[i]] = hash_table_t.get(t[i], 0) + 1 + + return hash_table_s == hash_table_t +``` + +(版本五) 使用排序 ```python -class Solution(object): +class Solution: def isAnagram(self, s: str, t: str) -> bool: - from collections import Counter - a_count = Counter(s) - b_count = Counter(t) - return a_count == b_count + return sorted(s) == sorted(t) ``` +(版本六) 使用Counter + +```python +from collections import Counter +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return Counter(s) == Counter(t) +``` Go: ```go From 3065cf38604dd672ad128a445b98c356604eb24e Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 20:04:47 -0500 Subject: [PATCH 0276/1533] =?UTF-8?q?Update=200349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\344\272\244\351\233\206.md" | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 0da0e30c37..c2f6ef46ba 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -160,22 +160,29 @@ class Solution { ``` Python3: +(版本一) 使用字典和集合 ```python class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: - val_dict = {} - ans = [] + # 使用哈希表存储一个数组中的所有元素 + table = {} for num in nums1: - val_dict[num] = 1 - + table[num] = table.get(num, 0) + 1 + + # 使用集合存储结果 + res = set() for num in nums2: - if num in val_dict.keys() and val_dict[num] == 1: - ans.append(num) - val_dict[num] = 0 + if num in table: + res.add(num) + del table[num] - return ans + return list(res) +``` +(版本二) 使用数组 + +```python -class Solution: # 使用数组方法 +class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: count1 = [0]*1001 count2 = [0]*1001 @@ -190,7 +197,14 @@ class Solution: # 使用数组方法 return result ``` +(版本三) 使用集合 +```python +class Solution: + def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: + return list(set(nums1) & set(nums2)) + +``` Go: ```go From eeae3282a87c334f726fc3c8cd5dabd5b71c4612 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 20:25:46 -0500 Subject: [PATCH 0277/1533] =?UTF-8?q?Update=200202.=E5=BF=AB=E4=B9=90?= =?UTF-8?q?=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\345\277\253\344\271\220\346\225\260.md" | 96 +++++++++++++++---- 1 file changed, 76 insertions(+), 20 deletions(-) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 5ac29e8639..7fe8cd8d75 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -108,23 +108,14 @@ class Solution { ``` Python: +(版本一)使用集合 ```python class Solution: - def isHappy(self, n: int) -> bool: - def calculate_happy(num): - sum_ = 0 - - # 从个位开始依次取,平方求和 - while num: - sum_ += (num % 10) ** 2 - num = num // 10 - return sum_ - - # 记录中间结果 + def isHappy(self, n: int) -> bool: record = set() while True: - n = calculate_happy(n) + n = self.get_sum(n) if n == 1: return True @@ -134,21 +125,86 @@ class Solution: else: record.add(n) -# python的另一种写法 - 通过字符串来计算各位平方和 + def get_sum(self,n: int) -> int: + new_num = 0 + while n: + n, r = divmod(n, 10) + new_num += r ** 2 + return new_num + ``` + (版本二)使用集合 + ```python +class Solution: + def isHappy(self, n: int) -> bool: + record = set() + while n not in record: + record.add(n) + new_num = 0 + n_str = str(n) + for i in n_str: + new_num+=int(i)**2 + if new_num==1: return True + else: n = new_num + return False +``` + (版本三)使用数组 + ```python class Solution: def isHappy(self, n: int) -> bool: record = [] while n not in record: record.append(n) - newn = 0 - nn = str(n) - for i in nn: - newn+=int(i)**2 - if newn==1: return True - n = newn + new_num = 0 + n_str = str(n) + for i in n_str: + new_num+=int(i)**2 + if new_num==1: return True + else: n = new_num return False ``` - + (版本四)使用快慢指针 + ```python +class Solution: + def isHappy(self, n: int) -> bool: + slow = n + fast = n + while self.get_sum(fast) != 1 and self.get_sum(self.get_sum(fast)): + slow = self.get_sum(slow) + fast = self.get_sum(self.get_sum(fast)) + if slow == fast: + return False + return True + def get_sum(self,n: int) -> int: + new_num = 0 + while n: + n, r = divmod(n, 10) + new_num += r ** 2 + return new_num +``` + (版本五)使用集合+精简 + ```python +class Solution: + def isHappy(self, n: int) -> bool: + seen = set() + while n != 1: + n = sum(int(i) ** 2 for i in str(n)) + if n in seen: + return False + seen.add(n) + return True +``` + (版本六)使用数组+精简 + ```python +class Solution: + def isHappy(self, n: int) -> bool: + seen = [] + while n != 1: + n = sum(int(i) ** 2 for i in str(n)) + if n in seen: + return False + seen.append(n) + return True +``` Go: ```go func isHappy(n int) bool { From 7118f36b66e1def134fda97e992affbd0c5cd79b Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 20:38:05 -0500 Subject: [PATCH 0278/1533] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\346\225\260\344\271\213\345\222\214.md" | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index eea3ba7a44..1af8787bab 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -151,7 +151,7 @@ public int[] twoSum(int[] nums, int target) { ``` Python: - +(版本一) 使用字典 ```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: @@ -163,6 +163,53 @@ class Solution: records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key return [] ``` +(版本二)使用集合 +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + #创建一个集合来存储我们目前看到的数字 + seen = set() + for i, num in enumerate(nums): + complement = target - num + if complement in seen: + return [nums.index(complement), i] + seen.add(num) +``` +(版本三)使用双指针 +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + # 对输入列表进行排序 + nums_sorted = sorted(nums) + + # 使用双指针 + left = 0 + right = len(nums_sorted) - 1 + while left < right: + current_sum = nums_sorted[left] + nums_sorted[right] + if current_sum == target: + # 如果和等于目标数,则返回两个数的下标 + left_index = nums.index(nums_sorted[left]) + right_index = nums.index(nums_sorted[right]) + if left_index == right_index: + right_index = nums[left_index+1:].index(nums_sorted[right]) + left_index + 1 + return [left_index, right_index] + elif current_sum < target: + # 如果总和小于目标,则将左侧指针向右移动 + left += 1 + else: + # 如果总和大于目标值,则将右指针向左移动 + right -= 1 +``` +(版本四)暴力法 +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i+1, len(nums)): + if nums[i] + nums[j] == target: + return [i,j] +``` Go: From 0905d2c7871daca4adb18566f86216a83e7894bc Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 20:46:57 -0500 Subject: [PATCH 0279/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\233\346\225\260\347\233\270\345\212\240II.md" | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 411b60e872..7818c02b5d 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -125,11 +125,11 @@ class Solution { ``` Python: - +(版本一) 使用字典 ```python class Solution(object): def fourSumCount(self, nums1, nums2, nums3, nums4): - # use a dict to store the elements in nums1 and nums2 and their sum + # 使用字典存储nums1和nums2中的元素及其和 hashmap = dict() for n1 in nums1: for n2 in nums2: @@ -138,7 +138,7 @@ class Solution(object): else: hashmap[n1+n2] = 1 - # if the -(a+b) exists in nums3 and nums4, we shall add the count + # 如果 -(n1+n2) 存在于nums3和nums4, 存入结果 count = 0 for n3 in nums3: for n4 in nums4: @@ -149,20 +149,18 @@ class Solution(object): ``` - +(版本二)使用 defaultdict ```python +from collections import defaultdict class Solution: def fourSumCount(self, nums1: list, nums2: list, nums3: list, nums4: list) -> int: - from collections import defaultdict # You may use normal dict instead. rec, cnt = defaultdict(lambda : 0), 0 - # To store the summary of all the possible combinations of nums1 & nums2, together with their frequencies. for i in nums1: for j in nums2: rec[i+j] += 1 - # To add up the frequencies if the corresponding value occurs in the dictionary for i in nums3: for j in nums4: - cnt += rec.get(-(i+j), 0) # No matched key, return 0. + cnt += rec.get(-(i+j), 0) return cnt ``` From 5cff41ab09e2b3866f3245c3d06d61b3b432f485 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 20:53:12 -0500 Subject: [PATCH 0280/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\225\260\347\233\270\345\212\240II.md" | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 7818c02b5d..31f0504f12 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -149,7 +149,29 @@ class Solution(object): ``` -(版本二)使用 defaultdict +(版本二) 使用字典 +```python +class Solution(object): + def fourSumCount(self, nums1, nums2, nums3, nums4): + # 使用字典存储nums1和nums2中的元素及其和 + hashmap = dict() + for n1 in nums1: + for n2 in nums2: + hashmap[n1+n2] = hashmap.get(n1+n2, 0) + 1 + + # 如果 -(n1+n2) 存在于nums3和nums4, 存入结果 + count = 0 + for n3 in nums3: + for n4 in nums4: + key = - n3 - n4 + if key in hashmap: + count += hashmap[key] + return count + + + +``` +(版本三)使用 defaultdict ```python from collections import defaultdict class Solution: From 38bc7021c6f15136e32785398ad17fdab9098d2b Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 21:05:34 -0500 Subject: [PATCH 0281/1533] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3.\350\265\216\351\207\221\344\277\241.md" | 86 +++++++------------ 1 file changed, 31 insertions(+), 55 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index d9a184b643..c2d80d8d89 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -142,34 +142,27 @@ class Solution { ``` -Python写法一(使用数组作为哈希表): - +(版本一)使用数组 ```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: - - arr = [0] * 26 - - for x in magazine: # 记录 magazine里各个字符出现次数 - arr[ord(x) - ord('a')] += 1 - - for x in ransomNote: # 在arr里对应的字符个数做--操作 - if arr[ord(x) - ord('a')] == 0: # 如果没有出现过直接返回 - return False - else: - arr[ord(x) - ord('a')] -= 1 - - return True + ransom_count = [0] * 26 + magazine_count = [0] * 26 + for c in ransomNote: + ransom_count[ord(c) - ord('a')] += 1 + for c in magazine: + magazine_count[ord(c) - ord('a')] += 1 + return all(ransom_count[i] <= magazine_count[i] for i in range(26)) ``` -Python写法二(使用defaultdict): +(版本二)使用defaultdict ```python +from collections import defaultdict + class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: - from collections import defaultdict - hashmap = defaultdict(int) for x in magazine: @@ -177,59 +170,42 @@ class Solution: for x in ransomNote: value = hashmap.get(x) - if value is None or value == 0: + if not value or not value: return False else: hashmap[x] -= 1 return True ``` - -Python写法三: +(版本三)使用字典 ```python -class Solution(object): - def canConstruct(self, ransomNote, magazine): - """ - :type ransomNote: str - :type magazine: str - :rtype: bool - """ - - # use a dict to store the number of letter occurance in ransomNote - hashmap = dict() - for s in ransomNote: - if s in hashmap: - hashmap[s] += 1 - else: - hashmap[s] = 1 - - # check if the letter we need can be found in magazine - for l in magazine: - if l in hashmap: - hashmap[l] -= 1 - - for key in hashmap: - if hashmap[key] > 0: +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + counts = {} + for c in magazine: + counts[c] = counts.get(c, 0) + 1 + for c in ransomNote: + if c not in counts or counts[c] == 0: return False - + counts[c] -= 1 return True ``` +(版本四)使用Counter -Python写法四: +```python +from collections import Counter + +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + return not Counter(ransomNote) - Counter(magazine) +``` +(版本五)使用count ```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: - c1 = collections.Counter(ransomNote) - c2 = collections.Counter(magazine) - x = c1 - c2 - #x只保留值大于0的符号,当c1里面的符号个数小于c2时,不会被保留 - #所以x只保留下了,magazine不能表达的 - if(len(x)==0): - return True - else: - return False + return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) ``` Go: From 1bc667eccdb247b9795e28a8793ec51fe9ed4883 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 21:06:23 -0500 Subject: [PATCH 0282/1533] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index c2d80d8d89..7572943786 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -206,6 +206,7 @@ class Solution: class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) + ``` Go: From 2ddbe7f3404ca4c7e70cf3264b6ba4e02327ca78 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 21:30:24 -0500 Subject: [PATCH 0283/1533] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\225\260\344\271\213\345\222\214.md" | 95 +++++++++++-------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 26c9eaa284..9cca779b21 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -298,61 +298,72 @@ class Solution { ``` Python: +(版本一) 双指针 ```Python class Solution: - def threeSum(self, nums): - ans = [] - n = len(nums) + def threeSum(self, nums: List[int]) -> List[List[int]]: + result = [] nums.sort() - # 找出a + b + c = 0 - # a = nums[i], b = nums[left], c = nums[right] - for i in range(n): - left = i + 1 - right = n - 1 - # 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了 - if nums[i] > 0: - break - if i >= 1 and nums[i] == nums[i - 1]: # 去重a + + for i in range(len(nums)): + # 如果第一个元素已经大于0,不需要进一步检查 + if nums[i] > 0: + return result + + # 跳过相同的元素以避免重复 + if i > 0 and nums[i] == nums[i - 1]: continue - while left < right: - total = nums[i] + nums[left] + nums[right] - if total > 0: - right -= 1 - elif total < 0: + + left = i + 1 + right = len(nums) - 1 + + while right > left: + sum_ = nums[i] + nums[left] + nums[right] + + if sum_ < 0: left += 1 + elif sum_ > 0: + right -= 1 else: - ans.append([nums[i], nums[left], nums[right]]) - # 去重逻辑应该放在找到一个三元组之后,对b 和 c去重 - while left != right and nums[left] == nums[left + 1]: left += 1 - while left != right and nums[right] == nums[right - 1]: right -= 1 - left += 1 + result.append([nums[i], nums[left], nums[right]]) + + # 跳过相同的元素以避免重复 + while right > left and nums[right] == nums[right - 1]: + right -= 1 + while right > left and nums[left] == nums[left + 1]: + left += 1 + right -= 1 - return ans + left += 1 + + return result ``` -Python (v3): +(版本二) 使用字典 ```python class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: - if len(nums) < 3: return [] - nums, res = sorted(nums), [] - for i in range(len(nums) - 2): - cur, l, r = nums[i], i + 1, len(nums) - 1 - if res != [] and res[-1][0] == cur: continue # Drop duplicates for the first time. - - while l < r: - if cur + nums[l] + nums[r] == 0: - res.append([cur, nums[l], nums[r]]) - # Drop duplicates for the second time in interation of l & r. Only used when target situation occurs, because that is the reason for dropping duplicates. - while l < r - 1 and nums[l] == nums[l + 1]: - l += 1 - while r > l + 1 and nums[r] == nums[r - 1]: - r -= 1 - if cur + nums[l] + nums[r] > 0: - r -= 1 + result = [] + nums.sort() + # 找出a + b + c = 0 + # a = nums[i], b = nums[j], c = -(a + b) + for i in range(len(nums)): + # 排序之后如果第一个元素已经大于零,那么不可能凑成三元组 + if nums[i] > 0: + break + if i > 0 and nums[i] == nums[i - 1]: #三元组元素a去重 + continue + d = {} + for j in range(i + 1, len(nums)): + if j > i + 2 and nums[j] == nums[j-1] == nums[j-2]: # 三元组元素b去重 + continue + c = 0 - (nums[i] + nums[j]) + if c in d: + result.append([nums[i], nums[j], c]) + d.pop(c) # 三元组元素c去重 else: - l += 1 - return res + d[nums[j]] = j + return result ``` Go: From a5bb942c95f405181ba311e14e1ad601bcfbf435 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 21:35:49 -0500 Subject: [PATCH 0284/1533] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 5f4c2ec920..8606b2ebd0 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -207,35 +207,40 @@ class Solution { ``` Python: +(版本一) 双指针 ```python -# 双指针法 -class Solution: - def fourSum(self, nums: List[int], target: int) -> List[List[int]]: - + def fourSum(self, nums: List[int], target: int) -> List[List[int]]: + result = [] nums.sort() - n = len(nums) - res = [] - for i in range(n): - if i > 0 and nums[i] == nums[i - 1]: continue # 对nums[i]去重 - for k in range(i+1, n): - if k > i + 1 and nums[k] == nums[k-1]: continue # 对nums[k]去重 - p = k + 1 - q = n - 1 - - while p < q: - if nums[i] + nums[k] + nums[p] + nums[q] > target: q -= 1 - elif nums[i] + nums[k] + nums[p] + nums[q] < target: p += 1 + for k in range(len(nums)): + if nums[k] > target and nums[k] >= 0: + break + if k > 0 and nums[k] == nums[k-1]: + continue + for i in range(k+1, len(nums)): + if nums[k] + nums[i] > target and nums[k] + nums[i] >= 0: + break + if i > k+1 and nums[i] == nums[i-1]: + continue + left, right = i+1, len(nums)-1 + while right > left: + if nums[k] + nums[i] + nums[left] + nums[right] > target: + right -= 1 + elif nums[k] + nums[i] + nums[left] + nums[right] < target: + left += 1 else: - res.append([nums[i], nums[k], nums[p], nums[q]]) - # 对nums[p]和nums[q]去重 - while p < q and nums[p] == nums[p + 1]: p += 1 - while p < q and nums[q] == nums[q - 1]: q -= 1 - p += 1 - q -= 1 - return res + result.append([nums[k], nums[i], nums[left], nums[right]]) + while right > left and nums[right] == nums[right-1]: + right -= 1 + while right > left and nums[left] == nums[left+1]: + left += 1 + right -= 1 + left += 1 + return result ``` +(版本二) 使用字典 + ```python -# 哈希表法 class Solution(object): def fourSum(self, nums, target): """ From dde92b1da3808200925d82f8f523da7b5cd93ca2 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 21:36:38 -0500 Subject: [PATCH 0285/1533] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 8606b2ebd0..4fee8358a6 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -209,34 +209,38 @@ class Solution { Python: (版本一) 双指针 ```python - def fourSum(self, nums: List[int], target: int) -> List[List[int]]: - result = [] +class Solution: + def fourSum(self, nums: List[int], target: int) -> List[List[int]]: nums.sort() - for k in range(len(nums)): - if nums[k] > target and nums[k] >= 0: + n = len(nums) + result = [] + for i in range(n): + if nums[i] > target and nums[i] > 0 and target > 0:# 剪枝(可省) break - if k > 0 and nums[k] == nums[k-1]: + if i > 0 and nums[i] == nums[i-1]:# 去重 continue - for i in range(k+1, len(nums)): - if nums[k] + nums[i] > target and nums[k] + nums[i] >= 0: + for j in range(i+1, n): + if nums[i] + nums[j] > target and target > 0: #剪枝(可省) break - if i > k+1 and nums[i] == nums[i-1]: + if j > i+1 and nums[j] == nums[j-1]: # 去重 continue - left, right = i+1, len(nums)-1 - while right > left: - if nums[k] + nums[i] + nums[left] + nums[right] > target: + left, right = j+1, n-1 + while left < right: + s = nums[i] + nums[j] + nums[left] + nums[right] + if s == target: + result.append([nums[i], nums[j], nums[left], nums[right]]) + while left < right and nums[left] == nums[left+1]: + left += 1 + while left < right and nums[right] == nums[right-1]: + right -= 1 + left += 1 right -= 1 - elif nums[k] + nums[i] + nums[left] + nums[right] < target: + elif s < target: left += 1 else: - result.append([nums[k], nums[i], nums[left], nums[right]]) - while right > left and nums[right] == nums[right-1]: - right -= 1 - while right > left and nums[left] == nums[left+1]: - left += 1 right -= 1 - left += 1 return result + ``` (版本二) 使用字典 From c4bc3197502473c5f14be392157bb83ddaa2dbeb Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 5 May 2023 21:39:52 -0500 Subject: [PATCH 0286/1533] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 4fee8358a6..a4d41d9bf9 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -252,36 +252,25 @@ class Solution(object): :type target: int :rtype: List[List[int]] """ - # use a dict to store value:showtimes - hashmap = dict() - for n in nums: - if n in hashmap: - hashmap[n] += 1 - else: - hashmap[n] = 1 + # 创建一个字典来存储输入列表中每个数字的频率 + freq = {} + for num in nums: + freq[num] = freq.get(num, 0) + 1 - # good thing about using python is you can use set to drop duplicates. + # 创建一个集合来存储最终答案,并遍历4个数字的所有唯一组合 ans = set() - # ans = [] # save results by list() for i in range(len(nums)): for j in range(i + 1, len(nums)): for k in range(j + 1, len(nums)): val = target - (nums[i] + nums[j] + nums[k]) - if val in hashmap: - # make sure no duplicates. + if val in freq: + # 确保没有重复 count = (nums[i] == val) + (nums[j] == val) + (nums[k] == val) - if hashmap[val] > count: - ans_tmp = tuple(sorted([nums[i], nums[j], nums[k], val])) - ans.add(ans_tmp) - # Avoiding duplication in list manner but it cause time complexity increases - # if ans_tmp not in ans: - # ans.append(ans_tmp) - else: - continue - return list(ans) - # if used list() to save results, just - # return ans + if freq[val] > count: + ans.add(tuple(sorted([nums[i], nums[j], nums[k], val]))) + return [list(x) for x in ans] + ``` Go: From b617532ce9b7af1cd163a0ccccb1f956f77e8cd2 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 01:55:48 -0500 Subject: [PATCH 0287/1533] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91?= =?UTF-8?q?=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\350\247\222\347\232\204\345\200\274.md" | 104 +++++++++++------- 1 file changed, 66 insertions(+), 38 deletions(-) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index 5ff254845d..26768c74dc 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -271,52 +271,80 @@ class Solution { ### Python - -递归: +(版本一)递归法 + 回溯 ```python class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: - max_depth = -float("INF") - leftmost_val = 0 - - def __traverse(root, cur_depth): - nonlocal max_depth, leftmost_val - if not root.left and not root.right: - if cur_depth > max_depth: - max_depth = cur_depth - leftmost_val = root.val - if root.left: - cur_depth += 1 - __traverse(root.left, cur_depth) - cur_depth -= 1 - if root.right: - cur_depth += 1 - __traverse(root.right, cur_depth) - cur_depth -= 1 - - __traverse(root, 0) - return leftmost_val + self.max_depth = float('-inf') + self.result = None + self.traversal(root, 0) + return self.result + + def traversal(self, node, depth): + if not node.left and not node.right: + if depth > self.max_depth: + self.max_depth = depth + self.result = node.val + return + + if node.left: + depth += 1 + self.traversal(node.left, depth) + depth -= 1 + if node.right: + depth += 1 + self.traversal(node.right, depth) + depth -= 1 + ``` -迭代 - 层序遍历: +(版本二)递归法+精简 ```python class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: - queue = deque() - if root: - queue.append(root) - result = 0 - while queue: - q_len = len(queue) - for i in range(q_len): - if i == 0: - result = queue[i].val - cur = queue.popleft() - if cur.left: - queue.append(cur.left) - if cur.right: - queue.append(cur.right) - return result + self.max_depth = float('-inf') + self.result = None + self.traversal(root, 0) + return self.result + + def traversal(self, node, depth): + if not node.left and not node.right: + if depth > self.max_depth: + self.max_depth = depth + self.result = node.val + return + + if node.left: + self.traversal(node.left, depth+1) + if node.right: + self.traversal(node.right, depth+1) +``` + +(版本三) 迭代法 +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +from collections import deque +class Solution: + def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: + queue = deque([root]) + while queue: + size = len(queue) + leftmost = queue[0].val + for i in range(size): + node = queue.popleft() + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + if not queue: + return leftmost + + ``` ### Go From bb8b112ae0079dd0d09d6a075e17888616ba3a55 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 16:51:15 -0500 Subject: [PATCH 0288/1533] =?UTF-8?q?Update=200344.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\255\227\347\254\246\344\270\262.md" | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 775cfc5872..03c82767d7 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -174,6 +174,7 @@ class Solution { ``` Python: +(版本一) 双指针 ```python class Solution: def reverseString(self, s: List[str]) -> None: @@ -190,7 +191,62 @@ class Solution: right -= 1 ``` - +(版本二) 使用栈 +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + stack = [] + for char in s: + stack.append(char) + for i in range(len(s)): + s[i] = stack.pop() + +``` +(版本三) 使用range +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + n = len(s) + for i in range(n // 2): + s[i], s[n - i - 1] = s[n - i - 1], s[i] + +``` +(版本四) 使用reversed +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + s[:] = reversed(s) + +``` +(版本五) 使用切片 +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + s[:] = s[::-1] + +``` +(版本六) 使用列表推导 +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + s[:] = [s[i] for i in range(len(s) - 1, -1, -1)] + +``` Go: ```Go func reverseString(s []byte) { From a37ae94618edd6104f9d0815bc0d97e185175f1e Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 16:54:34 -0500 Subject: [PATCH 0289/1533] =?UTF-8?q?Update=200344.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\215\350\275\254\345\255\227\347\254\246\344\270\262.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 03c82767d7..1c74f9aa46 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -183,8 +183,8 @@ class Solution: """ left, right = 0, len(s) - 1 - # 该方法已经不需要判断奇偶数,经测试后时间空间复杂度比用 for i in range(right//2)更低 - # 推荐该写法,更加通俗易懂 + # 该方法已经不需要判断奇偶数,经测试后时间空间复杂度比用 for i in range(len(s)//2)更低 + # 因为while每次循环需要进行条件判断,而range函数不需要,直接生成数字,因此时间复杂度更低。推荐使用range while left < right: s[left], s[right] = s[right], s[left] left += 1 From abcedd41a87e6923cfd39705765cdd5716b31ad0 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 16:59:12 -0500 Subject: [PATCH 0290/1533] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87Offer05.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index 2e1ee1de1b..833d46284b 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -266,6 +266,7 @@ func replaceSpace(s string) string { python: +####因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) ```python class Solution: def replaceSpace(self, s: str) -> str: From 3958cc166a1ab090f0060e45bab41535170cd186 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 16:59:48 -0500 Subject: [PATCH 0291/1533] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87Offer05.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...fer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index 833d46284b..d930702fc1 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -265,8 +265,8 @@ func replaceSpace(s string) string { -python: -####因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) +#python: +#因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) ```python class Solution: def replaceSpace(self, s: str) -> str: From 799325bacebd60e3088e5ab4a82ef599ba1d3e47 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 17:00:54 -0500 Subject: [PATCH 0292/1533] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87Offer05.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...fer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index d930702fc1..a6c32d3054 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -265,8 +265,8 @@ func replaceSpace(s string) string { -#python: -#因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) +# python: +# 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) ```python class Solution: def replaceSpace(self, s: str) -> str: From dc7ac6337d5ae0a462cc332a5a1a68de43095212 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 17:01:39 -0500 Subject: [PATCH 0293/1533] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87Offer05.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...fer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index a6c32d3054..d86cc6dcf9 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -265,8 +265,8 @@ func replaceSpace(s string) string { -# python: -# 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) +python: +#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) ```python class Solution: def replaceSpace(self, s: str) -> str: From c09089accda7c3cc46bc283eb63edfc9563894ab Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 17:10:18 -0500 Subject: [PATCH 0294/1533] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87Offer05.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\347\251\272\346\240\274.md" | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index d86cc6dcf9..dbad781ef3 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -266,7 +266,8 @@ func replaceSpace(s string) string { python: -#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能不为O(1) +#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1) +(版本一)转换成列表,并且添加相匹配的空间,然后进行填充 ```python class Solution: def replaceSpace(self, s: str) -> str: @@ -291,14 +292,22 @@ class Solution: return ''.join(res) ``` - +(版本二)添加空列表,添加匹配的结果 +```python +class Solution: + def replaceSpace(self, s: str) -> str: + res = [] + for i in range(len(s)): + if s[i] == ' ': + res.append('%20') + else: + res.append(s[i]) + return ''.join(res) +``` +(版本三)使用切片 ```python class Solution: def replaceSpace(self, s: str) -> str: - # method 1 - Very rude - return "%20".join(s.split(" ")) - - # method 2 - Reverse the s when counting in for loop, then update from the end. n = len(s) for e, i in enumerate(s[::-1]): print(i, e) @@ -307,7 +316,18 @@ class Solution: print("") return s ``` - +(版本四)使用join + split +```python +class Solution: + def replaceSpace(self, s: str) -> str: + return "%20".join(s.split(" ")) +``` +(版本五)使用replace +```python +class Solution: + def replaceSpace(self, s: str) -> str: + return s.replace(' ', '%20') +``` javaScript: ```js From c75140d2572be2030e64a8aa18a7cc336b7a5a74 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 17:26:08 -0500 Subject: [PATCH 0295/1533] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\347\232\204\345\215\225\350\257\215.md" | 142 +++--------------- 1 file changed, 24 insertions(+), 118 deletions(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 8fa7c77c9e..a323226416 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -434,134 +434,40 @@ class Solution { ``` python: - +(版本一)先删除空白,然后整个反转,最后单词反转。 +### 因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1) ```Python class Solution: - #1.去除多余的空格 - def trim_spaces(self, s): - n = len(s) - left = 0 - right = n-1 - - while left <= right and s[left] == ' ': #去除开头的空格 - left += 1 - while left <= right and s[right] == ' ': #去除结尾的空格 - right -= 1 - tmp = [] - while left <= right: #去除单词中间多余的空格 - if s[left] != ' ': - tmp.append(s[left]) - elif tmp[-1] != ' ': #当前位置是空格,但是相邻的上一个位置不是空格,则该空格是合理的 - tmp.append(s[left]) - left += 1 - return tmp - - #2.翻转字符数组 - def reverse_string(self, nums, left, right): - while left < right: - nums[left], nums[right] = nums[right], nums[left] - left += 1 - right -= 1 - return None - - #3.翻转每个单词 - def reverse_each_word(self, nums): - start = 0 - end = 0 - n = len(nums) - while start < n: - while end < n and nums[end] != ' ': - end += 1 - self.reverse_string(nums, start, end-1) - start = end + 1 - end += 1 - return None - -#4.翻转字符串里的单词 - def reverseWords(self, s): #测试用例:"the sky is blue" - l = self.trim_spaces(s) #输出:['t', 'h', 'e', ' ', 's', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e' - self.reverse_string(l, 0, len(l)-1) #输出:['e', 'u', 'l', 'b', ' ', 's', 'i', ' ', 'y', 'k', 's', ' ', 'e', 'h', 't'] - self.reverse_each_word(l) #输出:['b', 'l', 'u', 'e', ' ', 'i', 's', ' ', 's', 'k', 'y', ' ', 't', 'h', 'e'] - return ''.join(l) #输出:blue is sky the - + def reverseWords(self, s: str) -> str: + # 删除前后空白 + s = s.strip() + # 反转整个字符串 + s = s[::-1] + # 将字符串拆分为单词,并反转每个单词 + s = ' '.join(word[::-1] for word in s.split()) + return s ``` +(版本二)使用双指针 ```python class Solution: def reverseWords(self, s: str) -> str: - # method 1 - Rude but work & efficient method. - s_list = [i for i in s.split(" ") if len(i) > 0] - return " ".join(s_list[::-1]) - - # method 2 - Carlo's idea - def trim_head_tail_space(ss: str): - p = 0 - while p < len(ss) and ss[p] == " ": - p += 1 - return ss[p:] - - # Trim the head and tail space - s = trim_head_tail_space(s) - s = trim_head_tail_space(s[::-1])[::-1] - - pf, ps, s = 0, 0, s[::-1] # Reverse the string. - while pf < len(s): - if s[pf] == " ": - # Will not excede. Because we have clean the tail space. - if s[pf] == s[pf + 1]: - s = s[:pf] + s[pf + 1:] - continue - else: - s = s[:ps] + s[ps: pf][::-1] + s[pf:] - ps, pf = pf + 1, pf + 2 - else: - pf += 1 - return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions, + # 将字符串拆分为单词,即转换成列表类型 + words = s.split() + + # 反转单词 + left, right = 0, len(words) - 1 + while left < right: + words[left], words[right] = words[right], words[left] + left += 1 + right -= 1 + + # 将列表转换成字符串 + return " ".join(words) ``` -```python -class Solution: # 使用双指针法移除空格 - def reverseWords(self, s: str) -> str: - - def removeextraspace(s): - start = 0; end = len(s)-1 - while s[start]==' ': - start+=1 - while s[end]==' ': - end-=1 - news = list(s[start:end+1]) - slow = fast = 0 - while fast0 and news[fast]==news[fast-1]==' ': - fast+=1 - news[slow]=news[fast] - slow+=1; fast+=1 - #return "".join(news[:slow]) - return news[:slow] - - def reversestr(s): - left,right = 0,len(s)-1 - news = list(s) - while left Date: Sat, 6 May 2023 17:26:56 -0500 Subject: [PATCH 0296/1533] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index a323226416..062ccca538 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -435,7 +435,7 @@ class Solution { python: (版本一)先删除空白,然后整个反转,最后单词反转。 -### 因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1) +#### 因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1) ```Python class Solution: def reverseWords(self, s: str) -> str: From 7cfd38940249adb1f659da29b69edc2ae3083113 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 17:52:08 -0500 Subject: [PATCH 0297/1533] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87Offer58-II?= =?UTF-8?q?.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\255\227\347\254\246\344\270\262.md" | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index f368514f4b..6cd8845609 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -142,15 +142,16 @@ class Solution { ``` python: +(版本一)使用切片 ```python -# 方法一:可以使用切片方法 class Solution: def reverseLeftWords(self, s: str, n: int) -> str: - return s[n:] + s[0:n] + return s[n:] + s[:n] ``` +(版本二)使用reversed + join + ```python -# 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法 class Solution: def reverseLeftWords(self, s: str, n: int) -> str: s = list(s) @@ -161,32 +162,29 @@ class Solution: return "".join(s) ``` +(版本三)自定义reversed函数 ```python -# 方法三:如果连reversed也不让使用,那么自己手写一个 class Solution: - def reverseLeftWords(self, s: str, n: int) -> str: - def reverse_sub(lst, left, right): - while left < right: - lst[left], lst[right] = lst[right], lst[left] - left += 1 - right -= 1 + def reverseLeftWords(self, s: str, n: int) -> str: + s_list = list(s) - res = list(s) - end = len(res) - 1 - reverse_sub(res, 0, n - 1) - reverse_sub(res, n, end) - reverse_sub(res, 0, end) - return ''.join(res) + self.reverse(s_list, 0, n - 1) + self.reverse(s_list, n, len(s_list) - 1) + self.reverse(s_list, 0, len(s_list) - 1) -# 同方法二 -# 时间复杂度:O(n) -# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改 + return ''.join(s_list) + + def reverse(self, s, start, end): + while start < end: + s[start], s[end] = s[end], s[start] + start += 1 + end -= 1 ``` +(版本四)使用 模 +下标 ```python 3 -#方法四:考虑不能用切片的情况下,利用模+下标实现 class Solution: def reverseLeftWords(self, s: str, n: int) -> str: new_s = '' @@ -196,17 +194,21 @@ class Solution: return new_s ``` +(版本五)使用 模 + 切片 ```python 3 -# 方法五:另类的切片方法 class Solution: def reverseLeftWords(self, s: str, n: int) -> str: - n = len(s) - s = s + s - return s[k : n+k] + l = len(s) + # 复制输入字符串与它自己连接 + s = s + s + + # 计算旋转字符串的起始索引 + k = n % (l * 2) + + # 从连接的字符串中提取旋转后的字符串并返回 + return s[k : k + l] -# 时间复杂度:O(n) -# 空间复杂度:O(n) ``` Go: From 3cf55028680832e76c82f9f819443b79339f1d49 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 19:39:34 -0500 Subject: [PATCH 0298/1533] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strSt?= =?UTF-8?q?r.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 99 +++++++++++-------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 263c168995..5cc0499467 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -692,23 +692,38 @@ class Solution { ``` Python3: +(版本一)前缀表(不减一) + ```python -//暴力解法: -class Solution(object): - def strStr(self, haystack, needle): - """ - :type haystack: str - :type needle: str - :rtype: int - """ - m, n = len(haystack), len(needle) - for i in range(m): - if haystack[i:i+n] == needle: - return i - return -1 +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + if len(needle) == 0: + return 0 + next = self.getNext(needle) + j = 0 + for i in range(len(haystack)): + while j >= 1 and haystack[i] != needle[j]: + j = next[j-1] + if haystack[i] == needle[j]: + j += 1 + if j == len(needle): + return i - len(needle) + 1 + return -1 + + def getNext(self, needle): + next = [0] * len(needle) + j = 0 + next[0] = j + for i in range(1, len(needle)): + while j >= 1 and needle[i] != needle[j]: + j = next[j-1] + if needle[i] == needle[j]: + j += 1 + next[i] = j + return next ``` +(版本二)前缀表(减一) ```python -// 方法一 class Solution: def strStr(self, haystack: str, needle: str) -> int: a = len(needle) @@ -739,8 +754,11 @@ class Solution: return next ``` + +(版本三)前缀表(减一) + + ```python -// 方法二 class Solution: def strStr(self, haystack: str, needle: str) -> int: a = len(needle) @@ -774,35 +792,36 @@ class Solution: return next ``` +(版本四)暴力法 +```python +class Solution(object): + def strStr(self, haystack, needle): + """ + :type haystack: str + :type needle: str + :rtype: int + """ + m, n = len(haystack), len(needle) + for i in range(m): + if haystack[i:i+n] == needle: + return i + return -1 +``` +(版本五)使用 index ```python -// 前缀表(不减一)Python实现 class Solution: def strStr(self, haystack: str, needle: str) -> int: - if len(needle) == 0: - return 0 - next = self.getNext(needle) - j = 0 - for i in range(len(haystack)): - while j >= 1 and haystack[i] != needle[j]: - j = next[j-1] - if haystack[i] == needle[j]: - j += 1 - if j == len(needle): - return i - len(needle) + 1 - return -1 - - def getNext(self, needle): - next = [0] * len(needle) - j = 0 - next[0] = j - for i in range(1, len(needle)): - while j >= 1 and needle[i] != needle[j]: - j = next[j-1] - if needle[i] == needle[j]: - j += 1 - next[i] = j - return next + try: + return haystack.index(needle) + except ValueError: + return -1 ``` +(版本六)使用 find +```python +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + return haystack.find(needle) + Go: From 1fbd900c6a942b86d9c1d2f394ed89023e22a50d Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 19:47:55 -0500 Subject: [PATCH 0299/1533] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strSt?= =?UTF-8?q?r.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 131 ++++++------------ 1 file changed, 45 insertions(+), 86 deletions(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 5cc0499467..ff13db3910 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -692,107 +692,66 @@ class Solution { ``` Python3: -(版本一)前缀表(不减一) - +(版本一)前缀表(减一) ```python class Solution: - def strStr(self, haystack: str, needle: str) -> int: - if len(needle) == 0: - return 0 - next = self.getNext(needle) - j = 0 - for i in range(len(haystack)): - while j >= 1 and haystack[i] != needle[j]: - j = next[j-1] - if haystack[i] == needle[j]: - j += 1 - if j == len(needle): - return i - len(needle) + 1 - return -1 - - def getNext(self, needle): - next = [0] * len(needle) - j = 0 + def getNext(self, next, s): + j = -1 next[0] = j - for i in range(1, len(needle)): - while j >= 1 and needle[i] != needle[j]: - j = next[j-1] - if needle[i] == needle[j]: + for i in range(1, len(s)): + while j >= 0 and s[i] != s[j+1]: + j = next[j] + if s[i] == s[j+1]: j += 1 next[i] = j - return next -``` -(版本二)前缀表(减一) -```python -class Solution: + def strStr(self, haystack: str, needle: str) -> int: - a = len(needle) - b = len(haystack) - if a == 0: + if not needle: return 0 - next = self.getnext(a,needle) - p=-1 - for j in range(b): - while p >= 0 and needle[p+1] != haystack[j]: - p = next[p] - if needle[p+1] == haystack[j]: - p += 1 - if p == a-1: - return j-a+1 + next = [0] * len(needle) + self.getNext(next, needle) + j = -1 + for i in range(len(haystack)): + while j >= 0 and haystack[i] != needle[j+1]: + j = next[j] + if haystack[i] == needle[j+1]: + j += 1 + if j == len(needle) - 1: + return i - len(needle) + 1 return -1 - - def getnext(self,a,needle): - next = ['' for i in range(a)] - k = -1 - next[0] = k - for i in range(1, len(needle)): - while (k > -1 and needle[k+1] != needle[i]): - k = next[k] - if needle[k+1] == needle[i]: - k += 1 - next[i] = k - return next ``` - - -(版本三)前缀表(减一) - +(版本二)前缀表(不减一) ```python class Solution: + def getNext(self, next: List[int], s: str) -> None: + j = 0 + next[0] = 0 + for i in range(1, len(s)): + while j > 0 and s[i] != s[j]: + j = next[j - 1] + if s[i] == s[j]: + j += 1 + next[i] = j + def strStr(self, haystack: str, needle: str) -> int: - a = len(needle) - b = len(haystack) - if a == 0: + if len(needle) == 0: return 0 - i = j = 0 - next = self.getnext(a, needle) - while(i < b and j < a): - if j == -1 or needle[j] == haystack[i]: - i += 1 - j += 1 - else: - j = next[j] - if j == a: - return i-j - else: - return -1 - - def getnext(self, a, needle): - next = ['' for i in range(a)] - j, k = 0, -1 - next[0] = k - while(j < a-1): - if k == -1 or needle[k] == needle[j]: - k += 1 + next = [0] * len(needle) + self.getNext(next, needle) + j = 0 + for i in range(len(haystack)): + while j > 0 and haystack[i] != needle[j]: + j = next[j - 1] + if haystack[i] == needle[j]: j += 1 - next[j] = k - else: - k = next[k] - return next + if j == len(needle): + return i - len(needle) + 1 + return -1 ``` -(版本四)暴力法 + +(版本三)暴力法 ```python class Solution(object): def strStr(self, haystack, needle): @@ -807,7 +766,7 @@ class Solution(object): return i return -1 ``` -(版本五)使用 index +(版本四)使用 index ```python class Solution: def strStr(self, haystack: str, needle: str) -> int: @@ -816,7 +775,7 @@ class Solution: except ValueError: return -1 ``` -(版本六)使用 find +(版本五)使用 find ```python class Solution: def strStr(self, haystack: str, needle: str) -> int: From 995028d7c45099f71d9ef398ad92a8b655e0c4f9 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 19:48:45 -0500 Subject: [PATCH 0300/1533] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strSt?= =?UTF-8?q?r.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0028.\345\256\236\347\216\260strStr.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index ff13db3910..b18131f391 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -780,7 +780,7 @@ class Solution: class Solution: def strStr(self, haystack: str, needle: str) -> int: return haystack.find(needle) - +``` Go: From e8b4db9d7a5cf58e56e58254195cc2a98d926479 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 19:49:19 -0500 Subject: [PATCH 0301/1533] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strSt?= =?UTF-8?q?r.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0028.\345\256\236\347\216\260strStr.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index b18131f391..86308b4799 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -780,6 +780,7 @@ class Solution: class Solution: def strStr(self, haystack: str, needle: str) -> int: return haystack.find(needle) + ``` Go: From 5000a978fd41dd32a639935459365fd58e691c37 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 20:05:02 -0500 Subject: [PATCH 0302/1533] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\345\255\227\347\254\246\344\270\262.md" | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 98c02a2544..5d56ad18fb 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -264,8 +264,7 @@ class Solution { Python: -这里使用了前缀表统一减一的实现方式 - +(版本一) 前缀表 减一 ```python class Solution: def repeatedSubstringPattern(self, s: str) -> bool: @@ -289,7 +288,7 @@ class Solution: return nxt ``` -前缀表(不减一)的代码实现 +(版本二) 前缀表 不减一 ```python class Solution: @@ -314,6 +313,40 @@ class Solution: return nxt ``` + +(版本三) 使用 find + +```python +class Solution: + def repeatedSubstringPattern(self, s: str) -> bool: + n = len(s) + if n <= 1: + return False + ss = s[1:] + s[:-1] + print(ss.find(s)) + return ss.find(s) != -1 +``` + +(版本四) 暴力法 + +```python +class Solution: + def repeatedSubstringPattern(self, s: str) -> bool: + n = len(s) + if n <= 1: + return False + + substr = "" + for i in range(1, n//2 + 1): + if n % i == 0: + substr = s[:i] + if substr * (n//i) == s: + return True + + return False +``` + + Go: 这里使用了前缀表统一减一的实现方式 From f97ff3a1beeb938107bd76dfa38dac3c93430095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 7 May 2023 09:28:28 +0800 Subject: [PATCH 0303/1533] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 062ccca538..4474f1c613 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -435,7 +435,7 @@ class Solution { python: (版本一)先删除空白,然后整个反转,最后单词反转。 -#### 因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1) +**因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)** ```Python class Solution: def reverseWords(self, s: str) -> str: From 3f45b56cef522ca54b648ded97a8d3b0c6a0f919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 7 May 2023 09:47:01 +0800 Subject: [PATCH 0304/1533] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\274\202\344\275\215\350\257\215.md" | 68 +------------------ 1 file changed, 1 insertion(+), 67 deletions(-) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 0e5683c794..3f4b00dc30 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -122,21 +122,7 @@ class Solution { ``` Python: -(版本一) 使用数组 -```python -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - record = [0] * 26 - for i in range(len(s)): - record[ord(s[i]) - ord("a")] += 1 - for i in range(len(t)): - record[ord(t[i]) - ord("a")] -= 1 - for i in range(26): - if record[i] != 0: - return False - return True -``` -(版本二) 使用数组 + ```python class Solution: def isAnagram(self, s: str, t: str) -> bool: @@ -153,59 +139,7 @@ class Solution: return True ``` -(版本三) 使用defaultdict - -```python -from collections import defaultdict - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - - s_dict = defaultdict(int) - t_dict = defaultdict(int) - - for x in s: - s_dict[x] += 1 - - for x in t: - t_dict[x] += 1 - - return s_dict == t_dict -``` -(版本四) 使用字典 - -```python -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - if len(s) != len(t): - return False - - hash_table_s = {} - hash_table_t = {} - - for i in range(len(s)): - hash_table_s[s[i]] = hash_table_s.get(s[i], 0) + 1 - hash_table_t[t[i]] = hash_table_t.get(t[i], 0) + 1 - - return hash_table_s == hash_table_t -``` - -(版本五) 使用排序 -```python -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - return sorted(s) == sorted(t) -``` -(版本六) 使用Counter - -```python -from collections import Counter - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - return Counter(s) == Counter(t) -``` Go: ```go From 6e88b2034d70d261a9cd0388cdcf906e50677195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 7 May 2023 09:54:15 +0800 Subject: [PATCH 0305/1533] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\274\202\344\275\215\350\257\215.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 3f4b00dc30..09674ecdaa 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -138,7 +138,31 @@ class Solution: return False return True ``` +Python写法二(没有使用数组作为哈希表,只是介绍defaultdict这样一种解题思路): +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + from collections import defaultdict + + s_dict = defaultdict(int) + t_dict = defaultdict(int) + for x in s: + s_dict[x] += 1 + + for x in t: + t_dict[x] += 1 + return s_dict == t_dict +``` +Python写法三(没有使用数组作为哈希表,只是介绍Counter这种更方便的解题思路): + +```python +class Solution(object): + def isAnagram(self, s: str, t: str) -> bool: + from collections import Counter + a_count = Counter(s) + b_count = Counter(t) + return a_count == b_count Go: From d2abce01594ae1bbd73889040c82b35455b3d93e Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 6 May 2023 22:54:54 -0500 Subject: [PATCH 0306/1533] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 269 +++++++++++------- 1 file changed, 160 insertions(+), 109 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 5958de93e6..9ee2464ee4 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -451,140 +451,191 @@ class Solution { ### 0112.路径总和 -**递归** - +(版本一) 递归 ```python -class solution: - def haspathsum(self, root: treenode, targetsum: int) -> bool: - def isornot(root, targetsum) -> bool: - if (not root.left) and (not root.right) and targetsum == 0: - return true # 遇到叶子节点,并且计数为0 - if (not root.left) and (not root.right): - return false # 遇到叶子节点,计数不为0 - if root.left: - targetsum -= root.left.val # 左节点 - if isornot(root.left, targetsum): return true # 递归,处理左节点 - targetsum += root.left.val # 回溯 - if root.right: - targetsum -= root.right.val # 右节点 - if isornot(root.right, targetsum): return true # 递归,处理右节点 - targetsum += root.right.val # 回溯 - return false - - if root == none: - return false # 别忘记处理空treenode - else: - return isornot(root, targetsum - root.val) +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def traversal(self, cur: TreeNode, count: int) -> bool: + if not cur.left and not cur.right and count == 0: # 遇到叶子节点,并且计数为0 + return True + if not cur.left and not cur.right: # 遇到叶子节点直接返回 + return False + + if cur.left: # 左 + count -= cur.left.val + if self.traversal(cur.left, count): # 递归,处理节点 + return True + count += cur.left.val # 回溯,撤销处理结果 -class Solution: # 简洁版 - def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: - if not root: return False - if root.left==root.right==None and root.val == targetSum: return True - return self.hasPathSum(root.left,targetSum-root.val) or self.hasPathSum(root.right,targetSum-root.val) + if cur.right: # 右 + count -= cur.right.val + if self.traversal(cur.right, count): # 递归,处理节点 + return True + count += cur.right.val # 回溯,撤销处理结果 + + return False + + def hasPathSum(self, root: TreeNode, sum: int) -> bool: + if root is None: + return False + return self.traversal(root, sum - root.val) ``` -**迭代 - 层序遍历** - +(版本二) 递归 + 精简 ```python -class solution: - def haspathsum(self, root: treenode, targetsum: int) -> bool: +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def hasPathSum(self, root: TreeNode, sum: int) -> bool: if not root: - return false - - stack = [] # [(当前节点,路径数值), ...] - stack.append((root, root.val)) - - while stack: - cur_node, path_sum = stack.pop() - - if not cur_node.left and not cur_node.right and path_sum == targetsum: - return true - - if cur_node.right: - stack.append((cur_node.right, path_sum + cur_node.right.val)) + return False + if not root.left and not root.right and sum == root.val: + return True + return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val) + +``` +(版本三) 迭代 +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def hasPathSum(self, root: TreeNode, sum: int) -> bool: + if not root: + return False + # 此时栈里要放的是pair<节点指针,路径数值> + st = [(root, root.val)] + while st: + node, path_sum = st.pop() + # 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true + if not node.left and not node.right and path_sum == sum: + return True + # 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来 + if node.right: + st.append((node.right, path_sum + node.right.val)) + # 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来 + if node.left: + st.append((node.left, path_sum + node.left.val)) + return False + + + +``` - if cur_node.left: - stack.append((cur_node.left, path_sum + cur_node.left.val)) - return false -``` ### 0113.路径总和-ii -**递归** - +(版本一) 递归 ```python -class solution: - def pathsum(self, root: treenode, targetsum: int) -> list[list[int]]: - - def traversal(cur_node, remain): - if not cur_node.left and not cur_node.right: - if remain == 0: - result.append(path[:]) - return - - if cur_node.left: - path.append(cur_node.left.val) - traversal(cur_node.left, remain-cur_node.left.val) - path.pop() +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def __init__(self): + self.result = [] + self.path = [] + + def traversal(self, cur, count): + if not cur.left and not cur.right and count == 0: # 遇到了叶子节点且找到了和为sum的路径 + self.result.append(self.path[:]) + return + + if not cur.left and not cur.right: # 遇到叶子节点而没有找到合适的边,直接返回 + return + + if cur.left: # 左 (空节点不遍历) + self.path.append(cur.left.val) + count -= cur.left.val + self.traversal(cur.left, count) # 递归 + count += cur.left.val # 回溯 + self.path.pop() # 回溯 + + if cur.right: # 右 (空节点不遍历) + self.path.append(cur.right.val) + count -= cur.right.val + self.traversal(cur.right, count) # 递归 + count += cur.right.val # 回溯 + self.path.pop() # 回溯 - if cur_node.right: - path.append(cur_node.right.val) - traversal(cur_node.right, remain-cur_node.right.val) - path.pop() + return - result, path = [], [] + def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]: + self.result.clear() + self.path.clear() if not root: - return [] - path.append(root.val) - traversal(root, targetsum - root.val) - return result + return self.result + self.path.append(root.val) # 把根节点放进路径 + self.traversal(root, sum - root.val) + return self.result ``` -**迭代法,用第二个队列保存目前的总和与路径** - +(版本二) 递归 + 精简 ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: - if not root: - return [] - que, temp = deque([root]), deque([(root.val, [root.val])]) + def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]: + result = [] - while que: - for _ in range(len(que)): - node = que.popleft() - value, path = temp.popleft() - if (not node.left) and (not node.right): - if value == targetSum: - result.append(path) - if node.left: - que.append(node.left) - temp.append((node.left.val+value, path+[node.left.val])) - if node.right: - que.append(node.right) - temp.append((node.right.val+value, path+[node.right.val])) + self.traversal(root, targetSum, [], result) return result + def traversal(self,node, count, path, result): + if not node: + return + path.append(node.val) + count -= node.val + if not node.left and not node.right and count == 0: + result.append(list(path)) + self.traversal(node.left, count, path, result) + self.traversal(node.right, count, path, result) + path.pop() ``` - -**迭代法,前序遍历** - +(版本三) 迭代 ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: - if not root: return [] - stack, path_stack,result = [[root,root.val]],[[root.val]],[] + def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]: + if not root: + return [] + stack = [(root, [root.val])] + res = [] while stack: - cur,cursum = stack.pop() - path = path_stack.pop() - if cur.left==cur.right==None: - if cursum==targetSum: result.append(path) - if cur.right: - stack.append([cur.right,cursum+cur.right.val]) - path_stack.append(path+[cur.right.val]) - if cur.left: - stack.append([cur.left,cursum+cur.left.val]) - path_stack.append(path+[cur.left.val]) - return result + node, path = stack.pop() + if not node.left and not node.right and sum(path) == targetSum: + res.append(path) + if node.right: + stack.append((node.right, path + [node.right.val])) + if node.left: + stack.append((node.left, path + [node.left.val])) + return res + + + ``` ## go From 61d479ec47df5d3a1743c4de97ba0e4175954959 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Mon, 8 May 2023 15:59:31 -0400 Subject: [PATCH 0307/1533] improving java solution by using stringBuilder 1. improving java solution by using stringBuilder 2. adding explanation in the code --- ...\345\216\237IP\345\234\260\345\235\200.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 9d4d59180e..7b15aad987 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -314,6 +314,47 @@ class Solution { return true; } } +//方法一:但使用stringBuilder,故优化时间、空间复杂度,因为向字符串插入字符时无需复制整个字符串,从而减少了操作的时间复杂度,也不用开新空间存subString,从而减少了空间复杂度。 +class Solution { + List result = new ArrayList<>(); + public List restoreIpAddresses(String s) { + StringBuilder sb = new StringBuilder(s); + backTracking(sb, 0, 0); + return result; + } + private void backTracking(StringBuilder s, int startIndex, int dotCount){ + if(dotCount == 3){ + if(isValid(s, startIndex, s.length() - 1)){ + result.add(s.toString()); + } + return; + } + for(int i = startIndex; i < s.length(); i++){ + if(isValid(s, startIndex, i)){ + s.insert(i + 1, '.'); + backTracking(s, i + 2, dotCount + 1); + s.deleteCharAt(i + 1); + }else{ + break; + } + } + } + //[start, end] + private boolean isValid(StringBuilder s, int start, int end){ + if(start > end) + return false; + if(s.charAt(start) == '0' && start != end) + return false; + int num = 0; + for(int i = start; i <= end; i++){ + int digit = s.charAt(i) - '0'; + num = num * 10 + digit; + if(num > 255) + return false; + } + return true; + } +} //方法二:比上面的方法时间复杂度低,更好地剪枝,优化时间复杂度 class Solution { @@ -358,6 +399,7 @@ class Solution { } ``` + ## python python2: From 898c42a990b57ab9824d53efd7a2b647dd2724d9 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Mon, 8 May 2023 18:10:28 -0400 Subject: [PATCH 0308/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E7=94=A8set2k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\345\255\220\345\272\217\345\210\227.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 436dbf01c8..2a58f55072 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -203,6 +203,30 @@ public: ### Java +```Java +//using set, aligned with the unimproved method +class Solution { + List> result = new ArrayList<>(); + List path = new ArrayList<>(); + public List> findSubsequences(int[] nums) { + backTracking(nums, 0); + return result; + } + private void backTracking(int[] nums, int startIndex){ + if(path.size() >= 2) + result.add(new ArrayList<>(path)); + HashSet hs = new HashSet<>(); + for(int i = startIndex; i < nums.length; i++){ + if(!path.isEmpty() && path.get(path.size() -1 ) > nums[i] || hs.contains(nums[i])) + continue; + hs.add(nums[i]); + path.add(nums[i]); + backTracking(nums, i + 1); + path.remove(path.size() - 1); + } + } +} +``` ```java class Solution { From 40f8230fd86b475e87d970f973042a2b4cbee613 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 9 May 2023 00:20:58 -0500 Subject: [PATCH 0309/1533] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python 代码重复 --- ...40\344\272\214\345\217\211\346\240\221.md" | 38 +------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index adb374f957..89afad084a 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -400,8 +400,6 @@ public: }; ``` -## Python - # 105.从前序与中序遍历序列构造二叉树 @@ -657,38 +655,6 @@ class Solution { ## Python -```python -class Solution: - def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: - # 第一步: 特殊情况讨论: 树为空. 或者说是递归终止条件 - if not postorder: - return - - # 第二步: 后序遍历的最后一个就是当前的中间节点 - root_val = postorder[-1] - root = TreeNode(root_val) - - # 第三步: 找切割点. - root_index = inorder.index(root_val) - - # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. - left_inorder = inorder[:root_index] - right_inorder = inorder[root_index + 1:] - - # 第五步: 切割postorder数组. 得到postorder数组的左,右半边. - # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. - left_postorder = postorder[:len(left_inorder)] - right_postorder = postorder[len(left_inorder): len(postorder) - 1] - - - # 第六步: 递归 - root.left = self.buildTree(left_inorder, left_postorder) - root.right = self.buildTree(right_inorder, right_postorder) - - # 第七步: 返回答案 - return root -``` - 105.从前序与中序遍历序列构造二叉树 ```python @@ -717,7 +683,7 @@ class Solution: # 第六步: 递归 root.left = self.buildTree(preorder_left, inorder_left) root.right = self.buildTree(preorder_right, inorder_right) - + # 第七步: 返回答案 return root ``` @@ -749,7 +715,7 @@ class Solution: # 第六步: 递归 root.left = self.buildTree(inorder_left, postorder_left) root.right = self.buildTree(inorder_right, postorder_right) - + # 第七步: 返回答案 return root ``` From 7eea41ee0e18381088bce52f7ea218e1ffc78c77 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 9 May 2023 14:04:22 -0500 Subject: [PATCH 0310/1533] =?UTF-8?q?Update=200654.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\214\345\217\211\346\240\221.md" | 86 +++++++++++-------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 64b38b488e..224e75e44c 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -259,52 +259,70 @@ class Solution { ``` ### Python - +(版本一) 基础版 ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - """递归法 更快""" def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: - if not nums: - return None - maxvalue = max(nums) - index = nums.index(maxvalue) + if len(nums) == 1: + return TreeNode(nums[0]) + node = TreeNode(0) + # 找到数组中最大的值和对应的下标 + maxValue = 0 + maxValueIndex = 0 + for i in range(len(nums)): + if nums[i] > maxValue: + maxValue = nums[i] + maxValueIndex = i + node.val = maxValue + # 最大值所在的下标左区间 构造左子树 + if maxValueIndex > 0: + new_list = nums[:maxValueIndex] + node.left = self.constructMaximumBinaryTree(new_list) + # 最大值所在的下标右区间 构造右子树 + if maxValueIndex < len(nums) - 1: + new_list = nums[maxValueIndex+1:] + node.right = self.constructMaximumBinaryTree(new_list) + return node - root = TreeNode(maxvalue) +``` +(版本二) 使用下标 +class Solution: + def traversal(self, nums: List[int], left: int, right: int) -> TreeNode: + if left >= right: + return None + maxValueIndex = left + for i in range(left + 1, right): + if nums[i] > nums[maxValueIndex]: + maxValueIndex = i + root = TreeNode(nums[maxValueIndex]) + root.left = self.traversal(nums, left, maxValueIndex) + root.right = self.traversal(nums, maxValueIndex + 1, right) + return root - left = nums[:index] - right = nums[index + 1:] + def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: + return self.traversal(nums, 0, len(nums)) - root.left = self.constructMaximumBinaryTree(left) - root.right = self.constructMaximumBinaryTree(right) - return root +``` +(版本三) 使用切片 class Solution: - """最大二叉树 递归法""" - def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: - return self.traversal(nums, 0, len(nums)) - - def traversal(self, nums: List[int], begin: int, end: int) -> TreeNode: - # 列表长度为0时返回空节点 - if begin == end: + if not nums: return None + max_val = max(nums) + max_index = nums.index(max_val) + node = TreeNode(max_val) + node.left = self.constructMaximumBinaryTree(nums[:max_index]) + node.right = self.constructMaximumBinaryTree(nums[max_index+1:]) + return node - # 找到最大的值和其对应的下标 - max_index = begin - for i in range(begin, end): - if nums[i] > nums[max_index]: - max_index = i - - # 构建当前节点 - root = TreeNode(nums[max_index]) - - # 递归构建左右子树 - root.left = self.traversal(nums, begin, max_index) - root.right = self.traversal(nums, max_index + 1, end) - - return root ``` - ### Go From a45046be8a6387a0c4e7fbf6872c554b6a2f3249 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 9 May 2023 15:11:13 -0500 Subject: [PATCH 0311/1533] =?UTF-8?q?Update=200617.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 创建 root节点 和 python queue的代码优化版本 --- ...66\344\272\214\345\217\211\346\240\221.md" | 67 ++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index f98948f0f3..18a245c4db 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -352,8 +352,7 @@ class Solution { ``` ### Python - -**递归法 - 前序遍历** +(版本一) 递归 - 前序 - 修改root1 ```python # Definition for a binary tree node. # class TreeNode: @@ -376,9 +375,34 @@ class Solution: return root1 # ⚠️ 注意: 本题我们重复使用了题目给出的节点而不是创建新节点. 节省时间, 空间. +``` +(版本二) 递归 - 前序 - 新建root +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode: + # 递归终止条件: + # 但凡有一个节点为空, 就立刻返回另外一个. 如果另外一个也为None就直接返回None. + if not root1: + return root2 + if not root2: + return root1 + # 上面的递归终止条件保证了代码执行到这里root1, root2都非空. + root = TreeNode() # 创建新节点 + root.val += root1.val + root2.val# 中 + root.left = self.mergeTrees(root1.left, root2.left) #左 + root.right = self.mergeTrees(root1.right, root2.right) # 右 + + return root # ⚠️ 注意: 本题我们创建了新节点. + ``` -**迭代法** +(版本三) 迭代 ```python class Solution: def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode: @@ -413,7 +437,44 @@ class Solution: return root1 ``` +(版本四) 迭代 + 代码优化 +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +from collections import deque + +class Solution: + def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode: + if not root1: + return root2 + if not root2: + return root1 + + queue = deque() + queue.append((root1, root2)) + + while queue: + node1, node2 = queue.popleft() + node1.val += node2.val + if node1.left and node2.left: + queue.append((node1.left, node2.left)) + elif not node1.left: + node1.left = node2.left + + if node1.right and node2.right: + queue.append((node1.right, node2.right)) + elif not node1.right: + node1.right = node2.right + + return root1 + + +``` ### Go ```go From 2d06a685ea39c988ad48b1a6025c8476cc06a640 Mon Sep 17 00:00:00 2001 From: NeedyChild <100189185+NeedyChild@users.noreply.github.com> Date: Tue, 9 May 2023 15:22:33 -0700 Subject: [PATCH 0312/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200494.=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=92=8C.md=20Java=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index cc2bc8df83..f8843d0855 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -292,6 +292,85 @@ class Solution { } ``` +易于理解的二维数组版本: +```java +class Solution { + public int findTargetSumWays(int[] nums, int target) { + + // 01背包应用之“有多少种不同的填满背包最大容量的方法“ + // 易于理解的二维数组解法及详细注释 + + int sum = 0; + for(int i = 0; i < nums.length; i++) { + sum += nums[i]; + } + + // 注意nums[i] >= 0的题目条件,意味着sum也是所有nums[i]的绝对值之和 + // 这里保证了sum + target一定是大于等于零的,也就是left大于等于零(毕竟我们定义left大于right) + if(sum < Math.abs(target)){ + return 0; + } + + // 利用二元一次方程组将left用target和sum表示出来(替换掉right组合),详见代码随想录对此题的分析 + // 如果所求的left数组和为小数,则作为整数数组的nums里的任何元素自然是没有办法凑出这个小数的 + if((sum + target) % 2 != 0) { + return 0; + } + + int left = (sum + target) / 2; + + // dp[i][j]:遍历到数组第i个数时, left为j时的能装满背包的方法总数 + int[][] dp = new int[nums.length][left + 1]; + + // 初始化最上行(dp[0][j]),当nums[0] == j时(注意nums[0]和j都一定是大于等于零的,因此不需要判断等于-j时的情况),有唯一一种取法可取到j,dp[0][j]此时等于1 + // 其他情况dp[0][j] = 0 + // java整数数组默认初始值为0 + for(int j = 0; j <= left; j++) { + if(nums[0] == j) { + dp[0][j] = 1; + } + } + + // 初始化最左列(dp[i][0]) + // 当从nums数组的索引0到i的部分有n个0时(n > 0),每个0可以取+/-,因此有2的n次方中可以取到j = 0的方案 + // n = 0说明当前遍历到的数组部分没有0全为正数,因此只有一种方案可以取到j = 0(就是所有数都不取) + int numZeros = 0; + for(int i = 0; i < nums.length; i++) { + if(nums[i] == 0) { + numZeros++; + } + dp[i][0] = (int) Math.pow(2, numZeros); + + } + + // 递推公式分析: + // 当nums[i] > j时,这时候nums[i]一定不能取,所以是dp[i - 1][j]种方案数 + // nums[i] <= j时,num[i]可取可不取,因此方案数是dp[i - 1][j] + dp[i - 1][j - nums[i]] + // 由递推公式可知,先遍历i或j都可 + for(int i = 1; i < nums.length; i++) { + for(int j = 1; j <= left; j++) { + if(nums[i] > j) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; + } + } + } + + // 打印dp数组 + // for(int i = 0; i < nums.length; i++) { + // for(int j = 0; j <= left; j++) { + // System.out.print(dp[i][j] + " "); + // } + // System.out.println(""); + // } + + return dp[nums.length - 1][left]; + + } +} +``` + ### Python ```python class Solution: From 3818de4610acd3a1a3e1a9794f3e6caa4320a9e9 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 9 May 2023 18:50:14 -0500 Subject: [PATCH 0313/1533] =?UTF-8?q?Update=200700.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\221\344\270\255\347\232\204\346\220\234\347\264\242.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 40724f48a8..13064f9717 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -230,7 +230,7 @@ class Solution { ### Python -递归法: +(方法一) 递归 ```python class Solution: @@ -250,12 +250,12 @@ class Solution: ``` -迭代法: +(方法二)迭代 ```python class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: - while root is not None: + while root: if val < root.val: root = root.left elif val > root.val: root = root.right else: return root From 38503ddc59e4554ac2316ca9c6e486b48285df8a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 12 May 2023 20:41:46 +0800 Subject: [PATCH 0314/1533] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8F?= =?UTF-8?q?II.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\232\204\351\207\215\351\207\217II.md" | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 210ce73738..a8150b1f04 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -379,8 +379,23 @@ object Solution { } ``` - - +### Rust + +```rust +impl Solution { + pub fn last_stone_weight_ii(stones: Vec) -> i32 { + let sum = stones.iter().sum::(); + let target = sum as usize / 2; + let mut dp = vec![0; target + 1]; + for s in stones { + for j in (s as usize..=target).rev() { + dp[j] = dp[j].max(dp[j - s as usize] + s); + } + } + sum - dp[target] * 2 + } +} +```

From db21086b42d3a7ff1d07dbe4bfbf3e94bd2301b8 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 12 May 2023 22:09:19 +0800 Subject: [PATCH 0315/1533] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index cc2bc8df83..e9e3337080 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -417,6 +417,32 @@ object Solution { } ``` +### Rust + +```rust +impl Solution { + pub fn find_target_sum_ways(nums: Vec, target: i32) -> i32 { + let sum = nums.iter().sum::(); + if target.abs() > sum { + return 0; + } + if (target + sum) % 2 == 1 { + return 0; + } + let size = (sum + target) as usize / 2; + let mut dp = vec![0; size + 1]; + dp[0] = 1; + for n in nums { + for s in (n as usize..=size).rev() { + // + dp[s] += dp[s - n as usize]; + } + } + dp[size] + } +} +``` +

From 03829cde0d626a5d77609b29d6a1581924e40800 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 12 May 2023 22:09:48 +0800 Subject: [PATCH 0316/1533] Apply suggestions from code review --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 1 - 1 file changed, 1 deletion(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index e9e3337080..c9f888920f 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -434,7 +434,6 @@ impl Solution { dp[0] = 1; for n in nums { for s in (n as usize..=size).rev() { - // dp[s] += dp[s - n as usize]; } } From 1ed8111a24113240dc7707bc0cc4e56c575482e3 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Mon, 15 May 2023 15:21:03 -0400 Subject: [PATCH 0317/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20```Java=20?= =?UTF-8?q?=E4=BB=A5=E6=AD=A3=E5=B8=B8=E9=A1=AF=E7=A4=BA=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=AD=97=E9=A1=8F=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 ```Java 以正常顯示保留字顏色 --- ...\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 41a1ede256..94c7940486 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -367,7 +367,7 @@ class MyStack { ``` 优化,使用一个 Queue 实现,但用卡哥的逻辑实现 -``` +```Java class MyStack { Queue queue; From 4948ef48f46d516a475f8bf57ab559b5a71513af Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Wed, 17 May 2023 16:50:29 -0400 Subject: [PATCH 0318/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E4=B8=AD=20lambda=E4=BB=A5=E5=8F=8Alinkedlist.add?= =?UTF-8?q?=E7=9A=84=E8=A8=BB=E9=87=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java解法中 lambda以及linkedlist.add的註釋 --- ...\230\351\207\215\345\273\272\351\230\237\345\210\227.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 3f82d0bc20..67aef3ec1c 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -191,14 +191,14 @@ class Solution { public int[][] reconstructQueue(int[][] people) { // 身高从大到小排(身高相同k小的站前面) Arrays.sort(people, (a, b) -> { - if (a[0] == b[0]) return a[1] - b[1]; - return b[0] - a[0]; + if (a[0] == b[0]) return a[1] - b[1]; // a - b 是升序排列,故在a[0] == b[0]的狀況下,會根據k值升序排列 + return b[0] - a[0]; //b - a 是降序排列,在a[0] != b[0],的狀況會根據h值降序排列 }); LinkedList que = new LinkedList<>(); for (int[] p : people) { - que.add(p[1],p); + que.add(p[1],p); //Linkedlist.add(index, value),會將value插入到指定index裡。 } return que.toArray(new int[people.length][]); From 16f4a48bd6b0539c60eed95c4f2c54a052685386 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sat, 20 May 2023 15:30:25 +0800 Subject: [PATCH 0319/1533] update --- ...47\345\255\220\345\272\217\345\222\214.md" | 2 +- ...00\344\275\263\346\227\266\346\234\272.md" | 17 +++++++----- ...\344\275\263\346\227\266\346\234\272II.md" | 2 +- ...01\350\247\204\345\210\222\357\274\211.md" | 23 +++++++++------- ...344\275\263\346\227\266\346\234\272III.md" | 27 +++++++++++-------- ...\344\275\263\346\227\266\346\234\272IV.md" | 17 +++++++----- ...23\345\256\266\345\212\253\350\210\215.md" | 4 +++ ...\345\256\266\345\212\253\350\210\215II.md" | 25 ++++++++++------- ...53\345\206\267\345\206\273\346\234\237.md" | 4 +++ ...345\256\266\345\212\253\350\210\215III.md" | 5 ++++ ...01\350\247\204\345\210\222\357\274\211.md" | 5 ++++ ...04\346\225\260\347\273\204\345\222\214.md" | 23 ---------------- ...06\350\256\272\345\237\272\347\241\200.md" | 5 ++++ 13 files changed, 92 insertions(+), 67 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 48f8be29c1..39c5833287 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -124,7 +124,7 @@ public: ## 动态规划 -当然本题还可以用动态规划来做,当前[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png)主要讲解贪心系列,后续到动态规划系列的时候会详细讲解本题的 dp 方法。 +当然本题还可以用动态规划来做,在代码随想录动态规划章节我会详细介绍,如果大家想在想看,可以直接跳转:[动态规划版本详解](https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF) 那么先给出我的 dp 代码如下,有时间的录友可以提前做一做: diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index 5b398f3fc7..753cb10692 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -14,16 +14,21 @@ 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。 -示例 1: -输入:[7,1,5,3,6,4] -输出:5 +* 示例 1: +* 输入:[7,1,5,3,6,4] +* 输出:5 解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。 -示例 2: -输入:prices = [7,6,4,3,1] -输出:0 +* 示例 2: +* 输入:prices = [7,6,4,3,1] +* 输出:0 解释:在这种情况下, 没有交易完成, 所以最大利润为 0。 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index e631c5e2ae..0d8ad608d7 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -102,7 +102,7 @@ public: ### 动态规划 -动态规划将在下一个系列详细讲解,本题解先给出我的 C++代码(带详细注释),感兴趣的同学可以自己先学习一下。 +动态规划将在下一个系列详细讲解,本题解先给出我的 C++代码(带详细注释),想先学习的话,可以看本篇:[122.买卖股票的最佳时机II(动态规划)](https://programmercarl.com/0122.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BAII%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF) ```CPP class Solution { diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 2779083d89..146c6a4cb1 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -15,25 +15,30 @@ 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 -示例 1: -输入: [7,1,5,3,6,4] -输出: 7 +* 示例 1: +* 输入: [7,1,5,3,6,4] +* 输出: 7 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。 -示例 2: -输入: [1,2,3,4,5] -输出: 4 +* 示例 2: +* 输入: [1,2,3,4,5] +* 输出: 4 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 -示例 3: -输入: [7,6,4,3,1] -输出: 0 +* 示例 3: +* 输入: [7,6,4,3,1] +* 输出: 0 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 提示: * 1 <= prices.length <= 3 * 10 ^ 4 * 0 <= prices[i] <= 10 ^ 4 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 本题我们在讲解贪心专题的时候就已经讲解过了[贪心算法:买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html),只不过没有深入讲解动态规划的解法,那么这次我们再好好分析一下动规的解法。 diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 33157238ac..af6870d4cc 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -15,23 +15,23 @@ 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 -示例 1: -输入:prices = [3,3,5,0,0,3,1,4] -输出:6 +* 示例 1: +* 输入:prices = [3,3,5,0,0,3,1,4] +* 输出:6 解释:在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3。 -示例 2: -输入:prices = [1,2,3,4,5] -输出:4 +* 示例 2: +* 输入:prices = [1,2,3,4,5] +* 输出:4 解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4。注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 -示例 3: -输入:prices = [7,6,4,3,1] -输出:0 +* 示例 3: +* 输入:prices = [7,6,4,3,1] +* 输出:0 解释:在这个情况下, 没有交易完成, 所以最大利润为0。 -示例 4: -输入:prices = [1] +* 示例 4: +* 输入:prices = [1] 输出:0 提示: @@ -39,6 +39,11 @@ * 1 <= prices.length <= 10^5 * 0 <= prices[i] <= 10^5 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index f6744a2b36..37270664dd 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -14,14 +14,14 @@ 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 -示例 1: -输入:k = 2, prices = [2,4,1] -输出:2 +* 示例 1: +* 输入:k = 2, prices = [2,4,1] +* 输出:2 解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2。 -示例 2: -输入:k = 2, prices = [3,2,6,5,0,3] -输出:7 +* 示例 2: +* 输入:k = 2, prices = [3,2,6,5,0,3] +* 输出:7 解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4。随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。 @@ -31,6 +31,11 @@ * 0 <= prices.length <= 1000 * 0 <= prices[i] <= 1000 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 这道题目可以说是[动态规划:123.买卖股票的最佳时机III](https://programmercarl.com/0123.买卖股票的最佳时机III.html)的进阶版,这里要求至多有k次交易。 diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index c25f3b8677..b7cdc1ced2 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -31,6 +31,10 @@ * 0 <= nums.length <= 100 * 0 <= nums[i] <= 400 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index becad06920..aebda7f53d 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -14,23 +14,28 @@ 示例 1: -输入:nums = [2,3,2] -输出:3 -解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。 +* 输入:nums = [2,3,2] +* 输出:3 +* 解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。 -示例 2: -输入:nums = [1,2,3,1] -输出:4 -解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。偷窃到的最高金额 = 1 + 3 = 4 。 +* 示例 2: +* 输入:nums = [1,2,3,1] +* 输出:4 +* 解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。偷窃到的最高金额 = 1 + 3 = 4 。 -示例 3: -输入:nums = [0] -输出:0 +* 示例 3: +* 输入:nums = [0] +* 输出:0 提示: * 1 <= nums.length <= 100 * 0 <= nums[i] <= 1000 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 这道题目和[198.打家劫舍](https://programmercarl.com/0198.打家劫舍.html)是差不多的,唯一区别就是成环了。 diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index d62e91c74a..a56d9b8464 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -20,6 +20,10 @@ * 输出: 3 * 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出] +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index f75c4c88ad..ca8cea2369 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -16,6 +16,11 @@ ![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png) +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 这道题目和 [198.打家劫舍](https://programmercarl.com/0198.打家劫舍.html),[213.打家劫舍II](https://programmercarl.com/0213.打家劫舍II.html)也是如出一辙,只不过这个换成了树。 diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 3973080778..12789934ff 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -32,6 +32,11 @@ * 0 < prices[i] < 50000. * 0 <= fee < 50000. +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 本题贪心解法:[贪心算法:买卖股票的最佳时机含手续费](https://programmercarl.com/0714.买卖股票的最佳时机含手续费.html) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 27a575c717..6252c697cd 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -130,29 +130,6 @@ class Solution { } ``` -```java -class Solution { - public int largestSumAfterKNegations(int[] A, int K) { - if (A.length == 1) return k % 2 == 0 ? A[0] : -A[0]; - Arrays.sort(A); - int sum = 0; - int idx = 0; - for (int i = 0; i < K; i++) { - if (i < A.length - 1 && A[idx] < 0) { - A[idx] = -A[idx]; - if (A[idx] >= Math.abs(A[idx + 1])) idx++; - continue; - } - A[idx] = -A[idx]; - } - - for (int i = 0; i < A.length; i++) { - sum += A[i]; - } - return sum; - } -} -``` ### Python ```python diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" index 77f01b1393..a99c069002 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -10,6 +10,11 @@ +## 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 什么是动态规划 动态规划,英文:Dynamic Programming,简称DP,如果某一问题有很多重叠子问题,使用动态规划是最有效的。 From b660360285c0fbf520106b7afd03f049bc0b6300 Mon Sep 17 00:00:00 2001 From: yujiahan2018 Date: Sat, 20 May 2023 19:29:45 +0800 Subject: [PATCH 0320/1533] =?UTF-8?q?Update=200718.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\255\220\346\225\260\347\273\204.md" | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 08be67326e..31782d5d6c 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -198,7 +198,57 @@ public: 而且为了让 `if (dp[i][j] > result) result = dp[i][j];` 收集到全部结果,两层for训练一定从0开始遍历,这样需要加上 `&& i > 0 && j > 0`的判断。 -相对于版本一来说还是多写了不少代码。而且逻辑上也复杂了一些。 优势就是dp数组的定义,更直观一点。 +对于基础不牢的小白来说,在推导出转移方程后可能疑惑上述代码为什么要从`i=0,j=0`遍历而不是从`i=1,j=1`开始遍历,原因在于这里如果不是从`i=0,j=0`位置开始遍历,会漏掉如下样例结果: +```txt +nums1 = [70,39,25,40,7] +nums2 = [52,20,67,5,31] +``` + +当然,如果你愿意也可以使用如下代码,与上面那个c++是同一思路: +```java +class Solution { + public int findLength(int[] nums1, int[] nums2) { + int len1 = nums1.length; + int len2 = nums2.length; + int[][] result = new int[len1][len2]; + + int maxresult = Integer.MIN_VALUE; + + for(int i=0;i Date: Sat, 20 May 2023 19:31:51 +0800 Subject: [PATCH 0321/1533] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\345\256\266\345\212\253\350\210\215.md" | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 6e682ec39e..7c8ba4db36 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -141,7 +141,36 @@ class Solution { } } -// 空间优化 dp数组只存与计算相关的两次数据 +// 使用滚动数组思想,优化空间 +// 分析本题可以发现,所求结果仅依赖于前两种状态,此时可以使用滚动数组思想将空间复杂度降低为3个空间 +class Solution { + public int rob(int[] nums) { + + int len = nums.length; + + if (len == 0) return 0; + else if (len == 1) return nums[0]; + else if (len == 2) return Math.max(nums[0],nums[1]); + + + int[] result = new int[3]; //存放选择的结果 + result[0] = nums[0]; + result[1] = Math.max(nums[0],nums[1]); + + + for(int i=2;i nums[1] ? nums[0] : nums[1]; + dp[1] = Math.max(nums[0],nums[1]); int res = 0; // 遍历 for (int i = 2; i < nums.length; i++) { - res = (dp[0] + nums[i]) > dp[1] ? (dp[0] + nums[i]) : dp[1]; + res = Math.max((dp[0] + nums[i]) , dp[1] ); dp[0] = dp[1]; dp[1] = res; } From f5329cd4b913d73f049e6b30f7d2a3a8a2a90770 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 15:38:38 -0500 Subject: [PATCH 0322/1533] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\344\272\214\345\217\211\346\240\221.md" | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index a3bc77fb11..c0561e1097 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -553,6 +553,52 @@ class Solution: 迭代法: +```python +class Solution: + def getDepth(self, cur): + st = [] + if cur is not None: + st.append(cur) + depth = 0 + result = 0 + while st: + node = st[-1] + if node is not None: + st.pop() + st.append(node) # 中 + st.append(None) + depth += 1 + if node.right: + st.append(node.right) # 右 + if node.left: + st.append(node.left) # 左 + + else: + node = st.pop() + st.pop() + depth -= 1 + result = max(result, depth) + return result + + def isBalanced(self, root): + st = [] + if root is None: + return True + st.append(root) + while st: + node = st.pop() # 中 + if abs(self.getDepth(node.left) - self.getDepth(node.right)) > 1: + return False + if node.right: + st.append(node.right) # 右(空节点不入栈) + if node.left: + st.append(node.left) # 左(空节点不入栈) + return True + +``` + +迭代法精简版: + ```python class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: @@ -576,8 +622,6 @@ class Solution: height_map[real_node] = 1 + max(left, right) return True ``` - - ### Go ```Go From 2024fc272af5811c3c594bf08959af2c9f62371d Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 16:48:27 -0500 Subject: [PATCH 0323/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\234\211\350\267\257\345\276\204.md" | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index c396f4a0fb..4cafbded9f 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -473,31 +473,27 @@ class Solution { 递归法+回溯(版本一) ```Python # Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right -import copy -from typing import List, Optional - class Solution: - def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]: - if not root: - return [] + def traversal(self, cur, path, result): + path.append(cur.val) # 中 + if not cur.left and not cur.right: # 到达叶子节点 + sPath = '->'.join(map(str, path)) + result.append(sPath) + return + if cur.left: # 左 + self.traversal(cur.left, path, result) + path.pop() # 回溯 + if cur.right: # 右 + self.traversal(cur.right, path, result) + path.pop() # 回溯 + + def binaryTreePaths(self, root): result = [] - self.generate_paths(root, [], result) + path = [] + if not root: + return result + self.traversal(root, path, result) return result - - def generate_paths(self, node: TreeNode, path: List[int], result: List[str]) -> None: - path.append(node.val) - if not node.left and not node.right: - result.append('->'.join(map(str, path))) - if node.left: - self.generate_paths(node.left, copy.copy(path), result) - if node.right: - self.generate_paths(node.right, copy.copy(path), result) - path.pop() ``` From 9b770de8cccfee4588a4966086aa4f285c347d68 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 17:07:10 -0500 Subject: [PATCH 0324/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\211\200\346\234\211\350\267\257\345\276\204.md" | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 4cafbded9f..195b6f9422 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -497,7 +497,7 @@ class Solution: ``` -递归法+回溯(版本二) +递归法+隐形回溯(版本一) ```Python # Definition for a binary tree node. # class TreeNode: @@ -505,7 +505,6 @@ class Solution: # self.val = val # self.left = left # self.right = right -import copy from typing import List, Optional class Solution: @@ -523,13 +522,13 @@ class Solution: if not node.left and not node.right: result.append('->'.join(map(str, path))) else: - self.generate_paths(node.left, copy.copy(path), result) - self.generate_paths(node.right, copy.copy(path), result) - path.pop() + # path[:] 是隐藏回溯 + self.generate_paths(node.left, path[:], result) + self.generate_paths(node.right, path[:], result) ``` -递归法+隐形回溯 +递归法+隐形回溯(版本二) ```Python # Definition for a binary tree node. # class TreeNode: From 1a672740e99c000e44e1c5186bc57184b1f35b58 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 17:14:09 -0500 Subject: [PATCH 0325/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...200\346\234\211\350\267\257\345\276\204.md" | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 195b6f9422..31cbdb562f 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -512,19 +512,19 @@ class Solution: if not root: return [] result = [] - self.generate_paths(root, [], result) + self.traversal(root, [], result) return result - def generate_paths(self, node: TreeNode, path: List[int], result: List[str]) -> None: - if not node: + def traversal(self, cur: TreeNode, path: List[int], result: List[str]) -> None: + if not cur: return - path.append(node.val) - if not node.left and not node.right: + path.append(cur.val) + if not cur.left and not cur.right: result.append('->'.join(map(str, path))) - else: - # path[:] 是隐藏回溯 - self.generate_paths(node.left, path[:], result) - self.generate_paths(node.right, path[:], result) + if cur.left: + self.traversal(cur.left, path[:], result) + if cur.right: + self.traversal(cur.right, path[:], result) ``` From 2e81b18c5d92d99e7b50c35f0db76f8b824b3915 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 19:04:11 -0500 Subject: [PATCH 0326/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\346\211\200\346\234\211\350\267\257\345\276\204.md" | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 31cbdb562f..7bd56fbd0d 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -470,7 +470,7 @@ class Solution { ## Python: -递归法+回溯(版本一) +递归法+回溯 ```Python # Definition for a binary tree node. class Solution: @@ -561,16 +561,11 @@ class Solution: 迭代法: ```Python -from collections import deque - - class Solution: - """二叉树的所有路径 迭代法""" def binaryTreePaths(self, root: TreeNode) -> List[str]: # 题目中节点数至少为1 - stack, path_st, result = deque([root]), deque(), [] - path_st.append(str(root.val)) + stack, path_st, result = [root], [str(root.val)], [] while stack: cur = stack.pop() From 117ef697fac9428f8982a9076a9f25445f1d72ee Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 19:24:48 -0500 Subject: [PATCH 0327/1533] =?UTF-8?q?Update=200404.=E5=B7=A6=E5=8F=B6?= =?UTF-8?q?=E5=AD=90=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...66\345\255\220\344\271\213\345\222\214.md" | 88 +++++++++++-------- 1 file changed, 52 insertions(+), 36 deletions(-) diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 617978b743..aa2868df4e 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -247,8 +247,7 @@ class Solution { ### Python - -**递归后序遍历** +递归 ```python # Definition for a binary tree node. # class TreeNode: @@ -257,47 +256,64 @@ class Solution { # self.left = left # self.right = right class Solution: - def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int: - if not root: + def sumOfLeftLeaves(self, root): + if root is None: + return 0 + if root.left is None and root.right is None: return 0 - # 检查根节点的左子节点是否为叶节点 - if root.left and not root.left.left and not root.left.right: - left_val = root.left.val - else: - left_val = self.sumOfLeftLeaves(root.left) + leftValue = self.sumOfLeftLeaves(root.left) # 左 + if root.left and not root.left.left and not root.left.right: # 左子树是左叶子的情况 + leftValue = root.left.val - # 递归地计算右子树左叶节点的和 - right_val = self.sumOfLeftLeaves(root.right) - - return left_val + right_val + rightValue = self.sumOfLeftLeaves(root.right) # 右 + + sum_val = leftValue + rightValue # 中 + return sum_val ``` +递归精简版 -**迭代** ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def sumOfLeftLeaves(self, root: TreeNode) -> int: - """ - Idea: Each time check current node's left node. - If current node don't have one, skip it. - """ - stack = [] - if root: - stack.append(root) - res = 0 - - while stack: - # 每次都把当前节点的左节点加进去. - cur_node = stack.pop() - if cur_node.left and not cur_node.left.left and not cur_node.left.right: - res += cur_node.left.val - - if cur_node.left: - stack.append(cur_node.left) - if cur_node.right: - stack.append(cur_node.right) - - return res + def sumOfLeftLeaves(self, root): + if root is None: + return 0 + leftValue = 0 + if root.left is not None and root.left.left is None and root.left.right is None: + leftValue = root.left.val + return leftValue + self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right) +``` + +迭代法 +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumOfLeftLeaves(self, root): + if root is None: + return 0 + st = [root] + result = 0 + while st: + node = st.pop() + if node.left and node.left.left is None and node.left.right is None: + result += node.left.val + if node.right: + st.append(node.right) + if node.left: + st.append(node.left) + return result + ``` ### Go From 1ac1a8c332fde5f37e848577fae4970e46c1d7e9 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 19:42:19 -0500 Subject: [PATCH 0328/1533] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91?= =?UTF-8?q?=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\213\350\247\222\347\232\204\345\200\274.md" | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index 26768c74dc..743b0df9be 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -330,19 +330,24 @@ class Solution: # self.right = right from collections import deque class Solution: - def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: - queue = deque([root]) + def findBottomLeftValue(self, root): + if root is None: + return 0 + queue = deque() + queue.append(root) + result = 0 while queue: size = len(queue) - leftmost = queue[0].val for i in range(size): node = queue.popleft() + if i == 0: + result = node.val if node.left: queue.append(node.left) if node.right: queue.append(node.right) - if not queue: - return leftmost + return result + ``` From 12634b2d89a00e25f2c3897a675c58922c93d0b0 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 21:48:07 -0500 Subject: [PATCH 0329/1533] =?UTF-8?q?Update=200654.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\200\345\244\247\344\272\214\345\217\211\346\240\221.md" | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 224e75e44c..07fbeb826f 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -292,6 +292,8 @@ class Solution: ``` (版本二) 使用下标 +```python + class Solution: def traversal(self, nums: List[int], left: int, right: int) -> TreeNode: if left >= right: @@ -309,7 +311,8 @@ class Solution: return self.traversal(nums, 0, len(nums)) -``` +```python + (版本三) 使用切片 class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: From 41cf3a4cdd276f52e2039a886ed21f0bdfa50444 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 21:49:22 -0500 Subject: [PATCH 0330/1533] =?UTF-8?q?Update=200654.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\200\345\244\247\344\272\214\345\217\211\346\240\221.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 07fbeb826f..980523f5ec 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -292,7 +292,6 @@ class Solution: ``` (版本二) 使用下标 -```python class Solution: def traversal(self, nums: List[int], left: int, right: int) -> TreeNode: @@ -311,9 +310,11 @@ class Solution: return self.traversal(nums, 0, len(nums)) -```python (版本三) 使用切片 + +```python + class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: if not nums: From 7a544d95b8aa54cbb3e7457458fd4a67304048ae Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 22 May 2023 21:50:17 -0500 Subject: [PATCH 0331/1533] =?UTF-8?q?Update=200654.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 980523f5ec..33a9176ed1 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -292,6 +292,7 @@ class Solution: ``` (版本二) 使用下标 +```python class Solution: def traversal(self, nums: List[int], left: int, right: int) -> TreeNode: @@ -309,7 +310,7 @@ class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: return self.traversal(nums, 0, len(nums)) - + ``` (版本三) 使用切片 From 6498ee65f7d9d38e59d3e338bdbec9a79cb6a438 Mon Sep 17 00:00:00 2001 From: zdz <2549626703@qq.com> Date: Tue, 23 May 2023 22:36:43 +0800 Subject: [PATCH 0332/1533] =?UTF-8?q?fix=EF=BC=9A0123.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?III=20=3D=E3=80=8B=E9=94=99=E8=AF=AF=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\346\234\200\344\275\263\346\227\266\346\234\272III.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index af6870d4cc..6ac9a576ea 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -242,9 +242,9 @@ class Solution { for (int i = 1; i < len; i++) { dp[i][1] = Math.max(dp[i - 1][1], -prices[i]); - dp[i][2] = Math.max(dp[i - 1][2], dp[i][1] + prices[i]); - dp[i][3] = Math.max(dp[i - 1][3], dp[i][2] - prices[i]); - dp[i][4] = Math.max(dp[i - 1][4], dp[i][3] + prices[i]); + dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]); + dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]); + dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]); } return dp[len - 1][4]; From 3058654e93415b39469468dabf4ac02907d04b71 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 20:47:45 -0500 Subject: [PATCH 0333/1533] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 178 +++++++++--------- 1 file changed, 87 insertions(+), 91 deletions(-) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 95afe6805c..f2e148c239 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -341,117 +341,113 @@ class Solution { ## Python -**递归** - 利用BST中序遍历特性,把树"压缩"成数组 +递归法(版本一)利用中序递增性质,转换成数组 ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def isValidBST(self, root: TreeNode) -> bool: - # 思路: 利用BST中序遍历的特性. - # 中序遍历输出的二叉搜索树节点的数值是有序序列 - candidate_list = [] - - def __traverse(root: TreeNode) -> None: - nonlocal candidate_list - if not root: - return - __traverse(root.left) - candidate_list.append(root.val) - __traverse(root.right) - - def __is_sorted(nums: list) -> bool: - for i in range(1, len(nums)): - if nums[i] <= nums[i - 1]: # ⚠️ 注意: Leetcode定义二叉搜索树中不能有重复元素 - return False - return True - - __traverse(root) - res = __is_sorted(candidate_list) - - return res + def __init__(self): + self.vec = [] + + def traversal(self, root): + if root is None: + return + self.traversal(root.left) + self.vec.append(root.val) # 将二叉搜索树转换为有序数组 + self.traversal(root.right) + + def isValidBST(self, root): + self.vec = [] # 清空数组 + self.traversal(root) + for i in range(1, len(self.vec)): + # 注意要小于等于,搜索树里不能有相同元素 + if self.vec[i] <= self.vec[i - 1]: + return False + return True + ``` -**递归** - 标准做法 +递归法(版本二)设定极小值,进行比较 ```python class Solution: - def isValidBST(self, root: TreeNode) -> bool: - # 规律: BST的中序遍历节点数值是从小到大. - cur_max = -float("INF") - def __isValidBST(root: TreeNode) -> bool: - nonlocal cur_max - - if not root: - return True - - is_left_valid = __isValidBST(root.left) - if cur_max < root.val: - cur_max = root.val - else: - return False - is_right_valid = __isValidBST(root.right) - - return is_left_valid and is_right_valid - return __isValidBST(root) + def __init__(self): + self.maxVal = float('-inf') # 因为后台测试数据中有int最小值 + + def isValidBST(self, root): + if root is None: + return True + + left = self.isValidBST(root.left) + # 中序遍历,验证遍历的元素是不是从小到大 + if self.maxVal < root.val: + self.maxVal = root.val + else: + return False + right = self.isValidBST(root.right) + + return left and right + ``` -**递归** - 避免初始化最小值做法: +递归法(版本三)直接取该树的最小值 ```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def isValidBST(self, root: TreeNode) -> bool: - # 规律: BST的中序遍历节点数值是从小到大. - pre = None - def __isValidBST(root: TreeNode) -> bool: - nonlocal pre - - if not root: - return True - - is_left_valid = __isValidBST(root.left) - if pre and pre.val>=root.val: return False - pre = root - is_right_valid = __isValidBST(root.right) - - return is_left_valid and is_right_valid - return __isValidBST(root) + def __init__(self): + self.pre = None # 用来记录前一个节点 + + def isValidBST(self, root): + if root is None: + return True + + left = self.isValidBST(root.left) + + if self.pre is not None and self.pre.val >= root.val: + return False + self.pre = root # 记录前一个节点 + + right = self.isValidBST(root.right) + return left and right + + + ``` +迭代法 ```python -迭代-中序遍历 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def isValidBST(self, root: TreeNode) -> bool: + def isValidBST(self, root): stack = [] cur = root - pre = None - while cur or stack: - if cur: # 指针来访问节点,访问到最底层 + pre = None # 记录前一个节点 + while cur is not None or len(stack) > 0: + if cur is not None: stack.append(cur) - cur = cur.left - else: # 逐一处理节点 - cur = stack.pop() - if pre and cur.val <= pre.val: # 比较当前节点和前节点的值的大小 + cur = cur.left # 左 + else: + cur = stack.pop() # 中 + if pre is not None and cur.val <= pre.val: return False - pre = cur - cur = cur.right + pre = cur # 保存前一个访问的结点 + cur = cur.right # 右 return True ``` -```python -# 遵循Carl的写法,只添加了节点判断的部分 -class Solution: - def isValidBST(self, root: TreeNode) -> bool: - # method 2 - que, pre = [], None - while root or que: - while root: - que.append(root) - root = root.left - root = que.pop() - # 对第一个节点只做记录,对后面的节点进行比较 - if pre is None: - pre = root.val - else: - if pre >= root.val: return False - pre = root.val - root = root.right - return True -``` + ## Go From 85e4c414571db69f8dd4c27c67928632845e65e4 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 20:56:07 -0500 Subject: [PATCH 0334/1533] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\347\273\235\345\257\271\345\267\256.md" | 100 ++++++++++-------- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index fa1430dec1..3fe57702e5 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -204,66 +204,82 @@ class Solution { ``` ## Python -递归 +递归法(版本一)利用中序递增,结合数组 ```python class Solution: - def getMinimumDifference(self, root: TreeNode) -> int: - res = [] - r = float("inf") - def buildaList(root): //把二叉搜索树转换成有序数组 - if not root: return None - if root.left: buildaList(root.left) //左 - res.append(root.val) //中 - if root.right: buildaList(root.right) //右 - return res - - buildaList(root) - for i in range(len(res)-1): // 统计有序数组的最小差值 - r = min(abs(res[i]-res[i+1]),r) - return r - - -class Solution: # 双指针法,不用数组 (同Carl写法) - 更快 - def getMinimumDifference(self, root: Optional[TreeNode]) -> int: - global pre,minval - pre = None - minval = 10**5 - self.traversal(root) - return minval + def __init__(self): + self.vec = [] - def traversal(self,root): - global pre,minval - if not root: return None + def traversal(self, root): + if root is None: + return self.traversal(root.left) - if pre and root.val-pre.val int: + def __init__(self): + self.result = float('inf') + self.pre = None + + def traversal(self, cur): + if cur is None: + return + self.traversal(cur.left) # 左 + if self.pre is not None: # 中 + self.result = min(self.result, cur.val - self.pre.val) + self.pre = cur # 记录前一个 + self.traversal(cur.right) # 右 + + def getMinimumDifference(self, root): + self.traversal(root) + return self.result + + +``` + +迭代法 +```python +class Solution: + def getMinimumDifference(self, root): stack = [] cur = root pre = None result = float('inf') - while cur or stack: - if cur: # 指针来访问节点,访问到最底层 - stack.append(cur) - cur = cur.left - else: # 逐一处理节点 + + while cur is not None or len(stack) > 0: + if cur is not None: + stack.append(cur) # 将访问的节点放进栈 + cur = cur.left # 左 + else: cur = stack.pop() - if pre: # 当前节点和前节点的值的差值 - result = min(result, abs(cur.val - pre.val)) + if pre is not None: # 中 + result = min(result, cur.val - pre.val) pre = cur - cur = cur.right + cur = cur.right # 右 + return result + + ``` - ## Go: 中序遍历,然后计算最小差值 From c5d1845f3f48ff7ccd78319fdb4481033b159df1 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:06:32 -0500 Subject: [PATCH 0335/1533] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\344\274\227\346\225\260.md" | 136 +++++++++++------- 1 file changed, 83 insertions(+), 53 deletions(-) diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index b7ef606fe6..6dc8ed8332 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -475,8 +475,7 @@ class Solution { ## Python -> 递归法 -> 常量空间,递归产生的栈不算 +递归法(版本一)利用字典 ```python # Definition for a binary tree node. @@ -485,77 +484,108 @@ class Solution { # self.val = val # self.left = left # self.right = right +from collections import defaultdict + +class Solution: + def searchBST(self, cur, freq_map): + if cur is None: + return + freq_map[cur.val] += 1 # 统计元素频率 + self.searchBST(cur.left, freq_map) + self.searchBST(cur.right, freq_map) + + def findMode(self, root): + freq_map = defaultdict(int) # key:元素,value:出现频率 + result = [] + if root is None: + return result + self.searchBST(root, freq_map) + max_freq = max(freq_map.values()) + for key, freq in freq_map.items(): + if freq == max_freq: + result.append(key) + return result + +``` + +递归法(版本二)利用二叉搜索树性质 + +```python class Solution: def __init__(self): - self.pre = TreeNode() - self.count = 0 - self.max_count = 0 + self.maxCount = 0 # 最大频率 + self.count = 0 # 统计频率 + self.pre = None self.result = [] - def findMode(self, root: TreeNode) -> List[int]: - if not root: return None - self.search_BST(root) - return self.result - - def search_BST(self, cur: TreeNode) -> None: - if not cur: return None - self.search_BST(cur.left) - # 第一个节点 - if not self.pre: + def searchBST(self, cur): + if cur is None: + return + + self.searchBST(cur.left) # 左 + # 中 + if self.pre is None: # 第一个节点 self.count = 1 - # 与前一个节点数值相同 - elif self.pre.val == cur.val: - self.count += 1 - # 与前一个节点数值不相同 - else: + elif self.pre.val == cur.val: # 与前一个节点数值相同 + self.count += 1 + else: # 与前一个节点数值不同 self.count = 1 - self.pre = cur + self.pre = cur # 更新上一个节点 - if self.count == self.max_count: + if self.count == self.maxCount: # 如果与最大值频率相同,放进result中 self.result.append(cur.val) - - if self.count > self.max_count: - self.max_count = self.count - self.result = [cur.val] # 清空self.result,确保result之前的的元素都失效 - - self.search_BST(cur.right) -``` + if self.count > self.maxCount: # 如果计数大于最大值频率 + self.maxCount = self.count # 更新最大频率 + self.result = [cur.val] # 很关键的一步,不要忘记清空result,之前result里的元素都失效了 + + self.searchBST(cur.right) # 右 + return + + def findMode(self, root): + self.count = 0 + self.maxCount = 0 + self.pre = None # 记录前一个节点 + self.result = [] -> 迭代法-中序遍历 -> 利用二叉搜索树特性,在历遍过程中更新结果,一次历遍 -> 但需要使用额外空间存储历遍的节点 + self.searchBST(root) + return self.result +``` +迭代法 ```python class Solution: - def findMode(self, root: TreeNode) -> List[int]: - stack = [] + def findMode(self, root): + st = [] cur = root pre = None - maxCount, count = 0, 0 - res = [] - while cur or stack: - if cur: # 指针来访问节点,访问到最底层 - stack.append(cur) - cur = cur.left - else: # 逐一处理节点 - cur = stack.pop() - if pre == None: # 第一个节点 + maxCount = 0 # 最大频率 + count = 0 # 统计频率 + result = [] + + while cur is not None or st: + if cur is not None: # 指针来访问节点,访问到最底层 + st.append(cur) # 将访问的节点放进栈 + cur = cur.left # 左 + else: + cur = st.pop() + if pre is None: # 第一个节点 count = 1 elif pre.val == cur.val: # 与前一个节点数值相同 count += 1 - else: + else: # 与前一个节点数值不同 count = 1 - if count == maxCount: - res.append(cur.val) - if count > maxCount: - maxCount = count - res.clear() - res.append(cur.val) + + if count == maxCount: # 如果和最大值相同,放进result中 + result.append(cur.val) + + if count > maxCount: # 如果计数大于最大值频率 + maxCount = count # 更新最大频率 + result = [cur.val] # 很关键的一步,不要忘记清空result,之前result里的元素都失效了 pre = cur - cur = cur.right - return res - + cur = cur.right # 右 + + return result ``` ## Go From daa5417db9ef711d219ca8e201c6dd0cd57c9a20 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:11:37 -0500 Subject: [PATCH 0336/1533] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.md" | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 33201def08..0ebd556669 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -274,25 +274,44 @@ class Solution { ``` ## Python - +递归法(版本一) ```python class Solution: - """二叉树的最近公共祖先 递归法""" - - def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': - if not root or root == p or root == q: + def lowestCommonAncestor(self, root, p, q): + if root == q or root == p or root is None: return root - + left = self.lowestCommonAncestor(root.left, p, q) right = self.lowestCommonAncestor(root.right, p, q) - - if left and right: + + if left is not None and right is not None: return root - if left: + + if left is None and right is not None: + return right + elif left is not None and right is None: return left - return right + else: + return None ``` +递归法(版本二)精简 +```python +class Solution: + def lowestCommonAncestor(self, root, p, q): + if root == q or root == p or root is None: + return root + left = self.lowestCommonAncestor(root.left, p, q) + right = self.lowestCommonAncestor(root.right, p, q) + + if left is not None and right is not None: + return root + + if left is None: + return right + return left + +``` ## Go ```Go From e7b7aa8e4a85d49933ddfa14e4911cc8d0f72bfe Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:16:37 -0500 Subject: [PATCH 0337/1533] =?UTF-8?q?Update=200235.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E7=A5=96=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.md" | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 8353303aa3..9777bb0be8 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -275,34 +275,57 @@ class Solution { ## Python -递归法: +递归法(版本一) ```python class Solution: - """二叉搜索树的最近公共祖先 递归法""" + def traversal(self, cur, p, q): + if cur is None: + return cur + # 中 + if cur.val > p.val and cur.val > q.val: # 左 + left = self.traversal(cur.left, p, q) + if left is not None: + return left + + if cur.val < p.val and cur.val < q.val: # 右 + right = self.traversal(cur.right, p, q) + if right is not None: + return right + + return cur + + def lowestCommonAncestor(self, root, p, q): + return self.traversal(root, p, q) +``` - def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': +迭代法(版本二)精简 +```python +class Solution: + def lowestCommonAncestor(self, root, p, q): if root.val > p.val and root.val > q.val: return self.lowestCommonAncestor(root.left, p, q) - if root.val < p.val and root.val < q.val: + elif root.val < p.val and root.val < q.val: return self.lowestCommonAncestor(root.right, p, q) - return root + else: + return root + ``` -迭代法: +迭代法 ```python class Solution: - """二叉搜索树的最近公共祖先 迭代法""" - - def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': - while True: + def lowestCommonAncestor(self, root, p, q): + while root: if root.val > p.val and root.val > q.val: root = root.left elif root.val < p.val and root.val < q.val: root = root.right else: return root -``` + return None + +``` ## Go 递归法: From 511bf447936dbb628609c5b1cad23462f20ac817 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:29:16 -0500 Subject: [PATCH 0338/1533] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\346\223\215\344\275\234.md" | 165 ++++++++---------- 1 file changed, 68 insertions(+), 97 deletions(-) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 4e834201bd..1ba7461fed 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -256,132 +256,103 @@ class Solution { ----- ## Python -**递归法** - 有返回值 - +递归法(版本一) ```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right class Solution: - def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: - # 返回更新后的以当前root为根节点的新树,方便用于更新上一层的父子节点关系链 - - # Base Case - if not root: return TreeNode(val) - - # 单层递归逻辑: - if val < root.val: - # 将val插入至当前root的左子树中合适的位置 - # 并更新当前root的左子树为包含目标val的新左子树 - root.left = self.insertIntoBST(root.left, val) + def __init__(self): + self.parent = None + + def traversal(self, cur, val): + if cur is None: + node = TreeNode(val) + if val > self.parent.val: + self.parent.right = node + else: + self.parent.left = node + return - if root.val < val: - # 将val插入至当前root的右子树中合适的位置 - # 并更新当前root的右子树为包含目标val的新右子树 - root.right = self.insertIntoBST(root.right, val) + self.parent = cur + if cur.val > val: + self.traversal(cur.left, val) + if cur.val < val: + self.traversal(cur.right, val) - # 返回更新后的以当前root为根节点的新树 + def insertIntoBST(self, root, val): + self.parent = TreeNode(0) + if root is None: + return TreeNode(val) + self.traversal(root, val) return root + ``` -**递归法** - 无返回值 +递归法(版本二) ```python class Solution: - def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: - if not root: + def insertIntoBST(self, root, val): + if root is None: return TreeNode(val) parent = None - def __traverse(cur: TreeNode, val: int) -> None: - # 在函数运行的同时把新节点插入到该被插入的地方. - nonlocal parent - if not cur: - new_node = TreeNode(val) - if parent.val < val: - parent.right = new_node - else: - parent.left = new_node - return - - parent = cur # 重点: parent的作用只有运行到上面if not cur:才会发挥出来. - if cur.val < val: - __traverse(cur.right, val) - else: - __traverse(cur.left, val) - return - __traverse(root, val) - return root -``` - -**递归法** - 无返回值 - another easier way -```python -class Solution: - def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: - newNode = TreeNode(val) - if not root: return newNode - - if not root.left and val < root.val: - root.left = newNode - if not root.right and val > root.val: - root.right = newNode - - if val < root.val: - self.insertIntoBST(root.left, val) - if val > root.val: - self.insertIntoBST(root.right, val) - + cur = root + while cur: + parent = cur + if val < cur.val: + cur = cur.left + else: + cur = cur.right + if val < parent.val: + parent.left = TreeNode(val) + else: + parent.right = TreeNode(val) return root ``` -**递归法** - 无返回值 有注释 不用Helper function +递归法(版本三) ```python class Solution: def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: - if not root: # for root==None + if root is None or root.val == val: return TreeNode(val) - if root.valval: - if root.left==None: # found the parent + elif root.val > val: + if root.left is None: root.left = TreeNode(val) - else: # not found, keep searching + else: self.insertIntoBST(root.left, val) - # return the final tree + elif root.val < val: + if root.right is None: + root.right = TreeNode(val) + else: + self.insertIntoBST(root.right, val) return root ``` -**迭代法** -与无返回值的递归函数的思路大体一致 + + +迭代法 ```python class Solution: - def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: - if not root: - return TreeNode(val) - parent = None # 此步可以省略 - cur = root + def insertIntoBST(self, root, val): + if root is None: # 如果根节点为空,创建新节点作为根节点并返回 + node = TreeNode(val) + return node - # 用while循环不断地找新节点的parent - while cur: - parent = cur # 首先保存当前非空节点作为下一次迭代的父节点 - if cur.val < val: - cur = cur.right - elif cur.val > val: + cur = root + parent = root # 记录上一个节点,用于连接新节点 + while cur is not None: + parent = cur + if cur.val > val: cur = cur.left + else: + cur = cur.right + + node = TreeNode(val) + if val < parent.val: + parent.left = node # 将新节点连接到父节点的左子树 + else: + parent.right = node # 将新节点连接到父节点的右子树 - # 运行到这意味着已经跳出上面的while循环, - # 同时意味着新节点的parent已经被找到. - # parent已被找到, 新节点已经ready. 把两个节点黏在一起就好了. - if parent.val > val: - parent.left = TreeNode(val) - else: - parent.right = TreeNode(val) - return root + ``` ----- From 731d1ca8aa61b1b8969ead0c5cf8b7d8b21b55e6 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:38:27 -0500 Subject: [PATCH 0339/1533] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 132 +++++++++--------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 2602b52873..d4dca1d41d 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -324,88 +324,90 @@ class Solution { ``` ## Python - +递归法(版本一) ```python class Solution: - def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: - if not root : return None # 节点为空,返回 - if root.val < key : - root.right = self.deleteNode(root.right, key) - elif root.val > key : + def deleteNode(self, root, key): + if root is None: + return root + if root.val == key: + if root.left is None and root.right is None: + return None + elif root.left is None: + return root.right + elif root.right is None: + return root.left + else: + cur = root.right + while cur.left is not None: + cur = cur.left + cur.left = root.left + return root.right + if root.val > key: root.left = self.deleteNode(root.left, key) - else: - # 当前节点的左子树为空,返回当前的右子树 - if not root.left : return root.right - # 当前节点的右子树为空,返回当前的左子树 - if not root.right: return root.left - # 左右子树都不为空,找到右孩子的最左节点 记为p - node = root.right - while node.left : - node = node.left - # 将当前节点的左子树挂在p的左孩子上 - node.left = root.left - # 当前节点的右子树替换掉当前节点,完成当前节点的删除 - root = root.right + if root.val < key: + root.right = self.deleteNode(root.right, key) return root ``` -**普通二叉树的删除方式** +递归法(版本二) ```python class Solution: - def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: - if not root: return root - if root.val == key: - if not root.right: # 这里第二次操作目标值:最终删除的作用 + def deleteNode(self, root, key): + if root is None: # 如果根节点为空,直接返回 + return root + if root.val == key: # 找到要删除的节点 + if root.right is None: # 如果右子树为空,直接返回左子树作为新的根节点 return root.left - tmp = root.right - while tmp.left: - tmp = tmp.left - root.val, tmp.val = tmp.val, root.val # 这里第一次操作目标值:交换目标值其右子树最左面节点。 - - root.left = self.deleteNode(root.left, key) - root.right = self.deleteNode(root.right, key) + cur = root.right + while cur.left: # 找到右子树中的最左节点 + cur = cur.left + root.val, cur.val = cur.val, root.val # 将要删除的节点值与最左节点值交换 + root.left = self.deleteNode(root.left, key) # 在左子树中递归删除目标节点 + root.right = self.deleteNode(root.right, key) # 在右子树中递归删除目标节点 return root + ``` **迭代法** ```python class Solution: - def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: - # 找到节点后分两步,1. 把节点的左子树和右子树连起来,2. 把右子树跟父节点连起来 - # root is None - if not root: return root - p = root - last = None - while p: - if p.val==key: - # 1. connect left to right - # right is not None -> left is None | left is not None - if p.right: - if p.left: - node = p.right - while node.left: - node = node.left - node.left = p.left - right = p.right - else: - # right is None -> right=left - right = p.left - # 2. connect right to last - if last==None: - root = right - elif last.val>key: - last.left = right - else: - last.right = right - # 3. return + def deleteOneNode(self, target: TreeNode) -> TreeNode: + """ + 将目标节点(删除节点)的左子树放到目标节点的右子树的最左面节点的左孩子位置上 + 并返回目标节点右孩子为新的根节点 + 是动画里模拟的过程 + """ + if target is None: + return target + if target.right is None: + return target.left + cur = target.right + while cur.left: + cur = cur.left + cur.left = target.left + return target.right + + def deleteNode(self, root: TreeNode, key: int) -> TreeNode: + if root is None: + return root + cur = root + pre = None # 记录cur的父节点,用来删除cur + while cur: + if cur.val == key: break + pre = cur + if cur.val > key: + cur = cur.left else: - # Update last and continue - last = p - if p.val>key: - p = p.left - else: - p = p.right + cur = cur.right + if pre is None: # 如果搜索树只有头结点 + return self.deleteOneNode(cur) + # pre 要知道是删左孩子还是右孩子 + if pre.left and pre.left.val == key: + pre.left = self.deleteOneNode(cur) + if pre.right and pre.right.val == key: + pre.right = self.deleteOneNode(cur) return root ``` From 53652744d2aa05765a10ce824424ee1c06afc57c Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:43:40 -0500 Subject: [PATCH 0340/1533] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 82 ++++++++++--------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 18d8a0cc8d..95372d6196 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -271,64 +271,72 @@ class Solution { ## Python -**递归** - +递归法(版本一) ```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right class Solution: def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode: - ''' - 确认递归函数参数以及返回值:返回更新后剪枝后的当前root节点 - ''' - # Base Case - if not root: return None - - # 单层递归逻辑 + if root is None: + return None if root.val < low: - # 若当前root节点小于左界:只考虑其右子树,用于替代更新后的其本身,抛弃其左子树整体 + # 寻找符合区间 [low, high] 的节点 return self.trimBST(root.right, low, high) - - if high < root.val: - # 若当前root节点大于右界:只考虑其左子树,用于替代更新后的其本身,抛弃其右子树整体 + if root.val > high: + # 寻找符合区间 [low, high] 的节点 return self.trimBST(root.left, low, high) + root.left = self.trimBST(root.left, low, high) # root.left 接入符合条件的左孩子 + root.right = self.trimBST(root.right, low, high) # root.right 接入符合条件的右孩子 + return root - if low <= root.val <= high: - root.left = self.trimBST(root.left, low, high) - root.right = self.trimBST(root.right, low, high) - # 返回更新后的剪枝过的当前节点root - return root ``` +递归法(版本二)精简 +```python +class Solution: + def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode: + if root is None: + return None + if root.val < low: + return self.trimBST(root.right, low, high) + if root.val > high: + return self.trimBST(root.left, low, high) + root.left = self.trimBST(root.left, low, high) + root.right = self.trimBST(root.right, low, high) + return root -**迭代** +``` + +迭代法 ```python class Solution: - def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: - if not root: return root - # 处理头结点,让root移动到[L, R] 范围内,注意是左闭右开 - while root and (root.val < low or root.val > high): - if root.val < low: # 小于L往右走 - root = root.right - else: # 大于R往左走 - root = root.left - # 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况 + def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode: + if not root: + return None + + # 处理头结点,让root移动到[L, R] 范围内,注意是左闭右闭 + while root and (root.val < L or root.val > R): + if root.val < L: + root = root.right # 小于L往右走 + else: + root = root.left # 大于R往左走 + cur = root + + # 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况 while cur: - while cur.left and cur.left.val < low: + while cur.left and cur.left.val < L: cur.left = cur.left.right cur = cur.left - # 此时root已经在[L, R] 范围内,处理右孩子大于R的情况 + cur = root + + # 此时root已经在[L, R] 范围内,处理右孩子大于R的情况 while cur: - while cur.right and cur.right.val > high: + while cur.right and cur.right.val > R: cur.right = cur.right.left cur = cur.right + return root + ``` ## Go From 76e3811c5e7d2299ff06c2aeb231f8fa78bcb957 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:46:41 -0500 Subject: [PATCH 0341/1533] =?UTF-8?q?Update=200108.=E5=B0=86=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 100 ++++++++---------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 89778421d1..056ef3e268 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -316,73 +316,65 @@ class Solution { ``` ## Python -**递归** - +递归法 ```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right class Solution: - def sortedArrayToBST(self, nums: List[int]) -> TreeNode: - ''' - 构造二叉树:重点是选取数组最中间元素为分割点,左侧是递归左区间;右侧是递归右区间 - 必然是平衡树 - 左闭右闭区间 - ''' - # 返回根节点 - root = self.traversal(nums, 0, len(nums)-1) - return root - def traversal(self, nums: List[int], left: int, right: int) -> TreeNode: - # Base Case if left > right: return None - # 确定左右界的中心,防越界 mid = left + (right - left) // 2 - # 构建根节点 - mid_root = TreeNode(nums[mid]) - # 构建以左右界的中心为分割点的左右子树 - mid_root.left = self.traversal(nums, left, mid-1) - mid_root.right = self.traversal(nums, mid+1, right) - - # 返回由被传入的左右界定义的某子树的根节点 - return mid_root + root = TreeNode(nums[mid]) + root.left = self.traversal(nums, left, mid - 1) + root.right = self.traversal(nums, mid + 1, right) + return root + + def sortedArrayToBST(self, nums: List[int]) -> TreeNode: + root = self.traversal(nums, 0, len(nums) - 1) + return root + ``` -**迭代**(左闭右开) +迭代法 ```python +from collections import deque + class Solution: - def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: - if len(nums) == 0: return None - root = TreeNode() # 初始化 - nodeSt = [root] - leftSt = [0] - rightSt = [len(nums)] - - while nodeSt: - node = nodeSt.pop() # 处理根节点 - left = leftSt.pop() - right = rightSt.pop() - mid = left + (right - left) // 2 - node.val = nums[mid] - - if left < mid: # 处理左区间 - node.left = TreeNode() - nodeSt.append(node.left) - leftSt.append(left) - rightSt.append(mid) - - if right > mid + 1: # 处理右区间 - node.right = TreeNode() - nodeSt.append(node.right) - leftSt.append(mid + 1) - rightSt.append(right) + def sortedArrayToBST(self, nums: List[int]) -> TreeNode: + if len(nums) == 0: + return None + root = TreeNode(0) # 初始根节点 + nodeQue = deque() # 放遍历的节点 + leftQue = deque() # 保存左区间下标 + rightQue = deque() # 保存右区间下标 + + nodeQue.append(root) # 根节点入队列 + leftQue.append(0) # 0为左区间下标初始位置 + rightQue.append(len(nums) - 1) # len(nums) - 1为右区间下标初始位置 + + while nodeQue: + curNode = nodeQue.popleft() + left = leftQue.popleft() + right = rightQue.popleft() + mid = left + (right - left) // 2 + + curNode.val = nums[mid] # 将mid对应的元素给中间节点 + + if left <= mid - 1: # 处理左区间 + curNode.left = TreeNode(0) + nodeQue.append(curNode.left) + leftQue.append(left) + rightQue.append(mid - 1) + + if right >= mid + 1: # 处理右区间 + curNode.right = TreeNode(0) + nodeQue.append(curNode.right) + leftQue.append(mid + 1) + rightQue.append(right) + return root + ``` ## Go From 14acf5a55469e766e05b74f2279132ce2a7b66d7 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 23 May 2023 21:57:22 -0500 Subject: [PATCH 0342/1533] =?UTF-8?q?Update=200538.=E6=8A=8A=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E7=B4=AF=E5=8A=A0=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\347\264\257\345\212\240\346\240\221.md" | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index ad5310e150..ad4decc53f 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -200,8 +200,30 @@ class Solution { ``` ## Python -**递归** - +递归法(版本一) +```python +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def convertBST(self, root: TreeNode) -> TreeNode: + self.pre = 0 # 记录前一个节点的数值 + self.traversal(root) + return root + def traversal(self, cur): + if cur is None: + return + self.traversal(cur.right) + cur.val += self.pre + self.pre = cur.val + self.traversal(cur.left) + + +``` +递归法(版本二) ```python # Definition for a binary tree node. # class TreeNode: @@ -234,7 +256,32 @@ class Solution: return root ``` -**迭代** +迭代法(版本一) +```python +class Solution: + def __init__(self): + self.pre = 0 # 记录前一个节点的数值 + + def traversal(self, root): + stack = [] + cur = root + while cur or stack: + if cur: + stack.append(cur) + cur = cur.right # 右 + else: + cur = stack.pop() # 中 + cur.val += self.pre + self.pre = cur.val + cur = cur.left # 左 + + def convertBST(self, root): + self.pre = 0 + self.traversal(root) + return root + +``` +迭代法(版本二) ```python class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: From 450b3f2f15ab7afa7b18f835879dc1573249edee Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 24 May 2023 00:51:14 -0500 Subject: [PATCH 0343/1533] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\260\217\346\267\261\345\272\246.md" | 108 +++++++++--------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 0c086f1bbb..ef59730e63 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -297,36 +297,72 @@ class Solution { ## Python -递归法: +递归法(版本一) ```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right class Solution: - def minDepth(self, root: TreeNode) -> int: - if not root: + def getDepth(self, node): + if node is None: return 0 + leftDepth = self.getDepth(node.left) # 左 + rightDepth = self.getDepth(node.right) # 右 - if not root.left and not root.right: - return 1 + # 当一个左子树为空,右不为空,这时并不是最低点 + if node.left is None and node.right is not None: + return 1 + rightDepth - left_depth = float('inf') - right_depth = float('inf') + # 当一个右子树为空,左不为空,这时并不是最低点 + if node.left is not None and node.right is None: + return 1 + leftDepth - if root.left: - left_depth = self.minDepth(root.left) - if root.right: - right_depth = self.minDepth(root.right) - - return 1 + min(left_depth, right_depth) + result = 1 + min(leftDepth, rightDepth) + return result + + def minDepth(self, root): + return self.getDepth(root) ``` +递归法(版本二) -迭代法: +```python +class Solution: + def minDepth(self, root): + if root is None: + return 0 + if root.left is None and root.right is not None: + return 1 + self.minDepth(root.right) + if root.left is not None and root.right is None: + return 1 + self.minDepth(root.left) + return 1 + min(self.minDepth(root.left), self.minDepth(root.right)) + + +``` +递归法(版本三)前序 + +```python +class Solution: + def __init__(self): + self.result = float('inf') + + def getDepth(self, node, depth): + if node is None: + return + if node.left is None and node.right is None: + self.result = min(self.result, depth) + if node.left: + self.getDepth(node.left, depth + 1) + if node.right: + self.getDepth(node.right, depth + 1) + + def minDepth(self, root): + if root is None: + return 0 + self.getDepth(root, 1) + return self.result + + +``` +迭代法 ```python # Definition for a binary tree node. @@ -359,39 +395,7 @@ class Solution: return depth ``` -迭代法: - -```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right -class Solution: - def minDepth(self, root: TreeNode) -> int: - if not root: - return 0 - - queue = collections.deque([(root, 1)]) - - while queue: - node, depth = queue.popleft() - - # Check if the node is a leaf node - if not node.left and not node.right: - return depth - - # Add left and right child to the queue - if node.left: - queue.append((node.left, depth+1)) - if node.right: - queue.append((node.right, depth+1)) - - return 0 - -``` ## Go From 832897e4bbeb21c5906729257af347f6cd90c97c Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 24 May 2023 01:56:01 -0500 Subject: [PATCH 0344/1533] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\344\272\214\345\217\211\346\240\221.md" | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index c0561e1097..e10a612afd 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -536,18 +536,16 @@ class Solution: ```python class Solution: - def isBalanced(self, root: TreeNode) -> bool: - return self.height(root) != -1 - def height(self, node: TreeNode) -> int: - if not node: - return 0 - left = self.height(node.left) - if left == -1: - return -1 - right = self.height(node.right) - if right == -1 or abs(left - right) > 1: - return -1 - return max(left, right) + 1 + def isBalanced(self, root: Optional[TreeNode]) -> bool: + return self.get_hight(root) != -1 + def get_hight(self, node): + if not node: + return 0 + left = self.get_hight(node.left) + right = self.get_hight(node.right) + if left == -1 or right == -1 or abs(left - right) > 1: + return -1 + return max(left, right) + 1 ``` From 2f86a5ce27078eff58b5d6e2765091ed01f04d94 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 24 May 2023 22:39:35 -0500 Subject: [PATCH 0345/1533] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\222\345\205\245\346\223\215\344\275\234.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 1ba7461fed..fc4351babc 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -326,7 +326,22 @@ class Solution: return root ``` +递归法(版本四) +```python +class Solution: + def insertIntoBST(self, root, val): + if root is None: + node = TreeNode(val) + return node + + if root.val > val: + root.left = self.insertIntoBST(root.left, val) + if root.val < val: + root.right = self.insertIntoBST(root.right, val) + return root + +``` 迭代法 ```python From 3965f2590ce244b3e3eb922b922e74e58cf6941d Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Thu, 25 May 2023 15:51:34 +0800 Subject: [PATCH 0346/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 22 ++++++++++++ ...\345\275\242\351\223\276\350\241\250II.md" | 28 +++++++++++++++ ...76\350\241\250\345\205\203\347\264\240.md" | 35 +++++++++++++++++++ ...04\347\232\204\345\271\263\346\226\271.md" | 8 +++++ 4 files changed, 93 insertions(+) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index c6f5bfc781..84eac96b84 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -412,6 +412,28 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { ``` +C#: +```csharp +public class Solution { + public ListNode RemoveNthFromEnd(ListNode head, int n) { + ListNode dummpHead = new ListNode(0); + dummpHead.next = head; + var fastNode = dummpHead; + var slowNode = dummpHead; + while(n-- != 0 && fastNode != null) + { + fastNode = fastNode.next; + } + while(fastNode.next != null) + { + fastNode = fastNode.next; + slowNode = slowNode.next; + } + slowNode.next = slowNode.next.next; + return dummpHead.next; + } +} +```

diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index e80a715a6c..f87d2cd914 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -437,6 +437,34 @@ object Solution { } ``` +C#: +```CSharp +public class Solution +{ + public ListNode DetectCycle(ListNode head) + { + ListNode fast = head; + ListNode slow = head; + while (fast != null && fast.next != null) + { + slow = slow.next; + fast = fast.next.next; + if (fast == slow) + { + fast = head; + while (fast != slow) + { + fast = fast.next; + slow = slow.next; + } + return fast; + } + } + return null; + } +} +``` +

diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 6a0de2826c..f875165826 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -596,6 +596,41 @@ class Solution { } ``` +C# +```CSharp +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution +{ + public ListNode RemoveElements(ListNode head, int val) + { + ListNode dummyHead = new ListNode(0,head); + ListNode temp = dummyHead; + while(temp.next != null) + { + if(temp.next.val == val) + { + temp.next = temp.next.next; + } + else + { + temp = temp.next; + } + } + return dummyHead.next; + } +} +``` +

diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 4fbdd1cdac..a316096e20 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -488,6 +488,14 @@ public class Solution { return result; } } + +C# LINQ: +```csharp +public class Solution { + public int[] SortedSquares(int[] nums) { + return nums.Select(x => x * x).OrderBy(x => x).ToArray(); + } +} ```

From 343e0775eb6c10fc2ff7a487cd92381d577f1fb4 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 02:17:43 -0500 Subject: [PATCH 0347/1533] =?UTF-8?q?Update=200216.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\220\210\346\200\273\345\222\214III.md" | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index f631c3cdc8..5a2c1033a4 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -360,28 +360,25 @@ class Solution { ```py class Solution: - def __init__(self): - self.res = [] - self.sum_now = 0 - self.path = [] - - def combinationSum3(self, k: int, n: int) -> [[int]]: - self.backtracking(k, n, 1) - return self.res - - def backtracking(self, k: int, n: int, start_num: int): - if self.sum_now > n: # 剪枝 + def combinationSum3(self, k: int, n: int) -> List[List[int]]: + result = [] # 存放结果集 + self.backtracking(n, k, 0, 1, [], result) + return result + + def backtracking(self, targetSum, k, currentSum, startIndex, path, result): + if currentSum > targetSum: # 剪枝操作 + return # 如果path的长度等于k但currentSum不等于targetSum,则直接返回 + if len(path) == k: + if currentSum == targetSum: + result.append(path[:]) return - if len(self.path) == k: # len(path)==k时不管sum是否等于n都会返回 - if self.sum_now == n: - self.res.append(self.path[:]) - return - for i in range(start_num, 10 - (k - len(self.path)) + 1): - self.path.append(i) - self.sum_now += i - self.backtracking(k, n, i + 1) - self.path.pop() - self.sum_now -= i + for i in range(startIndex, 9 - (k - len(path)) + 2): # 剪枝 + currentSum += i # 处理 + path.append(i) # 处理 + self.backtracking(targetSum, k, currentSum, i + 1, path, result) # 注意i+1调整startIndex + currentSum -= i # 回溯 + path.pop() # 回溯 + ``` ## Go From 89e9d75cbf5c1b33cc1b86cb6ee38fc3b0dd382d Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 02:24:01 -0500 Subject: [PATCH 0348/1533] =?UTF-8?q?Update=200077.=E7=BB=84=E5=90=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 80 +++++++-------------- 1 file changed, 27 insertions(+), 53 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 9c6d481ddb..c4da4471c0 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -377,68 +377,42 @@ class Solution { ``` ### Python - -```python -class Solution(object): - def combine(self, n, k): - """ - :type n: int - :type k: int - :rtype: List[List[int]] - """ - result = [] - path = [] - def backtracking(n, k, startidx): - if len(path) == k: - result.append(path[:]) - return - - # 剪枝, 最后k - len(path)个节点直接构造结果,无需递归 - last_startidx = n - (k - len(path)) + 1 - - for x in range(startidx, last_startidx + 1): - path.append(x) - backtracking(n, k, x + 1) # 递归 - path.pop() # 回溯 - - backtracking(n, k, 1) - return result -``` - +未剪枝优化 ```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: - res = [] - path = [] - def backtrack(n, k, StartIndex): - if len(path) == k: - res.append(path[:]) - return - for i in range(StartIndex, n + 1): - path.append(i) - backtrack(n, k, i+1) - path.pop() - backtrack(n, k, 1) - return res + result = [] # 存放结果集 + self.backtracking(n, k, 1, [], result) + return result + def backtracking(self, n, k, startIndex, path, result): + if len(path) == k: + result.append(path[:]) + return + for i in range(startIndex, n + 1): # 需要优化的地方 + path.append(i) # 处理节点 + self.backtracking(n, k, i + 1, path, result) + path.pop() # 回溯,撤销处理的节点 + ``` -剪枝: + +剪枝优化: ```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: - res=[] #存放符合条件结果的集合 - path=[] #用来存放符合条件结果 - def backtrack(n,k,startIndex): - if len(path) == k: - res.append(path[:]) - return - for i in range(startIndex,n-(k-len(path))+2): #优化的地方 - path.append(i) #处理节点 - backtrack(n,k,i+1) #递归 - path.pop() #回溯,撤销处理的节点 - backtrack(n,k,1) - return res + result = [] # 存放结果集 + self.backtracking(n, k, 1, [], result) + return result + def backtracking(self, n, k, startIndex, path, result): + if len(path) == k: + result.append(path[:]) + return + for i in range(startIndex, n - (k - len(path)) + 2): # 优化的地方 + path.append(i) # 处理节点 + self.backtracking(n, k, i + 1, path, result) + path.pop() # 回溯,撤销处理的节点 + ``` ### Go From 4d48c45107d2286ab9d5bbd6ce1adddac816859a Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 02:25:21 -0500 Subject: [PATCH 0349/1533] =?UTF-8?q?Update=200077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E4=BC=98=E5=8C=96.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\344\274\230\345\214\226.md" | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index 9736549c57..fc543eea79 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -179,18 +179,21 @@ Python: ```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: - res=[] #存放符合条件结果的集合 - path=[] #用来存放符合条件结果 - def backtrack(n,k,startIndex): - if len(path) == k: - res.append(path[:]) - return - for i in range(startIndex,n-(k-len(path))+2): #优化的地方 - path.append(i) #处理节点 - backtrack(n,k,i+1) #递归 - path.pop() #回溯,撤销处理的节点 - backtrack(n,k,1) - return res + result = [] # 存放结果集 + self.backtracking(n, k, 1, [], result) + return result + def backtracking(self, n, k, startIndex, path, result): + if len(path) == k: + result.append(path[:]) + return + for i in range(startIndex, n - (k - len(path)) + 2): # 优化的地方 + path.append(i) # 处理节点 + self.backtracking(n, k, i + 1, path, result) + path.pop() # 回溯,撤销处理的节点 + + + + ``` Go: ```Go From 847c60ac3c93af41eaba8bf8e48de71c5eb728ab Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 06:41:01 -0500 Subject: [PATCH 0350/1533] =?UTF-8?q?Update=200017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\347\273\204\345\220\210.md" | 203 +++++++++++------- 1 file changed, 129 insertions(+), 74 deletions(-) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index d113549716..9f1473dbf7 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -285,99 +285,154 @@ class Solution { ``` ## Python -**回溯** +回溯 ```python class Solution: def __init__(self): - self.answers: List[str] = [] - self.answer: str = '' - self.letter_map = { - '2': 'abc', - '3': 'def', - '4': 'ghi', - '5': 'jkl', - '6': 'mno', - '7': 'pqrs', - '8': 'tuv', - '9': 'wxyz' - } - - def letterCombinations(self, digits: str) -> List[str]: - self.answers.clear() - if not digits: return [] + self.letterMap = [ + "", # 0 + "", # 1 + "abc", # 2 + "def", # 3 + "ghi", # 4 + "jkl", # 5 + "mno", # 6 + "pqrs", # 7 + "tuv", # 8 + "wxyz" # 9 + ] + self.result = [] + self.s = "" + + def backtracking(self, digits, index): + if index == len(digits): + self.result.append(self.s) + return + digit = int(digits[index]) # 将索引处的数字转换为整数 + letters = self.letterMap[digit] # 获取对应的字符集 + for i in range(len(letters)): + self.s += letters[i] # 处理字符 + self.backtracking(digits, index + 1) # 递归调用,注意索引加1,处理下一个数字 + self.s = self.s[:-1] # 回溯,删除最后添加的字符 + + def letterCombinations(self, digits): + if len(digits) == 0: + return self.result self.backtracking(digits, 0) - return self.answers + return self.result + +``` +回溯精简(版本一) +```python +class Solution: + def __init__(self): + self.letterMap = [ + "", # 0 + "", # 1 + "abc", # 2 + "def", # 3 + "ghi", # 4 + "jkl", # 5 + "mno", # 6 + "pqrs", # 7 + "tuv", # 8 + "wxyz" # 9 + ] + self.result = [] - def backtracking(self, digits: str, index: int) -> None: - # 回溯函数没有返回值 - # Base Case - if index == len(digits): # 当遍历穷尽后的下一层时 - self.answers.append(self.answer) - return - # 单层递归逻辑 - letters: str = self.letter_map[digits[index]] + def getCombinations(self, digits, index, s): + if index == len(digits): + self.result.append(s) + return + digit = int(digits[index]) + letters = self.letterMap[digit] for letter in letters: - self.answer += letter # 处理 - self.backtracking(digits, index + 1) # 递归至下一层 - self.answer = self.answer[:-1] # 回溯 + self.getCombinations(digits, index + 1, s + letter) + + def letterCombinations(self, digits): + if len(digits) == 0: + return self.result + self.getCombinations(digits, 0, "") + return self.result + ``` -**回溯简化** +回溯精简(版本二) ```python class Solution: def __init__(self): - self.answers: List[str] = [] - self.letter_map = { - '2': 'abc', - '3': 'def', - '4': 'ghi', - '5': 'jkl', - '6': 'mno', - '7': 'pqrs', - '8': 'tuv', - '9': 'wxyz' - } - - def letterCombinations(self, digits: str) -> List[str]: - self.answers.clear() - if not digits: return [] - self.backtracking(digits, 0, '') - return self.answers + self.letterMap = [ + "", # 0 + "", # 1 + "abc", # 2 + "def", # 3 + "ghi", # 4 + "jkl", # 5 + "mno", # 6 + "pqrs", # 7 + "tuv", # 8 + "wxyz" # 9 + ] - def backtracking(self, digits: str, index: int, answer: str) -> None: - # 回溯函数没有返回值 - # Base Case - if index == len(digits): # 当遍历穷尽后的下一层时 - self.answers.append(answer) - return - # 单层递归逻辑 - letters: str = self.letter_map[digits[index]] + def getCombinations(self, digits, index, s, result): + if index == len(digits): + result.append(s) + return + digit = int(digits[index]) + letters = self.letterMap[digit] for letter in letters: - self.backtracking(digits, index + 1, answer + letter) # 递归至下一层 + 回溯 + self.getCombinations(digits, index + 1, s + letter, result) + + def letterCombinations(self, digits): + result = [] + if len(digits) == 0: + return result + self.getCombinations(digits, 0, "", result) + return result + + ``` -**使用itertools** + +回溯优化使用列表 ```python class Solution: - def letterCombinations(self, digits: str) -> List[str]: - import itertools - if not digits: - return list() - - phoneMap = { - "2": "abc", - "3": "def", - "4": "ghi", - "5": "jkl", - "6": "mno", - "7": "pqrs", - "8": "tuv", - "9": "wxyz", - } + def __init__(self): + self.letterMap = [ + "", # 0 + "", # 1 + "abc", # 2 + "def", # 3 + "ghi", # 4 + "jkl", # 5 + "mno", # 6 + "pqrs", # 7 + "tuv", # 8 + "wxyz" # 9 + ] + + def getCombinations(self, digits, index, path, result): + if index == len(digits): + result.append(''.join(path)) + return + digit = int(digits[index]) + letters = self.letterMap[digit] + for letter in letters: + path.append(letter) + self.getCombinations(digits, index + 1, path, result) + path.pop() + + def letterCombinations(self, digits): + result = [] + if len(digits) == 0: + return result + self.getCombinations(digits, 0, [], result) + return result + + - groups = (phoneMap[digit] for digit in digits) - return ["".join(combination) for combination in itertools.product(*groups)] ``` + ## Go 主要在于递归中传递下一个数字 From 2c514203087e51aa244ca3df18108e44d64f3f89 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 10:28:01 -0500 Subject: [PATCH 0351/1533] =?UTF-8?q?Update=200039.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\346\200\273\345\222\214.md" | 130 +++++++++++------- 1 file changed, 78 insertions(+), 52 deletions(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index c4ee5ca6ca..b603475589 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -271,75 +271,101 @@ class Solution { ## Python -**回溯** +回溯(版本一) ```python class Solution: - def __init__(self): - self.path = [] - self.paths = [] - def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: - ''' - 因为本题没有组合数量限制,所以只要元素总和大于target就算结束 - ''' - self.path.clear() - self.paths.clear() - self.backtracking(candidates, target, 0, 0) - return self.paths - - def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None: - # Base Case - if sum_ == target: - self.paths.append(self.path[:]) # 因为是shallow copy,所以不能直接传入self.path + def backtracking(self, candidates, target, total, startIndex, path, result): + if total > target: return - if sum_ > target: + if total == target: + result.append(path[:]) return - # 单层递归逻辑 - for i in range(start_index, len(candidates)): - sum_ += candidates[i] - self.path.append(candidates[i]) - self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i+1 - sum_ -= candidates[i] # 回溯 - self.path.pop() # 回溯 + for i in range(startIndex, len(candidates)): + total += candidates[i] + path.append(candidates[i]) + self.backtracking(candidates, target, total, i, path, result) # 不用i+1了,表示可以重复读取当前的数 + total -= candidates[i] + path.pop() + + def combinationSum(self, candidates, target): + result = [] + self.backtracking(candidates, target, 0, 0, [], result) + return result + ``` -**剪枝回溯** +回溯剪枝(版本一) ```python class Solution: - def __init__(self): - self.path = [] - self.paths = [] + def backtracking(self, candidates, target, total, startIndex, path, result): + if total == target: + result.append(path[:]) + return + + for i in range(startIndex, len(candidates)): + if total + candidates[i] > target: + break + total += candidates[i] + path.append(candidates[i]) + self.backtracking(candidates, target, total, i, path, result) + total -= candidates[i] + path.pop() + + def combinationSum(self, candidates, target): + result = [] + candidates.sort() # 需要排序 + self.backtracking(candidates, target, 0, 0, [], result) + return result + +``` + +回溯(版本二) + +```python +class Solution: def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: - ''' - 因为本题没有组合数量限制,所以只要元素总和大于target就算结束 - ''' - self.path.clear() - self.paths.clear() + result =[] + self.backtracking(candidates, target, 0, [], result) + return result + def backtracking(self, candidates, target, startIndex, path, result): + if target == 0: + result.append(path[:]) + return + if target < 0: + return + for i in range(startIndex, len(candidates)): + path.append(candidates[i]) + self.backtracking(candidates, target - candidates[i], i, path, result) + path.pop() - # 为了剪枝需要提前进行排序 - candidates.sort() - self.backtracking(candidates, target, 0, 0) - return self.paths +``` + +回溯剪枝(版本二) - def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None: - # Base Case - if sum_ == target: - self.paths.append(self.path[:]) # 因为是shallow copy,所以不能直接传入self.path +```python +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result =[] + candidates.sort() + self.backtracking(candidates, target, 0, [], result) + return result + def backtracking(self, candidates, target, startIndex, path, result): + if target == 0: + result.append(path[:]) return - # 单层递归逻辑 - # 如果本层 sum + condidates[i] > target,就提前结束遍历,剪枝 - for i in range(start_index, len(candidates)): - if sum_ + candidates[i] > target: - return - sum_ += candidates[i] - self.path.append(candidates[i]) - self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i-1 - sum_ -= candidates[i] # 回溯 - self.path.pop() # 回溯 + + for i in range(startIndex, len(candidates)): + if target - candidates[i] < 0: + break + path.append(candidates[i]) + self.backtracking(candidates, target - candidates[i], i, path, result) + path.pop() + ``` ## Go From 20db57f364270b9c6b9782b1bdaa675c6e1f7152 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 11:10:46 -0500 Subject: [PATCH 0352/1533] =?UTF-8?q?Update=200040.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\210\346\200\273\345\222\214II.md" | 141 +++++++++--------- 1 file changed, 70 insertions(+), 71 deletions(-) diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 83708df720..be72d90361 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -354,93 +354,92 @@ class Solution { ``` ## Python -**回溯+巧妙去重(省去使用used** +回溯 ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] - def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: - ''' - 类似于求三数之和,求四数之和,为了避免重复组合,需要提前进行数组排序 - ''' - self.paths.clear() - self.path.clear() - # 必须提前进行数组排序,避免重复 - candidates.sort() - self.backtracking(candidates, target, 0, 0) - return self.paths - def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None: - # Base Case - if sum_ == target: - self.paths.append(self.path[:]) + def backtracking(self, candidates, target, total, startIndex, path, result): + if total == target: + result.append(path[:]) return - - # 单层递归逻辑 - for i in range(start_index, len(candidates)): - # 剪枝,同39.组合总和 - if sum_ + candidates[i] > target: - return - - # 跳过同一树层使用过的元素 - if i > start_index and candidates[i] == candidates[i-1]: + + for i in range(startIndex, len(candidates)): + if i > startIndex and candidates[i] == candidates[i - 1]: continue - - sum_ += candidates[i] - self.path.append(candidates[i]) - self.backtracking(candidates, target, sum_, i+1) - self.path.pop() # 回溯,为了下一轮for loop - sum_ -= candidates[i] # 回溯,为了下一轮for loop + + if total + candidates[i] > target: + break + + total += candidates[i] + path.append(candidates[i]) + self.backtracking(candidates, target, total, i + 1, path, result) + total -= candidates[i] + path.pop() + + def combinationSum2(self, candidates, target): + result = [] + candidates.sort() + self.backtracking(candidates, target, 0, 0, [], result) + return result + ``` -**回溯+去重(使用used)** +回溯+去重(使用used) ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] - self.used = [] - def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: - ''' - 类似于求三数之和,求四数之和,为了避免重复组合,需要提前进行数组排序 - 本题需要使用used,用来标记区别同一树层的元素使用重复情况:注意区分递归纵向遍历遇到的重复元素,和for循环遇到的重复元素,这两者的区别 - ''' - self.paths.clear() - self.path.clear() - self.usage_list = [False] * len(candidates) - # 必须提前进行数组排序,避免重复 - candidates.sort() - self.backtracking(candidates, target, 0, 0) - return self.paths - def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None: - # Base Case - if sum_ == target: - self.paths.append(self.path[:]) + def backtracking(self, candidates, target, total, startIndex, used, path, result): + if total == target: + result.append(path[:]) return - - # 单层递归逻辑 - for i in range(start_index, len(candidates)): - # 剪枝,同39.组合总和 - if sum_ + candidates[i] > target: - return - - # 检查同一树层是否出现曾经使用过的相同元素 - # 若数组中前后元素值相同,但前者却未被使用(used == False),说明是for loop中的同一树层的相同元素情况 - if i > 0 and candidates[i] == candidates[i-1] and self.usage_list[i-1] == False: + + for i in range(startIndex, len(candidates)): + # 对于相同的数字,只选择第一个未被使用的数字,跳过其他相同数字 + if i > startIndex and candidates[i] == candidates[i - 1] and not used[i - 1]: continue - sum_ += candidates[i] - self.path.append(candidates[i]) - self.usage_list[i] = True - self.backtracking(candidates, target, sum_, i+1) - self.usage_list[i] = False # 回溯,为了下一轮for loop - self.path.pop() # 回溯,为了下一轮for loop - sum_ -= candidates[i] # 回溯,为了下一轮for loop + if total + candidates[i] > target: + break + + total += candidates[i] + path.append(candidates[i]) + used[i] = True + self.backtracking(candidates, target, total, i + 1, used, path, result) + used[i] = False + total -= candidates[i] + path.pop() + + def combinationSum2(self, candidates, target): + used = [False] * len(candidates) + result = [] + candidates.sort() + self.backtracking(candidates, target, 0, 0, used, [], result) + return result + ``` +回溯优化 +```python +class Solution: + def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: + candidates.sort() + results = [] + self.combinationSumHelper(candidates, target, 0, [], results) + return results + def combinationSumHelper(self, candidates, target, index, path, results): + if target == 0: + results.append(path[:]) + return + for i in range(index, len(candidates)): + if i > index and candidates[i] == candidates[i - 1]: + continue + if candidates[i] > target: + break + path.append(candidates[i]) + self.combinationSumHelper(candidates, target - candidates[i], i + 1, path, results) + path.pop() +``` ## Go 主要在于如何在回溯中去重 From 59742e5544055ea3f4830ff5fea98f7e26671385 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 11:11:44 -0500 Subject: [PATCH 0353/1533] =?UTF-8?q?Update=200040.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index be72d90361..1dd02b69cf 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -384,7 +384,7 @@ class Solution: return result ``` -回溯+去重(使用used) +回溯 使用used ```python class Solution: From a4dcbe67217574e6fd197ad0c9598177faec79e9 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Fri, 26 May 2023 18:05:09 -0400 Subject: [PATCH 0354/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E4=BA=8C?= =?UTF-8?q?=E7=B6=AD"=E6=95=B4=E6=95=B8"=E6=95=B8=E7=B5=84=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E3=80=81=E5=84=AA=E5=8C=96=E4=B8=80=E7=B6=AD?= =?UTF-8?q?=E6=95=B8=E7=B5=84=E7=9A=84=E8=A7=A3=E6=B3=95(=E5=89=AA?= =?UTF-8?q?=E6=9E=9D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增java二維"整數"數組的解法,原本是用boolean數組,有一點繞,故附上二維"整數"數組。 2. 優化一維數組的解法(剪枝),在每一次內層循環完後,立刻檢查dp[target] == target。實測時間複雜度: 26ms -> 20ms --- ...11\345\222\214\345\255\220\351\233\206.md" | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 8115e18ea9..54d9061217 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -218,8 +218,12 @@ class Solution { for(int i = 0; i < n; i++) { for(int j = target; j >= nums[i]; j--) { //物品 i 的重量是 nums[i],其价值也是 nums[i] - dp[j] = Math.max(dp[j], dp[j-nums[i]] + nums[i]); + dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]); } + + //剪枝一下,每一次完成內層的for-loop,立即檢查是否dp[target] == target,優化時間複雜度(26ms -> 20ms) + if(dp[target] == target) + return true; } return dp[target] == target; } @@ -294,6 +298,61 @@ false true false false false true true false false false false false false true false false false true true false false false false true false true false false false true true false false false true true ``` +二維數組整數版本 +```Java +class Solution { + public boolean canPartition(int[] nums) { + //using 2-D DP array. + int len = nums.length; + //check edge cases; + if(len == 0) + return false; + + int sum = 0; + for (int num : nums) + sum += num; + //we only deal with even numbers. If sum is odd, return false; + if(sum % 2 == 1) + return false; + + int target = sum / 2; + int[][] dp = new int[nums.length][target + 1]; + + // for(int j = 0; j <= target; j++){ + // if(j < nums[0]) + // dp[0][j] = 0; + // else + // dp[0][j] = nums[0]; + // } + + //initialize dp array + for(int j = nums[0]; j <= target; j++){ + dp[0][j] = nums[0]; + } + + for(int i = 1; i < len; i++){ + for(int j = 0; j <= target; j++){ + if (j < nums[i]) + dp[i][j] = dp[i - 1][j]; + else + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); + } + } + + //print out DP array + // for(int x : dp){ + // System.out.print(x + ","); + // } + // System.out.print(" "+i+" row"+"\n"); + return dp[len - 1][target] == target; + } +} +//dp数组的打印结果 for test case 1. +0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +0, 1, 1, 1, 1, 5, 6, 6, 6, 6, 6, 6, +0, 1, 1, 1, 1, 5, 6, 6, 6, 6, 6, 11, +0, 1, 1, 1, 1, 5, 6, 6, 6, 6, 10, 11, +``` ### Python: ```python From 45863868708ba1c3260060fb5afa854fa6874184 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 21:03:46 -0500 Subject: [PATCH 0355/1533] =?UTF-8?q?Update=200131.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\345\233\236\346\226\207\344\270\262.md" | 135 +++++++++++------- 1 file changed, 87 insertions(+), 48 deletions(-) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 30bba455fb..53f43eea68 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -349,12 +349,9 @@ class Solution { ``` ## Python -**回溯+正反序判断回文串** +回溯 基本版 ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] def partition(self, s: str) -> List[List[str]]: ''' @@ -363,76 +360,118 @@ class Solution: 当切割线迭代至字符串末尾,说明找到一种方法 类似组合问题,为了不重复切割同一位置,需要start_index来做标记下一轮递归的起始位置(切割线) ''' - self.path.clear() - self.paths.clear() - self.backtracking(s, 0) - return self.paths + result = [] + self.backtracking(s, 0, [], result) + return result - def backtracking(self, s: str, start_index: int) -> None: + def backtracking(self, s, start_index, path, result ): # Base Case - if start_index >= len(s): - self.paths.append(self.path[:]) + if start_index == len(s): + result.append(path[:]) return # 单层递归逻辑 for i in range(start_index, len(s)): # 此次比其他组合题目多了一步判断: # 判断被截取的这一段子串([start_index, i])是否为回文串 - temp = s[start_index:i+1] - if temp == temp[::-1]: # 若反序和正序相同,意味着这是回文串 - self.path.append(temp) - self.backtracking(s, i+1) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 - self.path.pop() + if self.is_palindrome(s, start_index, i): + path.append(s[start_index:i+1]) + self.backtracking(s, i+1, path, result) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 + path.pop() # 回溯 else: continue + + def is_palindrome(self, s: str, start: int, end: int) -> bool: + i: int = start + j: int = end + while i < j: + if s[i] != s[j]: + return False + i += 1 + j -= 1 + return True ``` -**回溯+函数判断回文串** +回溯+优化判定回文函数 ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] def partition(self, s: str) -> List[List[str]]: - ''' - 递归用于纵向遍历 - for循环用于横向遍历 - 当切割线迭代至字符串末尾,说明找到一种方法 - 类似组合问题,为了不重复切割同一位置,需要start_index来做标记下一轮递归的起始位置(切割线) - ''' - self.path.clear() - self.paths.clear() - self.backtracking(s, 0) - return self.paths + result = [] + self.backtracking(s, 0, [], result) + return result - def backtracking(self, s: str, start_index: int) -> None: + def backtracking(self, s, start_index, path, result ): # Base Case - if start_index >= len(s): - self.paths.append(self.path[:]) + if start_index == len(s): + result.append(path[:]) return # 单层递归逻辑 for i in range(start_index, len(s)): - # 此次比其他组合题目多了一步判断: - # 判断被截取的这一段子串([start_index, i])是否为回文串 - if self.is_palindrome(s, start_index, i): - self.path.append(s[start_index:i+1]) - self.backtracking(s, i+1) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 - self.path.pop() # 回溯 + # 若反序和正序相同,意味着这是回文串 + if s[start_index: i + 1] == s[start_index: i + 1][::-1]: + path.append(s[start_index:i+1]) + self.backtracking(s, i+1, path, result) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 + path.pop() # 回溯 else: continue +``` +回溯+高效判断回文子串 +```python +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = [] + isPalindrome = [[False] * len(s) for _ in range(len(s))] # 初始化isPalindrome矩阵 + self.computePalindrome(s, isPalindrome) + self.backtracking(s, 0, [], result, isPalindrome) + return result + + def backtracking(self, s, startIndex, path, result, isPalindrome): + if startIndex >= len(s): + result.append(path[:]) + return - def is_palindrome(self, s: str, start: int, end: int) -> bool: - i: int = start - j: int = end - while i < j: - if s[i] != s[j]: - return False - i += 1 - j -= 1 - return True + for i in range(startIndex, len(s)): + if isPalindrome[startIndex][i]: # 是回文子串 + substring = s[startIndex:i + 1] + path.append(substring) + self.backtracking(s, i + 1, path, result, isPalindrome) # 寻找i+1为起始位置的子串 + path.pop() # 回溯过程,弹出本次已经填在的子串 + + def computePalindrome(self, s, isPalindrome): + for i in range(len(s) - 1, -1, -1): # 需要倒序计算,保证在i行时,i+1行已经计算好了 + for j in range(i, len(s)): + if j == i: + isPalindrome[i][j] = True + elif j - i == 1: + isPalindrome[i][j] = (s[i] == s[j]) + else: + isPalindrome[i][j] = (s[i] == s[j] and isPalindrome[i+1][j-1]) ``` +回溯+使用all函数判断回文子串 +```python +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = [] + self.partition_helper(s, 0, [], result) + return result + def partition_helper(self, s, start_index, path, result): + if start_index == len(s): + result.append(path[:]) + return + + for i in range(start_index + 1, len(s) + 1): + sub = s[start_index:i] + if self.isPalindrome(sub): + path.append(sub) + self.partition_helper(s, i, path, result) + path.pop() + + def isPalindrome(self, s): + return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2)) + +``` ## Go ```go var ( From 78445d75573bd7d45e42a52cb1388a5f61449a73 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 26 May 2023 21:06:59 -0500 Subject: [PATCH 0356/1533] =?UTF-8?q?Update=200131.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\206\345\211\262\345\233\236\346\226\207\344\270\262.md" | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 53f43eea68..a797a0b5fb 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -378,8 +378,7 @@ class Solution: path.append(s[start_index:i+1]) self.backtracking(s, i+1, path, result) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 path.pop() # 回溯 - else: - continue + def is_palindrome(self, s: str, start: int, end: int) -> bool: i: int = start @@ -413,8 +412,7 @@ class Solution: path.append(s[start_index:i+1]) self.backtracking(s, i+1, path, result) # 递归纵向遍历:从下一处进行切割,判断其余是否仍为回文串 path.pop() # 回溯 - else: - continue + ``` 回溯+高效判断回文子串 ```python From 51c89762f6c9643f76da1ea7fb516ca204fd8634 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 00:51:28 -0500 Subject: [PATCH 0357/1533] =?UTF-8?q?Update=200093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\216\237IP\345\234\260\345\235\200.md" | 134 ++++++++---------- 1 file changed, 56 insertions(+), 78 deletions(-) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 9d4d59180e..81192ce592 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -360,104 +360,82 @@ class Solution { ## python -python2: -```python -class Solution(object): - def restoreIpAddresses(self, s): - """ - :type s: str - :rtype: List[str] - """ - ans = [] - path = [] - def backtrack(path, startIndex): - if len(s) > 12: return [] - if len(path) == 4: - if startIndex == len(s): - ans.append(".".join(path[:])) - return - for i in range(startIndex+1, min(startIndex+4, len(s)+1)): # 剪枝 - string = s[startIndex:i] - if not 0 <= int(string) <= 255: - continue - if not string == "0" and not string.lstrip('0') == string: - continue - path.append(string) - backtrack(path, i) - path.pop() - - backtrack([], 0) - return ans -``` - -python3: +回溯(版本一) ```python class Solution: - def __init__(self): - self.result = [] - def restoreIpAddresses(self, s: str) -> List[str]: - ''' - 本质切割问题使用回溯搜索法,本题只能切割三次,所以纵向递归总共四层 - 因为不能重复分割,所以需要start_index来记录下一层递归分割的起始位置 - 添加变量point_num来记录逗号的数量[0,3] - ''' - self.result.clear() - if len(s) > 12: return [] - self.backtracking(s, 0, 0) - return self.result - - def backtracking(self, s: str, start_index: int, point_num: int) -> None: - # Base Case - if point_num == 3: - if self.is_valid(s, start_index, len(s)-1): - self.result.append(s[:]) + result = [] + self.backtracking(s, 0, 0, "", result) + return result + + def backtracking(self, s, start_index, point_num, current, result): + if point_num == 3: # 逗点数量为3时,分隔结束 + if self.is_valid(s, start_index, len(s) - 1): # 判断第四段子字符串是否合法 + current += s[start_index:] # 添加最后一段子字符串 + result.append(current) return - # 单层递归逻辑 + for i in range(start_index, len(s)): - # [start_index, i]就是被截取的子串 - if self.is_valid(s, start_index, i): - s = s[:i+1] + '.' + s[i+1:] - self.backtracking(s, i+2, point_num+1) # 在填入.后,下一子串起始后移2位 - s = s[:i+1] + s[i+2:] # 回溯 + if self.is_valid(s, start_index, i): # 判断 [start_index, i] 这个区间的子串是否合法 + sub = s[start_index:i + 1] + self.backtracking(s, i + 1, point_num + 1, current + sub + '.', result) else: - # 若当前被截取的子串大于255或者大于三位数,直接结束本层循环 break - - def is_valid(self, s: str, start: int, end: int) -> bool: - if start > end: return False - # 若数字是0开头,不合法 - if s[start] == '0' and start != end: + + def is_valid(self, s, start, end): + if start > end: return False - if not 0 <= int(s[start:end+1]) <= 255: + if s[start] == '0' and start != end: # 0开头的数字不合法 return False + num = 0 + for i in range(start, end + 1): + if not s[i].isdigit(): # 遇到非数字字符不合法 + return False + num = num * 10 + int(s[i]) + if num > 255: # 如果大于255了不合法 + return False return True + ``` +回溯(版本二) -python3; 简单拼接版本(类似Leetcode131写法): ```python -class Solution: +class Solution: def restoreIpAddresses(self, s: str) -> List[str]: - global results, path results = [] - path = [] - self.backtracking(s,0) + self.backtracking(s, 0, [], results) return results - def backtracking(self,s,index): - global results,path - if index == len(s) and len(path)==4: - results.append('.'.join(path)) # 在连接时需要中间间隔符号的话就在''中间写上对应的间隔符 + def backtracking(self, s, index, path, results): + if index == len(s) and len(path) == 4: + results.append('.'.join(path)) + return + + if len(path) > 4: # 剪枝 return - for i in range(index,len(s)): - if len(path)>3: break # 剪枝 - temp = s[index:i+1] - if (int(temp)<256 and int(temp)>0 and temp[0]!='0') or (temp=='0'): - path.append(temp) - self.backtracking(s,i+1) - path.pop() + + for i in range(index, min(index + 3, len(s))): + if self.is_valid(s, index, i): + sub = s[index:i+1] + path.append(sub) + self.backtracking(s, i+1, path, results) + path.pop() + + def is_valid(self, s, start, end): + if start > end: + return False + if s[start] == '0' and start != end: # 0开头的数字不合法 + return False + num = int(s[start:end+1]) + return 0 <= num <= 255 + + + + ``` + + ## Go ```go From 41537211f86035ac58a7a0121e35d4838ebd61f2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 27 May 2023 16:07:52 +0800 Subject: [PATCH 0358/1533] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\344\270\200\345\222\214\351\233\266.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 6a178a254b..145f6ec193 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -491,6 +491,33 @@ object Solution { } ``` +### Rust + +```rust +impl Solution { + pub fn find_max_form(strs: Vec, m: i32, n: i32) -> i32 { + let (m, n) = (m as usize, n as usize); + let mut dp = vec![vec![0; n + 1]; m + 1]; + for s in strs { + let (mut one_num, mut zero_num) = (0, 0); + for c in s.chars() { + match c { + '0' => zero_num += 1, + '1' => one_num += 1, + _ => (), + } + } + for i in (zero_num..=m).rev() { + for j in (one_num..=n).rev() { + dp[i][j] = dp[i][j].max(dp[i - zero_num][j - one_num] + 1); + } + } + } + dp[m][n] + } +} +``` +

From 8b6e90d2e24bdec8574a213f16f81c049f7e5fb2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 27 May 2023 17:36:47 +0800 Subject: [PATCH 0359/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\205\250\350\203\214\345\214\205.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index e927aa2061..9a48cb71cf 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -388,6 +388,43 @@ object Solution { } ``` +Rust: + +```rust +impl Solution { + // 先遍历物品 + fn complete_pack() { + let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4); + let mut dp = vec![0; bag_size + 1]; + for (weight, value) in goods { + for j in weight..=bag_size { + dp[j] = dp[j].max(dp[j - weight] + value); + } + } + println!("先遍历物品:{}", dp[bag_size]); + } + + // 先遍历背包 + fn complete_pack_after() { + let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4); + let mut dp = vec![0; bag_size + 1]; + for i in 0..=bag_size { + for (weight, value) in &goods { + if i >= *weight { + dp[i] = dp[i].max(dp[i - weight] + value); + } + } + } + println!("先遍历背包:{}", dp[bag_size]); + } +} + +#[test] +fn test_complete_pack() { + Solution::complete_pack(); + Solution::complete_pack_after(); +} +```

From 73590631ec3e17e727cca5a56024a07967946668 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 27 May 2023 18:49:18 +0800 Subject: [PATCH 0360/1533] =?UTF-8?q?Update=200518.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2II.md=20=E4=BC=98=E5=8C=96=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\351\222\261\345\205\221\346\215\242II.md" | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index c208754f77..8da351148b 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -282,17 +282,18 @@ func change(amount int, coins []int) int { Rust: ```rust -pub fn change(amount: i32, coins: Vec) -> i32 { - let amount = amount as usize; - let coins = coins.iter().map(|&c|c as usize).collect::>(); - let mut dp = vec![0usize; amount + 1]; - dp[0] = 1; - for i in 0..coins.len() { - for j in coins[i]..=amount { - dp[j] += dp[j - coins[i]]; +impl Solution { + pub fn change(amount: i32, coins: Vec) -> i32 { + let amount = amount as usize; + let mut dp = vec![0; amount + 1]; + dp[0] = 1; + for coin in coins { + for j in coin as usize..=amount { + dp[j] += dp[j - coin as usize]; + } } + dp[amount] } - dp[amount] as i32 } ``` From b76f5b6bffc6f53d81c7afdd6f05853f0577d68e Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 27 May 2023 23:14:11 +0800 Subject: [PATCH 0361/1533] =?UTF-8?q?Update=200377.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C=E2=85=A3.md=20=E4=BC=98=E5=8C=96=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\220\210\346\200\273\345\222\214\342\205\243.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index ee65972371..ec57906087 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -253,16 +253,17 @@ Rust ```Rust impl Solution { pub fn combination_sum4(nums: Vec, target: i32) -> i32 { - let mut dp = vec![0; target as usize + 1]; + let target = target as usize; + let mut dp = vec![0; target + 1]; dp[0] = 1; - for i in 1..=target as usize { - for &j in nums.iter() { - if i as i32 >= j { - dp[i] += dp[i- j as usize]; + for i in 1..=target { + for &n in &nums { + if i >= n as usize { + dp[i] += dp[i - n as usize]; } } } - return dp[target as usize]; + dp[target] } } ``` From 824ce31f87024fb4d7cc4e4fccdb9c56a761d0cb Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 27 May 2023 23:28:33 +0800 Subject: [PATCH 0362/1533] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88=E6=9C=AC?= =?UTF-8?q?.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\214\205\347\211\210\346\234\254.md" | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 8f8bc9a649..5dde64098d 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -225,8 +225,25 @@ function climbStairs(n: number): number { }; ``` +Rust: - +```rust +impl Solution { + pub fn climb_stairs(n: i32) -> i32 { + let (n, m) = (n as usize, 2); + let mut dp = vec![0; n + 1]; + dp[0] = 1; + for i in 1..=n { + for j in 1..=m { + if i >= j { + dp[i] += dp[i - j]; + } + } + } + dp[n] + } +} +```

From d45c141cc676cf0ef55dccd85705f3eb5c8323a7 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 18:38:57 -0500 Subject: [PATCH 0363/1533] =?UTF-8?q?Update=200078.=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0078.\345\255\220\351\233\206.md" | 36 ++++++++------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index f26d0821e9..ecc202b73b 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -206,28 +206,20 @@ class Solution { ## Python ```python class Solution: - def __init__(self): - self.path: List[int] = [] - self.paths: List[List[int]] = [] - - def subsets(self, nums: List[int]) -> List[List[int]]: - self.paths.clear() - self.path.clear() - self.backtracking(nums, 0) - return self.paths - - def backtracking(self, nums: List[int], start_index: int) -> None: - # 收集子集,要先于终止判断 - self.paths.append(self.path[:]) - # Base Case - if start_index == len(nums): - return - - # 单层递归逻辑 - for i in range(start_index, len(nums)): - self.path.append(nums[i]) - self.backtracking(nums, i+1) - self.path.pop() # 回溯 + def subsets(self, nums): + result = [] + path = [] + self.backtracking(nums, 0, path, result) + return result + + def backtracking(self, nums, startIndex, path, result): + result.append(path[:]) # 收集子集,要放在终止添加的上面,否则会漏掉自己 + # if startIndex >= len(nums): # 终止条件可以不加 + # return + for i in range(startIndex, len(nums)): + path.append(nums[i]) + self.backtracking(nums, i + 1, path, result) + path.pop() ``` ## Go From 5801ae752325eddc69508dd3635fadc6b74abf69 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 18:53:28 -0500 Subject: [PATCH 0364/1533] =?UTF-8?q?Update=200090.=E5=AD=90=E9=9B=86II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0090.\345\255\220\351\233\206II.md" | 124 +++++++++--------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 1a9f8fda3e..0df0600eaf 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -235,86 +235,84 @@ class Solution { } ``` -### Python + + +#### Python3 + +回溯 利用used数组去重 ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] - - def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: - nums.sort() - self.backtracking(nums, 0) - return self.paths - - def backtracking(self, nums: List[int], start_index: int) -> None: - # ps.空集合仍符合要求 - self.paths.append(self.path[:]) - # Base Case - if start_index == len(nums): - return - - # 单层递归逻辑 - for i in range(start_index, len(nums)): - if i > start_index and nums[i] == nums[i-1]: - # 当前后元素值相同时,跳入下一个循环,去重 + def subsetsWithDup(self, nums): + result = [] + path = [] + used = [False] * len(nums) + nums.sort() # 去重需要排序 + self.backtracking(nums, 0, used, path, result) + return result + + def backtracking(self, nums, startIndex, used, path, result): + result.append(path[:]) # 收集子集 + for i in range(startIndex, len(nums)): + # used[i - 1] == True,说明同一树枝 nums[i - 1] 使用过 + # used[i - 1] == False,说明同一树层 nums[i - 1] 使用过 + # 而我们要对同一树层使用过的元素进行跳过 + if i > 0 and nums[i] == nums[i - 1] and not used[i - 1]: continue - self.path.append(nums[i]) - self.backtracking(nums, i+1) - self.path.pop() + path.append(nums[i]) + used[i] = True + self.backtracking(nums, i + 1, used, path, result) + used[i] = False + path.pop() + ``` -#### Python3 +回溯 利用集合去重 -不使用used数组 ```python class Solution: - def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: - res = [] + def subsetsWithDup(self, nums): + result = [] path = [] - nums.sort() # 去重需要先对数组进行排序 - - def backtracking(nums, startIndex): - # 终止条件 - res.append(path[:]) - if startIndex == len(nums): - return - - # for循环 - for i in range(startIndex, len(nums)): - # 数层去重 - if i > startIndex and nums[i] == nums[i-1]: # 去重 - continue - path.append(nums[i]) - backtracking(nums, i+1) - path.pop() - - backtracking(nums, 0) - return res + nums.sort() # 去重需要排序 + self.backtracking(nums, 0, path, result) + return result + + def backtracking(self, nums, startIndex, path, result): + result.append(path[:]) # 收集子集 + uset = set() + for i in range(startIndex, len(nums)): + if nums[i] in uset: + continue + uset.add(nums[i]) + path.append(nums[i]) + self.backtracking(nums, i + 1, path, result) + path.pop() + ``` -使用used数组 +回溯 利用递归的时候下一个startIndex是i+1而不是0去重 + ```python class Solution: - def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: + def subsetsWithDup(self, nums): result = [] path = [] - nums.sort() - used = [0] * len(nums) - def backtrack(nums, startIdx): - result.append(path[:]) - for i in range(startIdx, len(nums)): - if i > startIdx and nums[i] == nums[i-1] and used[i-1] == 0: - continue - used[i] = 1 - path.append(nums[i]) - backtrack(nums, i+1) - path.pop() - used[i] = 0 - backtrack(nums, 0) + nums.sort() # 去重需要排序 + self.backtracking(nums, 0, path, result) return result -``` + def backtracking(self, nums, startIndex, path, result): + result.append(path[:]) # 收集子集 + for i in range(startIndex, len(nums)): + # 而我们要对同一树层使用过的元素进行跳过 + if i > startIndex and nums[i] == nums[i - 1]: + continue + path.append(nums[i]) + self.backtracking(nums, i + 1, path, result) + path.pop() + + +``` ### Go ```Go From eb72f2637195f8c50bff7660ba90ddf217654840 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 19:17:26 -0500 Subject: [PATCH 0365/1533] =?UTF-8?q?Update=200491.=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\345\255\220\345\272\217\345\210\227.md" | 106 +++++++----------- 1 file changed, 40 insertions(+), 66 deletions(-) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 436dbf01c8..56a70e8bc5 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -267,79 +267,53 @@ class Solution { ### Python python3 -**回溯** +回溯 利用set去重 ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] - - def findSubsequences(self, nums: List[int]) -> List[List[int]]: - ''' - 本题求自增子序列,所以不能改变原数组顺序 - ''' - self.backtracking(nums, 0) - return self.paths - - def backtracking(self, nums: List[int], start_index: int): - # 收集结果,同78.子集,仍要置于终止条件之前 - if len(self.path) >= 2: - # 本题要求所有的节点 - self.paths.append(self.path[:]) + def findSubsequences(self, nums): + result = [] + self.backtracking(nums, 0, [], result) + return result + + def backtracking(self, nums, startIndex, path, result): + if len(path) > 1: + result.append(path[:]) # 将当前路径的副本加入结果集 - # Base Case(可忽略) - if start_index == len(nums): - return - - # 单层递归逻辑 - # 深度遍历中每一层都会有一个全新的usage_list用于记录本层元素是否重复使用 - usage_list = set() - # 同层横向遍历 - for i in range(start_index, len(nums)): - # 若当前元素值小于前一个时(非递增)或者曾用过,跳入下一循环 - if (self.path and nums[i] < self.path[-1]) or nums[i] in usage_list: - continue - usage_list.add(nums[i]) - self.path.append(nums[i]) - self.backtracking(nums, i+1) - self.path.pop() + used = set() # 使用集合来进行去重操作 + for i in range(startIndex, len(nums)): + if path and nums[i] < path[-1]: + continue # 如果当前元素小于上一个元素,则跳过当前元素 + + if nums[i] in used: + continue # 如果当前元素已经使用过,则跳过当前元素 + + used.add(nums[i]) # 标记当前元素已经使用过 + self.backtracking(nums, i + 1, path + [nums[i]], result) + ``` -**回溯+哈希表去重** +回溯 利用哈希表去重 ```python class Solution: - def __init__(self): - self.paths = [] - self.path = [] - - def findSubsequences(self, nums: List[int]) -> List[List[int]]: - ''' - 本题求自增子序列,所以不能改变原数组顺序 - ''' - self.backtracking(nums, 0) - return self.paths - - def backtracking(self, nums: List[int], start_index: int): - # 收集结果,同78.子集,仍要置于终止条件之前 - if len(self.path) >= 2: - # 本题要求所有的节点 - self.paths.append(self.path[:]) + def findSubsequences(self, nums): + result = [] + path = [] + self.backtracking(nums, 0, path, result) + return result + + def backtracking(self, nums, startIndex, path, result): + if len(path) > 1: + result.append(path[:]) # 注意要使用切片将当前路径的副本加入结果集 - # Base Case(可忽略) - if start_index == len(nums): - return - - # 单层递归逻辑 - # 深度遍历中每一层都会有一个全新的usage_list用于记录本层元素是否重复使用 - usage_list = [False] * 201 # 使用列表去重,题中取值范围[-100, 100] - # 同层横向遍历 - for i in range(start_index, len(nums)): - # 若当前元素值小于前一个时(非递增)或者曾用过,跳入下一循环 - if (self.path and nums[i] < self.path[-1]) or usage_list[nums[i]+100] == True: - continue - usage_list[nums[i]+100] = True - self.path.append(nums[i]) - self.backtracking(nums, i+1) - self.path.pop() + used = [0] * 201 # 使用数组来进行去重操作,题目说数值范围[-100, 100] + for i in range(startIndex, len(nums)): + if (path and nums[i] < path[-1]) or used[nums[i] + 100] == 1: + continue # 如果当前元素小于上一个元素,或者已经使用过当前元素,则跳过当前元素 + + used[nums[i] + 100] = 1 # 标记当前元素已经使用过 + path.append(nums[i]) # 将当前元素加入当前递增子序列 + self.backtracking(nums, i + 1, path, result) + path.pop() + ``` ### Go From 7e3fc8ea0d59a9a2c0e1765b62cc6e386cef5fdf Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 19:17:50 -0500 Subject: [PATCH 0366/1533] =?UTF-8?q?Update=200491.=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 56a70e8bc5..a805a2628b 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -266,7 +266,7 @@ class Solution { ### Python -python3 + 回溯 利用set去重 ```python class Solution: From a6c95b78e80f754dc901747046e06008710a639f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 19:21:17 -0500 Subject: [PATCH 0367/1533] =?UTF-8?q?Update=200491.=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\345\255\220\345\272\217\345\210\227.md" | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index a805a2628b..50cb991879 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -272,23 +272,24 @@ class Solution { class Solution: def findSubsequences(self, nums): result = [] - self.backtracking(nums, 0, [], result) + path = [] + self.backtracking(nums, 0, path, result) return result - + def backtracking(self, nums, startIndex, path, result): if len(path) > 1: - result.append(path[:]) # 将当前路径的副本加入结果集 + result.append(path[:]) # 注意要使用切片将当前路径的副本加入结果集 + # 注意这里不要加return,要取树上的节点 - used = set() # 使用集合来进行去重操作 + uset = set() # 使用集合对本层元素进行去重 for i in range(startIndex, len(nums)): - if path and nums[i] < path[-1]: - continue # 如果当前元素小于上一个元素,则跳过当前元素 - - if nums[i] in used: - continue # 如果当前元素已经使用过,则跳过当前元素 + if (path and nums[i] < path[-1]) or nums[i] in uset: + continue - used.add(nums[i]) # 标记当前元素已经使用过 - self.backtracking(nums, i + 1, path + [nums[i]], result) + uset.add(nums[i]) # 记录这个元素在本层用过了,本层后面不能再用了 + path.append(nums[i]) + self.backtracking(nums, i + 1, path, result) + path.pop() ``` 回溯 利用哈希表去重 @@ -314,6 +315,7 @@ class Solution: self.backtracking(nums, i + 1, path, result) path.pop() + ``` ### Go From b033a08c13547a43f3ac4a873f65151987569e6a Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 20:53:39 -0500 Subject: [PATCH 0368/1533] =?UTF-8?q?Update=200046.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6.\345\205\250\346\216\222\345\210\227.md" | 73 ++++--------------- 1 file changed, 16 insertions(+), 57 deletions(-) diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index e08aec940b..029a98e376 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -213,68 +213,27 @@ class Solution { ``` ### Python -**回溯** +回溯 使用used ```python class Solution: - def __init__(self): - self.path = [] - self.paths = [] - - def permute(self, nums: List[int]) -> List[List[int]]: - ''' - 因为本题排列是有序的,这意味着同一层的元素可以重复使用,但同一树枝上不能重复使用(usage_list) - 所以处理排列问题每层都需要从头搜索,故不再使用start_index - ''' - usage_list = [False] * len(nums) - self.backtracking(nums, usage_list) - return self.paths - - def backtracking(self, nums: List[int], usage_list: List[bool]) -> None: - # Base Case本题求叶子节点 - if len(self.path) == len(nums): - self.paths.append(self.path[:]) + def permute(self, nums): + result = [] + self.backtracking(nums, [], [False] * len(nums), result) + return result + + def backtracking(self, nums, path, used, result): + if len(path) == len(nums): + result.append(path[:]) return - - # 单层递归逻辑 - for i in range(0, len(nums)): # 从头开始搜索 - # 若遇到self.path里已收录的元素,跳过 - if usage_list[i] == True: + for i in range(len(nums)): + if used[i]: continue - usage_list[i] = True - self.path.append(nums[i]) - self.backtracking(nums, usage_list) # 纵向传递使用信息,去重 - self.path.pop() - usage_list[i] = False -``` -**回溯+丢掉usage_list** -```python -class Solution: - def __init__(self): - self.path = [] - self.paths = [] - - def permute(self, nums: List[int]) -> List[List[int]]: - ''' - 因为本题排列是有序的,这意味着同一层的元素可以重复使用,但同一树枝上不能重复使用 - 所以处理排列问题每层都需要从头搜索,故不再使用start_index - ''' - self.backtracking(nums) - return self.paths - - def backtracking(self, nums: List[int]) -> None: - # Base Case本题求叶子节点 - if len(self.path) == len(nums): - self.paths.append(self.path[:]) - return + used[i] = True + path.append(nums[i]) + self.backtracking(nums, path, used, result) + path.pop() + used[i] = False - # 单层递归逻辑 - for i in range(0, len(nums)): # 从头开始搜索 - # 若遇到self.path里已收录的元素,跳过 - if nums[i] in self.path: - continue - self.path.append(nums[i]) - self.backtracking(nums) - self.path.pop() ``` ### Go From fd2608a38c99b2ff98130112c9c3a555b1be362d Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 21:20:59 -0500 Subject: [PATCH 0369/1533] =?UTF-8?q?Update=200047.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\205\250\346\216\222\345\210\227II.md" | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index b4f7a4d8cb..de9b2bb486 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -208,28 +208,25 @@ class Solution { ```python class Solution: - def permuteUnique(self, nums: List[int]) -> List[List[int]]: - # res用来存放结果 - if not nums: return [] - res = [] - used = [0] * len(nums) - def backtracking(nums, used, path): - # 终止条件 - if len(path) == len(nums): - res.append(path.copy()) - return - for i in range(len(nums)): - if not used[i]: - if i>0 and nums[i] == nums[i-1] and not used[i-1]: - continue - used[i] = 1 - path.append(nums[i]) - backtracking(nums, used, path) - path.pop() - used[i] = 0 - # 记得给nums排序 - backtracking(sorted(nums),used,[]) - return res + def permuteUnique(self, nums): + nums.sort() # 排序 + result = [] + self.backtracking(nums, [], [False] * len(nums), result) + return result + + def backtracking(self, nums, path, used, result): + if len(path) == len(nums): + result.append(path[:]) + return + for i in range(len(nums)): + if (i > 0 and nums[i] == nums[i - 1] and not used[i - 1]) or used[i]: + continue + used[i] = True + path.append(nums[i]) + self.backtracking(nums, path, used, result) + path.pop() + used[i] = False + ``` ### Go From 19ecde0984c6bc4e383b2df72a47ff4032a77ca4 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 21:49:43 -0500 Subject: [PATCH 0370/1533] =?UTF-8?q?Update=20=E5=9B=9E=E6=BA=AF=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6?= =?UTF-8?q?=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\347\247\215\345\206\231\346\263\225.md" | 108 +++++++++--------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index f39c3c74c3..4e09b9252a 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -356,76 +356,80 @@ Python: ```python class Solution: - def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: - res = [] - nums.sort() - def backtracking(start, path): - res.append(path) - uset = set() - for i in range(start, len(nums)): - if nums[i] not in uset: - backtracking(i + 1, path + [nums[i]]) - uset.add(nums[i]) - - backtracking(0, []) - return res + def subsetsWithDup(self, nums): + nums.sort() # 去重需要排序 + result = [] + self.backtracking(nums, 0, [], result) + return result + + def backtracking(self, nums, startIndex, path, result): + result.append(path[:]) + used = set() + for i in range(startIndex, len(nums)): + if nums[i] in used: + continue + used.add(nums[i]) + path.append(nums[i]) + self.backtracking(nums, i + 1, path, result) + path.pop() + ``` **40. 组合总和 II** ```python class Solution: - def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: - - res = [] + def combinationSum2(self, candidates, target): candidates.sort() + result = [] + self.backtracking(candidates, target, 0, 0, [], result) + return result + + def backtracking(self, candidates, target, sum, startIndex, path, result): + if sum == target: + result.append(path[:]) + return + used = set() + for i in range(startIndex, len(candidates)): + if sum + candidates[i] > target: + break + if candidates[i] in used: + continue + used.add(candidates[i]) + sum += candidates[i] + path.append(candidates[i]) + self.backtracking(candidates, target, sum, i + 1, path, result) + sum -= candidates[i] + path.pop() - def backtracking(start, path): - if sum(path) == target: - res.append(path) - elif sum(path) < target: - used = set() - for i in range(start, len(candidates)): - if candidates[i] in used: - continue - else: - used.add(candidates[i]) - backtracking(i + 1, path + [candidates[i]]) - - backtracking(0, []) - - return res ``` **47. 全排列 II** ```python class Solution: - def permuteUnique(self, nums: List[int]) -> List[List[int]]: - path = [] - res = [] - used = [False]*len(nums) - - def backtracking(): - if len(path) == len(nums): - res.append(path.copy()) - - deduplicate = set() - for i, num in enumerate(nums): - if used[i] == True: - continue - if num in deduplicate: - continue + def permuteUnique(self, nums): + nums.sort() # 排序 + result = [] + self.backtracking(nums, [False] * len(nums), [], result) + return result + + def backtracking(self, nums, used, path, result): + if len(path) == len(nums): + result.append(path[:]) + return + used_set = set() + for i in range(len(nums)): + if nums[i] in used_set: + continue + if not used[i]: + used_set.add(nums[i]) used[i] = True path.append(nums[i]) - backtracking() - used[i] = False + self.backtracking(nums, used, path, result) path.pop() - deduplicate.add(num) - - backtracking() + used[i] = False - return res ``` JavaScript: From 498395d07a6363198a99a530555c768432f1148d Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 27 May 2023 22:18:11 -0500 Subject: [PATCH 0371/1533] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 124 +++++++++++------- 1 file changed, 74 insertions(+), 50 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 95e7d3ed5c..fa6414e9ac 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -347,64 +347,88 @@ class Solution { ``` ### python +回溯 使用used数组 ```python class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: - # defaultdic(list) 是为了方便直接append - tickets_dict = defaultdict(list) - for item in tickets: - tickets_dict[item[0]].append(item[1]) - # 给每一个机场的到达机场排序,小的在前面,在回溯里首先被pop(0)出去 - # 这样最先找的的path就是排序最小的答案,直接返回 - for airport in tickets_dict: tickets_dict[airport].sort() - ''' - tickets_dict里面的内容是这样的 - {'JFK': ['ATL', 'SFO'], 'SFO': ['ATL'], 'ATL': ['JFK', 'SFO']}) - ''' - path = ["JFK"] - def backtracking(start_point): - # 终止条件 - if len(path) == len(tickets) + 1: - return True - for _ in tickets_dict[start_point]: - #必须及时删除,避免出现死循环 - end_point = tickets_dict[start_point].pop(0) - path.append(end_point) - # 只要找到一个就可以返回了 - if backtracking(end_point): - return True - path.pop() - tickets_dict[start_point].append(end_point) - - backtracking("JFK") - return path -``` - -python - 使用used数组 - 神似之前几题写法 + tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的 + used = [0] * len(tickets) + path = ['JFK'] + results = [] + self.backtracking(tickets, used, path, 'JFK', results) + return results[0] + + def backtracking(self, tickets, used, path, cur, results): + if len(path) == len(tickets) + 1: # 终止条件:路径长度等于机票数量+1 + results.append(path[:]) # 将当前路径添加到结果列表 + return True + + for i, ticket in enumerate(tickets): # 遍历机票列表 + if ticket[0] == cur and used[i] == 0: # 找到起始机场为cur且未使用过的机票 + used[i] = 1 # 标记该机票为已使用 + path.append(ticket[1]) # 将到达机场添加到路径中 + state = self.backtracking(tickets, used, path, ticket[1], results) # 递归搜索 + path.pop() # 回溯,移除最后添加的到达机场 + used[i] = 0 # 标记该机票为未使用 + if state: + return True # 只要找到一个可行路径就返回,不继续搜索 +``` +回溯 使用字典 ```python +from collections import defaultdict + class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: - global used,path,results - used = [0]*len(tickets) - path = ['JFK'] - results = [] - tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的 - self.backtracking(tickets,'JFK') - return results[0] - def backtracking(self,tickets,cur): - if sum(used) == len(tickets): - results.append(path[:]) - return True # 只要找到就返回 - for i in range(len(tickets)): - if tickets[i][0]==cur and used[i]==0: - used[i]=1 - path.append(tickets[i][1]) - state = self.backtracking(tickets,tickets[i][1]) - path.pop() - used[i]=0 - if state: return True # 只要找到就返回,不继续搜索了 + targets = defaultdict(list) # 构建机场字典 + for ticket in tickets: + targets[ticket[0]].append(ticket[1]) + for airport in targets: + targets[airport].sort() # 对目的地列表进行排序 + + path = ["JFK"] # 起始机场为"JFK" + self.backtracking(targets, path, len(tickets)) + return path + + def backtracking(self, targets, path, ticketNum): + if len(path) == ticketNum + 1: + return True # 找到有效行程 + + airport = path[-1] # 当前机场 + destinations = targets[airport] # 当前机场可以到达的目的地列表 + for i, dest in enumerate(destinations): + targets[airport].pop(i) # 标记已使用的机票 + path.append(dest) # 添加目的地到路径 + if self.backtracking(targets, path, ticketNum): + return True # 找到有效行程 + targets[airport].insert(i, dest) # 回溯,恢复机票 + path.pop() # 移除目的地 + return False # 没有找到有效行程 + +``` +回溯 使用字典 逆序 +```python +from collections import defaultdict + +class Solution: + def findItinerary(self, tickets): + targets = defaultdict(list) # 创建默认字典,用于存储机场映射关系 + for ticket in tickets: + targets[ticket[0]].append(ticket[1]) # 将机票输入到字典中 + + for key in targets: + targets[key].sort(reverse=True) # 对到达机场列表进行字母逆序排序 + + result = [] + self.backtracking("JFK", targets, result) # 调用回溯函数开始搜索路径 + return result[::-1] # 返回逆序的行程路径 + + def backtracking(self, airport, targets, result): + while targets[airport]: # 当机场还有可到达的机场时 + next_airport = targets[airport].pop() # 弹出下一个机场 + self.backtracking(next_airport, targets, result) # 递归调用回溯函数进行深度优先搜索 + result.append(airport) # 将当前机场添加到行程路径中 ``` ### GO From 93e0a18f0f58363435f02267ce417a13ca6314f5 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 28 May 2023 02:52:41 -0500 Subject: [PATCH 0372/1533] =?UTF-8?q?Update=200051.N=E7=9A=87=E5=90=8E.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 83 ++++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index bd1d1c9bec..ab575bd826 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -348,48 +348,47 @@ class Solution { ```python class Solution: def solveNQueens(self, n: int) -> List[List[str]]: - if not n: return [] - board = [['.'] * n for _ in range(n)] - res = [] - def isVaild(board,row, col): - #判断同一列是否冲突 - for i in range(len(board)): - if board[i][col] == 'Q': - return False - # 判断左上角是否冲突 - i = row -1 - j = col -1 - while i>=0 and j>=0: - if board[i][j] == 'Q': - return False - i -= 1 - j -= 1 - # 判断右上角是否冲突 - i = row - 1 - j = col + 1 - while i>=0 and j < len(board): - if board[i][j] == 'Q': - return False - i -= 1 - j += 1 - return True - - def backtracking(board, row, n): - # 如果走到最后一行,说明已经找到一个解 - if row == n: - temp_res = [] - for temp in board: - temp_str = "".join(temp) - temp_res.append(temp_str) - res.append(temp_res) - for col in range(n): - if not isVaild(board, row, col): - continue - board[row][col] = 'Q' - backtracking(board, row+1, n) - board[row][col] = '.' - backtracking(board, 0, n) - return res + result = [] # 存储最终结果的二维字符串数组 + + chessboard = ['.' * n for _ in range(n)] # 初始化棋盘 + self.backtracking(n, 0, chessboard, result) # 回溯求解 + return [[''.join(row) for row in solution] for solution in result] # 返回结果集 + + def backtracking(self, n: int, row: int, chessboard: List[str], result: List[List[str]]) -> None: + if row == n: + result.append(chessboard[:]) # 棋盘填满,将当前解加入结果集 + return + + for col in range(n): + if self.isValid(row, col, chessboard): + chessboard[row] = chessboard[row][:col] + 'Q' + chessboard[row][col+1:] # 放置皇后 + self.backtracking(n, row + 1, chessboard, result) # 递归到下一行 + chessboard[row] = chessboard[row][:col] + '.' + chessboard[row][col+1:] # 回溯,撤销当前位置的皇后 + + def isValid(self, row: int, col: int, chessboard: List[str]) -> bool: + # 检查列 + for i in range(row): + if chessboard[i][col] == 'Q': + return False # 当前列已经存在皇后,不合法 + + # 检查 45 度角是否有皇后 + i, j = row - 1, col - 1 + while i >= 0 and j >= 0: + if chessboard[i][j] == 'Q': + return False # 左上方向已经存在皇后,不合法 + i -= 1 + j -= 1 + + # 检查 135 度角是否有皇后 + i, j = row - 1, col + 1 + while i >= 0 and j < len(chessboard): + if chessboard[i][j] == 'Q': + return False # 右上方向已经存在皇后,不合法 + i -= 1 + j += 1 + + return True # 当前位置合法 + ``` From ff5c98184ed3d01c1ef579d5df5c0d3906ac4f6b Mon Sep 17 00:00:00 2001 From: dd <593274462@qq.com> Date: Sun, 28 May 2023 22:42:49 +0800 Subject: [PATCH 0373/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=201005.K?= =?UTF-8?q?=E6=AC=A1=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E7=BB=84=E5=92=8C=20js=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=AE=AD=E5=A4=B4=E5=87=BD=E6=95=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\226\347\232\204\346\225\260\347\273\204\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 571bc2ac51..9759702ae2 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -189,9 +189,9 @@ var largestSumAfterKNegations = function(nums, k) { nums[nums.length-1] = - nums[nums.length-1] k--; } - return nums.reduce((a, b) => { - a + b - }) + + // 使用箭头函数的隐式返回值时,需使用简写省略花括号,否则要在 a + b 前加上 return + return nums.reduce((a, b) => a + b) }; // 版本二 (优化: 一次遍历) From 4a6e87f2f02c3d023c7ae2de7113abe808dd9a96 Mon Sep 17 00:00:00 2001 From: August <549392485@qq.com> Date: Sun, 28 May 2023 23:57:29 +0800 Subject: [PATCH 0374/1533] =?UTF-8?q?Update=200034.=E5=9C=A8=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index 7e58a870d8..e5266cd9ea 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -240,7 +240,7 @@ class Solution { while (left - 1 >= 0 && nums[left - 1] == nums[index]) { // 防止数组越界。逻辑短路,两个条件顺序不能换 left--; } - // 向左滑动,找右边界 + // 向右滑动,找右边界 while (right + 1 < nums.length && nums[right + 1] == nums[index]) { // 防止数组越界。 right++; } From 11581df17788327ea3114c1f98d332af2cd0d260 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 28 May 2023 17:58:11 -0500 Subject: [PATCH 0375/1533] =?UTF-8?q?Update=200455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\217\221\351\245\274\345\271\262.md" | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index efadb43309..f32af57b58 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -177,32 +177,33 @@ class Solution { ``` ### Python - +贪心 大饼干优先 ```python class Solution: - # 思路1:优先考虑小胃口 - def findContentChildren(self, g: List[int], s: List[int]) -> int: - g.sort() - s.sort() - res = 0 - for i in range(len(s)): - if res = g[res]: #小饼干先喂饱小胃口 - res += 1 - return res -``` + def findContentChildren(self, g, s): + g.sort() # 将孩子的贪心因子排序 + s.sort() # 将饼干的尺寸排序 + index = len(s) - 1 # 饼干数组的下标,从最后一个饼干开始 + result = 0 # 满足孩子的数量 + for i in range(len(g)-1, -1, -1): # 遍历胃口,从最后一个孩子开始 + if index >= 0 and s[index] >= g[i]: # 遍历饼干 + result += 1 + index -= 1 + return result +``` +贪心 小饼干优先 ```python class Solution: - # 思路2:优先考虑大胃口 - def findContentChildren(self, g: List[int], s: List[int]) -> int: - g.sort() - s.sort() - start, count = len(s) - 1, 0 - for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口 - if start >= 0 and g[index] <= s[start]: - start -= 1 - count += 1 - return count + def findContentChildren(self, g, s): + g.sort() # 将孩子的贪心因子排序 + s.sort() # 将饼干的尺寸排序 + index = 0 + for i in range(len(s)): # 遍历饼干 + if index < len(g) and g[index] <= s[i]: # 如果当前孩子的贪心因子小于等于当前饼干尺寸 + index += 1 # 满足一个孩子,指向下一个孩子 + return index # 返回满足的孩子数目 + ``` ### Go From 18a406e14fd4f5a0d4b5c1a1e3f37410ff5e3fce Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 28 May 2023 18:45:13 -0500 Subject: [PATCH 0376/1533] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\212\250\345\272\217\345\210\227.md" | 87 ++++++++++++++----- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index ff388b5579..285a0280a5 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -305,21 +305,43 @@ class Solution { ### Python -**贪心** +贪心(版本一) + +```python +class Solution: + def wiggleMaxLength(self, nums): + if len(nums) <= 1: + return len(nums) # 如果数组长度为0或1,则返回数组长度 + curDiff = 0 # 当前一对元素的差值 + preDiff = 0 # 前一对元素的差值 + result = 1 # 记录峰值的个数,初始为1(默认最右边的元素被视为峰值) + for i in range(len(nums) - 1): + curDiff = nums[i + 1] - nums[i] # 计算下一个元素与当前元素的差值 + # 如果遇到一个峰值 + if (preDiff <= 0 and curDiff > 0) or (preDiff >= 0 and curDiff < 0): + result += 1 # 峰值个数加1 + preDiff = curDiff # 注意这里,只在摆动变化的时候更新preDiff + return result # 返回最长摆动子序列的长度 + +``` +贪心(版本二) ```python class Solution: def wiggleMaxLength(self, nums: List[int]) -> int: - preC,curC,res = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度 + if len(nums) <= 1: + return len(nums) # 如果数组长度为0或1,则返回数组长度 + preDiff,curDiff ,result = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度 for i in range(len(nums) - 1): - curC = nums[i + 1] - nums[i] - if curC * preC <= 0 and curC !=0: #差值为0时,不算摆动 - res += 1 - preC = curC #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值 - return res + curDiff = nums[i + 1] - nums[i] + if curDiff * preDiff <= 0 and curDiff !=0: #差值为0时,不算摆动 + result += 1 + preDiff = curDiff #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值 + return result + ``` -**动态规划** +动态规划(版本一) ```python class Solution: @@ -341,25 +363,44 @@ class Solution: return max(dp[-1][0], dp[-1][1]) ``` +动态规划(版本二) + ```python class Solution: - def wiggleMaxLength(self, nums: List[int]) -> int: - # up i作为波峰最长的序列长度 - # down i作为波谷最长的序列长度 - n = len(nums) - # 长度为0和1的直接返回长度 - if n<2: return n - for i in range(1,n): - if nums[i]>nums[i-1]: - # nums[i] 为波峰,1. 前面是波峰,up值不变,2. 前面是波谷,down值加1 - # 目前up值取两者的较大值(其实down+1即可,可以推理前一步down和up最多相差1,所以down+1>=up) - up = max(up, down+1) - elif nums[i] nums[i]: + dp[i][1] = max(dp[i][1], dp[j][0] + 1) # 如果前一个数比当前数大,可以形成一个上升峰值,更新dp[i][1] + for j in range(i): + if nums[j] < nums[i]: + dp[i][0] = max(dp[i][0], dp[j][1] + 1) # 如果前一个数比当前数小,可以形成一个下降峰值,更新dp[i][0] + return max(dp[-1][0], dp[-1][1]) # 返回最大的摆动序列长度 + ``` +动态规划(版本三)优化 + +```python +class Solution: + def wiggleMaxLength(self, nums): + if len(nums) <= 1: + return len(nums) # 如果数组长度为0或1,则返回数组长度 + + up = down = 1 # 记录上升和下降摆动序列的最大长度 + for i in range(1, len(nums)): + if nums[i] > nums[i-1]: + up = down + 1 # 如果当前数比前一个数大,则可以形成一个上升峰值 + elif nums[i] < nums[i-1]: + down = up + 1 # 如果当前数比前一个数小,则可以形成一个下降峰值 + + return max(up, down) # 返回上升和下降摆动序列的最大长度 + + +``` ### Go **贪心** From b2812eb707d9e2c5382781b23d5add54e8e1db63 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 29 May 2023 19:02:51 +0800 Subject: [PATCH 0377/1533] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=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 0378/1533] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=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 0379/1533] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=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] +} +``` + +

From 2a61ec45e6366c35189ead2b08914104c1df672e Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Mon, 29 May 2023 17:38:31 -0500 Subject: [PATCH 0380/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0047.\345\205\250\346\216\222\345\210\227II.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index b1908fb491..a1036b37ea 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -98,6 +98,8 @@ public: } }; +// 时间复杂度: 最差情况所有元素都是唯一的。复杂度和全排列1都是 O(n! * n) 对于 n 个元素一共有 n! 中排列方案。而对于每一个答案,我们需要 O(n) 去复制最终放到 result 数组 +// 空间复杂度: O(n) 回溯树的深度取决于我们有多少个元素 ``` ## 拓展 From 83a87ab5ad2f1dd0f81f3e779921a46c15d979f8 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 30 May 2023 16:28:09 +0800 Subject: [PATCH 0381/1533] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0.md=20about=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\271\263\346\226\271\346\225\260.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index f5b23d2636..d573695181 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -345,6 +345,29 @@ function numSquares(n: number): number { }; ``` +Rust: + +```rust +// 先遍历背包 +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![i32::MAX; n + 1]; + dp[0] = 0; + for i in 0..=n { + let mut j = 1; + loop { + match j * j > i { + true => break, + false => dp[i] = dp[i].min(dp[i - j * j] + 1), + } + j += 1; + } + } + dp[n] + } +} +```

From a2ff242303546243f606e717e34ff8d2965eeb9d Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 30 May 2023 18:30:14 +0800 Subject: [PATCH 0382/1533] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\271\263\346\226\271\346\225\260.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index d573695181..1a0b55c02a 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -369,6 +369,27 @@ impl Solution { } ``` +```rust +// 先遍历物品 +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let (n, mut goods) = (n as usize, 1); + let mut dp = vec![i32::MAX; n + 1]; + dp[0] = 0; + loop { + if goods * goods > n { + break; + } + for j in goods * goods..=n { + dp[j] = dp[j].min(dp[j - goods * goods] + 1); + } + goods += 1; + } + dp[n] + } +} +``` +

From 46495a7753ebf1eb603891229375e06fc1f05a07 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 30 May 2023 21:21:02 +0800 Subject: [PATCH 0383/1533] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...05\250\345\271\263\346\226\271\346\225\260.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index 1a0b55c02a..71ab190637 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -331,6 +331,7 @@ var numSquares2 = function(n) { TypeScript: ```typescript +// 先遍历物品 function numSquares(n: number): number { const goodsNum: number = Math.floor(Math.sqrt(n)); const dp: number[] = new Array(n + 1).fill(Infinity); @@ -345,6 +346,20 @@ function numSquares(n: number): number { }; ``` +```rust +// 先遍历背包 +function numSquares(n: number): number { + const dp = Array(n + 1).fill(Infinity) + dp[0] = 0; + for(let i = 1; i <= n; i++){ + for(let j = 1; j * j <= i; j++){ + dp[i] = Math.min(dp[i], dp[i -j * j] + 1) + } + } + return dp[n] +}; +``` + Rust: ```rust From 3e18a30e36375046900278f7f09dc3ed53aa766f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 30 May 2023 22:15:23 +0800 Subject: [PATCH 0384/1533] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8B=86=E5=88=86.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\350\257\215\346\213\206\345\210\206.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 230942ef02..402ce132cf 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -464,7 +464,24 @@ function wordBreak(s: string, wordDict: string[]): boolean { }; ``` +Rust: +```rust +impl Solution { + pub fn word_break(s: String, word_dict: Vec) -> bool { + let mut dp = vec![false; s.len() + 1]; + dp[0] = true; + for i in 1..=s.len() { + for j in 0..i { + if word_dict.iter().any(|word| *word == s[j..i]) && dp[j] { + dp[i] = true; + } + } + } + dp[s.len()] + } +} +```

From 08823a26309c0bf87ecb90f680694f87255513bd Mon Sep 17 00:00:00 2001 From: rosethorn999 <24700793+rosethorn999@users.noreply.github.com> Date: Tue, 30 May 2023 12:44:16 -0700 Subject: [PATCH 0385/1533] chore: Missing language name --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index c7469d65bf..e74cdf71fd 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -146,6 +146,7 @@ class Solution { ``` +Python: (版本一)使用数组 ```python class Solution: @@ -210,7 +211,6 @@ class Solution: class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) - ``` Go: From be926a3c5b3e67023cf99f4f0311d091c01a7e08 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Wed, 31 May 2023 21:43:25 +0800 Subject: [PATCH 0386/1533] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\223\345\256\266\345\212\253\350\210\215.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 6e682ec39e..1ecd2eb716 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -250,7 +250,24 @@ function rob(nums: number[]): number { }; ``` +Rust: +```rust +impl Solution { + pub fn rob(nums: Vec) -> i32 { + if nums.len() == 1 { + return nums[0]; + } + let mut dp = vec![0; nums.len()]; + dp[0] = nums[0]; + dp[1] = nums[0].max(nums[1]); + for i in 2..nums.len() { + dp[i] = (dp[i - 2] + nums[i]).max(dp[i - 1]); + } + dp[nums.len() - 1] + } +} +```

From f22c658d365595a080ff5b516a3f2fca775e0ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Wed, 31 May 2023 22:50:48 +0800 Subject: [PATCH 0387/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\234\211\350\267\257\345\276\204.md" | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 7bd56fbd0d..061535072d 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -390,6 +390,8 @@ public: ```Java //解法一 + +//方式一 class Solution { /** * 递归法 @@ -428,9 +430,32 @@ class Solution { } } } + +//方式二 +class Solution { + + List result = new ArrayList<>(); + + public List binaryTreePaths(TreeNode root) { + deal(root, ""); + return result; + } + + public void deal(TreeNode node, String s) { + if (node == null) + return; + if (node.left == null && node.right == null) { + result.add(new StringBuilder(s).append(node.val).toString()); + return; + } + String tmp = new StringBuilder(s).append(node.val).append("->").toString(); + deal(node.left, tmp); + deal(node.right, tmp); + } +} ``` ```java -// 解法2 +// 解法二 class Solution { /** * 迭代法 From d9887223965e84771503808e5685c1307998fabe Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 31 May 2023 23:44:40 -0500 Subject: [PATCH 0388/1533] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\345\255\220\345\272\217\345\222\214.md" | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 48f8be29c1..bc5e1d55b6 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -198,21 +198,35 @@ class Solution { ``` ### Python +暴力法 +```python +class Solution: + def maxSubArray(self, nums): + result = float('-inf') # 初始化结果为负无穷大 + count = 0 + for i in range(len(nums)): # 设置起始位置 + count = 0 + for j in range(i, len(nums)): # 从起始位置i开始遍历寻找最大值 + count += nums[j] + result = max(count, result) # 更新最大值 + return result +``` ```python class Solution: - def maxSubArray(self, nums: List[int]) -> int: - result = -float('inf') + def maxSubArray(self, nums): + result = float('-inf') # 初始化结果为负无穷大 count = 0 for i in range(len(nums)): count += nums[i] - if count > result: + if count > result: # 取区间累计的最大值(相当于不断确定最大子序终止位置) result = count - if count <= 0: + if count <= 0: # 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和 count = 0 return result -``` + +``` ### Go ```go From d65f7a32da277ab0020c3104c6cc3f51b0e51998 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 00:41:29 -0500 Subject: [PATCH 0389/1533] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\203\346\270\270\346\210\217II.md" | 101 ++++++++++-------- 1 file changed, 58 insertions(+), 43 deletions(-) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 8a9395824c..2f0349b2cc 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -205,66 +205,81 @@ class Solution { ``` ### Python - +贪心(版本一) ```python class Solution: - def jump(self, nums: List[int]) -> int: - if len(nums) == 1: return 0 - ans = 0 - curDistance = 0 - nextDistance = 0 + def jump(self, nums): + if len(nums) == 1: + return 0 + + cur_distance = 0 # 当前覆盖最远距离下标 + ans = 0 # 记录走的最大步数 + next_distance = 0 # 下一步覆盖最远距离下标 + for i in range(len(nums)): - nextDistance = max(i + nums[i], nextDistance) - if i == curDistance: - if curDistance != len(nums) - 1: - ans += 1 - curDistance = nextDistance - if nextDistance >= len(nums) - 1: break + next_distance = max(nums[i] + i, next_distance) # 更新下一步覆盖最远距离下标 + if i == cur_distance: # 遇到当前覆盖最远距离下标 + ans += 1 # 需要走下一步 + cur_distance = next_distance # 更新当前覆盖最远距离下标(相当于加油了) + if next_distance >= len(nums) - 1: # 当前覆盖最远距离达到数组末尾,不用再做ans++操作,直接结束 + break + return ans + ``` +贪心(版本二) ```python -# 贪心版本二 class Solution: - def jump(self, nums: List[int]) -> int: - if len(nums) == 1: - return 0 - curDistance, nextDistance = 0, 0 - step = 0 - for i in range(len(nums)-1): - nextDistance = max(nextDistance, nums[i]+i) - if i == curDistance: - curDistance = nextDistance - step += 1 - return step + def jump(self, nums): + cur_distance = 0 # 当前覆盖的最远距离下标 + ans = 0 # 记录走的最大步数 + next_distance = 0 # 下一步覆盖的最远距离下标 + + for i in range(len(nums) - 1): # 注意这里是小于len(nums) - 1,这是关键所在 + next_distance = max(nums[i] + i, next_distance) # 更新下一步覆盖的最远距离下标 + if i == cur_distance: # 遇到当前覆盖的最远距离下标 + cur_distance = next_distance # 更新当前覆盖的最远距离下标 + ans += 1 + + return ans + ``` +贪心(版本三) 类似‘55-跳跃游戏’写法 + ```python -# 贪心版本三 - 类似‘55-跳跃游戏’写法 class Solution: def jump(self, nums) -> int: - if len(nums)==1: return 0 - i = 0 - count = 0 - cover = 0 - while i<=cover: - for i in range(i,cover+1): - cover = max(nums[i]+i,cover) - if cover>=len(nums)-1: return count+1 - count+=1 + if len(nums)==1: # 如果数组只有一个元素,不需要跳跃,步数为0 + return 0 -``` + i = 0 # 当前位置 + count = 0 # 步数计数器 + cover = 0 # 当前能够覆盖的最远距离 + + while i <= cover: # 当前位置小于等于当前能够覆盖的最远距离时循环 + for i in range(i, cover+1): # 遍历从当前位置到当前能够覆盖的最远距离之间的所有位置 + cover = max(nums[i]+i, cover) # 更新当前能够覆盖的最远距离 + if cover >= len(nums)-1: # 如果当前能够覆盖的最远距离达到或超过数组的最后一个位置,直接返回步数+1 + return count+1 + count += 1 # 每一轮遍历结束后,步数+1 + +``` +动态规划 ```python -# 动态规划做法 class Solution: def jump(self, nums: List[int]) -> int: - result = [10**4+1]*len(nums) - result[0]=0 - for i in range(len(nums)): - for j in range(nums[i]+1): - if i+j Date: Thu, 1 Jun 2023 00:45:16 -0500 Subject: [PATCH 0390/1533] =?UTF-8?q?Update=201005.K=E6=AC=A1=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204\346\225\260\347\273\204\345\222\214.md" | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 27a575c717..343bef04d9 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -155,17 +155,23 @@ class Solution { ``` ### Python +贪心 ```python class Solution: def largestSumAfterKNegations(self, A: List[int], K: int) -> int: - A = sorted(A, key=abs, reverse=True) # 将A按绝对值从大到小排列 - for i in range(len(A)): - if K > 0 and A[i] < 0: + A.sort(key=lambda x: abs(x), reverse=True) # 第一步:按照绝对值降序排序数组A + + for i in range(len(A)): # 第二步:执行K次取反操作 + if A[i] < 0 and K > 0: A[i] *= -1 K -= 1 - if K > 0: - A[-1] *= (-1)**K #取A最后一个数只需要写-1 - return sum(A) + + if K % 2 == 1: # 第三步:如果K还有剩余次数,将绝对值最小的元素取反 + A[-1] *= -1 + + result = sum(A) # 第四步:计算数组A的元素和 + return result + ``` ### Go From 648b0d2edc8cbf6c46b673f54ef610ba0524d5e3 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 01:03:49 -0500 Subject: [PATCH 0391/1533] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9?= =?UTF-8?q?=E7=AB=99.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\345\212\240\346\262\271\347\253\231.md" | 76 +++++++++++++------ 1 file changed, 53 insertions(+), 23 deletions(-) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index f432bf0b1f..ad9acfbc80 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -249,44 +249,74 @@ class Solution { ``` ### Python +暴力法 +```python + +class Solution: + def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: + for i in range(len(cost)): + rest = gas[i] - cost[i] # 记录剩余油量 + index = (i + 1) % len(cost) # 下一个加油站的索引 + + while rest > 0 and index != i: # 模拟以i为起点行驶一圈(如果有rest==0,那么答案就不唯一了) + rest += gas[index] - cost[index] # 更新剩余油量 + index = (index + 1) % len(cost) # 更新下一个加油站的索引 + + if rest >= 0 and index == i: # 如果以i为起点跑一圈,剩余油量>=0,并且回到起始位置 + return i # 返回起始位置i + + return -1 # 所有起始位置都无法环绕一圈,返回-1 + +``` +贪心(版本一) ```python -# 解法1 class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: - n = len(gas) - cur_sum = 0 - min_sum = float('inf') + curSum = 0 # 当前累计的剩余油量 + minFuel = float('inf') # 从起点出发,油箱里的油量最小值 - for i in range(n): - cur_sum += gas[i] - cost[i] - min_sum = min(min_sum, cur_sum) + for i in range(len(gas)): + rest = gas[i] - cost[i] + curSum += rest + if curSum < minFuel: + minFuel = curSum - if cur_sum < 0: return -1 - if min_sum >= 0: return 0 + if curSum < 0: + return -1 # 情况1:整个行程的总消耗大于总供给,无法完成一圈 - for j in range(n - 1, 0, -1): - min_sum += gas[j] - cost[j] - if min_sum >= 0: - return j + if minFuel >= 0: + return 0 # 情况2:从起点出发到任何一个加油站时油箱的剩余油量都不会小于0,可以从起点出发完成一圈 - return -1 -``` + for i in range(len(gas) - 1, -1, -1): + rest = gas[i] - cost[i] + minFuel += rest + if minFuel >= 0: + return i # 情况3:找到一个位置使得从该位置出发油箱的剩余油量不会小于0,返回该位置的索引 + + return -1 # 无法完成一圈 +``` +贪心(版本二) ```python -# 解法2 class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: - start = 0 - curSum = 0 - totalSum = 0 + curSum = 0 # 当前累计的剩余油量 + totalSum = 0 # 总剩余油量 + start = 0 # 起始位置 + for i in range(len(gas)): curSum += gas[i] - cost[i] totalSum += gas[i] - cost[i] - if curSum < 0: - curSum = 0 - start = i + 1 - if totalSum < 0: return -1 + + if curSum < 0: # 当前累计剩余油量curSum小于0 + start = i + 1 # 起始位置更新为i+1 + curSum = 0 # curSum重新从0开始累计 + + if totalSum < 0: + return -1 # 总剩余油量totalSum小于0,说明无法环绕一圈 return start + + ``` ### Go From cfb10d2e2a24324a319fec0986dee0ef465394fc Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 01:35:04 -0500 Subject: [PATCH 0392/1533] =?UTF-8?q?Update=200135.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E7=B3=96=E6=9E=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\206\345\217\221\347\263\226\346\236\234.md" | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index 1ba1563fb3..cf3ccc8e90 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -178,13 +178,21 @@ class Solution { class Solution: def candy(self, ratings: List[int]) -> int: candyVec = [1] * len(ratings) + + # 从前向后遍历,处理右侧比左侧评分高的情况 for i in range(1, len(ratings)): if ratings[i] > ratings[i - 1]: candyVec[i] = candyVec[i - 1] + 1 - for j in range(len(ratings) - 2, -1, -1): - if ratings[j] > ratings[j + 1]: - candyVec[j] = max(candyVec[j], candyVec[j + 1] + 1) - return sum(candyVec) + + # 从后向前遍历,处理左侧比右侧评分高的情况 + for i in range(len(ratings) - 2, -1, -1): + if ratings[i] > ratings[i + 1]: + candyVec[i] = max(candyVec[i], candyVec[i + 1] + 1) + + # 统计结果 + result = sum(candyVec) + return result + ``` ### Go From ba3c7ec19b6b2ad3f95f35ae55f25b2459d710b3 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 01:37:41 -0500 Subject: [PATCH 0393/1533] =?UTF-8?q?Update=200860.=E6=9F=A0=E6=AA=AC?= =?UTF-8?q?=E6=B0=B4=E6=89=BE=E9=9B=B6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\346\260\264\346\211\276\351\233\266.md" | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index d96e08797b..5046df121a 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -164,24 +164,39 @@ class Solution { ```python class Solution: def lemonadeChange(self, bills: List[int]) -> bool: - five, ten = 0, 0 + five = 0 + ten = 0 + twenty = 0 + for bill in bills: + # 情况一:收到5美元 if bill == 5: five += 1 - elif bill == 10: - if five < 1: return False - five -= 1 + + # 情况二:收到10美元 + if bill == 10: + if five <= 0: + return False ten += 1 - else: - if ten > 0 and five > 0: - ten -= 1 + five -= 1 + + # 情况三:收到20美元 + if bill == 20: + # 先尝试使用10美元和5美元找零 + if five > 0 and ten > 0: five -= 1 - elif five > 2: + ten -= 1 + #twenty += 1 + # 如果无法使用10美元找零,则尝试使用三张5美元找零 + elif five >= 3: five -= 3 + #twenty += 1 else: return False + return True + ``` ### Go From cd4d54616200a53d9a22fbd919edf905ad1d4dd1 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 02:16:29 -0500 Subject: [PATCH 0394/1533] =?UTF-8?q?Update=200435.=E6=97=A0=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\217\240\345\214\272\351\227\264.md" | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 37ef819d5b..61f42d4241 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -248,20 +248,45 @@ class Solution { ``` ### Python +贪心 基于左边界 ```python class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: - if len(intervals) == 0: return 0 - intervals.sort(key=lambda x: x[1]) - count = 1 # 记录非交叉区间的个数 - end = intervals[0][1] # 记录区间分割点 + if not intervals: + return 0 + + intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序 + count = 0 # 记录重叠区间数量 + for i in range(1, len(intervals)): - if end <= intervals[i][0]: + if intervals[i][0] < intervals[i - 1][1]: # 存在重叠区间 + intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界 count += 1 - end = intervals[i][1] - return len(intervals) - count + + return count + ``` +贪心 基于左边界 把452.用最少数量的箭引爆气球代码稍做修改 +```python +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + if not intervals: + return 0 + + intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序 + + result = 1 # 不重叠区间数量,初始化为1,因为至少有一个不重叠的区间 + + for i in range(1, len(intervals)): + if intervals[i][0] >= intervals[i - 1][1]: # 没有重叠 + result += 1 + else: # 重叠情况 + intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界 + + return len(intervals) - result + +``` ### Go ```go func eraseOverlapIntervals(intervals [][]int) int { From 458f94b8b94b6e9252dc23dc11f6627a4272e2b8 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 02:26:34 -0500 Subject: [PATCH 0395/1533] =?UTF-8?q?Update=200763.=E5=88=92=E5=88=86?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\345\214\272\351\227\264.md" | 111 +++++++----------- 1 file changed, 42 insertions(+), 69 deletions(-) diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index fbcafdc8cb..158f76d625 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -231,83 +231,56 @@ class Solution{ ``` ### Python +贪心(版本一) ```python class Solution: def partitionLabels(self, s: str) -> List[int]: - hash = [0] * 26 - for i in range(len(s)): - hash[ord(s[i]) - ord('a')] = i + last_occurrence = {} # 存储每个字符最后出现的位置 + for i, ch in enumerate(s): + last_occurrence[ch] = i + result = [] - left = 0 - right = 0 - for i in range(len(s)): - right = max(right, hash[ord(s[i]) - ord('a')]) - if i == right: - result.append(right - left + 1) - left = i + 1 - return result + start = 0 + end = 0 + for i, ch in enumerate(s): + end = max(end, last_occurrence[ch]) # 找到当前字符出现的最远位置 + if i == end: # 如果当前位置是最远位置,表示可以分割出一个区间 + result.append(end - start + 1) + start = i + 1 -# 解法二(不相交区间法) + return result + +``` +贪心(版本二)与452.用最少数量的箭引爆气球 (opens new window)、435.无重叠区间 (opens new window)相同的思路。 +```python class Solution: - def partitionLabels(self, s: str) -> List[int]: - # 记录每个字母出现的区间 - def getBord(s): - hash = [[-float('inf')] * 2 for _ in range(26)] - for i in range(len(s)): - if hash[ord(s[i]) - ord('a')][0] == -float('inf'): - hash[ord(s[i]) - ord('a')][0] = i - hash[ord(s[i]) - ord('a')][1] = i - # 去除字符串中未出现的字母所占用区间 - hash_filter = [] - for item in hash: - if item[0] != -float('inf'): hash_filter.append(item) - return hash_filter - - # 得到无重叠区间题意中的输入样例格式:区间列表 - hash = getBord(s) - # 按照左边界从小到大排序 - hash.sort(key= lambda x: x[0]) - res = [] - left = 0 - # 记录最大右边界 - right = hash[0][1] + def countLabels(self, s): + # 初始化一个长度为26的区间列表,初始值为负无穷 + hash = [[float('-inf'), float('-inf')] for _ in range(26)] + hash_filter = [] + for i in range(len(s)): + if hash[ord(s[i]) - ord('a')][0] == float('-inf'): + hash[ord(s[i]) - ord('a')][0] = i + hash[ord(s[i]) - ord('a')][1] = i for i in range(len(hash)): - # 一旦下一区间左边界大于当前右边界,即可认为出现分割点 - if hash[i][0] > right: - res.append(right - left + 1) - left = hash[i][0] - # 实时更新最大右边界 - right = max(right, hash[i][1]) - # 最右侧区间(字符串长度为1时的特殊情况也包含于其中) - res.append(right - left + 1) + if hash[i][0] != float('-inf'): + hash_filter.append(hash[i]) + return hash_filter + + def partitionLabels(self, s): + res = [] + hash = self.countLabels(s) + hash.sort(key=lambda x: x[0]) # 按左边界从小到大排序 + rightBoard = hash[0][1] # 记录最大右边界 + leftBoard = 0 + for i in range(1, len(hash)): + if hash[i][0] > rightBoard: # 出现分割点 + res.append(rightBoard - leftBoard + 1) + leftBoard = hash[i][0] + rightBoard = max(rightBoard, hash[i][1]) + res.append(rightBoard - leftBoard + 1) # 最右端 return res - -# 解法三:区间合并法 (结合下一题 56. Merge Intervals 的写法) -class Solution: # - def partitionLabels(self, s: str) -> List[int]: - aaa = list(set(s)) - #aaa.sort() - bbb = list(s) - ccc = [] - for i in reversed(bbb): - ccc.append(i) - intervals = [] - for i in range(len(aaa)): - intervals.append([bbb.index(aaa[i]),len(bbb)-ccc.index(aaa[i])-1]) - # 先求出各个字母的存在区间,之后利用区间合并方法得出所有不相邻的最大区间。 - intervals.sort(key = lambda x:x[0]) - newinterval = [] - left, right = intervals[0][0], intervals[0][1] - for i in range(1,len(intervals)): - if intervals[i][0] in range(left, right+1): - right = max(intervals[i][1],intervals[i-1][1],right) - left = min(intervals[i-1][0],left) - else: - newinterval.append(right-left+1) - left = intervals[i][0] - right = intervals[i][1] - newinterval.append(right-left+1) - return newinterval + ``` ### Go From 352b51d550a6edcc9b6355b8773b91d4f69cb507 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 02:36:11 -0500 Subject: [PATCH 0396/1533] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\345\271\266\345\214\272\351\227\264.md" | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 952d32afc2..ead2fb7587 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -141,18 +141,24 @@ class Solution { ### Python ```python class Solution: - def merge(self, intervals: List[List[int]]) -> List[List[int]]: - if len(intervals) == 0: return intervals - intervals.sort(key=lambda x: x[0]) + def merge(self, intervals): result = [] - result.append(intervals[0]) + if len(intervals) == 0: + return result # 区间集合为空直接返回 + + intervals.sort(key=lambda x: x[0]) # 按照区间的左边界进行排序 + + result.append(intervals[0]) # 第一个区间可以直接放入结果集中 + for i in range(1, len(intervals)): - last = result[-1] - if last[1] >= intervals[i][0]: - result[-1] = [last[0], max(last[1], intervals[i][1])] + if result[-1][1] >= intervals[i][0]: # 发现重叠区间 + # 合并区间,只需要更新结果集最后一个区间的右边界,因为根据排序,左边界已经是最小的 + result[-1][1] = max(result[-1][1], intervals[i][1]) else: - result.append(intervals[i]) + result.append(intervals[i]) # 区间不重叠 + return result + ``` ### Go From 41dd31d826218bef8b25890b9dc31744fc3b5dd7 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 02:54:22 -0500 Subject: [PATCH 0397/1533] =?UTF-8?q?Update=200738.=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\347\232\204\346\225\260\345\255\227.md" | 107 ++++++++++++++++-- 1 file changed, 100 insertions(+), 7 deletions(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index 1cf8a0a61e..d88ebbb096 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -164,17 +164,110 @@ class Solution { ### Python: +暴力 ```python class Solution: - def monotoneIncreasingDigits(self, n: int) -> int: - a = list(str(n)) - for i in range(len(a)-1,0,-1): - if int(a[i]) < int(a[i-1]): - a[i-1] = str(int(a[i-1]) - 1) - a[i:] = '9' * (len(a) - i) #python不需要设置flag值,直接按长度给9就好了 - return int("".join(a)) + def checkNum(self, num): + max_digit = 10 + while num: + digit = num % 10 + if max_digit >= digit: + max_digit = digit + else: + return False + num //= 10 + return True + + def monotoneIncreasingDigits(self, N): + for i in range(N, 0, -1): + if self.checkNum(i): + return i + return 0 + +``` +贪心(版本一) +```python +class Solution: + def monotoneIncreasingDigits(self, N: int) -> int: + # 将整数转换为字符串 + strNum = str(N) + # flag用来标记赋值9从哪里开始 + # 设置为字符串长度,为了防止第二个for循环在flag没有被赋值的情况下执行 + flag = len(strNum) + + # 从右往左遍历字符串 + for i in range(len(strNum) - 1, 0, -1): + # 如果当前字符比前一个字符小,说明需要修改前一个字符 + if strNum[i - 1] > strNum[i]: + flag = i # 更新flag的值,记录需要修改的位置 + # 将前一个字符减1,以保证递增性质 + strNum = strNum[:i - 1] + str(int(strNum[i - 1]) - 1) + strNum[i:] + + # 将flag位置及之后的字符都修改为9,以保证最大的递增数字 + for i in range(flag, len(strNum)): + strNum = strNum[:i] + '9' + strNum[i + 1:] + + # 将最终的字符串转换回整数并返回 + return int(strNum) + +``` +贪心(版本二) +```python +class Solution: + def monotoneIncreasingDigits(self, N: int) -> int: + # 将整数转换为字符串 + strNum = list(str(N)) + + # 从右往左遍历字符串 + for i in range(len(strNum) - 1, 0, -1): + # 如果当前字符比前一个字符小,说明需要修改前一个字符 + if strNum[i - 1] > strNum[i]: + strNum[i - 1] = str(int(strNum[i - 1]) - 1) # 将前一个字符减1 + # 将修改位置后面的字符都设置为9,因为修改前一个字符可能破坏了递增性质 + for j in range(i, len(strNum)): + strNum[j] = '9' + + # 将列表转换为字符串,并将字符串转换为整数并返回 + return int(''.join(strNum)) + + ``` +贪心(版本三) +```python +class Solution: + def monotoneIncreasingDigits(self, N: int) -> int: + # 将整数转换为字符串 + strNum = list(str(N)) + + # 从右往左遍历字符串 + for i in range(len(strNum) - 1, 0, -1): + # 如果当前字符比前一个字符小,说明需要修改前一个字符 + if strNum[i - 1] > strNum[i]: + strNum[i - 1] = str(int(strNum[i - 1]) - 1) # 将前一个字符减1 + # 将修改位置后面的字符都设置为9,因为修改前一个字符可能破坏了递增性质 + strNum[i:] = '9' * (len(strNum) - i) + + # 将列表转换为字符串,并将字符串转换为整数并返回 + return int(''.join(strNum)) + +``` +贪心(版本四)精简 + +```python +class Solution: + def monotoneIncreasingDigits(self, N: int) -> int: + strNum = str(N) + for i in range(len(strNum) - 1, 0, -1): + # 如果当前字符比前一个字符小,说明需要修改前一个字符 + if strNum[i - 1] > strNum[i]: + # 将前一个字符减1,以保证递增性质 + # 使用字符串切片操作将修改后的前面部分与后面部分进行拼接 + strNum = strNum[:i - 1] + str(int(strNum[i - 1]) - 1) + '9' * (len(strNum) - i) + return int(strNum) + + +``` ### Go ```go func monotoneIncreasingDigits(N int) int { From 5b4249d4a853a77e9aed0af171204c9b8d0ee2ac Mon Sep 17 00:00:00 2001 From: liuyiwei <709224218@qq.com> Date: Thu, 1 Jun 2023 16:34:00 +0800 Subject: [PATCH 0398/1533] =?UTF-8?q?151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BD=BF=E7=94=A8=E4=BA=86=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E6=80=9D=E6=83=B3=E7=9A=84=20Go=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\347\232\204\345\215\225\350\257\215.md" | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 4474f1c613..6dd3cd4975 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -467,9 +467,57 @@ class Solution: return " ".join(words) ``` - Go: +版本一: + +```go +func reverseWords(s string) string { + b := []byte(s) + + // 移除前面、中间、后面存在的多余空格 + slow := 0 + for i := 0; i < len(b); i++ { + if b[i] != ' ' { + if slow != 0 { + b[slow] = ' ' + slow++ + } + for i < len(b) && b[i] != ' ' { // 复制逻辑 + b[slow] = b[i] + slow++ + i++ + } + } + } + b = b[0:slow] + + // 翻转整个字符串 + reverse(b) + // 翻转每个单词 + last := 0 + for i := 0; i <= len(b); i++ { + if i == len(b) || b[i] == ' ' { + reverse(b[last:i]) + last = i + 1 + } + } + return string(b) +} + +func reverse(b []byte) { + left := 0 + right := len(b) - 1 + for left < right { + b[left], b[right] = b[right], b[left] + left++ + right-- + } +} +``` + +版本二: + ```go import ( "fmt" From ed4543b30042ab127a261b660054e54aaaf3102f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 04:03:00 -0500 Subject: [PATCH 0399/1533] =?UTF-8?q?Update=200968.=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\214\345\217\211\346\240\221.md" | 128 ++++++++++++------ 1 file changed, 88 insertions(+), 40 deletions(-) diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index fdec56cafa..e2ba8ebffc 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -371,56 +371,104 @@ class Solution { ### Python - +贪心(版本一) ```python class Solution: - def minCameraCover(self, root: TreeNode) -> int: - # Greedy Algo: + # Greedy Algo: # 从下往上安装摄像头:跳过leaves这样安装数量最少,局部最优 -> 全局最优 # 先给leaves的父节点安装,然后每隔两层节点安装一个摄像头,直到Head # 0: 该节点未覆盖 # 1: 该节点有摄像头 # 2: 该节点有覆盖 + def minCameraCover(self, root: TreeNode) -> int: + # 定义递归函数 + result = [0] # 用于记录摄像头的安装数量 + if self.traversal(root, result) == 0: + result[0] += 1 + + return result[0] + + + def traversal(self, cur: TreeNode, result: List[int]) -> int: + if not cur: + return 2 + + left = self.traversal(cur.left, result) + right = self.traversal(cur.right, result) + + # 情况1: 左右节点都有覆盖 + if left == 2 and right == 2: + return 0 + + # 情况2: + # left == 0 && right == 0 左右节点无覆盖 + # left == 1 && right == 0 左节点有摄像头,右节点无覆盖 + # left == 0 && right == 1 左节点无覆盖,右节点有摄像头 + # left == 0 && right == 2 左节点无覆盖,右节点覆盖 + # left == 2 && right == 0 左节点覆盖,右节点无覆盖 + if left == 0 or right == 0: + result[0] += 1 + return 1 + + # 情况3: + # left == 1 && right == 2 左节点有摄像头,右节点有覆盖 + # left == 2 && right == 1 左节点有覆盖,右节点有摄像头 + # left == 1 && right == 1 左右节点都有摄像头 + if left == 1 or right == 1: + return 2 + - result = 0 - # 从下往上遍历:后序(左右中) - def traversal(curr: TreeNode) -> int: - nonlocal result - - if not curr: return 2 - left = traversal(curr.left) - right = traversal(curr.right) - - # Case 1: - # 左右节点都有覆盖 - if left == 2 and right == 2: - return 0 - - # Case 2: - # left == 0 && right == 0 左右节点无覆盖 - # left == 1 && right == 0 左节点有摄像头,右节点无覆盖 - # left == 0 && right == 1 左节点有无覆盖,右节点摄像头 - # left == 0 && right == 2 左节点无覆盖,右节点覆盖 - # left == 2 && right == 0 左节点覆盖,右节点无覆盖 - elif left == 0 or right == 0: - result += 1 - return 1 - - # Case 3: - # left == 1 && right == 2 左节点有摄像头,右节点有覆盖 - # left == 2 && right == 1 左节点有覆盖,右节点有摄像头 - # left == 1 && right == 1 左右节点都有摄像头 - elif left == 1 or right == 1: - return 2 - - # 其他情况前段代码均已覆盖 - - if traversal(root) == 0: - result += 1 - - return result ``` +贪心(版本二)利用elif精简代码 +```python +class Solution: + # Greedy Algo: + # 从下往上安装摄像头:跳过leaves这样安装数量最少,局部最优 -> 全局最优 + # 先给leaves的父节点安装,然后每隔两层节点安装一个摄像头,直到Head + # 0: 该节点未覆盖 + # 1: 该节点有摄像头 + # 2: 该节点有覆盖 + def minCameraCover(self, root: TreeNode) -> int: + # 定义递归函数 + result = [0] # 用于记录摄像头的安装数量 + if self.traversal(root, result) == 0: + result[0] += 1 + + return result[0] + + + def traversal(self, cur: TreeNode, result: List[int]) -> int: + if not cur: + return 2 + + left = self.traversal(cur.left, result) + right = self.traversal(cur.right, result) + + # 情况1: 左右节点都有覆盖 + if left == 2 and right == 2: + return 0 + + # 情况2: + # left == 0 && right == 0 左右节点无覆盖 + # left == 1 && right == 0 左节点有摄像头,右节点无覆盖 + # left == 0 && right == 1 左节点无覆盖,右节点有摄像头 + # left == 0 && right == 2 左节点无覆盖,右节点覆盖 + # left == 2 && right == 0 左节点覆盖,右节点无覆盖 + elif left == 0 or right == 0: + result[0] += 1 + return 1 + + # 情况3: + # left == 1 && right == 2 左节点有摄像头,右节点有覆盖 + # left == 2 && right == 1 左节点有覆盖,右节点有摄像头 + # left == 1 && right == 1 左右节点都有摄像头 + else: + return 2 + + + +``` ### Go ```go From 28cf557d7c7bcf208764e083e4550c411f7c2ba0 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 15:02:00 -0500 Subject: [PATCH 0400/1533] =?UTF-8?q?Update=200509.=E6=96=90=E6=B3=A2?= =?UTF-8?q?=E9=82=A3=E5=A5=91=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\351\202\243\345\245\221\346\225\260.md" | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 08175058bb..195b68acec 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -203,18 +203,9 @@ class Solution { ``` ### Python +动态规划(版本一) ```python -class Solution: - def fib(self, n: int) -> int: - if n < 2: - return n - a, b, c = 0, 1, 0 - for i in range(1, n): - c = a + b - a, b = b, c - return c -# 动态规划 (注释版。无修饰) class Solution: def fib(self, n: int) -> int: @@ -238,7 +229,24 @@ class Solution: # 返回答案 return dp[n] -# 递归实现 +``` +动态规划(版本二) +```python +class Solution: + def fib(self, n: int) -> int: + if n < 2: + return n + a, b, c = 0, 1, 0 + for i in range(1, n): + c = a + b + a, b = b, c + return c + + +``` +递归(版本一) +```python + class Solution: def fib(self, n: int) -> int: if n < 2: From 69361bba979b9bc359a8ddfd43fc98014801c47f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 15:14:15 -0500 Subject: [PATCH 0401/1533] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0.\347\210\254\346\245\274\346\242\257.md" | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index a06ee91e1d..64c7e403c0 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -251,32 +251,66 @@ class Solution { ``` ### Python - +动态规划(版本一) ```python # 空间复杂度为O(n)版本 class Solution: def climbStairs(self, n: int) -> int: - # dp[i] 为第 i 阶楼梯有多少种方法爬到楼顶 - dp = [0]*(n+1) - dp[0] = 1 + if n <= 1: + return n + + dp = [0] * (n + 1) dp[1] = 1 - for i in range(2, n+1): - dp[i] = dp[i-1] + dp[i-2] + dp[2] = 2 + + for i in range(3, n + 1): + dp[i] = dp[i - 1] + dp[i - 2] + return dp[n] -# 空间复杂度为O(1)版本 +``` +动态规划(版本二) +```python + +# 空间复杂度为O(3)版本 class Solution: def climbStairs(self, n: int) -> int: - dp = [0]*(n+1) - dp[0] = 1 + if n <= 1: + return n + + dp = [0] * 3 dp[1] = 1 - for i in range(2,n+1): - tmp = dp[0] + dp[1] - dp[0] = dp[1] - dp[1] = tmp - return dp[1] + dp[2] = 2 + + for i in range(3, n + 1): + total = dp[1] + dp[2] + dp[1] = dp[2] + dp[2] = total + + return dp[2] + ``` +动态规划(版本三) +```python +# 空间复杂度为O(1)版本 +class Solution: + def climbStairs(self, n: int) -> int: + if n <= 1: + return n + + prev1 = 1 + prev2 = 2 + + for i in range(3, n + 1): + total = prev1 + prev2 + prev1 = prev2 + prev2 = total + + return prev2 + + +``` ### Go ```Go func climbStairs(n int) int { From 8e7d9f579ca973454fdd11f6857f1bd29664db3a Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 15:19:27 -0500 Subject: [PATCH 0402/1533] =?UTF-8?q?Update=200509.=E6=96=90=E6=B3=A2?= =?UTF-8?q?=E9=82=A3=E5=A5=91=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\351\202\243\345\245\221\346\225\260.md" | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 195b68acec..cdbf860cba 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -230,17 +230,41 @@ class Solution: return dp[n] ``` -动态规划(版本二) +态规划(版本二) ```python + class Solution: def fib(self, n: int) -> int: - if n < 2: + if n <= 1: return n - a, b, c = 0, 1, 0 - for i in range(1, n): - c = a + b - a, b = b, c - return c + + dp = [0, 1] + + for i in range(2, n + 1): + total = dp[0] + dp[1] + dp[0] = dp[1] + dp[1] = total + + return dp[1] + + +``` +动态规划(版本三) +```python +class Solution: + def fib(self, n: int) -> int: + if n <= 1: + return n + + prev1, prev2 = 0, 1 + + for _ in range(2, n + 1): + curr = prev1 + prev2 + prev1, prev2 = prev2, curr + + return prev2 + + ``` From b5494585fbff9e9ccb489674410c1f60401a9456 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 15:19:56 -0500 Subject: [PATCH 0403/1533] =?UTF-8?q?Update=200509.=E6=96=90=E6=B3=A2?= =?UTF-8?q?=E9=82=A3=E5=A5=91=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index cdbf860cba..45a9cf6b57 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -230,7 +230,7 @@ class Solution: return dp[n] ``` -态规划(版本二) +动态规划(版本二) ```python class Solution: From 283b74478afc69b4f8d8e6e33a98dfa30d659372 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 16:03:35 -0500 Subject: [PATCH 0404/1533] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\210\254\346\245\274\346\242\257.md" | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index d7258d4576..97d065ed2b 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -245,26 +245,42 @@ class Solution { ``` ### Python + +动态规划(版本一) ```python -# 第一步不支付费用 + class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: - n = len(cost) - dp = [0]*(n+1) # 到达前两步费用为0 - for i in range(2, n+1): - dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]) - return dp[-1] + dp = [0] * (len(cost) + 1) + dp[0] = 0 # 初始值,表示从起点开始不需要花费体力 + dp[1] = 0 # 初始值,表示经过第一步不需要花费体力 + + for i in range(2, len(cost) + 1): + # 在第i步,可以选择从前一步(i-1)花费体力到达当前步,或者从前两步(i-2)花费体力到达当前步 + # 选择其中花费体力较小的路径,加上当前步的花费,更新dp数组 + dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + + return dp[len(cost)] # 返回到达楼顶的最小花费 + ``` +动态规划(版本二) + ```python -# 第一步支付费用 class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: - dp = [0] * (len(cost) + 1) - dp[0] = 0 - dp[1] = 0 + dp0 = 0 # 初始值,表示从起点开始不需要花费体力 + dp1 = 0 # 初始值,表示经过第一步不需要花费体力 + for i in range(2, len(cost) + 1): - dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i-2]) - return dp[len(cost)] + # 在第i步,可以选择从前一步(i-1)花费体力到达当前步,或者从前两步(i-2)花费体力到达当前步 + # 选择其中花费体力较小的路径,加上当前步的花费,得到当前步的最小花费 + dpi = min(dp1 + cost[i - 1], dp0 + cost[i - 2]) + + dp0 = dp1 # 更新dp0为前一步的值,即上一次循环中的dp1 + dp1 = dpi # 更新dp1为当前步的最小花费 + + return dp1 # 返回到达楼顶的最小花费 + ``` ### Go From 6128c57e151b55d433ee74135838c303a9694908 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:13:23 -0500 Subject: [PATCH 0405/1533] =?UTF-8?q?Update=200062.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\220\214\350\267\257\345\276\204.md" | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index bf4363692b..430dacdc70 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -287,17 +287,70 @@ public: ``` ### Python +递归 +```python +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + if m == 1 or n == 1: + return 1 + return self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1) +``` +动态规划(版本一) ```python -class Solution: # 动态规划 +class Solution: def uniquePaths(self, m: int, n: int) -> int: - dp = [[1 for i in range(n)] for j in range(m)] + # 创建一个二维列表用于存储唯一路径数 + dp = [[0] * n for _ in range(m)] + + # 设置第一行和第一列的基本情况 + for i in range(m): + dp[i][0] = 1 + for j in range(n): + dp[0][j] = 1 + + # 计算每个单元格的唯一路径数 for i in range(1, m): for j in range(1, n): - dp[i][j] = dp[i][j - 1] + dp[i - 1][j] + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + + # 返回右下角单元格的唯一路径数 return dp[m - 1][n - 1] + +``` +动态规划(版本二) +```python +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + # 创建一个一维列表用于存储每列的唯一路径数 + dp = [1] * n + + # 计算每个单元格的唯一路径数 + for j in range(1, m): + for i in range(1, n): + dp[i] += dp[i - 1] + + # 返回右下角单元格的唯一路径数 + return dp[n - 1] ``` +数论 +```python +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + numerator = 1 # 分子 + denominator = m - 1 # 分母 + count = m - 1 # 计数器,表示剩余需要计算的乘积项个数 + t = m + n - 2 # 初始乘积项 + while count > 0: + numerator *= t # 计算乘积项的分子部分 + t -= 1 # 递减乘积项 + while denominator != 0 and numerator % denominator == 0: + numerator //= denominator # 约简分子 + denominator -= 1 # 递减分母 + count -= 1 # 计数器减1,继续下一项的计算 + return numerator # 返回最终的唯一路径数 +``` ### Go ```Go From 906293efae7e1de646593c7ae560cab726b2662f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:48:22 -0500 Subject: [PATCH 0406/1533] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\214\350\267\257\345\276\204II.md" | 157 ++++++++++++------ 1 file changed, 109 insertions(+), 48 deletions(-) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 85130ab4e4..ac9cae75a7 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -271,69 +271,130 @@ class Solution { ### Python - +动态规划(版本一) ```python class Solution: - def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: - # 构造一个DP table - row = len(obstacleGrid) - col = len(obstacleGrid[0]) - dp = [[0 for _ in range(col)] for _ in range(row)] - dp[0][0] = 0 if obstacleGrid[0][0] == 1 else 1 - if dp[0][0] == 0: - return 0 # 如果第一个格子就是障碍,return 0 - # 第一行 - for i in range(1, col): - if obstacleGrid[0][i] == 1: - # 遇到障碍物时,直接退出循环,后面默认都是0 + def uniquePathsWithObstacles(self, obstacleGrid): + m = len(obstacleGrid) + n = len(obstacleGrid[0]) + if obstacleGrid[m - 1][n - 1] == 1 or obstacleGrid[0][0] == 1: + return 0 + dp = [[0] * n for _ in range(m)] + for i in range(m): + if obstacleGrid[i][0] == 0: # 遇到障碍物时,直接退出循环,后面默认都是0 + dp[i][0] = 1 + else: break - dp[0][i] = 1 - - # 第一列 - for i in range(1, row): - if obstacleGrid[i][0] == 1: - # 遇到障碍物时,直接退出循环,后面默认都是0 + for j in range(n): + if obstacleGrid[0][j] == 0: + dp[0][j] = 1 + else: break - dp[i][0] = 1 - # print(dp) - - for i in range(1, row): - for j in range(1, col): - if obstacleGrid[i][j] == 0: - dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - return dp[-1][-1] + for i in range(1, m): + for j in range(1, n): + if obstacleGrid[i][j] == 1: + continue + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + return dp[m - 1][n - 1] + ``` +动态规划(版本二) +```python +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid): + m = len(obstacleGrid) # 网格的行数 + n = len(obstacleGrid[0]) # 网格的列数 + + if obstacleGrid[m - 1][n - 1] == 1 or obstacleGrid[0][0] == 1: + # 如果起点或终点有障碍物,直接返回0 + return 0 + + dp = [[0] * n for _ in range(m)] # 创建一个二维列表用于存储路径数 + + # 设置起点的路径数为1 + dp[0][0] = 1 if obstacleGrid[0][0] == 0 else 0 + + # 计算第一列的路径数 + for i in range(1, m): + if obstacleGrid[i][0] == 0: + dp[i][0] = dp[i - 1][0] + + # 计算第一行的路径数 + for j in range(1, n): + if obstacleGrid[0][j] == 0: + dp[0][j] = dp[0][j - 1] + + # 计算其他位置的路径数 + for i in range(1, m): + for j in range(1, n): + if obstacleGrid[i][j] == 1: + continue + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + + return dp[m - 1][n - 1] # 返回终点的路径数 + + +``` +动态规划(版本三) ```python class Solution: - """ - 使用一维dp数组 - """ + def uniquePathsWithObstacles(self, obstacleGrid): + if obstacleGrid[0][0] == 1: + return 0 + + dp = [0] * len(obstacleGrid[0]) # 创建一个一维列表用于存储路径数 + + # 初始化第一行的路径数 + for j in range(len(dp)): + if obstacleGrid[0][j] == 1: + dp[j] = 0 + elif j == 0: + dp[j] = 1 + else: + dp[j] = dp[j - 1] + + # 计算其他行的路径数 + for i in range(1, len(obstacleGrid)): + for j in range(len(dp)): + if obstacleGrid[i][j] == 1: + dp[j] = 0 + elif j != 0: + dp[j] = dp[j] + dp[j - 1] + + return dp[-1] # 返回最后一个元素,即终点的路径数 - def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: - m, n = len(obstacleGrid), len(obstacleGrid[0]) +``` +动态规划(版本四) - # 初始化dp数组 - # 该数组缓存当前行 - curr = [0] * n +```python +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid): + if obstacleGrid[0][0] == 1: + return 0 + + m, n = len(obstacleGrid), len(obstacleGrid[0]) + + dp = [0] * n # 创建一个一维列表用于存储路径数 + + # 初始化第一行的路径数 for j in range(n): if obstacleGrid[0][j] == 1: break - curr[j] = 1 + dp[j] = 1 - for i in range(1, m): # 从第二行开始 - for j in range(n): # 从第一列开始,因为第一列可能有障碍物 - # 有障碍物处无法通行,状态就设成0 + # 计算其他行的路径数 + for i in range(1, m): + if obstacleGrid[i][0] == 1: + dp[0] = 0 + for j in range(1, n): if obstacleGrid[i][j] == 1: - curr[j] = 0 - elif j > 0: - # 等价于 - # dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - curr[j] = curr[j] + curr[j - 1] - # 隐含的状态更新 - # dp[i][0] = dp[i - 1][0] - - return curr[n - 1] + dp[j] = 0 + else: + dp[j] += dp[j - 1] + + return dp[-1] # 返回最后一个元素,即终点的路径数 + ``` From 32185d5f2d8b3873253da3b44cbb1fb2aaa86762 Mon Sep 17 00:00:00 2001 From: Lozakaka <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 1 Jun 2023 19:44:43 -0400 Subject: [PATCH 0407/1533] =?UTF-8?q?=E7=B7=A8=E8=BC=AFJAVA=E8=A7=A3?= =?UTF-8?q?=E7=AD=94=E7=9A=84=E9=83=A8=E5=88=86=E5=85=A7=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 提供JAVA填充數組的函數Arrays.fill() 2. for loop中不需要if statement,並有用 // 解釋原因 --- ...14\345\205\250\345\271\263\346\226\271\346\225\260.md" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index f5b23d2636..82d02b302d 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -177,15 +177,19 @@ class Solution { for (int j = 0; j <= n; j++) { dp[j] = max; } + //如果不想要寫for-loop填充數組的話,也可以用JAVA內建的Arrays.fill()函數。 + //Arrays.fill(dp, Integer.MAX_VALUE); + //当和为0时,组合的个数为0 dp[0] = 0; // 遍历物品 for (int i = 1; i * i <= n; i++) { // 遍历背包 for (int j = i * i; j <= n; j++) { - if (dp[j - i * i] != max) { + //if (dp[j - i * i] != max) { dp[j] = Math.min(dp[j], dp[j - i * i] + 1); - } + //} + //不需要這個if statement,因爲在完全平方數這一題不會有"湊不成"的狀況發生( 一定可以用"1"來組成任何一個n),故comment掉這個if statement。 } } return dp[n]; From 442008bb8ecfa3f64c9c9402e4c59c46575a17ae Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 22:44:10 -0500 Subject: [PATCH 0408/1533] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\225\260\346\213\206\345\210\206.md" | 66 ++++++++++++++++--- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index c2fbbdde68..f24e2985d5 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -245,20 +245,68 @@ class Solution { ``` ### Python +动态规划(版本一) ```python class Solution: - def integerBreak(self, n: int) -> int: - dp = [0] * (n + 1) - dp[2] = 1 + # 假设对正整数 i 拆分出的第一个正整数是 j(1 <= j < i),则有以下两种方案: + # 1) 将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j * (i-j) + # 2) 将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j * dp[i-j] + def integerBreak(self, n): + dp = [0] * (n + 1) # 创建一个大小为n+1的数组来存储计算结果 + dp[2] = 1 # 初始化dp[2]为1,因为当n=2时,只有一个切割方式1+1=2,乘积为1 + + # 从3开始计算,直到n for i in range(3, n + 1): - # 假设对正整数 i 拆分出的第一个正整数是 j(1 <= j < i),则有以下两种方案: - # 1) 将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j * (i-j) - # 2) 将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j * dp[i-j] - for j in range(1, i / 2 + 1): - dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j])) - return dp[n] + # 遍历所有可能的切割点 + for j in range(1, i // 2 + 1): + + # 计算切割点j和剩余部分(i-j)的乘积,并与之前的结果进行比较取较大值 + + dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j)) + + return dp[n] # 返回最终的计算结果 + +``` +动态规划(版本二) +```python +class Solution: + def integerBreak(self, n): + if n <= 3: + return 1 * (n - 1) # 对于n小于等于3的情况,返回1 * (n - 1) + + dp = [0] * (n + 1) # 创建一个大小为n+1的数组来存储最大乘积结果 + dp[1] = 1 # 当n等于1时,最大乘积为1 + dp[2] = 2 # 当n等于2时,最大乘积为2 + dp[3] = 3 # 当n等于3时,最大乘积为3 + + # 从4开始计算,直到n + for i in range(4, n + 1): + # 遍历所有可能的切割点 + for j in range(1, i // 2 + 1): + # 计算切割点j和剩余部分(i - j)的乘积,并与之前的结果进行比较取较大值 + dp[i] = max(dp[i], dp[i - j] * dp[j]) + + return dp[n] # 返回整数拆分的最大乘积结果 + ``` +贪心 +```python +class Solution: + def integerBreak(self, n): + if n == 2: # 当n等于2时,只有一种拆分方式:1+1=2,乘积为1 + return 1 + if n == 3: # 当n等于3时,只有一种拆分方式:1+1+1=3,乘积为1 + return 2 + if n == 4: # 当n等于4时,有两种拆分方式:2+2=4和1+1+1+1=4,乘积都为4 + return 4 + result = 1 + while n > 4: + result *= 3 # 每次乘以3,因为3的乘积比其他数字更大 + n -= 3 # 每次减去3 + result *= n # 将剩余的n乘以最后的结果 + return result +``` ### Go ```go func integerBreak(n int) int { From 41224f61bd37979847ee6258cda1282aa227befe Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 22:50:49 -0500 Subject: [PATCH 0409/1533] =?UTF-8?q?Update=200096.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\217\211\346\220\234\347\264\242\346\240\221.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 9f41906d27..368a5747b1 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -197,12 +197,13 @@ class Solution { ```python class Solution: def numTrees(self, n: int) -> int: - dp = [0] * (n + 1) - dp[0], dp[1] = 1, 1 - for i in range(2, n + 1): - for j in range(1, i + 1): - dp[i] += dp[j - 1] * dp[i - j] - return dp[-1] + dp = [0] * (n + 1) # 创建一个长度为n+1的数组,初始化为0 + dp[0] = 1 # 当n为0时,只有一种情况,即空树,所以dp[0] = 1 + for i in range(1, n + 1): # 遍历从1到n的每个数字 + for j in range(1, i + 1): # 对于每个数字i,计算以i为根节点的二叉搜索树的数量 + dp[i] += dp[j - 1] * dp[i - j] # 利用动态规划的思想,累加左子树和右子树的组合数量 + return dp[n] # 返回以1到n为节点的二叉搜索树的总数量 + ``` ### Go From 477f4595749ea6d38b22f3bac14cdc35c8aa2191 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 2 Jun 2023 22:24:44 +0800 Subject: [PATCH 0410/1533] =?UTF-8?q?Update=200213.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DII.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\256\266\345\212\253\350\210\215II.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index 6395f3a884..494ad2bd7f 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -248,6 +248,35 @@ function robRange(nums: number[], start: number, end: number): number { } ``` +Rust: + +```rust +impl Solution { + pub fn rob(nums: Vec) -> i32 { + match nums.len() { + 1 => nums[0], + _ => Self::rob_range(&nums, 0, nums.len() - 2).max(Self::rob_range( + &nums, + 1, + nums.len() - 1, + )), + } + } + + pub fn rob_range(nums: &Vec, start: usize, end: usize) -> i32 { + if start == end { + return nums[start]; + } + let mut dp = vec![0; nums.len()]; + dp[start] = nums[start]; + dp[start + 1] = nums[start].max(nums[start + 1]); + for i in start + 2..=end { + dp[i] = dp[i - 1].max(dp[i - 2] + nums[i]); + } + dp[end] + } +} +```

From 6bb21c8e5b18ea720651c087ecbe350ffe87a371 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 11:20:55 +0800 Subject: [PATCH 0411/1533] =?UTF-8?q?Update=200337.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DIII.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\256\266\345\212\253\350\210\215III.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index ca8cea2369..1708b7a1e8 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -490,6 +490,33 @@ function robNode(node: TreeNode | null): MaxValueArr { } ``` +### Rust + +动态规划: + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn rob(root: Option>>) -> i32 { + let (v1, v2) = Self::rob_tree(&root); + v1.max(v2) + } + pub fn rob_tree(cur: &Option>>) -> (i32, i32) { + match cur { + None => (0, 0), + Some(node) => { + let left = Self::rob_tree(&node.borrow_mut().left); + let right = Self::rob_tree(&node.borrow_mut().right); + ( + left.0.max(left.1) + right.0.max(right.1), // 偷左右节点 + node.borrow().val + left.0 + right.0, // 偷父节点 + ) + } + } + } +} +```

From 2d66c713f06609bfcbfa6099b4e858686ae620a2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 13:58:57 +0800 Subject: [PATCH 0412/1533] =?UTF-8?q?Update=200121.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\200\344\275\263\346\227\266\346\234\272.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index 753cb10692..be8630f584 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -510,7 +510,22 @@ public class Solution } ``` +Rust: +> 贪心 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let (mut low, mut res) = (i32::MAX, 0); + for p in prices { + low = p.min(low); + res = res.max(p - low); + } + res + } +} +```

From b716a8ab44c06d9ff0b144774aa43b93efd75c90 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 14:05:04 +0800 Subject: [PATCH 0413/1533] =?UTF-8?q?Update=200121.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\200\344\275\263\346\227\266\346\234\272.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index be8630f584..9c8a43902d 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -527,6 +527,21 @@ impl Solution { } ``` +> 动态规划 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let mut dp = vec![-prices[0], 0]; + for p in prices { + dp[0] = dp[0].max(-p); + dp[1] = dp[1].max(dp[0] + p); + } + dp[1] + } +} +``` +

From 745e91e1e7043474e15ec790a89a472d0d25e279 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 14:22:19 +0800 Subject: [PATCH 0414/1533] =?UTF-8?q?Update=200122.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?II.md=20=E4=BC=98=E5=8C=96=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272II.md" | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index 0d8ad608d7..89c654fad1 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -322,13 +322,10 @@ function maxProfit(prices: number[]): number { ```Rust impl Solution { - fn max(a: i32, b: i32) -> i32 { - if a > b { a } else { b } - } pub fn max_profit(prices: Vec) -> i32 { let mut result = 0; for i in 1..prices.len() { - result += Self::max(prices[i] - prices[i - 1], 0); + result += (prices[i] - prices[i - 1]).max(0); } result } @@ -339,18 +336,14 @@ impl Solution { ```Rust impl Solution { - fn max(a: i32, b: i32) -> i32 { - if a > b { a } else { b } - } pub fn max_profit(prices: Vec) -> i32 { - let n = prices.len(); - let mut dp = vec![vec![0; 2]; n]; - dp[0][0] -= prices[0]; - for i in 1..n { - dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]); - dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i]); + let mut dp = vec![vec![0; 2]; prices.len()]; + dp[0][0] = -prices[0]; + for i in 1..prices.len() { + dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - prices[i]); + dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + prices[i]); } - Self::max(dp[n - 1][0], dp[n - 1][1]) + dp[prices.len() - 1][1] } } ``` From fd7a98b8315eb56f07caac3cc586f6b96e3d246b Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 14:40:00 +0800 Subject: [PATCH 0415/1533] =?UTF-8?q?Update=200122.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?II=EF=BC=88=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=EF=BC=89.md=20a?= =?UTF-8?q?bout=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 146c6a4cb1..5976f25898 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -346,7 +346,52 @@ public class Solution } ``` +Rust: +> 贪心 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let mut result = 0; + for i in 1..prices.len() { + result += (prices[i] - prices[i - 1]).max(0); + } + result + } +} +``` + +>动态规划 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let mut dp = vec![vec![0; 2]; prices.len()]; + dp[0][0] = -prices[0]; + for i in 1..prices.len() { + dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - prices[i]); + dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + prices[i]); + } + dp[prices.len() - 1][1] + } +} +``` + +> 优化 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let mut dp = vec![-prices[0], 0]; + for i in 1..=prices.len() { + dp[0] = dp[0].max(dp[1] - prices[i - 1]); + dp[1] = dp[1].max(dp[0] + prices[i - 1]); + } + dp[1] + } +} +```

From 2719dbdd0eeeaf534acf09f0a67d3f84bae84a04 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 14:58:09 +0800 Subject: [PATCH 0416/1533] =?UTF-8?q?Update=200122.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?II=EF=BC=88=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...250\346\200\201\350\247\204\345\210\222\357\274\211.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 5976f25898..43e567c5bf 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -384,9 +384,10 @@ impl Solution { impl Solution { pub fn max_profit(prices: Vec) -> i32 { let mut dp = vec![-prices[0], 0]; - for i in 1..=prices.len() { - dp[0] = dp[0].max(dp[1] - prices[i - 1]); - dp[1] = dp[1].max(dp[0] + prices[i - 1]); + for p in prices { + // 可以看作 low、res + dp[0] = dp[0].max(dp[1] - p); + dp[1] = dp[1].max(dp[0] + p); } dp[1] } From 2a6a8da5e0e5d0949f92f8ef04aab26c5138770f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 14:59:17 +0800 Subject: [PATCH 0417/1533] =?UTF-8?q?Update=20problems/0122.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAII=EF=BC=88=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" | 1 - 1 file changed, 1 deletion(-) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 43e567c5bf..24e15024b5 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -385,7 +385,6 @@ impl Solution { pub fn max_profit(prices: Vec) -> i32 { let mut dp = vec![-prices[0], 0]; for p in prices { - // 可以看作 low、res dp[0] = dp[0].max(dp[1] - p); dp[1] = dp[1].max(dp[0] + p); } From 09ab0e48738d17c184c197d5043a2e8423d0686d Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 3 Jun 2023 20:10:36 -0500 Subject: [PATCH 0418/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-1.md" | 79 ++++++++++++------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index c25252488a..9df2fc4cb2 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -339,38 +339,63 @@ public class BagProblem { ``` ### python +无参数版 +```python +def test_2_wei_bag_problem1(): + weight = [1, 3, 4] + value = [15, 20, 30] + bagweight = 4 + + # 二维数组 + dp = [[0] * (bagweight + 1) for _ in range(len(weight))] + + # 初始化 + for j in range(weight[0], bagweight + 1): + dp[0][j] = value[0] + + # weight数组的大小就是物品个数 + for i in range(1, len(weight)): # 遍历物品 + for j in range(bagweight + 1): # 遍历背包容量 + if j < weight[i]: + dp[i][j] = dp[i - 1][j] + else: + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]) + print(dp[len(weight) - 1][bagweight]) + +test_2_wei_bag_problem1() + +``` +有参数版 ```python -def test_2_wei_bag_problem1(bag_size, weight, value) -> int: - rows, cols = len(weight), bag_size + 1 - dp = [[0 for _ in range(cols)] for _ in range(rows)] - - # 初始化dp数组. - for i in range(rows): - dp[i][0] = 0 - first_item_weight, first_item_value = weight[0], value[0] - for j in range(1, cols): - if first_item_weight <= j: - dp[0][j] = first_item_value - - # 更新dp数组: 先遍历物品, 再遍历背包. - for i in range(1, len(weight)): - cur_weight, cur_val = weight[i], value[i] - for j in range(1, cols): - if cur_weight > j: # 说明背包装不下当前物品. - dp[i][j] = dp[i - 1][j] # 所以不装当前物品. - else: - # 定义dp数组: dp[i][j] 前i个物品里,放进容量为j的背包,价值总和最大是多少。 - dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight]+ cur_val) - - print(dp) +def test_2_wei_bag_problem1(weight, value, bagweight): + # 二维数组 + dp = [[0] * (bagweight + 1) for _ in range(len(weight))] + # 初始化 + for j in range(weight[0], bagweight + 1): + dp[0][j] = value[0] + + # weight数组的大小就是物品个数 + for i in range(1, len(weight)): # 遍历物品 + for j in range(bagweight + 1): # 遍历背包容量 + if j < weight[i]: + dp[i][j] = dp[i - 1][j] + else: + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]) + + return dp[len(weight) - 1][bagweight] if __name__ == "__main__": - bag_size = 4 - weight = [1, 3, 4] - value = [15, 20, 30] - test_2_wei_bag_problem1(bag_size, weight, value) + + weight = [1, 3, 4] + value = [15, 20, 30] + bagweight = 4 + + result = test_2_wei_bag_problem1(weight, value, bagweight) + print(result) + + ``` From 37fdbd369e16ab7739bfdfd46b9fd8dbcdf514b9 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:59:37 -0500 Subject: [PATCH 0419/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-2.md" | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 3b798334a6..d481e044c5 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -246,25 +246,46 @@ int main() { ### Python +无参版 ```python def test_1_wei_bag_problem(): weight = [1, 3, 4] value = [15, 20, 30] - bag_weight = 4 - # 初始化: 全为0 - dp = [0] * (bag_weight + 1) - - # 先遍历物品, 再遍历背包容量 - for i in range(len(weight)): - for j in range(bag_weight, weight[i] - 1, -1): - # 递归公式 + bagWeight = 4 + + # 初始化 + dp = [0] * (bagWeight + 1) + for i in range(len(weight)): # 遍历物品 + for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - print(dp) + print(dp[bagWeight]) + test_1_wei_bag_problem() ``` +有参版 +```python +def test_1_wei_bag_problem(weight, value, bagWeight): + # 初始化 + dp = [0] * (bagWeight + 1) + for i in range(len(weight)): # 遍历物品 + for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + + return dp[bagWeight] + +if __name__ == "__main__": + + weight = [1, 3, 4] + value = [15, 20, 30] + bagweight = 4 + + result = test_1_wei_bag_problem(weight, value, bagweight) + print(result) + +``` ### Go ```go func test_1_wei_bag_problem(weight, value []int, bagWeight int) int { From 5ab0a94c7152431b23303a6e64dc18a07c9ed37f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 05:36:45 -0500 Subject: [PATCH 0420/1533] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\345\222\214\345\255\220\351\233\206.md" | 103 +++++++++++------- 1 file changed, 63 insertions(+), 40 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 8115e18ea9..bb210e29e2 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -296,62 +296,85 @@ false true false false false true true false false false true true ``` ### Python: +卡哥版 ```python -# 一维度数组解法 class Solution: def canPartition(self, nums: List[int]) -> bool: - target = sum(nums) - if target % 2 == 1: return False - target //= 2 - dp = [0] * (target + 1) - for i in range(len(nums)): - for j in range(target, nums[i] - 1, -1): - dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) - return target == dp[target] -``` + _sum = 0 + + # dp[i]中的i表示背包内总和 + # 题目中说:每个数组中的元素不会超过 100,数组的大小不会超过 200 + # 总和不会大于20000,背包最大只需要其中一半,所以10001大小就可以了 + dp = [0] * 10001 + for num in nums: + _sum += num + # 也可以使用内置函数一步求和 + # _sum = sum(nums) + if _sum % 2 == 1: + return False + target = _sum // 2 + + # 开始 0-1背包 + for num in nums: + for j in range(target, num - 1, -1): # 每一个元素一定是不可重复放入,所以从大到小遍历 + dp[j] = max(dp[j], dp[j - num] + num) + # 集合中的元素正好可以凑成总和target + if dp[target] == target: + return True + return False + +``` +二维DP版 ```python -# 二维度数组解法 class Solution: def canPartition(self, nums: List[int]) -> bool: - target = sum(nums) - nums = sorted(nums) + + total_sum = sum(nums) - # 做最初的判断 - if target % 2 != 0: + if total_sum % 2 != 0: return False - # 找到 target value 可以认为这个是背包的体积 - target = target // 2 + target_sum = total_sum // 2 + dp = [[False] * (target_sum + 1) for _ in range(len(nums) + 1)] - row = len(nums) - col = target + 1 + # 初始化第一行(空子集可以得到和为0) + for i in range(len(nums) + 1): + dp[i][0] = True - # 定义 dp table - dp = [[0 for _ in range(col)] for _ in range(row)] + for i in range(1, len(nums) + 1): + for j in range(1, target_sum + 1): + if j < nums[i - 1]: + # 当前数字大于目标和时,无法使用该数字 + dp[i][j] = dp[i - 1][j] + else: + # 当前数字小于等于目标和时,可以选择使用或不使用该数字 + dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]] - # 初始 dp value - for i in range(row): - dp[i][0] = 0 - - for j in range(1, target): - if nums[0] <= j: - dp[0][j] = nums[0] + return dp[len(nums)][target_sum] - # 遍历 先遍历物品再遍历背包 - for i in range(1, row): +``` +一维DP版 +```python +class Solution: + def canPartition(self, nums: List[int]) -> bool: - cur_weight = nums[i] - cur_value = nums[i] + total_sum = sum(nums) - for j in range(1, col): - if cur_weight > j: - dp[i][j] = dp[i - 1][j] - else: - dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight] + cur_value) - - # 输出结果 - return dp[-1][col - 1] == target + if total_sum % 2 != 0: + return False + + target_sum = total_sum // 2 + dp = [False] * (target_sum + 1) + dp[0] = True + + for num in nums: + # 从target_sum逆序迭代到num,步长为-1 + for i in range(target_sum, num - 1, -1): + dp[i] = dp[i] or dp[i - num] + + return dp[target_sum] + ``` ### Go: From 212e8f8b393dac2730bf0136b09838cdbc4d24b8 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 05:48:05 -0500 Subject: [PATCH 0421/1533] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\347\255\211\345\222\214\345\255\220\351\233\206.md" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index bb210e29e2..7f6e3d5b3b 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -349,7 +349,10 @@ class Solution: dp[i][j] = dp[i - 1][j] else: # 当前数字小于等于目标和时,可以选择使用或不使用该数字 - dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]] + # dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]] + + # 当前数字小于等于目标和时,可以选择使用或不使用该数字的最大值 + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - nums[i - 1]]) return dp[len(nums)][target_sum] @@ -371,9 +374,10 @@ class Solution: for num in nums: # 从target_sum逆序迭代到num,步长为-1 for i in range(target_sum, num - 1, -1): - dp[i] = dp[i] or dp[i - num] + dp[i] = max(dp[i], dp[i - num]) # 可以将其改成 dp[i] = dp[i] or dp[i - num] return dp[target_sum] + ``` From cb9da4fa38d861567a67ef0bd8467461e9ef90ff Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 05:48:34 -0500 Subject: [PATCH 0422/1533] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 7f6e3d5b3b..341362baaa 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -351,7 +351,7 @@ class Solution: # 当前数字小于等于目标和时,可以选择使用或不使用该数字 # dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]] - # 当前数字小于等于目标和时,可以选择使用或不使用该数字的最大值 + # 当前数字小于等于目标和时,选择使用或不使用该数字的最大值 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - nums[i - 1]]) return dp[len(nums)][target_sum] From 285b6ab370ea7e08d768574751e1971c3ff11810 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 05:53:50 -0500 Subject: [PATCH 0423/1533] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\347\255\211\345\222\214\345\255\220\351\233\206.md" | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 341362baaa..1f12931274 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -349,10 +349,7 @@ class Solution: dp[i][j] = dp[i - 1][j] else: # 当前数字小于等于目标和时,可以选择使用或不使用该数字 - # dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]] - - # 当前数字小于等于目标和时,选择使用或不使用该数字的最大值 - dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - nums[i - 1]]) + dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]] return dp[len(nums)][target_sum] @@ -374,8 +371,7 @@ class Solution: for num in nums: # 从target_sum逆序迭代到num,步长为-1 for i in range(target_sum, num - 1, -1): - dp[i] = max(dp[i], dp[i - num]) # 可以将其改成 dp[i] = dp[i] or dp[i - num] - + dp[i] = dp[i] or dp[i - num] return dp[target_sum] From 980c98ca0d15304b701d4cfbcc52a1d59ab736bf Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 06:27:03 -0500 Subject: [PATCH 0424/1533] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8F?= =?UTF-8?q?II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\232\204\351\207\215\351\207\217II.md" | 75 +++++++++++++++++-- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index a8150b1f04..a978b80262 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -224,18 +224,79 @@ class Solution { ``` ### Python: +卡哥版 ```python class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: - sumweight = sum(stones) - target = sumweight // 2 - dp = [0] * (target + 1) - for i in range(len(stones)): - for j in range(target, stones[i] - 1, -1): - dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]) - return sumweight - 2 * dp[target] + dp = [0] * 15001 + total_sum = sum(stones) + target = total_sum // 2 + + for stone in stones: # 遍历物品 + for j in range(target, stone - 1, -1): # 遍历背包 + dp[j] = max(dp[j], dp[j - stone] + stone) + + return total_sum - dp[target] - dp[target] + ``` +二维DP版 +```python +class Solution: + def lastStoneWeightII(self, stones: List[int]) -> int: + total_sum = sum(stones) + target = total_sum // 2 + + # 创建二维dp数组,行数为石头的数量加1,列数为target加1 + # dp[i][j]表示前i个石头能否组成总重量为j + dp = [[False] * (target + 1) for _ in range(len(stones) + 1)] + + # 初始化第一列,表示总重量为0时,前i个石头都能组成 + for i in range(len(stones) + 1): + dp[i][0] = True + + for i in range(1, len(stones) + 1): + for j in range(1, target + 1): + # 如果当前石头重量大于当前目标重量j,则无法选择该石头 + if stones[i - 1] > j: + dp[i][j] = dp[i - 1][j] + else: + # 可选择该石头或不选择该石头 + dp[i][j] = dp[i - 1][j] or dp[i - 1][j - stones[i - 1]] + + # 找到最大的重量i,使得dp[len(stones)][i]为True + # 返回总重量减去两倍的最接近总重量一半的重量 + for i in range(target, -1, -1): + if dp[len(stones)][i]: + return total_sum - 2 * i + + return 0 + +``` +一维DP版 +```python +class Solution: + def lastStoneWeightII(self, stones): + total_sum = sum(stones) + target = total_sum // 2 + dp = [False] * (target + 1) + dp[0] = True + + for stone in stones: + for j in range(target, stone - 1, -1): + # 判断当前重量是否可以通过选择之前的石头得到或选择当前石头和之前的石头得到 + dp[j] = dp[j] or dp[j - stone] + + for i in range(target, -1, -1): + if dp[i]: + # 返回剩余石头的重量,即总重量减去两倍的最接近总重量一半的重量 + return total_sum - 2 * i + + return 0 + + + +``` ### Go: ```go func lastStoneWeightII(stones []int) int { From 8b69827fca7cfe9bd6e2fff92d7774189b489a32 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 07:01:19 -0500 Subject: [PATCH 0425/1533] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 85 ++++++++++++++++--- 1 file changed, 75 insertions(+), 10 deletions(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index c9f888920f..32931e6b1f 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -293,19 +293,84 @@ class Solution { ``` ### Python +回溯版 +```python +class Solution: + + + def backtracking(self, candidates, target, total, startIndex, path, result): + if total == target: + result.append(path[:]) # 将当前路径的副本添加到结果中 + # 如果 sum + candidates[i] > target,则停止遍历 + for i in range(startIndex, len(candidates)): + if total + candidates[i] > target: + break + total += candidates[i] + path.append(candidates[i]) + self.backtracking(candidates, target, total, i + 1, path, result) + total -= candidates[i] + path.pop() + + def findTargetSumWays(self, nums: List[int], target: int) -> int: + total = sum(nums) + if target > total: + return 0 # 此时没有方案 + if (target + total) % 2 != 0: + return 0 # 此时没有方案,两个整数相加时要注意数值溢出的问题 + bagSize = (target + total) // 2 # 转化为组合总和问题,bagSize就是目标和 + + # 以下是回溯法代码 + result = [] + nums.sort() # 需要对nums进行排序 + self.backtracking(nums, bagSize, 0, 0, [], result) + return len(result) + +``` +二维DP ```python class Solution: def findTargetSumWays(self, nums: List[int], target: int) -> int: - sumValue = sum(nums) - #注意边界条件为 target>sumValue or target<-sumValue or (sumValue + target) % 2 == 1 - if abs(target) > sumValue or (sumValue + target) % 2 == 1: return 0 - bagSize = (sumValue + target) // 2 - dp = [0] * (bagSize + 1) - dp[0] = 1 - for i in range(len(nums)): - for j in range(bagSize, nums[i] - 1, -1): - dp[j] += dp[j - nums[i]] - return dp[bagSize] + total_sum = sum(nums) # 计算nums的总和 + if abs(target) > total_sum: + return 0 # 此时没有方案 + if (target + total_sum) % 2 == 1: + return 0 # 此时没有方案 + target_sum = (target + total_sum) // 2 # 目标和 + + # 创建二维动态规划数组,行表示选取的元素数量,列表示累加和 + dp = [[0] * (target_sum + 1) for _ in range(len(nums) + 1)] + + # 初始化状态 + dp[0][0] = 1 + + # 动态规划过程 + for i in range(1, len(nums) + 1): + for j in range(target_sum + 1): + dp[i][j] = dp[i - 1][j] # 不选取当前元素 + if j >= nums[i - 1]: + dp[i][j] += dp[i - 1][j - nums[i - 1]] # 选取当前元素 + + return dp[len(nums)][target_sum] # 返回达到目标和的方案数 + + +``` +一维DP +```python +class Solution: + def findTargetSumWays(self, nums: List[int], target: int) -> int: + total_sum = sum(nums) # 计算nums的总和 + if abs(target) > total_sum: + return 0 # 此时没有方案 + if (target + total_sum) % 2 == 1: + return 0 # 此时没有方案 + target_sum = (target + total_sum) // 2 # 目标和 + dp = [0] * (target_sum + 1) # 创建动态规划数组,初始化为0 + dp[0] = 1 # 当目标和为0时,只有一种方案,即什么都不选 + for num in nums: + for j in range(target_sum, num - 1, -1): + dp[j] += dp[j - num] # 状态转移方程,累加不同选择方式的数量 + return dp[target_sum] # 返回达到目标和的方案数 + ``` ### Go From b9faa11851fa43443380efefade020a02e0744f4 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 07:41:05 -0500 Subject: [PATCH 0426/1533] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\344\270\200\345\222\214\351\233\266.md" | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 6a178a254b..145f64aaa2 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -210,19 +210,35 @@ class Solution { ``` ### Python +DP(版本一) ```python class Solution: def findMaxForm(self, strs: List[str], m: int, n: int) -> int: - dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0 + dp = [[0] * (n + 1) for _ in range(m + 1)] # 创建二维动态规划数组,初始化为0 + for s in strs: # 遍历物品 + zeroNum = s.count('0') # 统计0的个数 + oneNum = len(s) - zeroNum # 统计1的个数 + for i in range(m, zeroNum - 1, -1): # 遍历背包容量且从后向前遍历 + for j in range(n, oneNum - 1, -1): + dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1) # 状态转移方程 + return dp[m][n] + +``` +DP(版本二) +```python +class Solution: + def findMaxForm(self, strs: List[str], m: int, n: int) -> int: + dp = [[0] * (n + 1) for _ in range(m + 1)] # 创建二维动态规划数组,初始化为0 # 遍历物品 - for str in strs: - ones = str.count('1') - zeros = str.count('0') - # 遍历背包容量且从后向前遍历! + for s in strs: + ones = s.count('1') # 统计字符串中1的个数 + zeros = s.count('0') # 统计字符串中0的个数 + # 遍历背包容量且从后向前遍历 for i in range(m, zeros - 1, -1): for j in range(n, ones - 1, -1): - dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1) + dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1) # 状态转移方程 return dp[m][n] + ``` ### Go From e002075a39841cca40e9fc95c1e23fdf4fcf5e42 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 07:52:08 -0500 Subject: [PATCH 0427/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\205\250\350\203\214\345\214\205.md" | 77 ++++++++++++++----- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index e927aa2061..2dc45fdb77 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -222,44 +222,79 @@ private static void testCompletePackAnotherWay(){ Python: - +先遍历物品,再遍历背包(无参版) ```python -# 先遍历物品,再遍历背包 -def test_complete_pack1(): +def test_CompletePack(): weight = [1, 3, 4] value = [15, 20, 30] - bag_weight = 4 + bagWeight = 4 + dp = [0] * (bagWeight + 1) + for i in range(len(weight)): # 遍历物品 + for j in range(weight[i], bagWeight + 1): # 遍历背包容量 + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + print(dp[bagWeight]) - dp = [0]*(bag_weight + 1) +test_CompletePack() - for i in range(len(weight)): - for j in range(weight[i], bag_weight + 1): +``` + +先遍历物品,再遍历背包(有参版) +```python +def test_CompletePack(weight, value, bagWeight): + dp = [0] * (bagWeight + 1) + for i in range(len(weight)): # 遍历物品 + for j in range(weight[i], bagWeight + 1): # 遍历背包容量 dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - - print(dp[bag_weight]) + return dp[bagWeight] -# 先遍历背包,再遍历物品 -def test_complete_pack2(): +if __name__ == "__main__": weight = [1, 3, 4] value = [15, 20, 30] - bag_weight = 4 + bagWeight = 4 + result = test_CompletePack(weight, value, bagWeight) + print(result) - dp = [0]*(bag_weight + 1) +``` +先遍历背包,再遍历物品(无参版) +```python +def test_CompletePack(): + weight = [1, 3, 4] + value = [15, 20, 30] + bagWeight = 4 - for j in range(bag_weight + 1): - for i in range(len(weight)): - if j >= weight[i]: dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - - print(dp[bag_weight]) + dp = [0] * (bagWeight + 1) + + for j in range(bagWeight + 1): # 遍历背包容量 + for i in range(len(weight)): # 遍历物品 + if j - weight[i] >= 0: + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + + print(dp[bagWeight]) + +test_CompletePack() -if __name__ == '__main__': - test_complete_pack1() - test_complete_pack2() ``` +先遍历背包,再遍历物品(有参版) +```python +def test_CompletePack(weight, value, bagWeight): + dp = [0] * (bagWeight + 1) + for j in range(bagWeight + 1): # 遍历背包容量 + for i in range(len(weight)): # 遍历物品 + if j - weight[i] >= 0: + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + return dp[bagWeight] + +if __name__ == "__main__": + weight = [1, 3, 4] + value = [15, 20, 30] + bagWeight = 4 + result = test_CompletePack(weight, value, bagWeight) + print(result) +``` Go: ```go From ea98e43831f3f735bc38b0e5a55f52fe38c5cdd5 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 07:53:16 -0500 Subject: [PATCH 0428/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 2dc45fdb77..a559a51289 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -222,6 +222,9 @@ private static void testCompletePackAnotherWay(){ Python: + + + 先遍历物品,再遍历背包(无参版) ```python def test_CompletePack(): From d7eac13627675be325ca50ce7fa6370b3c589936 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 10:13:13 -0500 Subject: [PATCH 0429/1533] =?UTF-8?q?Update=200377.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C=E2=85=A3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\346\200\273\345\222\214\342\205\243.md" | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index ee65972371..bb847489bc 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -175,22 +175,36 @@ class Solution { ``` Python: - +卡哥版 ```python class Solution: - def combinationSum4(self, nums, target): + def combinationSum4(self, nums: List[int], target: int) -> int: dp = [0] * (target + 1) dp[0] = 1 + for i in range(1, target + 1): # 遍历背包 + for j in range(len(nums)): # 遍历物品 + if i - nums[j] >= 0: + dp[i] += dp[i - nums[j]] + return dp[target] - for i in range(1, target+1): - for j in nums: - if i >= j: - dp[i] += dp[i - j] - - return dp[-1] ``` +优化版 + +```python +class Solution: + def combinationSum4(self, nums: List[int], target: int) -> int: + dp = [0] * (target + 1) # 创建动态规划数组,用于存储组合总数 + dp[0] = 1 # 初始化背包容量为0时的组合总数为1 + + for i in range(1, target + 1): # 遍历背包容量 + for j in nums: # 遍历物品列表 + if i >= j: # 当背包容量大于等于当前物品重量时 + dp[i] += dp[i - j] # 更新组合总数 + return dp[-1] # 返回背包容量为target时的组合总数 + +``` Go: ```go func combinationSum4(nums []int, target int) int { From 93952c67e170d338fbc4b017cf10563bf4887c92 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 10:13:29 -0500 Subject: [PATCH 0430/1533] =?UTF-8?q?Update=200377.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C=E2=85=A3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index bb847489bc..b6b15386cc 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -175,6 +175,8 @@ class Solution { ``` Python: + + 卡哥版 ```python class Solution: From 245556b1c8e27f14854273383af529379007a360 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 23:27:54 -0500 Subject: [PATCH 0431/1533] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=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" | 80 ++++++++++++++----- 1 file changed, 59 insertions(+), 21 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..903777cad4 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" @@ -216,38 +216,76 @@ class Solution { ``` Python: +先遍历物品 后遍历背包 +```python +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + dp = [float('inf')] * (amount + 1) # 创建动态规划数组,初始值为正无穷大 + dp[0] = 0 # 初始化背包容量为0时的最小硬币数量为0 + + for coin in coins: # 遍历硬币列表,相当于遍历物品 + for i in range(coin, amount + 1): # 遍历背包容量 + if dp[i - coin] != float('inf'): # 如果dp[i - coin]不是初始值,则进行状态转移 + dp[i] = min(dp[i - coin] + 1, dp[i]) # 更新最小硬币数量 + + if dp[amount] == float('inf'): # 如果最终背包容量的最小硬币数量仍为正无穷大,表示无解 + return -1 + return dp[amount] # 返回背包容量为amount时的最小硬币数量 + +``` + +先遍历背包 后遍历物品 +```python +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + dp = [float('inf')] * (amount + 1) # 创建动态规划数组,初始值为正无穷大 + dp[0] = 0 # 初始化背包容量为0时的最小硬币数量为0 + + for i in range(1, amount + 1): # 遍历背包容量 + for j in range(len(coins)): # 遍历硬币列表,相当于遍历物品 + if i - coins[j] >= 0 and dp[i - coins[j]] != float('inf'): # 如果dp[i - coins[j]]不是初始值,则进行状态转移 + dp[i] = min(dp[i - coins[j]] + 1, dp[i]) # 更新最小硬币数量 + + if dp[amount] == float('inf'): # 如果最终背包容量的最小硬币数量仍为正无穷大,表示无解 + return -1 + return dp[amount] # 返回背包容量为amount时的最小硬币数量 +``` +先遍历物品 后遍历背包(优化版) ```python class Solution: def coinChange(self, coins: List[int], amount: int) -> int: - '''版本一''' - # 初始化 - dp = [float("inf")]*(amount + 1) + dp = [float('inf')] * (amount + 1) dp[0] = 0 - # 遍历物品 + for coin in coins: - # 遍历背包 - for j in range(coin, amount + 1): - dp[j] = min(dp[j], dp[j - coin] + 1) - return dp[amount] if dp[amount] != float("inf") else -1 - - def coinChange1(self, coins: List[int], amount: int) -> int: - '''版本二''' - # 初始化 - dp = [float("inf")]*(amount + 1) - dp[0] = 0 - # 遍历物品 - for j in range(1, amount + 1): - # 遍历背包 - for coin in coins: - if j >= coin: - dp[j] = min(dp[j], dp[j - coin] + 1) - return dp[amount] if dp[amount] != float("inf") else -1 + for i in range(coin, amount + 1): + # 更新凑成金额 i 所需的最少硬币数量 + dp[i] = min(dp[i], dp[i - coin] + 1) + + return dp[amount] if dp[amount] != float('inf') else -1 + + ``` +先遍历背包 后遍历物品(优化版) +```python +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + dp = [float('inf')] * (amount + 1) + dp[0] = 0 + for i in range(1, amount + 1): # 遍历背包容量 + for coin in coins: # 遍历物品 + if i - coin >= 0: + # 更新凑成金额 i 所需的最少硬币数量 + dp[i] = min(dp[i], dp[i - coin] + 1) + return dp[amount] if dp[amount] != float('inf') else -1 + +``` + Go: ```go // 版本一, 先遍历物品,再遍历背包 From beb0a51841fcf308e3044046b93009bf9ea38c82 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 23:28:14 -0500 Subject: [PATCH 0432/1533] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" | 2 ++ 1 file changed, 2 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 903777cad4..76ccb234f7 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" @@ -216,6 +216,8 @@ class Solution { ``` Python: + + 先遍历物品 后遍历背包 ```python class Solution: From 0964e16991e4845f8e6dff468d7d0f0ad8a0d0c4 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 23:32:42 -0500 Subject: [PATCH 0433/1533] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 76ccb234f7..67b1a6d9cd 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" @@ -261,7 +261,7 @@ class Solution: dp[0] = 0 for coin in coins: - for i in range(coin, amount + 1): + for i in range(coin, amount + 1): # 进行优化,从能装得下的背包开始计算,则不需要进行比较 # 更新凑成金额 i 所需的最少硬币数量 dp[i] = min(dp[i], dp[i - coin] + 1) From 87e7b9def8de5dc5bb786c9744827e81108859f1 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Sun, 4 Jun 2023 23:58:00 -0500 Subject: [PATCH 0434/1533] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\271\263\346\226\271\346\225\260.md" | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index f5b23d2636..b26f981ccf 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -217,36 +217,45 @@ class Solution { Python: +先遍历物品, 再遍历背包 ```python class Solution: def numSquares(self, n: int) -> int: - '''版本一,先遍历背包, 再遍历物品''' - # 初始化 - nums = [i**2 for i in range(1, n + 1) if i**2 <= n] - dp = [10**4]*(n + 1) + dp = [float('inf')] * (n + 1) dp[0] = 0 - # 遍历背包 - for j in range(1, n + 1): - # 遍历物品 - for num in nums: - if j >= num: - dp[j] = min(dp[j], dp[j - num] + 1) + + for i in range(1, n + 1): # 遍历背包 + for j in range(1, int(i ** 0.5) + 1): # 遍历物品 + # 更新凑成数字 i 所需的最少完全平方数数量 + dp[i] = min(dp[i], dp[i - j * j] + 1) + return dp[n] - - def numSquares1(self, n: int) -> int: - '''版本二, 先遍历物品, 再遍历背包''' - # 初始化 - nums = [i**2 for i in range(1, n + 1) if i**2 <= n] - dp = [10**4]*(n + 1) + +``` +其他版本 +```python +class Solution: + def numSquares(self, n: int) -> int: + # 创建动态规划数组,初始值为最大值 + dp = [float('inf')] * (n + 1) + # 初始化已知情况 dp[0] = 0 - # 遍历物品 - for num in nums: - # 遍历背包 - for j in range(num, n + 1): - dp[j] = min(dp[j], dp[j - num] + 1) + + # 遍历背包容量 + for i in range(1, n + 1): + # 遍历完全平方数作为物品 + j = 1 + while j * j <= i: + # 更新最少完全平方数的数量 + dp[i] = min(dp[i], dp[i - j * j] + 1) + j += 1 + + # 返回结果 return dp[n] -``` + + +``` Go: ```go // 版本一,先遍历物品, 再遍历背包 From 45037cec331a3c924a54a24a279e5d0b6d342361 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 5 Jun 2023 00:05:11 -0500 Subject: [PATCH 0435/1533] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\250\345\271\263\346\226\271\346\225\260.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index b26f981ccf..80b69f0c92 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -231,6 +231,22 @@ class Solution: return dp[n] +``` +先遍历背包, 再遍历物品 +```python +class Solution: + def numSquares(self, n: int) -> int: + dp = [float('inf')] * (n + 1) + dp[0] = 0 + + for i in range(1, int(n ** 0.5) + 1): # 遍历物品 + for j in range(i * i, n + 1): # 遍历背包 + # 更新凑成数字 j 所需的最少完全平方数数量 + dp[j] = min(dp[j - i * i] + 1, dp[j]) + + return dp[n] + + ``` 其他版本 ```python From d8c51b2d362de5a72ec86a7df3f31e486aa8febf Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 5 Jun 2023 03:09:10 -0500 Subject: [PATCH 0436/1533] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\350\257\215\346\213\206\345\210\206.md" | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 230942ef02..0f24c71ebe 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -337,10 +337,53 @@ class Solution { Python: +回溯 +```python +class Solution: + def backtracking(self, s: str, wordSet: set[str], startIndex: int) -> bool: + # 边界情况:已经遍历到字符串末尾,返回True + if startIndex >= len(s): + return True + + # 遍历所有可能的拆分位置 + for i in range(startIndex, len(s)): + word = s[startIndex:i + 1] # 截取子串 + if word in wordSet and self.backtracking(s, wordSet, i + 1): + # 如果截取的子串在字典中,并且后续部分也可以被拆分成单词,返回True + return True + + # 无法进行有效拆分,返回False + return False + + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + wordSet = set(wordDict) # 转换为哈希集合,提高查找效率 + return self.backtracking(s, wordSet, 0) + +``` +DP(版本一) +```python +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + wordSet = set(wordDict) + n = len(s) + dp = [False] * (n + 1) # dp[i] 表示字符串的前 i 个字符是否可以被拆分成单词 + dp[0] = True # 初始状态,空字符串可以被拆分成单词 + + for i in range(1, n + 1): + for j in range(i): + if dp[j] and s[j:i] in wordSet: + dp[i] = True # 如果 s[0:j] 可以被拆分成单词,并且 s[j:i] 在单词集合中存在,则 s[0:i] 可以被拆分成单词 + break + + return dp[n] + + +``` +DP(版本二) + ```python class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: - '''排列''' dp = [False]*(len(s) + 1) dp[0] = True # 遍历背包 @@ -351,17 +394,6 @@ class Solution: dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j]) return dp[len(s)] ``` -```python -class Solution: # 和视频中写法一致(和最上面C++写法一致) - def wordBreak(self, s: str, wordDict: List[str]) -> bool: - dp = [False]*(len(s)+1) - dp[0]=True - for j in range(1,len(s)+1): - for i in range(j): - word = s[i:j] - if word in wordDict and dp[i]: dp[j]=True - return dp[-1] -``` From de7879f26e10077522203378528fc02854d209e0 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 5 Jun 2023 03:12:19 -0500 Subject: [PATCH 0437/1533] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 0f24c71ebe..742fe06cb4 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -369,8 +369,8 @@ class Solution: dp = [False] * (n + 1) # dp[i] 表示字符串的前 i 个字符是否可以被拆分成单词 dp[0] = True # 初始状态,空字符串可以被拆分成单词 - for i in range(1, n + 1): - for j in range(i): + for i in range(1, n + 1): # 遍历背包 + for j in range(i): # 遍历单词 if dp[j] and s[j:i] in wordSet: dp[i] = True # 如果 s[0:j] 可以被拆分成单词,并且 s[j:i] 在单词集合中存在,则 s[0:i] 可以被拆分成单词 break From bdedd2df0f256b3b5d2c489fb558e92563822ce6 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 5 Jun 2023 07:46:22 -0500 Subject: [PATCH 0438/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=A4=9A=E9=87=8D?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\351\207\215\350\203\214\345\214\205.md" | 124 ++++++++++++++---- 1 file changed, 98 insertions(+), 26 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index af10dab761..1a856bf5fa 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -194,55 +194,127 @@ public void testMultiPack2(){ Python: +改变物品数量为01背包格式(无参版) ```python -def test_multi_pack1(): - '''版本一:改变物品数量为01背包格式''' +def test_multi_pack(): weight = [1, 3, 4] value = [15, 20, 30] nums = [2, 3, 2] - bag_weight = 10 + bagWeight = 10 + + # 将数量大于1的物品展开 for i in range(len(nums)): - # 将物品展开数量为1 while nums[i] > 1: weight.append(weight[i]) value.append(value[i]) nums[i] -= 1 - - dp = [0]*(bag_weight + 1) - # 遍历物品 - for i in range(len(weight)): - # 遍历背包 - for j in range(bag_weight, weight[i] - 1, -1): + + dp = [0] * (bagWeight + 1) + for i in range(len(weight)): # 遍历物品 + for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - - print(" ".join(map(str, dp))) + for j in range(bagWeight + 1): + print(dp[j], end=" ") + print() + + print(dp[bagWeight]) -def test_multi_pack2(): - '''版本:改变遍历个数''' + +test_multi_pack() + +``` + + +改变遍历个数(无参版) +```python +def test_multi_pack(): weight = [1, 3, 4] value = [15, 20, 30] nums = [2, 3, 2] - bag_weight = 10 + bagWeight = 10 + dp = [0] * (bagWeight + 1) + + for i in range(len(weight)): # 遍历物品 + for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 + # 以上为01背包,然后加一个遍历个数 + for k in range(1, nums[i] + 1): # 遍历个数 + if j - k * weight[i] >= 0: + dp[j] = max(dp[j], dp[j - k * weight[i]] + k * value[i]) + + # 打印一下dp数组 + for j in range(bagWeight + 1): + print(dp[j], end=" ") + print() + + print(dp[bagWeight]) + + +test_multi_pack() + +``` + + +改变物品数量为01背包格式(有参版) +```python +def test_multi_pack(weight, value, nums, bagWeight): + # 将数量大于1的物品展开 + for i in range(len(nums)): + while nums[i] > 1: + weight.append(weight[i]) + value.append(value[i]) + nums[i] -= 1 + + dp = [0] * (bagWeight + 1) + for i in range(len(weight)): # 遍历物品 + for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + for j in range(bagWeight + 1): + print(dp[j], end=" ") + print() + + print(dp[bagWeight]) - dp = [0]*(bag_weight + 1) - for i in range(len(weight)): - for j in range(bag_weight, weight[i] - 1, -1): - # 以上是01背包,加上遍历个数 - for k in range(1, nums[i] + 1): - if j - k*weight[i] >= 0: - dp[j] = max(dp[j], dp[j - k*weight[i]] + k*value[i]) - print(" ".join(map(str, dp))) -if __name__ == '__main__': - test_multi_pack1() - test_multi_pack2() +if __name__ == "__main__": + weight = [1, 3, 4] + value = [15, 20, 30] + nums = [2, 3, 2] + bagWeight = 10 + test_multi_pack(weight, value, nums, bagWeight) ``` +改变遍历个数(有参版) +```python +def test_multi_pack(weight, value, nums, bagWeight): + dp = [0] * (bagWeight + 1) + + for i in range(len(weight)): # 遍历物品 + for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 + # 以上为01背包,然后加一个遍历个数 + for k in range(1, nums[i] + 1): # 遍历个数 + if j - k * weight[i] >= 0: + dp[j] = max(dp[j], dp[j - k * weight[i]] + k * value[i]) + # 使用 join 函数打印 dp 数组 + print(' '.join(str(dp[j]) for j in range(bagWeight + 1))) + print(dp[bagWeight]) + + + + + +if __name__ == "__main__": + weight = [1, 3, 4] + value = [15, 20, 30] + nums = [2, 3, 2] + bagWeight = 10 + test_multi_pack(weight, value, nums, bagWeight) + +``` Go: ```go From c33556c440f24942f94a55a0a2f48a1abb300290 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 5 Jun 2023 07:54:00 -0500 Subject: [PATCH 0439/1533] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\345\256\266\345\212\253\350\210\215.md" | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 6e682ec39e..a2fbe8ce62 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -166,30 +166,64 @@ class Solution { ``` Python: +1维DP ```python class Solution: def rob(self, nums: List[int]) -> int: - if len(nums) == 0: + if len(nums) == 0: # 如果没有房屋,返回0 return 0 - if len(nums) == 1: + if len(nums) == 1: # 如果只有一个房屋,返回其金额 return nums[0] + + # 创建一个动态规划数组,用于存储最大金额 dp = [0] * len(nums) - dp[0] = nums[0] - dp[1] = max(nums[0], nums[1]) + dp[0] = nums[0] # 将dp的第一个元素设置为第一个房屋的金额 + dp[1] = max(nums[0], nums[1]) # 将dp的第二个元素设置为第一二个房屋中的金额较大者 + + # 遍历剩余的房屋 for i in range(2, len(nums)): - dp[i] = max(dp[i-2]+nums[i], dp[i-1]) - return dp[-1] + # 对于每个房屋,选择抢劫当前房屋和抢劫前一个房屋的最大金额 + dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]) + + return dp[-1] # 返回最后一个房屋中可抢劫的最大金额 +``` +2维DP +```python +class Solution: + def rob(self, nums: List[int]) -> int: + if not nums: # 如果没有房屋,返回0 + return 0 + + n = len(nums) + dp = [[0, 0] for _ in range(n)] # 创建二维动态规划数组,dp[i][0]表示不抢劫第i个房屋的最大金额,dp[i][1]表示抢劫第i个房屋的最大金额 + + dp[0][1] = nums[0] # 抢劫第一个房屋的最大金额为第一个房屋的金额 + + for i in range(1, n): + dp[i][0] = max(dp[i-1][0], dp[i-1][1]) # 不抢劫第i个房屋,最大金额为前一个房屋抢劫和不抢劫的最大值 + dp[i][1] = dp[i-1][0] + nums[i] # 抢劫第i个房屋,最大金额为前一个房屋不抢劫的最大金额加上当前房屋的金额 + + return max(dp[n-1][0], dp[n-1][1]) # 返回最后一个房屋中可抢劫的最大金额 + ``` +优化版 ```python -class Solution: # 二维dp数组写法 +class Solution: def rob(self, nums: List[int]) -> int: - dp = [[0,0] for _ in range(len(nums))] - dp[0][1] = nums[0] - for i in range(1,len(nums)): - dp[i][0] = max(dp[i-1][1],dp[i-1][0]) - dp[i][1] = dp[i-1][0]+nums[i] - print(dp) - return max(dp[-1]) + if not nums: # 如果没有房屋,返回0 + return 0 + + prev_max = 0 # 上一个房屋的最大金额 + curr_max = 0 # 当前房屋的最大金额 + + for num in nums: + temp = curr_max # 临时变量保存当前房屋的最大金额 + curr_max = max(prev_max + num, curr_max) # 更新当前房屋的最大金额 + prev_max = temp # 更新上一个房屋的最大金额 + + return curr_max # 返回最后一个房屋中可抢劫的最大金额 + + ``` Go: ```Go From bc1324292575f7c67c7ed207b0fe5714244a5984 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 5 Jun 2023 07:54:20 -0500 Subject: [PATCH 0440/1533] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index a2fbe8ce62..084aa301a4 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -166,6 +166,7 @@ class Solution { ``` Python: + 1维DP ```python class Solution: From 3b3d9c4d11328311f945a420f40a03cdb8a92512 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 5 Jun 2023 08:07:53 -0500 Subject: [PATCH 0441/1533] =?UTF-8?q?Update=200213.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\256\266\345\212\253\350\210\215II.md" | 103 +++++++++++++----- 1 file changed, 78 insertions(+), 25 deletions(-) diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index 6395f3a884..3f532a410b 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -130,40 +130,93 @@ class Solution { ``` Python: + ```Python class Solution: def rob(self, nums: List[int]) -> int: - #在198入门级的打家劫舍问题上分两种情况考虑 - #一是不偷第一间房,二是不偷最后一间房 - if len(nums)==1:#题目中提示nums.length>=1,所以不需要考虑len(nums)==0的情况 + if len(nums) == 0: + return 0 + if len(nums) == 1: return nums[0] - val1=self.roblist(nums[1:])#不偷第一间房 - val2=self.roblist(nums[:-1])#不偷最后一间房 - return max(val1,val2) - - def roblist(self,nums): - l=len(nums) - dp=[0]*l - dp[0]=nums[0] - for i in range(1,l): - if i==1: - dp[i]=max(dp[i-1],nums[i]) - else: - dp[i]=max(dp[i-1],dp[i-2]+nums[i]) - return dp[-1] + + result1 = self.robRange(nums, 0, len(nums) - 2) # 情况二 + result2 = self.robRange(nums, 1, len(nums) - 1) # 情况三 + return max(result1, result2) + # 198.打家劫舍的逻辑 + def robRange(self, nums: List[int], start: int, end: int) -> int: + if end == start: + return nums[start] + + prev_max = nums[start] + curr_max = max(nums[start], nums[start + 1]) + + for i in range(start + 2, end + 1): + temp = curr_max + curr_max = max(prev_max + nums[i], curr_max) + prev_max = temp + + return curr_max + ``` +2维DP ```python -class Solution: # 二维dp数组写法 +class Solution: def rob(self, nums: List[int]) -> int: - if len(nums)<3: return max(nums) - return max(self.default(nums[:-1]),self.default(nums[1:])) - def default(self,nums): - dp = [[0,0] for _ in range(len(nums))] + if len(nums) < 3: + return max(nums) + + # 情况二:不抢劫第一个房屋 + result1 = self.robRange(nums[:-1]) + + # 情况三:不抢劫最后一个房屋 + result2 = self.robRange(nums[1:]) + + return max(result1, result2) + + def robRange(self, nums): + dp = [[0, 0] for _ in range(len(nums))] dp[0][1] = nums[0] - for i in range(1,len(nums)): - dp[i][0] = max(dp[i-1]) - dp[i][1] = dp[i-1][0] + nums[i] + + for i in range(1, len(nums)): + dp[i][0] = max(dp[i - 1]) + dp[i][1] = dp[i - 1][0] + nums[i] + return max(dp[-1]) + + + +``` + +优化版 +```python +class Solution: + def rob(self, nums: List[int]) -> int: + if not nums: # 如果没有房屋,返回0 + return 0 + + if len(nums) == 1: # 如果只有一个房屋,返回该房屋的金额 + return nums[0] + + # 情况二:不抢劫第一个房屋 + prev_max = 0 # 上一个房屋的最大金额 + curr_max = 0 # 当前房屋的最大金额 + for num in nums[1:]: + temp = curr_max # 临时变量保存当前房屋的最大金额 + curr_max = max(prev_max + num, curr_max) # 更新当前房屋的最大金额 + prev_max = temp # 更新上一个房屋的最大金额 + result1 = curr_max + + # 情况三:不抢劫最后一个房屋 + prev_max = 0 # 上一个房屋的最大金额 + curr_max = 0 # 当前房屋的最大金额 + for num in nums[:-1]: + temp = curr_max # 临时变量保存当前房屋的最大金额 + curr_max = max(prev_max + num, curr_max) # 更新当前房屋的最大金额 + prev_max = temp # 更新上一个房屋的最大金额 + result2 = curr_max + + return max(result1, result2) + ``` Go: From 4eadc742c74eb2c1b059cbac439110c13fe23b3d Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 6 Jun 2023 23:24:33 +0800 Subject: [PATCH 0442/1533] Update --- README.md | 2 +- ...01\350\247\204\345\210\222\357\274\211.md" | 11 ++- ...04\345\255\220\345\272\217\345\210\227.md" | 5 +- ...07\345\255\220\345\272\217\345\210\227.md" | 5 ++ ...55\345\255\220\345\272\217\345\210\227.md" | 12 ++- ...22\345\242\236\345\272\217\345\210\227.md" | 4 + ...27\344\275\231\350\277\236\346\216\245.md" | 70 ++++++++++------- ...\344\275\231\350\277\236\346\216\245II.md" | 25 +++++- ...15\345\255\220\346\225\260\347\273\204.md" | 5 ++ ...70\344\272\244\347\232\204\347\272\277.md" | 5 ++ ...61\345\255\220\345\272\217\345\210\227.md" | 23 +++--- ...30\345\234\250\350\267\257\345\276\204.md" | 78 ++++++++++--------- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- 13 files changed, 162 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index a31629607f..0172cb671b 100644 --- a/README.md +++ b/README.md @@ -492,7 +492,7 @@ 大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 -加入「代码随想录」刷题小分队(微信群),可以扫下方二维码加我微信。 +加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。 如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦** diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index c7d1b2fd7b..3117b1bf27 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -11,9 +11,14 @@ 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: -输入: [-2,1,-3,4,-1,2,1,-5,4] -输出: 6 -解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 +* 输入: [-2,1,-3,4,-1,2,1,-5,4] +* 输出: 6 +* 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 + +## 算法公开课 + +**《代码随想录》算法视频公开课:[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 6127f19045..8c82880d58 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -18,8 +18,9 @@ 提示: -0 <= s.length, t.length <= 1000 -s 和 t 由英文字母组成 +* 0 <= s.length, t.length <= 1000 +* s 和 t 由英文字母组成 + ## 思路 diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index e8cb0b5f18..e1f2bef814 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -31,6 +31,11 @@ * 1 <= nums.length <= 2500 * -10^4 <= nums[i] <= 104 +## 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 首先通过本题大家要明确什么是子序列,“子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序”。 diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 2fa647d200..c10114c05a 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -14,12 +14,12 @@ 字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。 示例 1: -输入:s = "abc", t = "ahbgdc" -输出:true +* 输入:s = "abc", t = "ahbgdc" +* 输出:true 示例 2: -输入:s = "axc", t = "ahbgdc" -输出:false +* 输入:s = "axc", t = "ahbgdc" +* 输出:false 提示: @@ -28,6 +28,10 @@ 两个字符串都只由小写字符组成。 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 79a8311d78..9f6601e72e 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -27,6 +27,10 @@ * 0 <= nums.length <= 10^4 * -10^9 <= nums[i] <= 10^9 +## 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index e3002b65ef..97f100d042 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -39,8 +39,8 @@ 这里整理出我的并查集模板如下: ```CPP -int n = 1005; // 节点数量3 到 1000 -int father[1005]; +int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好 +vector father = vector (n, 0); // C++里的一种数组结构 // 并查集初始化 void init() { @@ -50,40 +50,58 @@ void init() { } // 并查集里寻根的过程 int find(int u) { - return u == father[u] ? u : father[u] = find(father[u]); -} -// 将v->u 这条边加入并查集 -void join(int u, int v) { - u = find(u); - v = find(v); - if (u == v) return ; - father[v] = u; + return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 } + // 判断 u 和 v是否找到同一个根 -bool same(int u, int v) { +bool isSame(int u, int v) { u = find(u); v = find(v); return u == v; } + +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} + ``` -以上模板汇总,只要修改 n 和father数组的大小就可以了。 +以上模板 只要修改 n 就可以了,本题 节点数量不会超过1000。 并查集主要有三个功能。 1. 寻找根节点,函数:find(int u),也就是判断这个节点的祖先节点是哪个 2. 将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上 -3. 判断两个节点是否在同一个集合,函数:same(int u, int v),就是判断两个节点是不是同一个根节点 +3. 判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点 简单介绍并查集之后,我们再来看一下这道题目。 -题目说是无向图,返回一条可以删去的边,使得结果图是一个有着N个节点的树。 +题目说是无向图,返回一条可以删去的边,使得结果图是一个有着N个节点的树(即:只有一个根节点)。 如果有多个答案,则返回二维数组中最后出现的边。 -那么我们就可以从前向后遍历每一条边,边的两个节点如果不在同一个集合,就加入集合(即:同一个根节点)。 +那么我们就可以从前向后遍历每一条边(因为优先让前面的边连上),边的两个节点如果不在同一个集合,就加入集合(即:同一个根节点)。 + +如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104720.png) + +节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 -如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,如果再加入这条边一定就出现环了。 + +(如果题目中说:如果有多个答案,则返回二维数组中最前出现的边。 那我们就要 从后向前遍历每一条边了) + +如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了。 + +如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104330.png) + +已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 这个思路清晰之后,代码就很好写了。 @@ -93,7 +111,7 @@ bool same(int u, int v) { class Solution { private: int n = 1005; // 节点数量3 到 1000 - int father[1005]; + vector father = vector (n, 0); // C++里的一种数组结构 // 并查集初始化 void init() { @@ -105,24 +123,22 @@ private: int find(int u) { return u == father[u] ? u : father[u] = find(father[u]); } - // 将v->u 这条边加入并查集 - void join(int u, int v) { - u = find(u); - v = find(v); - if (u == v) return ; - father[v] = u; - } - // 判断 u 和 v是否找到同一个根,本题用不上 - bool same(int u, int v) { + // 判断 u 和 v是否找到同一个根 + bool isSame(int u, int v) { u = find(u); v = find(v); return u == v; } + // 将v->u 这条边加入并查集 + void join(int u, int v) { + if (isSame(u, v)) return ; + father[v] = u; + } public: vector findRedundantConnection(vector>& edges) { init(); for (int i = 0; i < edges.size(); i++) { - if (same(edges[i][0], edges[i][1])) return edges[i]; + if (isSame(edges[i][0], edges[i][1])) return edges[i]; else join(edges[i][0], edges[i][1]); } return {}; diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 5a0bd5d711..149eab0148 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -44,7 +44,6 @@ 且只有一个节点入度为2,为什么不看出度呢,出度没有意义,一棵树中随便一个父节点就有多个出度。 - 第三种情况是没有入度为2的点,那么图中一定出现了有向环(**注意这里强调是有向环!**) 如图: @@ -52,7 +51,27 @@ -首先先计算节点的入度,代码如下: +首先先计算节点的入度,这里不少录友在计算入度的时候就搞蒙了,分不清 edges[i][j] 表示的都是什么。 + +例如题目示例一给的是:edges = [[1,2],[1,3],[2,3]] + +那大家很自然就想 对应二维数组的数值是: edges[1][2] ,edges[1][3],edges[2][3],但又想不出来 edges[1][2] 数值又是什么呢? 越想约懵。 + +其实 edges = [[1,2],[1,3],[2,3]],表示的是 + +edges[0][0] = 1,edges[0][1] = 2, + +edges[1][0] = 1,edges[1][1] = 3, + +edges[2][0] = 2,edges[2][1] = 3, + +二维数组大家都学过,但是往往和图结合在一起的时候,就非常容易搞混,哪里是数组,哪里是下标了。 + +搞清楚之后,我们如何统计入度呢? + +即 edges[i][1] 表示的节点都是 箭头指向的节点,即这个几点有一个入度! (如果想统计出度,那么就是 edges[i][0])。 + +所以,统计入度的代码如下: ```cpp int inDegree[N] = {0}; // 记录节点入度 @@ -94,7 +113,7 @@ if (vec.size() > 0) { vector getRemoveEdge(const vector>& edges) ``` -此时 大家应该知道了,我们要实现两个最为关键的函数: +大家应该知道了,我们要实现两个最为关键的函数: * `isTreeAfterRemoveEdge()` 判断删一个边之后是不是树了 * `getRemoveEdge` 确定图中一定有了有向环,那么要找到需要删除的那条边 diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 08be67326e..d654f2ba26 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -23,6 +23,11 @@ * 1 <= len(A), len(B) <= 1000 * 0 <= A[i], B[i] < 100 +## 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 7b60abdda3..7142d75c09 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -17,6 +17,11 @@ ![1035.不相交的线](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032116363533.png) +## 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 相信不少录友看到这道题目都没啥思路,我们来逐步分析一下。 diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 730e9ad1f7..3799e81918 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -18,25 +18,30 @@ 示例 1: -输入:text1 = "abcde", text2 = "ace" -输出:3 -解释:最长公共子序列是 "ace",它的长度为 3。 +* 输入:text1 = "abcde", text2 = "ace" +* 输出:3 +* 解释:最长公共子序列是 "ace",它的长度为 3。 示例 2: -输入:text1 = "abc", text2 = "abc" -输出:3 -解释:最长公共子序列是 "abc",它的长度为 3。 +* 输入:text1 = "abc", text2 = "abc" +* 输出:3 +* 解释:最长公共子序列是 "abc",它的长度为 3。 示例 3: -输入:text1 = "abc", text2 = "def" -输出:0 -解释:两个字符串没有公共子序列,返回 0。 +* 输入:text1 = "abc", text2 = "def" +* 输出:0 +* 解释:两个字符串没有公共子序列,返回 0。 提示: * 1 <= text1.length <= 1000 * 1 <= text2.length <= 1000 输入的字符串只含有小写英文字符。 +## 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + ## 思路 本题和[动态规划:718. 最长重复子数组](https://programmercarl.com/0718.最长重复子数组.html)区别在于这里不要求是连续的了,但要有相对顺序,即:"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 3969456784..16c7cb1e85 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 1971. 寻找图中是否存在路径 [题目链接](https://leetcode.cn/problems/find-if-path-exists-in-graph/) @@ -30,7 +31,7 @@ ## 思路 -这道题目也是并查集基础题目。 +本题是并查集基础题目。 首先要知道并查集可以解决什么问题呢? @@ -39,8 +40,8 @@ 这里整理出我的并查集模板如下: ```CPP -int n = 1005; // 节点数量3 到 1000 -int father[1005]; +int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好 +vector father = vector (n, 0); // C++里的一种数组结构 // 并查集初始化 void init() { @@ -50,79 +51,86 @@ void init() { } // 并查集里寻根的过程 int find(int u) { - return u == father[u] ? u : father[u] = find(father[u]); -} -// 将v->u 这条边加入并查集 -void join(int u, int v) { - u = find(u); - v = find(v); - if (u == v) return ; - father[v] = u; + return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 } + // 判断 u 和 v是否找到同一个根 -bool same(int u, int v) { +bool isSame(int u, int v) { u = find(u); v = find(v); return u == v; } + +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} ``` -以上模板汇总,只要修改 n 和father数组的大小就可以了。 +以上模板中,只要修改 n 大小就可以,本科n不会超过2 * 10^5。 并查集主要有三个功能。 1. 寻找根节点,函数:find(int u),也就是判断这个节点的祖先节点是哪个 2. 将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上 -3. 判断两个节点是否在同一个集合,函数:same(int u, int v),就是判断两个节点是不是同一个根节点 +3. 判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点 简单介绍并查集之后,我们再来看一下这道题目。 -为什么说这道题目是并查集基础题目,因为 可以直接套用模板。 +为什么说这道题目是并查集基础题目,题目中各个点是双向图链接,那么判断 一个顶点到另一个顶点有没有有效路径其实就是看这两个顶点是否在同一个集合里。 + +如何算是同一个集合呢,有边连在一起,就算是一个集合。 + +此时我们就可以直接套用并查集模板。 使用join(int u, int v)将每条边加入到并查集。 -最后 same(int u, int v) 判断是否是同一个根 就可以里。 +最后 isSame(int u, int v) 判断是否是同一个根 就可以了。 -代码如下: +C++代码如下: -```c++ +```CPP class Solution { - private: - int n = 200005; // 节点数量 20000 - int father[200005]; + int n = 200005; // 节点数量 20000 + vector father = vector (n, 0); // C++里的一种数组结构 // 并查集初始化 void init() { - for (int i = 0; i < n; ++i) { - father[i] = i; + for (int i = 0; i < n; ++i) { father[i] = i; } } // 并查集里寻根的过程 int find(int u) { return u == father[u] ? u : father[u] = find(father[u]); } - // 将v->u 这条边加入并查集 - void join(int u, int v) { - u = find(u); - v = find(v); - if (u == v) return ; - father[v] = u; - } - // 判断 u 和 v是否找到同一个根,本题用不上 - bool same(int u, int v) { + + // 判断 u 和 v是否找到同一个根 + bool isSame(int u, int v) { u = find(u); v = find(v); return u == v; } + // 将v->u 这条边加入并查集 + void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; + } + public: bool validPath(int n, vector>& edges, int source, int destination) { - init(); + init(); for (int i = 0; i < edges.size(); i++) { - join(edges[i][0], edges[i][1]); + join(edges[i][0], edges[i][1]); } - return same(source, destination); + return isSame(source, destination); + } }; ``` diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 3afedc8249..da139e060c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -39,7 +39,7 @@ 什么是完全二叉树? -完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1) 个节点。 +完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层(h从1开始),则该层包含 1~ 2^(h-1) 个节点。 **大家要自己看完全二叉树的定义,很多同学对完全二叉树其实不是真正的懂了。** From 22615387a5966b8930076b08abe29ae22ce8ce8a Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 6 Jun 2023 10:25:25 -0500 Subject: [PATCH 0443/1533] =?UTF-8?q?Update=200309.=E6=9C=80=E4=BD=B3?= =?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E5=86=B7=E5=86=BB=E6=9C=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\345\206\267\345\206\273\346\234\237.md" | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index a56d9b8464..67f6d564c7 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -248,23 +248,51 @@ class Solution { ``` Python: - +版本一 ```python +from typing import List + class Solution: def maxProfit(self, prices: List[int]) -> int: n = len(prices) if n == 0: return 0 - dp = [[0] * 4 for _ in range(n)] - dp[0][0] = -prices[0] #持股票 + dp = [[0] * 4 for _ in range(n)] # 创建动态规划数组,4个状态分别表示持有股票、不持有股票且处于冷冻期、不持有股票且不处于冷冻期、不持有股票且当天卖出后处于冷冻期 + dp[0][0] = -prices[0] # 初始状态:第一天持有股票的最大利润为买入股票的价格 for i in range(1, n): - dp[i][0] = max(dp[i-1][0], max(dp[i-1][3], dp[i-1][1]) - prices[i]) - dp[i][1] = max(dp[i-1][1], dp[i-1][3]) - dp[i][2] = dp[i-1][0] + prices[i] - dp[i][3] = dp[i-1][2] - return max(dp[n-1][3], dp[n-1][1], dp[n-1][2]) + dp[i][0] = max(dp[i-1][0], max(dp[i-1][3], dp[i-1][1]) - prices[i]) # 当前持有股票的最大利润等于前一天持有股票的最大利润或者前一天不持有股票且不处于冷冻期的最大利润减去当前股票的价格 + dp[i][1] = max(dp[i-1][1], dp[i-1][3]) # 当前不持有股票且处于冷冻期的最大利润等于前一天持有股票的最大利润加上当前股票的价格 + dp[i][2] = dp[i-1][0] + prices[i] # 当前不持有股票且不处于冷冻期的最大利润等于前一天不持有股票的最大利润或者前一天处于冷冻期的最大利润 + dp[i][3] = dp[i-1][2] # 当前不持有股票且当天卖出后处于冷冻期的最大利润等于前一天不持有股票且不处于冷冻期的最大利润 + return max(dp[n-1][3], dp[n-1][1], dp[n-1][2]) # 返回最后一天不持有股票的最大利润 + ``` +版本二 +```python +class Solution: + def maxProfit(self, prices: List[int]) -> int: + n = len(prices) + if n < 2: + return 0 + + # 定义三种状态的动态规划数组 + dp = [[0] * 3 for _ in range(n)] + dp[0][0] = -prices[0] # 持有股票的最大利润 + dp[0][1] = 0 # 不持有股票,且处于冷冻期的最大利润 + dp[0][2] = 0 # 不持有股票,不处于冷冻期的最大利润 + + for i in range(1, n): + # 当前持有股票的最大利润等于前一天持有股票的最大利润或者前一天不持有股票且不处于冷冻期的最大利润减去当前股票的价格 + dp[i][0] = max(dp[i-1][0], dp[i-1][2] - prices[i]) + # 当前不持有股票且处于冷冻期的最大利润等于前一天持有股票的最大利润加上当前股票的价格 + dp[i][1] = dp[i-1][0] + prices[i] + # 当前不持有股票且不处于冷冻期的最大利润等于前一天不持有股票的最大利润或者前一天处于冷冻期的最大利润 + dp[i][2] = max(dp[i-1][2], dp[i-1][1]) + + # 返回最后一天不持有股票的最大利润 + return max(dp[-1][1], dp[-1][2]) +``` Go: ```go // 最佳买卖股票时机含冷冻期 动态规划 From d3a07fa1bd7c325188a416486daa7a85e6816680 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 6 Jun 2023 15:29:07 -0500 Subject: [PATCH 0444/1533] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...07\345\255\220\345\272\217\345\210\227.md" | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 01d349496b..98d7b14ffb 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -141,7 +141,7 @@ class Solution { } } ``` - +DP Python: ```python class Solution: @@ -157,7 +157,31 @@ class Solution: result = max(result, dp[i]) #取长的子序列 return result ``` +贪心 +```python +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + if len(nums) <= 1: + return len(nums) + + tails = [nums[0]] # 存储递增子序列的尾部元素 + for num in nums[1:]: + if num > tails[-1]: + tails.append(num) # 如果当前元素大于递增子序列的最后一个元素,直接加入到子序列末尾 + else: + # 使用二分查找找到当前元素在递增子序列中的位置,并替换对应位置的元素 + left, right = 0, len(tails) - 1 + while left < right: + mid = (left + right) // 2 + if tails[mid] < num: + left = mid + 1 + else: + right = mid + tails[left] = num + + return len(tails) # 返回递增子序列的长度 +``` Go: ```go // 动态规划求解 From a7d546ca2a6c7af9df378b3928c58f373177ed54 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Tue, 6 Jun 2023 15:29:32 -0500 Subject: [PATCH 0445/1533] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\212\345\215\207\345\255\220\345\272\217\345\210\227.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 98d7b14ffb..5424933180 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -141,8 +141,10 @@ class Solution { } } ``` -DP + Python: + +DP ```python class Solution: def lengthOfLIS(self, nums: List[int]) -> int: From 4fab08d0306d82aef04379ef01b9b08aae65543a Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 6 Jun 2023 19:09:57 -0400 Subject: [PATCH 0446/1533] =?UTF-8?q?=E6=8F=90=E4=BE=9BJAVA=E7=9A=842*2?= =?UTF-8?q?=E6=95=B8=E7=B5=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原本的空間優化是直接優化成一維數組,故提供一個2*2數組的版本 --- ...00\344\275\263\346\227\266\346\234\272.md" | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index 753cb10692..794185da90 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -243,8 +243,27 @@ class Solution { } } ``` +> 动态规划:版本二(使用二維數組(和卡哥思路一致),下面還有使用一維滾動數組的更優化版本) -> 动态规划:版本二 +```Java +class Solution { + public int maxProfit(int[] prices) { + int len = prices.length; + int dp[][] = new int[2][2]; + + dp[0][0] = - prices[0]; + dp[0][1] = 0; + + for (int i = 1; i < len; i++){ + dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], - prices[i]); + dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], prices[i] + dp[(i - 1) % 2][0]); + } + return dp[(len - 1) % 2][1]; + } +} +``` + +> 动态规划:版本二(使用一維數組) ``` java class Solution { @@ -271,6 +290,10 @@ class Solution { } } ``` +```Java + +``` + Python: From 9287b6be6f3dbcae30e3d5e8c636af24b41a3a7c Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 6 Jun 2023 19:25:22 -0400 Subject: [PATCH 0447/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9EJAVA=202*2=E6=95=B8?= =?UTF-8?q?=E7=B5=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit //DP using 2*2 Array (下方還有使用一維滾動數組的更優化版本) --- ...\201\350\247\204\345\210\222\357\274\211.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 146c6a4cb1..70ab8364c8 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -154,7 +154,24 @@ class Solution } } ``` +```java +//DP using 2*2 Array (下方還有使用一維滾動數組的更優化版本) +class Solution { + public int maxProfit(int[] prices) { + int dp[][] = new int [2][2]; + //dp[i][0]: holding the stock + //dp[i][1]: not holding the stock + dp[0][0] = - prices[0]; + dp[0][1] = 0; + for(int i = 1; i < prices.length; i++){ + dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]); + dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i]); + } + return dp[(prices.length - 1) % 2][1]; + } +} +``` ```java // 优化空间 class Solution { From bc229e302b2ab74f4345f18bae6626543d91e0fd Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 7 Jun 2023 08:58:27 +0800 Subject: [PATCH 0448/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=9B=BE=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 24 ++++++++++++++++++- ...75\347\232\204\350\267\257\345\276\204.md" | 4 ++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0172cb671b..65156a862f 100644 --- a/README.md +++ b/README.md @@ -387,10 +387,32 @@ 4. [单调栈:42.接雨水](./problems/0042.接雨水.md) 5. [单调栈:84.柱状图中最大的矩形](./problems/0084.柱状图中最大的矩形.md) -(持续更新中....) ## 图论 +通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor + +### 深搜广搜 + +* [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md) +* [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md) +* [图论:广度优先搜索理论基础](./problems/图论广索理论基础.md) +* [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md) +* [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md) +* [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md) +* [图论:1020.飞地的数量](./problems/1020.飞地的数量.md) +* [图论:130.被围绕的区域](./problems/0130.被围绕的区域.md) +* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md) +* [图论:827.最大人工岛](./problems/0827.最大人工岛.md) +* [图论:127.单词接龙](./problems/0127.单词接龙.md) +* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md) +* [图论:463.岛屿的周长](./problems/0463.岛屿的周长.md) + +### 并查集 + +(持续更新中....) + + ## 十大排序 ## 数论 diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index 89643e0490..2ea4ae4760 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -159,7 +159,7 @@ public: ## 其他语言版本 -## Java +Java ```Java // 深度优先遍历 @@ -190,7 +190,7 @@ class Solution { } ``` -## Python +Python ```python class Solution: def __init__(self): From c652187f747a2b7546f45e0f29cc985b929a8454 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 7 Jun 2023 03:59:52 -0500 Subject: [PATCH 0449/1533] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\242\236\345\272\217\345\210\227.md" | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 79a8311d78..0d9866a009 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -204,7 +204,7 @@ public static int findLengthOfLCIS(int[] nums) { Python: -> 动态规划: +DP ```python class Solution: def findLengthOfLCIS(self, nums: List[int]) -> int: @@ -219,8 +219,27 @@ class Solution: return result ``` +DP(优化版) +```python +class Solution: + def findLengthOfLCIS(self, nums: List[int]) -> int: + if not nums: + return 0 + + max_length = 1 + current_length = 1 -> 贪心法: + for i in range(1, len(nums)): + if nums[i] > nums[i - 1]: + current_length += 1 + max_length = max(max_length, current_length) + else: + current_length = 1 + + return max_length + +``` +贪心 ```python class Solution: def findLengthOfLCIS(self, nums: List[int]) -> int: From accc97cc06aa605499160cc2ba4804382647b977 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 7 Jun 2023 18:05:32 +0800 Subject: [PATCH 0450/1533] UPdate --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 65156a862f..dd70d8cb6b 100644 --- a/README.md +++ b/README.md @@ -403,12 +403,6 @@ * [图论:1020.飞地的数量](./problems/1020.飞地的数量.md) * [图论:130.被围绕的区域](./problems/0130.被围绕的区域.md) * [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md) -* [图论:827.最大人工岛](./problems/0827.最大人工岛.md) -* [图论:127.单词接龙](./problems/0127.单词接龙.md) -* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md) -* [图论:463.岛屿的周长](./problems/0463.岛屿的周长.md) - -### 并查集 (持续更新中....) From 8fb169701478cce21d5bfba761289479a0e27fbd Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 7 Jun 2023 05:26:40 -0500 Subject: [PATCH 0451/1533] =?UTF-8?q?Update=200718.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\255\220\346\225\260\347\273\204.md" | 96 +++++++++++++++---- 1 file changed, 79 insertions(+), 17 deletions(-) diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 08be67326e..78ef7ddd85 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -247,37 +247,99 @@ class Solution { Python: -> 动态规划: +2维DP ```python class Solution: - def findLength(self, A: List[int], B: List[int]) -> int: - dp = [[0] * (len(B)+1) for _ in range(len(A)+1)] + def findLength(self, nums1: List[int], nums2: List[int]) -> int: + # 创建一个二维数组 dp,用于存储最长公共子数组的长度 + dp = [[0] * (len(nums2) + 1) for _ in range(len(nums1) + 1)] + # 记录最长公共子数组的长度 result = 0 - for i in range(1, len(A)+1): - for j in range(1, len(B)+1): - if A[i-1] == B[j-1]: - dp[i][j] = dp[i-1][j-1] + 1 - result = max(result, dp[i][j]) + + # 遍历数组 nums1 + for i in range(1, len(nums1) + 1): + # 遍历数组 nums2 + for j in range(1, len(nums2) + 1): + # 如果 nums1[i-1] 和 nums2[j-1] 相等 + if nums1[i - 1] == nums2[j - 1]: + # 在当前位置上的最长公共子数组长度为前一个位置上的长度加一 + dp[i][j] = dp[i - 1][j - 1] + 1 + # 更新最长公共子数组的长度 + if dp[i][j] > result: + result = dp[i][j] + + # 返回最长公共子数组的长度 return result + ``` -> 动态规划:滚动数组 +1维DP ```python class Solution: - def findLength(self, A: List[int], B: List[int]) -> int: - dp = [0] * (len(B) + 1) + def findLength(self, nums1: List[int], nums2: List[int]) -> int: + # 创建一个一维数组 dp,用于存储最长公共子数组的长度 + dp = [0] * (len(nums2) + 1) + # 记录最长公共子数组的长度 result = 0 - for i in range(1, len(A)+1): - for j in range(len(B), 0, -1): - if A[i-1] == B[j-1]: - dp[j] = dp[j-1] + 1 + + # 遍历数组 nums1 + for i in range(1, len(nums1) + 1): + # 用于保存上一个位置的值 + prev = 0 + # 遍历数组 nums2 + for j in range(1, len(nums2) + 1): + # 保存当前位置的值,因为会在后面被更新 + current = dp[j] + # 如果 nums1[i-1] 和 nums2[j-1] 相等 + if nums1[i - 1] == nums2[j - 1]: + # 在当前位置上的最长公共子数组长度为上一个位置的长度加一 + dp[j] = prev + 1 + # 更新最长公共子数组的长度 + if dp[j] > result: + result = dp[j] else: - dp[j] = 0 #注意这里不相等的时候要有赋0的操作 - result = max(result, dp[j]) + # 如果不相等,将当前位置的值置为零 + dp[j] = 0 + # 更新 prev 变量为当前位置的值,供下一次迭代使用 + prev = current + + # 返回最长公共子数组的长度 return result + ``` +2维DP 扩展 +```python +class Solution: + def findLength(self, nums1: List[int], nums2: List[int]) -> int: + # 创建一个二维数组 dp,用于存储最长公共子数组的长度 + dp = [[0] * (len(nums2) + 1) for _ in range(len(nums1) + 1)] + # 记录最长公共子数组的长度 + result = 0 + + # 对第一行和第一列进行初始化 + for i in range(len(nums1)): + if nums1[i] == nums2[0]: + dp[i + 1][1] = 1 + for j in range(len(nums2)): + if nums1[0] == nums2[j]: + dp[1][j + 1] = 1 + + # 填充dp数组 + for i in range(1, len(nums1) + 1): + for j in range(1, len(nums2) + 1): + if nums1[i - 1] == nums2[j - 1]: + # 如果 nums1[i-1] 和 nums2[j-1] 相等,则当前位置的最长公共子数组长度为左上角位置的值加一 + dp[i][j] = dp[i - 1][j - 1] + 1 + if dp[i][j] > result: + # 更新最长公共子数组的长度 + result = dp[i][j] + + # 返回最长公共子数组的长度 + return result + +``` Go: ```Go func findLength(A []int, B []int) int { From 0a742d83a49e942324163fc0e7642cd1dfd7010f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Wed, 7 Jun 2023 05:32:55 -0500 Subject: [PATCH 0452/1533] =?UTF-8?q?Update=201143.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...61\345\255\220\345\272\217\345\210\227.md" | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 730e9ad1f7..4b71256982 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -198,21 +198,49 @@ class Solution { ``` Python: - +2维DP ```python class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: - len1, len2 = len(text1)+1, len(text2)+1 - dp = [[0 for _ in range(len1)] for _ in range(len2)] # 先对dp数组做初始化操作 - for i in range(1, len2): - for j in range(1, len1): # 开始列出状态转移方程 - if text1[j-1] == text2[i-1]: - dp[i][j] = dp[i-1][j-1]+1 + # 创建一个二维数组 dp,用于存储最长公共子序列的长度 + dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] + + # 遍历 text1 和 text2,填充 dp 数组 + for i in range(1, len(text1) + 1): + for j in range(1, len(text2) + 1): + if text1[i - 1] == text2[j - 1]: + # 如果 text1[i-1] 和 text2[j-1] 相等,则当前位置的最长公共子序列长度为左上角位置的值加一 + dp[i][j] = dp[i - 1][j - 1] + 1 else: - dp[i][j] = max(dp[i-1][j], dp[i][j-1]) - return dp[-1][-1] + # 如果 text1[i-1] 和 text2[j-1] 不相等,则当前位置的最长公共子序列长度为上方或左方的较大值 + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + + # 返回最长公共子序列的长度 + return dp[len(text1)][len(text2)] + ``` +1维DP +```python +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + m, n = len(text1), len(text2) + dp = [0] * (n + 1) # 初始化一维DP数组 + + for i in range(1, m + 1): + prev = 0 # 保存上一个位置的最长公共子序列长度 + for j in range(1, n + 1): + curr = dp[j] # 保存当前位置的最长公共子序列长度 + if text1[i - 1] == text2[j - 1]: + # 如果当前字符相等,则最长公共子序列长度加一 + dp[j] = prev + 1 + else: + # 如果当前字符不相等,则选择保留前一个位置的最长公共子序列长度中的较大值 + dp[j] = max(dp[j], dp[j - 1]) + prev = curr # 更新上一个位置的最长公共子序列长度 + + return dp[n] # 返回最后一个位置的最长公共子序列长度作为结果 +``` Go: ```Go From 5e1c10bfccc9409399872cbd0826461fee526e15 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 7 Jun 2023 19:45:25 +0800 Subject: [PATCH 0453/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\232\204\345\214\272\345\237\237.md" | 2 +- ...7.\346\267\261\346\220\234\347\211\210.md" | 2 +- ...64\346\265\201\351\227\256\351\242\230.md" | 2 +- ...77\347\232\204\345\221\250\351\225\277.md" | 3 + ...00\345\244\247\351\235\242\347\247\257.md" | 6 +- ...47\344\272\272\345\267\245\345\262\233.md" | 2 +- ...31\345\222\214\346\210\277\351\227\264.md" | 2 +- ...60\347\232\204\346\225\260\351\207\217.md" | 3 +- problems/qita/kstar.md | 131 ++++++++ problems/qita/xunlianying.md | 298 ++++++++++++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- 12 files changed, 444 insertions(+), 11 deletions(-) create mode 100644 problems/qita/kstar.md create mode 100644 problems/qita/xunlianying.md rename "problems/\345\233\276\350\256\272\345\271\277\347\264\242\347\220\206\350\256\272\345\237\272\347\241\200.md" => "problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" (97%) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index 7afa71b40f..abb68e199c 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -18,7 +18,7 @@ ## 思路 -这道题目和1020. 飞地的数量正好反过来了,[1020. 飞地的数量](https://leetcode.cn/problems/number-of-enclaves/solution/by-carlsun-2-7lt9/)是求 地图中间的空格数,而本题是要把地图中间的'O'都改成'X'。 +这道题目和1020. 飞地的数量正好反过来了,[1020. 飞地的数量](https://programmercarl.com/1020.%E9%A3%9E%E5%9C%B0%E7%9A%84%E6%95%B0%E9%87%8F.html)是求 地图中间的空格数,而本题是要把地图中间的'O'都改成'X'。 那么两题在思路上也是差不多的。 diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 8680d2b1a3..6d42162a57 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -41,7 +41,7 @@ ### 深度优先搜索 -以下代码使用dfs实现,如果对dfs不太了解的话,建议先看这篇题解:[797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/), +以下代码使用dfs实现,如果对dfs不太了解的话,建议先看这篇题解:[797.所有可能的路径](https://programmercarl.com/0797.%E6%89%80%E6%9C%89%E5%8F%AF%E8%83%BD%E7%9A%84%E8%B7%AF%E5%BE%84.html), C++代码如下: diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index ec22936577..35f3b4d6e3 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -140,7 +140,7 @@ public: 按照这样的逻辑,就可以写出如下遍历代码:(详细注释) -(如果对dfs基础内容就不懂,建议看 [「代码随想录」DFS算法精讲!](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/),还可以顺便解决 797. 所有可能的路径) +(如果对dfs基础内容就不懂,建议看 [「代码随想录」DFS算法精讲!](https://programmercarl.com/图论深搜理论基础.html),还可以顺便解决 [797. 所有可能的路径](https://programmercarl.com/0797.%E6%89%80%E6%9C%89%E5%8F%AF%E8%83%BD%E7%9A%84%E8%B7%AF%E5%BE%84.html)) ```CPP class Solution { diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 2c3f2ec756..2f399f4016 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -3,6 +3,9 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ + + # 463. 岛屿的周长 [力扣题目链接](https://leetcode.cn/problems/island-perimeter/) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 581f30a852..e5deb897d6 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -35,8 +35,8 @@ 本题思路上比较简单,难点其实都是 dfs 和 bfs的理论基础,关于理论基础我在这里都有详细讲解 : -* [DFS理论基础](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/) -* [BFS理论基础](https://leetcode.cn/circle/discuss/V3FulB/) +* [DFS理论基础](https://programmercarl.com/图论深搜理论基础.html) +* [BFS理论基础](https://programmercarl.com/图论广搜理论基础.html) ## DFS @@ -136,7 +136,7 @@ public: ## BFS -关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://leetcode.cn/circle/discuss/V3FulB/) +关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://programmercarl.com/图论广搜理论基础.html) 本题BFS代码如下: diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 45b2ef5ab9..9112fabde2 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -39,7 +39,7 @@ 每改变一个0的方格,都需要重新计算一个地图的最大面积,所以 整体时间复杂度为:n^4。 -如果对深度优先搜索不了解的录友,可以看这里:[深度优先搜索精讲](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/) +如果对深度优先搜索不了解的录友,可以看这里:[深度优先搜索精讲](https://programmercarl.com/图论深搜理论基础.html) ## 优化思路 diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index faf2b97f5d..00156d4ec3 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -56,7 +56,7 @@ 所以本题是一个有向图搜索全路径的问题。 只能用深搜(DFS)或者广搜(BFS)来搜。 -关于DFS的理论,如果大家有困惑,可以先看我这篇题解: [DFS理论基础](https://programmercarl.com/%E5%9B%BE%E8%AE%BA%E6%B7%B1%E6%90%9C%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html) +关于DFS的理论,如果大家有困惑,可以先看我这篇题解: [DFS理论基础](https://programmercarl.com/图论深搜理论基础.html) **以下dfs分析 大家一定要仔细看,本题有两种dfs的解法,很多题解没有讲清楚**。 看完之后 相信你对dfs会有更深的理解。 diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index f97678e872..e92b2412f3 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -42,7 +42,8 @@ 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 -如果对深搜或者广搜不够了解,建议先看这里:[深度优先搜索精讲](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/),[广度优先搜索精讲](https://leetcode.cn/circle/discuss/V3FulB/) +如果对深搜或者广搜不够了解,建议先看这里:[深度优先搜索精讲](https://programmercarl.com/图论深搜理论基础.html),[广度优先搜索精讲](https://programmercarl.com/图论广搜理论基础.html)。 + 采用深度优先搜索的代码如下: diff --git a/problems/qita/kstar.md b/problems/qita/kstar.md new file mode 100644 index 0000000000..86983cab18 --- /dev/null +++ b/problems/qita/kstar.md @@ -0,0 +1,131 @@ +# 代码随想录知识星球 + +前一阵知识星球刚刚发布了[星球精华-大厂八股文(第三版)](https://programmercarl.com/other/kstar_baguwen.html) + +这份八股文,就有30w字,将近400张思维导图,表格,分析图,整个PDF将近900页的篇幅。 + +这些其实都是星球录友们,每日打卡的内容,但这我也仅仅是整理了一部分,因为信息量确实巨大。 + +目前星球里已经有将近1000 个精华帖: + +
+ +同时还有[计算机2023届求职薪资PDF](https://programmercarl.com/other/2022salary.html)等一些列独家资料,都在星期置顶帖里: + +
+ +星球里的录友都可以得到我1V1的指导,**我已经详细回答了7000+个问题**: (这个回答问题数量,可以看出我有劳模的潜质 哈哈) + +
+ +有的时候,大家还是需要过来人,给指点一点,甚至是“踹一脚” 就会想清楚很多。 + +
+ +不仅我回答问题,我还会邀请星球里各个方向的录友来和大家一起交流具体技术问题,这个就是星球导师计划: (如果想提问的话,也在星球置顶1可以找到链接) + +
+ +
+ +可以看看星球导师计划里具体的问答: + +
+ +同时我还给录友们至少修改了上千份的简历,我也总结了很多大家写简历上问题。在 「写简历」这个tab上,可以找到我总结的所有问题和简历模板 + +
+ +【专业技能】【项目经验】【自我评价】都应该怎么写,面试时候 自我介绍,应该怎么说,我都给出了我的建议: + +
+ + +如果你还在犹豫要不要加入的话,**可以进来体验三天,三天内点击知识星球APP右上角,可以自助全额退款**。 绝对不会坑大家! +
+ + +一些录友当初也是进来 白嫖一波资料,就退款跑了 哈哈哈,不过后面又加回来,例如这位录友: + +
+ +**星球里的资料仅仅是辅助,更重要的是星球里的这一圈人,你会发现 这个圈子的质量非常高!** + +不仅仅是 **211、985录友非常之多**,关键是大家都非常努力上进! + +这是知识星球APP里可以看到,录友们的日常打卡: + +
+ +刷星球上的内容,要刷朋友圈,刷抖音,有意义的多。 + +
+ +
+ +星球网页版是这样的: + +
+ +加入星球,是很多录友当年做的最有意义的一件事情 + +
+
+
+
+ +可以看看星球里的交流氛围: + +
+ +
+ +
+ +
+ +
+ +大家的很多疑问在星球置顶3,我都做了详细的整理,录友都说我是“整理狂魔”,不过大家懒,我就得勤劳一些。 + +
+ +星球置顶3的信息量非常大,不仅仅是整理各个求职方向的学习路线,还有大家的常见疑惑,我之前回答过的内容,都做了整理。 + +大家看完之后,其实对自己就会有明确的规划了。 + +
+ +给大家看看星球置顶帖3的部分内容,以下仅仅是部分截图: + +
+ +
+ +
+ +
+ + +大家加入星球后,一定要看星球置顶帖和精华帖的内容,你会发现这里很有优秀录友的帖子,包括:各种资料,学习路线,学习心得,规划,职场发展等等。 + +
+ +很多录友看完之后都更加明确了自己的方向。 + +
+ + +相对于其他星球,「代码随想录」知识星球到底怎么样,可以看看录友们是怎么说的。 + +
+ +最后也欢迎大家加入代码随想录[知识星球](https://mp.weixin.qq.com/s/wPaJumc8afuzWLo72yRlIw),**这里有很多优秀的人,有很多精彩的事!** + +
+ +这里依然给出10元代金券,微信扫领代金券加入,如果感觉不值得,**三天内知识星球APP右上角直接全额退款!** 无任何套路。 + +
+ + diff --git a/problems/qita/xunlianying.md b/problems/qita/xunlianying.md new file mode 100644 index 0000000000..0d8c1c83a6 --- /dev/null +++ b/problems/qita/xunlianying.md @@ -0,0 +1,298 @@ + +# 代码随想录算法训练营 + +> 训练营17期将在6月28日开营,目前可以报名,提前拉群,在群里等着开营就好! + +大家可以百度搜索:代码随想录算法训练营, 看看往期录友们在训练营里打卡总结的博客。 + +
+ +这是训练营里录友坚持到最后一天的打卡,大家可以看看他们的博客是每天都有记录的: + +* [训练营结束,深感坚持是最难的(Java-犯困-东南研二)](https://blog.csdn.net/weixin_57956443/article/details/128995318) +* [训练营一刷总结(Java-HQH-研二)](https://blog.csdn.net/weixin_43821876/article/details/128991822) +* [训练营总结,一群人才能走的更远(Java-Lixy-已工作南京)](https://blog.csdn.net/weixin_45368277/article/details/128997823) +* [训练营总结,中途🐑了,也坚持下来(C++-Jane-科大研二)](https://blog.csdn.net/Jane_10358/article/details/128977424) +* [这两个月有很多不可控因素,但依然坚持下来(java-hha-南工大二)](https://blog.csdn.net/qerwtrt4t/article/details/128975401) +* [训练营总结,最后坚持下来(C++ - 阿舟 - 已工作武汉)](https://blog.csdn.net/m0_74360161/article/details/129000723) +* [训练营总结,一刷知识点回顾(Java-魏-待就业)](https://blog.csdn.net/weixin_48111139/article/details/128973746) +* [在训练营中,零基础刷一遍的感受(C++-东风-东北大学研二)](https://blog.csdn.net/nightcood/article/details/128947111) + + +博客链接:[https://blog.csdn.net/m0_61724447/article/details/128443084](https://blog.csdn.net/m0_61724447/article/details/128443084) +
+ +博客链接:[https://juejin.cn/post/7170304080504586254](https://juejin.cn/post/7170304080504586254) +
+ +博客链接:[https://blog.csdn.net/weixin_44047621/article/details/128430623](https://blog.csdn.net/weixin_44047621/article/details/128430623) +
+ +博客链接:[https://blog.csdn.net/weixin_47467016/article/details/128460565](https://blog.csdn.net/weixin_47467016/article/details/128460565v) +
+ +也有一些录友,把总结发在训练营内部打卡表里,例如: + +昵称:java-低调-已工作 + +通过两个月的时间系统性的学习了算法,然后按照不同的题目去做分类,设计的刷题进度也很好,让自己有了一个质的提升,贵在坚持,好在自己也是坚持了下来,**通过自己的坚持,让自己养成了一个刷题的好习惯,这才是最难能可贵的**。 + +但是时间跨度有点大,还是要继续坚持之后自己去二刷,这样才能更好的巩固,把算法知识学习的更好。 + +--------- + +昵称:java-岂几岂几-毕业 + +收获真的很大,这是第一次刷算法题,清楚了面试高频题的题型,**巩固了之前摇摇欲坠的自学算法基础**。接下来计划是重刷随想录,并且补充上一亩三分地刷题区置顶贴里列出的题型,在巩固一刷的基础上增加做题量。 + +------------ + +昵称:python/go-ds-研三 + +跟着卡哥的训练营最大的收获就是把代码随想录都通读了一遍,因为进营之前就已经刷过不少力扣题了,但很多都是当时自己捣鼓出来或者看官方题解的。 + +而这一次的60天刷题,不管题目做没做过,都看过卡哥的代码随想录了,**这其中的区别也是最大的收获就是知识体系建立起来了**,越往后做题,条理越清晰。 + +即使有些题一刷还是做不太出来,但不再像之前自己做那样做题前后都是懵逼状态了,而是有一个清晰明了的判断了。 + +但coding能力还是有待改进,接下来要进行二刷,同时也祝卡哥的事业蒸蒸日上,代码随想录越办越好! + +------------ + +昵称:Python-ukn-研二 + +完美收官,有点小遗憾的是后面dp做得有点赶,没有沉下心来消化,接下来重点把自己不擅长的专题和重点专题二刷甚至三刷。 + +**跟着训练营练下来最大的感受是很有信心,有节奏有计划**,每过完一个专题,就多一分成就感,题感也越来越好,期待自己的规律二刷,谢谢一路坚持的小伙伴们!谢谢大佬助手和卡哥! + + +----------- + + +### 训练营的目的是什么? + +对于刷题,学算法,[《代码随想录》](https://programmercarl.com/other/publish.html)(programmercarl.com)已经把刷题顺序给大家列好了,大家跟着刷就行。 + +但即使这样,其实不少录友还会有很多疑问,不知道怎么用代码随想录,例如: + +* 卡哥,**有没有一起从0开始刷代码随想录的录友,想一起组个队** +* 卡哥您好,我是985准研一非科班,自学java, 然后现在在刷代码随想录,**请问需要每个题目的所有解法都掌握吗**?请教下卡哥正确的刷题姿势🙏 +* **我大概多久才能刷完代码随想录**? +* 二叉树,我只掌握 递归够用么? +* 很多解法,我是不是只用暴力就可以,**时间比较紧,我还要去掌握优化方法吗**? +* 卡哥,**请问跟着代码随想录刷题有答疑的服务吗**? 因为有的题目 自己写的怎么都不对,浪费很多时间,可能过来人指点一下立刻就知道。 +* 卡哥,我KMP太难了,我跳过可以吗? +* 卡哥,我进了刷题群,可是**大家刷题进度不尽相同,所以讨论起来经常不在一个频道上**。 +* 刚开始还看了一周代码随想录,后来又..摆烂了... **最近又重新再看代码随想录,然后卡住了又摆烂了好几天了**...... +* 卡哥,**我刷题很容易囫囵吞枣,虽然说代码随想录一刷,但很多内容根本没消化,在进度上欺骗自己**,好像一刷完了,但感觉自己理解的,不到30%。 +* 卡哥,**感觉之前刷的都忘了,能力没有什么提升,现在还是一道都不会做**。我一刷每道题都得先看看题解然后忘了再去看边看边写。 + +**以上这些是不是有戳中某些录友们的痛处**。 + +其实对于很多算法基础不太好的录友,即使资料已经很齐全,但还是需要一些规划和答疑。 + +而且在时间规划上,因为刚开始刷的录友,不知道 前方题目 是多大难度,所以 一开始计划 一天刷三道,往往因为遇到了一道难题,一天也解决不了,耽误了整体进度,甚至直接开始摆烂,下次再开始刷题可能就很久以后了...... + +所以 **代码随想录算法训练营** 帮助大家在规划时间内,有质量的完成代码随想录一刷。 + +我亲自给大家规划节奏,大家一起按照我的节奏来,规定时间内,一刷一定能把代码随想录所有内容吃透,然后大家自己去二刷,三刷就好了,师傅领进门修行在个人。 + +### 训练营提供一些什么呢? + +1.具体内容 + +针对代码随想录上,**195篇算法文章,主要题目150道**,手把手带大家刷完,帮大家做好详细刷题规划,每天布置刷题任务,监督博客记录总结。 + +任务布置 +
+ +每日规划: +
+ +训练营周期内,每天应该做哪些题目,同时我根据题目的难度,适当调整每天学习安排,不会是 每天固定3题的这种,而是根据难度而定。 + +我会告诉大家,哪些解法是一刷的时候必须掌握的,哪些解法可以二刷再去学习,哪些总结是必看的。 + +每日打卡: +
+ +关于如果debug自己的代码,训练营会给具体建议: +
+ +训练营群中每日讨论的重点内容都会做整理,在分享给大家训练营成员: +
+ +**同时每天做针对大家的疑问做详细答疑,保证大家消化当天的学习内容**。 + +2.**气氛气氛还是气氛** + +训练营中,**大家都是同一个基础,同一个进度刷题,每天刷题题目都是一样的**,这样的一个学习群,大家讨论起来更有意义。 +**还有会监督机制**,训练营的成员要注册一个自己的博客(自己搭建或者使用博客网站都可以),每天要去写今日刷题心得和总结,来进行打卡。 + +
+ +3.带大家写博客 + +很多录友平时刷题,或者学习技术,没有写博客的习惯,或者因为懒,就不写了。 + +但大家学了很多技术之后,发现 好像都忘了。。。 + +所以训练营会带着大家写博客,每天都要写博客,博客的标题,格式,我都帮大家规划好,倒逼自己养生记录的习惯。 + +因为训练营很多录友开始有了写博客的习惯,以下是一些录友博客的结尾部分: + +
+ +
+ +每天训练营群里会每天统计大家的博客情况。这样不仅可以监督自己总结,针对大家写的比较好的博客,会给予曝光,增加自己写博客的动力。 + +训练营里的录友们可以相互参考对方的博客,看谁总结的更好。 + +
+ + +4.关于答疑 + +很多录友可能担心自己的问题,得不到解决,或者在群里和大家讨论,也没人回复 导致自己因为小问题卡了很久,甚至直接摆烂好一阵子。 + +所以训练营里大家的问题,我都会做答疑。 + +估计训练营里的问题会比较多,我也可能回答不过来。所以我会找了算法能力很强的助手协助我给大家答疑,也就是说,**大家刷题遇到问题,不会有后顾之忧,当天的问题,当天一定会得到解决**。 + +
+ +当然训练营题目答疑,**仅限于 每天规划的题目**,并不会大家刷的其他算法题都做答疑,那样的话工作量很容易不可控(这里我也不会夸大承诺,欺骗大家报名之类的),如果是其他算法题可以在群里和大家交流。 + +### 训练营的资料是什么呢? + +**强调一下:训练营里所有的资料,都是我独立制作而且是开源免费的:即代码随想录网站(programmercarl.com),Github:https://github.com/youngyangyang04/leetcode-master和[代码随想录算法公开课](https://mp.weixin.qq.com/s/xncn6IHJGs45sJOChN6V_g)** + +训练营提供给大家的服务是**规划,监督,指导和答疑**。 + +至于代码随想录算法内容的质量如何,这个已经是有口皆碑了,基本是面试求职必刷的资料。 + +
+ +
+ +而且代码随想录开源的内容要比市面上 大家付费几百,上千元报的算法训练营的资料都要好的多。 + +毕竟内容是开源的,质量如何 大家自己去看就好。 + +### 训练营的学习方式 + +组织方式:一个学习微信群(180人左右),大家进群之后,等群公告就好,我会通知开始时间和每日刷题计划。 + +所需时间:训练营为期60天(两个月),群里每天会布置学习任务,只要大家跟上节奏,60天一定可以刷完代码随想录。 + +每日任务:需要花费3-4个小时左右的时间来完成。这是针对一般算法水平的学习速度来规划的时间,不同水平会有差异。 + +每周周日会休息一天,没跟上进度的录友,可以跟进度,跟上进度的录友可以复习或者适当放松一下。 + +监督机制:训练营里,每天会针对大家每天所刷的题目做答疑,同时也会有监督打卡机制,在群公告里会详细描述。 + +所需语言:**所有语种都可以**,毕竟代码随想录几乎支持所有主流语言,**也会针对大家所用的语言做针对性答疑**。 + +### 开营时间 + +**训练营开始常态化报名,即一直可以报名,当人满180人的时候,就开始新的一期**。 最新的一期可以看文章评论区。 + +### 训练营的价格 + +大家应该最关心的是价格了,**定价依然是268元**,注意这是两个月训练营的费用,而且是全程规划,指导,监督和答疑。 + +(对于[知识星球](https://programmercarl.com/other/kstar.html)里的录友的话,训练营会立减30元,也就是238元,后面如果推出其他服务,星球录友都相当于VIP,都会有优惠。当然如果你已经报了训练营,再去报知识星球,并不给再给大家优惠了,一定要先是星球成员,再报训练营才有优惠) + +大家能在市面上找到算法训练营都价格不菲,基本都是上千的单价,**而且内容和质量并没有 代码随想录 优质**。 + +后面一定会涨价的,**如果你确实需要有人带,有监督,给规划,有答疑,能花两个月时间更下来的话,还是早报早学习**。 + +### 我适合报名吗? + +符合一下特点的录友可以报名: + +* 基础比较差,没刷过代码随想录或者刚开始刷 +* 刷过一些代码随想录的题目了,感觉掌握不扎实,想用2个月时间系统重刷一遍 +* 自己刷题,**很容易遇到各种代码问题,需要有人答疑** +* 以前一刷过,但基本都忘了,想高质量二刷 +* **想找队友,一起从0刷代码随想录** +* 自控能力差,遇到点问题就容易躺好一阵子,需要别人监督学习 +* **想有一个规划时间,来刷完代码随想录** +* 不知道代码随想录中哪些解法是必备的,哪些解法是可以简单了解的 +* 刷题总会忘,感觉刷了和没刷差不多,**不擅长做总结,不擅长写博客记录心得**,自己也懒得写博客写总结 + +以下录友不合适报名: + +* 自学能力强,代码随想录资料都是开源的,刷题顺序也列好了,自学能力强的录友自己学就行 +* 有算法和代码基础,基本算法题遇到的问题,都能通过自己debug解决 +* 没有两个月时间,每天也不能抽出那么多时间学习算法 +* 算法0基础,基本的数据结构都没听说过,例如数组,链表。 +* 编程0基础,基本的编程语言还不会,因为训练营还是默认大家会熟悉所用编程语言里的各种容器的使用 + +**训练营不限编程语言**,任何语言都可以报名,都会答疑。 + + +### 常见疑问 + +**海外录友有时差可以报名吗**? + +可以的,一期就有很多海外的录友,有疑问在群里也会回复,而且群里讨论的重点内容,都会有总结,不用担心错过了精彩内容。 + +**已经工作的录友适合报名吗**? + +适合报名。对于工作的录友,每天未必说一定挤出3-4个小时来刷题。 + +对于时间充足的录友,要刷拓展题,要写博客作总结。 如果时间紧张,任务上是可以适当精简。 + +所以每日任务弹性还是比较大的,至少跟上进度保证每天的题目代码提交通过了,看看群里的讨论,自己理解加深了就可以。 + +工作的录友要学会挤时间,训练营一期录友有不少是工作的,他们是这么搞的: + +训练营每日晚上提前发布明天的任务, 他们第二天通勤 时候 可以先看题想思路,白天抽空看文章解析 看看思路是否一致,看看群里讨论内容,晚上下班可以一口气把当天的题目刷完。 + +加入训练营,每日对自己有一个压迫感,挤一挤 时间就有了。 + +对于工作的录友,我之前本来计划是安排一期 工作日题量小 休息日题量多一些的训练营,但通过一期发现,包括已经工作的录友,**大家休息日真的没有心思学习,甚至“比工作日更忙”**,所以理想很丰满,现实很骨感。 + +**要不要搞三个月四个月半年的训练营**? + +目前来看四个月以上的时间有点太长了,时间长价格也会高,毕竟要服务的时间长了。 + +而且刷题在于一鼓作气,把时间拉的太长,很多录友都是前期 动力十足,后面无论是 如何@ 如何公告 如果催大家 赶进度,大家都会无动于衷,从最终效果来看 战线不能太长。 + +所以没有逼自己一把 跟上进度的决心,就算搞一年时间的训练营,该放弃的还是会放弃。 + +至于三个月的训练营,是可以考虑的,不过安排时间还要待定。 + +### 报名方式 + +扫码支付268元。 (如果是[代码随想录知识星球](https://programmercarl.com/other/kstar.html)成员录友,只需要支付238元,提交客服的时候需提供知识星球截图,**注意一定要是代码随想录知识星球**) + +
+ +付款后,将付款截图发给客服,客服会在24h内统一回复,**所以大家发给客服信息不要急,当天一定会回复的**。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230603175235.png) + +客服的联系方式就在大家的微信聊天窗口,不用担心突出聊天窗口错过消息,客服回复之后 会有微信提示的。 + +关于训练营的任何问题,可以在客服这里咨询! + + +### 最后 + +训练营其实算是代码随想录的一个补充,其内容都是免费开放的,有学习能力的录友自己学习就好。 + +单就从我的 [代码随想录算法公开课](https://mp.weixin.qq.com/s/xncn6IHJGs45sJOChN6V_g) 来说,质量如何,大家可以去看评论区,我完全可以把它做成付费的视频课,但我还是选择免费开放给大家,目前一周会更新四个算法视频,已经快把二叉树系列更完了。 + +之所以做训练营,是因为大家太多的问题,不是视频或者文章教程可以解决的,需要的是规划,组织,监督和答疑。 + +所以我才组织训练营,搞成付费的也是为了质量更高一些,同时也是因为需要一些门槛,要不然就和普通刷题群没什么区别了。 + +等大家跟着代码随想录训练营一路走下来之后,大家再回顾自己两个月学习的内容和总结的博客,**一定会发现 这个价格 物超所值**! + +关于训练营的任何疑问都可以扫码联系客服 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230603175235.png) + diff --git "a/problems/\345\233\276\350\256\272\345\271\277\347\264\242\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" similarity index 97% rename from "problems/\345\233\276\350\256\272\345\271\277\347\264\242\347\220\206\350\256\272\345\237\272\347\241\200.md" rename to "problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index d1bb14e055..1f3a13728f 100644 --- "a/problems/\345\233\276\350\256\272\345\271\277\347\264\242\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -8,7 +8,7 @@ > 号外!!代码随想录图论内容已经计划开更了! -在[深度优先搜索](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 +在[深度优先搜索](https://programmercarl.com/图论深搜理论基础.html)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 广搜(bfs)是一圈一圈的搜索过程,和深搜(dfs)是一条路跑到黑然后在回溯。 diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 9f965abb2a..eb6a26fe6d 100644 --- "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,7 +6,7 @@ # 深度优先搜索理论基础 -录友们期待图论内容已久了,为什么鸽了这么久,主要是最近半年开始更新[代码随想录算法公开课](https://mp.weixin.qq.com/s/xncn6IHJGs45sJOChN6V_g),是开源在B站的算法视频,已经帮助非常多基础不好的录友学习算法。 +录友们期待图论内容已久了,为什么鸽了这么久,主要是最近半年开始更新[代码随想录算法公开课](https://www.bilibili.com/video/BV1fA4y1o715/),是开源在B站的算法视频,已经帮助非常多基础不好的录友学习算法。 录视频其实是非常累的,也要花很多时间,所以图论这边就没抽出时间来。 From 17a01cecefcdb2b71806a56bc6789686bcb755ac Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 8 Jun 2023 17:38:49 -0400 Subject: [PATCH 0454/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A8=BB?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java註解 if statement的解釋 --- ...5\250\350\203\214\345\214\205\347\211\210\346\234\254.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 8f8bc9a649..bc5e350908 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -133,12 +133,13 @@ Java: class Solution { public int climbStairs(int n) { int[] dp = new int[n + 1]; - int m = 2; + int m = 2; //有兩個物品:itme1重量爲一,item2重量爲二 dp[0] = 1; for (int i = 1; i <= n; i++) { // 遍历背包 for (int j = 1; j <= m; j++) { //遍历物品 - if (i >= j) dp[i] += dp[i - j]; + if (i >= j) //當前的背包容量 大於 物品重量的時候,我們才需要記錄當前的這個裝得方法(方法數+) + dp[i] += dp[i - j]; } } From 652fa2bbb0a43799365df33544bf13638a6b8d20 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Fri, 9 Jun 2023 18:11:29 -0400 Subject: [PATCH 0455/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=202*2=20array?= =?UTF-8?q?=20solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 12789934ff..1443f14710 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -154,6 +154,25 @@ class Solution { return dp[1]; } } +```Java +//使用 2*2 array +class Solution { + public int maxProfit(int[] prices, int fee) { + int dp[][] = new int[2][2]; + int len = prices.length; + //[i][0] = holding the stock + //[i][1] = not holding the stock + dp[0][0] = -prices[0]; + + for(int i = 1; i < len; i++){ + dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]); + dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i] - fee); + } + + return dp[(len - 1) % 2][1]; + } +} +``` ``` Python: From 88162c3a77ad266002158c448a6dc4f934bb9dc6 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Fri, 9 Jun 2023 18:27:21 -0400 Subject: [PATCH 0456/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=202*4=20soluti?= =?UTF-8?q?on=20=E4=B8=A6=E9=99=84=E4=B8=8A=20=E5=B0=8D=E6=96=BC2-D=20arra?= =?UTF-8?q?y,=201-D=20array=E7=9A=84=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\345\206\267\345\206\273\346\234\237.md" | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index a56d9b8464..83cd448acf 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -200,7 +200,33 @@ class Solution { } } ``` - +```java +//using 2*4 array for space optimization +//這裡稍微說一下,我在LeetCode提交的時候,2*4 2-D array的performance基本上和下面的1-D array performance差不多 +//都是time: 1ms, space: 40.X MB (其實 length*4 的 2-D array也僅僅是space:41.X MB,看起來不多) +//股票累的DP題目大致上都是這樣,就當作是一個延伸就好了。真的有人問如何優化,最起碼有東西可以講。 +class Solution { + /** + 1. [i][0] holding the stock + 2. [i][1] after cooldown but stil not buing the stock + 3. [i][2] selling the stock + 4. [i][3] cooldown + */ + public int maxProfit(int[] prices) { + int len = prices.length; + int dp[][] = new int [2][4]; + dp[0][0] = -prices[0]; + + for(int i = 1; i < len; i++){ + dp[i % 2][0] = Math.max(Math.max(dp[(i - 1) % 2][0], dp[(i - 1) % 2][1] - prices[i]), dp[(i - 1) % 2][3] - prices[i]); + dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][3]); + dp[i % 2][2] = dp[(i - 1) % 2][0] + prices[i]; + dp[i % 2][3] = dp[(i - 1) % 2][2]; + } + return Math.max(Math.max(dp[(len - 1) % 2][1], dp[(len - 1) % 2][2]), dp[(len - 1) % 2][3]); + } +} +``` ```java // 一维数组优化 class Solution { From cef6de49f42d50d0c877f962f7355b48317510b0 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sat, 10 Jun 2023 16:54:54 +0800 Subject: [PATCH 0457/1533] debug --- ...684.\345\206\227\344\275\231\350\277\236\346\216\245.md" | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 97f100d042..177338dd6d 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -131,9 +131,11 @@ private: } // 将v->u 这条边加入并查集 void join(int u, int v) { - if (isSame(u, v)) return ; + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 father[v] = u; - } +} public: vector findRedundantConnection(vector>& edges) { init(); From a32bbeb71a37153276d69412526819493ffe3bd5 Mon Sep 17 00:00:00 2001 From: Feegg <1468434504@qq.com> Date: Sun, 11 Jun 2023 02:13:01 +0800 Subject: [PATCH 0458/1533] =?UTF-8?q?=E5=AF=B90707=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=EF=BC=8Cgo=E7=89=88=E6=9C=AC=E5=8D=95?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=81=9A=E4=BA=86=E4=BF=AE=E6=94=B9=EF=BC=8C?= =?UTF-8?q?=E5=8E=9F=E6=9D=A5=E7=89=88=E6=9C=AC=E6=B2=A1=E6=9C=89=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E6=8C=87=E5=AE=9Aindex=E4=B8=8B?= =?UTF-8?q?=E6=A0=87=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C=E5=B9=B6=E4=B8=94?= =?UTF-8?q?=E5=B7=B2=E6=9C=89=E7=9A=84=E5=9C=A8leetcode=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=9C=89=E8=AF=AF=EF=BC=8C=E5=81=9A=E4=BA=86=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=B0=83=E6=95=B4=EF=BC=8C=E5=9F=BA=E4=BA=8E=E5=8E=9F=E6=9D=A5?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\351\223\276\350\241\250.md" | 146 +++++++++++------- 1 file changed, 87 insertions(+), 59 deletions(-) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index aa04d0e1af..b9f2795238 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -663,100 +663,128 @@ Go: //单链表实现 package main -import "fmt" +import ( + "fmt" +) -func main() { - var list = new(SingleLinkedList) - list.Init() - list.addAtHead(100) - list.addAtTail(242) - list.addAtTail(777) - list.addAtIndex(1, 99999) - list.printLinkedList() +type SingleNode struct { + Val int // 节点的值 + Next *SingleNode // 下一个节点的指针 } -// 单链表写法 // - -type SingleNode struct { - Val int - Next *SingleNode +type MyLinkedList struct { + dummyHead *SingleNode // 虚拟头节点 + Size int // 链表大小 } -type SingleLinkedList struct { - dummyHead *SingleNode - Size int +func main() { + list := Constructor() // 初始化链表 + list.AddAtHead(100) // 在头部添加元素 + list.AddAtTail(242) // 在尾部添加元素 + list.AddAtTail(777) // 在尾部添加元素 + list.AddAtIndex(1, 99999) // 在指定位置添加元素 + list.printLinkedList() // 打印链表 } -// 初始化链表 -func (list *SingleLinkedList) Init() *SingleLinkedList { - list.Size = 0 - list.dummyHead = new(SingleNode) - return list +/** Initialize your data structure here. */ +func Constructor() MyLinkedList { + newNode := &SingleNode{ // 创建新节点 + -999, + nil, + } + return MyLinkedList{ // 返回链表 + dummyHead: newNode, + Size: 0, + } + } -// 获取第index个节点数值 -func (list *SingleLinkedList) get(index int) int { - if list != nil || index < 0 || index > list.Size { +/** Get the value of the index-th node in the linked list. If the index is + invalid, return -1. */ +func (this *MyLinkedList) Get(index int) int { + /*if this != nil || index < 0 || index > this.Size { + return -1 + }*/ + if this == nil || index < 0 || index >= this.Size { // 如果索引无效则返回-1 return -1 } // 让cur等于真正头节点 - cur := list.dummyHead.Next - for i := 0; i < index; i++ { + cur := this.dummyHead.Next // 设置当前节点为真实头节点 + for i := 0; i < index; i++ { // 遍历到索引所在的节点 cur = cur.Next } - return cur.Val + return cur.Val // 返回节点值 } -// 在链表最前面插入一个节点 -func (list *SingleLinkedList) addAtHead(val int) { +/** Add a node of value val before the first element of the linked list. After + the insertion, the new node will be the first node of the linked list. */ +func (this *MyLinkedList) AddAtHead(val int) { // 以下两行代码可用一行代替 // newNode := new(SingleNode) // newNode.Val = val - newNode := &SingleNode{Val: val} - - newNode.Next = list.dummyHead.Next - list.dummyHead.Next = newNode - list.Size++ + newNode := &SingleNode{Val: val} // 创建新节点 + newNode.Next = this.dummyHead.Next // 新节点指向当前头节点 + this.dummyHead.Next = newNode // 新节点变为头节点 + this.Size++ // 链表大小增加1 } -// 在链表最后面插入一个节点 -func (list *SingleLinkedList) addAtTail(val int) { - newNode := &SingleNode{Val: val} - cur := list.dummyHead - for cur.Next != nil { +/** Append a node of value val to the last element of the linked list. */ +func (this *MyLinkedList) AddAtTail(val int) { + newNode := &SingleNode{Val: val} // 创建新节点 + cur := this.dummyHead // 设置当前节点为虚拟头节点 + for cur.Next != nil { // 遍历到最后一个节点 cur = cur.Next } - cur.Next = newNode - list.Size++ + cur.Next = newNode // 在尾部添加新节点 + this.Size++ // 链表大小增加1 } -// 打印链表 -func (list *SingleLinkedList) printLinkedList() { - cur := list.dummyHead - for cur.Next != nil { - fmt.Println(cur.Next.Val) +/** Add a node of value val before the index-th node in the linked list. If + index equals to the length of linked list, the node will be appended to the + end of linked list. If index is greater than the length, the node will not be + inserted. */ +func (this *MyLinkedList) AddAtIndex(index int, val int) { + if index < 0 { // 如果索引小于0,设置为0 + index = 0 + } else if index > this.Size { // 如果索引大于链表长度,直接返回 + return + } + + newNode := &SingleNode{Val: val} // 创建新节点 + cur := this.dummyHead // 设置当前节点为虚拟头节点 + for i := 0; i < index; i++ { // 遍历到指定索引的前一个节点 cur = cur.Next } + newNode.Next = cur.Next // 新节点指向原索引节点 + cur.Next = newNode // 原索引的前一个节点指向新节点 + this.Size++ // 链表大小增加1 } -// 在第index个节点之前插入新节点 -func (list *SingleLinkedList) addAtIndex(index int, val int) { - if index < 0 { - index = 0 - } else if index > list.Size { +/** Delete the index-th node in the linked list, if the index is valid. */ +func (this *MyLinkedList) DeleteAtIndex(index int) { + if index < 0 || index >= this.Size { // 如果索引无效则直接返回 return } - - newNode := &SingleNode{Val: val} - cur := list.dummyHead //用虚拟头节点不用考虑在头部插入的情况 - for i := 0; i < index; i++ { + cur := this.dummyHead // 设置当前节点为虚拟头节点 + for i := 0; i < index; i++ { // 遍历到要删除节点的前一个节点 cur = cur.Next } - newNode.Next = cur.Next - cur.Next = newNode - list.Size++ + if cur.Next != nil { + cur.Next = cur.Next.Next // 当前节点直接指向下下个节点,即删除了下一个节点 + } + this.Size-- // 注意删除节点后应将链表大小减一 +} + +// 打印链表 +func (list *MyLinkedList) printLinkedList() { + cur := list.dummyHead // 设置当前节点为虚拟头节点 + for cur.Next != nil { // 遍历链表 + fmt.Println(cur.Next.Val) // 打印节点值 + cur = cur.Next // 切换到下一个节点 + } } + ``` ```go From 50ed10475df9c62dae9e149a867a6f083a0dde04 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 12 Jun 2023 01:59:52 -0500 Subject: [PATCH 0459/1533] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\210\254\346\245\274\346\242\257.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index cd5e40e63d..9eaaa4e9fe 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -282,7 +282,35 @@ class Solution: return dp1 # 返回到达楼顶的最小花费 ``` +动态规划(版本三) +```python +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + dp = [0] * len(cost) + dp[0] = cost[0] # 第一步有花费 + dp[1] = cost[1] + for i in range(2, len(cost)): + dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i] + # 注意最后一步可以理解为不用花费,所以取倒数第一步,第二步的最少值 + return min(dp[-1], dp[-2]) + +``` +动态规划(版本四) + +```python +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + n = len(cost) + prev_1 = cost[0] # 前一步的最小花费 + prev_2 = cost[1] # 前两步的最小花费 + for i in range(2, n): + current = min(prev_1, prev_2) + cost[i] # 当前位置的最小花费 + prev_1, prev_2 = prev_2, current # 更新前一步和前两步的最小花费 + return min(prev_1, prev_2) # 最后一步可以理解为不用花费,取倒数第一步和第二步的最少值 + + +``` ### Go ```Go func minCostClimbingStairs(cost []int) int { From 5527410f890ecfccdb017ce5aa5fc902328ab922 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 12 Jun 2023 02:59:20 -0500 Subject: [PATCH 0460/1533] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\214\350\267\257\345\276\204II.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 7b5b44f00c..cb305b4189 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -397,7 +397,39 @@ class Solution: ``` +动态规划(版本五) +```python +class Solution: + def uniquePathsWithObstacles(self, obstacleGrid): + if obstacleGrid[0][0] == 1: + return 0 + + m, n = len(obstacleGrid), len(obstacleGrid[0]) + + dp = [0] * n # 创建一个一维列表用于存储路径数 + + # 初始化第一行的路径数 + for j in range(n): + if obstacleGrid[0][j] == 1: + break + dp[j] = 1 + + # 计算其他行的路径数 + for i in range(1, m): + if obstacleGrid[i][0] == 1: + dp[0] = 0 + for j in range(1, n): + if obstacleGrid[i][j] == 1: + dp[j] = 0 + continue + + dp[j] += dp[j - 1] + + return dp[-1] # 返回最后一个元素,即终点的路径数 + + +``` ### Go ```go From 072eb2aa471f025afdb690e57a23c0932b5a4697 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 12 Jun 2023 03:32:07 -0500 Subject: [PATCH 0461/1533] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index d70641dbe9..3ff8dedb03 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -262,10 +262,11 @@ class Solution: # 计算切割点j和剩余部分(i-j)的乘积,并与之前的结果进行比较取较大值 - dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j)) + dp[i] = max(dp[i], (i - j) * j, dp[i - j] * j) return dp[n] # 返回最终的计算结果 + ``` 动态规划(版本二) ```python From b1e8b17eb1bfd4e620b49037154693d4f9188a96 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Wed, 14 Jun 2023 18:30:56 -0400 Subject: [PATCH 0462/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E4=B8=80?= =?UTF-8?q?=E7=B6=AD=E6=95=B8=E7=B5=84=E8=A7=A3=E6=B3=95=EF=BC=88=E5=BE=88?= =?UTF-8?q?=E5=8D=A1=E5=93=A5=E9=82=8F=E8=BC=AF=E4=B8=80=E8=87=B4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原本的那個版本不知道在寫什麼 --- ...\344\275\263\346\227\266\346\234\272IV.md" | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 5bed5ecc5d..14f514a98f 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -227,7 +227,7 @@ class Solution { } } -//版本三:一维 dp数组 +//版本三:一维 dp数组 (下面有和卡哥邏輯一致的一維數組JAVA解法) class Solution { public int maxProfit(int k, int[] prices) { if(prices.length == 0){ @@ -259,6 +259,41 @@ class Solution { } } ``` +```JAVA +class Solution { + public int maxProfit(int k, int[] prices) { + + //edge cases + if(prices.length == 0 || k == 0) + return 0; + + + int dp[] = new int [k * 2 + 1]; + + //和卡哥邏輯一致,奇數天購入股票,故初始化只初始化奇數天。 + for(int i = 1; i < 2 * k + 1; i += 2){ + dp[i] = -prices[0]; + } + + for(int i = 1; i < prices.length; i++){ //i 從 1 開始,因爲第 i = 0 天已經透過初始化完成了。 + for(int j = 1; j < 2 * k + 1; j++){ //j 從 1 開始,因爲第 j = 0 天已經透過初始化完成了。 + //奇數天購買 + if(j % 2 == 1) + dp[j] = Math.max(dp[j], dp[j - 1] - prices[i]); + //偶數天賣出 + else + dp[j] = Math.max(dp[j], dp[j - 1] + prices[i]); + } + //打印DP數組 + //for(int x : dp) + // System.out.print(x +", "); + //System.out.println(); + } + //return 第2 * k次賣出的獲利。 + return dp[2 * k]; + } +} +``` Python: From 9c1988edec9d6eeca9db1bd558ad16410f40eaf6 Mon Sep 17 00:00:00 2001 From: Binbin Date: Thu, 15 Jun 2023 19:32:34 +0800 Subject: [PATCH 0463/1533] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9A=E5=B7=B2=E7=BB=8F=E5=A1=AB=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E4=B8=B2=20->=20=E5=B7=B2=E7=BB=8F=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\211\262\345\233\236\346\226\207\344\270\262.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 636cf59c4b..92fed58a2e 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -118,7 +118,7 @@ for (int i = startIndex; i < s.size(); i++) { continue; } backtracking(s, i + 1); // 寻找i+1为起始位置的子串 - path.pop_back(); // 回溯过程,弹出本次已经填在的子串 + path.pop_back(); // 回溯过程,弹出本次已经添加的子串 } ``` @@ -189,7 +189,7 @@ private: continue; } backtracking(s, i + 1); // 寻找i+1为起始位置的子串 - path.pop_back(); // 回溯过程,弹出本次已经填在的子串 + path.pop_back(); // 回溯过程,弹出本次已经添加的子串 } } bool isPalindrome(const string& s, int start, int end) { @@ -245,7 +245,7 @@ private: continue; } backtracking(s, i + 1); // 寻找i+1为起始位置的子串 - path.pop_back(); // 回溯过程,弹出本次已经填在的子串 + path.pop_back(); // 回溯过程,弹出本次已经添加的子串 } } void computePalindrome(const string& s) { @@ -437,7 +437,7 @@ class Solution: substring = s[startIndex:i + 1] path.append(substring) self.backtracking(s, i + 1, path, result, isPalindrome) # 寻找i+1为起始位置的子串 - path.pop() # 回溯过程,弹出本次已经填在的子串 + path.pop() # 回溯过程,弹出本次已经添加的子串 def computePalindrome(self, s, isPalindrome): for i in range(len(s) - 1, -1, -1): # 需要倒序计算,保证在i行时,i+1行已经计算好了 @@ -497,7 +497,7 @@ func dfs(s string, start int) { if isPalindrome(str) { // 是回文子串 path = append(path, str) dfs(s, i+1) // 寻找i+1为起始位置的子串 - path = path[:len(path)-1] // 回溯过程,弹出本次已经填在的子串 + path = path[:len(path)-1] // 回溯过程,弹出本次已经添加的子串 } } } From 3becfab93f477723e4c3aee75da45989bebd0bb6 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 15 Jun 2023 17:58:09 -0400 Subject: [PATCH 0464/1533] =?UTF-8?q?=E8=AA=AA=E6=98=8EJAVA=20code?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=B3=A8=E6=84=8F=E7=9A=84=E5=9C=B0=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 81f02ace98..8cc270ec64 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -177,6 +177,7 @@ Java: dp[i] = 1; } int res = 1; + //可以注意到,這邊的 i 是從 0 開始,所以會出現和卡哥的C++ code有差異的地方,在一些地方會看到有 i + 1 的偏移。 for (int i = 0; i < nums.length - 1; i++) { if (nums[i + 1] > nums[i]) { dp[i + 1] = dp[i] + 1; From 018c9cb96399a5005f6ed42e0eb69ebc07f24d97 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 15 Jun 2023 18:46:08 -0400 Subject: [PATCH 0465/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=202-D=20array?= =?UTF-8?q?=20=E5=BB=BA=E8=AD=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\254\345\205\261\345\255\220\345\272\217\345\210\227.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 2b5eed3d2d..68269b87c5 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -144,6 +144,11 @@ Java: */ class Solution { public int longestCommonSubsequence(String text1, String text2) { + // char[] char1 = text1.toCharArray(); + // char[] char2 = text2.toCharArray(); + // 可以在一開始的時候就先把text1, text2 轉成char[],之後就不需要有這麼多爲了處理字串的調整 + // 就可以和卡哥的code更一致 + int[][] dp = new int[text1.length() + 1][text2.length() + 1]; // 先对dp数组做初始化操作 for (int i = 1 ; i <= text1.length() ; i++) { char char1 = text1.charAt(i - 1); From 342e7a597eb50c1570fdd713a237b761ab777bd6 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Fri, 16 Jun 2023 20:58:06 -0400 Subject: [PATCH 0466/1533] =?UTF-8?q?=E5=8A=A0=E5=85=A5B=E7=AB=99=E8=A6=96?= =?UTF-8?q?=E9=A0=BB=E7=B6=B2=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\210\240\351\231\244\346\223\215\344\275\234.md" | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 561ad2f238..054f452bf9 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -14,10 +14,15 @@ * 输入: "sea", "eat" * 输出: 2 -* 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea" +* 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea" -## 思路 +# 算法公开课 + +**《代码随想录》算法视频公开课:[动态规划之子序列,还是为了编辑距离做铺垫 | LeetCode:583.两个字符串的删除操(https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +## 思路 ### 动态规划一 本题和[动态规划:115.不同的子序列](https://programmercarl.com/0115.不同的子序列.html)相比,其实就是两个字符串都可以删除了,情况虽说复杂一些,但整体思路是不变的。 From c25a76587ec1028b0ab80607c2dcd14a15f18475 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Fri, 16 Jun 2023 21:23:27 -0400 Subject: [PATCH 0467/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=EF=BC=9A?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E9=95=B7=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8F=8D=E6=8E=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java:用最長公共子序列反推 --- ...40\351\231\244\346\223\215\344\275\234.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 561ad2f238..3e225e8250 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -184,6 +184,31 @@ class Solution { } } ``` +```java +//DP - longest common subsequence (用最長公共子序列反推) +class Solution { + public int minDistance(String word1, String word2) { + char[] char1 = word1.toCharArray(); + char[] char2 = word2.toCharArray(); + + int len1 = char1.length; + int len2 = char2.length; + + int dp[][] = new int [len1 + 1][len2 + 1]; + + for(int i = 1; i <= len1; i++){ + for(int j = 1; j <= len2; j++){ + if(char1[i - 1] == char2[j - 1]) + dp[i][j] = dp[i - 1][j - 1] + 1; + else + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + + return len1 + len2 - (2 * dp[len1][len2]);//和leetcode 1143只差在這一行。 + } +} +``` Python: From 517d9def4429b836bdb8e890d5f46309b0d7c0d0 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Sun, 18 Jun 2023 18:39:06 -0400 Subject: [PATCH 0468/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9EB=E7=AB=99=E5=BD=B1?= =?UTF-8?q?=E7=89=87=E7=B6=B2=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增B站影片網址 --- .../0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index cc4ab00cdc..703e891311 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -40,6 +40,8 @@ exection -> execution (插入 'u') * 0 <= word1.length, word2.length <= 500 * word1 和 word2 由小写英文字母组成 +# 算法公开课 +**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 080dd35c79d0f4fe670ee0dd049c8422d67f94be Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:15:43 +0800 Subject: [PATCH 0469/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0047.\345\205\250\346\216\222\345\210\227II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index 6999b732dc..afede33a46 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -101,7 +101,7 @@ public: // 时间复杂度: 最差情况所有元素都是唯一的。复杂度和全排列1都是 O(n! * n) 对于 n 个元素一共有 n! 中排列方案。而对于每一个答案,我们需要 O(n) 去复制最终放到 result 数组 // 空间复杂度: O(n) 回溯树的深度取决于我们有多少个元素 ``` -* 时间复杂度: O(n) +* 时间复杂度: O(n! * n) * 空间复杂度: O(n) ## 拓展 From 346e927f6c8a98594b444233dede3bca8f6ad5eb Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:51:21 -0400 Subject: [PATCH 0470/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=20for=20leetcode=205.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java解法 for leetcode 5. 感覺卡哥可以提一下,其實在下一偏有提到,但是真的有關聯的這一篇沒有提到:這一題稍微改一下就能通過兩題,這一點我沒有寫在這個fork上。 --- ...36\346\226\207\345\255\220\344\270\262.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index e172de54d9..084b9f74e6 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -304,7 +304,38 @@ class Solution { } } ``` +LeetCode 5. Longest Palindromic Substring(LeetCode 647. 同一題的思路改一下、加一點,就能通過LeetCode 5) +```java +class Solution { + public String longestPalindrome(String s) { + //題目要求要return 最長的回文連續子串,故需要記錄當前最長的連續回文子串長度、最終起點、最終終點。 + int finalStart = 0; + int finalEnd = 0; + int finalLen = 0; + + char[] chars = s.toCharArray(); + int len = chars.length; + boolean[][] dp = new boolean[len][len]; + for (int i = len - 1; i >= 0; i--) { + for (int j = i; j < len; j++) { + if (chars[i] == chars[j] && (j - i <= 1 || dp[i + 1][j - 1])) + dp[i][j] = true; + //和LeetCode 647,差別就在這個if statement。 + //如果當前[i, j]範圍內的substring是回文子串(dp[i][j]) 且(&&) 長度大於當前要記錄的最終長度(j - i + 1 > finalLen) + //我們就更新 當前最長的連續回文子串長度、最終起點、最終終點 + if (dp[i][j] && j - i + 1 > finalLen) { + finalLen = j - i + 1; + finalStart = i; + finalEnd = j; + } + } + } + //String.substring這個method的用法是[起點, 終點),包含起點,不包含終點(左閉右開區間),故終點 + 1。 + return s.substring(finalStart, finalEnd + 1); + } +} +``` Python: From 1f31f092f1ec713a6df0f7617c39bc2107a9ba5f Mon Sep 17 00:00:00 2001 From: Ao Liu <63785048+Ao-Last@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:38:00 +0800 Subject: [PATCH 0471/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?.md=20md=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 101dd6f255..4ea43947e8 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -163,6 +163,7 @@ class Solution(object): a_count = Counter(s) b_count = Counter(t) return a_count == b_count +``` Go: From b92fcc936e4515fd922e4f00ecfd0e0816f275b4 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 22 Jun 2023 03:13:04 -0400 Subject: [PATCH 0472/1533] =?UTF-8?q?=E8=A7=A3=E6=B1=BA=E8=B7=91=E6=9D=BF?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=20+=20=E6=8F=90=E4=BE=9Bjava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95(=E5=92=8C=E5=8D=A1=E5=93=A5=E9=82=8F=E8=BC=AF?= =?UTF-8?q?=E4=B8=80=E8=87=B4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 跑版: 原本的code box 結束的```不見了,所以看不到結尾圖片。 新解法和原本JAVA解法的差異: 原版的應該是自己寫的DFS,邏輯大致一致,但還是有差異,故提供用卡哥C++ code改的版本 --- ...7.\346\267\261\346\220\234\347\211\210.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 6d42162a57..c30ace1999 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -176,6 +176,48 @@ public void dfs(char[][] grid, int i, int j){ dfs(grid,i,j + 1); dfs(grid,i,j - 1); } +``` +```java +//graph - dfs (和卡哥的代碼邏輯一致) +class Solution { + boolean[][] visited; + int dir[][] = { + {0, 1}, //right + {1, 0}, //down + {-1, 0}, //up + {0, -1} //left + }; + public int numIslands(char[][] grid) { + int count = 0; + visited = new boolean[grid.length][grid[0].length]; + + for(int i = 0; i < grid.length; i++){ + for(int j = 0; j < grid[0].length; j++){ + if(visited[i][j] == false && grid[i][j] == '1'){ + count++; + dfs(grid, i, j); + } + } + } + return count; + } + + private void dfs(char[][]grid, int x, int y){ + if(visited[x][y] == true || grid[x][y] == '0') + return; + + visited[x][y] = true; + + for(int i = 0; i < 4; i++){ + int nextX = x + dir[i][0]; + int nextY = y + dir[i][1]; + if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) + continue; + dfs(grid, nextX, nextY); + } + } +} +```

From 35a87bd56c87a642cd28f119be3814fe0e1bf63f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 23 Jun 2023 12:14:15 -0500 Subject: [PATCH 0473/1533] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\211\345\222\214\345\255\220\351\233\206.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 1f12931274..a1158d7eba 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -324,6 +324,21 @@ class Solution: return True return False +``` + +卡哥版(简化版) +```python +class Solution: + def canPartition(self, nums: List[int]) -> bool: + if sum(nums) % 2 != 0: + return False + target = sum(nums) // 2 + dp = [0] * (target + 1) + for num in nums: + for j in range(target, num-1, -1): + dp[j] = max(dp[j], dp[j-num] + num) + return dp[-1] == target + ``` 二维DP版 ```python From 71980306bf6f653145c0c0f0313a02bd8dd1a77f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 23 Jun 2023 12:58:24 -0500 Subject: [PATCH 0474/1533] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8F?= =?UTF-8?q?II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\264\347\232\204\351\207\215\351\207\217II.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index a978b80262..932029ab7a 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -238,6 +238,21 @@ class Solution: return total_sum - dp[target] - dp[target] +``` + +卡哥版(简化版) +```python +class Solution: + def lastStoneWeightII(self, stones): + total_sum = sum(stones) + target = total_sum // 2 + dp = [0] * (target + 1) + for stone in stones: + for j in range(target, stone - 1, -1): + dp[j] = max(dp[j], dp[j - stone] + stone) + return total_sum - 2* dp[-1] + + ``` 二维DP版 ```python From 33eca1635693f64d0eeb6d7c477093665dded20b Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Sat, 24 Jun 2023 20:18:36 -0400 Subject: [PATCH 0475/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 調整語言順序 2. 新增java - DFS, java - BFS解法 --- ...00\345\244\247\351\235\242\347\247\257.md" | 162 ++++++++++++++---- 1 file changed, 129 insertions(+), 33 deletions(-) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index e5deb897d6..37a601bcf8 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -189,6 +189,135 @@ public: ``` # 其它语言版本 +## Java +### DFS +```java +// DFS +class Solution { + int[][] dir = { + {0, 1}, //right + {1, 0}, //down + {0, -1}, //left + {-1, 0} //up + }; + boolean visited[][]; + int count; + public int maxAreaOfIsland(int[][] grid) { + int res = 0; + visited = new boolean[grid.length][grid[0].length]; + for(int i = 0; i < grid.length; i++){ + for(int j = 0; j < grid[0].length; j++){ + if(visited[i][j] == false && grid[i][j] == 1){ + count = 0; + dfs(grid, i, j); + res = Math.max(res, count); + } + } + } + return res; + } + private void dfs(int[][] grid, int x, int y){ + if(visited[x][y] == true || grid[x][y] == 0) + return; + + visited[x][y] = true; + count++; + + for(int i = 0; i < 4; i++){ + int nextX = x + dir[i][0]; + int nextY = y + dir[i][1]; + + if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) + continue; + dfs(grid, nextX, nextY); + } + } +} + + +``` +### BFS +```java +//BFS +class Solution { + int[][] dir = { + {0, 1}, {1, 0}, {0, -1}, {-1, 0} + }; + + int count; + boolean visited[][]; + + public int maxAreaOfIsland(int[][] grid) { + int res = 0; + visited = new boolean[grid.length][grid[0].length]; + + for(int i = 0; i < grid.length; i++){ + for(int j = 0; j < grid[0].length; j++){ + if(visited[i][j] == false && grid[i][j] == 1){ + count = 0; + bfs(grid, i, j); + res = Math.max(res, count); + } + } + } + return res; + } + private void bfs(int[][] grid, int x, int y){ + Queue que = new LinkedList<>(); + que.offer(x); + que.offer(y); + visited[x][y] = true; + count++; + + while(!que.isEmpty()){ + int currX = que.poll(); + int currY = que.poll(); + + for(int i = 0; i < 4; i++){ + int nextX = currX + dir[i][0]; + int nextY = currY + dir[i][1]; + + if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) + continue; + if(visited[nextX][nextY] == false && grid[nextX][nextY] == 1){ + que.offer(nextX); + que.offer(nextY); + visited[nextX][nextY] = true; + count++; + } + } + } + } +} +``` +### DFS 優化(遇到島嶼後,就把他淹沒) +```java +//这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。 +public int maxAreaOfIsland(int[][] grid) { + int res = 0; + for(int i = 0;i < grid.length;i++){ + for(int j = 0;j < grid[0].length;j++){ + //每遇到一个岛屿就计算这个岛屿的面积同时”淹没“这个岛屿 + if(grid[i][j] == 1){ + //每次计算一个岛屿的面积都要与res比较,维护最大的岛屿面积作为最后的答案 + res = Math.max(res,dfs(grid,i,j)); + } + } + } + return res; +} +public int dfs(int[][] grid,int i,int j){ + //搜索边界:i,j超过grid的范围或者当前元素为0,即当前所在的地方已经是海洋 + if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) return 0; + //淹没土地,防止后续被重复计算 + grid[i][j] = 0; + //递归的思路:要求当前土地(i,j)所在的岛屿的面积,则等于1加上下左右相邻的土地的总面积 + return 1 + dfs(grid,i - 1,j) + + dfs(grid,i + 1,j) + + dfs(grid,i,j + 1) + + dfs(grid,i,j - 1); +} +``` ## Python ### BFS @@ -261,39 +390,6 @@ class Solution: if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]): self.dfs(grid, visited, new_x, new_y) ``` - - - -## Java - -这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。 - -```java -public int maxAreaOfIsland(int[][] grid) { - int res = 0; - for(int i = 0;i < grid.length;i++){ - for(int j = 0;j < grid[0].length;j++){ - //每遇到一个岛屿就计算这个岛屿的面积同时”淹没“这个岛屿 - if(grid[i][j] == 1){ - //每次计算一个岛屿的面积都要与res比较,维护最大的岛屿面积作为最后的答案 - res = Math.max(res,dfs(grid,i,j)); - } - } - } - return res; -} -public int dfs(int[][] grid,int i,int j){ - //搜索边界:i,j超过grid的范围或者当前元素为0,即当前所在的地方已经是海洋 - if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) return 0; - //淹没土地,防止后续被重复计算 - grid[i][j] = 0; - //递归的思路:要求当前土地(i,j)所在的岛屿的面积,则等于1加上下左右相邻的土地的总面积 - return 1 + dfs(grid,i - 1,j) + - dfs(grid,i + 1,j) + - dfs(grid,i,j + 1) + - dfs(grid,i,j - 1); -} -```

From 2b3ce4453520a16bac6d65c2b3295cc65b90f00e Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Sat, 24 Jun 2023 23:38:40 -0400 Subject: [PATCH 0476/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\232\204\346\225\260\351\207\217.md" | 126 +++++++++++++++++- 1 file changed, 124 insertions(+), 2 deletions(-) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index e92b2412f3..7538d774d2 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -149,7 +149,63 @@ public: ### Java -深度优先遍历版本: +深度优先遍历(没有终止条件 + 空間優化(淹沒島嶼,沒有使用visited數組)) +```java +//DFS +class Solution { + int count = 0; + int[][] dir ={ + {0, 1}, + {1, 0}, + {-1, 0}, + {0, -1} + }; + private void dfs(int[][] grid, int x, int y){ + if(grid[x][y] == 0) + return; + + grid[x][y] = 0; + count++; + + for(int i = 0; i < 4; i++){ + int nextX = x + dir[i][0]; + int nextY = y + dir[i][1]; + + if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) + continue; + dfs(grid, nextX, nextY); + } + + } + + public int numEnclaves(int[][] grid) { + for(int i = 0; i < grid.length; i++){ + if(grid[i][0] == 1) + dfs(grid, i, 0); + if(grid[i][grid[0].length - 1] == 1) + dfs(grid, i, grid[0].length - 1); + } + //初始化的時候,j 的上下限有調整過,必免重複操作。 + for(int j = 1; j < grid[0].length - 1; j++){ + if(grid[0][j] == 1) + dfs(grid, 0, j); + if(grid[grid.length - 1][j] == 1) + dfs(grid, grid.length - 1, j); + } + count = 0; + + for(int i = 1; i < grid.length - 1; i++){ + for(int j = 1; j < grid[0].length - 1; j++){ + if(grid[i][j] == 1) + dfs(grid, i, j); + } + } + return count; + } +} +``` + +深度优先遍历(没有终止条件) ```java class Solution { @@ -206,7 +262,7 @@ class Solution { } ``` -广度优先遍历版本: +广度优先遍历(使用visited數組) ```java class Solution { @@ -269,6 +325,72 @@ class Solution { } ``` +廣度优先遍历(空間優化(淹沒島嶼,沒有使用visited數組)) +```java +//BFS +class Solution { + int count = 0; + int[][] dir ={ + {0, 1}, + {1, 0}, + {-1, 0}, + {0, -1} + }; + private void bfs(int[][] grid, int x, int y){ + Queue que = new LinkedList<>(); + que.offer(x); + que.offer(y); + count++; + grid[x][y] = 0; + + while(!que.isEmpty()){ + int currX = que.poll(); + int currY = que.poll(); + + for(int i = 0; i < 4; i++){ + int nextX = currX + dir[i][0]; + int nextY = currY + dir[i][1]; + + if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) + continue; + + if(grid[nextX][nextY] == 1){ + que.offer(nextX); + que.offer(nextY); + count++; + grid[nextX][nextY] = 0; + } + } + } + } + + public int numEnclaves(int[][] grid) { + for(int i = 0; i < grid.length; i++){ + if(grid[i][0] == 1) + bfs(grid, i, 0); + if(grid[i][grid[0].length - 1] == 1) + bfs(grid, i, grid[0].length - 1); + } + for(int j = 1; j < grid[0].length; j++){ + if(grid[0][j] == 1) + bfs(grid, 0 , j); + if(grid[grid.length - 1][j] == 1) + bfs(grid, grid.length - 1, j); + } + count = 0; + for(int i = 1; i < grid.length - 1; i++){ + for(int j = 1; j < grid[0].length - 1; j++){ + if(grid[i][j] == 1) + bfs(grid,i ,j); + } + } + return count; + } +} + + +``` + ### Python 深度优先遍历 From cdc5a1e9e47690fee5c5f5b34d66f643d6e08907 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Mon, 26 Jun 2023 21:37:58 -0400 Subject: [PATCH 0477/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9EJAVA=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. java的BFS解法有寫一個helper function去呼叫 2. java's DFS with 終止條件 --- ...25\347\232\204\345\214\272\345\237\237.md" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index abb68e199c..e244873ba3 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -188,6 +188,54 @@ class Solution { } } ``` +```Java +//BFS(使用helper function) +class Solution { + int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + public void solve(char[][] board) { + for(int i = 0; i < board.length; i++){ + if(board[i][0] == 'O') bfs(board, i, 0); + if(board[i][board[0].length - 1] == 'O') bfs(board, i, board[0].length - 1); + } + + for(int j = 1 ; j < board[0].length - 1; j++){ + if(board[0][j] == 'O') bfs(board, 0, j); + if(board[board.length - 1][j] == 'O') bfs(board, board.length - 1, j); + } + + for(int i = 0; i < board.length; i++){ + for(int j = 0; j < board[0].length; j++){ + if(board[i][j] == 'O') board[i][j] = 'X'; + if(board[i][j] == 'A') board[i][j] = 'O'; + } + } + } + private void bfs(char[][] board, int x, int y){ + Queue que = new LinkedList<>(); + board[x][y] = 'A'; + que.offer(x); + que.offer(y); + + while(!que.isEmpty()){ + int currX = que.poll(); + int currY = que.poll(); + + for(int i = 0; i < 4; i++){ + int nextX = currX + dir[i][0]; + int nextY = currY + dir[i][1]; + + if(nextX < 0 || nextY < 0 || nextX >= board.length || nextY >= board[0].length) + continue; + if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A') + continue; + bfs(board, nextX, nextY); + } + } + } +} + +``` + ```Java // 深度优先遍历 // 使用 visited 数组进行标记 @@ -296,6 +344,47 @@ class Solution { } } ``` +```java +//DFS(有終止條件) +class Solution { + int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + public void solve(char[][] board) { + + for(int i = 0; i < board.length; i++){ + if(board[i][0] == 'O') dfs(board, i, 0); + if(board[i][board[0].length - 1] == 'O') dfs(board, i, board[0].length - 1); + } + + for(int j = 1 ; j < board[0].length - 1; j++){ + if(board[0][j] == 'O') dfs(board, 0, j); + if(board[board.length - 1][j] == 'O') dfs(board, board.length - 1, j); + } + + for(int i = 0; i < board.length; i++){ + for(int j = 0; j < board[0].length; j++){ + if(board[i][j] == 'O') board[i][j] = 'X'; + if(board[i][j] == 'A') board[i][j] = 'O'; + } + } + } + + private void dfs(char[][] board, int x, int y){ + if(board[x][y] == 'X'|| board[x][y] == 'A') + return; + board[x][y] = 'A'; + for(int i = 0; i < 4; i++){ + int nextX = x + dir[i][0]; + int nextY = y + dir[i][1]; + + if(nextX < 0 || nextY < 0 || nextX >= board.length || nextY >= board[0].length) + continue; + // if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A') + // continue; + dfs(board, nextX, nextY); + } + } +} +```

From f8f8539393813c4f4ec73427d5eb157acc641fe1 Mon Sep 17 00:00:00 2001 From: Yifan Liu <39271063+yifanliuu@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:49:22 +0800 Subject: [PATCH 0478/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Epython=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E9=80=92=E5=BD=92=E7=89=88=200024.=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index ab204d8974..d7c03b645b 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -177,6 +177,30 @@ class Solution { ``` Python: +```python +# 递归版本 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next + +class Solution: + def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: + if head is None or head.next is None: + return head + + # 待翻转的两个node分别是pre和cur + pre = head + cur = head.next + next = head.next.next + + cur.next = pre # 交换 + pre.next = self.swapPairs(next) # 将以next为head的后续链表两两交换 + + return cur +``` + ```python # Definition for singly-linked list. # class ListNode: From 5085e78e601fa6450e80b0d5d9b060df26857dcb Mon Sep 17 00:00:00 2001 From: Leon He Date: Thu, 29 Jun 2023 11:25:27 -0700 Subject: [PATCH 0479/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add python solution --- ...7.\345\271\277\346\220\234\347\211\210.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 39af9f50a3..2bb0973a11 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -200,3 +200,44 @@ class Solution { +``` +### Python +BFS solution +```python +class Solution: + def __init__(self): + self.dirs = [[0, 1], [1, 0], [-1, 0], [0, -1]] + + def numIslands(self, grid: List[List[str]]) -> int: + m = len(grid) + n = len(grid[0]) + visited = [[False]*n for _ in range(m)] + res = 0 + for i in range(m): + for j in range(n): + if visited[i][j] == False and grid[i][j] == '1': + res += 1 + self.bfs(grid, i, j, visited) # Call bfs within this condition + return res + + def bfs(self, grid, i, j, visited): + q = deque() + q.append((i,j)) + visited[i][j] = True + while q: + x, y = q.popleft() + for k in range(4): + next_i = x + self.dirs[k][0] + next_j = y + self.dirs[k][1] + + if next_i < 0 or next_i >= len(grid): + continue + if next_j < 0 or next_j >= len(grid[0]): + continue + if visited[next_i][next_j]: + continue + if grid[next_i][next_j] == '0': + continue + q.append((next_i, next_j)) + visited[next_i][next_j] = True +``` From 6cd9b165290298868b6b86ca88693c5b77408841 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 29 Jun 2023 16:56:19 -0400 Subject: [PATCH 0480/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=20DFS=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=E7=95=B6=E4=BD=9C=E5=BB=B6=E4=BC=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\347\232\204\345\221\250\351\225\277.md" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 2f399f4016..18f1d01eb2 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -142,6 +142,53 @@ class Solution { return landSum * 4 - cover * 2; } } +// 延伸 - 傳統DFS解法(使用visited數組)(遇到邊界 或是 海水 就edge ++) +class Solution { + int dir[][] ={ + {0, 1}, + {0, -1}, + {1, 0}, + {-1, 0} + }; + + boolean visited[][]; + int res = 0; + + public int islandPerimeter(int[][] grid) { + int row = grid.length; + int col = grid[0].length; + visited = new boolean[row][col]; + + int result = 0; + + for(int i = 0; i < row; i++){ + for(int j = 0; j < col; j++){ + if(visited[i][j] == false && grid[i][j] == 1) + result += dfs(grid, i, j); + } + } + return result; + } + + private int dfs(int[][] grid, int x, int y){ + //如果遇到 邊界(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length)或是 遇到海水(grid[x][y] == 0)就return 1(edge + 1) + if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] == 0) + return 1; + //如果該地已經拜訪過,就return 0 避免重複計算 + if(visited[x][y]) + return 0; + int temp = 0; + visited[x][y] = true; + for(int i = 0; i < 4; i++){ + int nextX = x + dir[i][0]; + int nextY = y + dir[i][1]; + //用temp 把edge存起來 + temp +=dfs(grid, nextX, nextY); + } + return temp; + } +} + ``` Python: From c1b2141ea20e58b031c874f55c341fbfc8b69470 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 10:22:49 +0800 Subject: [PATCH 0481/1533] =?UTF-8?q?Update=200123.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?III.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...344\275\263\346\227\266\346\234\272III.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 6ac9a576ea..f5b959460a 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -413,7 +413,34 @@ function maxProfit(prices: number[]): number { }; ``` +Rust: +> 版本一 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + /* + * 定义 5 种状态: + * 0: 没有操作, 1: 第一次买入, 2: 第一次卖出, 3: 第二次买入, 4: 第二次卖出 + */ + let mut dp = vec![vec![0; 5]; prices.len()]; + dp[0][1] = -prices[0]; + dp[0][3] = -prices[0]; + + for (i, &p) in prices.iter().enumerate().skip(1) { + // 不操作 + // dp[i][0] = dp[i - 1][0]; + dp[i][1] = dp[i - 1][1].max(-p); + dp[i][2] = dp[i - 1][2].max(dp[i - 1][1] + p); + dp[i][3] = dp[i - 1][3].max(dp[i - 1][2] - p); + dp[i][4] = dp[i - 1][4].max(dp[i - 1][3] + p); + } + + dp[prices.len() - 1][4] + } +} +```

From 85daaae4656f0411eab2e1ec6ff1bbfa608120c1 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 10:31:32 +0800 Subject: [PATCH 0482/1533] =?UTF-8?q?Update=200123.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?III.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272III.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index f5b959460a..7286e95770 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -442,6 +442,24 @@ impl Solution { } ``` +> 版本二(绕) + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let mut dp = vec![0, -prices[0], 0, -prices[0], 0]; + + for p in prices { + dp[1] = dp[1].max(-p); + dp[2] = dp[2].max(dp[1] + p); + dp[3] = dp[3].max(dp[2] - p); + dp[4] = dp[4].max(dp[3] + p); + } + dp[4] + } +} +``` +

From 2444c084b15fce271ac3639bca32bc579c594ad1 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 11:33:23 +0800 Subject: [PATCH 0483/1533] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?IV.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272IV.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 5bed5ecc5d..8cfb4c1b5b 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -439,6 +439,28 @@ function maxProfit(k: number, prices: number[]): number { }; ``` +Rust: + +```rust +impl Solution { + pub fn max_profit(k: i32, prices: Vec) -> i32 { + let mut dp = vec![vec![0; 2 * k as usize + 1]; prices.len()]; + + for v in dp[0].iter_mut().skip(1).step_by(2) { + *v = -prices[0]; + } + + for (i, &p) in prices.iter().enumerate().skip(1) { + for j in (0..2 * k as usize - 1).step_by(2) { + dp[i][j + 1] = dp[i - 1][j + 1].max(dp[i - 1][j] - p); + dp[i][j + 2] = dp[i - 1][j + 2].max(dp[i - 1][j + 1] + p); + } + } + + dp[prices.len() - 1][2 * k as usize] + } +} +```

From 7d92df37eb7aca18042512789a286ec4eec3123a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 12:36:36 +0800 Subject: [PATCH 0484/1533] =?UTF-8?q?Update=200309.=E6=9C=80=E4=BD=B3?= =?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E5=86=B7=E5=86=BB=E6=9C=9F.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\345\206\267\345\206\273\346\234\237.md" | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index d10e61b77a..cd71136ba6 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -455,7 +455,29 @@ function maxProfit(prices: number[]): number { }; ``` - +Rust: + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + /* + * dp[i][0]: 持股状态; + * dp[i][1]: 无股状态,当天为非冷冻期; + * dp[i][2]: 无股状态,当天卖出; + * dp[i][3]: 无股状态,当天为冷冻期; + */ + let mut dp = vec![vec![0; 4]; prices.len()]; + dp[0][0] = -prices[0]; + for (i, &p) in prices.iter().enumerate().skip(1) { + dp[i][0] = dp[i - 1][0].max((dp[i - 1][3] - p).max(dp[i - 1][1] - p)); + dp[i][1] = dp[i - 1][1].max(dp[i - 1][3]); + dp[i][2] = dp[i - 1][0] + p; + dp[i][3] = dp[i - 1][2]; + } + *dp[prices.len() - 1].iter().skip(1).max().unwrap() + } +} +```

From e563783a71fe3de6564c7c01e8ffbfc961f3efee Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 12:52:41 +0800 Subject: [PATCH 0485/1533] =?UTF-8?q?Update=200714.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=88=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=EF=BC=89.md=20=E4=BC=98=E5=8C=96=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 1443f14710..9aa82d4ab2 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -270,18 +270,29 @@ impl Solution { **动态规划** ```Rust impl Solution { - fn max(a: i32, b: i32) -> i32 { - if a > b { a } else { b } + pub fn max_profit(prices: Vec, fee: i32) -> i32 { + let mut dp = vec![vec![0; 2]; prices.len()]; + dp[0][0] = -prices[0]; + for (i, &p) in prices.iter().enumerate().skip(1) { + dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - p); + dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + p - fee); + } + dp[prices.len() - 1][1] } +} +``` + +**动态规划优化** + +```rust +impl Solution { pub fn max_profit(prices: Vec, fee: i32) -> i32 { - let n = prices.len(); - let mut dp = vec![vec![0; 2]; n]; - dp[0][0] -= prices[0]; - for i in 1..n { - dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]); - dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee); + let (mut low, mut res) = (-prices[0], 0); + for p in prices { + low = low.max(res - p); + res = res.max(p + low - fee); } - Self::max(dp[n - 1][0], dp[n - 1][1]) + res } } ``` From e452f183adde4732da370e14dc0379ebffd0bdbe Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 13:14:26 +0800 Subject: [PATCH 0486/1533] =?UTF-8?q?Update=200123.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?III.md=20=E4=BD=BF=E7=94=A8=E5=85=83=E7=A5=96=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\200\344\275\263\346\227\266\346\234\272III.md" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 7286e95770..a646b7d517 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -447,15 +447,15 @@ impl Solution { ```rust impl Solution { pub fn max_profit(prices: Vec) -> i32 { - let mut dp = vec![0, -prices[0], 0, -prices[0], 0]; + let (mut one_buy, mut one_sale, mut two_buy, mut two_sale) = (-prices[0], 0, -prices[0], 0); for p in prices { - dp[1] = dp[1].max(-p); - dp[2] = dp[2].max(dp[1] + p); - dp[3] = dp[3].max(dp[2] - p); - dp[4] = dp[4].max(dp[3] + p); + one_buy = one_buy.max(-p); + one_sale = one_sale.max(p + one_buy); + two_buy = two_buy.max(one_sale - p); + two_sale = two_sale.max(two_buy + p); } - dp[4] + two_sale } } ``` From 53f077dc13f8a0076c94bd7db7bbeccd4815a9ff Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 13:15:33 +0800 Subject: [PATCH 0487/1533] Apply suggestions from code review --- ...\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 9aa82d4ab2..042de94718 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -282,7 +282,7 @@ impl Solution { } ``` -**动态规划优化** +**动态规划空间优化** ```rust impl Solution { From c4a7033e50a8713808654ac4b179ca4883b4998f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 13:45:44 +0800 Subject: [PATCH 0488/1533] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?IV.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272IV.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 8cfb4c1b5b..fb1a689811 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -462,6 +462,33 @@ impl Solution { } ``` +空间优化: + +```rust +impl Solution { + pub fn max_profit(k: i32, prices: Vec) -> i32 { + let mut dp = vec![0; 2 * k as usize + 1]; + for v in dp.iter_mut().skip(1).step_by(2) { + *v = -prices[0]; + } + + for p in prices { + for i in 1..=2 * k as usize { + if i % 2 == 1 { + // 买入 + dp[i] = dp[i].max(dp[i - 1] - p); + continue; + } + // 卖出 + dp[i] = dp[i].max(dp[i - 1] + p); + } + } + + dp[2 * k as usize] + } +} +``` +

From d945a304c49f4f41de91b2794b35de0c5f79110b Mon Sep 17 00:00:00 2001 From: Binbin Date: Mon, 3 Jul 2023 19:29:44 +0800 Subject: [PATCH 0489/1533] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9Arecode=20->=20record?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index e74cdf71fd..70501469d8 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -88,7 +88,7 @@ public: return false; } for (int i = 0; i < magazine.length(); i++) { - // 通过recode数据记录 magazine里各个字符出现次数 + // 通过record数据记录 magazine里各个字符出现次数 record[magazine[i]-'a'] ++; } for (int j = 0; j < ransomNote.length(); j++) { @@ -218,7 +218,7 @@ Go: ```go func canConstruct(ransomNote string, magazine string) bool { record := make([]int, 26) - for _, v := range magazine { // 通过recode数据记录 magazine里各个字符出现次数 + for _, v := range magazine { // 通过record数据记录 magazine里各个字符出现次数 record[v-'a']++ } for _, v := range ransomNote { // 遍历ransomNote,在record里对应的字符个数做--操作 From dacd1a91d4c4cfeeeebd77b3840118f1ba61e02d Mon Sep 17 00:00:00 2001 From: ZKkkk Date: Tue, 4 Jul 2023 19:46:00 +0800 Subject: [PATCH 0490/1533] =?UTF-8?q?Update=201221.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=B9=B3=E8=A1=A1=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20about=20Type?= =?UTF-8?q?Script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\241\345\255\227\347\254\246\344\270\262.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" index b587514a41..2a7b092290 100644 --- "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" @@ -156,6 +156,21 @@ var balancedStringSplit = function(s) { }; ``` +### TypeScript + +```ts +function balancedStringSplit(s: string): number { + let count: number = 0 + let res: number = 0 + for(let i of s){ + if(i === 'R') count++ + else count-- + if(count === 0) res++ + } + + return res +}; +```

From 4fe73f30dc793ace07e15b23ff0956eb221c5856 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Wed, 5 Jul 2023 14:18:55 +0800 Subject: [PATCH 0491/1533] =?UTF-8?q?=E4=B9=A6=E5=86=99=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=8Cnull=E5=86=99=E6=88=90=E4=BA=86nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 3d73598da9..28c5a4c037 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -51,7 +51,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 ``` -if (root == nullptr) return root; +if (root == null) return root; ``` * 确定单层递归的逻辑 From 751ba68faa3d16b45fce13e0ada8aac58d24e719 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Wed, 5 Jul 2023 14:35:56 +0800 Subject: [PATCH 0492/1533] =?UTF-8?q?null=E5=86=99=E6=88=90=E4=BA=86nullpt?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 28c5a4c037..48737486d0 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -85,14 +85,14 @@ if (root == null) return root; if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - if (root->left == nullptr) return root->right; + if (root->left == null) return root->right; // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == nullptr) return root->left; + else if (root->right == null) return root->left; // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != nullptr) { + while(cur->left != null) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -118,23 +118,23 @@ return root; class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - if (root->left == nullptr && root->right == nullptr) { + if (root->left == null && root->right == null) { ///! 内存释放 delete root; - return nullptr; + return null; } // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - else if (root->left == nullptr) { + else if (root->left == null) { auto retNode = root->right; ///! 内存释放 delete root; return retNode; } // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == nullptr) { + else if (root->right == null) { auto retNode = root->left; ///! 内存释放 delete root; @@ -144,7 +144,7 @@ public: // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != nullptr) { + while(cur->left != null) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -178,9 +178,9 @@ public: class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; + if (root == null) return root; if (root->val == key) { - if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用 + if (root->right == null) { // 这里第二次操作目标值:最终删除的作用 return root->left; } TreeNode *cur = root->right; @@ -211,8 +211,8 @@ private: // 并返回目标节点右孩子为新的根节点 // 是动画里模拟的过程 TreeNode* deleteOneNode(TreeNode* target) { - if (target == nullptr) return target; - if (target->right == nullptr) return target->left; + if (target == null) return target; + if (target->right == null) return target->left; TreeNode* cur = target->right; while (cur->left) { cur = cur->left; @@ -222,16 +222,16 @@ private: } public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; + if (root == null) return root; TreeNode* cur = root; - TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur + TreeNode* pre = null; // 记录cur的父节点,用来删除cur while (cur) { if (cur->val == key) break; pre = cur; if (cur->val > key) cur = cur->left; else cur = cur->right; } - if (pre == nullptr) { // 如果搜索树只有头结点 + if (pre == null) { // 如果搜索树只有头结点 return deleteOneNode(cur); } // pre 要知道是删左孩子还是右孩子 From d7753b31b6f66def80719e82cb7d8ddef3056a7e Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Thu, 6 Jul 2023 23:14:49 +0800 Subject: [PATCH 0493/1533] =?UTF-8?q?Update=201002.=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix formatting --- ...6\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index 9ec3c6c4e8..a53148b313 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -16,6 +16,7 @@ 输入:words = ["bella","label","roller"] 输出:["e","l","l"] + 示例 2: 输入:words = ["cool","lock","cook"] From 873257dcd812b1d190f9decbf0fed64761d23933 Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Sat, 8 Jul 2023 07:25:59 +0800 Subject: [PATCH 0494/1533] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix Python solution comment --- .../0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index ca62e3edc2..438fa352f8 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -160,7 +160,7 @@ class Solution: for index, value in enumerate(nums): if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key return [records[target- value], index] - records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key + records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中 return [] ``` (版本二)使用集合 From dba8820427c64b66add3b7ad053226e7d9943e53 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 10:43:24 +0800 Subject: [PATCH 0495/1533] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5?= =?UTF-8?q?=E6=B8=A9=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\346\227\245\346\270\251\345\272\246.md" | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index 749dc97235..d2da37371a 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -455,7 +455,27 @@ function dailyTemperatures(temperatures: number[]): number[] { }; ``` - +Rust: + +```rust +impl Solution { + /// 单调栈的本质是以空间换时间,记录之前已访问过的非递增子序列下标 + pub fn daily_temperatures(temperatures: Vec) -> Vec { + let mut res = vec![0; temperatures.len()]; + let mut stack = vec![]; + for (idx, &value) in temperatures.iter().enumerate() { + while !stack.is_empty() && temperatures[*stack.last().unwrap()] < value { + // 弹出,并计算res中对应位置的值 + let i = stack.pop().unwrap(); + res[i] = (idx - i) as i32; + } + // 入栈 + stack.push(idx) + } + res + } +} +```

From 965afc9af3dc5c0fb8932937770f8395c98b7b86 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:20:21 +0800 Subject: [PATCH 0496/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=A7=A3=E6=B3=95=E7=9A=84=E4=BD=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E9=80=82=E5=90=88=E5=9B=BE=E7=9A=84=E8=A7=A3=E6=B3=95=E6=94=BE?= =?UTF-8?q?=E5=9C=A8=E7=AC=AC=E4=B8=80=E4=B8=AA=EF=BC=8C=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E4=B8=AA=E8=A7=A3=E6=B3=95=EF=BC=88=E9=9A=BE=E7=90=86=E8=A7=A3?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=89=E6=94=BE=E5=9C=A8=E5=85=B6=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9Ejava=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 86 +++++++++++++++---- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 48737486d0..d13c48e529 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -268,6 +268,34 @@ public: ## Java +```java +// 解法1(最好理解的版本) +class Solution { + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) return root; + if (root.val == key) { + if (root.left == null) { + return root.right; + } else if (root.right == null) { + return root.left; + } else { + TreeNode cur = root.right; + while (cur.left != null) { + cur = cur.left; + } + cur.left = root.left; + root = root.right; + return root; + } + } + if (root.val > key) root.left = deleteNode(root.left, key); + if (root.val < key) root.right = deleteNode(root.right, key); + return root; + } +} +``` + + ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -296,33 +324,57 @@ class Solution { } } ``` +递归法 ```java -// 解法2 class Solution { public TreeNode deleteNode(TreeNode root, int key) { - if (root == null) return root; - if (root.val == key) { - if (root.left == null) { - return root.right; - } else if (root.right == null) { - return root.left; - } else { - TreeNode cur = root.right; - while (cur.left != null) { - cur = cur.left; - } - cur.left = root.left; - root = root.right; - return root; + if (root == null){ + return null; + } + //寻找对应的对应的前面的节点,以及他的前一个节点 + TreeNode cur = root; + TreeNode pre = null; + while (cur != null){ + if (cur.val < key){ + pre = cur; + cur = cur.right; + } else if (cur.val > key) { + pre = cur; + cur = cur.left; + }else { + break; } } - if (root.val > key) root.left = deleteNode(root.left, key); - if (root.val < key) root.right = deleteNode(root.right, key); + if (pre == null){ + return deleteOneNode(cur); + } + if (pre.left !=null && pre.left.val == key){ + pre.left = deleteOneNode(cur); + } + if (pre.right !=null && pre.right.val == key){ + pre.right = deleteOneNode(cur); + } return root; } + + public TreeNode deleteOneNode(TreeNode node){ + if (node == null){ + return null; + } + if (node.right == null){ + return node.left; + } + TreeNode cur = node.right; + while (cur.left !=null){ + cur = cur.left; + } + cur.left = node.left; + return node.right; + } } ``` + ## Python 递归法(版本一) ```python From 0fd7b04d5c0fac9a760af96d2d5de7f5beaeb896 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:42:19 +0800 Subject: [PATCH 0497/1533] =?UTF-8?q?=E6=92=A4=E5=9B=9E=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index d13c48e529..e617cc69b6 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -51,7 +51,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 ``` -if (root == null) return root; +if (root == nullptr) return root; ``` * 确定单层递归的逻辑 @@ -85,14 +85,14 @@ if (root == null) return root; if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - if (root->left == null) return root->right; + if (root->left == nullptr) return root->right; // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) return root->left; + else if (root->right == nullptr) return root->left; // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -118,23 +118,23 @@ return root; class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - if (root->left == null && root->right == null) { + if (root->left == nullptr && root->right == nullptr) { ///! 内存释放 delete root; - return null; + return nullptr; } // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - else if (root->left == null) { + else if (root->left == nullptr) { auto retNode = root->right; ///! 内存释放 delete root; return retNode; } // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) { + else if (root->right == nullptr) { auto retNode = root->left; ///! 内存释放 delete root; @@ -144,7 +144,7 @@ public: // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -178,9 +178,9 @@ public: class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; + if (root == nullptr) return root; if (root->val == key) { - if (root->right == null) { // 这里第二次操作目标值:最终删除的作用 + if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用 return root->left; } TreeNode *cur = root->right; @@ -211,8 +211,8 @@ private: // 并返回目标节点右孩子为新的根节点 // 是动画里模拟的过程 TreeNode* deleteOneNode(TreeNode* target) { - if (target == null) return target; - if (target->right == null) return target->left; + if (target == nullptr) return target; + if (target->right == nullptr) return target->left; TreeNode* cur = target->right; while (cur->left) { cur = cur->left; @@ -222,16 +222,16 @@ private: } public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; + if (root == nullptr) return root; TreeNode* cur = root; - TreeNode* pre = null; // 记录cur的父节点,用来删除cur + TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur while (cur) { if (cur->val == key) break; pre = cur; if (cur->val > key) cur = cur->left; else cur = cur->right; } - if (pre == null) { // 如果搜索树只有头结点 + if (pre == nullptr) { // 如果搜索树只有头结点 return deleteOneNode(cur); } // pre 要知道是删左孩子还是右孩子 From 1c7b15bbbc224e37f9341161f31a94cebe73dfb0 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 18:26:09 +0800 Subject: [PATCH 0498/1533] =?UTF-8?q?Update=200496.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\244\247\345\205\203\347\264\240I.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 31c3ce4387..411a47df9b 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -387,6 +387,32 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` +Rust + +```rust +impl Solution { + pub fn next_greater_element(nums1: Vec, nums2: Vec) -> Vec { + let mut ans = vec![-1; nums1.len()]; + use std::collections::HashMap; + let mut map = HashMap::new(); + for (idx, &i) in nums1.iter().enumerate() { + map.insert(i, idx); + } + let mut stack = vec![]; + for (idx, &i) in nums2.iter().enumerate() { + while !stack.is_empty() && nums2[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + if let Some(&jdx) = map.get(&nums2[pos]) { + ans[jdx] = i; + } + } + stack.push(idx); + } + ans + } +} +``` +

From 85e2b18a1d9f5eba4704a3b585524ea54bc73c7d Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 20:38:47 +0800 Subject: [PATCH 0499/1533] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\244\247\345\205\203\347\264\240II.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 3fd4b3b6db..a090f32c3e 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -266,6 +266,24 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` +Rust +```rust +impl Solution { + pub fn next_greater_elements(nums: Vec) -> Vec { + let mut ans = vec![-1; nums.len() * 2]; + let mut stack = vec![]; + let double = nums.repeat(2); + for (idx, &i) in double.iter().enumerate() { + while !stack.is_empty() && double[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + ans[pos] = i; + } + stack.push(idx); + } + ans.into_iter().take(nums.len()).collect() + } +} +```

From 6e7e5e0fae032918f5288e86c177c3e40916c6e0 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 22:13:12 +0800 Subject: [PATCH 0500/1533] =?UTF-8?q?Update=200042.=E6=8E=A5=E9=9B=A8?= =?UTF-8?q?=E6=B0=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index db66095da2..833a7613e2 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -926,6 +926,56 @@ int trap(int* height, int heightSize) { * 空间复杂度 O(1) +Rust + +双指针 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let n = height.len(); + let mut max_left = vec![0; height.len()]; + let mut max_right = vec![0; height.len()]; + max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| { + let lmax = lm.max(height[idx]); + let rmax = rm.max(height[n - 1 - idx]); + *x = lmax; *y = rmax; + (lmax, rmax) + }); + height.iter().enumerate().fold(0, |acc, (idx, x)| { + let h = max_left[idx].min(max_right[idx]); + if h > 0 { h - x + acc } else { acc } + }) + } +} +``` + +单调栈 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let mut stack = vec![]; + let mut ans = 0; + for (right_pos, &right_h) in height.iter().enumerate() { + while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h { + let mid_pos = stack.pop().unwrap(); + if !stack.is_empty() { + let left_pos = *stack.last().unwrap(); + let left_h = height[left_pos]; + let top = std::cmp::min(left_h, right_h); + if top > height[mid_pos] { + ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32; + } + } + } + stack.push(right_pos); + } + ans + } +} +``` +

From cef827812ab2084e6d6c0437bff6fd4d2f0b2720 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sun, 9 Jul 2023 14:30:01 +0800 Subject: [PATCH 0501/1533] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index f9a8350818..bc82a860cc 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -670,6 +670,61 @@ function largestRectangleArea(heights: number[]): number { ``` +Rust + +双指针预处理 +```rust + +impl Solution { + pub fn largest_rectangle_area(v: Vec) -> i32 { + let n = v.len(); + let mut left_smaller_idx = vec![-1; n]; + let mut right_smaller_idx = vec![n as i32; n]; + for i in 1..n { + let mut mid = i as i32 - 1; + while mid >= 0 && v[mid as usize] >= v[i] { + mid = left_smaller_idx[mid as usize]; + } + left_smaller_idx[i] = mid; + } + for i in (0..n-1).rev() { + let mut mid = i + 1; + while mid < n && v[mid] >= v[i] { + mid = right_smaller_idx[mid] as usize; + } + right_smaller_idx[i] = mid as i32; + } + let mut res = 0; + for (idx, &e) in v.iter().enumerate() { + res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); + } + dbg!(res) + } +} +``` + +单调栈 +```rust +impl Solution { + pub fn largest_rectangle_area1(mut v: Vec) -> i32 { + v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 + v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 + let mut res = 0; + let mut stack = vec![]; // 递增的栈 + for (idx, &e) in v.iter().enumerate() { + while !stack.is_empty() && v[*stack.last().unwrap()] > e { + let pos = stack.pop().unwrap(); + let prev_pos = *stack.last().unwrap(); + let s = (idx - prev_pos - 1) as i32 * v[pos]; + res = res.max(s); + } + stack.push(idx); + } + res + } +} +``` +

From a31b5865df04e5004724995e2b36610ec45d9489 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 10 Jul 2023 16:00:53 +0800 Subject: [PATCH 0502/1533] =?UTF-8?q?=E7=BB=84=E5=90=88=E6=96=B0=E5=A2=9Ej?= =?UTF-8?q?ava=E7=89=88=E6=9C=AC=E6=9C=AA=E5=89=AA=E6=9E=9D=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 444e15ce74..8ade6e114f 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -351,7 +351,30 @@ public: ### Java: +未剪枝优化 +```java +class Solution { + List> result= new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combine(int n, int k) { + backtracking(n,k,1); + return result; + } + public void backtracking(int n,int k,int startIndex){ + if (path.size() == k){ + result.add(new ArrayList<>(path)); + return; + } + for (int i =startIndex;i<=n;i++){ + path.add(i); + backtracking(n,k,i+1); + path.removeLast(); + } + } +} +``` +剪枝优化: ```java class Solution { List> result = new ArrayList<>(); From 3ba32880fa55769ef99686ee42bd5b8da9bc077b Mon Sep 17 00:00:00 2001 From: "jingsong.zeng" Date: Tue, 11 Jul 2023 14:17:53 +0800 Subject: [PATCH 0503/1533] Add the dart implementation of 0704. --- ...14\345\210\206\346\237\245\346\211\276.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 75135749bd..ba35867129 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -755,8 +755,56 @@ object Solution { } } ``` +**Dart:** + +```dart +(版本一)左闭右闭区间 +class Solution { + int search(List nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left <= right) { + int middle = ((left + right)/2).truncate(); + switch (nums[middle].compareTo(target)) { + case 1: + right = middle - 1; + continue; + case -1: + left = middle + 1; + continue; + default: + return middle; + } + } + return -1; + } +} + +(版本二)左闭右开区间 +class Solution { + int search(List nums, int target) { + int left = 0; + int right = nums.length; + while (left < right) { + int middle = left + ((right - left) >> 1); + switch (nums[middle].compareTo(target)) { + case 1: + right = middle; + continue; + case -1: + left = middle + 1; + continue; + default: + return middle; + } + } + return -1; + } +} +``` +

From 886287eb27b94681d578248af3ac2b348ee7e94f Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 14 Jul 2023 11:12:45 +0800 Subject: [PATCH 0504/1533] =?UTF-8?q?=E5=B9=B6=E6=9F=A5=E9=9B=86=E5=9B=BE?= =?UTF-8?q?=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 433 ++++++++++++++++++ 1 file changed, 433 insertions(+) create mode 100644 "problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" new file mode 100644 index 0000000000..5cd753e9ba --- /dev/null +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -0,0 +1,433 @@ + +# 并查集理论基础 + +图论中,关于深搜和广搜我们在这里:[钥匙和房间](https://mp.weixin.qq.com/s/E9NlJy9PW1oJuD8N2EURoQ) 已经更新完毕了。 + +接下来我们来讲一下并查集,首先当然是并查集理论基础。 + +## 背景 + +首先要知道并查集可以解决什么问题呢? + +并查集常用来解决连通性问题。 + +大白话就是当我们需要判断两个元素是否在同一个集合里的时候,我们就要想到用并查集。 + +并查集主要有两个功能: + +* 将两个元素添加到一个集合中。 +* 判断两个元素在不在同一个集合 + +接下来围绕并查集的这两个功能来展开讲解。 + +## 原理讲解 + +从代码层面,我们如何将两个元素添加到同一个集合中呢。 + +此时有录友会想到:可以把他放到同一个数组里或者set 或者 map 中,这样就表述两个元素在同一个集合。 + +那么问题来了,对这些元素分门别类,可不止一个集合,可能是很多集合,成百上千,那么要定义这么多个数组吗? + +有录友想,那可以定义一个二维数组。 + +但如果我们要判断两个元素是否在同一个集合里的时候 我们又能怎么办? 只能把而二维数组都遍历一遍。 + +而且每当想添加一个元素到某集合的时候,依然需要把把二维数组组都遍历一遍,才知道要放在哪个集合里。 + +这仅仅是一个粗略的思路,如果沿着这个思路去实现代码,非常复杂,因为管理集合还需要很多逻辑。 + +那么我们来换一个思路来看看。 + +我们将三个元素A,B,C (分别是数字)放在同一个集合,其实就是将三个元素连通在一起,如何连通呢。 + +只需要用一个一维数组来表示,即:father[A] = B,father[B] = C 这样就表述 A 与 B 与 C连通了(有向连通图)。 + +代码如下: + +``` CPP +// 将v,u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} +``` + +可能有录友想,这样我可以知道 A 连通 B,因为 A 是索引下标,根据 father[A]的数值就知道 A 连通 B。那怎么知道 B 连通 A呢? + +我们的目的是判断这三个元素是否在同一个集合里,知道 A 连通 B 就已经足够了。 + +这里要讲到寻根思路,只要 A ,B,C 在同一个根下就是同一个集合。 + +给出A元素,就可以通过 father[A] = B,father[B] = C,找到根为 C。 + +给出B元素,就可以通过 father[B] = C,找到根也为为 C,说明 A 和 B 是在同一个集合里。 +大家会想第一段代码里find函数是如何实现的呢?其实就是通过数组下标找到数组元素,一层一层寻根过程,代码如下: + +```CPP +// 并查集里寻根的过程 +int find(int u) { + if (u == father[u]) return u; // 如果根就是自己,直接返回 + else return find(father[u]); // 如果根不是自己,就根据数组下标一层一层向下找 +} + +``` + + +如何表示 C 也在同一个元素里呢? 我们需要 father[C] = C,即C的根也为C,这样就方便表示 A,B,C 都在同一个集合里了。 + +所以father数组初始化的时候要 father[i] = i,默认自己指向自己。 + +代码如下: + +```CPP +// 并查集初始化 +void init() { + for (int i = 0; i < n; ++i) { + father[i] = i; + } +} +``` + +最后我们如何判断两个元素是否在同一个集合里,如果通过 find函数 找到 两个元素属于同一个根的话,那么这两个元素就是同一个集合,代码如下: + + +```CPP +// 判断 u 和 v是否找到同一个根 +bool isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} +``` + +## 路径压缩 + +在实现 find 函数的过程中,我们知道,通过递归的方式,不断获取father数组下标对应的数值,最终找到这个集合的根。 + +搜索过程像是一个多叉树中从叶子到根节点的过程,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602102619.png) + +如果这棵多叉树高度很深的话,每次find函数 去寻找跟的过程就要递归很多次。 + +我们的目的只需要知道这些节点在同一个根下就可以,所以对这棵多叉树的构造只需要这样就可以了,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602103040.png) + +除了根节点其他所有节点都挂载根节点下,这样我们在寻根的时候就很快,只需要一步, + +如果我们想达到这样的效果,就需要 **路径压缩**,将非根节点的所有节点直接指向根节点。 +那么在代码层面如何实现呢? + +我们只需要在递归的过程中,让 father[u] 接住 递归函数 find(father[u]) 的返回结果。 + +因为 find 函数向上寻找根节点,father[u] 表述 u 的父节点,那么让 father[u] 直接获取 find函数 返回的根节点,这样就让节点 u 的父节点 变成根节点。 + +代码如下,注意看注释,路径压缩就一行代码: + +```CPP +// 并查集里寻根的过程 +int find(int u) { + if (u == father[u]) return u; + else return father[u] = find(father[u]); // 路径压缩 +} +``` + +以上代码在C++中,可以用三元表达式来精简一下,代码如下: + +```CPP +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); +} + +``` + +相信不少录友在学习并查集的时候,对上面这三行代码实现的 find函数 很熟悉,但理解上却不够深入,仅仅知道这行代码很好用,不知道这里藏着路径压缩的过程。 + +所以对于算法初学者来说,直接看精简代码学习是不太友好的,往往忽略了很多细节。 + +## 代码模板 + +那么此时并查集的模板就出来了, 整体模板C++代码如下: + +```CPP +int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好 +vector father = vector (n, 0); // C++里的一种数组结构 + +// 并查集初始化 +void init() { + for (int i = 0; i < n; ++i) { + father[i] = i; + } +} +// 并查集里寻根的过程 +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 +} + +// 判断 u 和 v是否找到同一个根 +bool isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} + +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} +``` +通过模板,我们可以知道,并查集主要有三个功能。 + +1. 寻找根节点,函数:find(int u),也就是判断这个节点的祖先节点是哪个 +2. 将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上 +3. 判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点 + +## 常见误区 + +这里估计有录友会想,模板中的 join 函数里的这段代码: + +```CPP +u = find(u); // 寻找u的根 +v = find(v); // 寻找v的根 +if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + +``` + +与 isSame 函数的实现是不是重复了? 如果抽象一下呢,代码如下: + +```CPP +// 判断 u 和 v是否找到同一个根 +bool isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} + +// 将v->u 这条边加入并查集 +void join(int u, int v) { + if (isSame) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} +``` + +这样写可以吗? 好像看出去没问题,而且代码更精简了。 + +**其实这么写是有问题的**,在join函数中 我们需要寻找 u 和 v 的根,然后再进行连线在一起,而不是直接 用 u 和 v 连线在一起。 + +举一个例子: + +``` +join(1, 2); +join(3, 2); +``` + +此时构成的图是这样的: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230525111307.png) + +此时问 1,3是否在同一个集合,我们调用 `join(1, 2); join(3, 2);` 很明显本意要表示 1,3是在同一个集合。 + +但我们来看一下代码逻辑,当我们调用 `isSame(1, 3)`的时候,find(1) 返回的是1,find(3)返回的是3。 `return 1 == 3` 返回的是false,代码告诉我们 1 和 3 不在同一个集合,这明显不符合我们的预期,所以问题出在哪里? + +问题出在我们精简的代码上,即 join 函数 一定要先 通过find函数寻根再进行关联。 + +如果find函数是这么实现,再来看一下逻辑过程。 + +```CPP +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} +``` + +分别将 这两对元素加入集合。 + +```CPP +join(1, 2); +join(3, 2); +``` + +当执行`join(3, 2)`的时候,会先通过find函数寻找 3的根为3,2的根为1 (第一个`join(1, 2)`,将2的根设置为1),所以最后是将1 指向 3。 + +构成的图是这样的: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230525112101.png) + +因为在join函数里,我们有find函数进行寻根的过程,这样就保证元素 1,2,3在这个有向图里是强连通的。 + +此时我们在调用 `isSame(1, 3)`的时候,find(1) 返回的是3,find(3) 返回的也是3,`return 3 == 3` 返回的是true,即告诉我们 元素 1 和 元素3 是 在同一个集合里的。 + + + + +## 模拟过程 + +(**凸显途径合并的过程,每一个join都要画图**) + +不少录友在接触并查集模板之后,用起来很娴熟,因为模板确实相对固定,但是对并查集内部数据组织方式以及如何判断是否是同一个集合的原理很模糊。 + +通过以上讲解之后,我在带大家一步一步去画一下,并查集内部数据连接方式。 + +注意:为了让录友们了解基础并查集的操作,不至于混乱,**以下模拟过程中不考虑路径压缩的过程**,了解基础并查集操作后,路径压缩也很容易理解。 + +我们先通过一些列的join操作,将两两元素分别放入同一个集合中。 + +```CPP +join(1, 8); +join(3, 8); +join(1, 7); +join(8, 5); +join(6, 2); +join(2, 9); + +``` + +此时我们生成的的有向图为: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230526122203.png) + +有录友可能想,`join(3, 8)` 在图中为什么 将 元素1 连向元素 3 而不是将 元素 8 连向 元素 3 呢? + +这一点 我在 「常见误区」标题下已经详细讲解了,因为在`join(int u, int v)`函数里 要分别对 u 和 v 寻根之后再进行关联。 + +大家看懂这个有向图后,相信应该知道如下函数的返回值了。 + +```CPP +cout << isSame(8, 7) << endl; +cout << isSame(7, 2) << endl; +``` + +返回值分别如下,表示,8 和 7 是同一个集合,而 7 和 2 不是同一个集合。 + +``` +true +false +``` + +## 拓展 + + +在「路径压缩」讲解中,我们知道如何靠压缩路径来缩短查询根节点的时间。 + +其实还有另一种方法:按秩(rank)合并。 + +rank表示树的高度,即树中结点层次的最大值。 + +例如两个集合(多叉树)需要合并,如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602172250.png) + +树1 rank 为2,树2 rank 为 3。那么合并两个集合,是 树1 合入 树2,还是 树2 合入 树1呢? + +我们来看两个不同方式合入的效果。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602172933.png) + +这里可以看出,树2 合入 树1 会导致整棵树的高度变的更高,而 树1 合入 树2 整棵树的高度 和 树2 保持一致。 + +所以在 join函数中如何合并两棵树呢? + +一定是 rank 小的树合入 到 rank大 的树,这样可以保证最后合成的树rank 最小,降低在树上查询的路径长度。 + +按秩合并的代码如下: + +```CPP +int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好 +vector father = vector (n, 0); // C++里的一种数组结构 +vector rank = vector (n, 1); // 初始每棵树的高度都为1 + +// 并查集初始化 +void init() { + for (int i = 0; i < n; ++i) { + father[i] = i; + rank[i] = 1; // 也可以不写 + } +} +// 并查集里寻根的过程 +int find(int u) { + return u == father[u] ? u : find(father[u]);// 注意这里不做路径压缩 +} + +// 判断 u 和 v是否找到同一个根 +bool isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} + +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + + if (rank[u] <= rank[v]) father[u] = v; // rank小的树合入到rank大的树 + else father[v] = u; + + if (rank[u] == rank[v] && u != v) rank[v]++; // 如果两棵树高度相同,则v的高度+1因为,方面 if (rank[u] <= rank[v]) father[u] = v; 注意是 <= +} +``` + +可以注意到在上面的模板代码中,我是没有做路径压缩的,因为一旦做路径压缩,rank记录的高度就不准了,根据rank来判断如何合并就没有意义。 + +也可以在 路径压缩的时候,再去实时修生rank的数值,但这样在代码实现上麻烦了不少,关键是收益很小。 + +其实我们在优化并查集查询效率的时候,只用路径压缩的思路就够了,不仅代码实现精简,而且效率足够高。 + +按秩合并的思路并没有将树形结构尽可能的扁平化,所以在整理效率上是没有路径压缩高的。 + +说到这里可能有录友会想,那在路径压缩的代码中,只有查询的过程 即 find 函数的执行过程中会有路径压缩,如果一直没有使用find函数,是不是相当于这棵树就没有路径压缩,导致查询效率依然很低呢? + +大家可以再去回顾使用路径压缩的 并查集模板,在isSame函数 和 join函数中,我们都调用了 find 函数来进行寻根操作。 + +也就是说,无论使用并查集模板里哪一个函数(除了init函数),都会有路径压缩的过程,第二次访问相同节点的时候,这个节点就是直连根节点的,即 第一次访问的时候它的路径就被压缩了。 + +**所以这里推荐大家直接使用路径压缩的并查集模板就好**,但按秩合并的优化思路我依然给大家讲清楚,有助于更深一步理解并查集的优化过程。 + +## 复杂度分析 + + +这里对路径压缩版并查集来做分析。 + +空间复杂度: O(n) ,申请一个father数组。 + +关于时间复杂度,如果想精确表达出来需要繁琐的数学证明,就不在本篇讲解范围内了,大家感兴趣可以自己去深入去研究。 + +这里做一个简单的分析思路。 + +路径压缩后的并查集时间复杂度在O(logn)与O(1)之间,且随着查询或者合并操作的增加,时间复杂度会越来越趋于O(1)。 + +了解到这个程度对于求职面试来说就够了。 + +在第一次查询的时候,相当于是n叉树上从叶子节点到根节点的查询过程,时间复杂度是logn,但路径压缩后,后面的查询操作都是O(1),而 join 函数 和 isSame函数 里涉及的查询操作也是一样的过程。 + + +## 总结 + +本篇我们讲解了并查集的背景、原理、两种优化方式(路径压缩,按秩合并),代码模板,常见误区,以及模拟过程。 + +要知道并查集解决什么问题,在什么场景下我们要想到使用并查集。 + + +接下来进一步优化并查集的执行效率,重点介绍了路径压缩的方式,另一种方法:按秩合并,我们在 「拓展」中讲解。 + +通过一步一步的原理讲解,最后给出并查集的模板,所有的并查集题目都在这个模板的基础上进行操作或者适当修改。 + +但只给出模板还是不够的,针对大家学习并查集的常见误区,详细讲解了模板代码的细节。 + +为了让录友们进一步了解并查集的运行过程,我们再通过具体用例模拟一遍代码过程并画出对应的内部数据连接图(有向图)。 + +这里也建议大家去模拟一遍才能对并查集理解的更到位。 + +如果对模板代码还是有点陌生,不用担心,接下来我会讲解对应LeetCode上的并查集题目,通过一系列题目练习,大家就会感受到这套模板有多么的好用! + +敬请期待 并查集题目精讲系列。 + + From ba5ecfecaf7160911e4d464c5aecdce1f6973146 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 14 Jul 2023 11:13:00 +0800 Subject: [PATCH 0505/1533] Update --- ...\273\351\231\244\345\205\203\347\264\240.md" | 7 +++++-- ...72\346\227\213\347\237\251\351\230\265II.md" | 9 ++++++--- ...\276\350\241\250\345\205\203\347\264\240.md" | 4 ++++ ...\273\350\275\254\351\223\276\350\241\250.md" | 5 +++++ ...\204\345\255\220\346\225\260\347\273\204.md" | 13 ++++++++----- ...\220\345\255\227\347\254\246\344\270\262.md" | 16 ++++++++-------- ...\227\344\275\231\350\277\236\346\216\245.md" | 10 +++------- ...27\344\275\231\350\277\236\346\216\245II.md" | 4 ++-- ...\214\345\210\206\346\237\245\346\211\276.md" | 6 ++++-- ...\276\350\256\241\351\223\276\350\241\250.md" | 8 ++++++-- ...\204\347\232\204\345\271\263\346\226\271.md" | 17 ++++++++++------- ...\230\345\234\250\350\267\257\345\276\204.md" | 6 +++--- 12 files changed, 64 insertions(+), 41 deletions(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 908011532a..3d43a199b0 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -26,9 +26,12 @@ **你不需要考虑数组中超出新长度后面的元素。** -## 思路 -针对本题,我录制了视频讲解:[数组中移除元素并不容易!LeetCode:27. 移除元素](https://www.bilibili.com/video/BV12A4y1Z7LP),结合本题解一起看,事半功倍! +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[数组中移除元素并不容易!LeetCode:27. 移除元素](https://www.bilibili.com/video/BV12A4y1Z7LP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 有的同学可能说了,多余的元素,删掉不就得了。 diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index b4dad9c3de..fd40f3fc52 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -6,7 +6,7 @@ -## 59.螺旋矩阵II +# 59.螺旋矩阵II [力扣题目链接](https://leetcode.cn/problems/spiral-matrix-ii/) @@ -22,9 +22,12 @@ [ 7, 6, 5 ] ] -## 思路 -为了利于录友们理解,我特意录制了视频,[拿下螺旋矩阵!LeetCode:59.螺旋矩阵II](https://www.bilibili.com/video/BV1SL4y1N7mV),结合视频一起看,事半功倍! +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下螺旋矩阵!LeetCode:59.螺旋矩阵II](https://www.bilibili.com/video/BV1SL4y1N7mV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这道题目可以说在面试中出现频率较高的题目,**本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。** diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index f875165826..300f98e911 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -27,6 +27,10 @@ 输入:head = [7,7,7,7], val = 7 输出:[] +# 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + # 思路 diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index 0425e18280..c63c998dc7 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -17,6 +17,11 @@ 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL +# 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + # 思路 本题我录制了B站视频,[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频在看本篇题解,更有助于大家对链表的理解。 diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index d7ae478033..82d551ea44 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -13,9 +13,9 @@ 示例: -输入:s = 7, nums = [2,3,1,2,4,3] -输出:2 -解释:子数组 [4,3] 是该条件下的长度最小的子数组。 +* 输入:s = 7, nums = [2,3,1,2,4,3] +* 输出:2 +* 解释:子数组 [4,3] 是该条件下的长度最小的子数组。 提示: @@ -23,9 +23,12 @@ * 1 <= nums.length <= 10^5 * 1 <= nums[i] <= 10^5 -# 思路 +# 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -为了易于大家理解,我特意录制了B站视频[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE),结合视频看本题解,事半功倍! + +# 思路 ## 暴力解法 diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 5d56ad18fb..e26d04ad87 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -16,18 +16,18 @@ 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。 示例 1: -输入: "abab" -输出: True -解释: 可由子字符串 "ab" 重复两次构成。 +* 输入: "abab" +* 输出: True +* 解释: 可由子字符串 "ab" 重复两次构成。 示例 2: -输入: "aba" -输出: False +* 输入: "aba" +* 输出: False 示例 3: -输入: "abcabcabcabc" -输出: True -解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) +* 输入: "abcabcabcabc" +* 输出: True +* 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) # 思路 diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 177338dd6d..c4d62d9b46 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -4,11 +4,8 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - # 684.冗余连接 - [力扣题目链接](https://leetcode.cn/problems/redundant-connection/) 树可以看成是一个连通且 无环 的 无向 图。 @@ -78,7 +75,9 @@ void join(int u, int v) { 2. 将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上 3. 判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点 -简单介绍并查集之后,我们再来看一下这道题目。 +如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html) + +我们再来看一下这道题目。 题目说是无向图,返回一条可以删去的边,使得结果图是一个有着N个节点的树(即:只有一个根节点)。 @@ -92,7 +91,6 @@ void join(int u, int v) { 节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 - (如果题目中说:如果有多个答案,则返回二维数组中最前出现的边。 那我们就要 从后向前遍历每一条边了) 如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了。 @@ -151,8 +149,6 @@ public: 可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。 -这里对并查集就不展开过多的讲解了,翻到了自己十年前写过了一篇并查集的文章[并查集学习](https://blog.csdn.net/youngyangyang04/article/details/6447435),哈哈,那时候还太年轻,写不咋地,有空我会重写并查集基础篇! - # 其他语言版本 diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 149eab0148..8c56afdc08 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -104,7 +104,7 @@ if (vec.size() > 0) { } ``` -在来看情况三,明确没有入度为2的情况,那么一定有有向环,找到构成环的边就是要删除的边。 +在来看情况三,明确没有入度为2的情况,那么一定有向环,找到构成环的边就是要删除的边。 可以定义一个函数,代码如下: @@ -122,7 +122,7 @@ vector getRemoveEdge(const vector>& edges) **因为如果两个点所在的边在添加图之前如果就可以在并查集里找到了相同的根,那么这条边添加上之后 这个图一定不是树了** -这里对并查集就不展开过多的讲解了,翻到了自己十年前写过了一篇并查集的文章[并查集学习](https://blog.csdn.net/youngyangyang04/article/details/6447435),哈哈,那时候还太年轻,写不咋地,有空我会重写一篇! +如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html) 本题C++代码如下:(详细注释了) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 75135749bd..c59ae868ce 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -33,10 +33,12 @@ * n 将在 [1, 10000]之间。 * nums 的每个元素都将在 [-9999, 9999]之间。 +## 算法公开课 + +***[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -## 思路 -为了易于大家理解,我还录制了视频,可以看这里:[B站:手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715) +## 思路 **这道题目的前提是数组为有序数组**,同时题目还强调**数组中无重复元素**,因为一旦有重复元素,使用二分查找法返回的元素下标可能不是唯一的,这些都是使用二分法的前提条件,当大家看到题目描述满足如上条件的时候,可要想一想是不是可以用二分法了。 diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index aa04d0e1af..87e5f5a933 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -24,9 +24,13 @@ ![707示例](https://code-thinking-1253855093.file.myqcloud.com/pics/20200814200558953.png) -# 思路 -为了方便大家理解,我特意录制了视频:[帮你把链表操作学个通透!LeetCode:707.设计链表](https://www.bilibili.com/video/BV1FU4y1X7WD),结合视频在看本题解,事半功倍。 +# 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你把链表操作学个通透!LeetCode:707.设计链表](https://www.bilibili.com/video/BV1FU4y1X7WD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +# 思路 如果对链表的基础知识还不太懂,可以看这篇文章:[关于链表,你该了解这些!](https://programmercarl.com/链表理论基础.html) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index a316096e20..de06c41925 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -13,17 +13,20 @@ 给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。 示例 1: -输入:nums = [-4,-1,0,3,10] -输出:[0,1,9,16,100] -解释:平方后,数组变为 [16,1,0,9,100],排序后,数组变为 [0,1,9,16,100] +* 输入:nums = [-4,-1,0,3,10] +* 输出:[0,1,9,16,100] +* 解释:平方后,数组变为 [16,1,0,9,100],排序后,数组变为 [0,1,9,16,100] 示例 2: -输入:nums = [-7,-3,2,3,11] -输出:[4,9,9,49,121] +* 输入:nums = [-7,-3,2,3,11] +* 输出:[4,9,9,49,121] -# 思路 +# 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[双指针法经典题目!LeetCode:977.有序数组的平方](https://www.bilibili.com/video/BV1QB4y1D7ep),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[双指针法经典题目!LeetCode:977.有序数组的平方](https://www.bilibili.com/video/BV1QB4y1D7ep),结合本题解一起看,事半功倍! + +# 思路 ## 暴力排序 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 16c7cb1e85..5f1d894333 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -31,9 +31,9 @@ ## 思路 -本题是并查集基础题目。 +本题是并查集基础题目。 如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html) -首先要知道并查集可以解决什么问题呢? +并查集可以解决什么问题呢? 主要就是集合问题,两个节点在不在一个集合,也可以将两个节点添加到一个集合中。 @@ -70,7 +70,7 @@ void join(int u, int v) { } ``` -以上模板中,只要修改 n 大小就可以,本科n不会超过2 * 10^5。 +以上模板中,只要修改 n 大小就可以,本题n不会超过2 * 10^5。 并查集主要有三个功能。 From 0129bde1d87599790af4ad5dc32cc2cd05320e16 Mon Sep 17 00:00:00 2001 From: Nihilism <114405451+Nihilism0@users.noreply.github.com> Date: Fri, 14 Jul 2023 13:56:34 +0800 Subject: [PATCH 0506/1533] =?UTF-8?q?=E4=BC=98=E5=8C=96=200151.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8DGo=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...214\347\232\204\345\215\225\350\257\215.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 6dd3cd4975..ee944089d3 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -546,26 +546,28 @@ func reverseWords(s string) string { b = b[:slowIndex] } //2.反转整个字符串 - reverse(&b, 0, len(b)-1) + reverse(b) //3.反转单个单词 i单词开始位置,j单词结束位置 i := 0 for i < len(b) { j := i for ; j < len(b) && b[j] != ' '; j++ { } - reverse(&b, i, j-1) + reverse(b[i:j]) i = j i++ } return string(b) } -func reverse(b *[]byte, left, right int) { - for left < right { - (*b)[left], (*b)[right] = (*b)[right], (*b)[left] - left++ - right-- - } +func reverse(b []byte) { + left := 0 + right := len(b) - 1 + for left < right { + b[left], b[right] = b[right], b[left] + left++ + right-- + } } ``` From 8887adbb109acd58ac65bea198d1dc287d99db42 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: Fri, 14 Jul 2023 21:52:24 +0800 Subject: [PATCH 0507/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\345\271\277\346\220\234\347\211\210.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 39af9f50a3..4a990a445b 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -196,6 +196,41 @@ class Solution { } } ``` + +Python: + +```python +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + ans = 0 + + def bfs(x, y): + q = deque() + q.append([x, y]) + visited[x][y] = True + while q: + curx, cury = q.popleft() + for d in dirs: + nextx = curx + d[0] + nexty = cury + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + if visited[nextx][nexty] == False and grid[nextx][nexty] == "1": + q.append([nextx, nexty]) + visited[nextx][nexty] = True # 只要加入队列立刻标记 + + for i in range(m): + for j in range(n): + if grid[i][j] == "1" and not visited[i][j]: + ans += 1 # 遇到没访问过的陆地,+1 + bfs(i, j) # 将与其链接的陆地都标记上 true + + return ans +``` +

From 9b5c5ebeea85b133514ab42c0218b79e13599b45 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: Fri, 14 Jul 2023 22:07:13 +0800 Subject: [PATCH 0508/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index c30ace1999..f610e32330 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -218,6 +218,67 @@ class Solution { } } ``` + +Python: + +```python +# 版本一 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + if not visited[nextx][nexty] and grid[nextx][nexty] == '1': # 没有访问过的同时是陆地的 + visited[nextx][nexty] = True + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + visited[i][j] = True + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` + +```python +# 版本二 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + if visited[x][y] or grid[x][y] == '0': + return # 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = True + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` +

From e5d3d1188555ea83aa00c156afcc249d4fd1fe42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 16 Jul 2023 20:39:00 +0800 Subject: [PATCH 0509/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...207\217.\345\271\277\346\220\234\347\211\210.md" | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 2bb0973a11..c20fe4f1aa 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -196,11 +196,8 @@ class Solution { } } ``` -

- - - -``` + +## 其他语言版本 ### Python BFS solution ```python @@ -241,3 +238,9 @@ class Solution: q.append((next_i, next_j)) visited[next_i][next_j] = True ``` + +

+ + + +``` From 5794b17ab4ccb5da0411896eda509c503014d7d6 Mon Sep 17 00:00:00 2001 From: TonySu Date: Sun, 16 Jul 2023 22:00:05 +0800 Subject: [PATCH 0510/1533] =?UTF-8?q?Update=200039.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md,=20=E4=BF=AE=E5=A4=8DPython=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E4=B8=80=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 版本的回溯剪枝版本一当中, break 会终止整个for 循环,从而提前终止搜索,应该改成continue --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 4d9466c3c0..e09a44e496 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -311,7 +311,7 @@ class Solution: for i in range(startIndex, len(candidates)): if total + candidates[i] > target: - break + continue total += candidates[i] path.append(candidates[i]) self.backtracking(candidates, target, total, i, path, result) From 3a76b3a85e097446ab5f9e7dbb6028bac81c75de Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 11:52:50 +0800 Subject: [PATCH 0511/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200704.=E4=BA=8C?= =?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\210\206\346\237\245\346\211\276.md" | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index c59ae868ce..d202dfa184 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -35,7 +35,7 @@ ## 算法公开课 -***[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -160,7 +160,7 @@ public: ## 其他语言版本 -**Java:** +### **Java:** (版本一)左闭右闭区间 @@ -206,7 +206,7 @@ class Solution { } ``` -**Python:** +### **Python:** (版本一)左闭右闭区间 @@ -246,7 +246,7 @@ class Solution: return -1 # 未找到目标值 ``` -**Go:** +### **Go:** (版本一)左闭右闭区间 @@ -288,7 +288,7 @@ func search(nums []int, target int) int { } ``` -**JavaScript:** +### **JavaScript:** (版本一)左闭右闭区间 [left, right] ```js @@ -345,7 +345,7 @@ var search = function(nums, target) { }; ``` -**TypeScript** +### **TypeScript** (版本一)左闭右闭区间 @@ -387,7 +387,7 @@ function search(nums: number[], target: number): number { }; ``` -**Ruby:** +### **Ruby:** ```ruby # (版本一)左闭右闭区间 @@ -425,7 +425,7 @@ def search(nums, target) end ``` -**Swift:** +### **Swift:** ```swift // (版本一)左闭右闭区间 @@ -479,7 +479,7 @@ func search(nums: [Int], target: Int) -> Int { ``` -**Rust:** +### **Rust:** ```rust # (版本一)左闭右闭区间 @@ -523,7 +523,8 @@ impl Solution { } ``` -**C:** +### **C:** + ```c // (版本一) 左闭右闭区间 [left, right] int search(int* nums, int numsSize, int target){ @@ -575,7 +576,8 @@ int search(int* nums, int numsSize, int target){ } ``` -**PHP:** +### **PHP:** + ```php // 左闭右闭区间 class Solution { @@ -607,7 +609,8 @@ class Solution { } ``` -**C#:** +### **C#:** + ```csharp //左闭右闭 public class Solution { @@ -652,7 +655,8 @@ public class Solution{ } ``` -**Kotlin:** +### **Kotlin:** + ```kotlin class Solution { fun search(nums: IntArray, target: Int): Int { @@ -682,9 +686,8 @@ class Solution { } ``` +### **Kotlin:** - -**Kotlin:** ```Kotlin // (版本一)左闭右开区间 class Solution { @@ -715,7 +718,7 @@ class Solution { } } ``` -**Scala:** +### **Scala:** (版本一)左闭右闭区间 ```scala @@ -763,3 +766,4 @@ object Solution { + From 948aca860da277d744c9fb130039fe96dd79491f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 14:59:20 +0800 Subject: [PATCH 0512/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200704.=E4=BA=8C?= =?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index d202dfa184..52abf57851 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 704. 二分查找 +# 704. 二分查找 [力扣题目链接](https://leetcode.cn/problems/binary-search/) @@ -766,4 +766,3 @@ object Solution { - From fdbc7442ac2b685414544f0af79d64c40da05d0f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:11:55 +0800 Subject: [PATCH 0513/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.md" | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 3d43a199b0..ce9eccf00b 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 27. 移除元素 +# 27. 移除元素 [力扣题目链接](https://leetcode.cn/problems/remove-element/) @@ -159,8 +159,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int removeElement(int[] nums, int val) { @@ -197,7 +197,7 @@ class Solution { } ``` -Python: +### Python: ``` python 3 @@ -233,8 +233,8 @@ class Solution: ``` +### Go: -Go: ```go func removeElement(nums []int, val int) int { length:=len(nums) @@ -275,7 +275,8 @@ func removeElement(nums []int, val int) int { } ``` -JavaScript: +### JavaScript: + ```javascript //时间复杂度:O(n) //空间复杂度:O(1) @@ -290,7 +291,7 @@ var removeElement = (nums, val) => { }; ``` -TypeScript: +### TypeScript: ```typescript function removeElement(nums: number[], val: number): number { @@ -305,7 +306,7 @@ function removeElement(nums: number[], val: number): number { }; ``` -Ruby: +### Ruby: ```ruby def remove_element(nums, val) @@ -319,7 +320,8 @@ def remove_element(nums, val) i end ``` -Rust: +### Rust: + ```rust impl Solution { pub fn remove_element(nums: &mut Vec, val: i32) -> i32 { @@ -335,7 +337,7 @@ impl Solution { } ``` -Swift: +### Swift: ```swift func removeElement(_ nums: inout [Int], _ val: Int) -> Int { @@ -351,7 +353,8 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -375,7 +378,8 @@ class Solution { } ``` -C: +### C: + ```c int removeElement(int* nums, int numsSize, int val){ int slow = 0; @@ -391,7 +395,8 @@ int removeElement(int* nums, int numsSize, int val){ } ``` -Kotlin: +### Kotlin: + ```kotlin fun removeElement(nums: IntArray, `val`: Int): Int { var slowIndex = 0 // 初始化慢指针 @@ -402,7 +407,8 @@ fun removeElement(nums: IntArray, `val`: Int): Int { } ``` -Scala: +### Scala: + ```scala object Solution { def removeElement(nums: Array[Int], `val`: Int): Int = { @@ -418,7 +424,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int RemoveElement(int[] nums, int val) { From ec101bebb24b9af7910c02b979b198c238ae12b1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:21:00 +0800 Subject: [PATCH 0514/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200977.=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\345\271\263\346\226\271.md" | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index de06c41925..4bee585ba8 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -21,14 +21,14 @@ * 输入:nums = [-7,-3,2,3,11] * 输出:[4,9,9,49,121] -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[双指针法经典题目!LeetCode:977.有序数组的平方](https://www.bilibili.com/video/BV1QB4y1D7ep),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 -## 暴力排序 +### 暴力排序 最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下: @@ -47,7 +47,7 @@ public: 这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。 -## 双指针法 +### 双指针法 数组其实是有序的, 只不过负数平方之后可能成为最大数了。 @@ -99,7 +99,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] sortedSquares(int[] nums) { @@ -141,7 +142,8 @@ class Solution { } ``` -Python: +### Python: + ```Python (版本一)双指针法 class Solution: @@ -176,7 +178,8 @@ class Solution: return sorted(x*x for x in nums) ``` -Go: +### Go: + ```Go func sortedSquares(nums []int) []int { n := len(nums) @@ -196,7 +199,8 @@ func sortedSquares(nums []int) []int { return ans } ``` -Rust +### Rust: + ```rust impl Solution { pub fn sorted_squares(nums: Vec) -> Vec { @@ -217,7 +221,8 @@ impl Solution { } } ``` -Javascript: +### Javascript: + ```Javascript /** * @param {number[]} nums @@ -242,7 +247,7 @@ var sortedSquares = function(nums) { }; ``` -Typescript: +### Typescript: 双指针法: @@ -277,7 +282,7 @@ function sortedSquares(nums: number[]): number[] { }; ``` -Swift: +### Swift: ```swift func sortedSquares(_ nums: [Int]) -> [Int] { @@ -305,7 +310,7 @@ func sortedSquares(_ nums: [Int]) -> [Int] { } ``` -Ruby: +### Ruby: ```ruby def sorted_squares(nums) @@ -323,8 +328,8 @@ def sorted_squares(nums) end ``` +### C: -C: ```c int* sortedSquares(int* nums, int numsSize, int* returnSize){ //返回的数组大小就是原数组大小 @@ -357,7 +362,8 @@ int* sortedSquares(int* nums, int numsSize, int* returnSize){ } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -386,7 +392,7 @@ class Solution { } ``` -Kotlin: +### Kotlin: 双指针法 ```kotlin @@ -437,7 +443,7 @@ class Solution { } ``` -Scala: +### Scala: 双指针: ```scala @@ -473,7 +479,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int[] SortedSquares(int[] nums) { @@ -504,3 +511,4 @@ public class Solution { + From 76c84efca0bcfab9e889bf97458dd6b6423e0f05 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:32:24 +0800 Subject: [PATCH 0515/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200209.=20?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E7=BB=84=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\346\225\260\347\273\204.md" | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 82d551ea44..4b1d0e9608 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -23,14 +23,14 @@ * 1 <= nums.length <= 10^5 * 1 <= nums[i] <= 10^5 -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 -## 暴力解法 +### 暴力解法 这道题目暴力解法当然是 两个for循环,然后不断的寻找符合条件的子序列,时间复杂度很明显是O(n^2)。 @@ -64,7 +64,7 @@ public: 后面力扣更新了数据,暴力解法已经超时了。 -## 滑动窗口 +### 滑动窗口 接下来就开始介绍数组操作中另一个重要的方法:**滑动窗口**。 @@ -151,8 +151,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { @@ -173,7 +173,7 @@ class Solution { } ``` -Python: +### Python: ```python (版本一)滑动窗口法 @@ -216,7 +216,8 @@ class Solution: return min_len if min_len != float('inf') else 0 ``` -Go: +### Go: + ```go func minSubArrayLen(target int, nums []int) int { i := 0 @@ -242,8 +243,7 @@ func minSubArrayLen(target int, nums []int) int { } ``` - -JavaScript: +### JavaScript: ```js var minSubArrayLen = function(target, nums) { @@ -266,7 +266,7 @@ var minSubArrayLen = function(target, nums) { }; ``` -Typescript: +### Typescript: ```typescript function minSubArrayLen(target: number, nums: number[]): number { @@ -288,7 +288,7 @@ function minSubArrayLen(target: number, nums: number[]): number { }; ``` -Swift: +### Swift: ```swift func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { @@ -309,7 +309,7 @@ func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -336,7 +336,8 @@ impl Solution { } ``` -PHP: +### PHP: + ```php // 双指针 - 滑动窗口 class Solution { @@ -365,7 +366,7 @@ class Solution { } ``` -Ruby: +### Ruby: ```ruby def min_sub_array_len(target, nums) @@ -383,8 +384,9 @@ def min_sub_array_len(target, nums) end ``` -C: +### C: 暴力解法: + ```c int minSubArrayLen(int target, int* nums, int numsSize){ //初始化最小长度为INT_MAX @@ -433,7 +435,8 @@ int minSubArrayLen(int target, int* nums, int numsSize){ } ``` -Kotlin: +### Kotlin: + ```kotlin class Solution { fun minSubArrayLen(target: Int, nums: IntArray): Int { @@ -485,7 +488,7 @@ class Solution { } } ``` -Scala: +### Scala: 滑动窗口: ```scala @@ -533,7 +536,8 @@ object Solution { } } ``` -C#: +### C#: + ```csharp public class Solution { public int MinSubArrayLen(int s, int[] nums) { @@ -559,3 +563,4 @@ public class Solution { + From 7a30d500cb5f328c3e4c952f2f9ae0ecb3382b45 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:47:48 +0800 Subject: [PATCH 0516/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20059.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\227\213\347\237\251\351\230\265II.md" | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index fd40f3fc52..f03fcdad35 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -26,7 +26,7 @@ ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下螺旋矩阵!LeetCode:59.螺旋矩阵II](https://www.bilibili.com/video/BV1SL4y1N7mV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - + ## 思路 这道题目可以说在面试中出现频率较高的题目,**本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。** @@ -125,15 +125,15 @@ public: ## 类似题目 -* 54.螺旋矩阵 -* 剑指Offer 29.顺时针打印矩阵 +* [54.螺旋矩阵](https://leetcode.cn/problems/spiral-matrix/) +* [剑指Offer 29.顺时针打印矩阵](https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/) ## 其他语言版本 -Java: +### Java: ```Java class Solution { @@ -176,7 +176,7 @@ class Solution { } ``` -python3: +### python3: ```python class Solution: @@ -207,7 +207,7 @@ class Solution: return nums ``` -javaScript +### JavaScript: ```javascript @@ -259,7 +259,7 @@ var generateMatrix = function(n) { ``` -TypeScript: +### TypeScript: ```typescript function generateMatrix(n: number): number[][] { @@ -304,7 +304,7 @@ function generateMatrix(n: number): number[][] { }; ``` -Go: +### Go: ```go package main @@ -397,7 +397,7 @@ func generateMatrix(n int) [][]int { } ``` -Swift: +### Swift: ```swift func generateMatrix(_ n: Int) -> [[Int]] { @@ -453,7 +453,7 @@ func generateMatrix(_ n: Int) -> [[Int]] { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -506,7 +506,8 @@ impl Solution { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -548,7 +549,8 @@ class Solution { } ``` -C: +### C: + ```c int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){ //初始化返回的结果数组的大小 @@ -607,7 +609,8 @@ int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){ return ans; } ``` -Scala: +### Scala: + ```scala object Solution { def generateMatrix(n: Int): Array[Array[Int]] = { @@ -659,7 +662,8 @@ object Solution { } } ``` -C#: +### C#: + ```csharp public class Solution { public int[][] GenerateMatrix(int n) { @@ -688,3 +692,4 @@ public class Solution { + From 053632c31d05cae6391b29db2ec76f1945eb2cfe Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:51:53 +0800 Subject: [PATCH 0517/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...3\204\346\200\273\347\273\223\347\257\207.md" | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index ef962187fe..7550ce0254 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -4,9 +4,9 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+# 数组总结篇 - -# 数组理论基础 +## 数组理论基础 数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力 @@ -51,7 +51,7 @@ 所以**Java的二维数组在内存中不是 `3*4` 的连续地址空间,而是四条连续的地址空间组成!** -# 数组的经典题目 +## 数组的经典题目 在面试中,数组是必考的基础数据结构。 @@ -59,7 +59,7 @@ 我们之前一共讲解了四道经典数组题目,每一道题目都代表一个类型,一种思想。 -## 二分法 +### 二分法 [数组:每次遇到二分法,都是一看就会,一写就废](https://programmercarl.com/0704.二分查找.html) @@ -75,7 +75,7 @@ **二分法是算法面试中的常考题,建议通过这道题目,锻炼自己手撕二分的能力**。 -## 双指针法 +### 双指针法 * [数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html) @@ -91,7 +91,7 @@ 双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组和链表操作的面试题,都使用双指针法。 -## 滑动窗口 +### 滑动窗口 * [数组:滑动窗口拯救了你](https://programmercarl.com/0209.长度最小的子数组.html) @@ -107,7 +107,7 @@ 如果没有接触过这一类的方法,很难想到类似的解题思路,滑动窗口方法还是很巧妙的。 -## 模拟行为 +### 模拟行为 * [数组:这个循环可以转懵很多人!](https://programmercarl.com/0059.螺旋矩阵II.html) @@ -118,7 +118,7 @@ 相信大家有遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,拆了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。 -# 总结 +## 总结 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/数组总结.png) From a3cc34ef7413e7e80420674bd261e4de393f152c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:53:46 +0800 Subject: [PATCH 0518/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=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 --- ...\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index 67b7b20d1b..d104c883f7 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,7 +6,7 @@ -## 数组理论基础 +# 数组理论基础 数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力 From b51a1d89bf6b340f3a96ba7f109c1d481a887693 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:21:20 +0800 Subject: [PATCH 0519/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=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 --- ...06\350\256\272\345\237\272\347\241\200.md" | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index a09974df7c..da5a1b0244 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -16,15 +16,15 @@ 如图所示: ![链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194529815.png) -# 链表的类型 +## 链表的类型 接下来说一下链表的几种类型: -## 单链表 +### 单链表 刚刚说的就是单链表。 -## 双链表 +### 双链表 单链表中的指针域只能指向节点的下一个节点。 @@ -35,7 +35,7 @@ 如图所示: ![链表2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194559317.png) -## 循环链表 +### 循环链表 循环链表,顾名思义,就是链表首尾相连。 @@ -44,7 +44,7 @@ ![链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194629603.png) -# 链表的存储方式 +## 链表的存储方式 了解完链表的类型,再来说一说链表在内存中的存储方式。 @@ -60,7 +60,7 @@ 这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。 -# 链表的定义 +## 链表的定义 接下来说一说链表的定义。 @@ -100,9 +100,9 @@ head->val = 5; 所以如果不定义构造函数使用默认构造函数的话,在初始化的时候就不能直接给变量赋值! -# 链表的操作 +## 链表的操作 -## 删除节点 +### 删除节点 删除D节点,如图所示: @@ -116,7 +116,7 @@ head->val = 5; 其他语言例如Java、Python,就有自己的内存回收机制,就不用自己手动释放了。 -## 添加节点 +### 添加节点 如图所示: @@ -126,7 +126,7 @@ head->val = 5; 但是要注意,要是删除第五个节点,需要从头节点查找到第四个节点通过next指针进行删除操作,查找的时间复杂度是O(n)。 -# 性能分析 +## 性能分析 再把链表的特性和数组的特性进行一个对比,如图所示: @@ -143,8 +143,7 @@ head->val = 5; ## 其他语言版本 - -Java: +### Java: ```java public class ListNode { @@ -171,7 +170,7 @@ public class ListNode { } ``` -JavaScript: +### JavaScript: ```javascript class ListNode { @@ -184,7 +183,7 @@ class ListNode { } ``` -TypeScript: +### TypeScript: ```typescript class ListNode { @@ -197,7 +196,7 @@ class ListNode { } ``` -Python: +### Python: ```python class ListNode: @@ -206,7 +205,7 @@ class ListNode: self.next = next ``` -Go: +### Go: ```go type ListNode struct { @@ -215,7 +214,7 @@ type ListNode struct { } ``` -Scala: +### Scala: ```scala class ListNode(_x: Int = 0, _next: ListNode = null) { @@ -224,7 +223,7 @@ class ListNode(_x: Int = 0, _next: ListNode = null) { } ``` -Rust: +### Rust: ```rust #[derive(PartialEq, Eq, Clone, Debug)] @@ -246,3 +245,4 @@ impl ListNode { + From a78116889f705d9e7f72f927decb0a9e844bd807 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:28:12 +0800 Subject: [PATCH 0520/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=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 --- ...76\350\241\250\345\205\203\347\264\240.md" | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 300f98e911..f09c572b95 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -27,14 +27,12 @@ 输入:head = [7,7,7,7], val = 7 输出:[] -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 - -为了方便大家理解,我特意录制了视频:[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),结合视频在看本题解,事半功倍。 +## 思路 这里以链表 1 4 2 4 来举例,移除元素4。 @@ -91,7 +89,7 @@ 最后呢在题目中,return 头结点的时候,别忘了 `return dummyNode->next;`, 这才是新的头结点 -# C++代码 +### C++代码 **直接使用原来的链表来进行移除节点操作:** @@ -159,7 +157,7 @@ public: ## 其他语言版本 -C: +### C: 用原来的链表操作: ```c @@ -227,7 +225,7 @@ struct ListNode* removeElements(struct ListNode* head, int val){ } ``` -Java: +### Java: ```java /** @@ -308,7 +306,7 @@ public ListNode removeElements(ListNode head, int val) { } ``` -Python: +### Python: ```python (版本一)虚拟头节点法 @@ -334,7 +332,7 @@ class Solution: ``` -Go: +### Go: ```go /** @@ -359,7 +357,7 @@ func removeElements(head *ListNode, val int) *ListNode { } ``` -javaScript: +### JavaScript: ```js /** @@ -381,7 +379,7 @@ var removeElements = function(head, val) { }; ``` -TypeScript: +### TypeScript: 版本一(在原链表上直接删除): @@ -437,7 +435,7 @@ function removeElements(head: ListNode | null, val: number): ListNode | null { }; ``` -Swift: +### Swift: ```swift /** @@ -465,7 +463,7 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? { } ``` -PHP: +### PHP: ```php /** @@ -493,7 +491,7 @@ func removeElements(head *ListNode, val int) *ListNode { } ``` -RUST: +### Rust: ```rust // Definition for singly-linked list. @@ -531,7 +529,7 @@ impl Solution { } ``` -Scala: +### Scala: ```scala /** @@ -564,7 +562,7 @@ object Solution { } ``` -Kotlin: +### Kotlin: ```kotlin /** @@ -600,7 +598,8 @@ class Solution { } ``` -C# +### C# + ```CSharp /** * Definition for singly-linked list. From 61366ff59037d5f22f256e5bc410efd8a36a35d3 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:41:02 +0800 Subject: [PATCH 0521/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200707.=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 4 +-- ...76\350\256\241\351\223\276\350\241\250.md" | 34 ++++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index f09c572b95..c8f802a183 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -88,9 +88,6 @@ 最后呢在题目中,return 头结点的时候,别忘了 `return dummyNode->next;`, 这才是新的头结点 - -### C++代码 - **直接使用原来的链表来进行移除节点操作:** ```CPP @@ -638,3 +635,4 @@ public class Solution + diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 3f096a2246..c27f0107fb 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -25,12 +25,12 @@ ![707示例](https://code-thinking-1253855093.file.myqcloud.com/pics/20200814200558953.png) -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你把链表操作学个通透!LeetCode:707.设计链表](https://www.bilibili.com/video/BV1FU4y1X7WD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 如果对链表的基础知识还不太懂,可以看这篇文章:[关于链表,你该了解这些!](https://programmercarl.com/链表理论基础.html) @@ -58,8 +58,6 @@ 下面采用的设置一个虚拟头结点(这样更方便一些,大家看代码就会感受出来)。 - -## 代码 ```CPP class MyLinkedList { public: @@ -167,7 +165,8 @@ private: ## 其他语言版本 -C: +### C: + ```C typedef struct MyLinkedList { int val; @@ -291,7 +290,8 @@ void myLinkedListFree(MyLinkedList* obj) { */ ``` -Java: +### Java: + ```Java //单链表 class ListNode { @@ -487,7 +487,8 @@ class MyLinkedList { */ ``` -Python: +### Python: + ```python (版本一)单链表法 class ListNode: @@ -661,7 +662,7 @@ class MyLinkedList: # obj.deleteAtIndex(index) ``` -Go: +### Go: ```go //单链表实现 @@ -915,7 +916,7 @@ func (this *MyLinkedList) DeleteAtIndex(index int) { } ``` -javaScript: +### JavaScript: ```js @@ -1055,7 +1056,8 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { */ ``` -TypeScript: +### TypeScript: + ```TypeScript class ListNode { public val: number; @@ -1173,7 +1175,8 @@ class MyLinkedList { } ``` -Kotlin: +### Kotlin: + ```kotlin class MyLinkedList { @@ -1241,8 +1244,7 @@ class MyLinkedList { } ``` - -Swift: +### Swift: ```swift class MyLinkedList { @@ -1323,7 +1325,8 @@ class MyLinkedList { } ``` -Scala: +### Scala: + ```scala class ListNode(_x: Int = 0, _next: ListNode = null) { var next: ListNode = _next @@ -1393,7 +1396,7 @@ class MyLinkedList() { } ``` -Rust: +### Rust: ```rust #[derive(Debug)] @@ -1486,4 +1489,3 @@ impl MyLinkedList { - From 32bf073bc8c2fe08aabc9aaf0ef61184febfb0a8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:50:44 +0800 Subject: [PATCH 0522/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200206.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\350\275\254\351\223\276\350\241\250.md" | 745 ++++++++++++++++++ ...73\350\275\254\351\223\276\350\241\250.md" | 55 +- 2 files changed, 772 insertions(+), 28 deletions(-) create mode 100644 "problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" diff --git "a/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" new file mode 100644 index 0000000000..5a57939af2 --- /dev/null +++ "b/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" @@ -0,0 +1,745 @@ +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ + +> 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。 + +# 206.反转链表 + +[力扣题目链接](https://leetcode.cn/problems/reverse-linked-list/) + +题意:反转一个单链表。 + +示例: +输入: 1->2->3->4->5->NULL +输出: 5->4->3->2->1->NULL + +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +## 思路 + +如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。 + +其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: + + +![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png) + +之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 + +那么接下来看一看是如何反转的呢? + +我们拿有示例中的链表来举例,如动画所示:(纠正:动画应该是先移动pre,在移动cur) + +![](https://code-thinking.cdn.bcebos.com/gifs/206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.gif) + +首先定义一个cur指针,指向头结点,再定义一个pre指针,初始化为null。 + +然后就要开始反转了,首先要把 cur->next 节点用tmp指针保存一下,也就是保存一下这个节点。 + +为什么要保存一下这个节点呢,因为接下来要改变 cur->next 的指向了,将cur->next 指向pre ,此时已经反转了第一个节点了。 + +接下来,就是循环走如下代码逻辑了,继续移动pre和cur指针。 + +最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。 + +### 双指针法 +```CPP +class Solution { +public: + ListNode* reverseList(ListNode* head) { + ListNode* temp; // 保存cur的下一个节点 + ListNode* cur = head; + ListNode* pre = NULL; + while(cur) { + temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next + cur->next = pre; // 翻转操作 + // 更新pre 和 cur指针 + pre = cur; + cur = temp; + } + return pre; + } +}; +``` + +* 时间复杂度: O(n) +* 空间复杂度: O(1) + +### 递归法 + +递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。 + +关键是初始化的地方,可能有的同学会不理解, 可以看到双指针法中初始化 cur = head,pre = NULL,在递归法中可以从如下代码看出初始化的逻辑也是一样的,只不过写法变了。 + +具体可以看代码(已经详细注释),**双指针法写出来之后,理解如下递归写法就不难了,代码逻辑都是一样的。** +```CPP +class Solution { +public: + ListNode* reverse(ListNode* pre,ListNode* cur){ + if(cur == NULL) return pre; + ListNode* temp = cur->next; + cur->next = pre; + // 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步 + // pre = cur; + // cur = temp; + return reverse(cur,temp); + } + ListNode* reverseList(ListNode* head) { + // 和双指针法初始化是一样的逻辑 + // ListNode* cur = head; + // ListNode* pre = NULL; + return reverse(NULL, head); + } + +}; +``` + +* 时间复杂度: O(n), 要递归处理链表的每个节点 +* 空间复杂度: O(n), 递归调用了 n 层栈空间 + +我们可以发现,上面的递归写法和双指针法实质上都是从前往后翻转指针指向,其实还有另外一种与双指针法不同思路的递归写法:从后往前翻转指针指向。 + +具体代码如下(带详细注释): + +```CPP +class Solution { +public: + ListNode* reverseList(ListNode* head) { + // 边缘条件判断 + if(head == NULL) return NULL; + if (head->next == NULL) return head; + + // 递归调用,翻转第二个节点开始往后的链表 + ListNode *last = reverseList(head->next); + // 翻转头节点与第二个节点的指向 + head->next->next = head; + // 此时的 head 节点为尾节点,next 需要指向 NULL + head->next = NULL; + return last; + } +}; +``` + +* 时间复杂度: O(n) +* 空间复杂度: O(n) + + +## 其他语言版本 + +### Java: + +```java +// 双指针 +class Solution { + public ListNode reverseList(ListNode head) { + ListNode prev = null; + ListNode cur = head; + ListNode temp = null; + while (cur != null) { + temp = cur.next;// 保存下一个节点 + cur.next = prev; + prev = cur; + cur = temp; + } + return prev; + } +} +``` + +```java +// 递归 +class Solution { + public ListNode reverseList(ListNode head) { + return reverse(null, head); + } + + private ListNode reverse(ListNode prev, ListNode cur) { + if (cur == null) { + return prev; + } + ListNode temp = null; + temp = cur.next;// 先保存下一个节点 + cur.next = prev;// 反转 + // 更新prev、cur位置 + // prev = cur; + // cur = temp; + return reverse(cur, temp); + } +} +``` + +```java +// 从后向前递归 +class Solution { + ListNode reverseList(ListNode head) { + // 边缘条件判断 + if(head == null) return null; + if (head.next == null) return head; + + // 递归调用,翻转第二个节点开始往后的链表 + ListNode last = reverseList(head.next); + // 翻转头节点与第二个节点的指向 + head.next.next = head; + // 此时的 head 节点为尾节点,next 需要指向 NULL + head.next = null; + return last; + } +} +``` + +### Python: + +```python +(版本一)双指针法 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: ListNode) -> ListNode: + cur = head + pre = None + while cur: + temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next + cur.next = pre #反转 + #更新pre、cur指针 + pre = cur + cur = temp + return pre +``` + +```python +(版本二)递归法 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: ListNode) -> ListNode: + return self.reverse(head, None) + def reverse(self, cur: ListNode, pre: ListNode) -> ListNode: + if cur == None: + return pre + temp = cur.next + cur.next = pre + return self.reverse(temp, cur) + +``` + + + +### Go: + +```go +//双指针 +func reverseList(head *ListNode) *ListNode { + var pre *ListNode + cur := head + for cur != nil { + next := cur.Next + cur.Next = pre + pre = cur + cur = next + } + return pre +} + +//递归 +func reverseList(head *ListNode) *ListNode { + return help(nil, head) +} + +func help(pre, head *ListNode)*ListNode{ + if head == nil { + return pre + } + next := head.Next + head.Next = pre + return help(head, next) +} + +``` +### JavaScript: + +```js +/** + * @param {ListNode} head + * @return {ListNode} + */ + +// 双指针: +var reverseList = function(head) { + if(!head || !head.next) return head; + let temp = null, pre = null, cur = head; + while(cur) { + temp = cur.next; + cur.next = pre; + pre = cur; + cur = temp; + } + // temp = cur = null; + return pre; +}; + +// 递归: +var reverse = function(pre, head) { + if(!head) return pre; + const temp = head.next; + head.next = pre; + pre = head + return reverse(pre, temp); +} + +var reverseList = function(head) { + return reverse(null, head); +}; + +// 递归2 +var reverse = function(head) { + if(!head || !head.next) return head; + // 从后往前翻 + const pre = reverse(head.next); + head.next = pre.next; + pre.next = head; + return head; +} + +var reverseList = function(head) { + let cur = head; + while(cur && cur.next) { + cur = cur.next; + } + reverse(head); + return cur; +}; +``` + +### TypeScript: + +```typescript +// 双指针法 +function reverseList(head: ListNode | null): ListNode | null { + let preNode: ListNode | null = null, + curNode: ListNode | null = head, + tempNode: ListNode | null; + while (curNode) { + tempNode = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + } + return preNode; +}; + +// 递归(从前往后翻转) +function reverseList(head: ListNode | null): ListNode | null { + function recur(preNode: ListNode | null, curNode: ListNode | null): ListNode | null { + if (curNode === null) return preNode; + let tempNode: ListNode | null = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + return recur(preNode, curNode); + } + return recur(null, head); +}; + +// 递归(从后往前翻转) +function reverseList(head: ListNode | null): ListNode | null { + if (head === null) return null; + let newHead: ListNode | null; + function recur(node: ListNode | null, preNode: ListNode | null): void { + if (node.next === null) { + newHead = node; + newHead.next = preNode; + } else { + recur(node.next, node); + node.next = preNode; + } + } + recur(head, null); + return newHead; +}; +``` + +### Ruby: + +```ruby +# 双指针 +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end +def reverse_list(head) + # return nil if head.nil? # 循环判断条件亦能起到相同作用因此不必单独判断 + cur, per = head, nil + until cur.nil? + tem = cur.next + cur.next = per + per = cur + cur = tem + end + per +end + +# 递归 +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end +def reverse_list(head) + reverse(nil, head) +end + +def reverse(pre, cur) + return pre if cur.nil? + tem = cur.next + cur.next = pre + reverse(cur, tem) # 通过递归实现双指针法中的更新操作 +end +``` + +### Kotlin: + +```Kotlin +fun reverseList(head: ListNode?): ListNode? { + var pre: ListNode? = null + var cur = head + while (cur != null) { + val temp = cur.next + cur.next = pre + pre = cur + cur = temp + } + return pre +} +``` +```kotlin +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun reverseList(head: ListNode?): ListNode? { + // temp用来存储临时的节点 + var temp: ListNode? + // cur用来遍历链表 + var cur: ListNode? = head + // pre用来作为链表反转的工具 + // pre是比pre前一位的节点 + var pre: ListNode? = null + while (cur != null) { + // 临时存储原本cur的下一个节点 + temp = cur.next + // 使cur下一节点地址为它之前的 + cur.next = pre + // 之后随着cur的遍历移动pre + pre = cur; + // 移动cur遍历链表各个节点 + cur = temp; + } + // 由于开头使用pre为null,所以cur等于链表本身长度+1, + // 此时pre在cur前一位,所以此时pre为头节点 + return pre; + } +} +``` + +### Swift: + +```swift +/// 双指针法 (迭代) +/// - Parameter head: 头结点 +/// - Returns: 翻转后的链表头结点 +func reverseList(_ head: ListNode?) -> ListNode? { + if head == nil || head?.next == nil { + return head + } + var pre: ListNode? = nil + var cur: ListNode? = head + var temp: ListNode? = nil + while cur != nil { + temp = cur?.next + cur?.next = pre + pre = cur + cur = temp + } + return pre +} + +/// 递归 +/// - Parameter head: 头结点 +/// - Returns: 翻转后的链表头结点 +func reverseList2(_ head: ListNode?) -> ListNode? { + return reverse(pre: nil, cur: head) +} +func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? { + if cur == nil { + return pre + } + let temp: ListNode? = cur?.next + cur?.next = pre + return reverse(pre: cur, cur: temp) +} +``` + +### C: +双指针法: + +```c +struct ListNode* reverseList(struct ListNode* head){ + //保存cur的下一个结点 + struct ListNode* temp; + //pre指针指向前一个当前结点的前一个结点 + struct ListNode* pre = NULL; + //用head代替cur,也可以再定义一个cur结点指向head。 + while(head) { + //保存下一个结点的位置 + temp = head->next; + //翻转操作 + head->next = pre; + //更新结点 + pre = head; + head = temp; + } + return pre; +} +``` + +递归法: +```c +struct ListNode* reverse(struct ListNode* pre, struct ListNode* cur) { + if(!cur) + return pre; + struct ListNode* temp = cur->next; + cur->next = pre; + //将cur作为pre传入下一层 + //将temp作为cur传入下一层,改变其指针指向当前cur + return reverse(cur, temp); +} + +struct ListNode* reverseList(struct ListNode* head){ + return reverse(NULL, head); +} +``` + + + +### PHP: + +```php +// 双指针法: +function reverseList($head) { + $cur = $head; + $pre = NULL; + while($cur){ + $temp = $cur->next; + $cur->next = $pre; + $pre = $cur; + $cur = $temp; + } + return $pre; + } +``` + +### Scala: +双指针法: + +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + var pre: ListNode = null + var cur = head + while (cur != null) { + var tmp = cur.next + cur.next = pre + pre = cur + cur = tmp + } + pre + } +} +``` +递归法: +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + reverse(null, head) + } + + def reverse(pre: ListNode, cur: ListNode): ListNode = { + if (cur == null) { + return pre // 如果当前cur为空,则返回pre + } + val tmp: ListNode = cur.next + cur.next = pre + reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点 + } + +} +``` + +### Rust: +双指针法: + +```rust +impl Solution { + pub fn reverse_list(head: Option>) -> Option> { + let mut cur = head; + let mut pre = None; + while let Some(mut node) = cur.take() { + cur = node.next; + node.next = pre; + pre = Some(node); + } + pre + } +} +``` + +递归法: + +```rust +impl Solution { + pub fn reverse_list(head: Option>) -> Option> { + fn rev( + mut head: Option>, + mut pre: Option>, + ) -> Option> { + if let Some(mut node) = head.take() { + let cur = node.next; + node.next = pre; + pre = Some(node); + return rev(cur, pre); + } + pre + } + rev(head, None) + } +} +``` +### C#: +三指针法, 感觉会更直观: + +```cs +public LinkNumbers Reverse() +{ + ///用三指针,写的过程中能够弥补二指针在翻转过程中的想象 + LinkNumbers pre = null; + var move = root; + var next = root; + + while (next != null) + { + next = next.linknext; + move.linknext = pre; + pre = move; + move = next; + } + root = pre; + return root; +} + +///LinkNumbers的定义 +public class LinkNumbers +{ + /// + /// 链表值 + /// + public int value { get; set; } + + /// + /// 链表指针 + /// + public LinkNumbers linknext { get; set; } +} +``` + +## 其他解法 + +### 使用虚拟头结点解决链表反转 + +> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈) + +```java +// 迭代方法:增加虚头结点,使用头插法实现链表翻转 +public static ListNode reverseList1(ListNode head) { + // 创建虚头结点 + ListNode dumpyHead = new ListNode(-1); + dumpyHead.next = null; + // 遍历所有节点 + ListNode cur = head; + while(cur != null){ + ListNode temp = cur.next; + // 头插法 + cur.next = dumpyHead.next; + dumpyHead.next = cur; + cur = temp; + } + return dumpyHead.next; +} +``` + + + +### 使用栈解决反转链表的问题 + +* 首先将所有的结点入栈 +* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 + +```java +public ListNode reverseList(ListNode head) { + // 如果链表为空,则返回空 + if (head == null) return null; + // 如果链表中只有只有一个元素,则直接返回 + if (head.next == null) return head; + // 创建栈 每一个结点都入栈 + Stack stack = new Stack<>(); + ListNode cur = head; + while (cur != null) { + stack.push(cur); + cur = cur.next; + } + // 创建一个虚拟头结点 + ListNode pHead = new ListNode(0); + cur = pHead; + while (!stack.isEmpty()) { + ListNode node = stack.pop(); + cur.next = node; + cur = cur.next; + } + // 最后一个元素的next要赋值为空 + cur.next = null; + return pHead.next; +} +``` + +> 采用这种方法需要注意一点。就是当整个出栈循环结束以后,cur正好指向原来链表的第一个结点,而此时结点1中的next指向的是结点2,因此最后还需要`cur.next = null` +![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png) + +

+ + + diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index c63c998dc7..5a57939af2 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -17,14 +17,12 @@ 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 - -本题我录制了B站视频,[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。 @@ -51,9 +49,7 @@ 最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。 -# C++代码 - -## 双指针法 +### 双指针法 ```CPP class Solution { public: @@ -76,7 +72,7 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1) -## 递归法 +### 递归法 递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。 @@ -137,8 +133,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // 双指针 class Solution { @@ -198,7 +194,8 @@ class Solution { } ``` -Python +### Python: + ```python (版本一)双指针法 # Definition for singly-linked list. @@ -219,8 +216,6 @@ class Solution: return pre ``` -Python递归法: - ```python (版本二)递归法 # Definition for singly-linked list. @@ -242,7 +237,7 @@ class Solution: -Go: +### Go: ```go //双指针 @@ -273,7 +268,7 @@ func help(pre, head *ListNode)*ListNode{ } ``` -javaScript: +### JavaScript: ```js /** @@ -328,7 +323,7 @@ var reverseList = function(head) { }; ``` -TypeScript: +### TypeScript: ```typescript // 双指针法 @@ -376,7 +371,7 @@ function reverseList(head: ListNode | null): ListNode | null { }; ``` -Ruby: +### Ruby: ```ruby # 双指针 @@ -421,7 +416,8 @@ def reverse(pre, cur) end ``` -Kotlin: +### Kotlin: + ```Kotlin fun reverseList(head: ListNode?): ListNode? { var pre: ListNode? = null @@ -471,7 +467,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift /// 双指针法 (迭代) /// - Parameter head: 头结点 @@ -508,8 +505,9 @@ func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? { } ``` -C: +### C: 双指针法: + ```c struct ListNode* reverseList(struct ListNode* head){ //保存cur的下一个结点 @@ -549,7 +547,8 @@ struct ListNode* reverseList(struct ListNode* head){ -PHP: +### PHP: + ```php // 双指针法: function reverseList($head) { @@ -565,8 +564,9 @@ function reverseList($head) { } ``` -Scala: +### Scala: 双指针法: + ```scala object Solution { def reverseList(head: ListNode): ListNode = { @@ -601,7 +601,7 @@ object Solution { } ``` -Rust: +### Rust: 双指针法: ```rust @@ -640,7 +640,7 @@ impl Solution { } } ``` -C#: +### C#: 三指针法, 感觉会更直观: ```cs @@ -677,11 +677,11 @@ public class LinkNumbers } ``` +## 其他解法 +### 使用虚拟头结点解决链表反转 -## 使用虚拟头结点解决链表翻转 - -> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈) +> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈) ```java // 迭代方法:增加虚头结点,使用头插法实现链表翻转 @@ -704,7 +704,7 @@ public static ListNode reverseList1(ListNode head) { -## 使用栈解决反转链表的问题 +### 使用栈解决反转链表的问题 * 首先将所有的结点入栈 * 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 @@ -743,4 +743,3 @@ public ListNode reverseList(ListNode head) { - From 2acceb7474b60ad4aa16eef4b9bf94657ac85aeb Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:00:29 +0800 Subject: [PATCH 0523/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20024.=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 37 +- ...15\350\275\254\351\223\276\350\241\250.md" | 745 ------------------ 2 files changed, 23 insertions(+), 759 deletions(-) delete mode 100644 "problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index d7c03b645b..c612c4b3b9 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 24. 两两交换链表中的节点 +# 24. 两两交换链表中的节点 [力扣题目链接](https://leetcode.cn/problems/swap-nodes-in-pairs/) @@ -16,9 +16,11 @@ 24.两两交换链表中的节点-题意 -## 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -《代码随想录》算法公开课:[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 这道题目正常模拟就可以了。 @@ -88,7 +90,8 @@ public: ## 其他语言版本 -C: +### C: + ```c /** * Definition for singly-linked list. @@ -132,7 +135,7 @@ struct ListNode* swapPairs(struct ListNode* head){ } ``` -Java: +### Java: ```Java // 递归版本 @@ -176,7 +179,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 递归版本 # Definition for singly-linked list. @@ -226,7 +230,8 @@ class Solution: ``` -Go: +### Go: + ```go func swapPairs(head *ListNode) *ListNode { dummy := &ListNode{ @@ -262,7 +267,8 @@ func swapPairs(head *ListNode) *ListNode { } ``` -Javascript: +### Javascript: + ```javascript var swapPairs = function (head) { let ret = new ListNode(0, head), temp = ret; @@ -277,7 +283,7 @@ var swapPairs = function (head) { }; ``` -TypeScript: +### TypeScript: ```typescript function swapPairs(head: ListNode | null): ListNode | null { @@ -296,7 +302,7 @@ function swapPairs(head: ListNode | null): ListNode | null { }; ``` -Kotlin: +### Kotlin: ```kotlin fun swapPairs(head: ListNode?): ListNode? { @@ -316,7 +322,8 @@ fun swapPairs(head: ListNode?): ListNode? { } ``` -Swift: +### Swift: + ```swift func swapPairs(_ head: ListNode?) -> ListNode? { if head == nil || head?.next == nil { @@ -337,7 +344,8 @@ func swapPairs(_ head: ListNode?) -> ListNode? { return dummyHead.next } ``` -Scala: +### Scala: + ```scala // 虚拟头节点 object Solution { @@ -361,7 +369,8 @@ object Solution { } ``` -PHP: +### PHP: + ```php //虚拟头结点 function swapPairs($head) { @@ -404,7 +413,7 @@ function swapPairs($head) } ``` -Rust: +### Rust: ```rust // 虚拟头节点 diff --git "a/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" deleted file mode 100644 index 5a57939af2..0000000000 --- "a/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" +++ /dev/null @@ -1,745 +0,0 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - -> 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。 - -# 206.反转链表 - -[力扣题目链接](https://leetcode.cn/problems/reverse-linked-list/) - -题意:反转一个单链表。 - -示例: -输入: 1->2->3->4->5->NULL -输出: 5->4->3->2->1->NULL - -## 算法公开课 - -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - -## 思路 - -如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。 - -其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: - - -![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png) - -之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 - -那么接下来看一看是如何反转的呢? - -我们拿有示例中的链表来举例,如动画所示:(纠正:动画应该是先移动pre,在移动cur) - -![](https://code-thinking.cdn.bcebos.com/gifs/206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.gif) - -首先定义一个cur指针,指向头结点,再定义一个pre指针,初始化为null。 - -然后就要开始反转了,首先要把 cur->next 节点用tmp指针保存一下,也就是保存一下这个节点。 - -为什么要保存一下这个节点呢,因为接下来要改变 cur->next 的指向了,将cur->next 指向pre ,此时已经反转了第一个节点了。 - -接下来,就是循环走如下代码逻辑了,继续移动pre和cur指针。 - -最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。 - -### 双指针法 -```CPP -class Solution { -public: - ListNode* reverseList(ListNode* head) { - ListNode* temp; // 保存cur的下一个节点 - ListNode* cur = head; - ListNode* pre = NULL; - while(cur) { - temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next - cur->next = pre; // 翻转操作 - // 更新pre 和 cur指针 - pre = cur; - cur = temp; - } - return pre; - } -}; -``` - -* 时间复杂度: O(n) -* 空间复杂度: O(1) - -### 递归法 - -递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。 - -关键是初始化的地方,可能有的同学会不理解, 可以看到双指针法中初始化 cur = head,pre = NULL,在递归法中可以从如下代码看出初始化的逻辑也是一样的,只不过写法变了。 - -具体可以看代码(已经详细注释),**双指针法写出来之后,理解如下递归写法就不难了,代码逻辑都是一样的。** -```CPP -class Solution { -public: - ListNode* reverse(ListNode* pre,ListNode* cur){ - if(cur == NULL) return pre; - ListNode* temp = cur->next; - cur->next = pre; - // 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步 - // pre = cur; - // cur = temp; - return reverse(cur,temp); - } - ListNode* reverseList(ListNode* head) { - // 和双指针法初始化是一样的逻辑 - // ListNode* cur = head; - // ListNode* pre = NULL; - return reverse(NULL, head); - } - -}; -``` - -* 时间复杂度: O(n), 要递归处理链表的每个节点 -* 空间复杂度: O(n), 递归调用了 n 层栈空间 - -我们可以发现,上面的递归写法和双指针法实质上都是从前往后翻转指针指向,其实还有另外一种与双指针法不同思路的递归写法:从后往前翻转指针指向。 - -具体代码如下(带详细注释): - -```CPP -class Solution { -public: - ListNode* reverseList(ListNode* head) { - // 边缘条件判断 - if(head == NULL) return NULL; - if (head->next == NULL) return head; - - // 递归调用,翻转第二个节点开始往后的链表 - ListNode *last = reverseList(head->next); - // 翻转头节点与第二个节点的指向 - head->next->next = head; - // 此时的 head 节点为尾节点,next 需要指向 NULL - head->next = NULL; - return last; - } -}; -``` - -* 时间复杂度: O(n) -* 空间复杂度: O(n) - - -## 其他语言版本 - -### Java: - -```java -// 双指针 -class Solution { - public ListNode reverseList(ListNode head) { - ListNode prev = null; - ListNode cur = head; - ListNode temp = null; - while (cur != null) { - temp = cur.next;// 保存下一个节点 - cur.next = prev; - prev = cur; - cur = temp; - } - return prev; - } -} -``` - -```java -// 递归 -class Solution { - public ListNode reverseList(ListNode head) { - return reverse(null, head); - } - - private ListNode reverse(ListNode prev, ListNode cur) { - if (cur == null) { - return prev; - } - ListNode temp = null; - temp = cur.next;// 先保存下一个节点 - cur.next = prev;// 反转 - // 更新prev、cur位置 - // prev = cur; - // cur = temp; - return reverse(cur, temp); - } -} -``` - -```java -// 从后向前递归 -class Solution { - ListNode reverseList(ListNode head) { - // 边缘条件判断 - if(head == null) return null; - if (head.next == null) return head; - - // 递归调用,翻转第二个节点开始往后的链表 - ListNode last = reverseList(head.next); - // 翻转头节点与第二个节点的指向 - head.next.next = head; - // 此时的 head 节点为尾节点,next 需要指向 NULL - head.next = null; - return last; - } -} -``` - -### Python: - -```python -(版本一)双指针法 -# Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def reverseList(self, head: ListNode) -> ListNode: - cur = head - pre = None - while cur: - temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next - cur.next = pre #反转 - #更新pre、cur指针 - pre = cur - cur = temp - return pre -``` - -```python -(版本二)递归法 -# Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def reverseList(self, head: ListNode) -> ListNode: - return self.reverse(head, None) - def reverse(self, cur: ListNode, pre: ListNode) -> ListNode: - if cur == None: - return pre - temp = cur.next - cur.next = pre - return self.reverse(temp, cur) - -``` - - - -### Go: - -```go -//双指针 -func reverseList(head *ListNode) *ListNode { - var pre *ListNode - cur := head - for cur != nil { - next := cur.Next - cur.Next = pre - pre = cur - cur = next - } - return pre -} - -//递归 -func reverseList(head *ListNode) *ListNode { - return help(nil, head) -} - -func help(pre, head *ListNode)*ListNode{ - if head == nil { - return pre - } - next := head.Next - head.Next = pre - return help(head, next) -} - -``` -### JavaScript: - -```js -/** - * @param {ListNode} head - * @return {ListNode} - */ - -// 双指针: -var reverseList = function(head) { - if(!head || !head.next) return head; - let temp = null, pre = null, cur = head; - while(cur) { - temp = cur.next; - cur.next = pre; - pre = cur; - cur = temp; - } - // temp = cur = null; - return pre; -}; - -// 递归: -var reverse = function(pre, head) { - if(!head) return pre; - const temp = head.next; - head.next = pre; - pre = head - return reverse(pre, temp); -} - -var reverseList = function(head) { - return reverse(null, head); -}; - -// 递归2 -var reverse = function(head) { - if(!head || !head.next) return head; - // 从后往前翻 - const pre = reverse(head.next); - head.next = pre.next; - pre.next = head; - return head; -} - -var reverseList = function(head) { - let cur = head; - while(cur && cur.next) { - cur = cur.next; - } - reverse(head); - return cur; -}; -``` - -### TypeScript: - -```typescript -// 双指针法 -function reverseList(head: ListNode | null): ListNode | null { - let preNode: ListNode | null = null, - curNode: ListNode | null = head, - tempNode: ListNode | null; - while (curNode) { - tempNode = curNode.next; - curNode.next = preNode; - preNode = curNode; - curNode = tempNode; - } - return preNode; -}; - -// 递归(从前往后翻转) -function reverseList(head: ListNode | null): ListNode | null { - function recur(preNode: ListNode | null, curNode: ListNode | null): ListNode | null { - if (curNode === null) return preNode; - let tempNode: ListNode | null = curNode.next; - curNode.next = preNode; - preNode = curNode; - curNode = tempNode; - return recur(preNode, curNode); - } - return recur(null, head); -}; - -// 递归(从后往前翻转) -function reverseList(head: ListNode | null): ListNode | null { - if (head === null) return null; - let newHead: ListNode | null; - function recur(node: ListNode | null, preNode: ListNode | null): void { - if (node.next === null) { - newHead = node; - newHead.next = preNode; - } else { - recur(node.next, node); - node.next = preNode; - } - } - recur(head, null); - return newHead; -}; -``` - -### Ruby: - -```ruby -# 双指针 -# Definition for singly-linked list. -# class ListNode -# attr_accessor :val, :next -# def initialize(val = 0, _next = nil) -# @val = val -# @next = _next -# end -# end -def reverse_list(head) - # return nil if head.nil? # 循环判断条件亦能起到相同作用因此不必单独判断 - cur, per = head, nil - until cur.nil? - tem = cur.next - cur.next = per - per = cur - cur = tem - end - per -end - -# 递归 -# Definition for singly-linked list. -# class ListNode -# attr_accessor :val, :next -# def initialize(val = 0, _next = nil) -# @val = val -# @next = _next -# end -# end -def reverse_list(head) - reverse(nil, head) -end - -def reverse(pre, cur) - return pre if cur.nil? - tem = cur.next - cur.next = pre - reverse(cur, tem) # 通过递归实现双指针法中的更新操作 -end -``` - -### Kotlin: - -```Kotlin -fun reverseList(head: ListNode?): ListNode? { - var pre: ListNode? = null - var cur = head - while (cur != null) { - val temp = cur.next - cur.next = pre - pre = cur - cur = temp - } - return pre -} -``` -```kotlin -/** - * Example: - * var li = ListNode(5) - * var v = li.`val` - * Definition for singly-linked list. - * class ListNode(var `val`: Int) { - * var next: ListNode? = null - * } - */ -class Solution { - fun reverseList(head: ListNode?): ListNode? { - // temp用来存储临时的节点 - var temp: ListNode? - // cur用来遍历链表 - var cur: ListNode? = head - // pre用来作为链表反转的工具 - // pre是比pre前一位的节点 - var pre: ListNode? = null - while (cur != null) { - // 临时存储原本cur的下一个节点 - temp = cur.next - // 使cur下一节点地址为它之前的 - cur.next = pre - // 之后随着cur的遍历移动pre - pre = cur; - // 移动cur遍历链表各个节点 - cur = temp; - } - // 由于开头使用pre为null,所以cur等于链表本身长度+1, - // 此时pre在cur前一位,所以此时pre为头节点 - return pre; - } -} -``` - -### Swift: - -```swift -/// 双指针法 (迭代) -/// - Parameter head: 头结点 -/// - Returns: 翻转后的链表头结点 -func reverseList(_ head: ListNode?) -> ListNode? { - if head == nil || head?.next == nil { - return head - } - var pre: ListNode? = nil - var cur: ListNode? = head - var temp: ListNode? = nil - while cur != nil { - temp = cur?.next - cur?.next = pre - pre = cur - cur = temp - } - return pre -} - -/// 递归 -/// - Parameter head: 头结点 -/// - Returns: 翻转后的链表头结点 -func reverseList2(_ head: ListNode?) -> ListNode? { - return reverse(pre: nil, cur: head) -} -func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? { - if cur == nil { - return pre - } - let temp: ListNode? = cur?.next - cur?.next = pre - return reverse(pre: cur, cur: temp) -} -``` - -### C: -双指针法: - -```c -struct ListNode* reverseList(struct ListNode* head){ - //保存cur的下一个结点 - struct ListNode* temp; - //pre指针指向前一个当前结点的前一个结点 - struct ListNode* pre = NULL; - //用head代替cur,也可以再定义一个cur结点指向head。 - while(head) { - //保存下一个结点的位置 - temp = head->next; - //翻转操作 - head->next = pre; - //更新结点 - pre = head; - head = temp; - } - return pre; -} -``` - -递归法: -```c -struct ListNode* reverse(struct ListNode* pre, struct ListNode* cur) { - if(!cur) - return pre; - struct ListNode* temp = cur->next; - cur->next = pre; - //将cur作为pre传入下一层 - //将temp作为cur传入下一层,改变其指针指向当前cur - return reverse(cur, temp); -} - -struct ListNode* reverseList(struct ListNode* head){ - return reverse(NULL, head); -} -``` - - - -### PHP: - -```php -// 双指针法: -function reverseList($head) { - $cur = $head; - $pre = NULL; - while($cur){ - $temp = $cur->next; - $cur->next = $pre; - $pre = $cur; - $cur = $temp; - } - return $pre; - } -``` - -### Scala: -双指针法: - -```scala -object Solution { - def reverseList(head: ListNode): ListNode = { - var pre: ListNode = null - var cur = head - while (cur != null) { - var tmp = cur.next - cur.next = pre - pre = cur - cur = tmp - } - pre - } -} -``` -递归法: -```scala -object Solution { - def reverseList(head: ListNode): ListNode = { - reverse(null, head) - } - - def reverse(pre: ListNode, cur: ListNode): ListNode = { - if (cur == null) { - return pre // 如果当前cur为空,则返回pre - } - val tmp: ListNode = cur.next - cur.next = pre - reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点 - } - -} -``` - -### Rust: -双指针法: - -```rust -impl Solution { - pub fn reverse_list(head: Option>) -> Option> { - let mut cur = head; - let mut pre = None; - while let Some(mut node) = cur.take() { - cur = node.next; - node.next = pre; - pre = Some(node); - } - pre - } -} -``` - -递归法: - -```rust -impl Solution { - pub fn reverse_list(head: Option>) -> Option> { - fn rev( - mut head: Option>, - mut pre: Option>, - ) -> Option> { - if let Some(mut node) = head.take() { - let cur = node.next; - node.next = pre; - pre = Some(node); - return rev(cur, pre); - } - pre - } - rev(head, None) - } -} -``` -### C#: -三指针法, 感觉会更直观: - -```cs -public LinkNumbers Reverse() -{ - ///用三指针,写的过程中能够弥补二指针在翻转过程中的想象 - LinkNumbers pre = null; - var move = root; - var next = root; - - while (next != null) - { - next = next.linknext; - move.linknext = pre; - pre = move; - move = next; - } - root = pre; - return root; -} - -///LinkNumbers的定义 -public class LinkNumbers -{ - /// - /// 链表值 - /// - public int value { get; set; } - - /// - /// 链表指针 - /// - public LinkNumbers linknext { get; set; } -} -``` - -## 其他解法 - -### 使用虚拟头结点解决链表反转 - -> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈) - -```java -// 迭代方法:增加虚头结点,使用头插法实现链表翻转 -public static ListNode reverseList1(ListNode head) { - // 创建虚头结点 - ListNode dumpyHead = new ListNode(-1); - dumpyHead.next = null; - // 遍历所有节点 - ListNode cur = head; - while(cur != null){ - ListNode temp = cur.next; - // 头插法 - cur.next = dumpyHead.next; - dumpyHead.next = cur; - cur = temp; - } - return dumpyHead.next; -} -``` - - - -### 使用栈解决反转链表的问题 - -* 首先将所有的结点入栈 -* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 - -```java -public ListNode reverseList(ListNode head) { - // 如果链表为空,则返回空 - if (head == null) return null; - // 如果链表中只有只有一个元素,则直接返回 - if (head.next == null) return head; - // 创建栈 每一个结点都入栈 - Stack stack = new Stack<>(); - ListNode cur = head; - while (cur != null) { - stack.push(cur); - cur = cur.next; - } - // 创建一个虚拟头结点 - ListNode pHead = new ListNode(0); - cur = pHead; - while (!stack.isEmpty()) { - ListNode node = stack.pop(); - cur.next = node; - cur = cur.next; - } - // 最后一个元素的next要赋值为空 - cur.next = null; - return pHead.next; -} -``` - -> 采用这种方法需要注意一点。就是当整个出栈循环结束以后,cur正好指向原来链表的第一个结点,而此时结点1中的next指向的是结点2,因此最后还需要`cur.next = null` -![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png) - -

- - - From a64e11b09ad66f4a1ea22cdc3603e5cde85d88ef Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:13:42 +0800 Subject: [PATCH 0524/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20019.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?n=E4=B8=AA=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 84eac96b84..1c95ad5b2f 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -7,7 +7,7 @@ -## 19.删除链表的倒数第N个节点 +# 19.删除链表的倒数第N个节点 [力扣题目链接](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/) @@ -31,10 +31,12 @@ 输入:head = [1,2], n = 1 输出:[1] +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频再看本篇题解,更有助于大家对链表的理解。** -## 思路 -《代码随想录》算法公开课:[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。 @@ -93,7 +95,7 @@ public: ## 其他语言版本 -java: +### Java: ```java public ListNode removeNthFromEnd(ListNode head, int n){ @@ -120,7 +122,8 @@ public ListNode removeNthFromEnd(ListNode head, int n){ } ``` -Python: +### Python: + ```python # Definition for singly-linked list. # class ListNode: @@ -151,7 +154,8 @@ class Solution: return dummy_head.next ``` -Go: +### Go: + ```Go /** * Definition for singly-linked list. @@ -178,7 +182,7 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode { } ``` -JavaScript: +### JavaScript: ```js /** @@ -198,7 +202,7 @@ var removeNthFromEnd = function(head, n) { return ret.next; }; ``` -TypeScript: +### TypeScript: 版本一(快慢指针法): @@ -263,7 +267,7 @@ function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { }; ``` -Kotlin: +### Kotlin: ```Kotlin fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { @@ -284,7 +288,8 @@ fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { } ``` -Swift: +### Swift: + ```swift func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { if head == nil { @@ -309,8 +314,8 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { } ``` +### PHP: -PHP: ```php function removeNthFromEnd($head, $n) { // 设置虚拟头节点 @@ -332,7 +337,8 @@ function removeNthFromEnd($head, $n) { } ``` -Scala: +### Scala: + ```scala object Solution { def removeNthFromEnd(head: ListNode, n: Int): ListNode = { @@ -356,7 +362,8 @@ object Solution { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn remove_nth_from_end(head: Option>, mut n: i32) -> Option> { @@ -377,7 +384,8 @@ impl Solution { } } ``` -C语言 +### C: + ```c /**c语言单链表的定义 * Definition for singly-linked list. @@ -412,7 +420,8 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { ``` -C#: +### C#: + ```csharp public class Solution { public ListNode RemoveNthFromEnd(ListNode head, int n) { From 8eb214c06025e359d9c4a40d87d4e3075f5d8bdf Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:23:24 +0800 Subject: [PATCH 0525/1533] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=98=2002.07.?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...\276\350\241\250\347\233\270\344\272\244.md" | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 75e0311630..833eadca6a 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -34,6 +34,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png) + ## 思路 @@ -101,7 +102,7 @@ public: ## 其他语言版本 -Java: +### Java: ```Java public class Solution { @@ -150,8 +151,7 @@ public class Solution { } ``` - -Python: +### Python: ```python @@ -280,7 +280,7 @@ class Solution: return pointerA ``` -Go: +### Go: ```go func getIntersectionNode(headA, headB *ListNode) *ListNode { @@ -341,7 +341,7 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -JavaScript: +### JavaScript: ```js var getListLen = function(head) { @@ -376,7 +376,7 @@ var getIntersectionNode = function(headA, headB) { }; ``` -TypeScript: +### TypeScript: ```typescript function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { @@ -413,7 +413,7 @@ function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): Li }; ``` -C: +### C: ```c ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { @@ -452,7 +452,7 @@ ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { } ``` -Scala: +### Scala: ```scala object Solution { @@ -508,3 +508,4 @@ object Solution { + From 17d56ed722d41a8d463de2de71c72345a88a2df5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:30:49 +0800 Subject: [PATCH 0526/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200142.=E7=8E=AF?= =?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\275\242\351\223\276\350\241\250.md" | 11 +++---- ...\345\275\242\351\223\276\350\241\250II.md" | 30 +++++++++---------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" index 7d7121a0ba..b1f42ba979 100644 --- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" +++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" @@ -70,7 +70,7 @@ public: ## 其他语言版本 -### Java +### Java: ```java public class Solution { @@ -90,7 +90,7 @@ public class Solution { } ``` -### Python +### Python: ```python class Solution: @@ -105,7 +105,7 @@ class Solution: return False ``` -### Go +### Go: ```go func hasCycle(head *ListNode) bool { @@ -125,7 +125,7 @@ func hasCycle(head *ListNode) bool { } ``` -### JavaScript +### JavaScript: ```js var hasCycle = function(head) { @@ -141,7 +141,7 @@ var hasCycle = function(head) { }; ``` -### TypeScript +### TypeScript: ```typescript function hasCycle(head: ListNode | null): boolean { @@ -163,3 +163,4 @@ function hasCycle(head: ListNode | null): boolean { + diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index f87d2cd914..d20101a706 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -11,7 +11,7 @@ > 找到有没有环已经很不容易了,还要让我找到环的入口? -## 142.环形链表II +# 142.环形链表II [力扣题目链接](https://leetcode.cn/problems/linked-list-cycle-ii/) @@ -24,9 +24,11 @@ ![循环链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20200816110112704.png) -## 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。** -《代码随想录》算法公开课:[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 这道题目,不仅考察对链表的操作,而且还需要一些数学运算。 @@ -148,7 +150,7 @@ public: * 时间复杂度: O(n),快慢指针相遇前,指针走的次数小于链表长度,快慢指针相遇后,两个index指针走的次数也小于链表长度,总体为走的次数小于 2n * 空间复杂度: O(1) -## 补充 +### 补充 在推理过程中,大家可能有一个疑问就是:**为什么第一次在环中相遇,slow的 步数 是 x+y 而不是 x + 若干环的长度 + y 呢?** @@ -190,8 +192,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java public class Solution { @@ -217,8 +218,7 @@ public class Solution { } ``` - -Python: +### Python: ```python (版本一)快慢指针法 @@ -270,7 +270,7 @@ class Solution: return None ``` -Go: +### Go: ```go func detectCycle(head *ListNode) *ListNode { @@ -290,7 +290,7 @@ func detectCycle(head *ListNode) *ListNode { } ``` -javaScript +### JavaScript ```js // 两种循环实现方式 @@ -334,7 +334,7 @@ var detectCycle = function(head) { }; ``` -TypeScript: +### TypeScript: ```typescript function detectCycle(head: ListNode | null): ListNode | null { @@ -356,7 +356,7 @@ function detectCycle(head: ListNode | null): ListNode | null { }; ``` -Swift: +### Swift: ```swift class Solution { @@ -391,7 +391,7 @@ extension ListNode: Equatable { } ``` -C: +### C: ```c ListNode *detectCycle(ListNode *head) { @@ -410,7 +410,7 @@ ListNode *detectCycle(ListNode *head) { } ``` -Scala: +### Scala: ```scala object Solution { @@ -437,7 +437,7 @@ object Solution { } ``` -C#: +### C#: ```CSharp public class Solution { From 28b58782375755eda70381aaab630e52b50992c8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:33:00 +0800 Subject: [PATCH 0527/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...3\276\350\241\250\346\200\273\347\273\223\347\257\207.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index cfbafc45b3..dacd4dee5d 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -3,7 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- +# 链表总结篇 ## 链表的理论基础 @@ -68,7 +68,7 @@ [链表:链表相交](https://programmercarl.com/面试题02.07.链表相交.html)使用双指针来找到两个链表的交点(引用完全相同,即:内存地址完全相同的交点) -## 环形链表 +### 环形链表 在[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)中,讲解了在链表如何找环,以及如何找环的入口位置。 @@ -100,3 +100,4 @@ + From a3a8182d28006fbc9d6003dc5907b543c1dba647 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 15:42:47 +0800 Subject: [PATCH 0528/1533] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20Rust=20=E5=92=8C=20Java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\207\345\255\220\345\272\217\345\210\227.md" | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index c58c3bf6c4..8287c77719 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -130,18 +130,16 @@ Java: class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; + int res = 0; Arrays.fill(dp, 1); - for (int i = 0; i < dp.length; i++) { + for (int i = 1; i < dp.length; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } + res = Math.max(res, dp[i]); } } - int res = 0; - for (int i = 0; i < dp.length; i++) { - res = Math.max(res, dp[i]); - } return res; } } @@ -291,7 +289,7 @@ function lengthOfLIS(nums: number[]): number { Rust: ```rust pub fn length_of_lis(nums: Vec) -> i32 { - let mut dp = vec![1; nums.len() + 1]; + let mut dp = vec![1; nums.len()]; let mut result = 1; for i in 1..nums.len() { for j in 0..i { @@ -306,13 +304,6 @@ pub fn length_of_lis(nums: Vec) -> i32 { ``` - - - - - - -

From 826554682f465da29414c4ad6aa9ba391a54e05a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 16:51:54 +0800 Subject: [PATCH 0529/1533] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md=20abo?= =?UTF-8?q?ut=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\242\236\345\272\217\345\210\227.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 8cc270ec64..58365df3a7 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -303,6 +303,9 @@ func findLengthOfLCIS(nums []int) int { ``` Rust: + +>动态规划 + ```rust pub fn find_length_of_lcis(nums: Vec) -> i32 { if nums.is_empty() { @@ -320,6 +323,25 @@ pub fn find_length_of_lcis(nums: Vec) -> i32 { } ``` +> 贪心 + +```rust +impl Solution { + pub fn find_length_of_lcis(nums: Vec) -> i32 { + let (mut res, mut count) = (1, 1); + for i in 1..nums.len() { + if nums[i] > nums[i - 1] { + count += 1; + res = res.max(count); + continue; + } + count = 1; + } + res + } +} +``` + Javascript: > 动态规划: From b42feba6057eab0cbb5f25709b3d5d77b3a073c3 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 17:25:21 +0800 Subject: [PATCH 0530/1533] =?UTF-8?q?Update=200718.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\255\220\346\225\260\347\273\204.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 82bf4f59e2..5b5e7a6c87 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -537,6 +537,29 @@ function findLength(nums1: number[], nums2: number[]): number { }; ``` +Rust: + +> 滚动数组 + +```rust +impl Solution { + pub fn find_length(nums1: Vec, nums2: Vec) -> i32 { + let (mut res, mut dp) = (0, vec![0; nums2.len()]); + + for n1 in nums1 { + for j in (0..nums2.len()).rev() { + if n1 == nums2[j] { + dp[j] = if j == 0 { 1 } else { dp[j - 1] + 1 }; + res = res.max(dp[j]); + } else { + dp[j] = 0; + } + } + } + res + } +} +``` From 9807ea62e7d2001b9f5c86e9be08b2bce8d1871f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 19:35:33 +0800 Subject: [PATCH 0531/1533] =?UTF-8?q?Update=201143.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...61\345\255\220\345\272\217\345\210\227.md" | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 68269b87c5..0bd8b1d606 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -327,23 +327,51 @@ function longestCommonSubsequence(text1: string, text2: string): number { ``` Rust: + +二维 dp: + ```rust -pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { - let (n, m) = (text1.len(), text2.len()); - let (s1, s2) = (text1.as_bytes(), text2.as_bytes()); - let mut dp = vec![0; m + 1]; - let mut last = vec![0; m + 1]; - for i in 1..=n { - dp.swap_with_slice(&mut last); - for j in 1..=m { - dp[j] = if s1[i - 1] == s2[j - 1] { - last[j - 1] + 1 - } else { - last[j].max(dp[j - 1]) - }; +impl Solution { + pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + let mut dp = vec![vec![0; text2.len() + 1]; text1.len() + 1]; + for (i, c1) in text1.chars().enumerate() { + for (j, c2) in text2.chars().enumerate() { + if c1 == c2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + } else { + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + } + dp[text1.len()][text2.len()] + } +} +``` + +二维: + +```rust +impl Solution { + pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + let mut dp = vec![0; text2.len() + 1]; + for c1 in text1.chars() { + // 初始化 prev + let mut prev = 0; + + for (j, c2) in text2.chars().enumerate() { + let temp = dp[j + 1]; + if c1 == c2 { + // 使用上一次的状态,防止重复计算 + dp[j + 1] = prev + 1; + } else { + dp[j + 1] = dp[j + 1].max(dp[j]); + } + // 使用上一次的状态更新 prev + prev = temp; + } } + dp[text2.len()] } - dp[m] } ``` From 695f86743842282e5dae571220af28ee2cac704e Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 22:55:35 +0800 Subject: [PATCH 0532/1533] =?UTF-8?q?Update=201035.=E4=B8=8D=E7=9B=B8?= =?UTF-8?q?=E4=BA=A4=E7=9A=84=E7=BA=BF.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\344\272\244\347\232\204\347\272\277.md" | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 7142d75c09..460644ae3e 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -155,21 +155,44 @@ func max(a, b int) int { Rust: ```rust -pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { - let (n, m) = (nums1.len(), nums2.len()); - let mut last = vec![0; m + 1]; // 记录滚动数组 - let mut dp = vec![0; m + 1]; - for i in 1..=n { - dp.swap_with_slice(&mut last); - for j in 1..=m { - if nums1[i - 1] == nums2[j - 1] { - dp[j] = last[j - 1] + 1; - } else { - dp[j] = last[j].max(dp[j - 1]); +impl Solution { + pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { + let mut dp = vec![vec![0; nums2.len() + 1]; nums1.len() + 1]; + for (i, num1) in nums1.iter().enumerate() { + for (j, num2) in nums2.iter().enumerate() { + if num1 == num2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + } else { + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + } + dp[nums1.len()][nums2.len()] + } +} +``` + +> 滚动数组 + +```rust +impl Solution { + pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { + let mut dp = vec![0; nums2.len() + 1]; + for num1 in nums1 { + let mut prev = 0; + for (j, &num2) in nums2.iter().enumerate() { + let temp = dp[j + 1]; + if num1 == num2 { + // 使用上一次的状态,防止重复计算 + dp[j + 1] = prev + 1; + } else { + dp[j + 1] = dp[j + 1].max(dp[j]); + } + prev = temp; } } + dp[nums2.len()] } - dp[m] } ``` From e5d694da4d071b1286eb22db2477f55bb91b0cf6 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 19 Jul 2023 10:33:50 +0800 Subject: [PATCH 0533/1533] Update --- ...\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index f8c07016a4..52a8448b51 100644 --- "a/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ "b/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -36,7 +36,7 @@ * 微众银行(总部深圳) * 招银科技(总部深圳) * 平安系列(平安科技、平安寿险、平安产险、平安金融、平安好医生等) -* Shopee(东南亚最大的电商平台,最近发展势头非常强劲) +* Shopee(21年有裁员风波) * 有赞(深圳) * 迅雷(总部深圳) * 金蝶(总部深圳) From 607e4ebd49c67b6ddd4fcdd91b248e74ed0ed75e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:18:19 +0800 Subject: [PATCH 0534/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88?= =?UTF-8?q?=E5=B8=8C=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?= =?UTF-8?q?=E6=8E=92=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 --- ...\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 0ab17ec02b..3055875a11 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -8,6 +8,8 @@ +# 哈希表理论基础 + ## 哈希表 首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 From c628aa65d438aaf4933f2b029ae71c93e3cfce63 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:25:34 +0800 Subject: [PATCH 0535/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?=20=E6=8E=92=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 --- ...15\345\274\202\344\275\215\350\257\215.md" | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 4ea43947e8..f47d8b05b6 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -7,7 +7,7 @@ > 数组就是简单的哈希表,但是数组的大小可不是无限开辟的 -## 242.有效的字母异位词 +# 242.有效的字母异位词 [力扣题目链接](https://leetcode.cn/problems/valid-anagram/) @@ -21,13 +21,14 @@ 输入: s = "rat", t = "car" 输出: false - **说明:** 你可以假设字符串只包含小写字母。 -## 思路 +## 算法公开课 -本题B站视频讲解版:[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA) +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。 @@ -88,12 +89,10 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1) - - ## 其他语言版本 +### Java: -Java: ```java /** * 242. 有效的字母异位词 字典解法 @@ -121,7 +120,7 @@ class Solution { } ``` -Python: +### Python: ```python class Solution: @@ -165,7 +164,7 @@ class Solution(object): return a_count == b_count ``` -Go: +### Go: ```go func isAnagram(s string, t string) bool { @@ -182,7 +181,7 @@ func isAnagram(s string, t string) bool { } ``` -javaScript: +### JavaScript: ```js /** @@ -218,7 +217,7 @@ var isAnagram = function(s, t) { }; ``` -TypeScript: +### TypeScript: ```typescript function isAnagram(s: string, t: string): boolean { @@ -233,7 +232,7 @@ function isAnagram(s: string, t: string): boolean { }; ``` -Swift: +### Swift: ```Swift func isAnagram(_ s: String, _ t: String) -> Bool { @@ -257,7 +256,8 @@ func isAnagram(_ s: String, _ t: String) -> Bool { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -292,7 +292,8 @@ class Solution { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn is_anagram(s: String, t: String) -> bool { @@ -312,8 +313,8 @@ impl Solution { } ``` +### Scala: -Scala: ```scala object Solution { def isAnagram(s: String, t: String): Boolean = { @@ -337,8 +338,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public bool IsAnagram(string s, string t) { int sl=s.Length,tl=t.Length; @@ -360,11 +361,12 @@ C#: ## 相关题目 * [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html) -* 49.字母异位词分组 -* 438.找到字符串中所有字母异位词 +* [49.字母异位词分组](https://leetcode.cn/problems/group-anagrams/) +* [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)

+ From 19dfa98e4f936f4b5f73886e6c1503c07f913bf8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:30:12 +0800 Subject: [PATCH 0536/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200349.=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20?= =?UTF-8?q?=E6=8E=92=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 --- ...04\347\232\204\344\272\244\351\233\206.md" | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index c2f6ef46ba..8daf5a35ba 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -10,7 +10,7 @@ > 如果哈希值比较少、特别分散、跨度非常大,使用数组就造成空间的极大浪费! -## 349. 两个数组的交集 +# 349. 两个数组的交集 [力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/) @@ -22,9 +22,11 @@ 输出结果中的每个元素一定是唯一的。 我们可以不考虑输出结果的顺序。 -## 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -关于本题,我录制了讲解视频:[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),看视频配合题解,事半功倍。 +## 思路 这道题目,主要要学会使用一种哈希数据结构:unordered_set,这个数据结构可以解决很多类似的问题。 @@ -118,8 +120,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```Java import java.util.HashSet; @@ -159,8 +160,9 @@ class Solution { } ``` -Python3: +### Python3: (版本一) 使用字典和集合 + ```python class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: @@ -206,7 +208,8 @@ class Solution: ``` -Go: +### Go: + ```go func intersection(nums1 []int, nums2 []int) []int { set:=make(map[int]struct{},0) // 用map模拟set @@ -227,7 +230,7 @@ func intersection(nums1 []int, nums2 []int) []int { } ``` -javaScript: +### JavaScript: ```js /** @@ -255,7 +258,7 @@ var intersection = function(nums1, nums2) { }; ``` -TypeScript: +### TypeScript: 版本一(正常解法): @@ -280,7 +283,7 @@ function intersection(nums1: number[], nums2: number[]): number[] { }; ``` -Swift: +### Swift: ```swift func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { @@ -298,7 +301,8 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -327,7 +331,8 @@ class Solution { } ``` -Rust: +### Rust: + ```rust use std::collections::HashSet; impl Solution { @@ -363,7 +368,8 @@ impl Solution { } ``` -C: +### C: + ```C int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){ @@ -394,7 +400,7 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re } ``` -Scala: +### Scala: 正常解法: ```scala @@ -439,8 +445,8 @@ object Solution { ``` +### C#: -C#: ```csharp public int[] Intersection(int[] nums1, int[] nums2) { if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0) @@ -461,11 +467,10 @@ C#: ``` ## 相关题目 -* 350.两个数组的交集 II +* [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)

- From 764b3e9e51608baf17183a5e36cab0888a6e46f9 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:34:27 +0800 Subject: [PATCH 0537/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200202.=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\345\277\253\344\271\220\346\225\260.md" | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 7fe8cd8d75..4a77e2b67c 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -28,7 +28,7 @@ 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1 -# 思路 +## 思路 这道题目看上去貌似一道数学问题,其实并不是! @@ -80,10 +80,10 @@ public: -# 其他语言版本 +## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean isHappy(int n) { @@ -107,8 +107,9 @@ class Solution { } ``` -Python: +### Python: (版本一)使用集合 + ```python class Solution: def isHappy(self, n: int) -> bool: @@ -131,7 +132,7 @@ class Solution: n, r = divmod(n, 10) new_num += r ** 2 return new_num - ``` +``` (版本二)使用集合 ```python class Solution: @@ -146,7 +147,7 @@ class Solution: if new_num==1: return True else: n = new_num return False -``` + ``` (版本三)使用数组 ```python class Solution: @@ -161,7 +162,7 @@ class Solution: if new_num==1: return True else: n = new_num return False -``` + ``` (版本四)使用快慢指针 ```python class Solution: @@ -180,7 +181,7 @@ class Solution: n, r = divmod(n, 10) new_num += r ** 2 return new_num -``` + ``` (版本五)使用集合+精简 ```python class Solution: @@ -192,7 +193,7 @@ class Solution: return False seen.add(n) return True -``` + ``` (版本六)使用数组+精简 ```python class Solution: @@ -204,8 +205,9 @@ class Solution: return False seen.append(n) return True -``` -Go: + ``` +### Go: + ```go func isHappy(n int) bool { m := make(map[int]bool) @@ -225,7 +227,7 @@ func getSum(n int) int { } ``` -javaScript: +### JavaScript: ```js var isHappy = function (n) { @@ -303,7 +305,7 @@ var isHappy = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript function isHappy(n: number): boolean { @@ -322,7 +324,7 @@ function isHappy(n: number): boolean { }; ``` -Swift: +### Swift: ```swift // number 每个位置上的数字的平方和 @@ -355,7 +357,8 @@ func isHappy(_ n: Int) -> Bool { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -386,7 +389,8 @@ class Solution { } ``` -Rust: +### Rust: + ```Rust use std::collections::HashSet; impl Solution { @@ -416,7 +420,8 @@ impl Solution { } ``` -C: +### C: + ```C typedef struct HashNodeTag { int key; /* num */ @@ -473,8 +478,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public class Solution { private int getSum(int n) { @@ -500,3 +505,4 @@ public class Solution { + From 23950d1c3e8541420eaca3f55f0896a84d3ba007 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:43:14 +0800 Subject: [PATCH 0538/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\346\225\260\344\271\213\345\222\214.md" | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index ca62e3edc2..e3fb0fb55f 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 1. 两数之和 +# 1. 两数之和 [力扣题目链接](https://leetcode.cn/problems/two-sum/) @@ -21,10 +21,12 @@ 所以返回 [0, 1] +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -## 思路 -建议看一下我录的这期视频:[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),结合本题解来学习,事半功倍。 +## 思路 很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。 @@ -128,8 +130,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java public int[] twoSum(int[] nums, int target) { int[] res = new int[2]; @@ -150,8 +152,9 @@ public int[] twoSum(int[] nums, int target) { } ``` -Python: +### Python: (版本一) 使用字典 + ```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: @@ -211,7 +214,7 @@ class Solution: return [i,j] ``` -Go: +### Go: ```go // 暴力解法 @@ -242,7 +245,7 @@ func twoSum(nums []int, target int) []int { } ``` -Rust +### Rust: ```rust use std::collections::HashMap; @@ -263,9 +266,7 @@ impl Solution { } } ``` -Rust - -``` +```rust use std::collections::HashMap; impl Solution { @@ -284,7 +285,7 @@ impl Solution { } ``` -Javascript +### Javascript: ```javascript var twoSum = function (nums, target) { @@ -299,7 +300,7 @@ var twoSum = function (nums, target) { }; ``` -TypeScript: +### TypeScript: ```typescript function twoSum(nums: number[], target: number): number[] { @@ -317,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] { }; ``` -php +### php: ```php function twoSum(array $nums, int $target): array @@ -337,7 +338,8 @@ function twoSum(array $nums, int $target): array } ``` -Swift: +### Swift: + ```swift func twoSum(_ nums: [Int], _ target: Int) -> [Int] { // 值: 下标 @@ -353,8 +355,8 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] { } ``` +### Scala: -Scala: ```scala object Solution { // 导入包 @@ -377,7 +379,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int[] TwoSum(int[] nums, int target) { @@ -396,7 +399,8 @@ public class Solution { } ``` -Dart: +### Dart: + ```dart List twoSum(List nums, int target) { var tmp = []; @@ -411,7 +415,8 @@ List twoSum(List nums, int target) { } ``` -C: +### C: + ```c From 2948791011238baa02f6beda8bb496e8c52fb6de Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:47:02 +0800 Subject: [PATCH 0539/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200454.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0II=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\225\260\347\233\270\345\212\240II.md" | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 4a16d4f4a7..90c3733449 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -34,10 +34,12 @@ 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,map使用有技巧!LeetCode:454.四数相加II](https://www.bilibili.com/video/BV1Md4y1Q7Yh),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -本题视频讲解:[学透哈希表,map使用有技巧!LeetCode:454.四数相加II](https://www.bilibili.com/video/BV1Md4y1Q7Yh),结合视频在看本题解,事半功倍。 + +## 思路 本题咋眼一看好像和[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)差不多,其实差很多。 @@ -92,8 +94,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { @@ -117,8 +119,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 使用字典 + ```python class Solution(object): def fourSumCount(self, nums1, nums2, nums3, nums4): @@ -179,7 +182,7 @@ class Solution: return cnt ``` -Go: +### Go: ```go func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { @@ -201,7 +204,7 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { } ``` -javaScript: +### JavaScript: ```js /** @@ -233,7 +236,7 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) { }; ``` -TypeScript: +### TypeScript: ```typescript function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number { @@ -258,7 +261,7 @@ function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: }; ``` -PHP: +### PHP: ```php class Solution { @@ -291,8 +294,8 @@ class Solution { } ``` +### Swift: -Swift: ```swift func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int { // ab和: ab和出现次数 @@ -316,7 +319,8 @@ func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int] } ``` -Rust: +### Rust: + ```rust use std::collections::HashMap; impl Solution { @@ -342,8 +346,8 @@ impl Solution { } ``` +### Scala: -Scala: ```scala object Solution { // 导包 @@ -380,7 +384,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { Dictionary dic = new Dictionary(); @@ -411,3 +416,4 @@ public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + From 0922ede8c7ce3fedbd7494834beb224aa3da6311 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:49:56 +0800 Subject: [PATCH 0540/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3.\350\265\216\351\207\221\344\277\241.md" | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index e74cdf71fd..8122240ecc 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -34,7 +34,7 @@ canConstruct("aa", "aab") -> true * 第二点 “你可以假设两个字符串均只含有小写字母。” *说明只有小写字母*,这一点很重要 -## 暴力解法 +### 暴力解法 那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下: @@ -66,7 +66,7 @@ public: 这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。 -## 哈希解法 +### 哈希解法 因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。 @@ -112,8 +112,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public boolean canConstruct(String ransomNote, String magazine) { @@ -146,8 +146,9 @@ class Solution { ``` -Python: +### Python: (版本一)使用数组 + ```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: @@ -213,7 +214,7 @@ class Solution: return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) ``` -Go: +### Go: ```go func canConstruct(ransomNote string, magazine string) bool { @@ -231,7 +232,7 @@ func canConstruct(ransomNote string, magazine string) bool { } ``` -javaScript: +### JavaScript: ```js /** @@ -254,7 +255,7 @@ var canConstruct = function(ransomNote, magazine) { }; ``` -TypeScript: +### TypeScript: ```typescript function canConstruct(ransomNote: string, magazine: string): boolean { @@ -275,8 +276,8 @@ function canConstruct(ransomNote: string, magazine: string): boolean { }; ``` +### PHP: -PHP: ```php class Solution { /** @@ -301,7 +302,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { var record = Array(repeating: 0, count: 26); @@ -324,7 +326,8 @@ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn can_construct(ransom_note: String, magazine: String) -> bool { @@ -347,7 +350,7 @@ impl Solution { } ``` -Scala: +### Scala: 版本一: 使用数组作为哈希表 ```scala @@ -411,8 +414,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public bool CanConstruct(string ransomNote, string magazine) { if(ransomNote.Length > magazine.Length) return false; @@ -434,3 +437,4 @@ public bool CanConstruct(string ransomNote, string magazine) { + From eb2cdf24bf03dd143072a4260fb11b76ececcc37 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:55:09 +0800 Subject: [PATCH 0541/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\225\260\344\271\213\345\222\214.md" | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 9cca779b21..4951c90cf1 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -26,14 +26,15 @@ [-1, -1, 2] ] +## 算法公开课 -# 思路 - -针对本题,我录制了视频讲解:[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 **注意[0, 0, 0, 0] 这组数据** -## 哈希解法 +## 思路 + +### 哈希解法 两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。 @@ -87,7 +88,7 @@ public: * 空间复杂度: O(n),额外的 set 开销 -## 双指针 +### 双指针 **其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。 @@ -166,9 +167,9 @@ public: * 空间复杂度: O(1) -## 去重逻辑的思考 +### 去重逻辑的思考 -### a的去重 +#### a的去重 说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right] @@ -188,7 +189,7 @@ a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳 if (nums[i] == nums[i + 1]) { // 去重操作 continue; } -``` +``` 那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。 @@ -208,7 +209,7 @@ if (i > 0 && nums[i] == nums[i - 1]) { 这是一个非常细节的思考过程。 -### b与c的去重 +#### b与c的去重 很多同学写本题的时候,去重的逻辑多加了 对right 和left 的去重:(代码中注释部分) @@ -225,7 +226,7 @@ while (right > left) { } else { } } -``` +``` 但细想一下,这种去重其实对提升程序运行效率是没有帮助的。 @@ -238,7 +239,7 @@ while (right > left) { 所以这种去重 是可以不加的。 仅仅是 把去重的逻辑提前了而已。 -# 思考题 +## 思考题 既然三数之和可以使用双指针法,我们之前讲过的[1.两数之和](https://programmercarl.com/0001.两数之和.html),可不可以使用双指针法呢? @@ -254,8 +255,8 @@ while (right > left) { ## 其他语言版本 +### Java: -Java: ```Java class Solution { public List> threeSum(int[] nums) { @@ -297,8 +298,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 双指针 + ```Python class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: @@ -366,7 +368,7 @@ class Solution: return result ``` -Go: +### Go: ```Go func threeSum(nums []int) [][]int { @@ -407,7 +409,7 @@ func threeSum(nums []int) [][]int { } ``` -javaScript: +### JavaScript: ```js var threeSum = function(nums) { @@ -512,7 +514,7 @@ var threeSum = function (nums) { }; ``` -TypeScript: +### TypeScript: ```typescript function threeSum(nums: number[]): number[][] { @@ -553,7 +555,8 @@ function threeSum(nums: number[]): number[][] { }; ``` -ruby: +### Ruby: + ```ruby def is_valid(strs) symbol_map = {')' => '(', '}' => '{', ']' => '['} @@ -571,8 +574,8 @@ def is_valid(strs) end ``` +### PHP: -PHP: ```php class Solution { /** @@ -613,7 +616,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift // 双指针法 func threeSum(_ nums: [Int]) -> [[Int]] { @@ -654,7 +658,8 @@ func threeSum(_ nums: [Int]) -> [[Int]] { } ``` -Rust: +### Rust: + ```Rust // 哈希解法 use std::collections::HashSet; @@ -718,7 +723,8 @@ impl Solution { } ``` -C: +### C: + ```C //qsort辅助cmp函数 int cmp(const void* ptr1, const void* ptr2) { @@ -792,7 +798,8 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes } ``` -C#: +### C#: + ```csharp public class Solution { @@ -850,7 +857,8 @@ public class Solution } } ``` -Scala: +### Scala: + ```scala object Solution { // 导包 @@ -898,3 +906,4 @@ object Solution { + From 3fe673d804209e55705b2b7206d86e3fc6e87e18 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:59:05 +0800 Subject: [PATCH 0542/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200018.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index a4d41d9bf9..28c20b7acd 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -27,9 +27,11 @@ [-2, 0, 0, 2] ] -# 思路 +## 算法公开课 -针对本题,我录制了视频讲解:[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。 @@ -141,22 +143,16 @@ if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) { if (nums[k] + nums[i] > target && nums[i] >= 0) { break; } -``` +``` 因为只要 nums[k] + nums[i] > target,那么 nums[i] 后面的数都是正数的话,就一定 不符合条件了。 不过这种剪枝 其实有点 小绕,大家能够理解 文章给的完整代码的剪枝 就够了。 - - - - - - ## 其他语言版本 +### Java: -Java: ```Java class Solution { public List> fourSum(int[] nums, int target) { @@ -206,8 +202,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 双指针 + ```python class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: @@ -273,7 +270,8 @@ class Solution(object): ``` -Go: +### Go: + ```go func fourSum(nums []int, target int) [][]int { if len(nums) < 4 { @@ -323,7 +321,7 @@ func fourSum(nums []int, target int) [][]int { } ``` -javaScript: +### JavaScript: ```js /** @@ -359,7 +357,7 @@ var fourSum = function(nums, target) { }; ``` -TypeScript: +### TypeScript: ```typescript function fourSum(nums: number[], target: number): number[][] { @@ -400,7 +398,7 @@ function fourSum(nums: number[], target: number): number[][] { }; ``` -PHP: +### PHP: ```php class Solution { @@ -445,7 +443,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { var res = [[Int]]() @@ -493,7 +492,8 @@ func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { } ``` -C#: +### C#: + ```csharp public class Solution { @@ -555,7 +555,8 @@ public class Solution } ``` -Rust: +### Rust: + ```Rust use std::cmp::Ordering; impl Solution { @@ -603,7 +604,8 @@ impl Solution { } ``` -Scala: +### Scala: + ```scala object Solution { // 导包 @@ -651,3 +653,4 @@ object Solution { + From ddee6adbbba4db9466cf720307b2e44f10e22140 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:01:01 +0800 Subject: [PATCH 0543/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88?= =?UTF-8?q?=E5=B8=8C=E8=A1=A8=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...\214\350\241\250\346\200\273\347\273\223.md" | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" index 9ee84f991b..6750636305 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" @@ -7,8 +7,10 @@ > 哈希表总结篇如约而至 +# 哈希表总结篇 -# 哈希表理论基础 + +## 哈希表理论基础 在[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)中,我们介绍了哈希表的基础理论知识,不同于枯燥的讲解,这里介绍了都是对刷题有帮助的理论知识点。 @@ -32,9 +34,9 @@ **只有对这些数据结构的底层实现很熟悉,才能灵活使用,否则很容易写出效率低下的程序**。 -# 哈希表经典题目 +## 哈希表经典题目 -## 数组作为哈希表 +### 数组作为哈希表 一些应用场景就是为数组量身定做的。 @@ -51,7 +53,7 @@ **上面两道题目用map确实可以,但使用map的空间消耗要比数组大一些,因为map要维护红黑树或者符号表,而且还要做哈希函数的运算。所以数组更加简单直接有效!** -## set作为哈希表 +### set作为哈希表 在[349. 两个数组的交集](https://programmercarl.com/0349.两个数组的交集.html)中我们给出了什么时候用数组就不行了,需要用set。 @@ -75,7 +77,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 在[202.快乐数](https://programmercarl.com/0202.快乐数.html)中,我们再次使用了unordered_set来判断一个数是否重复出现过。 -## map作为哈希表 +### map作为哈希表 在[1.两数之和](https://programmercarl.com/0001.两数之和.html)中map正式登场。 @@ -110,7 +112,7 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 所以18. 四数之和,15.三数之和都推荐使用双指针法! -# 总结 +## 总结 对于哈希表的知识相信很多同学都知道,但是没有成体系。 @@ -123,9 +125,8 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 - -

+ From 00ef6084c6ea18933e7a63923aad71addee70c6e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:32:11 +0800 Subject: [PATCH 0544/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200344.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...54\345\255\227\347\254\246\344\270\262.md" | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 1c74f9aa46..8a4fed4574 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -26,10 +26,12 @@ 输入:["H","a","n","n","a","h"] 输出:["h","a","n","n","a","H"] +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),结合本题解一起看,事半功倍! + +## 思路 先说一说题外话: @@ -138,8 +140,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public void reverseString(char[] s) { @@ -173,8 +175,9 @@ class Solution { ``` -Python: +### Python: (版本一) 双指针 + ```python class Solution: def reverseString(self, s: List[str]) -> None: @@ -247,7 +250,8 @@ class Solution: s[:] = [s[i] for i in range(len(s) - 1, -1, -1)] ``` -Go: +### Go: + ```Go func reverseString(s []byte) { left := 0 @@ -260,7 +264,7 @@ func reverseString(s []byte) { } ``` -javaScript: +### JavaScript: ```js /** @@ -278,7 +282,7 @@ var reverse = function(s) { }; ``` -TypeScript: +### TypeScript: ```typescript /** @@ -299,7 +303,7 @@ function reverseString(s: string[]): void { }; ``` -Swift: +### Swift: ```swift // 双指针 - 元组 @@ -316,7 +320,8 @@ func reverseString(_ s: inout [Character]) { ``` -Rust: +### Rust: + ```Rust impl Solution { pub fn reverse_string(s: &mut Vec) { @@ -332,7 +337,8 @@ impl Solution { } ``` -C: +### C: + ```c void reverseString(char* s, int sSize){ int left = 0; @@ -347,7 +353,8 @@ void reverseString(char* s, int sSize){ } ``` -C#: +### C#: + ```csharp public class Solution { @@ -361,8 +368,8 @@ public class Solution } ``` +### PHP: -PHP: ```php // 双指针 // 一: @@ -392,7 +399,8 @@ function reverse(&$s, $start, $end) { } ``` -Scala: +### Scala: + ```scala object Solution { def reverseString(s: Array[Char]): Unit = { @@ -411,4 +419,3 @@ object Solution { - From 5cb250100b49e1b8109ddf01ba2109b257094b47 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:37:57 +0800 Subject: [PATCH 0545/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200541.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...\345\255\227\347\254\246\344\270\262II.md" | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index 179395b3ac..80e662f9dd 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -23,9 +23,11 @@ 输入: s = "abcdefg", k = 2 输出: "bacdfeg" -# 思路 +## 算法公开课 -针对本题,我录制了视频讲解:[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这道题目其实也是模拟,实现题目中规定的反转规则就可以了。 @@ -42,8 +44,6 @@ 那么这里具体反转的逻辑我们要不要使用库函数呢,其实用不用都可以,使用reverse来实现反转也没毛病,毕竟不是解题关键部分。 -# C++代码 - 使用C++库函数reverse的版本如下: ```CPP @@ -129,7 +129,7 @@ public: ## 其他语言版本 -C: +### C: ```c char * reverseStr(char * s, int k){ @@ -152,7 +152,7 @@ char * reverseStr(char * s, int k){ } ``` -Java: +### Java: ```Java //解法一 @@ -256,7 +256,8 @@ class Solution { } } ``` -Python: +### Python: + ```python class Solution: def reverseStr(self, s: str, k: int) -> str: @@ -281,7 +282,7 @@ class Solution: return ''.join(res) ``` -Python3 (v2): +### Python3 (v2): ```python class Solution: @@ -296,7 +297,7 @@ class Solution: return s ``` -Go: +### Go: ```go func reverseStr(s string, k int) string { @@ -325,7 +326,7 @@ func reverse(b []byte) { } ``` -javaScript: +### JavaScript: ```js @@ -346,7 +347,7 @@ var reverseStr = function(s, k) { ``` -TypeScript: +### TypeScript: ```typescript function reverseStr(s: string, k: number): string { @@ -368,7 +369,7 @@ function reverseStr(s: string, k: number): string { }; ``` -Swift: +### Swift: ```swift func reverseStr(_ s: String, _ k: Int) -> String { @@ -388,7 +389,8 @@ func reverseStr(_ s: String, _ k: Int) -> String { } ``` -C#: +### C#: + ```csharp public class Solution { @@ -403,7 +405,7 @@ public class Solution } } ``` -Scala: +### Scala: 版本一: (正常解法) ```scala @@ -469,7 +471,7 @@ object Solution { } ``` -Rust: +### Rust: ```Rust impl Solution { @@ -503,4 +505,3 @@ impl Solution { - From 963eb8fc8e58094388bf6c0f27be7563d7a488b9 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:41:59 +0800 Subject: [PATCH 0546/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91?= =?UTF-8?q?=E6=8C=87Offer05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC=20?= =?UTF-8?q?=E6=8E=92=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 --- ...77\346\215\242\347\251\272\346\240\274.md" | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index dbad781ef3..fed08a5346 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -15,7 +15,7 @@ 输入:s = "We are happy." 输出:"We%20are%20happy." -# 思路 +## 思路 如果想把这道题目做到极致,就不要只用额外的辅助空间了! @@ -86,7 +86,7 @@ public: * [142.环形链表II](https://programmercarl.com/0142.环形链表II.html) * [344.反转字符串](https://programmercarl.com/0344.反转字符串.html) -# 拓展 +## 拓展 这里也给大家拓展一下字符串和数组有什么差别, @@ -121,7 +121,8 @@ for (int i = 0; i < a.size(); i++) { ## 其他语言版本 -C: +### C: + ```C char* replaceSpace(char* s){ //统计空格数量 @@ -152,8 +153,8 @@ char* replaceSpace(char* s){ } ``` +### Java: -Java: ```Java //使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制 public static String replaceSpace(String s) { @@ -211,8 +212,8 @@ public String replaceSpace(String s) { } ``` +### Go: -Go: ```go // 遍历添加 func replaceSpace(s string) string { @@ -264,9 +265,10 @@ func replaceSpace(s string) string { +### python: + +因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1) -python: -#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1) (版本一)转换成列表,并且添加相匹配的空间,然后进行填充 ```python class Solution: @@ -328,7 +330,7 @@ class Solution: def replaceSpace(self, s: str) -> str: return s.replace(' ', '%20') ``` -javaScript: +### JavaScript: ```js /** @@ -366,7 +368,7 @@ javaScript: }; ``` -TypeScript: +### TypeScript: ```typescript function replaceSpace(s: string): string { @@ -393,7 +395,7 @@ function replaceSpace(s: string): string { }; ``` -Swift: +### Swift: ```swift func replaceSpace(_ s: String) -> String { @@ -434,7 +436,7 @@ func replaceSpace(_ s: String) -> String { } ``` -Scala: +### Scala: 方式一: 双指针 ```scala @@ -491,8 +493,8 @@ object Solution { } ``` +### PHP: -PHP: ```php function replaceSpace($s){ $sLen = strlen($s); @@ -527,7 +529,7 @@ function spaceLen($s){ } ``` -Rust +### Rust: ```Rust impl Solution { @@ -563,3 +565,4 @@ impl Solution { + From e70ca923440e656d4c62761fcff2ff40e9c17b32 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:47:12 +0800 Subject: [PATCH 0547/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\347\232\204\345\215\225\350\257\215.md" | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 6dd3cd4975..19ccb725a0 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -28,10 +28,11 @@ 输出: "example good a" 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),结合本题解一起看,事半功倍! +## 思路 **这道题目可以说是综合考察了字符串的多种操作。** @@ -204,8 +205,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -433,9 +433,10 @@ class Solution { } ``` -python: +### python: (版本一)先删除空白,然后整个反转,最后单词反转。 **因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)** + ```Python class Solution: def reverseWords(self, s: str) -> str: @@ -467,7 +468,7 @@ class Solution: return " ".join(words) ``` -Go: +### Go: 版本一: @@ -571,7 +572,8 @@ func reverse(b *[]byte, left, right int) { -javaScript: +### JavaScript: + ```js /** * @param {string} s @@ -630,7 +632,7 @@ function reverse(strArr, start, end) { } ``` -TypeScript: +### TypeScript: ```typescript function reverseWords(s: string): string { @@ -689,7 +691,7 @@ function reverseWords(s: string): string { }; ``` -Swift: +### Swift: ```swift func reverseWords(_ s: String) -> String { @@ -766,7 +768,7 @@ func reverseWord(_ s: inout [Character]) { } ``` -Scala: +### Scala: ```scala object Solution { @@ -824,8 +826,8 @@ object Solution { } ``` +### PHP: -PHP: ```php function reverseWords($s) { $this->removeExtraSpaces($s); @@ -872,7 +874,7 @@ function reverseString(&$s, $start, $end) { return ; } ``` -Rust: +### Rust: ```Rust // 根据C++版本二思路进行实现 @@ -924,7 +926,7 @@ pub fn remove_extra_spaces(s: &mut Vec) { } } ``` -C: +### C: ```C // 翻转字符串中指定范围的字符 @@ -972,3 +974,4 @@ char * reverseWords(char * s){ + From 370a4d1c05b1204c652c00bcb90702947df11795 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:50:44 +0800 Subject: [PATCH 0548/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91?= =?UTF-8?q?=E6=8C=87Offer58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\255\227\347\254\246\344\270\262.md" | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 6cd8845609..008b7915c3 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -24,7 +24,7 @@ 限制: 1 <= k < s.length <= 10000 -# 思路 +## 思路 为了让本题更有意义,提升一下本题难度:**不能申请额外空间,只能在本串上操作**。 @@ -71,7 +71,7 @@ public: 是不是发现这代码也太简单了,哈哈。 -# 总结 +## 总结 此时我们已经反转好多次字符串了,来一起回顾一下吧。 @@ -86,7 +86,7 @@ public: 好了,反转字符串一共就介绍到这里,相信大家此时对反转字符串的常见操作已经很了解了。 -# 题外话 +## 题外话 一些同学热衷于使用substr,来做这道题。 其实使用substr 和 反转 时间复杂度是一样的 ,都是O(n),但是使用substr申请了额外空间,所以空间复杂度是O(n),而反转方法的空间复杂度是O(1)。 @@ -96,7 +96,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```java class Solution { public String reverseLeftWords(String s, int n) { @@ -141,7 +142,7 @@ class Solution { } ``` -python: +### python: (版本一)使用切片 ```python @@ -211,7 +212,7 @@ class Solution: ``` -Go: +### Go: ```go func reverseLeftWords(s string, n int) string { @@ -234,8 +235,7 @@ func reverse(b []byte, left, right int){ } ``` - -JavaScript: +### JavaScript: ```javascript var reverseLeftWords = function(s, n) { @@ -279,7 +279,7 @@ var reverseLeftWords = function (s, n) { }; ``` -TypeScript: +### TypeScript: ```typescript function reverseLeftWords(s: string, n: number): string { @@ -311,7 +311,7 @@ function reverseLeftWords(s: string, n: number): string { }; ``` -Swift: +### Swift: ```swift func reverseLeftWords(_ s: String, _ n: Int) -> String { @@ -358,8 +358,7 @@ function reverse(&$s, $start, $end) { } ``` - -Scala: +### Scala: ```scala object Solution { @@ -388,7 +387,7 @@ object Solution { } ``` -Rust: +### Rust: ```Rust impl Solution { @@ -419,3 +418,4 @@ impl Solution { + From 5acebcc6f70f37d675f03356ecfc2f8f43b5949b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 16:00:41 +0800 Subject: [PATCH 0549/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200028.=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0strStr=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index ac984d1a59..53b57fd534 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -28,7 +28,7 @@ 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。 -# 思路 +## 思路 本题是KMP 经典题目。 @@ -60,13 +60,13 @@ KMP的经典思想就是:**当出现字符串不匹配时,可以记录一部 读完本篇可以顺便把leetcode上28.实现strStr()题目做了。 -# 什么是KMP +### 什么是KMP 说到KMP,先说一下KMP这个名字是怎么来的,为什么叫做KMP呢。 因为是由这三位学者发明的:Knuth,Morris和Pratt,所以取了三位学者名字的首字母。所以叫做KMP -# KMP有什么用 +### KMP有什么用 KMP主要应用在字符串匹配上。 @@ -84,7 +84,7 @@ KMP的主要思想是**当出现字符串不匹配时,可以知道一部分之 下面Carl就带大家把KMP的精髓,next数组弄清楚。 -# 什么是前缀表 +### 什么是前缀表 写过KMP的同学,一定都写过next数组,那么这个next数组究竟是个啥呢? @@ -122,7 +122,7 @@ next数组就是一个前缀表(prefix table)。 那么什么是前缀表:**记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。** -# 最长公共前后缀? +### 最长公共前后缀 文章中字符串的**前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串**。 @@ -144,7 +144,7 @@ next数组就是一个前缀表(prefix table)。 等等.....。 -# 为什么一定要用前缀表 +### 为什么一定要用前缀表 这就是前缀表,那为啥就能告诉我们 上次匹配的位置,并跳过去呢? @@ -163,7 +163,7 @@ next数组就是一个前缀表(prefix table)。 **很多介绍KMP的文章或者视频并没有把为什么要用前缀表?这个问题说清楚,而是直接默认使用前缀表。** -# 如何计算前缀表 +### 如何计算前缀表 接下来就要说一说怎么计算前缀表。 @@ -205,7 +205,7 @@ next数组就是一个前缀表(prefix table)。 最后就在文本串中找到了和模式串匹配的子串了。 -# 前缀表与next数组 +### 前缀表与next数组 很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? @@ -217,7 +217,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减 后面我会提供两种不同的实现代码,大家就明白了。 -# 使用next数组来匹配 +### 使用next数组来匹配 **以下我们以前缀表统一减一之后的next数组来做演示**。 @@ -229,7 +229,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减 ![KMP精讲4](https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B24.gif) -# 时间复杂度分析 +### 时间复杂度分析 其中n为文本串长度,m为模式串长度,因为在匹配的过程中,根据前缀表不断调整匹配的位置,可以看出匹配的过程是O(n),之前还要单独生成next数组,时间复杂度是O(m)。所以整个KMP算法的时间复杂度是O(n+m)的。 @@ -239,7 +239,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减 都知道使用KMP算法,一定要构造next数组。 -# 构造next数组 +### 构造next数组 我们定义一个函数getNext来构建next数组,函数参数为指向next数组的指针,和一个字符串。 代码如下: @@ -338,7 +338,7 @@ void getNext(int* next, const string& s){ 得到了next数组之后,就要用这个来做匹配了。 -# 使用next数组来做匹配 +### 使用next数组来做匹配 在文本串s里 找是否出现过模式串t。 @@ -403,7 +403,7 @@ for (int i = 0; i < s.size(); i++) { // 注意i就从0开始 此时所有逻辑的代码都已经写出来了,力扣 28.实现strStr 题目的整体代码如下: -# 前缀表统一减一 C++代码实现 +### 前缀表统一减一 C++代码实现 ```CPP class Solution { @@ -447,7 +447,7 @@ public: * 时间复杂度: O(n + m) * 空间复杂度: O(m), 只需要保存字符串needle的前缀表 -# 前缀表(不减一)C++实现 +### 前缀表(不减一)C++实现 那么前缀表就不减一了,也不右移的,到底行不行呢? @@ -546,7 +546,7 @@ public: * 空间复杂度: O(m) -# 总结 +## 总结 我们介绍了什么是KMP,KMP可以解决什么问题,然后分析KMP算法里的next数组,知道了next数组就是前缀表,再分析为什么要是前缀表而不是什么其他表。 @@ -563,8 +563,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -691,8 +690,9 @@ class Solution { } ``` -Python3: +### Python3: (版本一)前缀表(减一) + ```python class Solution: def getNext(self, next, s): @@ -781,9 +781,9 @@ class Solution: def strStr(self, haystack: str, needle: str) -> int: return haystack.find(needle) -``` +``` -Go: +### Go: ```go // 方法一:前缀表使用减1实现 @@ -871,7 +871,7 @@ func strStr(haystack string, needle string) int { } ``` -JavaScript版本 +### JavaScript: > 前缀表统一减一 @@ -959,7 +959,7 @@ var strStr = function (haystack, needle) { }; ``` -TypeScript版本: +### TypeScript: > 前缀表统一减一 @@ -1036,7 +1036,7 @@ function strStr(haystack: string, needle: string): number { } ``` -Swift 版本 +### Swift: > 前缀表统一减一 @@ -1196,7 +1196,7 @@ func strStr(_ haystack: String, _ needle: String) -> Int { ``` -PHP: +### PHP: > 前缀表统一减一 ```php @@ -1272,7 +1272,7 @@ function getNext(&$next, $s){ } ``` -Rust: +### Rust: > 前缀表统一不减一 ```Rust @@ -1362,4 +1362,3 @@ impl Solution { - From 202dd382d29eb34f311d5f35f001255b2b770402 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 16:06:12 +0800 Subject: [PATCH 0550/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200459.=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=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 --- ...20\345\255\227\347\254\246\344\270\262.md" | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index e26d04ad87..f99102ab45 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > KMP算法还能干这个 # 459.重复的子字符串 @@ -29,9 +27,11 @@ * 输出: True * 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) -# 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串这么玩,可有点难度! | LeetCode:459.重复的子字符串](https://www.bilibili.com/video/BV1cg41127fw),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[字符串这么玩,可有点难度! | LeetCode:459.重复的子字符串](https://www.bilibili.com/video/BV1cg41127fw),结合本题解一起看,事半功倍! +## 思路 暴力的解法, 就是一个for循环获取 子串的终止位置, 然后判断子串是否能重复构成字符串,又嵌套一个for循环,所以是O(n^2)的时间复杂度。 @@ -44,7 +44,7 @@ 主要讲一讲移动匹配 和 KMP两种方法。 -## 移动匹配 +### 移动匹配 当一个字符串s:abcabc,内部由重复的子串组成,那么这个字符串的结构一定是这样的: @@ -80,9 +80,9 @@ public: 如果我们做过 [28.实现strStr](https://programmercarl.com/0028.实现strStr.html) 题目的话,其实就知道,**实现一个 高效的算法来判断 一个字符串中是否出现另一个字符串是很复杂的**,这里就涉及到了KMP算法。 -## KMP +### KMP -### 为什么会使用KMP +#### 为什么会使用KMP 以下使用KMP方式讲解,强烈建议大家先把以下两个视频看了,理解KMP算法,再来看下面讲解,否则会很懵。 * [视频讲解版:帮你把KMP算法学个通透!(理论篇)](https://www.bilibili.com/video/BV1PD4y1o7nd/) @@ -105,7 +105,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 ![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728205249.png) -### 如何找到最小重复子串 +#### 如何找到最小重复子串 这里有同学就问了,为啥一定是开头的ab呢。 其实最关键还是要理解 最长相等前后缀,如图: @@ -123,7 +123,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 正是因为 最长相等前后缀的规则,当一个字符串由重复子串组成的,最长相等前后缀不包含的子串就是最小重复子串。 -### 简单推理 +#### 简单推理 这里再给出一个数学推导,就容易理解很多。 @@ -229,7 +229,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -261,8 +261,7 @@ class Solution { } ``` - -Python: +### Python: (版本一) 前缀表 减一 ```python @@ -346,8 +345,7 @@ class Solution: return False ``` - -Go: +### Go: 这里使用了前缀表统一减一的实现方式 @@ -405,7 +403,7 @@ func repeatedSubstringPattern(s string) bool { } ``` -JavaScript版本 +### JavaScript: > 前缀表统一减一 @@ -479,7 +477,7 @@ var repeatedSubstringPattern = function (s) { }; ``` -TypeScript: +### TypeScript: > 前缀表统一减一 @@ -539,8 +537,7 @@ function repeatedSubstringPattern(s: string): boolean { }; ``` - -Swift: +### Swift: > 前缀表统一减一 ```swift @@ -623,7 +620,7 @@ Swift: } ``` -Rust: +### Rust: >前缀表统一不减一 ```Rust From 4fe1f08acd1a7fe4a6af2ea4920ca97a284ce0c4 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 16:09:03 +0800 Subject: [PATCH 0551/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...\254\246\344\270\262\346\200\273\347\273\223.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" index c12d1764ad..5c2f016471 100644 --- "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" +++ "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" @@ -11,7 +11,7 @@ 那么这次我们来做一个总结。 -# 什么是字符串 +## 什么是字符串 字符串是若干字符组成的有限序列,也可以理解为是一个字符数组,但是很多语言对字符串做了特殊的规定,接下来我来说一说C/C++中的字符串。 @@ -42,7 +42,7 @@ for (int i = 0; i < a.size(); i++) { 所以想处理字符串,我们还是会定义一个string类型。 -# 要不要使用库函数 +## 要不要使用库函数 在文章[344.反转字符串](https://programmercarl.com/0344.反转字符串.html)中强调了**打基础的时候,不要太迷恋于库函数。** @@ -52,7 +52,7 @@ for (int i = 0; i < a.size(); i++) { **如果库函数仅仅是 解题过程中的一小部分,并且你已经很清楚这个库函数的内部实现原理的话,可以考虑使用库函数。** -# 双指针法 +## 双指针法 在[344.反转字符串](https://programmercarl.com/0344.反转字符串.html) ,我们使用双指针法实现了反转字符串的操作,**双指针法在数组,链表和字符串中很常用。** @@ -67,7 +67,7 @@ for (int i = 0; i < a.size(); i++) { 一些同学会使用for循环里调用库函数erase来移除元素,这其实是O(n^2)的操作,因为erase就是O(n)的操作,所以这也是典型的不知道库函数的时间复杂度,上来就用的案例了。 -# 反转系列 +## 反转系列 在反转上还可以在加一些玩法,其实考察的是对代码的掌控能力。 @@ -87,7 +87,7 @@ for (int i = 0; i < a.size(); i++) { 在[字符串:反转个字符串还有这个用处?](https://programmercarl.com/剑指Offer58-II.左旋转字符串.html)中,我们通过**先局部反转再整体反转**达到了左旋的效果。 -# KMP +## KMP KMP的主要思想是**当出现字符串不匹配时,可以知道一部分之前已经匹配的文本内容,可以利用这些信息避免从头再去做匹配了。** @@ -110,7 +110,7 @@ KMP的精髓所在就是前缀表,在[KMP精讲](https://programmercarl.com/00 其中主要**理解j=next[x]这一步最为关键!** -# 总结 +## 总结 字符串类类型的题目,往往想法比较简单,但是实现起来并不容易,复杂的字符串题目非常考验对代码的掌控能力。 @@ -128,3 +128,4 @@ KMP算法是字符串查找最重要的算法,但彻底理解KMP并不容易 + From 1a2c911194a6eb2e9c35f7f179b76553bd48f45c Mon Sep 17 00:00:00 2001 From: Wang Ben Date: Wed, 19 Jul 2023 17:41:44 +0800 Subject: [PATCH 0552/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201207=20=E7=8B=AC?= =?UTF-8?q?=E4=B8=80=E6=97=A0=E4=BA=8C=E7=9A=84=E5=87=BA=E7=8E=B0=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=20Go=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\272\347\216\260\346\254\241\346\225\260.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index 1a7a001989..d7423b2b7d 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -135,6 +135,22 @@ class Solution: Go: +```Go +func uniqueOccurrences(arr []int) bool { + count := make(map[int]int) // 统计数字出现的频率 + for _, v := range arr { + count[v] += 1 + } + fre := make(map[int]struct{}) // 看相同频率是否重复出现 + for _, v := range count { + if _, ok := fre[v]; ok { + return false + } + fre[v] = struct{}{} + } + return true +} +``` JavaScript: ``` javascript From c3ebc91d4db0fc5b86945b910babd8c4ad2328b2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:27:16 +0800 Subject: [PATCH 0553/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8F=8C?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...46\214\207\351\222\210\346\200\273\347\273\223.md" | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" index 04a8cb9af8..6621e0396b 100644 --- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" +++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" @@ -8,7 +8,8 @@ 相信大家已经对双指针法很熟悉了,但是双指针法并不隶属于某一种数据结构,我们在讲解数组,链表,字符串都用到了双指针法,所有有必要针对双指针法做一个总结。 -# 数组篇 +# 双指针总结篇 +## 数组篇 在[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)中,原地移除数组上的元素,我们说到了数组上的元素,不能真正的删除,只能覆盖。 @@ -26,7 +27,7 @@ for (int i = 0; i < array.size(); i++) { 所以此时使用双指针法才展现出效率的优势:**通过两个指针在一个for循环下完成两个for循环的工作。** -# 字符串篇 +## 字符串篇 在[字符串:这道题目,使用库函数一行代码搞定](https://programmercarl.com/0344.反转字符串.html)中讲解了反转字符串,注意这里强调要原地反转,要不然就失去了题目的意义。 @@ -48,7 +49,7 @@ for (int i = 0; i < array.size(); i++) { **主要还是大家用erase用的比较随意,一定要注意for循环下用erase的情况,一般可以用双指针写效率更高!** -# 链表篇 +## 链表篇 翻转链表是现场面试,白纸写代码的好题,考察了候选者对链表以及指针的熟悉程度,而且代码也不长,适合在白纸上写。 @@ -62,7 +63,7 @@ for (int i = 0; i < array.size(); i++) { 那么找到环的入口,其实需要点简单的数学推理,我在文章中把找环的入口清清楚楚的推理的一遍,如果对找环入口不够清楚的同学建议自己看一看[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)。 -# N数之和篇 +## N数之和篇 在[哈希表:解决了两数之和,那么能解决三数之和么?](https://programmercarl.com/0015.三数之和.html)中,讲到使用哈希法可以解决1.两数之和的问题 @@ -87,7 +88,7 @@ for (int i = 0; i < array.size(); i++) { 同样的道理,五数之和,n数之和都是在这个基础上累加。 -# 总结 +## 总结 本文中一共介绍了leetcode上九道使用双指针解决问题的经典题目,除了链表一些题目一定要使用双指针,其他题目都是使用双指针来提高效率,一般是将O(n^2)的时间复杂度,降为$O(n)$。 From de770c60e5ae781a9e65071e03ba4876237ed6fb Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:36:04 +0800 Subject: [PATCH 0554/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=A0=88?= =?UTF-8?q?=E4=B8=8E=E9=98=9F=E5=88=97=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=20=E6=8E=92=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 --- ...10\227\347\220\206\350\256\272\345\237\272\347\241\200.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index 0075deb6c2..ad748e489e 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -4,8 +4,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ > 来看看栈和队列不为人知的一面 +# 栈与队列理论基础 + 我想栈和队列的原理大家应该很熟悉了,队列是先进先出,栈是先进后出。 如图所示: @@ -93,3 +96,4 @@ std::queue> third; // 定义以list为底层容器的队列 + From 4b5e198b170bade71d565d8ba3f7e29543f3bbef Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:40:33 +0800 Subject: [PATCH 0555/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200232.=E7=94=A8?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97=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 --- ...36\347\216\260\351\230\237\345\210\227.md" | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 4a57ee966b..c510fc12a6 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -36,10 +36,11 @@ queue.empty(); // 返回 false * 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。 * 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。 -## 思路 +## 算法公开课 -《代码随想录》算法公开课:[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对栈和队列的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 思路 这是一道模拟题,不涉及到具体算法,考察的就是对栈和队列的掌握程度。 @@ -132,7 +133,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java class MyQueue { @@ -179,8 +180,8 @@ class MyQueue { ``` +### Python: -Python: ```python class MyQueue: @@ -231,8 +232,8 @@ class MyQueue: ``` +### Go: -Go: ```Go type MyQueue struct { stackIn []int //输入栈 @@ -283,7 +284,7 @@ func (this *MyQueue) Empty() bool { } ``` - javaScript: +### JavaScript: ```js // 使用两个数组的栈方法(push, pop) 实现队列 @@ -338,7 +339,7 @@ MyQueue.prototype.empty = function() { }; ``` -TypeScript: +### TypeScript: ```typescript class MyQueue { @@ -374,7 +375,7 @@ class MyQueue { } ``` -Swift: +### Swift: ```swift class MyQueue { @@ -413,7 +414,8 @@ class MyQueue { } ``` -C: +### C: + ```C /* 1.两个type为int的数组(栈),大小为100 @@ -490,8 +492,8 @@ void myQueueFree(MyQueue* obj) { } ``` +### C#: -C#: ```csharp public class MyQueue { Stack inStack; @@ -534,7 +536,8 @@ public class MyQueue { -PHP: +### PHP: + ```php // SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5 >= 5.3.0, PHP 7, PHP 8] // https://www.php.net/manual/zh/class.splstack.php @@ -579,7 +582,8 @@ class MyQueue { } ``` -Scala: +### Scala: + ```scala class MyQueue() { import scala.collection.mutable @@ -621,7 +625,7 @@ class MyQueue() { } ``` -rust: +### Rust: ```rust struct MyQueue { @@ -666,4 +670,3 @@ impl MyQueue { - From c18dbf059544b7bd100ff101cc92a2ee241434e5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:45:24 +0800 Subject: [PATCH 0556/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=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 --- ...27\345\256\236\347\216\260\346\240\210.md" | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 94c7940486..13b742f839 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -25,11 +25,11 @@ * 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。 * 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - -《代码随想录》算法公开课:[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对链表的理解。 +## 思路 (这里要强调是单向队列) @@ -114,7 +114,7 @@ public: * 时间复杂度: push为O(n),其他为O(1) * 空间复杂度: O(n) -# 优化 +## 优化 其实这道题目就是用一个队列就够了。 @@ -162,9 +162,9 @@ public: * 空间复杂度: O(n) -# 其他语言版本 +## 其他语言版本 -Java: +### Java: 使用两个 Queue 实现方法1 ```java @@ -404,7 +404,7 @@ class MyStack { } ``` -Python: +### Python: ```python from collections import deque @@ -496,8 +496,7 @@ class MyStack: return not self.que ``` - -Go: +### Go: 使用两个队列实现 ```go @@ -628,9 +627,7 @@ func (this *MyStack) Empty() bool { */ ``` - - -javaScript: +### JavaScript: 使用数组(push, shift)模拟队列 @@ -740,7 +737,7 @@ MyStack.prototype.empty = function() { ``` -TypeScript: +### TypeScript: 版本一:使用两个队列模拟栈 @@ -812,7 +809,7 @@ class MyStack { } ``` -Swift +### Swift: ```Swift // 定义一个队列数据结构 @@ -931,8 +928,9 @@ class MyStack { } } ``` -Scala: +### Scala: 使用两个队列模拟栈: + ```scala import scala.collection.mutable @@ -1015,8 +1013,8 @@ class MyStack() { } ``` +### C#: -C#: ```csharp public class MyStack { Queue queue1; @@ -1051,8 +1049,9 @@ public class MyStack { } ``` -PHP -> 双对列 +### PHP: + +> 双队列 ```php // SplQueue 类通过使用一个双向链表来提供队列的主要功能。(PHP 5 >= 5.3.0, PHP 7, PHP 8) // https://www.php.net/manual/zh/class.splqueue.php @@ -1130,6 +1129,8 @@ class MyStack { } ``` +### Rust: + > rust:单队列 ```rust From a1ef2d03f64f6884fade8bfe5d2ed9dbceb129d8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:49:03 +0800 Subject: [PATCH 0557/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...10\347\232\204\346\213\254\345\217\267.md" | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index 213d61b795..045c79ee7f 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -39,12 +39,13 @@ * 输入: "{[]}" * 输出: true -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[栈的拿手好戏!| LeetCode:20. 有效的括号](https://www.bilibili.com/video/BV1AF411w78g),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的拿手好戏!| LeetCode:20. 有效的括号](https://www.bilibili.com/video/BV1AF411w78g),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 思路 -## 题外话 +### 题外话 **括号匹配是使用栈解决的经典问题。** @@ -68,7 +69,7 @@ cd a/b/c/../../ 这里我就不过多展开了,先来看题。 -## 进入正题 +### 进入正题 由于栈结构的特殊性,非常适合做对称匹配类的题目。 @@ -143,8 +144,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public boolean isValid(String s) { @@ -171,7 +172,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法一,仅使用栈,更省空间 class Solution: @@ -213,7 +215,8 @@ class Solution: return True if not stack else False ``` -Go: +### Go: + ```Go func isValid(s string) bool { hash := map[byte]byte{')':'(', ']':'[', '}':'{'} @@ -235,7 +238,8 @@ func isValid(s string) bool { } ``` -Ruby: +### Ruby: + ```ruby def is_valid(strs) symbol_map = {')' => '(', '}' => '{', ']' => '['} @@ -253,7 +257,8 @@ def is_valid(strs) end ``` -Javascript: +### Javascript: + ```javascript var isValid = function (s) { const stack = []; @@ -296,7 +301,7 @@ var isValid = function(s) { }; ``` -TypeScript: +### TypeScript: 版本一:普通版 @@ -348,7 +353,7 @@ function isValid(s: string): boolean { }; ``` -Swift +### Swift: ```swift func isValid(_ s: String) -> Bool { @@ -373,7 +378,8 @@ func isValid(_ s: String) -> Bool { } ``` -C: +### C: + ```C //辅助函数:判断栈顶元素与输入的括号是否为一对。若不是,则返回False int notMatch(char par, char* stack, int stackTop) { @@ -414,8 +420,8 @@ bool isValid(char * s){ } ``` +### C#: -C#: ```csharp public class Solution { public bool IsValid(string s) { @@ -447,7 +453,8 @@ public class Solution { } ``` -PHP: +### PHP: + ```php // https://www.php.net/manual/zh/class.splstack.php class Solution @@ -475,8 +482,8 @@ class Solution } ``` +### Scala: -Scala: ```scala object Solution { import scala.collection.mutable @@ -499,7 +506,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution { From 70888c2b6074fa28f291108b4225735f6c24126f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:53:33 +0800 Subject: [PATCH 0558/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=201047.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20?= =?UTF-8?q?=E6=8E=92=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 --- ...73\351\207\215\345\244\215\351\241\271.md" | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 486d198b4d..ad54f0f88e 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > 匹配问题都是栈的强项 # 1047. 删除字符串中的所有相邻重复项 @@ -30,11 +28,13 @@ * 1 <= S.length <= 20000 * S 仅由小写英文字母组成。 -# 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的好戏还要继续!| LeetCode:1047. 删除字符串中的所有相邻重复项](https://www.bilibili.com/video/BV12a411P7mw),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -《代码随想录》算法视频公开课:[栈的好戏还要继续!| LeetCode:1047. 删除字符串中的所有相邻重复项](https://www.bilibili.com/video/BV12a411P7mw),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 思路 -## 正题 +### 正题 本题要删除相邻相同元素,相对于[20. 有效的括号](https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html)来说其实也是匹配问题,20. 有效的括号 是匹配左右括号,本题是匹配相邻元素,最后都是做消除的操作。 @@ -105,7 +105,7 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1),返回值不计空间复杂度 -## 题外话 +### 题外话 这道题目就像是我们玩过的游戏对对碰,如果相同的元素挨在一起就要消除。 @@ -125,8 +125,7 @@ public: ## 其他语言版本 - -Java: +### Java: 使用 Deque 作为堆栈 ```Java @@ -203,7 +202,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法一,使用栈 class Solution: @@ -239,7 +239,7 @@ class Solution: return ''.join(res[0: slow]) ``` -Go: +### Go: ```go func removeDuplicates(s string) string { @@ -258,7 +258,7 @@ func removeDuplicates(s string) string { } ``` -javaScript: +### JavaScript: 法一:使用栈 @@ -295,7 +295,7 @@ var removeDuplicates = function(s) { }; ``` -TypeScript: +### TypeScript: ```typescript function removeDuplicates(s: string): string { @@ -318,7 +318,7 @@ function removeDuplicates(s: string): string { }; ``` -C: +### C: 方法一:使用栈 ```c @@ -371,7 +371,8 @@ char * removeDuplicates(char * s){ } ``` -Swift: +### Swift: + ```swift func removeDuplicates(_ s: String) -> String { var stack = [Character]() @@ -386,8 +387,8 @@ func removeDuplicates(_ s: String) -> String { } ``` +### C#: -C#: ```csharp public string RemoveDuplicates(string s) { //拿字符串直接作为栈,省去了栈还要转为字符串的操作 @@ -405,8 +406,8 @@ public string RemoveDuplicates(string s) { } ``` +### PHP: -PHP: ```php class Solution { function removeDuplicates($s) { @@ -431,8 +432,8 @@ class Solution { } ``` +### Scala: -Scala: ```scala object Solution { import scala.collection.mutable @@ -455,7 +456,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution { From 7f4d74049ebc0d42930d5afe51b4c766295b0366 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:57:59 +0800 Subject: [PATCH 0559/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200150.=E9=80=86?= =?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?= =?UTF-8?q?=20=E6=8E=92=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 --- ...76\345\274\217\346\261\202\345\200\274.md" | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 09cc4f96e3..663a68ea5c 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > 这不仅仅是一道好题,也展现出计算机的思考方式 # 150. 逆波兰表达式求值 @@ -63,9 +61,13 @@ * 适合用栈操作运算:遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。 -# 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -《代码随想录》算法视频公开课:[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解。 +## 思路 + +### 正题 在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。 @@ -117,7 +119,7 @@ public: * 空间复杂度: O(n) -## 题外话 +### 题外话 我们习惯看到的表达式都是中缀表达式,因为符合我们的习惯,但是中缀表达式对于计算机来说就不是很友好了。 @@ -134,11 +136,9 @@ public: > During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators, and continued to use it in some models into the 2020s. - - ## 其他语言版本 -java: +### Java: ```Java class Solution { @@ -164,7 +164,7 @@ class Solution { } ``` -python3 +### Python3: ```python from operator import add, sub, mul @@ -201,7 +201,8 @@ class Solution: ``` -Go: +### Go: + ```Go func evalRPN(tokens []string) int { stack := []int{} @@ -228,7 +229,7 @@ func evalRPN(tokens []string) int { } ``` -javaScript: +### JavaScript: ```js var evalRPN = function (tokens) { @@ -259,7 +260,7 @@ var evalRPN = function (tokens) { }; ``` -TypeScript: +### TypeScript: 普通版: @@ -324,7 +325,8 @@ function evalRPN(tokens: string[]): number { }; ``` -Swift: +### Swift: + ```Swift func evalRPN(_ tokens: [String]) -> Int { var stack = [Int]() @@ -357,7 +359,8 @@ func evalRPN(_ tokens: [String]) -> Int { } ``` -C#: +### C#: + ```csharp public int EvalRPN(string[] tokens) { int num; @@ -391,8 +394,8 @@ public int EvalRPN(string[] tokens) { } ``` +### PHP: -PHP: ```php class Solution { function evalRPN($tokens) { @@ -417,7 +420,8 @@ class Solution { } ``` -Scala: +### Scala: + ```scala object Solution { import scala.collection.mutable @@ -447,7 +451,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution { From 5a6bf6d85534e304e73460d67db006f5c1475657 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:01:53 +0800 Subject: [PATCH 0560/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC=20?= =?UTF-8?q?=E6=8E=92=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 --- ...43\346\234\200\345\244\247\345\200\274.md" | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index f1c4b76ccf..6f420479eb 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -28,11 +28,11 @@ * -10^4 <= nums[i] <= 10^4 * 1 <= k <= nums.length +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 - -《代码随想录》算法视频公开课:[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 思路 这是使用单调队列的经典题目。 @@ -196,7 +196,7 @@ public: 空间复杂度因为我们定义一个辅助队列,所以是O(k)。 -# 扩展 +## 扩展 大家貌似对单调队列 都有一些疑惑,首先要明确的是,题解中单调队列里的pop和push接口,仅适用于本题哈。单调队列不是一成不变的,而是不同场景不同写法,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 不要以为本题中的单调队列实现就是固定的写法哈。 @@ -204,10 +204,10 @@ public: -# 其他语言版本 +## 其他语言版本 +### Java: -Java: ```Java //解法一 //自定义数组 @@ -298,7 +298,8 @@ class Solution { } ``` -Python: +### Python: + ```python from collections import deque @@ -338,8 +339,7 @@ class Solution: return result ``` - -Go: +### Go: ```go // 封装单调队列的方式解题 @@ -401,7 +401,8 @@ func maxSlidingWindow(nums []int, k int) []int { } ``` -Javascript: +### Javascript: + ```javascript /** * @param {number[]} nums @@ -449,7 +450,7 @@ var maxSlidingWindow = function (nums, k) { }; ``` -TypeScript: +### TypeScript: ```typescript function maxSlidingWindow(nums: number[], k: number): number[] { @@ -497,7 +498,9 @@ function maxSlidingWindow(nums: number[], k: number): number[] { }; ``` -Swift: +### Swift: + +解法一: ```Swift /// 双向链表 @@ -638,7 +641,8 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { return result } ``` -Scala: +### Scala: + ```scala import scala.collection.mutable.ArrayBuffer object Solution { @@ -686,8 +690,8 @@ class MyQueue { } ``` +### PHP: -PHP: ```php class Solution { /** @@ -764,7 +768,8 @@ class MyQueue{ } ``` -C#: +### C#: + ```csharp class myDequeue{ private LinkedList linkedList = new LinkedList(); @@ -805,7 +810,7 @@ class myDequeue{ } ``` -rust: +### Rust: ```rust impl Solution { From 0e4f91d81d68b53269f33aa994ecab626202243b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:13:29 +0800 Subject: [PATCH 0561/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200347.=E5=89=8Dk?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0=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 --- ...30\351\242\221\345\205\203\347\264\240.md" | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 6c8b51b1ad..b3063111b4 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > 前K个大数问题,老生常谈,不得不谈 # 347.前 K 个高频元素 @@ -29,9 +27,11 @@ * 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。 * 你可以按任意顺序返回答案。 -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这道题目主要涉及到如下三块内容: 1. 要统计元素出现频率 @@ -122,7 +122,7 @@ public: * 时间复杂度: O(nlogk) * 空间复杂度: O(n) -# 拓展 +## 拓展 大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。 确实 例如我们在写快排的cmp函数的时候,`return left>right` 就是从大到小,`return left + From f7b87e625a9fab65b30d19b5252aef26c869fbdb Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:15:50 +0800 Subject: [PATCH 0562/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=A0=88?= =?UTF-8?q?=E4=B8=8E=E9=98=9F=E5=88=97=E6=80=BB=E7=BB=93=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 --- ...37\345\210\227\346\200\273\347\273\223.md" | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index c313220165..31ce955489 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -3,9 +3,9 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+# 栈与队列总结篇 - -# 栈与队列的理论基础 +## 栈与队列的理论基础 首先我们在[栈与队列:来看看栈和队列不为人知的一面](https://programmercarl.com/栈与队列理论基础.html)中讲解了栈和队列的理论基础。 @@ -37,9 +37,9 @@ **一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时在去弹出元素就是栈的顺序了。** -# 栈经典题目 +## 栈经典题目 -## 栈在系统中的应用 +### 栈在系统中的应用 如果还记得编译原理的话,编译器在 词法分析的过程中处理括号、花括号等这个符号的逻辑,就是使用了栈这种数据结构。 @@ -59,7 +59,7 @@ cd a/b/c/../../ **所以数据结构与算法的应用往往隐藏在我们看不到的地方!** -## 括号匹配问题 +### 括号匹配问题 在[栈与队列:系统中处处都是栈的应用](https://programmercarl.com/0020.有效的括号.html)中我们讲解了括号匹配问题。 @@ -75,23 +75,23 @@ cd a/b/c/../../ 这里还有一些技巧,在匹配左括号的时候,右括号先入栈,就只需要比较当前元素和栈顶相不相等就可以了,比左括号先入栈代码实现要简单的多了! -## 字符串去重问题 +### 字符串去重问题 在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中讲解了字符串去重问题。 1047. 删除字符串中的所有相邻重复项 思路就是可以把字符串顺序放到一个栈中,然后如果相同的话 栈就弹出,这样最后栈里剩下的元素都是相邻不相同的元素了。 -## 逆波兰表达式问题 +### 逆波兰表达式问题 在[栈与队列:有没有想过计算机是如何处理表达式的?](https://programmercarl.com/0150.逆波兰表达式求值.html)中讲解了求逆波兰表达式。 本题中每一个子表达式要得出一个结果,然后拿这个结果再进行运算,那么**这岂不就是一个相邻字符串消除的过程,和[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中的对对碰游戏是不是就非常像了。** -# 队列的经典题目 +## 队列的经典题目 -## 滑动窗口最大值问题 +### 滑动窗口最大值问题 在[栈与队列:滑动窗口里求最大值引出一个重要数据结构](https://programmercarl.com/0239.滑动窗口最大值.html)中讲解了一种数据结构:单调队列。 @@ -119,7 +119,7 @@ cd a/b/c/../../ 我们用deque作为单调队列的底层数据结构,C++中deque是stack和queue默认的底层实现容器(这个我们之前已经讲过),deque是可以两边扩展的,而且deque里元素并不是严格的连续分布的。 -## 求前 K 个高频元素 +### 求前 K 个高频元素 在[栈与队列:求前 K 个高频元素和队列有啥关系?](https://programmercarl.com/0347.前K个高频元素.html)中讲解了求前 K 个高频元素。 @@ -143,7 +143,7 @@ cd a/b/c/../../ 所以排序的过程的时间复杂度是$O(\log k)$,整个算法的时间复杂度是$O(n\log k)$。 -# 总结 +## 总结 在栈与队列系列中,我们强调栈与队列的基础,也是很多同学容易忽视的点。 @@ -162,3 +162,4 @@ cd a/b/c/../../ + From c6bcd423d6bdab6375e3af8bc7c3c01bfc0518a1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:48:05 +0800 Subject: [PATCH 0563/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?= =?UTF-8?q?=E6=8E=92=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 --- ...06\350\256\272\345\237\272\347\241\200.md" | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 4098cf1f74..184dba604c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -8,8 +8,11 @@ # 二叉树理论基础篇 +## 算法公开课 -《代码随想录》算法视频公开课:[关于二叉树,你该了解这些!](https://www.bilibili.com/video/BV1Hy4y1t7ij),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[关于二叉树,你该了解这些!](https://www.bilibili.com/video/BV1Hy4y1t7ij),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 题目分类 题目分类大纲如下: @@ -189,8 +192,7 @@ struct TreeNode { ## 其他语言版本 - -Java: +### Java: ```java public class TreeNode { @@ -208,8 +210,7 @@ public class TreeNode { } ``` - -Python: +### Python: ```python class TreeNode: @@ -219,7 +220,7 @@ class TreeNode: self.right = right ``` -Go: +### Go: ```go type TreeNode struct { @@ -229,7 +230,7 @@ type TreeNode struct { } ``` -JavaScript: +### JavaScript: ```javascript function TreeNode(val, left, right) { @@ -239,7 +240,7 @@ function TreeNode(val, left, right) { } ``` -TypeScript: +### TypeScript: ```typescript class TreeNode { @@ -254,7 +255,7 @@ class TreeNode { } ``` -Swift: +### Swift: ```Swift class TreeNode { @@ -271,7 +272,7 @@ class TreeNode { } ``` -Scala: +### Scala: ```scala class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { @@ -281,7 +282,7 @@ class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) } ``` -rust: +### Rust: ```rust #[derive(Debug, PartialEq, Eq)] From 7807b5e7989751cd9e5d2df8da6821438e01c378 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:53:16 +0800 Subject: [PATCH 0564/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86?= =?UTF-8?q?=20=E6=8E=92=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 --- ...22\345\275\222\351\201\215\345\216\206.md" | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 92f342f0db..730b18ac6d 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -4,15 +4,15 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- +> 一看就会,一写就废! # 二叉树的递归遍历 +## 算法公开课 -《代码随想录》算法视频公开课:[每次写递归都要靠直觉? 这次带你学透二叉树的递归遍历!](https://www.bilibili.com/video/BV1Wh411S7xt),相信结合视频在看本篇题解,更有助于大家对本题的理解。 - +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[每次写递归都要靠直觉? 这次带你学透二叉树的递归遍历!](https://www.bilibili.com/video/BV1Wh411S7xt),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -> 一看就会,一写就废! +## 思路 这次我们要好好谈一谈递归,为什么很多同学看递归算法都是“一看就会,一写就废”。 @@ -109,14 +109,10 @@ void traversal(TreeNode* cur, vector& vec) { 可能有同学感觉前后中序遍历的递归太简单了,要打迭代法(非递归),别急,我们明天打迭代法,打个通透! +## 其他语言版本 +### Java: - - -# 其他语言版本 - - -Java: ```Java // 前序遍历·递归·LC144_二叉树的前序遍历 class Solution { @@ -171,7 +167,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 前序遍历-递归-LC144_二叉树的前序遍历 # Definition for a binary tree node. @@ -219,7 +216,7 @@ class Solution: return left + right + [root.val] ``` -Go: +### Go: 前序遍历: ```go @@ -273,7 +270,7 @@ func postorderTraversal(root *TreeNode) (res []int) { } ``` -Javascript版本: +### Javascript: 前序遍历: ```Javascript @@ -327,7 +324,7 @@ var postorderTraversal = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历 @@ -370,7 +367,7 @@ function postorderTraversal(node: TreeNode | null): number[] { } ``` -C: +### C: ```c //前序遍历: @@ -422,8 +419,9 @@ int* postorderTraversal(struct TreeNode* root, int* returnSize){ } ``` -Swift: +### Swift: 前序遍历:(144.二叉树的前序遍历) + ```Swift func preorderTraversal(_ root: TreeNode?) -> [Int] { var res = [Int]() @@ -473,7 +471,10 @@ func postorder(_ root: TreeNode?, res: inout [Int]) { res.append(root!.val) } ``` -Scala: 前序遍历:(144.二叉树的前序遍历) +### Scala: + + 前序遍历:(144.二叉树的前序遍历) + ```scala object Solution { import scala.collection.mutable.ListBuffer @@ -525,7 +526,7 @@ object Solution { } ``` -rust: +### Rust: ```rust use std::cell::RefCell; From a13bb8292123ceded30c12bf7e713468fc29e50f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 16:07:52 +0800 Subject: [PATCH 0565/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86?= =?UTF-8?q?=20=E6=8E=92=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 --- ...55\344\273\243\351\201\215\345\216\206.md" | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 8b2414652c..69007995f0 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -4,16 +4,17 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+> 听说还可以用非递归的方式 # 二叉树的迭代遍历 -《代码随想录》算法视频公开课: -* [写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2) -* [写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g) -相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 算法公开课 +[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html): -> 听说还可以用非递归的方式 +* **[写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2)** +* **[写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g)** +**相信结合视频在看本篇题解,更有助于大家对本题的理解。** 看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目: @@ -21,13 +22,15 @@ * [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/) * [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/) +## 思路 + 为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢? 我们在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中提到了,**递归的实现就是:每一次递归调用都会把函数的局部变量、参数值和返回地址等压入调用栈中**,然后递归返回的时候,从栈顶弹出上一次递归的各项参数,所以这就是递归为什么可以返回上一层位置的原因。 此时大家应该知道我们用栈也可以是实现二叉树的前后中序遍历了。 -## 前序遍历(迭代法) +### 前序遍历(迭代法) 我们先看一下前序遍历。 @@ -69,7 +72,7 @@ public: 但接下来,**再用迭代法写中序遍历的时候,会发现套路又不一样了,目前的前序遍历的逻辑无法直接应用到中序遍历上。** -## 中序遍历(迭代法) +### 中序遍历(迭代法) 为了解释清楚,我说明一下 刚刚在迭代的过程中,其实我们有两个操作: @@ -112,7 +115,7 @@ public: ``` -## 后序遍历(迭代法) +### 后序遍历(迭代法) 再来看后序遍历,先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: @@ -142,7 +145,7 @@ public: ``` -# 总结 +## 总结 此时我们用迭代法写出了二叉树的前后中序遍历,大家可以看出前序和中序是完全两种代码风格,并不像递归写法那样代码稍做调整,就可以实现前后中序。 @@ -155,11 +158,9 @@ public: 当然可以,这种写法,还不是很好理解,我们将在下一篇文章里重点讲解,敬请期待! +## 其他语言版本 - -# 其他语言版本 - -Java: +### Java: ```java // 前序遍历顺序:中-左-右,入栈顺序:中-右-左 @@ -233,10 +234,7 @@ class Solution { } ``` - - - -Python: +### Python: ```python # 前序遍历-迭代-LC144_二叉树的前序遍历 @@ -281,7 +279,7 @@ class Solution: # 取栈顶元素右结点 cur = cur.right return result - ``` +``` ```python # 后序遍历-迭代-LC145_二叉树的后序遍历 @@ -303,10 +301,9 @@ class Solution: stack.append(node.right) # 将最终的数组翻转 return result[::-1] -``` - + ``` -Go: +### Go: > 迭代法前序遍历 @@ -400,7 +397,7 @@ func inorderTraversal(root *TreeNode) []int { } ``` -javaScript: +### JavaScript: ```js @@ -464,7 +461,7 @@ var postorderTraversal = function(root, res = []) { }; ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历(迭代法) @@ -519,7 +516,7 @@ function postorderTraversal(root: TreeNode | null): number[] { }; ``` -Swift: +### Swift: ```swift // 前序遍历迭代法 @@ -578,7 +575,8 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] { return result } ``` -Scala: +### Scala: + ```scala // 前序遍历(迭代法) object Solution { @@ -645,7 +643,7 @@ object Solution { } ``` -rust: +### Rust: ```rust use std::cell::RefCell; From cc510357c1fe6f0cf25fd4e800c2a89b03b4659f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 16:22:00 +0800 Subject: [PATCH 0566/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\350\277\255\344\273\243\346\263\225.md" | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 274642696c..8089af64ec 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -5,10 +5,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+> 统一写法是一种什么感觉 # 二叉树的统一迭代法 -> 统一写法是一种什么感觉 +## 思路 此时我们在[二叉树:一入递归深似海,从此offer是路人](https://programmercarl.com/二叉树的递归遍历.html)中用递归的方式,实现了二叉树前中后序的遍历。 @@ -28,7 +29,7 @@ 如何标记呢,**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法也可以叫做标记法。 -## 迭代法中序遍历 +### 迭代法中序遍历 中序遍历代码如下:(详细注释) @@ -71,7 +72,7 @@ public: 此时我们再来看前序遍历代码。 -## 迭代法前序遍历 +### 迭代法前序遍历 迭代法前序遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -102,7 +103,7 @@ public: }; ``` -## 迭代法后序遍历 +### 迭代法后序遍历 后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -143,15 +144,11 @@ public: 所以大家根据自己的个人喜好,对于二叉树的前中后序遍历,选择一种自己容易理解的递归和迭代法。 +## 其他语言版本 - - - -# 其他语言版本 - - -Java: +### Java: 迭代法前序遍历代码如下: + ```java class Solution { public List preorderTraversal(TreeNode root) { @@ -235,7 +232,7 @@ class Solution { } ``` -Python: +### Python: 迭代法前序遍历: ```python @@ -309,7 +306,8 @@ class Solution: return result ``` -Go: +### Go: + > 前序遍历统一迭代法 ```GO @@ -442,7 +440,7 @@ func postorderTraversal(root *TreeNode) []int { } ``` -javaScript: +### JavaScript: > 前序遍历统一迭代法 @@ -522,7 +520,7 @@ var postorderTraversal = function(root, res = []) { ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历(迭代法) @@ -591,7 +589,8 @@ function postorderTraversal(root: TreeNode | null): number[] { return res; }; ``` -Scala: +### Scala: + ```scala // 前序遍历 object Solution { @@ -667,7 +666,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution{ @@ -747,3 +746,4 @@ impl Solution{ + From 8e34d5ff5749229eeaa7dce33e1ed74c0dd992a2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 19:17:38 +0800 Subject: [PATCH 0567/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=20=E6=8E=92=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 --- ...02\345\272\217\351\201\215\345\216\206.md" | 284 +++++++++--------- 1 file changed, 149 insertions(+), 135 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index c2ad950814..ce9a247c1f 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -8,21 +8,23 @@ # 二叉树层序遍历登场! -《代码随想录》算法视频公开课:[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 学会二叉树的层序遍历,可以一口气打完以下十题: -* 102.二叉树的层序遍历 -* 107.二叉树的层次遍历II -* 199.二叉树的右视图 -* 637.二叉树的层平均值 -* 429.N叉树的层序遍历 -* 515.在每个树行中找最大值 -* 116.填充每个节点的下一个右侧节点指针 -* 117.填充每个节点的下一个右侧节点指针II -* 104.二叉树的最大深度 -* 111.二叉树的最小深度 +* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) +* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) +* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) +* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) +* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) +* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) @@ -31,7 +33,7 @@ -# 102.二叉树的层序遍历 +## 102.二叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/) @@ -39,7 +41,7 @@ ![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png) -思路: +### 思路 我们之前讲过了三篇关于二叉树的深度优先遍历的文章: @@ -63,7 +65,7 @@ 代码如下:**这份代码也可以作为二叉树层序遍历的模板,打十个就靠它了**。 -C++代码: +c++代码如下: ```CPP class Solution { @@ -111,7 +113,9 @@ public: }; ``` -java: +### 其他语言版本 + +#### Java: ```Java // 102.二叉树的层序遍历 @@ -167,7 +171,7 @@ class Solution { } ``` -python3代码: +#### Python: ```python @@ -219,12 +223,9 @@ class Solution: self.helper(node.left, level + 1, levels) self.helper(node.right, level + 1, levels) - ``` - - -go: +#### Go: ```go /** @@ -320,7 +321,7 @@ func levelOrder(root *TreeNode) (res [][]int) { } ``` -javascript代码: +#### Javascript: ```javascript var levelOrder = function(root) { @@ -350,7 +351,7 @@ var levelOrder = function(root) { ``` -TypeScript: +#### TypeScript: ```typescript function levelOrder(root: TreeNode | null): number[][] { @@ -377,7 +378,7 @@ function levelOrder(root: TreeNode | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrder(_ root: TreeNode?) -> [[Int]] { @@ -403,7 +404,7 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] { } ``` -Scala: +#### Scala: ```scala // 102.二叉树的层序遍历 @@ -430,7 +431,7 @@ object Solution { } ``` -Rust: +#### Rust: ```rust use std::cell::RefCell; @@ -466,7 +467,7 @@ impl Solution { **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** -# 107.二叉树的层次遍历 II +## 107.二叉树的层次遍历 II [力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) @@ -474,7 +475,7 @@ impl Solution { ![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png) -思路: +### 思路 相对于102.二叉树的层序遍历,就是最后把result数组反转一下就可以了。 @@ -506,7 +507,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python class Solution: @@ -537,7 +540,7 @@ class Solution: return result[::-1] ``` -Java: +#### Java: ```java // 107. 二叉树的层序遍历 II @@ -618,12 +621,10 @@ class Solution { return ans; } -} -``` - +``` -go: +#### Go: ```GO /** @@ -662,7 +663,7 @@ func levelOrderBottom(root *TreeNode) [][]int { } ``` -javascript代码 +#### Javascript: ```javascript var levelOrderBottom = function(root) { @@ -688,7 +689,7 @@ var levelOrderBottom = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function levelOrderBottom(root: TreeNode | null): number[][] { @@ -711,7 +712,7 @@ function levelOrderBottom(root: TreeNode | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { @@ -737,8 +738,7 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { } ``` - -Scala: +#### Scala: ```scala // 107.二叉树的层次遍历II @@ -764,7 +764,10 @@ object Solution { res.reverse.toList } -Rust: + +``` + +#### Rust: ```rust use std::cell::RefCell; @@ -796,7 +799,7 @@ impl Solution { } ``` -# 199.二叉树的右视图 +## 199.二叉树的右视图 [力扣题目链接](https://leetcode.cn/problems/binary-tree-right-side-view/) @@ -804,7 +807,7 @@ impl Solution { ![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png) -思路: +### 思路 层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。 @@ -832,7 +835,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python # Definition for a binary tree node. @@ -866,8 +871,7 @@ class Solution: return right_view ``` - -Java: +#### Java: ```java // 199.二叉树的右视图 @@ -911,7 +915,7 @@ public class N0199 { } ``` -go: +#### Go: ```GO /** @@ -945,8 +949,7 @@ func rightSideView(root *TreeNode) []int { } ``` - -javascript代码: +#### Javascript: ```javascript var rightSideView = function(root) { @@ -972,7 +975,7 @@ var rightSideView = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function rightSideView(root: TreeNode | null): number[] { @@ -992,7 +995,7 @@ function rightSideView(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func rightSideView(_ root: TreeNode?) -> [Int] { @@ -1017,7 +1020,7 @@ func rightSideView(_ root: TreeNode?) -> [Int] { } ``` -Scala: +#### Scala: ```scala // 199.二叉树的右视图 @@ -1043,7 +1046,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -1076,7 +1079,7 @@ impl Solution { } ``` -# 637.二叉树的层平均值 +## 637.二叉树的层平均值 [力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) @@ -1084,7 +1087,7 @@ impl Solution { ![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png) -思路: +### 思路 本题就是层序遍历的时候把一层求个总和在取一个均值。 @@ -1115,7 +1118,9 @@ public: ``` -python代码: +### 其他语言版本 + +#### Python: ```python class Solution: @@ -1155,7 +1160,7 @@ class Solution: return averages ``` -java: +#### Java: ```java // 637. 二叉树的层平均值 @@ -1197,7 +1202,7 @@ public class N0637 { } ``` -go: +#### Go: ```GO /** @@ -1235,7 +1240,7 @@ func averageOfLevels(root *TreeNode) []float64 { } ``` -javascript代码: +#### Javascript: ```javascript var averageOfLevels = function(root) { @@ -1262,7 +1267,7 @@ var averageOfLevels = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function averageOfLevels(root: TreeNode | null): number[] { @@ -1286,7 +1291,7 @@ function averageOfLevels(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func averageOfLevels(_ root: TreeNode?) -> [Double] { @@ -1313,7 +1318,7 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] { } ``` -Scala: +#### Scala: ```scala // 637.二叉树的层平均值 @@ -1339,7 +1344,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -1372,7 +1377,7 @@ impl Solution { } ``` -# 429.N叉树的层序遍历 +## 429.N叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) @@ -1390,8 +1395,7 @@ impl Solution { [5,6] ] - -思路: +### 思路 这道题依旧是模板题,只不过一个节点有多个孩子了 @@ -1423,7 +1427,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python """ @@ -1475,7 +1481,7 @@ class Solution: return result ``` -java: +#### Java: ```java // 429. N 叉树的层序遍历 @@ -1535,8 +1541,7 @@ public class N0429 { } ``` - -go: +#### Go: ```GO /** @@ -1567,7 +1572,7 @@ func levelOrder(root *Node) [][]int { } ``` -JavaScript代码: +#### JavaScript: ```JavaScript var levelOrder = function(root) { @@ -1596,7 +1601,7 @@ var levelOrder = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function levelOrder(root: Node | null): number[][] { @@ -1618,7 +1623,7 @@ function levelOrder(root: Node | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrder(_ root: Node?) -> [[Int]] { @@ -1643,7 +1648,7 @@ func levelOrder(_ root: Node?) -> [[Int]] { } ``` -Scala: +#### Scala: ```scala // 429.N叉树的层序遍历 @@ -1672,7 +1677,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust pub struct Solution; @@ -1720,7 +1725,7 @@ impl Solution { } ``` -# 515.在每个树行中找最大值 +## 515.在每个树行中找最大值 [力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) @@ -1728,7 +1733,7 @@ impl Solution { ![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png) -思路: +### 思路 层序遍历,取每一层的最大值 @@ -1758,7 +1763,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python # Definition for a binary tree node. @@ -1794,7 +1801,7 @@ class Solution: return result ``` -java代码: +#### Java: ```java class Solution { @@ -1820,7 +1827,7 @@ class Solution { } ``` -go: +#### Go: ```GO /** @@ -1864,7 +1871,7 @@ func max(x, y int) int { } ``` -javascript代码: +#### Javascript: ```javascript var largestValues = function(root) { @@ -1890,7 +1897,7 @@ var largestValues = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function largestValues(root: TreeNode | null): number[] { @@ -1916,7 +1923,7 @@ function largestValues(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func largestValues(_ root: TreeNode?) -> [Int] { @@ -1943,7 +1950,7 @@ func largestValues(_ root: TreeNode?) -> [Int] { } ``` -Scala: +#### Scala: ```scala // 515.在每个树行中找最大值 @@ -1970,7 +1977,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -2002,7 +2009,7 @@ impl Solution { } ``` -# 116.填充每个节点的下一个右侧节点指针 +## 116.填充每个节点的下一个右侧节点指针 [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) @@ -2024,7 +2031,7 @@ struct Node { ![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg) -思路: +### 思路 本题依然是层序遍历,只不过在单层遍历的时候记录一下本层的头部节点,然后在遍历的时候让前一个节点指向本节点就可以了 @@ -2063,7 +2070,9 @@ public: }; ``` -java代码: +### 其他语言版本 + +#### Java: ```java class Solution { @@ -2093,7 +2102,7 @@ class Solution { } ``` -python代码: +#### Python: ```python """ @@ -2133,7 +2142,7 @@ class Solution: return root ``` -go: +#### Go: ```GO /** @@ -2173,7 +2182,7 @@ func connect(root *Node) *Node { ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2209,7 +2218,7 @@ var connect = function(root) { ``` -TypeScript: +#### TypeScript: ```typescript function connect(root: Node | null): Node | null { @@ -2234,7 +2243,7 @@ function connect(root: Node | null): Node | null { }; ``` -Swift: +#### Swift: ```swift func connect(_ root: Node?) -> Node? { @@ -2266,7 +2275,7 @@ func connect(_ root: Node?) -> Node? { } ``` -Scala: +#### Scala: ```scala // 116.填充每个节点的下一个右侧节点指针 @@ -2297,11 +2306,11 @@ object Solution { } ``` -# 117.填充每个节点的下一个右侧节点指针II +## 117.填充每个节点的下一个右侧节点指针II [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) -思路: +### 思路 这道题目说是二叉树,但116题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道 @@ -2339,7 +2348,9 @@ public: }; ``` -Java 代码: +### 其他语言版本 + +#### Java: ```java // 二叉树之层次遍历 @@ -2377,7 +2388,7 @@ class Solution { } ``` -python代码: +#### Python: ```python # 层序遍历解法 @@ -2420,7 +2431,7 @@ class Solution: ``` -go: +#### Go: ```GO /** @@ -2459,7 +2470,7 @@ func connect(root *Node) *Node { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2494,7 +2505,7 @@ var connect = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function connect(root: Node | null): Node | null { @@ -2519,7 +2530,7 @@ function connect(root: Node | null): Node | null { }; ``` -Swift: +#### Swift: ```swift func connect(_ root: Node?) -> Node? { @@ -2551,7 +2562,7 @@ func connect(_ root: Node?) -> Node? { } ``` -Scala: +#### Scala: ```scala // 117.填充每个节点的下一个右侧节点指针II @@ -2582,7 +2593,7 @@ object Solution { } ``` -# 104.二叉树的最大深度 +## 104.二叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) @@ -2600,7 +2611,7 @@ object Solution { 返回它的最大深度 3 。 -思路: +### 思路 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。 @@ -2635,7 +2646,9 @@ public: }; ``` -Java: +### 其他语言版本 + +#### Java: ```Java class Solution { @@ -2661,7 +2674,7 @@ class Solution { } ``` -Python: +#### Python: ```python 3 # Definition for a binary tree node. @@ -2691,7 +2704,7 @@ class Solution: ``` -Go: +#### Go: ```go /** @@ -2726,7 +2739,7 @@ func maxDepth(root *TreeNode) int { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2759,7 +2772,7 @@ var maxDepth = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function maxDepth(root: TreeNode | null): number { @@ -2779,7 +2792,7 @@ function maxDepth(root: TreeNode | null): number { }; ``` -Swift: +#### Swift: ```swift func maxDepth(_ root: TreeNode?) -> Int { @@ -2804,7 +2817,7 @@ func maxDepth(_ root: TreeNode?) -> Int { } ``` -Scala: +#### Scala: ```scala // 104.二叉树的最大深度 @@ -2829,7 +2842,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -2859,10 +2872,12 @@ impl Solution { } ``` -# 111.二叉树的最小深度 +## 111.二叉树的最小深度 [力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) +### 思路 + 相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。 **需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点** @@ -2895,7 +2910,9 @@ public: }; ``` -Java: +### 其他语言版本 + +#### Java: ```java class Solution { @@ -2925,9 +2942,7 @@ class Solution { } ``` - - -Python 3: +#### Python: ```python 3 # Definition for a binary tree node. @@ -2960,7 +2975,7 @@ class Solution: return depth ``` -Go: +#### Go: ```go /** @@ -2999,7 +3014,7 @@ func minDepth(root *TreeNode) int { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -3035,7 +3050,7 @@ var minDepth = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function minDepth(root: TreeNode | null): number { @@ -3056,7 +3071,7 @@ function minDepth(root: TreeNode | null): number { }; ``` -Swift: +#### Swift: ```swift func minDepth(_ root: TreeNode?) -> Int { @@ -3082,7 +3097,7 @@ func minDepth(_ root: TreeNode?) -> Int { } ``` -Scala: +#### Scala: ```scala // 111.二叉树的最小深度 @@ -3108,7 +3123,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -3141,28 +3156,27 @@ impl Solution { } ``` -# 总结 +## 总结 二叉树的层序遍历,**就是图论中的广度优先搜索在二叉树中的应用**,需要借助队列来实现(此时又发现队列的一个应用了)。 来吧,一口气打十个: -* 102.二叉树的层序遍历 -* 107.二叉树的层次遍历II -* 199.二叉树的右视图 -* 637.二叉树的层平均值 -* 429.N叉树的层序遍历 -* 515.在每个树行中找最大值 -* 116.填充每个节点的下一个右侧节点指针 -* 117.填充每个节点的下一个右侧节点指针II -* 104.二叉树的最大深度 -* 111.二叉树的最小深度 +* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) +* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) +* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) +* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) +* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) +* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) **致敬叶师傅!** - -

+ From 1abe3506ea6108a14d32b3176bb44674212cd9b1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 14:19:58 +0800 Subject: [PATCH 0568/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200226.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...54\344\272\214\345\217\211\346\240\221.md" | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index b3aea9ed29..1151778364 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -16,7 +16,11 @@ 这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,权当一个乐子哈) -# 题外话 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 题外话 这道题目是非常经典的题目,也是比较简单的题目(至少一看就会)。 @@ -24,9 +28,7 @@ 如果做过这道题的同学也建议认真看完,相信一定有所收获! -# 思路 - -《代码随想录》算法视频公开课:[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 思路 我们之前介绍的都是各种方式遍历二叉树,这次要翻转了,感觉还是有点懵逼。 @@ -49,7 +51,7 @@ 那么层序遍历可以不可以呢?**依然可以的!只要把每一个节点的左右孩子翻转一下的遍历方式都是可以的!** -## 递归法 +### 递归法 对于二叉树的递归法的前中后序遍历,已经在[二叉树:前中后序递归遍历](https://programmercarl.com/二叉树的递归遍历.html)详细讲解了。 @@ -102,9 +104,9 @@ public: }; ``` -## 迭代法 +### 迭代法 -### 深度优先遍历 +#### 深度优先遍历 [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)中给出了前中后序迭代方式的写法,所以本题可以很轻松的写出如下迭代法的代码: @@ -163,7 +165,7 @@ public: 如果上面这个代码看不懂,回顾一下文章[二叉树:前中后序迭代方式的统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。 -### 广度优先遍历 +#### 广度优先遍历 也就是层序遍历,层数遍历也是可以翻转这棵树的,因为层序遍历也可以把每个节点的左右孩子都翻转一遍,代码如下: @@ -259,7 +261,7 @@ public: ## 其他语言版本 -### Java +### Java: ```Java //DFS递归 class Solution { @@ -310,7 +312,7 @@ class Solution { } ``` -### Python +### Python: 递归法:前序遍历: ```python @@ -466,7 +468,7 @@ class Solution: ``` -### Go +### Go: 递归版本的前序遍历 ```Go @@ -575,7 +577,7 @@ func invertTree(root *TreeNode) *TreeNode { } ``` -### JavaScript +### JavaScript: 使用递归版本的前序遍历 ```javascript @@ -783,7 +785,7 @@ function invertTree(root: TreeNode | null): TreeNode | null { }; ``` -### C +### C: 递归法 ```c @@ -961,7 +963,7 @@ object Solution { } ``` -### rust +### Rust: ```rust impl Solution { @@ -991,7 +993,7 @@ impl Solution { } ``` -### C# +### C#: ```csharp //递归 @@ -1042,3 +1044,4 @@ public class Solution { + From 49a38691744fbce62c7735a722b917448ab3e4a6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 14:46:55 +0800 Subject: [PATCH 0569/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200101.=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...60\344\272\214\345\217\211\346\240\221.md" | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 4418c62dc4..36b39740d0 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -13,9 +13,11 @@ ![101. 对称二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144607387.png) -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf), 相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 **首先想清楚,判断对称二叉树要比较的是哪两个节点,要比较的可不是左右节点!** @@ -41,7 +43,7 @@ 那么我们先来看看递归法的代码应该怎么写。 -## 递归法 +### 递归法 递归三部曲 @@ -159,13 +161,13 @@ public: **所以建议大家做题的时候,一定要想清楚逻辑,每一步做什么。把题目所有情况想到位,相应的代码写出来之后,再去追求简洁代码的效果。** -## 迭代法 +### 迭代法 这道题目我们也可以使用迭代法,但要注意,这里的迭代法可不是前中后序的迭代写法,因为本题的本质是判断两个树是否是相互翻转的,其实已经不是所谓二叉树遍历的前中后序的关系了。 这里我们可以使用队列来比较两个树(根节点的左右子树)是否相互翻转,(**注意这不是层序遍历**) -### 使用队列 +#### 使用队列 通过队列来判断根节点的左子树和右子树的内侧和外侧是否相等,如动画所示: @@ -207,7 +209,7 @@ public: }; ``` -### 使用栈 +#### 使用栈 细心的话,其实可以发现,这个迭代法,其实是把左右两个子树要比较的元素顺序放进一个容器,然后成对成对的取出来进行比较,那么其实使用栈也是可以的。 @@ -254,12 +256,12 @@ public: 这两道题目基本和本题是一样的,只要稍加修改就可以AC。 -* 100.相同的树 -* 572.另一个树的子树 +* [100.相同的树](https://leetcode.cn/problems/same-tree/) +* [572.另一个树的子树](https://leetcode.cn/problems/subtree-of-another-tree/) -# 其他语言版本 +## 其他语言版本 -Java +### Java: ```Java /** @@ -364,7 +366,7 @@ Java ``` -Python +### Python: 递归法: ```python @@ -470,7 +472,8 @@ class Solution: return True ``` -Go +### Go: + ```go /** * Definition for a binary tree node. @@ -521,8 +524,7 @@ func isSymmetric(root *TreeNode) bool { } ``` - -JavaScript +### JavaScript: 递归判断是否为对称二叉树: ```javascript @@ -610,7 +612,7 @@ var isSymmetric = function(root) { }; ``` -TypeScript: +### TypeScript: > 递归法 @@ -679,7 +681,7 @@ function isSymmetric(root: TreeNode | null): boolean { }; ``` -Swift: +### Swift: > 递归 ```swift @@ -761,7 +763,7 @@ func isSymmetric3(_ root: TreeNode?) -> Bool { } ``` -Scala +### Scala: > 递归: ```scala @@ -835,7 +837,7 @@ object Solution { } ``` -## Rust +### Rust: 递归: ```rust @@ -900,3 +902,4 @@ impl Solution { + From 4e77ae645077ade4306bbb1ef4219bc4e41c7d29 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 14:57:25 +0800 Subject: [PATCH 0570/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20=E6=8E=92=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 --- ...00\345\244\247\346\267\261\345\272\246.md" | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 7130867bf9..0504437521 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -5,6 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 104.二叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) @@ -23,17 +24,19 @@ 返回它的最大深度 3 。 -# 思路 +## 算法公开课 -看完本篇可以一起做了如下两道题目: +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -* 104.二叉树的最大深度 -* 559.n叉树的最大深度 +## 思路 + +看完本篇可以一起做了如下两道题目: -《代码随想录》算法视频公开课:[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [559.n叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/) -## 递归法 +### 递归法 本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。 @@ -164,7 +167,7 @@ public: }; ``` -## 迭代法 +### 迭代法 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。 @@ -202,10 +205,11 @@ public: }; ``` - 那么我们可以顺便解决一下n叉树的最大深度问题 -# 559.n叉树的最大深度 +## 相关题目推荐 + +### 559.n叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/) @@ -219,11 +223,11 @@ public: 我们应返回其最大深度,3。 -思路: +### 思路 依然可以提供递归法和迭代法,来解决这个问题,思路是和二叉树思路一样的,直接给出代码如下: -## 递归法 +#### 递归法 c++代码: @@ -240,7 +244,7 @@ public: } }; ``` -## 迭代法 +#### 迭代法 依然是层序遍历,代码如下: @@ -267,11 +271,11 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## java +### Java: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```java class solution { @@ -344,7 +348,8 @@ class solution { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 + ```java class Solution { /*递归法,后序遍历求root节点的高度*/ @@ -391,9 +396,9 @@ class solution { } ``` -## python +### Python : -### 104.二叉树的最大深度 +104.二叉树的最大深度 递归法: ```python @@ -448,7 +453,7 @@ class Solution: ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 递归法: ```python @@ -522,9 +527,10 @@ class Solution: return max_depth ``` +### Go: + +104.二叉树的最大深度 -## go -### 104.二叉树的最大深度 ```go /** * definition for a binary tree node. @@ -574,7 +580,7 @@ func maxdepth(root *treenode) int { ``` -### 559. n叉树的最大深度 +559. n叉树的最大深度 ```go func maxDepth(root *Node) int { @@ -598,9 +604,9 @@ func maxDepth(root *Node) int { } ``` -## javascript +### Javascript : -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```javascript var maxdepth = function(root) { @@ -649,7 +655,7 @@ var maxDepth = function(root) { }; ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 N叉树的最大深度 递归写法 ```js @@ -683,9 +689,9 @@ var maxDepth = function(root) { }; ``` -## TypeScript +### TypeScript: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```typescript // 后续遍历(自下而上) @@ -728,7 +734,7 @@ function maxDepth(root: TreeNode | null): number { }; ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 ```typescript // 后续遍历(自下而上) @@ -756,9 +762,9 @@ function maxDepth(root: TreeNode | null): number { ``` -## C +### C: -### 104.二叉树的最大深度 +104.二叉树的最大深度 二叉树最大深度递归 ```c @@ -814,9 +820,9 @@ int maxDepth(struct TreeNode* root){ } ``` -## Swift +### Swift: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```swift // 递归 - 后序 @@ -856,7 +862,7 @@ func maxDepth(_ root: TreeNode?) -> Int { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 ```swift // 递归 @@ -893,9 +899,10 @@ func maxDepth1(_ root: Node?) -> Int { } ``` -## Scala +### Scala: + +104.二叉树的最大深度 -### 104.二叉树的最大深度 递归法: ```scala object Solution { @@ -934,7 +941,7 @@ object Solution { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 递归法: ```scala @@ -972,8 +979,8 @@ object Solution { } ``` -## rust -### 0104.二叉树的最大深度 +### Rust: +0104.二叉树的最大深度 递归: ```rust From 600a42730c360df82e626ce91e27efc00342b310 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:03:31 +0800 Subject: [PATCH 0571/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20=E6=8E=92=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 --- ...00\345\260\217\346\267\261\345\272\246.md" | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index a1fc8a9355..61f9beb785 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -26,9 +26,11 @@ 返回它的最小深度 2. -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 看完了这篇[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),再来看看如何求最小深度。 @@ -52,7 +54,7 @@ 什么是叶子节点,左右孩子都为空的节点才是叶子节点! -## 递归法 +### 递归法 来来来,一起递归三部曲: @@ -199,7 +201,7 @@ public: }; ``` -## 迭代法 +### 迭代法 相对于[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),本题还可以使用层序遍历的方式来解决,思路是一样的。 @@ -237,10 +239,10 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java: ```Java class Solution { @@ -300,7 +302,7 @@ class Solution { } ``` -## Python +### Python : 递归法(版本一) @@ -400,9 +402,7 @@ class Solution: return depth ``` - - -## Go +### Go: ```go /** @@ -463,7 +463,7 @@ func minDepth(root *TreeNode) int { ``` -## JavaScript +### JavaScript: 递归法: @@ -509,7 +509,7 @@ var minDepth = function(root) { }; ``` -## TypeScript +### TypeScript: > 递归法 @@ -547,7 +547,7 @@ function minDepth(root: TreeNode | null): number { }; ``` -## Swift +### Swift: > 递归 ```Swift @@ -594,7 +594,7 @@ func minDepth(_ root: TreeNode?) -> Int { ``` -## Scala +### Scala: 递归法: ```scala @@ -633,7 +633,8 @@ object Solution { } ``` -rust: +### Rust: + ```rust impl Solution { // 递归 From 40f0e737046592e9b32075f7dad43b305158f90b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:08:43 +0800 Subject: [PATCH 0572/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\347\202\271\344\270\252\346\225\260.md" | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index 795a6f3777..d54f9b85d2 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -29,14 +29,17 @@ * 0 <= Node.val <= 5 * 10^4 * 题目数据保证输入的树是 完全二叉树 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +## 思路 -《代码随想录》算法视频公开课:[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频在看本篇题解,更有助于大家对本题的理解。 本篇给出按照普通二叉树的求法以及利用完全二叉树性质的求法。 -## 普通二叉树 +### 普通二叉树 首先按照普通二叉树的逻辑来求。 @@ -44,7 +47,7 @@ 递归遍历的顺序依然是后序(左右中)。 -### 递归 +#### 递归 如果对求二叉树深度还不熟悉的话,看这篇:[二叉树:看看这些树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)。 @@ -112,7 +115,7 @@ public: **网上基本都是这个精简的代码版本,其实不建议大家照着这个来写,代码确实精简,但隐藏了一些内容,连遍历的顺序都看不出来,所以初学者建议学习版本一的代码,稳稳的打基础**。 -### 迭代法 +#### 迭代 如果对求二叉树层序遍历还不熟悉的话,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 @@ -142,7 +145,7 @@ public: * 时间复杂度:O(n) * 空间复杂度:O(n) -## 完全二叉树 +### 完全二叉树 以上方法都是按照普通二叉树来做的,对于完全二叉树特性不了解的同学可以看这篇 [关于二叉树,你该了解这些!](https://programmercarl.com/二叉树理论基础.html),这篇详细介绍了各种二叉树的特性。 @@ -249,9 +252,9 @@ public: * 时间复杂度:O(log n × log n) * 空间复杂度:O(log n) -# 其他语言版本 +## 其他语言版本 -## Java +### Java: ```java class Solution { // 通用递归解法 @@ -312,7 +315,7 @@ class Solution { } ``` -## Python +### Python: 递归法: ```python @@ -408,7 +411,7 @@ class Solution: # 利用完全二叉树特性 return 1+self.countNodes(root.left)+self.countNodes(root.right) ``` -## Go +### Go: 递归版本 @@ -488,9 +491,7 @@ func countNodes(root *TreeNode) int { } ``` - - -## JavaScript: +### JavaScript: 递归版本 ```javascript @@ -559,7 +560,7 @@ var countNodes = function(root) { }; ``` -## TypeScrpt: +### TypeScrpt: > 递归法 @@ -614,7 +615,7 @@ function countNodes(root: TreeNode | null): number { }; ``` -## C: +### C: 递归法 ```c @@ -690,7 +691,7 @@ int countNodes(struct TreeNode* root){ } ``` -## Swift: +### Swift: > 递归 ```swift @@ -758,7 +759,7 @@ func countNodes(_ root: TreeNode?) -> Int { } ``` -## Scala +### Scala: 递归: ```scala @@ -821,9 +822,9 @@ object Solution { } ``` -rust: +### Rust: -// 递归 +递归 ```rust use std::cell::RefCell; use std::rc::Rc; @@ -838,7 +839,7 @@ impl Solution { } ``` -// 迭代 +迭代 ```rust use std::rc::Rc; use std::cell::RefCell; From ff6ff4adb86b80bc748c34f5f1c679f08d9c1ec2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:12:28 +0800 Subject: [PATCH 0573/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...241\344\272\214\345\217\211\346\240\221.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index e10a612afd..c7df9c8f19 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -33,8 +33,9 @@ 返回 false 。 +## 算法公开课 -**《代码随想录》算法视频公开课:[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 题外话 @@ -357,7 +358,7 @@ public: ## 其他语言版本 -### Java +### Java: ```Java class Solution { @@ -498,7 +499,7 @@ class Solution { } ``` -### Python +### Python: 递归法: @@ -620,7 +621,7 @@ class Solution: height_map[real_node] = 1 + max(left, right) return True ``` -### Go +### Go: ```Go func isBalanced(root *TreeNode) bool { @@ -652,7 +653,7 @@ func max(a, b int) int { } ``` -### JavaScript +### JavaScript: 递归法: @@ -723,7 +724,7 @@ var isBalanced = function (root) { }; ``` -### TypeScript +### TypeScript: ```typescript // 递归法 @@ -741,7 +742,7 @@ function isBalanced(root: TreeNode | null): boolean { }; ``` -### C +### C: 递归法: @@ -876,7 +877,7 @@ func getHeight(_ root: TreeNode?) -> Int { } ``` -### rust +### Rust: 递归 @@ -912,3 +913,4 @@ impl Solution { + From 95b5bb195d5af819f553cafaede57fd4c990a96d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:18:37 +0800 Subject: [PATCH 0574/1533] =?UTF-8?q?0257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84=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 --- ...00\346\234\211\350\267\257\345\276\204.md" | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 061535072d..44c0fd85e3 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -18,9 +18,11 @@ 示例: ![257.二叉树的所有路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415161576.png) -# 思路 +## 算法公开课 -**《代码随想录》算法视频公开课:[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这道题目要求从根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。 @@ -32,7 +34,7 @@ 我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。** -## 递归 +### 递归 1. 递归函数参数以及返回值 @@ -305,7 +307,7 @@ public: **综合以上,第二种递归的代码虽然精简但把很多重要的点隐藏在了代码细节里,第一种递归写法虽然代码多一些,但是把每一个逻辑处理都完整的展现出来了。** -## 拓展 +### 拓展 这里讲解本题解的写法逻辑以及一些更具体的细节,下面的讲解中,涉及到C++语法特性,如果不是C++的录友,就可以不看了,避免越看越晕。 @@ -328,7 +330,7 @@ public: 所以,第一个代码版本中,我才使用 vector 类型的path,这样方便给大家演示代码中回溯的操作。 vector类型的path,不管 每次 路径收集的数字是几位数,总之一定是int,所以就一次 pop_back就可以。 -## 迭代法 +### 迭代法 至于非递归的方式,我们可以依然可以使用前序遍历的迭代方式来模拟遍历路径的过程,对该迭代方式不了解的同学,可以看文章[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。 @@ -368,7 +370,7 @@ public: ``` 当然,使用java的同学,可以直接定义一个成员变量为object的栈`Stack stack = new Stack<>();`,这样就不用定义两个栈了,都放到一个栈里就可以了。 -# 总结 +## 总结 **本文我们开始初步涉及到了回溯,很多同学过了这道题目,可能都不知道自己其实使用了回溯,回溯和递归都是相伴相生的。** @@ -380,13 +382,9 @@ public: 对于本题充分了解递归与回溯的过程之后,有精力的同学可以再去实现迭代法。 +## 其他语言版本 - - - -# 其他语言版本 - -## Java: +### Java: ```Java //解法一 @@ -492,9 +490,9 @@ class Solution { } ``` --- -## Python: - - +### Python: + + 递归法+回溯 ```Python # Definition for a binary tree node. @@ -552,7 +550,7 @@ class Solution: self.traversal(cur.right, path[:], result) ``` - + 递归法+隐形回溯(版本二) ```Python # Definition for a binary tree node. @@ -610,7 +608,7 @@ class Solution: --- -## Go: +### Go: 递归法: @@ -672,7 +670,7 @@ func binaryTreePaths(root *TreeNode) []string { ``` --- -## JavaScript: +### JavaScript: 递归法: @@ -725,7 +723,7 @@ var binaryTreePaths = function(root) { }; ``` -## TypeScript: +### TypeScript: > 递归法 @@ -779,7 +777,7 @@ function binaryTreePaths(root: TreeNode | null): string[] { }; ``` -## Swift: +### Swift: > 递归/回溯 ```swift @@ -846,7 +844,7 @@ func binaryTreePaths(_ root: TreeNode?) -> [String] { } ``` -## Scala: +### Scala: 递归: ```scala @@ -876,7 +874,7 @@ object Solution { } ``` -rust: +### Rust: ```rust // 递归 @@ -907,4 +905,3 @@ impl Solution { - From a9039c0d2dd2c625c98009998677b20089b46524 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:39:36 +0800 Subject: [PATCH 0575/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...66\345\255\220\344\271\213\345\222\214.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index aa2868df4e..c1ad602d5f 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -16,9 +16,9 @@ ![404.左叶子之和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151927654.png) -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -48,7 +48,7 @@ if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) } ``` -## 递归法 +### 递归法 递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。 @@ -131,11 +131,11 @@ public: return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); } }; -``` +``` 精简之后的代码其实看不出来用的是什么遍历方式了,对于算法初学者以上根据第一个版本来学习。 -## 迭代法 +### 迭代法 本题迭代法使用前中后序都是可以的,只要把左叶子节点统计出来,就可以了,那么参考文章 [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:迭代法统一写法](https://programmercarl.com/二叉树的统一迭代法.html)中的写法,可以写出一个前序遍历的迭代法。 @@ -177,7 +177,7 @@ public: ## 其他语言版本 -### Java +### Java: **递归** @@ -246,7 +246,7 @@ class Solution { ``` -### Python +### Python: 递归 ```python # Definition for a binary tree node. @@ -316,7 +316,7 @@ class Solution: ``` -### Go +### Go: **递归法** @@ -368,7 +368,7 @@ func sumOfLeftLeaves(root *TreeNode) int { ``` -### JavaScript +### JavaScript: **递归法** @@ -417,7 +417,7 @@ var sumOfLeftLeaves = function(root) { }; ``` -### TypeScript +### TypeScript: > 递归法 @@ -462,7 +462,7 @@ function sumOfLeftLeaves(root: TreeNode | null): number { }; ``` -### Swift +### Swift: **递归法** ```swift @@ -511,7 +511,7 @@ func sumOfLeftLeaves(_ root: TreeNode?) -> Int { } ``` -### C +### C: 递归法: ```c int sumOfLeftLeaves(struct TreeNode* root){ @@ -561,7 +561,7 @@ int sumOfLeftLeaves(struct TreeNode* root){ } ``` -### Scala +### Scala: **递归:** ```scala @@ -600,7 +600,7 @@ object Solution { } ``` -### Rust +### Rust: **递归** @@ -656,3 +656,4 @@ impl Solution { + From f3a370113058a82be88164ddeabf5443f82f62a0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:41:28 +0800 Subject: [PATCH 0576/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20513.=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC=20?= =?UTF-8?q?=E6=8E=92=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 --- ...246\344\270\213\350\247\222\347\232\204\345\200\274.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index 743b0df9be..7ef934cc88 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -20,9 +20,9 @@ ![513.找树左下角的值1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153017586.png) -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -614,7 +614,7 @@ object Solution { } ``` -### rust +### Rust **层序遍历** @@ -689,3 +689,4 @@ impl Solution { + From bf6c4e7f7b3c8f118960362ab735f8e4977e0e58 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:53:29 +0800 Subject: [PATCH 0577/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 39285a3bc1..240462b6ff 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -21,9 +21,9 @@ 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -32,8 +32,8 @@ 那么接下来我通过详细讲解如下两道题,来回答这个问题: -* 112.路径总和 -* 113.路径总和ii +* [112.路径总和](https://leetcode.cn/problems/path-sum/) +* [113.路径总和ii](https://leetcode.cn/problems/path-sum-ii/) 这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。 @@ -218,7 +218,9 @@ public: 如果大家完全理解了本题的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。 -# 113. 路径总和ii +## 相关题目推荐 + +### 113. 路径总和ii [力扣题目链接](https://leetcode.cn/problems/path-sum-ii/) @@ -232,7 +234,7 @@ public: ![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png) -## 思路 +### 思路 113.路径总和ii要遍历整个树,找到所有路径,**所以递归函数不要返回值!** @@ -289,7 +291,7 @@ public: 至于113. 路径总和ii 的迭代法我并没有写,用迭代方式记录所有路径比较麻烦,也没有必要,如果大家感兴趣的话,可以再深入研究研究。 -## 总结 +### 总结 本篇通过leetcode上112. 路径总和 和 113. 路径总和ii 详细的讲解了 递归函数什么时候需要返回值,什么不需要返回值。 @@ -300,11 +302,11 @@ public: -# 其他语言版本 +## 其他语言版本 -## java +### Java -### 0112.路径总和 +#### 0112.路径总和 ```java class solution { @@ -422,7 +424,7 @@ class solution { } ``` -### 0113.路径总和-ii +#### 0113.路径总和-ii ```java class solution { @@ -529,9 +531,9 @@ class Solution { } ``` -## python +### Python -### 0112.路径总和 +#### 0112.路径总和 (版本一) 递归 ```python @@ -618,7 +620,7 @@ class Solution: -### 0113.路径总和-ii +#### 0113.路径总和-ii (版本一) 递归 ```python @@ -719,9 +721,9 @@ class Solution: ``` -## go +### Go -### 112. 路径总和 +#### 112. 路径总和 ```go //递归法 @@ -746,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { } ``` -### 113. 路径总和 II +#### 113. 路径总和 II ```go /** @@ -786,9 +788,9 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { } ``` -## javascript +### Javascript -### 0112.路径总和 +#### 0112.路径总和 **递归** @@ -852,7 +854,7 @@ let hasPathSum = function(root, targetSum) { }; ``` -### 0113.路径总和-ii +#### 0113.路径总和-ii **递归** @@ -950,9 +952,9 @@ let pathSum = function(root, targetSum) { }; ``` -## TypeScript +### TypeScript -### 0112.路径总和 +#### 0112.路径总和 **递归法:** @@ -1034,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { }; ``` -### 0112.路径总和 ii +#### 0112.路径总和 ii **递归法:** @@ -1070,9 +1072,9 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] { }; ``` -## Swift +### Swift -### 0112.路径总和 +#### 0112.路径总和 **递归** @@ -1141,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { } ``` -### 0113.路径总和 II +#### 0113.路径总和 II **递归** @@ -1192,10 +1194,10 @@ func traversal(_ cur: TreeNode?, count: Int) { } ``` -## C +### C -> 0112.路径总和 -> 递归法: +#### 0112.路径总和 +递归法: ```c bool hasPathSum(struct TreeNode* root, int targetSum){ @@ -1252,7 +1254,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ } ``` -> 0113.路径总和 II +#### 0113.路径总和 II ```c int** ret; @@ -1317,9 +1319,9 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur } ``` -## Scala +### Scala -### 0112.路径总和 +#### 0112.路径总和 **递归:** @@ -1369,7 +1371,7 @@ object Solution { } ``` -### 0113.路径总和 II +#### 0113.路径总和 II **递归:** @@ -1405,9 +1407,9 @@ object Solution { } ``` -## rust +### Rust -### 112.路径总和.md +#### 0112.路径总和 递归: @@ -1461,7 +1463,7 @@ impl Solution { } ``` -### 113.路径总和-ii +#### 0113.路径总和-ii ```rust impl Solution { @@ -1514,3 +1516,4 @@ impl Solution { + From fe9323900758c0f056299728d1551ab57d1efab3 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 18:00:07 +0800 Subject: [PATCH 0578/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200106.=E4=BB=8E?= =?UTF-8?q?=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=20=E6=8E=92=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 --- ...40\344\272\214\345\217\211\346\240\221.md" | 40 +++++++++---------- ...57\345\276\204\346\200\273\345\222\214.md" | 38 +++++++++--------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index a0bab99974..0144fc5c05 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -29,9 +29,9 @@ ![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png) -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder); 完整代码如下: -### C++完整代码 - ```CPP class Solution { private: @@ -281,8 +279,6 @@ public: 下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割) -### C++优化版本 - ```CPP class Solution { private: @@ -400,8 +396,9 @@ public: }; ``` +## 相关题目推荐 -# 105.从前序与中序遍历序列构造二叉树 +### 105.从前序与中序遍历序列构造二叉树 [力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) @@ -418,7 +415,7 @@ public: ![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png) -## 思路 +### 思路 本题和106是一样的道理。 @@ -547,7 +544,7 @@ public: }; ``` -# 思考题 +## 思考题 前序和中序可以唯一确定一棵二叉树。 @@ -569,7 +566,7 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 所以前序和后序不能唯一确定一棵二叉树! -# 总结 +## 总结 之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。 @@ -585,9 +582,9 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 106.从中序与后序遍历序列构造二叉树 @@ -688,7 +685,7 @@ class Solution { } ``` -## Python +### Python 105.从前序与中序遍历序列构造二叉树 @@ -754,7 +751,7 @@ class Solution: return root ``` -## Go +### Go 106 从中序与后序遍历序列构造二叉树 @@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode { ``` - - -## JavaScript +### JavaScript ```javascript var buildTree = function(inorder, postorder) { @@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) { }; ``` -## TypeScript +### TypeScript > 106.从中序与后序遍历序列构造二叉树 @@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null { }; ``` -## C +### C 106 从中序与后序遍历序列构造二叉树 @@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in } ``` -## Swift +### Swift 105 从前序与中序遍历序列构造二叉树 @@ -1140,7 +1135,7 @@ class Solution_0106 { } ``` -## Scala +### Scala 106 从中序与后序遍历序列构造二叉树 @@ -1188,7 +1183,7 @@ object Solution { } ``` -## rust +### Rust 106 从中序与后序遍历序列构造二叉树 @@ -1238,3 +1233,4 @@ impl Solution { + diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 240462b6ff..be03f719e1 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -306,7 +306,7 @@ public: ### Java -#### 0112.路径总和 +0112.路径总和 ```java class solution { @@ -424,7 +424,7 @@ class solution { } ``` -#### 0113.路径总和-ii +0113.路径总和-ii ```java class solution { @@ -533,7 +533,7 @@ class Solution { ### Python -#### 0112.路径总和 +0112.路径总和 (版本一) 递归 ```python @@ -620,7 +620,7 @@ class Solution: -#### 0113.路径总和-ii +0113.路径总和-ii (版本一) 递归 ```python @@ -723,7 +723,7 @@ class Solution: ``` ### Go -#### 112. 路径总和 +112. 路径总和 ```go //递归法 @@ -748,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { } ``` -#### 113. 路径总和 II +113. 路径总和 II ```go /** @@ -790,7 +790,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { ### Javascript -#### 0112.路径总和 +0112.路径总和 **递归** @@ -854,7 +854,7 @@ let hasPathSum = function(root, targetSum) { }; ``` -#### 0113.路径总和-ii +0113.路径总和-ii **递归** @@ -954,7 +954,7 @@ let pathSum = function(root, targetSum) { ### TypeScript -#### 0112.路径总和 +0112.路径总和 **递归法:** @@ -1036,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { }; ``` -#### 0112.路径总和 ii +0112.路径总和 ii **递归法:** @@ -1074,7 +1074,7 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] { ### Swift -#### 0112.路径总和 +0112.路径总和 **递归** @@ -1143,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { } ``` -#### 0113.路径总和 II +0113.路径总和 II **递归** @@ -1196,7 +1196,8 @@ func traversal(_ cur: TreeNode?, count: Int) { ### C -#### 0112.路径总和 +0112.路径总和 + 递归法: ```c @@ -1254,7 +1255,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ } ``` -#### 0113.路径总和 II +0113.路径总和 II ```c int** ret; @@ -1321,7 +1322,7 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur ### Scala -#### 0112.路径总和 +0112.路径总和 **递归:** @@ -1371,7 +1372,7 @@ object Solution { } ``` -#### 0113.路径总和 II +0113.路径总和 II **递归:** @@ -1409,7 +1410,7 @@ object Solution { ### Rust -#### 0112.路径总和 +0112.路径总和 递归: @@ -1463,7 +1464,7 @@ impl Solution { } ``` -#### 0113.路径总和-ii +0113.路径总和-ii ```rust impl Solution { @@ -1516,4 +1517,3 @@ impl Solution { - From b4eefe985945fdeaf4e1e34ebbc90b74258d43e2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 18:54:16 +0800 Subject: [PATCH 0579/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...200\345\244\247\344\272\214\345\217\211\346\240\221.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 33a9176ed1..77d7980f3d 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -25,9 +25,9 @@ 给定的数组的大小在 [1, 1000] 之间。 -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -310,7 +310,7 @@ class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: return self.traversal(nums, 0, len(nums)) - ``` +``` (版本三) 使用切片 @@ -587,3 +587,4 @@ impl Solution { + From eadc704c38816cfdb7f21f22d3489dc38ea5aecc Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:18:53 +0800 Subject: [PATCH 0580/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200617.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...0\345\271\266\344\272\214\345\217\211\346\240\221.md" | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 18a245c4db..44092aaebc 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -19,9 +19,9 @@ 注意: 合并必须从两个树的根节点开始。 -# 视频讲解 +# 算法公开课 -**《代码随想录》算法视频公开课:[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -164,7 +164,7 @@ public: }; ``` -## 迭代法 +### 迭代法 使用迭代法,如何同时处理两棵树呢? @@ -716,7 +716,7 @@ object Solution { } ``` -### rust +### Rust 递归: @@ -793,4 +793,3 @@ impl Solution { - From ac239bc437cfa8de6859fa8a98cc04b9cf6d1d4c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:20:25 +0800 Subject: [PATCH 0581/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200700.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...221\344\270\255\347\232\204\346\220\234\347\264\242.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 13064f9717..bc21bab872 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -18,9 +18,9 @@ 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -415,7 +415,7 @@ object Solution { } ``` -### rust +### Rust 递归: @@ -469,3 +469,4 @@ impl Solution { + From 7746734d79d4726143375aaf1a8a8f835483346e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:22:33 +0800 Subject: [PATCH 0582/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200098.=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20?= =?UTF-8?q?=E6=8E=92=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 --- ...11\346\220\234\347\264\242\346\240\221.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 95b657a58f..a48ec0656b 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -20,18 +20,18 @@ ![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png) -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。 有了这个特性,**验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。** -## 递归法 +### 递归法 可以递归中序遍历将二叉搜索树转变成一个数组,代码如下: @@ -211,7 +211,7 @@ public: 最后这份代码看上去整洁一些,思路也清晰。 -## 迭代法 +### 迭代法 可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html) @@ -245,7 +245,7 @@ public: 在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。 -# 总结 +## 总结 这道题目是一个简单题,但对于没接触过的同学还是有难度的。 @@ -254,10 +254,10 @@ public: 只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java //使用統一迭代法 @@ -369,7 +369,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一)利用中序递增性质,转换成数组 ```python @@ -479,7 +479,7 @@ class Solution: ``` -## Go +### Go ```Go func isValidBST(root *TreeNode) bool { @@ -526,7 +526,7 @@ func isValidBST(root *TreeNode) bool { } ``` -## JavaScript +### JavaScript 辅助数组解决 @@ -595,7 +595,7 @@ var isValidBST = function (root) { }; ``` -## TypeScript +### TypeScript > 辅助数组解决: @@ -637,7 +637,7 @@ function isValidBST(root: TreeNode | null): boolean { }; ``` -## Scala +### Scala 辅助数组解决: ```scala @@ -682,7 +682,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -735,3 +735,4 @@ impl Solution { + From 74bbca292355dbc8ce9316329bfea503234ef0d6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:26:08 +0800 Subject: [PATCH 0583/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\347\273\235\345\257\271\345\267\256.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 3e4391d62c..56911858dd 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -19,12 +19,12 @@ 提示:树中至少有 2 个节点。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 题目中要求在二叉搜索树上任意两节点的差的绝对值的最小值。 @@ -32,7 +32,7 @@ 遇到在二叉搜索树上求什么最值啊,差值之类的,就把它想成在一个有序数组上求最值,求差值,这样就简单多了。 -## 递归 +### 递归 那么二叉搜索树采用中序遍历,其实就是一个有序数组。 @@ -102,7 +102,7 @@ public: 是不是看上去也并不复杂! -## 迭代 +### 迭代 看过这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式的写法就不能统一一下么?](https://programmercarl.com/二叉树的统一迭代法.html)文章之后,不难写出两种中序遍历的迭代法。 @@ -135,7 +135,7 @@ public: }; ``` -# 总结 +## 总结 **遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。** @@ -145,10 +145,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归 ```java @@ -235,7 +235,7 @@ class Solution { } } ``` -## Python +### Python 递归法(版本一)利用中序递增,结合数组 ```python @@ -313,7 +313,7 @@ class Solution: ``` -## Go: +### Go 中序遍历,然后计算最小差值 ```go @@ -340,7 +340,7 @@ func getMinimumDifference(root *TreeNode) int { } ``` -## JavaScript +### JavaScript 递归 先转换为有序数组 ```javascript /** @@ -415,7 +415,7 @@ var getMinimumDifference = function(root) { } ``` -## TypeScript +### TypeScript > 辅助数组解决 @@ -482,7 +482,7 @@ function getMinimumDifference(root: TreeNode | null): number { }; ``` -## Scala +### Scala 构建二叉树的有序数组: @@ -561,7 +561,7 @@ object Solution { } ``` -## rust +### Rust 构建二叉树的有序数组: @@ -652,3 +652,4 @@ impl Solution { + From c1da5bb2339fbe525eaf1cc4f09702b1caefbc1d Mon Sep 17 00:00:00 2001 From: El nino Date: Sun, 23 Jul 2023 10:12:24 +0800 Subject: [PATCH 0584/1533] =?UTF-8?q?typo=EF=BC=9B=E4=B8=BA=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97=E6=B7=BB=E5=8A=A0=20cpp=20=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E9=AB=98=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index eb6a26fe6d..af4073f077 100644 --- "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -16,13 +16,13 @@ ## dfs 与 bfs 区别 -提到深度优先搜索(dfs),就不得不说和广度优先有什么区别(bfs) +提到深度优先搜索(dfs),就不得不说和广度优先搜索(bfs)有什么区别 先来了解dfs的过程,很多录友可能对dfs(深度优先搜索),bfs(广度优先搜索)分不清。 先给大家说一下两者大概的区别: -* dfs是可一个方向去搜,不到黄河不回头,直到遇到绝境了,搜不下去了,在换方向(换方向的过程就涉及到了回溯)。 +* dfs是可一个方向去搜,不到黄河不回头,直到遇到绝境了,搜不下去了,再换方向(换方向的过程就涉及到了回溯)。 * bfs是先把本节点所连接的所有节点遍历一遍,走到下一个节点的时候,再把连接节点的所有节点遍历一遍,搜索方向更像是广度,四面八方的搜索过程。 当然以上讲的是,大体可以这么理解,接下来 我们详细讲解dfs,(bfs在用单独一篇文章详细讲解) @@ -60,26 +60,26 @@ 上图演示中,其实我并没有把 所有的 从节点1 到节点6的dfs(深度优先搜索)的过程都画出来,那样太冗余了,但 已经把dfs 关键的地方都涉及到了,关键就两点: -* 搜索方向,是认准一个方向搜,直到碰壁之后在换方向 +* 搜索方向,是认准一个方向搜,直到碰壁之后再换方向 * 换方向是撤销原路径,改为节点链接的下一个路径,回溯的过程。 ## 代码框架 -正式因为dfs搜索可一个方向,并需要回溯,所以用递归的方式来实现是最方便的。 +正是因为dfs搜索可一个方向,并需要回溯,所以用递归的方式来实现是最方便的。 -很多录友对回溯很陌生,建议先看看码随想录,[回溯算法章节](https://programmercarl.com/回溯算法理论基础.html)。 +很多录友对回溯很陌生,建议先看看代码随想录,[回溯算法章节](https://programmercarl.com/回溯算法理论基础.html)。 有递归的地方就有回溯,那么回溯在哪里呢? 就地递归函数的下面,例如如下代码: -``` +```cpp void dfs(参数) { 处理节点 dfs(图,选择的节点); // 递归 回溯,撤销处理结果 } -``` +``` 可以看到回溯操作就在递归函数的下面,递归和回溯是相辅相成的。 @@ -89,7 +89,7 @@ void dfs(参数) { 我们在回顾一下[回溯法](https://programmercarl.com/回溯算法理论基础.html)的代码框架: -``` +```cpp void backtracking(参数) { if (终止条件) { 存放结果; @@ -102,11 +102,11 @@ void backtracking(参数) { } } -``` +``` 回溯算法,其实就是dfs的过程,这里给出dfs的代码框架: -``` +```cpp void dfs(参数) { if (终止条件) { 存放结果; @@ -136,9 +136,9 @@ void dfs(参数) { 1. 确认递归函数,参数 -``` +```cpp void dfs(参数) -``` +``` 通常我们递归的时候,我们递归搜索需要了解哪些参数,其实也可以在写递归函数的时候,发现需要什么参数,再去补充就可以。 @@ -146,7 +146,7 @@ void dfs(参数) 例如这样: -``` +```cpp vector> result; // 保存符合条件的所有路径 vector path; // 起点到终点的路径 void dfs (图,目前搜索的节点) @@ -158,7 +158,7 @@ void dfs (图,目前搜索的节点) 终止条件很重要,很多同学写dfs的时候,之所以容易死循环,栈溢出等等这些问题,都是因为终止条件没有想清楚。 -``` +```cpp if (终止条件) { 存放结果; return; @@ -173,7 +173,7 @@ if (终止条件) { 一般这里就是一个for循环的操作,去遍历 目前搜索节点 所能到的所有节点。 -``` +```cpp for (选择:本节点所连接的其他节点) { 处理节点; dfs(图,选择的节点); // 递归 From b68e881a659e4ef56446db940fbf12a6a6a09207 Mon Sep 17 00:00:00 2001 From: El nino Date: Sun, 23 Jul 2023 11:08:46 +0800 Subject: [PATCH 0585/1533] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd70d8cb6b..5fe165a71c 100644 --- a/README.md +++ b/README.md @@ -396,7 +396,7 @@ * [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md) * [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md) -* [图论:广度优先搜索理论基础](./problems/图论广索理论基础.md) +* [图论:广度优先搜索理论基础](./problems/图论广搜理论基础.md) * [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md) * [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md) * [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md) From eb9c4e647becea279c985e73fd2d4864c002b4a4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 17:29:22 +0800 Subject: [PATCH 0586/1533] =?UTF-8?q?Update=200392.=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index c10114c05a..8bb078bd5d 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -258,9 +258,25 @@ func isSubsequence(s string, t string) bool { } ``` - - - +Rust: + +```rust +impl Solution { + pub fn is_subsequence(s: String, t: String) -> bool { + let mut dp = vec![vec![0; t.len() + 1]; s.len() + 1]; + for (i, char_s) in s.chars().enumerate() { + for (j, char_t) in t.chars().enumerate() { + if char_s == char_t { + dp[i + 1][j + 1] = dp[i][j] + 1; + continue; + } + dp[i + 1][j + 1] = dp[i + 1][j] + } + } + dp[s.len()][t.len()] == s.len() + } +} +```

From 503c83db8e9323fe4a94bd824a2b19ff617780d2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 17:53:05 +0800 Subject: [PATCH 0587/1533] =?UTF-8?q?Update=200392.=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 8bb078bd5d..700d682510 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -278,6 +278,30 @@ impl Solution { } ``` +> 滚动数组 + +```rust +impl Solution { + pub fn is_subsequence(s: String, t: String) -> bool { + let mut dp = vec![0; t.len() + 1]; + let (s, t) = (s.as_bytes(), t.as_bytes()); + for &byte_s in s { + let mut pre = 0; + for j in 0..t.len() { + let temp = dp[j + 1]; + if byte_s == t[j] { + dp[j + 1] = pre + 1; + } else { + dp[j + 1] = dp[j]; + } + pre = temp; + } + } + dp[t.len()] == s.len() + } +} +``` +

From 55b85b5251c7b4f2060795070341eaf4d97347d0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 17:57:10 +0800 Subject: [PATCH 0588/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200501.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?= =?UTF-8?q?=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\344\274\227\346\225\260.md" | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 1da323435b..efbabc4ab6 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -33,20 +33,20 @@ 进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 这道题目呢,递归法我从两个维度来讲。 首先如果不是二叉搜索树的话,应该怎么解题,是二叉搜索树,又应该如何解题,两种方式做一个比较,可以加深大家对二叉树的理解。 -## 递归法 +### 递归法 -### 如果不是二叉搜索树 +#### 如果不是二叉搜索树 如果不是二叉搜索树,最直观的方法一定是把这个树都遍历了,用map统计频率,把频率排个序,最后取前面高频的元素的集合。 @@ -140,7 +140,7 @@ public: **所以如果本题没有说是二叉搜索树的话,那么就按照上面的思路写!** -### 是二叉搜索树 +#### 是二叉搜索树 **既然是搜索树,它中序遍历就是有序的**。 @@ -271,7 +271,7 @@ public: ``` -## 迭代法 +### 迭代法 只要把中序遍历转成迭代,中间节点的处理逻辑完全一样。 @@ -326,7 +326,7 @@ public: }; ``` -# 总结 +## 总结 本题在递归法中,我给出了如果是普通二叉树,应该怎么求众数。 @@ -345,10 +345,10 @@ public: > **需要强调的是 leetcode上的耗时统计是非常不准确的,看个大概就行,一样的代码耗时可以差百分之50以上**,所以leetcode的耗时统计别太当回事,知道理论上的效率优劣就行了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 暴力法 @@ -472,7 +472,7 @@ class Solution { } } ``` -統一迭代法 +统一迭代法 ```Java class Solution { public int[] findMode(TreeNode root) { @@ -526,7 +526,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一)利用字典 @@ -640,7 +640,7 @@ class Solution: return result ``` -## Go +### Go 计数法,不使用额外空间,利用二叉树性质,中序遍历 ```go @@ -676,7 +676,7 @@ func findMode(root *TreeNode) []int { } ``` -## JavaScript +### JavaScript 使用额外空间map的方法 ```javascript @@ -753,7 +753,7 @@ var findMode = function(root) { }; ``` -## TypeScript +### TypeScript > 辅助Map法 @@ -852,7 +852,7 @@ function findMode(root: TreeNode | null): number[] { }; ``` -## Scala +### Scala 暴力: ```scala @@ -923,7 +923,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -1015,3 +1015,4 @@ pub fn find_mode(root: Option>>) -> Vec { + From 3a2490b92fe00b5e2b6acb602f33328b48e426ad Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:41:57 +0800 Subject: [PATCH 0589/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200236.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.md" | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 0ebd556669..9db7409e04 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -34,12 +34,12 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉树中。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 遇到这个题目首先想的是要是能自底向上查找就好了,这样就可以找到公共祖先了。 @@ -226,7 +226,7 @@ public: }; ``` -# 总结 +## 总结 这道题目刷过的同学未必真正了解这里面回溯的过程,以及结果是如何一层一层传上去的。 @@ -243,10 +243,10 @@ public: 本题没有给出迭代法,因为迭代法不适合模拟回溯的过程。理解递归的解法就够了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -273,7 +273,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一) ```python class Solution: @@ -312,7 +312,7 @@ class Solution: return left ``` -## Go +### Go ```Go func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { @@ -343,7 +343,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { } ``` -## JavaScript +### JavaScript ```javascript var lowestCommonAncestor = function(root, p, q) { @@ -370,7 +370,7 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` -## TypeScript +### TypeScript ```typescript function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null { @@ -384,7 +384,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` -## Scala +### Scala ```scala object Solution { @@ -404,7 +404,7 @@ object Solution { } ``` -## rust +### Rust ```rust impl Solution { @@ -436,3 +436,4 @@ impl Solution { + From 07e1ccca1925d16b5f9f58050d135d3a326b44af Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:52:09 +0800 Subject: [PATCH 0590/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200235.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...54\345\205\261\347\245\226\345\205\210.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 9777bb0be8..2b8af06017 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -36,11 +36,11 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉搜索树中。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。 @@ -71,7 +71,7 @@ 可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回! -## 递归法 +### 递归法 递归三部曲如下: @@ -203,7 +203,7 @@ public: }; ``` -## 迭代法 +### 迭代法 对于二叉搜索树的迭代法,大家应该在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)就了解了。 @@ -229,7 +229,7 @@ public: 灵魂拷问:是不是又被简单的迭代法感动到痛哭流涕? -# 总结 +## 总结 对于二叉搜索树的最近祖先问题,其实要比[普通二叉树公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)简单的多。 @@ -238,10 +238,10 @@ public: 最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归法: ```java @@ -273,7 +273,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一) ```python @@ -326,7 +326,7 @@ class Solution: ``` -## Go +### Go 递归法: ```go @@ -350,7 +350,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { ``` -## JavaScript +### JavaScript 递归法: ```javascript @@ -391,7 +391,7 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` -## TypeScript +### TypeScript > 递归法: @@ -422,7 +422,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` -## Scala +### Scala 递归: @@ -453,7 +453,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -519,3 +519,4 @@ impl Solution { + From 70271161fff40fb3ab1929dcb37b19e3efe469b0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:54:12 +0800 Subject: [PATCH 0591/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\346\223\215\344\275\234.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index fc4351babc..511d161c70 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -23,11 +23,11 @@ * -10^8 <= val <= 10^8 * 新值和原始二叉搜索树中的任意节点值都不同 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 这道题目其实是一道简单题目,**但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人**,瞬间感觉题目复杂了很多。 @@ -43,7 +43,7 @@ 接下来就是遍历二叉搜索树的过程了。 -## 递归 +### 递归 递归三部曲: @@ -165,7 +165,7 @@ public: **网上千篇一律的代码,可能会误导大家认为通过递归函数返回节点 这样的写法是天经地义,其实这里是有优化的!** -## 迭代 +### 迭代 再来看看迭代法,对二叉搜索树迭代写法不熟悉,可以看这篇:[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html) @@ -198,7 +198,7 @@ public: }; ``` -# 总结 +## 总结 首先在二叉搜索树中的插入操作,大家不用恐惧其重构搜索树,其实根本不用重构。 @@ -207,9 +207,9 @@ public: 最后依然给出了迭代的方法,迭代的方法就需要记录当前遍历节点的父节点了,这个和没有返回值的递归函数实现的代码逻辑是一样的。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -254,7 +254,7 @@ class Solution { } ``` ----- -## Python +### Python 递归法(版本一) ```python @@ -371,7 +371,7 @@ class Solution: ``` ----- -## Go +### Go 递归法 @@ -417,7 +417,7 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { } ``` ----- -## JavaScript +### JavaScript 有返回值的递归写法 @@ -530,7 +530,7 @@ var insertIntoBST = function (root, val) { }; ``` -## TypeScript +### TypeScript > 递归-有返回值 @@ -595,7 +595,7 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { ``` -## Scala +### Scala 递归: @@ -632,7 +632,7 @@ object Solution { } ``` -### rust +### Rust 迭代: @@ -697,3 +697,4 @@ impl Solution { + From 0f22ada3df0468334a45059a037ec6a7fa198d85 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:56:18 +0800 Subject: [PATCH 0592/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200450.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 3d73598da9..13599fd849 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -24,15 +24,15 @@ ![450.删除二叉搜索树中的节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20201020171048265.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心理准备。 -## 递归 +### 递归 递归三部曲: @@ -161,7 +161,7 @@ public: }; ``` -## 普通二叉树的删除方式 +### 普通二叉树的删除方式 这里我在介绍一种通用的删除,普通二叉树的删除方式(没有使用搜索树的特性,遍历整棵树),用交换值的操作来删除目标节点。 @@ -198,7 +198,7 @@ public: 这个代码是简短一些,思路也巧妙,但是不太好想,实操性不强,推荐第一种写法! -## 迭代法 +### 迭代法 删除节点的迭代法还是复杂一些的,但其本质我在递归法里都介绍了,最关键就是删除节点的操作(动画模拟的过程) @@ -246,7 +246,7 @@ public: }; ``` -# 总结 +## 总结 读完本篇,大家会发现二叉搜索树删除节点比增加节点复杂的多。 @@ -264,10 +264,10 @@ public: 迭代法其实不太容易写出来,所以如果是初学者的话,彻底掌握第一种递归写法就够了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -323,7 +323,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一) ```python class Solution: @@ -411,7 +411,7 @@ class Solution: return root ``` -## Go +### Go ```Go // 递归版本 func deleteNode(root *TreeNode, key int) *TreeNode { @@ -497,7 +497,7 @@ func deleteNode(root *TreeNode, key int) *TreeNode { } ``` -## JavaScript +### JavaScript 递归 @@ -588,7 +588,7 @@ var deleteNode = function (root, key) { } ``` -## TypeScript +### TypeScript > 递归法: @@ -652,7 +652,7 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null { }; ``` -## Scala +### Scala ```scala object Solution { @@ -682,7 +682,7 @@ object Solution { } ``` -## rust +### Rust ```rust impl Solution { @@ -720,3 +720,4 @@ impl Solution { + From f0ac0b2efd588164b74632e75a6c4358e2357781 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 18:57:41 +0800 Subject: [PATCH 0593/1533] =?UTF-8?q?Update=200115.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\345\272\217\345\210\227.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 8c82880d58..56516132ba 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -311,7 +311,31 @@ function numDistinct(s: string, t: string): number { }; ``` +Rust: +```rust +impl Solution { + pub fn num_distinct(s: String, t: String) -> i32 { + if s.len() < t.len() { + return 0; + } + let mut dp = vec![vec![0; s.len() + 1]; t.len() + 1]; + // i = 0, t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素) + dp[0] = vec![1; s.len() + 1]; + for (i, char_t) in t.chars().enumerate() { + for (j, char_s) in s.chars().enumerate() { + if char_t == char_s { + // t 的前 i 个字符在 s 的前 j 个字符中作为子序列的个数 + dp[i + 1][j + 1] = dp[i][j] + dp[i + 1][j]; + continue; + } + dp[i + 1][j + 1] = dp[i + 1][j]; + } + } + dp[t.len()][s.len()] + } +} +```

From e14e2169c4c303d5e690705245230323774df795 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:58:10 +0800 Subject: [PATCH 0594/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200669.=E4=BF=AE?= =?UTF-8?q?=E5=89=AA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20?= =?UTF-8?q?=E6=8E=92=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 --- ...11\346\220\234\347\264\242\346\240\221.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 85922a1dd8..2cfbfefc60 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -20,17 +20,17 @@ ![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173219142.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 相信看到这道题目大家都感觉是一道简单题(事实上leetcode上也标明是简单)。 但还真的不简单! -## 递归法 +### 递归法 直接想法就是:递归处理,然后遇到 `root->val < low || root->val > high` 的时候直接return NULL,一波修改,赶紧利落。 @@ -188,7 +188,7 @@ public: 只看代码,其实不太好理解节点是如何移除的,这一块大家可以自己再模拟模拟! -## 迭代法 +### 迭代法 因为二叉搜索树的有序性,不需要使用栈模拟递归的过程。 @@ -233,7 +233,7 @@ public: }; ``` -# 总结 +## 总结 修剪二叉搜索树其实并不难,但在递归法中大家可看出我费了很大的功夫来讲解如何删除节点的,这个思路其实是比较绕的。 @@ -243,10 +243,10 @@ public: 本题我依然给出递归法和迭代法,初学者掌握递归就可以了,如果想进一步学习,就把迭代法也写一写。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java **递归** @@ -311,7 +311,7 @@ class Solution { ```` -## Python +### Python 递归法(版本一) ```python @@ -381,7 +381,7 @@ class Solution: ``` -## Go +### Go ```go // 递归 @@ -436,7 +436,7 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode { ``` -## JavaScript版本 +### JavaScript 迭代: @@ -492,7 +492,7 @@ var trimBST = function (root,low,high) { } ``` -## TypeScript +### TypeScript > 递归法 @@ -540,7 +540,7 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n }; ``` -## Scala +### Scala 递归法: @@ -557,7 +557,7 @@ object Solution { } ``` -## rust +### Rust // 递归 @@ -590,3 +590,4 @@ impl Solution { + From 572c08984f9ec9821383b3c9a51dbbf1684eb6af Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:00:08 +0800 Subject: [PATCH 0595/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200108.=E5=B0=86?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=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 --- ...11\346\220\234\347\264\242\346\240\221.md" | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 056ef3e268..e699005edb 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -20,11 +20,11 @@ ![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 做这道题目之前大家可以了解一下这几道: @@ -71,7 +71,7 @@ **这也是题目中强调答案不是唯一的原因。 理解这一点,这道题目算是理解到位了**。 -## 递归 +### 递归 递归三部曲: @@ -155,7 +155,7 @@ public: **注意:在调用traversal的时候传入的left和right为什么是0和nums.size() - 1,因为定义的区间为左闭右闭**。 -## 迭代法 +### 迭代法 迭代法可以通过三个队列来模拟,一个队列放遍历的节点,一个队列放左区间下标,一个队列放右区间下标。 @@ -203,7 +203,7 @@ public: }; ``` -# 总结 +## 总结 **在[二叉树:构造二叉树登场!](https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html) 和 [二叉树:构造一棵最大的二叉树](https://programmercarl.com/0654.最大二叉树.html)之后,我们顺理成章的应该构造一下二叉搜索树了,一不小心还是一棵平衡二叉搜索树**。 @@ -216,10 +216,10 @@ public: 最后依然给出迭代的方法,其实就是模拟取中间元素,然后不断分割去构造二叉树的过程。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归: 左闭右开 [left,right) ```Java @@ -315,7 +315,7 @@ class Solution { } ``` -## Python +### Python 递归法 ```python class Solution: @@ -377,7 +377,7 @@ class Solution: ``` -## Go +### Go 递归(隐含回溯) @@ -396,7 +396,7 @@ func sortedArrayToBST(nums []int) *TreeNode { } ``` -## JavaScript +### JavaScript 递归 ```javascript @@ -453,7 +453,7 @@ var sortedArrayToBST = function(nums) { return root; }; ``` -## TypeScript +### TypeScript ```typescript function sortedArrayToBST(nums: number[]): TreeNode | null { @@ -469,7 +469,7 @@ function sortedArrayToBST(nums: number[]): TreeNode | null { }; ``` -## C +### C 递归 ```c @@ -490,7 +490,7 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) { } ``` -## Scala +### Scala 递归: @@ -511,7 +511,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -536,3 +536,4 @@ impl Solution { + From 6bf34cd8ba3837d16013dd2ac78b5d632f3f2e22 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:02:05 +0800 Subject: [PATCH 0596/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200538.=E5=B0=86?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E7=B4=AF=E5=8A=A0=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...72\347\264\257\345\212\240\346\240\221.md" | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index 781763a4a5..c403c98f24 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -44,11 +44,11 @@ * 树中的所有值 互不相同 。 * 给定的树为二叉搜索树。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 一看到累加树,相信很多小伙伴都会疑惑:如何累加?遇到一个节点,然后再遍历其他节点累加?怎么一想这么麻烦呢。 @@ -64,7 +64,7 @@ 那么知道如何遍历这个二叉树,也就迎刃而解了,**从树中可以看出累加的顺序是右中左,所以我们需要反中序遍历这个二叉树,然后顺序累加就可以了**。 -## 递归 +### 递归 遍历顺序如图所示: @@ -131,7 +131,7 @@ public: }; ``` -## 迭代法 +### 迭代法 迭代法其实就是中序模板题了,在[二叉树:前中后序迭代法](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序统一方式迭代法](https://programmercarl.com/二叉树的统一迭代法.html)可以选一种自己习惯的写法。 @@ -166,17 +166,17 @@ public: }; ``` -# 总结 +## 总结 经历了前面各种二叉树增删改查的洗礼之后,这道题目应该比较简单了。 **好了,二叉树已经接近尾声了,接下来就是要对二叉树来一个大总结了**。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java **递归** ```Java @@ -237,7 +237,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一) ```python # Definition for a binary tree node. @@ -318,7 +318,7 @@ class Solution: self.traversal(root) return root -``` +``` 迭代法(版本二) ```python class Solution: @@ -338,9 +338,9 @@ class Solution: pre = cur.val cur =cur.left return root -``` +``` -## Go +### Go 弄一个sum暂存其和值 ```go @@ -362,7 +362,7 @@ func traversal(cur *TreeNode) { } ``` -## JavaScript +### JavaScript 递归 ```javascript @@ -401,7 +401,7 @@ var convertBST = function (root) { }; ``` -##C +### C 递归 ```c @@ -422,7 +422,7 @@ struct TreeNode* convertBST(struct TreeNode* root){ } ``` -## TypeScript +### TypeScript > 递归法 @@ -462,7 +462,7 @@ function convertBST(root: TreeNode | null): TreeNode | null { }; ``` -## Scala +### Scala ```scala object Solution { @@ -481,7 +481,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -535,3 +535,4 @@ impl Solution { + From 75390603854b899d5a164b0cf508ec393aaa3712 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:04:13 +0800 Subject: [PATCH 0597/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=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 --- ...21\346\200\273\347\273\223\347\257\207.md" | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" index 4c742a6bbf..829495438d 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -92,7 +92,7 @@ * 递归:中序,双指针操作 * 迭代:模拟中序,逻辑相同 * [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html) - + * 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合 * [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) @@ -154,29 +154,13 @@ 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 - **最后,二叉树系列就这么完美结束了,估计这应该是最长的系列了,感谢大家33天的坚持与陪伴,接下来我们又要开始新的系列了「回溯算法」!** - - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - -

+ From aaa8cc2443a9a05e5af3acae67d7f593c7c34077 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 19:40:19 +0800 Subject: [PATCH 0598/1533] =?UTF-8?q?Update=200115.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\345\272\217\345\210\227.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 56516132ba..91f35291cb 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -337,6 +337,36 @@ impl Solution { } ``` +> 滚动数组 + +```rust +impl Solution { + pub fn num_distinct(s: String, t: String) -> i32 { + if s.len() < t.len() { + return 0; + } + let (s, t) = (s.into_bytes(), t.into_bytes()); + // 对于 t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素) + let mut dp = vec![1; s.len() + 1]; + for char_t in t { + // dp[i - 1][j - 1],dp[j + 1] 更新之前的值 + let mut pre = dp[0]; + // 当开始遍历 t,s 的前 0 个字符无法包含任意子序列 + dp[0] = 0; + for (j, &char_s) in s.iter().enumerate() { + let temp = dp[j + 1]; + if char_t == char_s { + dp[j + 1] = pre + dp[j]; + } else { + dp[j + 1] = dp[j]; + } + pre = temp; + } + } + dp[s.len()] + } +} +```

From 0a83ee2c20c1b1294a2de20f58526755caa785e8 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 19:52:17 +0800 Subject: [PATCH 0599/1533] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\351\231\244\346\223\215\344\275\234.md" | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 48d15b0ba9..45e62f7d0f 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -368,7 +368,31 @@ function minDistance(word1: string, word2: string): number { }; ``` - +Rust: + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for i in 0..word1.len() { + dp[i + 1][0] = i + 1; + } + for j in 0..word2.len() { + dp[0][j + 1] = j + 1; + } + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j]; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]) + 1; + } + } + dp[word1.len()][word2.len()] as i32 + } +} +```

From d6bf0c52cae3da2f9842ba2a30be15099cd53280 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 20:00:17 +0800 Subject: [PATCH 0600/1533] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\351\231\244\346\223\215\344\275\234.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 45e62f7d0f..8b5ded3b8d 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -394,6 +394,26 @@ impl Solution { } ``` +> 版本 2 + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + (word1.len() + word2.len() - 2 * dp[word1.len()][word2.len()]) as i32 + } +} +``` +

From 6e613877f24394d2e4da3f97580d5e75be7d3b83 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 10:54:21 +0800 Subject: [PATCH 0601/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=9E?= =?UTF-8?q?=E6=BA=AF=E7=AE=97=E6=B3=95=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=20=E6=8E=92=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 --- ...06\350\256\272\345\237\272\347\241\200.md" | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index f11dbaef52..dff1823c0c 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,13 +6,19 @@ # 回溯算法理论基础 -## 题目分类大纲如下: +## 题目分类 回溯算法大纲 -可以配合我的B站视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) 一起学习! +## 算法公开课 -## 什么是回溯法 + + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/),相信结合视频再看本篇题解,更有助于大家对本题的理解。** + +## 理论基础 + +### 什么是回溯法 回溯法也可以叫做回溯搜索法,它是一种搜索的方式。 @@ -22,7 +28,7 @@ **所以以下讲解中,回溯函数也就是递归函数,指的都是一个函数**。 -## 回溯法的效率 +### 回溯法的效率 回溯法的性能如何呢,这里要和大家说清楚了,**虽然回溯法很难,很不好理解,但是回溯法并不是什么高效的算法**。 @@ -34,7 +40,7 @@ 此时大家应该好奇了,都什么问题,这么牛逼,只能暴力搜索。 -## 回溯法解决的问题 +### 回溯法解决的问题 回溯法,一般可以解决如下几种问题: @@ -55,7 +61,7 @@ 记住组合无序,排列有序,就可以了。 -## 如何理解回溯法 +### 如何理解回溯法 **回溯法解决的问题都可以抽象为树形结构**,是的,我指的是所有回溯法的问题都可以抽象为树形结构! @@ -66,7 +72,7 @@ 这块可能初学者还不太理解,后面的回溯算法解决的所有题目中,我都会强调这一点并画图举相应的例子,现在有一个印象就行。 -## 回溯法模板 +### 回溯法模板 这里给出Carl总结的回溯算法模板。 @@ -173,3 +179,4 @@ void backtracking(参数) { + From 6f3fdf7286c92830e0e9feff8fbdca90442af159 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 10:59:00 +0800 Subject: [PATCH 0602/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 444e15ce74..702ffafe52 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -5,10 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - - - # 第77题. 组合 [力扣题目链接](https://leetcode.cn/problems/combinations/ ) @@ -27,13 +23,12 @@ [1,4], ] -# 算法公开课 +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv),[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -**《代码随想录》算法视频公开课:[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv),[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 - -# 思路 +## 思路 本题是回溯法的经典题目。 @@ -345,12 +340,10 @@ public: - - ## 其他语言版本 -### Java: +### Java ```java class Solution { @@ -451,7 +444,7 @@ func dfs(n int, k int, start int) { } ``` -### javascript +### Javascript 剪枝: From f5e50a919d84cc41b00843a403ad3b566ac521b2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:02:27 +0800 Subject: [PATCH 0603/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E7=BB=84?= =?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\344\274\230\345\214\226.md" | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index 3926d00694..9577d65f3c 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -7,8 +7,11 @@ # 77.组合优化 +## 算法公开课 -**《代码随想录》算法视频公开课:[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解。** + +## 思路 在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)中,我们通过回溯搜索法,解决了n个数中求k个数的组合问题。 @@ -46,7 +49,7 @@ public: }; ``` -# 剪枝优化 +## 剪枝优化 我们说过,回溯法虽然是暴力搜索,但也有时候可以有点剪枝优化一下的。 @@ -135,7 +138,7 @@ public: -# 总结 +## 总结 本篇我们针对求组合问题的回溯法代码做了剪枝优化,这个优化如果不画图的话,其实不好理解,也不好讲清楚。 @@ -143,14 +146,10 @@ public: **就酱,学到了就帮Carl转发一下吧,让更多的同学知道这里!** - - - - ## 其他语言版本 +### Java -Java: ```java class Solution { List> result = new ArrayList<>(); @@ -179,7 +178,8 @@ class Solution { } ``` -Python: +### Python + ```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: @@ -199,7 +199,8 @@ class Solution: ``` -Go: +### Go + ```Go var ( path []int @@ -227,7 +228,7 @@ func dfs(n int, k int, start int) { } ``` -javaScript: +### JavaScript ```js var combine = function(n, k) { @@ -249,7 +250,7 @@ var combine = function(n, k) { }; ``` -TypeScript: +### TypeScript ```typescript function combine(n: number, k: number): number[][] { @@ -270,7 +271,7 @@ function combine(n: number, k: number): number[][] { }; ``` -Rust: +### Rust ```Rust impl Solution { @@ -296,7 +297,7 @@ impl Solution { } ``` -C: +### C ```c int* path; @@ -351,7 +352,7 @@ int** combine(int n, int k, int* returnSize, int** returnColumnSizes){ } ``` -Swift: +### Swift ```swift func combine(_ n: Int, _ k: Int) -> [[Int]] { @@ -381,7 +382,7 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { } ``` -Scala: +### Scala ```scala object Solution { @@ -414,3 +415,4 @@ object Solution { + From f30fd2982b532281d156d7c7cd9a9c7471a0a134 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:08:32 +0800 Subject: [PATCH 0604/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200216.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=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/0077.\347\273\204\345\220\210.md" | 3 +- ...345\220\210\346\200\273\345\222\214III.md" | 34 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 702ffafe52..7d71b51763 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -103,7 +103,7 @@ for (int i = 1; i <= n; i++) { 在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中我们提到了回溯法三部曲,那么我们按照回溯法三部曲开始正式讲解代码了。 -## 回溯法三部曲 +### 回溯法三部曲 * 递归函数的返回值以及参数 @@ -744,3 +744,4 @@ object Solution { + diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index 319b2eba71..4de7dc58f3 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -7,8 +7,6 @@ - - > 别看本篇选的是组合总和III,而不是组合总和,本题和上一篇77.组合相比难度刚刚好! # 216.组合总和III @@ -30,12 +28,12 @@ 输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 本题就是在[1,2,3,4,5,6,7,8,9]这个集合中找到和为n的k个数的组合。 @@ -54,7 +52,7 @@ 图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。 -## 回溯三部曲 +### 回溯三部曲 * **确定递归函数参数** @@ -165,7 +163,7 @@ public: }; ``` -## 剪枝 +### 剪枝 这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。 @@ -238,7 +236,7 @@ public: * 时间复杂度: O(n * 2^n) * 空间复杂度: O(n) -# 总结 +## 总结 开篇就介绍了本题与[77.组合](https://programmercarl.com/0077.组合.html)的区别,相对来说加了元素总和的限制,如果做完[77.组合](https://programmercarl.com/0077.组合.html)再做本题在合适不过。 @@ -249,10 +247,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java 模板方法 @@ -358,7 +356,7 @@ class Solution { } ``` -## Python +### Python ```py class Solution: @@ -383,7 +381,7 @@ class Solution: ``` -## Go +### Go 回溯+减枝 @@ -418,7 +416,7 @@ func dfs(k, n int, start int, sum int) { } ``` -## javaScript +### JavaScript ```js /** @@ -455,7 +453,7 @@ var combinationSum3 = function(k, n) { }; ``` -## TypeScript +### TypeScript ```typescript function combinationSum3(k: number, n: number): number[][] { @@ -479,7 +477,7 @@ function combinationSum3(k: number, n: number): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -516,7 +514,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -575,7 +573,7 @@ int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){ } ``` -## Swift +### Swift ```swift func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { @@ -607,7 +605,7 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { } ``` -## Scala +### Scala ```scala object Solution { From 87b215dd2e46588f3c97b25ba4cd2e3b15dbff61 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:11:15 +0800 Subject: [PATCH 0605/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200017.=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?= =?UTF-8?q?=E5=90=88=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\347\273\204\345\220\210.md" | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index cf5e45207b..b3ba1e5e5c 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -21,12 +21,12 @@ 说明:尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[还得用回溯算法!| LeetCode:17.电话号码的字母组合](https://www.bilibili.com/video/BV1yV4y1V7Ug),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[还得用回溯算法!| LeetCode:17.电话号码的字母组合](https://www.bilibili.com/video/BV1yV4y1V7Ug),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 从示例上来说,输入"23",最直接的想法就是两层for循环遍历了吧,正好把组合的情况都输出了。 @@ -40,7 +40,7 @@ 2. 两个字母就两个for循环,三个字符我就三个for循环,以此类推,然后发现代码根本写不出来 3. 输入1 * #按键等等异常情况 -## 数字和字母如何映射 +### 数字和字母如何映射 可以使用map或者定义一个二维数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下: @@ -59,7 +59,7 @@ const string letterMap[10] = { }; ``` -## 回溯法来解决n个for循环的问题 +### 回溯法来解决n个for循环的问题 对于回溯法还不了解的同学看这篇:[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html) @@ -134,9 +134,6 @@ for (int i = 0; i < letters.size(); i++) { **但是要知道会有这些异常,如果是现场面试中,一定要考虑到!** - -## C++代码 - 关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中的回溯法模板,不难写出如下C++代码: @@ -233,7 +230,7 @@ public: 所以大家可以按照版本一来写就可以了。 -# 总结 +## 总结 本篇将题目的三个要点一一列出,并重点强调了和前面讲解过的[77. 组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)的区别,本题是多个集合求组合,所以在回溯的搜索过程中,都有一些细节需要注意的。 @@ -241,10 +238,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -286,7 +283,7 @@ class Solution { } ``` -## Python +### Python 回溯 ```python class Solution: @@ -435,7 +432,7 @@ class Solution: -## Go +### Go 主要在于递归中传递下一个数字 @@ -470,7 +467,7 @@ func dfs(digits string, start int) { } ``` -## javaScript +### JavaScript ```js var letterCombinations = function(digits) { @@ -497,7 +494,7 @@ var letterCombinations = function(digits) { }; ``` -## TypeScript +### TypeScript ```typescript function letterCombinations(digits: string): string[] { @@ -531,7 +528,7 @@ function letterCombinations(digits: string): string[] { }; ``` -## Rust +### Rust ```Rust const map: [&str; 10] = [ @@ -563,7 +560,7 @@ impl Solution { } ``` -## C +### C ```c char* path; @@ -625,7 +622,7 @@ char ** letterCombinations(char * digits, int* returnSize){ } ``` -## Swift +### Swift ```swift func letterCombinations(_ digits: String) -> [String] { @@ -666,7 +663,7 @@ func letterCombinations(_ digits: String) -> [String] { } ``` -## Scala: +### Scala ```scala object Solution { @@ -702,3 +699,4 @@ object Solution { + From 2ed8beef418343adccc7ebc755a8c40159b6fe0d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:15:23 +0800 Subject: [PATCH 0606/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200039.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\346\200\273\345\222\214.md" | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 4d9466c3c0..2a36ce5adb 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -39,11 +39,11 @@ candidates 中的数字可以无限制重复被选取。 [3,5] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[Leetcode:39. 组合总和讲解](https://www.bilibili.com/video/BV1KT4y1M7HJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[Leetcode:39. 组合总和讲解](https://www.bilibili.com/video/BV1KT4y1M7HJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 题目中的**无限制重复被选取,吓得我赶紧想想 出现0 可咋办**,然后看到下面提示:1 <= candidates[i] <= 200,我就放心了。 @@ -57,7 +57,7 @@ candidates 中的数字可以无限制重复被选取。 而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。 -## 回溯三部曲 +### 回溯三部曲 * 递归函数参数 @@ -156,7 +156,7 @@ public: }; ``` -## 剪枝优化 +### 剪枝优化 在这个树形结构中: @@ -217,7 +217,7 @@ public: * 时间复杂度: O(n * 2^n),注意这只是复杂度的上界,因为剪枝的存在,真实的时间复杂度远小于此 * 空间复杂度: O(target) -# 总结 +## 总结 本题和我们之前讲过的[77.组合](https://programmercarl.com/0077.组合.html)、[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)有两点不同: @@ -238,10 +238,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java // 剪枝优化 @@ -271,7 +271,7 @@ class Solution { } ``` -## Python +### Python 回溯(版本一) @@ -370,7 +370,7 @@ class Solution: ``` -## Go +### Go 主要在于递归中传递下一个数字 @@ -404,7 +404,7 @@ func dfs(candidates []int, start int, target int) { } ``` -## JavaScript +### JavaScript ```js var combinationSum = function(candidates, target) { @@ -430,7 +430,7 @@ var combinationSum = function(candidates, target) { }; ``` -## TypeScript +### TypeScript ```typescript function combinationSum(candidates: number[], target: number): number[][] { @@ -456,7 +456,7 @@ function combinationSum(candidates: number[], target: number): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -485,7 +485,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -541,7 +541,7 @@ int** combinationSum(int* candidates, int candidatesSize, int target, int* retur } ``` -## Swift +### Swift ```swift func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { @@ -570,7 +570,7 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { } ``` -## Scala +### Scala ```scala object Solution { @@ -604,3 +604,4 @@ object Solution { + From c3827c43442ad816f4575d7d45958c36dfc92c8a Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:17:47 +0800 Subject: [PATCH 0607/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200040.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\210\346\200\273\345\222\214II.md" | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 9094020ea9..33e4a46f51 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -41,13 +41,11 @@ candidates 中的每个数字在每个组合中只能使用一次。 ] ``` -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - -# 思路 +## 思路 这道题目和[39.组合总和](https://programmercarl.com/0039.组合总和.html)如下区别: @@ -86,7 +84,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。 -## 回溯三部曲 +### 回溯三部曲 * **递归函数参数** @@ -217,7 +215,7 @@ public: * 时间复杂度: O(n * 2^n) * 空间复杂度: O(n) -## 补充 +### 补充 这里直接用startIndex来去重也是可以的, 就不用used数组了。 @@ -257,7 +255,7 @@ public: ``` -# 总结 +## 总结 本题同样是求组合总和,但就是因为其数组candidates有重复元素,而要求不能有重复的组合,所以相对于[39.组合总和](https://programmercarl.com/0039.组合总和.html)难度提升了不少。 @@ -265,14 +263,10 @@ public: 所以Carl有必要把去重的这块彻彻底底的给大家讲清楚,**就连“树层去重”和“树枝去重”都是我自创的词汇,希望对大家理解有帮助!** +## 其他语言版本 - - -# 其他语言版本 - - -## Java +### Java **使用标记数组** ```Java class Solution { @@ -355,7 +349,7 @@ class Solution { } ``` -## Python +### Python 回溯 ```python class Solution: @@ -442,7 +436,7 @@ class Solution: self.combinationSumHelper(candidates, target - candidates[i], i + 1, path, results) path.pop() ``` -## Go +### Go 主要在于如何在回溯中去重 **使用used数组** @@ -518,7 +512,7 @@ func dfs(candidates []int, start int, target int) { } } ``` -## javaScript +### JavaScript ```js /** @@ -588,7 +582,7 @@ var combinationSum2 = function(candidates, target) { }; ``` -## TypeScript +### TypeScript ```typescript function combinationSum2(candidates: number[], target: number): number[][] { @@ -619,7 +613,7 @@ function combinationSum2(candidates: number[], target: number): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -654,7 +648,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -716,7 +710,7 @@ int** combinationSum2(int* candidates, int candidatesSize, int target, int* retu } ``` -## Swift +### Swift ```swift func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] { @@ -749,7 +743,7 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] { ``` -## Scala +### Scala ```scala object Solution { @@ -784,3 +778,4 @@ object Solution { + From 7bd8751a0cf255e17652065d4b1894344032ceb0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:07:54 +0800 Subject: [PATCH 0608/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\345\233\236\346\226\207\344\270\262.md" | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 92fed58a2e..ca73e9f7ca 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -23,12 +23,12 @@ ["a","a","b"] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 本题这涉及到两个关键问题: @@ -58,7 +58,7 @@ 此时可以发现,切割问题的回溯搜索的过程和组合问题的回溯搜索的过程是差不多的。 -## 回溯三部曲 +### 回溯三部曲 * 递归函数参数 @@ -124,7 +124,7 @@ for (int i = startIndex; i < s.size(); i++) { **注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1**。 -## 判断回文子串 +### 判断回文子串 最后我们看一下回文子串要如何判断了,判断一个字符串是否是回文。 @@ -147,8 +147,6 @@ for (int i = startIndex; i < s.size(); i++) { 此时关键代码已经讲解完毕,整体代码如下(详细注释了) -## C++整体代码 - 根据Carl给出的回溯算法模板: ```CPP @@ -212,7 +210,7 @@ public: * 时间复杂度: O(n * 2^n) * 空间复杂度: O(n^2) -# 优化 +## 优化 上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在: @@ -272,7 +270,7 @@ public: ``` -# 总结 +## 总结 这道题目在leetcode上是中等,但可以说是hard的题目了,但是代码其实就是按照模板的样子来的。 @@ -306,10 +304,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { List> lists = new ArrayList<>(); @@ -351,7 +349,7 @@ class Solution { } ``` -## Python +### Python 回溯 基本版 ```python class Solution: @@ -473,7 +471,7 @@ class Solution: return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2)) ``` -## Go +### Go ```go var ( path []string // 放已经回文的子串 @@ -512,7 +510,7 @@ func isPalindrome(s string) bool { } ``` -## javaScript +### JavaScript ```js /** @@ -545,7 +543,7 @@ var partition = function(s) { }; ``` -## TypeScript +### TypeScript ```typescript function partition(s: string): string[][] { @@ -582,7 +580,7 @@ function partition(s: string): string[][] { }; ``` -## C +### C ```c char** path; @@ -679,7 +677,7 @@ char*** partition(char* s, int* returnSize, int** returnColumnSizes){ } ``` -## Swift +### Swift ```swift func partition(_ s: String) -> [[String]] { @@ -719,7 +717,7 @@ func partition(_ s: String) -> [[String]] { } ``` -## Rust +### Rust **回溯+函数判断回文串** ```Rust @@ -808,7 +806,7 @@ impl Solution { ``` -## Scala +### Scala ```scala object Solution { @@ -855,3 +853,4 @@ object Solution { + From 6f711891ba9df855e68147bc0f609753bd705cf7 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:11:47 +0800 Subject: [PATCH 0609/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\216\237IP\345\234\260\345\235\200.md" | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 55e57dde5f..59cd92da59 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -40,16 +40,12 @@ * 0 <= s.length <= 3000 * s 仅由数字组成 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - -# 思路 +## 思路 做这道题目之前,最好先把[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)这个做了。 @@ -63,7 +59,7 @@ ![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933.png) -## 回溯三部曲 +### 回溯三部曲 * 递归参数 @@ -134,7 +130,7 @@ for (int i = startIndex; i < s.size(); i++) { } ``` -## 判断子串是否合法 +### 判断子串是否合法 最后就是在写一个判断段位是否是有效段位了。 @@ -169,8 +165,6 @@ bool isValid(const string& s, int start, int end) { } ``` -## C++代码 - 根据[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)给出的回溯算法模板: @@ -247,7 +241,7 @@ public: * 时间复杂度: O(3^4),IP地址最多包含4个数字,每个数字最多有3种可能的分割方式,则搜索树的最大深度为4,每个节点最多有3个子节点。 * 空间复杂度: O(n) -# 总结 +## 总结 在[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)中我列举的分割字符串的难点,本题都覆盖了。 @@ -259,9 +253,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## java +### Java ```java class Solution { @@ -402,7 +396,7 @@ class Solution { ``` -## python +### Python 回溯(版本一) ```python @@ -478,9 +472,7 @@ class Solution: ``` - - -## Go +### Go ```go var ( @@ -517,7 +509,7 @@ func dfs(s string, start int) { } ``` -## JavaScript +### JavaScript ```js /** @@ -547,7 +539,7 @@ var restoreIpAddresses = function(s) { }; ``` -## TypeScript +### TypeScript ```typescript function isValidIpSegment(str: string): boolean { @@ -586,7 +578,7 @@ function restoreIpAddresses(s: string): string[] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -643,7 +635,7 @@ impl Solution { } ``` -## C +### C ```c //记录结果 char** result; @@ -719,7 +711,7 @@ char ** restoreIpAddresses(char * s, int* returnSize){ } ``` -## Swift +### Swift ```swift // 判断区间段是否合法 @@ -766,7 +758,7 @@ func restoreIpAddresses(_ s: String) -> [String] { } ``` -## Scala +### Scala ```scala object Solution { @@ -813,3 +805,4 @@ object Solution { + From 92695ceeddbc3b9c39d367bd522a44e1b37502b1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:16:33 +0800 Subject: [PATCH 0610/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200078.=E5=AD=90?= =?UTF-8?q?=E9=9B=86=E9=97=AE=E9=A2=98=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=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/0078.\345\255\220\351\233\206.md" | 33 ++++++++++----------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 21009f6a36..5f3654deae 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -27,12 +27,12 @@   [] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集](https://www.bilibili.com/video/BV1U84y1q7Ci),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集](https://www.bilibili.com/video/BV1U84y1q7Ci),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 求子集问题和[77.组合](https://programmercarl.com/0077.组合.html)和[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)又不一样了。 @@ -52,7 +52,7 @@ 从图中红线部分,可以看出**遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合**。 -## 回溯三部曲 +### 回溯三部曲 * 递归函数参数 @@ -102,8 +102,6 @@ for (int i = startIndex; i < nums.size(); i++) { } ``` -## C++代码 - 根据[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)给出的回溯算法模板: ``` @@ -158,7 +156,7 @@ public: 并不会,因为每次递归的下一层就是从i+1开始的。 -# 总结 +## 总结 相信大家经过了 * 组合问题: @@ -178,10 +176,10 @@ public: **而组合问题、分割问题是收集树形结构中叶子节点的结果**。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { List> result = new ArrayList<>();// 存放符合条件结果的集合 @@ -205,7 +203,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: def subsets(self, nums): @@ -224,7 +222,7 @@ class Solution: path.pop() ``` -## Go +### Go ```Go var ( path []int @@ -248,7 +246,7 @@ func dfs(nums []int, start int) { } ``` -## Javascript +### Javascript ```Javascript var subsets = function(nums) { @@ -267,7 +265,7 @@ var subsets = function(nums) { }; ``` -## TypeScript +### TypeScript ```typescript function subsets(nums: number[]): number[][] { @@ -287,7 +285,7 @@ function subsets(nums: number[]): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -311,7 +309,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -369,7 +367,7 @@ int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) } ``` -## Swift +### Swift ```swift func subsets(_ nums: [Int]) -> [[Int]] { @@ -392,7 +390,7 @@ func subsets(_ nums: [Int]) -> [[Int]] { } ``` -## Scala +### Scala 思路一: 使用本题解思路 @@ -451,3 +449,4 @@ object Solution { + From cbec2c04ed9c107921834854f41b36dfd9cab785 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:50:02 +0800 Subject: [PATCH 0611/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200090.=E5=AD=90?= =?UTF-8?q?=E9=9B=86II=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0090.\345\255\220\351\233\206II.md" | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 3238ee52a2..88c8bdade6 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -3,8 +3,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - # 90.子集II [力扣题目链接](https://leetcode.cn/problems/subsets-ii/) @@ -25,9 +23,9 @@ [] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -240,7 +238,7 @@ class Solution { -#### Python3 +### Python3 回溯 利用used数组去重 ```python @@ -646,3 +644,4 @@ object Solution { + From 322f817666b8bd7c65477a9168bb1db5c4f16410 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:51:29 +0800 Subject: [PATCH 0612/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200491.=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...0\222\345\242\236\345\255\220\345\272\217\345\210\227.md" | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 4b21008ea5..e2011372f8 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -23,9 +23,9 @@ * 数组中的整数范围是 [-100,100]。 * 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列](https://www.bilibili.com/video/BV1EG4y1h78v/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列](https://www.bilibili.com/video/BV1EG4y1h78v/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -619,4 +619,3 @@ object Solution { - From f08c25631fc39152caa4d3f78ffb6b0d02c3f2e5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:54:15 +0800 Subject: [PATCH 0613/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20046.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0046.\345\205\250\346\216\222\345\210\227.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index de1af64295..1f5263a79e 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -24,9 +24,9 @@ ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列](https://www.bilibili.com/video/BV19v4y1S79W/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列](https://www.bilibili.com/video/BV19v4y1S79W/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -491,3 +491,4 @@ object Solution { + From f57b19df94a1ed5122571e4482796a8594053af6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:55:37 +0800 Subject: [PATCH 0614/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200047.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97II=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0047.\345\205\250\346\216\222\345\210\227II.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index afede33a46..4fed8a5c8c 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -31,9 +31,9 @@ * 1 <= nums.length <= 8 * -10 <= nums[i] <= 10 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II](https://www.bilibili.com/video/BV1R84y1i7Tm/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II](https://www.bilibili.com/video/BV1R84y1i7Tm/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -58,7 +58,7 @@ 在[46.全排列](https://programmercarl.com/0046.全排列.html)中已经详细讲解了排列问题的写法,在[40.组合总和II](https://programmercarl.com/0040.组合总和II.html) 、[90.子集II](https://programmercarl.com/0090.子集II.html)中详细讲解了去重的写法,所以这次我就不用回溯三部曲分析了,直接给出代码,如下: -## C++代码 + ```CPP class Solution { @@ -170,7 +170,7 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { if (i > 0 && nums[i] == nums[i - 1]) { continue; } -``` +``` 其实并不行,一定要加上 `used[i - 1] == false`或者`used[i - 1] == true`,因为 used[i - 1] 要一直是 true 或者一直是false 才可以,而不是 一会是true 一会又是false。 所以这个条件要写上。 @@ -179,7 +179,7 @@ if (i > 0 && nums[i] == nums[i - 1]) { ## 其他语言版本 -### java +### Java ```java class Solution { @@ -221,7 +221,7 @@ class Solution { } ``` -### python +Python ```python class Solution: @@ -526,3 +526,4 @@ object Solution { + From d9fc7a2d0e76b3439f68a165bcfe98553af4b7dd Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:59:16 +0800 Subject: [PATCH 0615/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=9E?= =?UTF-8?q?=E6=BA=AF=E7=AE=97=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E7=9A=84=E5=8F=A6=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95=20?= =?UTF-8?q?=E6=8E=92=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 --- ...4\270\200\347\247\215\345\206\231\346\263\225.md" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 4e09b9252a..2be79805af 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -246,8 +246,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 ## 其他语言版本 - -Java: +### Java **47.全排列II** @@ -350,7 +349,7 @@ class Solution { } } ``` -Python: +### Python **90.子集II** @@ -432,7 +431,7 @@ class Solution: ``` -JavaScript: +### JavaScript **90.子集II** @@ -510,7 +509,7 @@ function permuteUnique(nums) { }; ``` -TypeScript: +### TypeScript **90.子集II** @@ -592,7 +591,7 @@ function permuteUnique(nums: number[]): number[][] { }; ``` -Rust: +### Rust **90.子集II**: @@ -713,3 +712,4 @@ impl Solution { + From 42c1bd94ab1bddac9f258730d26275ff6cfeeeec Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:03:21 +0800 Subject: [PATCH 0616/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200332.=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B=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 --- ...11\346\216\222\350\241\214\347\250\213.md" | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index fa6414e9ac..3798b48d87 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -29,9 +29,11 @@ * 输出:["JFK","ATL","JFK","SFO","ATL","SFO"] * 解释:另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"]。但是它自然排序更大更靠后。 -## 思路 +## 算法公开课 + +**如果对回溯算法基础还不了解的话,我还特意录制了一期视频,[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。 -**如果对回溯算法基础还不了解的话,我还特意录制了一期视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。 +## 思路 这道题目还是很难的,之前我们用回溯法解决了如下问题:[组合问题](https://programmercarl.com/0077.组合.html),[分割问题](https://programmercarl.com/0093.复原IP地址.html),[子集问题](https://programmercarl.com/0078.子集.html),[排列问题](https://programmercarl.com/0046.全排列.html)。 @@ -53,7 +55,7 @@ 针对以上问题我来逐一解答! -## 如何理解死循环 +### 如何理解死循环 对于死循环,我来举一个有重复机场的例子: @@ -61,7 +63,7 @@ 为什么要举这个例子呢,就是告诉大家,出发机场和到达机场也会重复的,**如果在解题的过程中没有对集合元素处理好,就会死循环。** -## 该记录映射关系 +### 该记录映射关系 有多种解法,字母序靠前排在前面,让很多同学望而退步,如何该记录映射关系呢 ? @@ -90,7 +92,7 @@ unordered_map> targets:unordered_map<出发机场, ma **相当于说我不删,我就做一个标记!** -## 回溯法 +### 回溯法 这道题目我使用回溯法,那么下面按照我总结的回溯模板来: @@ -260,8 +262,8 @@ for (pairtarget : targets[result[result.size() - 1]]) ## 其他语言版本 -### java - +### Java + ```java class Solution { private LinkedList res; @@ -346,7 +348,7 @@ class Solution { } ``` -### python +### Python 回溯 使用used数组 ```python @@ -406,7 +408,7 @@ class Solution: path.pop() # 移除目的地 return False # 没有找到有效行程 -``` +``` 回溯 使用字典 逆序 ```python from collections import defaultdict @@ -430,8 +432,8 @@ class Solution: self.backtracking(next_airport, targets, result) # 递归调用回溯函数进行深度优先搜索 result.append(airport) # 将当前机场添加到行程路径中 ``` - -### GO + +### Go ```go type pair struct { target string @@ -577,7 +579,7 @@ function findItinerary(tickets: string[][]): string[] { }; ``` -### C语言 +### C ```C char **result; @@ -638,7 +640,7 @@ char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, in return result; } ``` - + ### Swift 直接迭代tickets数组: @@ -791,3 +793,4 @@ impl Solution { + From f58a35db26a0fa4f4b4ae552e3713392b9e1d193 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:04:28 +0800 Subject: [PATCH 0617/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200051.N=E7=9A=87?= =?UTF-8?q?=E5=90=8E=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 13cdafb829..6bc4fa78c6 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -28,9 +28,9 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, * 输入:n = 1 * 输出:[["Q"]] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[这就是传说中的N皇后? 回溯算法安排!| LeetCode:51.N皇后](https://www.bilibili.com/video/BV1Rd4y1c7Bq/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这就是传说中的N皇后? 回溯算法安排!| LeetCode:51.N皇后](https://www.bilibili.com/video/BV1Rd4y1c7Bq/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -864,3 +864,4 @@ object Solution { + From dacd87b009f8a106172c4e07853bdf2b3aaf664f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:05:52 +0800 Subject: [PATCH 0618/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200051.N=E7=9A=87?= =?UTF-8?q?=E5=90=8EII=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0052.N\347\232\207\345\220\216II.md" | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index 90b920bac4..ac774c83f9 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -43,13 +43,11 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并   ".Q.."] ] -# 思路 +## 思路 详看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别 -# C++代码 - ```CPP class Solution { private: @@ -100,8 +98,9 @@ public: }; ``` -# 其他语言补充 -JavaScript +## 其他语言补充 +### JavaScript + ```javascript var totalNQueens = function(n) { let count = 0; @@ -146,7 +145,7 @@ var totalNQueens = function(n) { }; ``` -TypeScript: +### TypeScript ```typescript // 0-该格为空,1-该格有皇后 @@ -199,7 +198,7 @@ function checkValid(chess: GridStatus[][], i: number, j: number, n: number): boo } ``` -C +### C ```c //path[i]为在i行,path[i]列上存在皇后 @@ -258,7 +257,8 @@ int totalNQueens(int n){ return answer; } ``` -Java +### Java + ```java class Solution { int count = 0; @@ -310,4 +310,3 @@ class Solution { - From 028bfdc662a56e8f0c7ff17bd743a2d21fe0f016 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:11:08 +0800 Subject: [PATCH 0619/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200037.=E8=A7=A3?= =?UTF-8?q?=E6=95=B0=E7=8B=AC=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0037.\350\247\243\346\225\260\347\213\254.md" | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index 6edd3c5b5e..1763063eb7 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -6,8 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) +> 如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) # 37. 解数独 @@ -35,11 +34,9 @@ * 你可以假设给定的数独只有唯一解。 * 给定数独永远是 9x9 形式的。 -# 算法公开课 - -**《代码随想录》算法视频公开课:[回溯算法二维递归?解数独不过如此!| LeetCode:37. 解数独](https://www.bilibili.com/video/BV1TW4y1471V/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法二维递归?解数独不过如此!| LeetCode:37. 解数独](https://www.bilibili.com/video/BV1TW4y1471V/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -764,3 +761,4 @@ object Solution { + From 93cba02a128b8aa9cd5982d36537a802dda03262 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:15:04 +0800 Subject: [PATCH 0620/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=9E?= =?UTF-8?q?=E6=BA=AF=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\272\257\346\200\273\347\273\223.md" | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index 21d78bc211..f2e46829cb 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -8,7 +8,9 @@ > 20张树形结构图、14道精选回溯题目,21篇回溯法精讲文章,由浅入深,一气呵成,这是全网最强回溯算法总结! -# 回溯法理论基础 +# 回溯总结篇 + +## 回溯法理论基础 转眼间[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)里已经分享连续讲解了21天的回溯算法,是时候做一个大总结了,本篇高能,需要花费很大的精力来看! @@ -51,10 +53,10 @@ void backtracking(参数) { **事实证明这个模板会伴随整个回溯法系列!** -# 组合问题 - ## 组合问题 +### 组合问题 + 在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)中,我们开始用回溯法解决第一道题目:组合问题。 我在文中开始的时候给大家列举k层for循环例子,进而得出都是同样是暴力解法,为什么要用回溯法! @@ -82,9 +84,9 @@ void backtracking(参数) { **在for循环上做剪枝操作是回溯法剪枝的常见套路!** 后面的题目还会经常用到。 -## 组合总和 +### 组合总和 -### 组合总和(一) +#### 组合总和(一) 在[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中,相当于 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)加了一个元素总和的限制。 @@ -99,7 +101,7 @@ void backtracking(参数) { 所以剪枝的代码可以在for循环加上 `i <= 9 - (k - path.size()) + 1` 的限制! -### 组合总和(二) +#### 组合总和(二) 在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)中讲解的组合总和问题,和[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html),[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)和区别是:本题没有数量要求,可以无限重复,但是有总和的限制,所以间接的也是有个数的限制。 @@ -128,7 +130,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; ![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202115929.png) -### 组合总和(三) +#### 组合总和(三) 在[回溯算法:求组合总和(三)](https://programmercarl.com/0040.组合总和II.html)中集合元素会有重复,但要求解集不能包含重复的组合。 @@ -151,7 +153,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 对于去重,其实排列和子集问题也是一样的道理。 -## 多个集合求组合 +### 多个集合求组合 在[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)中,开始用多个集合来求组合,还是熟悉的模板题目,但是有一些细节。 @@ -167,7 +169,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 其实本题不算难,但也处处是细节,还是要反复琢磨。 -# 切割问题 +## 切割问题 在[回溯算法:分割回文串](https://programmercarl.com/0131.分割回文串.html)中,我们开始讲解切割问题,虽然最后代码看起来好像是一道模板题,但是从分析到学会套用这个模板,是比较难的。 @@ -192,9 +194,9 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; ![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202448642.png) -# 子集问题 +## 子集问题 -## 子集问题(一) +### 子集问题(一) 在[回溯算法:求子集问题!](https://programmercarl.com/0078.子集.html)中讲解了子集问题,**在树形结构中子集问题是要收集所有节点的结果,而组合问题是收集叶子节点的结果**。 @@ -219,7 +221,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 } ``` -## 子集问题(二) +### 子集问题(二) 在[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)中,开始针对子集问题进行去重。 @@ -229,7 +231,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 ![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449.png) -## 递增子序列 +### 递增子序列 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! @@ -247,9 +249,9 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 **相信这个图胜过千言万语的解释了**。 -# 排列问题 +## 排列问题 -## 排列问题(一) +### 排列问题(一) [回溯算法:排列问题!](https://programmercarl.com/0046.全排列.html) 又不一样了。 @@ -266,7 +268,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 * 每层都是从0开始搜索而不是startIndex * 需要used数组记录path里都放了哪些元素了 -## 排列问题(二) +### 排列问题(二) 排列问题也要去重了,在[回溯算法:排列问题(二)](https://programmercarl.com/0047.全排列II.html)中又一次强调了“树层去重”和“树枝去重”。 @@ -290,7 +292,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 本题used数组即是记录path里都放了哪些元素,同时也用来去重,一举两得。 -# 去重问题 +## 去重问题 以上我都是统一使用used数组来去重的,其实使用set也可以用来去重! @@ -310,7 +312,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 used数组可是全局变量,每层与每层之间公用一个used数组,所以空间复杂度是O(n + n),最终空间复杂度还是O(n)。 -# 重新安排行程(图论额外拓展) +## 重新安排行程(图论额外拓展) 之前说过,有递归的地方就有回溯,深度优先搜索也是用递归来实现的,所以往往伴随着回溯。 @@ -327,9 +329,9 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 本题其实是一道深度优先搜索的题目,但是我完全使用回溯法的思路来讲解这道题题目,**算是给大家拓展一下思维方式,其实深搜和回溯也是分不开的,毕竟最终都是用递归**。 -# 棋盘问题 +## 棋盘问题 -## N皇后问题 +### N皇后问题 在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。 @@ -349,7 +351,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 -## 解数独问题 +### 解数独问题 在[回溯算法:解数独](https://programmercarl.com/0037.解数独.html)中要征服回溯法的最后一道山峰。 @@ -373,7 +375,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 **这样,解数独这么难的问题也被我们攻克了**。 -# 性能分析 +## 性能分析 **关于回溯算法的复杂度分析在网上的资料鱼龙混杂,一些所谓的经典面试书籍不讲回溯算法,算法书籍对这块也避而不谈,感觉就像是算法里模糊的边界**。 @@ -410,7 +412,7 @@ N皇后问题分析: **一般说道回溯算法的复杂度,都说是指数级别的时间复杂度,这也算是一个概括吧!** -# 总结 +## 总结 **[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)历时21天,14道经典题目分析,20张树形图,21篇回溯法精讲文章,从组合到切割,从子集到排列,从棋盘问题到最后的复杂度分析**,至此收尾了。 @@ -449,3 +451,4 @@ N皇后问题分析: + From a7e35954b094020f7813d90d83a5d7f6ec869a23 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 22:37:56 +0800 Subject: [PATCH 0621/1533] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...26\350\276\221\350\267\235\347\246\273.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 703e891311..19e7aade7f 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -401,6 +401,34 @@ int minDistance(char * word1, char * word2){ } ``` +Rust: + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1]; + for i in 1..=word2.len() { + dp[0][i] = i; + } + + for (j, v) in dp.iter_mut().enumerate().skip(1) { + v[0] = j; + } + for (i, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j]; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]).min(dp[i][j]) + 1; + } + } + + dp[word1.len()][word2.len()] as i32 + } +} +``` +

From 60bb5dc4fc2288182542422fce73691bc120608f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 23:27:11 +0800 Subject: [PATCH 0622/1533] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...26\350\276\221\350\267\235\347\246\273.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 19e7aade7f..15f35f6c8e 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -429,6 +429,36 @@ impl Solution { } ``` +> 一维 dp + +```rust +impl Solution { + pub fn min_distance(word1: String, word2: String) -> i32 { + let mut dp = vec![0; word1.len() + 1]; + for (i, v) in dp.iter_mut().enumerate().skip(1) { + *v = i; + } + + for char2 in word2.chars() { + // 相当于 dp[i][0] 的初始化 + let mut pre = dp[0]; + dp[0] += 1; // j = 0, 将前 i 个字符变成空串的个数 + for (j, char1) in word1.chars().enumerate() { + let temp = dp[j + 1]; + if char1 == char2 { + dp[j + 1] = pre; + } else { + dp[j + 1] = dp[j + 1].min(dp[j]).min(pre) + 1; + } + pre = temp; + } + } + + dp[word1.len()] as i32 + } +} +``` +

From 69ad28ca0ecbcd726619d5aad81351d4e275db54 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 23:57:57 +0800 Subject: [PATCH 0623/1533] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E4=B8=B2.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\226\207\345\255\220\344\270\262.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 084b9f74e6..35396192c0 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -517,8 +517,33 @@ function expandRange(s: string, left: number, right: number): number { } ``` +Rust: +> 双指针 + +```rust +impl Solution { + pub fn count_substrings(s: String) -> i32 { + let mut res = 0; + for i in 0..s.len() { + res += Self::extend(&s, i, i, s.len()); + res += Self::extend(&s, i, i + 1, s.len()); + } + res + } + + fn extend(s: &str, mut i: usize, mut j: usize, len: usize) -> i32 { + let mut res = 0; + while i < len && j < len && s[i..=i] == s[j..=j] { + res += 1; + i = i.wrapping_sub(1); + j += 1; + } + res + } +} +```

From 6011184b37f9e1d661edef1b124b5e195ef55bd9 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 25 Jul 2023 00:18:19 +0800 Subject: [PATCH 0624/1533] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...236\346\226\207\345\255\220\344\270\262.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 35396192c0..52f12d9b03 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -519,6 +519,24 @@ function expandRange(s: string, left: number, right: number): number { Rust: +```rust +impl Solution { + pub fn count_substrings(s: String) -> i32 { + let mut dp = vec![vec![false; s.len()]; s.len()]; + let mut res = 0; + + for i in (0..s.len()).rev() { + for j in i..s.len() { + if s[i..=i] == s[j..=j] && (j - i <= 1 || dp[i + 1][j - 1]) { + dp[i][j] = true; + res += 1; + } + } + } + res + } +} +``` > 双指针 From 4822d8ff8cb71d4787a4d6a3f33ecba98fbc9be3 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 25 Jul 2023 00:45:10 +0800 Subject: [PATCH 0625/1533] =?UTF-8?q?Update=200516.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...07\345\255\220\345\272\217\345\210\227.md" | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index fcdd57b0f4..2180bab8d9 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -274,7 +274,26 @@ function longestPalindromeSubseq(s: string): number { }; ``` - +Rust: + +```rust +impl Solution { + pub fn longest_palindrome_subseq(s: String) -> i32 { + let mut dp = vec![vec![0; s.len()]; s.len()]; + for i in (0..s.len()).rev() { + dp[i][i] = 1; + for j in i + 1..s.len() { + if s[i..=i] == s[j..=j] { + dp[i][j] = dp[i + 1][j - 1] + 2; + continue; + } + dp[i][j] = dp[i + 1][j].max(dp[i][j - 1]); + } + } + dp[0][s.len() - 1] + } +} +```

From 29c31ca76f32dfc8fc2ba7a593d4a82547ce55d7 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:46:09 +0800 Subject: [PATCH 0626/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20455.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0052.N\347\232\207\345\220\216II.md" | 1 + .../0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index ac774c83f9..29c2b58818 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -310,3 +310,4 @@ class Solution { + diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index ce1987ef1c..c9c1a8525d 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -30,9 +30,9 @@ - 0 <= s.length <= 3 \* 10^4 - 1 <= g[i], s[j] <= 2^31 - 1 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From e713f9c90aee716d53a05b1cb6fb35a802f2d8fc Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:48:15 +0800 Subject: [PATCH 0627/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\206\345\212\250\345\272\217\345\210\227.md" | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 08de23ae24..943dfe39b6 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -33,11 +33,13 @@ - 输入: [1,2,3,4,5,6,7,8,9] - 输出: 2 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -## 思路 1(贪心解法) +## 思路 + +### 思路 1(贪心解法) 本题要求通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。 @@ -69,7 +71,7 @@ 2. 情况二:数组首尾两端 3. 情况三:单调坡中有平坡 -### 情况一:上下坡中有平坡 +#### 情况一:上下坡中有平坡 例如 [1,2,2,2,1]这样的数组,如图: @@ -87,7 +89,7 @@ 所以我们记录峰值的条件应该是: `(preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)`,为什么这里允许 prediff == 0 ,就是为了 上面我说的这种情况。 -### 情况二:数组首尾两端 +#### 情况二:数组首尾两端 所以本题统计峰值的时候,数组最左面和最右面如何统计呢? @@ -142,7 +144,7 @@ public: 所以此时我们要讨论情况三! -### 情况三:单调坡度有平坡 +#### 情况三:单调坡度有平坡 在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: @@ -187,7 +189,7 @@ public: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108174452.png) -## 思路 2(动态规划) +### 思路 2(动态规划) 考虑用动态规划的思想来解决这个问题。 @@ -696,4 +698,3 @@ object Solution { - From fcd25819e9bb9b6bd8b44ea4664a2f9ac13ee37f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:49:56 +0800 Subject: [PATCH 0628/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20053.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...201\350\247\204\345\210\222\357\274\211.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 6f3b368676..f1b6470985 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -17,7 +17,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -97,8 +97,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java /** * 1.dp[i]代表当前下标对应的最大值 @@ -140,7 +140,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def maxSubArray(self, nums: List[int]) -> int: @@ -153,7 +154,8 @@ class Solution: return result ``` -Go: +### Go: + ```Go // solution // 1, dp @@ -184,7 +186,7 @@ func max(a,b int) int{ } ``` -JavaScript: +### JavaScript: ```javascript const maxSubArray = nums => { @@ -203,8 +205,7 @@ const maxSubArray = nums => { }; ``` - -Scala: +### Scala: ```scala object Solution { @@ -221,7 +222,7 @@ object Solution { } ``` -TypeScript: +### TypeScript: ```typescript function maxSubArray(nums: number[]): number { @@ -244,3 +245,4 @@ function maxSubArray(nums: number[]): number { + From 7da76551ba655f419020fd596569c47b02bf53b4 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:53:02 +0800 Subject: [PATCH 0629/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200122.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAII=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\234\200\344\275\263\346\227\266\346\234\272II.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index 89c654fad1..2c2ab225fd 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -37,9 +37,9 @@ - 1 <= prices.length <= 3 \* 10 ^ 4 - 0 <= prices[i] <= 10 ^ 4 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -316,7 +316,7 @@ function maxProfit(prices: number[]): number { } ``` -### Rust +### Rust: 贪心: @@ -389,7 +389,7 @@ int maxProfit(int* prices, int pricesSize){ } ``` -### Scala +### Scala: 贪心: @@ -411,3 +411,4 @@ object Solution { + From 336884055c722c3c3e6ce43960d7eae176174087 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:55:04 +0800 Subject: [PATCH 0630/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200055.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index e54c20342b..bedb09abee 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -26,9 +26,9 @@ - 输出: false - 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From a04f7f54746d70794d976c616ed630bbad6070b6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:56:46 +0800 Subject: [PATCH 0631/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\267\263\350\267\203\346\270\270\346\210\217II.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 2f0349b2cc..02c8e4862e 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -25,9 +25,9 @@ 说明: 假设你总是可以到达数组的最后一个位置。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -53,7 +53,7 @@ **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** -## 方法一 +### 方法一 从图中可以看出来,就是移动下标达到了当前覆盖的最远距离下标时,步数就要加一,来增加覆盖距离。最后的步数就是最少步数。 @@ -90,7 +90,7 @@ public: * 空间复杂度: O(1) -## 方法二 +### 方法二 依然是贪心,思路和方法一差不多,代码可以简洁一些。 @@ -469,3 +469,4 @@ impl Solution { + From 9b3d4ac2454afb393931788ab3c8b4822189eef5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:01:20 +0800 Subject: [PATCH 0632/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=201005.K=E6=AC=A1?= =?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\226\347\232\204\346\225\260\347\273\204\345\222\214.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 4cf69d6f41..bed11c7a19 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -34,9 +34,9 @@ * 1 <= K <= 10000 * -100 <= A[i] <= 100 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 5b4a2e245b16fe3b6877f5d5c5a53f40be1048fd Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:10:42 +0800 Subject: [PATCH 0633/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200134.=E5=8A=A0?= =?UTF-8?q?=E6=B2=B9=E7=AB=99=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0134.\345\212\240\346\262\271\347\253\231.md" | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index ad9acfbc80..2f9539e8a4 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -45,12 +45,14 @@ * 解释: 你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +## 思路 -## 暴力方法 + +### 暴力方法 暴力的方法很明显就是O(n^2)的,遍历每一个加油站为起点的情况,模拟一圈。 @@ -85,7 +87,7 @@ public: * 空间复杂度:O(1) -## 贪心算法(方法一) +### 贪心算法(方法一) 直接从全局进行贪心选择,情况如下: @@ -134,7 +136,7 @@ public: 但不管怎么说,解法毕竟还是巧妙的,不用过于执着于其名字称呼。 -## 贪心算法(方法二) +### 贪心算法(方法二) 可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量rest[i]相加一定是大于等于零的。 @@ -633,3 +635,4 @@ object Solution { + From 1cdbefd8ef2a8803136b4e038889e7f91df6936c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:11:55 +0800 Subject: [PATCH 0634/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200135.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35.\345\210\206\345\217\221\347\263\226\346\236\234.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index cf3ccc8e90..d130bd6820 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -28,9 +28,9 @@ * 输出: 4 * 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -234,7 +234,7 @@ func findMax(num1 int, num2 int) int { } ``` -### Javascript: +### Javascript ```Javascript var candy = function(ratings) { let candys = new Array(ratings.length).fill(1) @@ -376,3 +376,4 @@ object Solution { + From 4f85769d51e99718f3c8ea53b1eafdfc3f218771 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:15:18 +0800 Subject: [PATCH 0635/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200860.=E6=9F=A0?= =?UTF-8?q?=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...7\240\346\252\254\346\260\264\346\211\276\351\233\266.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index 5046df121a..50a0c31a50 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -50,9 +50,9 @@ * 0 <= bills.length <= 10000 * bills[i] 不是 5 就是 10 或是 20  -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -403,3 +403,4 @@ object Solution { + From 1500bb9b3e289eb3d4a693b2efb9aa7f98ff2eb1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:17:26 +0800 Subject: [PATCH 0636/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200406.=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?UTF-8?q?=20=E6=8E=92=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 --- ...3\230\351\207\215\345\273\272\351\230\237\345\210\227.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 60d1fcab03..9cd78fac62 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -37,9 +37,9 @@ 题目数据确保队列可以被重建 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -402,3 +402,4 @@ object Solution { + From d31e23f5f629564076e184969c093cec7c273dcd Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:20:20 +0800 Subject: [PATCH 0637/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?UTF-8?q?=EF=BC=88vector=E5=8E=9F=E7=90=86=E8=AE=B2=E8=A7=A3)=20?= =?UTF-8?q?=E6=8E=92=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 --- ...0\206\350\256\262\350\247\243\357\274\211.md" | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 4f8cab8288..fdb4f58ba4 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -165,19 +165,9 @@ public: 相信在这里学习算法的录友们,都是想在软件行业长远发展的,都是要从事编程的工作,那么一定要深耕好一门编程语言,这个非常重要! - - - - ## 其他语言版本 - -Java: - - -Python: - -Rust: +### Rust ```rust // 版本二,使用list(链表) @@ -206,8 +196,7 @@ impl Solution{ } ``` - -Go: +### Go: Go中slice的`append`操作和C++中vector的扩容机制基本相同。 @@ -224,3 +213,4 @@ Go中slice的`append`操作和C++中vector的扩容机制基本相同。 + From eb320010a411c9420d83606314b0f4909f080d63 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:22:08 +0800 Subject: [PATCH 0638/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200452.=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95?= =?UTF-8?q?=E7=88=86=E6=B0=94=E7=90=83=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...56\255\345\274\225\347\210\206\346\260\224\347\220\203.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index ff476f4152..90cd7085d1 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -42,9 +42,9 @@ * points[i].length == 2 * -2^31 <= xstart < xend <= 2^31 - 1 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 2acb91c29165ee618334689298681b607348d2df Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:23:46 +0800 Subject: [PATCH 0639/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200435.=E6=97=A0?= =?UTF-8?q?=E9=87=8D=E5=8F=A0=E5=8C=BA=E9=97=B4=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...\207\215\345\217\240\345\214\272\351\227\264.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 61f42d4241..c307532e4f 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -30,9 +30,9 @@ * 输出: 0 * 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,依然是判断重叠区间 | LeetCode:435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,依然是判断重叠区间 | LeetCode:435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -89,9 +89,9 @@ public: 大家此时会发现如此复杂的一个问题,代码实现却这么简单! +## 补充 - -## 补充(1) +### 补充(1) 左边界排序可不可以呢? @@ -144,7 +144,7 @@ public: ``` -## 补充(2) +### 补充(2) 本题其实和[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像,弓箭的数量就相当于是非交叉区间的数量,只要把弓箭那道题目代码里射爆气球的判断条件加个等号(认为[0,1][1,2]不是相邻区间),然后用总区间数减去弓箭数量 就是要移除的区间数量了。 @@ -311,7 +311,7 @@ func min(a, b int) int { } ``` -### Javascript: +### Javascript - 按右边界排序 ```Javascript var eraseOverlapIntervals = function(intervals) { @@ -447,3 +447,4 @@ impl Solution { + From 481cd1d137ce6497f874c07af8e8c3d007a40bf7 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:25:25 +0800 Subject: [PATCH 0640/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200763.=E5=88=92?= =?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4=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 --- ...0\206\345\255\227\346\257\215\345\214\272\351\227\264.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 158f76d625..41314456c3 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -24,9 +24,9 @@ * S的长度在[1, 500]之间。 * S只包含小写字母 'a' 到 'z' 。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -409,3 +409,4 @@ impl Solution { + From bde75d3a98b857f533773609d825b6247c6dfcb6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:26:26 +0800 Subject: [PATCH 0641/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200056.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=8C=BA=E9=97=B4=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 8705f840e2..95781b1a1d 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -22,9 +22,9 @@ * 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。 * 注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -341,3 +341,4 @@ impl Solution { + From 3bbabf40845d916ddecaf7b9e1af60d8198bbc1b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:28:04 +0800 Subject: [PATCH 0642/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200738.=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97=20?= =?UTF-8?q?=E6=8E=92=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 --- ...42\236\347\232\204\346\225\260\345\255\227.md" | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index d88ebbb096..c2215cf633 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -26,12 +26,14 @@ 说明: N 是在 [0, 10^9] 范围内的一个整数。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +## 思路 -## 暴力解法 + +### 暴力解法 题意很简单,那么首先想的就是暴力解法了,来我替大家暴力一波,结果自然是超时! @@ -62,7 +64,7 @@ public: * 时间复杂度:O(n × m) m为n的数字长度 * 空间复杂度:O(1) -## 贪心算法 +### 贪心算法 题目要求小于等于N的最大单调递增的整数,那么拿一个两位的数字来举例。 @@ -120,7 +122,7 @@ public: ## 其他语言版本 -### Java: +### Java ```java 版本1 class Solution { @@ -163,7 +165,7 @@ class Solution { ``` -### Python: +### Python 暴力 ```python class Solution: @@ -395,3 +397,4 @@ impl Solution { + From 9a8f9780f63e01a8a64edc9c74a9833994d2662a Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:29:35 +0800 Subject: [PATCH 0643/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200968.=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...3\221\346\216\247\344\272\214\345\217\211\346\240\221.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index e2ba8ebffc..be04bd4755 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -38,9 +38,9 @@ * 给定树的节点数的范围是 [1, 1000]。 * 每个节点的值都是 0。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -732,3 +732,4 @@ impl Solution { + From f4a74a43d69c75f3e898c1900a989b8651e2456c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:31:11 +0800 Subject: [PATCH 0644/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=B4=AA?= =?UTF-8?q?=E5=BF=83=E7=AE=97=E6=B3=95=E6=80=BB=E7=BB=93=E7=AF=87=20?= =?UTF-8?q?=E6=8E=92=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 --- ...7\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" index d52e85513c..c375503d71 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" @@ -154,3 +154,4 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 + From 0ffbe67f5a3e8e3108e95a526f3e142faac13477 Mon Sep 17 00:00:00 2001 From: han Date: Tue, 25 Jul 2023 16:24:55 +0800 Subject: [PATCH 0645/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A059.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\227\213\347\237\251\351\230\265II.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index f03fcdad35..73e9e4daea 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -688,6 +688,58 @@ public class Solution { } ``` +### Ruby#: +```ruby +def generate_matrix(n) + result = Array.new(n) { Array.new(n, 0) } + #循环次数 + loop_times = 0 + #步长 + step = n - 1 + val = 1 + + + while loop_times < n / 2 + #模拟从左向右 + for i in 0..step - 1 + #行数不变,列数变 + result[loop_times][i+loop_times] = val + val += 1 + end + + #模拟从上到下 + for i in 0..step - 1 + #列数不变,行数变 + result[i+loop_times][n-loop_times-1] = val + val += 1 + end + + #模拟从右到左 + for i in 0..step - 1 + #行数不变,列数变 + result[n-loop_times-1][n-loop_times-i-1] = val + val += 1 + end + + #模拟从下到上 + for i in 0..step - 1 + #列数不变,行数变 + result[n-loop_times-i-1][loop_times] = val + val += 1 + end + + loop_times += 1 + step -= 2 + end + + #如果是奇数,则填充最后一个元素 + result[n/2][n/2] = n**2 if n % 2 + + return result + +end +``` +

From 6ed304e9cdb3f8bd3ef74220190f808ffbcab79d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:51:45 +0800 Subject: [PATCH 0646/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=20=E6=8E=92=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 --- ...210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" index a99c069002..bff26d1d34 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -12,7 +12,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 什么是动态规划 @@ -135,3 +135,4 @@ + From b295b12d1abdacd0e3ba09d6a9395d494ede0b42 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:53:34 +0800 Subject: [PATCH 0647/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200509.=E6=96=90?= =?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...220\346\263\242\351\202\243\345\245\221\346\225\260.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 7ace072334..0c073db5bb 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -32,9 +32,9 @@ F(n) = F(n - 1) + F(n - 2),其中 n > 1 * 0 <= n <= 30 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -278,7 +278,7 @@ class Solution: return self.fib(n - 1) + self.fib(n - 2) ``` -### Go: +### Go ```Go func fib(n int) int { if n < 2 { @@ -479,3 +479,4 @@ public class Solution + From 9b24f3cdefe2ee2ca10eb97c2cef4224cd864622 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:56:48 +0800 Subject: [PATCH 0648/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0070.\347\210\254\346\245\274\346\242\257.md" | 5 +++-- ...203\214\345\214\205\347\211\210\346\234\254.md" | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 1b24e49150..1a1f7e31b5 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -29,9 +29,9 @@ * 1 阶 + 2 阶 * 2 阶 + 1 阶 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -522,3 +522,4 @@ impl Solution { + diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 8c85985fba..4ca7a3710b 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -127,8 +127,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int climbStairs(int n) { @@ -148,7 +148,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -166,8 +166,8 @@ class Solution: return dp[n] ``` +### Go: -Go: ```go func climbStairs(n int) int { //定义 @@ -189,7 +189,8 @@ func climbStairs(n int) int { } ``` -JavaScript: +### JavaScript: + ```javascript var climbStairs = function(n) { const dp = new Array(n + 1).fill(0); @@ -206,7 +207,7 @@ var climbStairs = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript function climbStairs(n: number): number { @@ -226,7 +227,7 @@ function climbStairs(n: number): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -250,4 +251,3 @@ impl Solution { - From 86f794b11814568adac618f462076ad6b0d8edde Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:58:05 +0800 Subject: [PATCH 0649/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\261\350\264\271\347\210\254\346\245\274\346\242\257.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 9eaaa4e9fe..6fb518c371 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -37,9 +37,9 @@ * cost[i] 将会是一个整型数据,范围为 [0, 999] 。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ----------- @@ -523,3 +523,4 @@ public class Solution + From 0e3500ed1c7b0d6db94d33b6d5db2a39424ac94b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:01:47 +0800 Subject: [PATCH 0650/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92=20=E5=91=A8=E6=9C=AB=E6=80=BB?= =?UTF-8?q?=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + 4 files changed, 4 insertions(+) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index b4baa4adb0..831ce348a1 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列一) 这周我们正式开始动态规划的学习! diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index a77efa2fd2..71cb49a9db 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列二) ## 周一 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 8fd292ed9a..e74dd6bea8 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列六) 本周我们主要讲解了打家劫舍系列,这个系列也是dp解决的经典问题,那么来看看我们收获了哪些呢,一起来回顾一下吧。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index b814e1b39e..ec442a3979 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列七) 本周的主题就是股票系列,来一起回顾一下吧 From 126aaac841078b436f9d0e878a1bef1c0f8aeee1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:06:55 +0800 Subject: [PATCH 0651/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=83=8C?= =?UTF-8?q?=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=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 --- ...47\241\20001\350\203\214\345\214\205-1.md" | 22 +++++++++---------- ...47\241\20001\350\203\214\345\214\205-2.md" | 14 +++++++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index bd3191dca0..7511ac8715 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -8,8 +8,11 @@ # 动态规划:01背包理论基础 +## 算法公开课 -**《代码随想录》算法视频公开课:[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这周我们正式开始讲解背包问题! @@ -37,7 +40,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 之前可能有些录友已经可以熟练写出背包了,但只要把这个文章仔细看完,相信你会意外收获! -## 01 背包 +### 01 背包 有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 @@ -67,7 +70,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 以下讲解和图示中出现的数字都是以这个例子为例。 -## 二维dp数组01背包 +### 二维dp数组01背包 依然动规五部曲分析一波。 @@ -226,9 +229,6 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。 - -## 完整c++测试代码 - ```cpp void test_2_wei_bag_problem1() { vector weight = {1, 3, 4}; @@ -275,7 +275,7 @@ int main() { ## 其他语言版本 -### java +### Java ```java public class BagProblem { @@ -396,7 +396,8 @@ public class BagProblem { ``` -### python +### Python + 无参数版 ```python def test_2_wei_bag_problem1(): @@ -456,8 +457,7 @@ if __name__ == "__main__": ``` - -### go +### Go ```go func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { @@ -498,7 +498,7 @@ func main() { } ``` -### javascript +### Javascript ```js function testWeightBagProblem (weight, value, size) { diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index d481e044c5..019947e536 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -3,10 +3,13 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- # 动态规划:01背包理论基础(滚动数组) -**《代码随想录》算法视频公开课:[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 昨天[动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html)中是用二维dp数组来讲解01背包。 @@ -29,7 +32,7 @@ 问背包能背的物品最大价值是多少? -## 一维dp数组(滚动数组) +### 一维dp数组(滚动数组) 对于背包问题其实状态都是可以压缩的。 @@ -154,8 +157,6 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 -## 一维dp01背包完整C++测试代码 - ```CPP void test_1_wei_bag_problem() { vector weight = {1, 3, 4}; @@ -318,7 +319,7 @@ func main() { } ``` -### javaScript +### JavaScript ```js @@ -459,3 +460,4 @@ fn test_wei_bag_problem2() { + From fc19feb04939aee16372c6a6880a4a0ee3373110 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:13:26 +0800 Subject: [PATCH 0652/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200416.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E7=AD=89=E5=92=8C=E8=87=AA=E5=B7=B1=200474.=E4=B8=80?= =?UTF-8?q?=E5=92=8C=E9=9B=B6=200494.=E7=9B=AE=E6=A0=87=E5=92=8C=201049.?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84?= =?UTF-8?q?=E9=87=8D=E9=87=8FII=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\255\211\345\222\214\345\255\220\351\233\206.md" | 13 +++++++------ .../0474.\344\270\200\345\222\214\351\233\266.md" | 5 +++-- .../0494.\347\233\256\346\240\207\345\222\214.md" | 11 +++++------ ...44\264\347\232\204\351\207\215\351\207\217II.md" | 13 +++++++------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index a886b99a6d..0657c010bb 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -30,9 +30,9 @@ * 1 <= nums.length <= 200 * 1 <= nums[i] <= 100 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -53,7 +53,7 @@ * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -## 01背包问题 +### 01背包问题 背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 @@ -479,7 +479,7 @@ func canPartition(nums []int) bool { } ``` -### javaScript: +### JavaScript: ```js var canPartition = function(nums) { @@ -499,7 +499,7 @@ var canPartition = function(nums) { ``` -### Rust +### Rust: ```Rust impl Solution { @@ -681,7 +681,7 @@ function canPartition(nums: number[]): boolean { }; ``` -### Scala +### Scala: 滚动数组: ```scala @@ -730,3 +730,4 @@ object Solution { + diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 7c1206ef35..8f6197ac5c 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -34,9 +34,9 @@ * strs[i] 仅由 '0' 和 '1' 组成 * 1 <= m, n <= 100 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -538,3 +538,4 @@ impl Solution { + diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 1902d5ed44..4a4e966c14 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -37,9 +37,9 @@ * 初始的数组的和不会超过 1000 。 * 保证返回的最终结果能被 32 位整数存下。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -67,7 +67,7 @@ target是固定的,sum是固定的,left就可以求出来。 此时问题就是在集合nums中找出和为left的组合。 -## 回溯算法 +### 回溯算法 在回溯算法系列中,一起学过这道题目[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)的录友应该感觉很熟悉,这不就是组合总和问题么? @@ -118,7 +118,7 @@ public: 也可以使用记忆化回溯,但这里我就不在回溯上下功夫了,直接看动规吧 -## 动态规划 +### 动态规划 如何转化为01背包问题呢。 @@ -519,8 +519,6 @@ const findTargetSumWays = (nums, target) => { ### TypeScript -TypeScript: - ```ts function findTargetSumWays(nums: number[], target: number): number { // 把数组分成两个组合left, right.left + right = sum, left - right = target. @@ -590,3 +588,4 @@ impl Solution { + diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 932029ab7a..cc66131734 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -35,9 +35,9 @@ * 1 <= stones.length <= 30 * 1 <= stones[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -341,7 +341,7 @@ func max(a, b int) int { } ``` -### JavaScript +### JavaScript: ```javascript /** @@ -364,7 +364,7 @@ var lastStoneWeightII = function (stones) { }; ``` -### C +### C: ```c #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -413,7 +413,7 @@ function lastStoneWeightII(stones: number[]): number { }; ``` -### Scala +### Scala: 滚动数组: ```scala @@ -455,7 +455,7 @@ object Solution { } ``` -### Rust +### Rust: ```rust impl Solution { @@ -477,3 +477,4 @@ impl Solution { + 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 0653/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=200139.=E5=8D=95=E8=AF=8D=E6=8B=86=E5=88=86=200279.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E5=B9=B3=E6=96=B9=E6=95=B0=200322.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2=200377.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CI?= =?UTF-8?q?V=200518.=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2II=20=E5=A4=9A?= =?UTF-8?q?=E9=87=8D=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=20=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 --- ...25\350\257\215\346\213\206\345\210\206.md" | 23 ++++++++------- ...50\345\271\263\346\226\271\346\225\260.md" | 21 +++++++------- ...66\351\222\261\345\205\221\346\215\242.md" | 20 +++++++------ ...10\346\200\273\345\222\214\342\205\243.md" | 20 +++++++------ ...\351\222\261\345\205\221\346\215\242II.md" | 22 +++++++------- ...05\346\200\273\347\273\223\347\257\207.md" | 1 + ...32\351\207\215\350\203\214\345\214\205.md" | 10 +++---- ...14\345\205\250\350\203\214\345\214\205.md" | 29 ++++++++++--------- 8 files changed, 80 insertions(+), 66 deletions(-) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 0d88ba3681..d93288ae5a 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.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.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index 70ab8649fd..a0c138ee80 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.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.\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 1f3f4df27f..f32fd13e23 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" @@ -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.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index bd43d52657..d9699c5407 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.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.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 8da351148b..7c9f0fcefb 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.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/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" index c4e8cd9cff..9be93096fd 100644 --- "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" @@ -106,3 +106,4 @@ + diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index 1a856bf5fa..50c2e5bf8c 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.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/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 088a3d50e9..ac4c4a1cbe 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.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() { + From 89571ea8f867a22ca0ff69017ea8b741e965c3c4 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:33:10 +0800 Subject: [PATCH 0654/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200198.=E6=89=93?= =?UTF-8?q?=E5=AE=B6=E5=8A=AB=E8=88=8D=200213.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8DII=200337.=E6=89=93=E5=AE=B6=E5=8A=AB=E8=88=8DIII=20?= =?UTF-8?q?=E6=8E=92=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 --- ...223\345\256\266\345\212\253\350\210\215.md" | 18 +++++++++--------- ...3\345\256\266\345\212\253\350\210\215II.md" | 18 ++++++++++-------- ...\345\256\266\345\212\253\350\210\215III.md" | 5 +++-- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 80902559a6..a7bc4c998e 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -31,9 +31,9 @@ * 0 <= nums.length <= 100 * 0 <= nums[i] <= 400 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -121,8 +121,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java // 动态规划 class Solution { @@ -194,7 +194,7 @@ class Solution { } ``` -Python: +### Python: 1维DP ```python @@ -255,7 +255,8 @@ class Solution: ``` -Go: +### Go: + ```Go func rob(nums []int) int { n := len(nums) @@ -275,7 +276,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: ```javascript const rob = nums => { @@ -291,7 +292,7 @@ const rob = nums => { }; ``` -TypeScript: +### TypeScript: ```typescript function rob(nums: number[]): number { @@ -314,7 +315,7 @@ function rob(nums: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -338,4 +339,3 @@ impl Solution { - diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index ee62b5745e..cd9d596d70 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -31,9 +31,9 @@ * 1 <= nums.length <= 100 * 0 <= nums[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -104,8 +104,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int rob(int[] nums) { @@ -129,7 +129,7 @@ class Solution { } ``` -Python: +### Python: ```Python class Solution: @@ -219,7 +219,7 @@ class Solution: ``` -Go: +### Go: ```go // 打家劫舍Ⅱ 动态规划 @@ -257,7 +257,8 @@ func max(a, b int) int { } ``` -javascipt: +### JavaScript: + ```javascript var rob = function(nums) { const n = nums.length @@ -279,7 +280,7 @@ const robRange = (nums, start, end) => { return dp[end] } ``` -TypeScript: +### TypeScript: ```typescript function rob(nums: number[]): number { @@ -301,7 +302,7 @@ function robRange(nums: number[], start: number, end: number): number { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -336,3 +337,4 @@ impl Solution { + diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 1708b7a1e8..96d487e215 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -16,9 +16,9 @@ ![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -523,3 +523,4 @@ impl Solution { + From 6b322684a4ebc096d71d36846496501f6e3ffefa Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:55:12 +0800 Subject: [PATCH 0655/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BA=E7=B3=BB=E5=88=97=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\344\275\263\346\227\266\346\234\272.md" | 22 +++++++++---------- ...01\350\247\204\345\210\222\357\274\211.md" | 21 +++++++++--------- ...344\275\263\346\227\266\346\234\272III.md" | 17 +++++++------- ...\344\275\263\346\227\266\346\234\272IV.md" | 17 +++++++------- ...53\345\206\267\345\206\273\346\234\237.md" | 19 +++++++++------- ...01\350\247\204\345\210\222\357\274\211.md" | 21 ++++++++++-------- ...30\346\200\273\347\273\223\347\257\207.md" | 13 +---------- 7 files changed, 63 insertions(+), 67 deletions(-) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index 0630515662..cbdf40e85c 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -24,11 +24,9 @@ * 输出:0 解释:在这种情况下, 没有交易完成, 所以最大利润为 0。 -# 算法公开课 - -**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -202,7 +200,7 @@ public: ## 其他语言版本 -Java: +### Java: > 贪心法: @@ -294,8 +292,7 @@ class Solution { ``` - -Python: +### Python: > 贪心法: ```python @@ -351,7 +348,8 @@ class Solution: return dp1 ``` -Go: +### Go: + > 贪心法: ```Go func maxProfit(prices []int) int { @@ -418,7 +416,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 动态规划 @@ -454,7 +452,7 @@ var maxProfit = function(prices) { }; ``` -TypeScript: +### TypeScript: > 贪心法 @@ -492,7 +490,7 @@ function maxProfit(prices: number[]): number { }; ``` -C#: +### C#: > 贪心法 @@ -533,7 +531,7 @@ public class Solution } ``` -Rust: +### Rust: > 贪心 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 02f8d287d0..6e08b57c1d 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -34,9 +34,9 @@ * 1 <= prices.length <= 3 * 10 ^ 4 * 0 <= prices[i] <= 10 ^ 4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -133,8 +133,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // 动态规划 class Solution @@ -191,7 +191,7 @@ class Solution { } ``` -Python: +### Python: > 版本一: ```python @@ -221,7 +221,8 @@ class Solution: return dp[(length-1) % 2][1] ``` -Go: +### Go: + ```go // 买卖股票的最佳时机Ⅱ 动态规划 // 时间复杂度:O(n) 空间复杂度:O(n) @@ -250,7 +251,8 @@ func max(a, b int) int { } ``` -Javascript: +### JavaScript: + ```javascript // 方法一:动态规划(dp 数组) const maxProfit = (prices) => { @@ -290,7 +292,7 @@ const maxProfit = (prices) => { } ``` -TypeScript: +### TypeScript: > 动态规划 @@ -326,7 +328,7 @@ function maxProfit(prices: number[]): number { }; ``` -C#: +### C#: > 贪心法 @@ -363,7 +365,7 @@ public class Solution } ``` -Rust: +### Rust: > 贪心 @@ -414,4 +416,3 @@ impl Solution { - diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index a646b7d517..72dd90426c 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -39,9 +39,9 @@ * 1 <= prices.length <= 10^5 * 0 <= prices[i] <= 10^5 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -221,7 +221,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 版本一 @@ -277,7 +277,7 @@ class Solution { } ``` -Python: +### Python: > 版本一: ```python @@ -314,7 +314,7 @@ class Solution: return dp[4] ``` -Go: +### Go: ```go func maxProfit(prices []int) int { @@ -344,7 +344,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 版本一: @@ -383,7 +383,7 @@ const maxProfit = prices => { }; ``` -TypeScript: +### TypeScript: > 版本一 @@ -413,7 +413,7 @@ function maxProfit(prices: number[]): number { }; ``` -Rust: +### Rust: > 版本一 @@ -465,3 +465,4 @@ impl Solution { + diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 773a910a23..d4dc769893 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -31,9 +31,9 @@ * 0 <= prices.length <= 1000 * 0 <= prices[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -173,7 +173,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 版本一: 三维 dp数组 @@ -295,7 +295,7 @@ class Solution { } ``` -Python: +### Python: 版本一 @@ -329,7 +329,7 @@ class Solution: dp[j] = max(dp[j],dp[j-1]+prices[i]) return dp[2*k] ``` -Go: +### Go: 版本一: @@ -404,7 +404,7 @@ func max188(a, b int) int { } ``` -Javascript: +### JavaScript: ```javascript // 方法一:动态规划 @@ -454,7 +454,7 @@ var maxProfit = function(k, prices) { }; ``` -TypeScript: +### TypeScript: ```typescript function maxProfit(k: number, prices: number[]): number { @@ -474,7 +474,7 @@ function maxProfit(k: number, prices: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -529,3 +529,4 @@ impl Solution { + diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index cd71136ba6..f4093b67e6 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -20,9 +20,9 @@ * 输出: 3 * 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -174,7 +174,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -273,8 +273,9 @@ class Solution { } ``` -Python: +### Python: 版本一 + ```python from typing import List @@ -319,7 +320,8 @@ class Solution: return max(dp[-1][1], dp[-1][2]) ``` -Go: +### Go: + ```go // 最佳买卖股票时机含冷冻期 动态规划 // 时间复杂度O(n) 空间复杂度O(n) @@ -355,7 +357,7 @@ func max(a, b int) int { } ``` -Javascript: +### Javascript: ```javascript const maxProfit = (prices) => { @@ -397,7 +399,7 @@ const maxProfit = (prices) => { }; ``` -TypeScript: +### TypeScript: > 版本一,与本文思路一致 @@ -455,7 +457,7 @@ function maxProfit(prices: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -484,3 +486,4 @@ impl Solution { + diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 042de94718..7e8e3d7c61 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -32,9 +32,9 @@ * 0 < prices[i] < 50000. * 0 <= fee < 50000. -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -97,8 +97,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java /** * 卖出时支付手续费 @@ -173,9 +173,9 @@ class Solution { } } ``` -``` +### python + -Python: ```python class Solution: def maxProfit(self, prices: List[int], fee: int) -> int: @@ -188,7 +188,8 @@ class Solution: return max(dp[-1][0], dp[-1][1]) ``` -Go: +### Go: + ```go // 买卖股票的最佳时机含手续费 动态规划 // 时间复杂度O(n) 空间复杂度O(n) @@ -211,7 +212,8 @@ func max(a, b int) int { } ``` -Javascript: +### Javascript: + ```javascript const maxProfit = (prices,fee) => { let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); @@ -224,7 +226,7 @@ const maxProfit = (prices,fee) => { } ``` -TypeScript: +### TypeScript: ```typescript function maxProfit(prices: number[], fee: number): number { @@ -245,8 +247,9 @@ function maxProfit(prices: number[], fee: number): number { }; ``` -Rust: +### Rust: **贪心** + ```Rust impl Solution { pub fn max_profit(prices: Vec, fee: i32) -> i32 { diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index 2b04b7b085..4df21fb761 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -471,21 +471,10 @@ public: 「代码随想录」值得推荐给身边每一位学习算法的朋友同学们,关注后都会发现相见恨晚! -## 其他语言版本 - - -Java: - - -Python: - - -Go: - -

+ From 038d50957cff8e3e370a1812ff36afa5feb72945 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:58:53 +0800 Subject: [PATCH 0656/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200674.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97?= =?UTF-8?q?=20=E6=8E=92=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 --- ...07\345\255\220\345\272\217\345\210\227.md" | 24 +++++++++---------- ...22\345\242\236\345\272\217\345\210\227.md" | 18 +++++++------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index c58c3bf6c4..11cf13d9c5 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -33,7 +33,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -124,8 +124,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int lengthOfLIS(int[] nums) { @@ -147,7 +147,7 @@ class Solution { } ``` -Python: +### Python: DP ```python @@ -189,7 +189,8 @@ class Solution: return len(tails) # 返回递增子序列的长度 ``` -Go: +### Go: + ```go // 动态规划求解 func lengthOfLIS(nums []int) int { @@ -248,7 +249,8 @@ func lengthOfLIS(nums []int ) int { } ``` -Javascript +### Javascript: + ```javascript const lengthOfLIS = (nums) => { let dp = Array(nums.length).fill(1); @@ -267,7 +269,7 @@ const lengthOfLIS = (nums) => { }; ``` -TypeScript +### TypeScript: ```typescript function lengthOfLIS(nums: number[]): number { @@ -288,7 +290,8 @@ function lengthOfLIS(nums: number[]): number { }; ``` -Rust: +### Rust: + ```rust pub fn length_of_lis(nums: Vec) -> i32 { let mut dp = vec![1; nums.len() + 1]; @@ -307,13 +310,8 @@ pub fn length_of_lis(nums: Vec) -> i32 { - - - - - -

+ diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 8cc270ec64..0c5e64b34e 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -29,7 +29,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -157,8 +157,7 @@ public: ## 其他语言版本 - -Java: +### Java: > 动态规划: ```java @@ -207,7 +206,7 @@ public static int findLengthOfLCIS(int[] nums) { } ``` -Python: +### Python: DP ```python @@ -261,7 +260,8 @@ class Solution: return result ``` -Go: +### Go: + > 动态规划: ```go func findLengthOfLCIS(nums []int) int { @@ -302,7 +302,8 @@ func findLengthOfLCIS(nums []int) int { } ``` -Rust: +### Rust: + ```rust pub fn find_length_of_lcis(nums: Vec) -> i32 { if nums.is_empty() { @@ -320,7 +321,7 @@ pub fn find_length_of_lcis(nums: Vec) -> i32 { } ``` -Javascript: +### Javascript: > 动态规划: ```javascript @@ -363,7 +364,7 @@ const findLengthOfLCIS = (nums) => { }; ``` -TypeScript: +### TypeScript: > 动态规划: @@ -409,3 +410,4 @@ function findLengthOfLCIS(nums: number[]): number { + From 9b0c0f20dcc1fd75af00188a04886dee7adc8c60 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 16:07:52 +0800 Subject: [PATCH 0657/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200392.=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97=200718.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84=201035.=E4=B8=8D?= =?UTF-8?q?=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF=201143.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97=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 --- ...55\345\255\220\345\272\217\345\210\227.md" | 16 +++++++++------- ...15\345\255\220\346\225\260\347\273\204.md" | 18 +++++++++--------- ...70\344\272\244\347\232\204\347\272\277.md" | 17 +++++++++-------- ...61\345\255\220\345\272\217\345\210\227.md" | 19 ++++++++++++------- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index c10114c05a..6342a41f00 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -28,9 +28,9 @@ 两个字符串都只由小写字符组成。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -149,8 +149,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean isSubsequence(String s, String t) { @@ -174,7 +174,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def isSubsequence(self, s: str, t: str) -> bool: @@ -190,7 +191,7 @@ class Solution: return False ``` -JavaScript: +### JavaScript: ```javascript const isSubsequence = (s, t) => { @@ -213,7 +214,7 @@ const isSubsequence = (s, t) => { }; ``` -TypeScript: +### TypeScript: ```typescript function isSubsequence(s: string, t: string): boolean { @@ -237,7 +238,7 @@ function isSubsequence(s: string, t: string): boolean { }; ``` -Go: +### Go: ```go func isSubsequence(s string, t string) bool { @@ -266,3 +267,4 @@ func isSubsequence(s string, t string) bool { + diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 82bf4f59e2..18cc02407f 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -25,9 +25,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -126,7 +124,7 @@ public: * 时间复杂度:O(n × m),n 为A长度,m为B长度 * 空间复杂度:O(n × m) -## 滚动数组 +### 滚动数组 在如下图中: @@ -257,8 +255,8 @@ class Solution { ## 其他语言版本 +### Java: -Java: ```java // 版本一 class Solution { @@ -300,7 +298,7 @@ class Solution { } ``` -Python: +### Python: 2维DP ```python @@ -395,7 +393,8 @@ class Solution: ``` -Go: +### Go: + ```Go func findLength(A []int, B []int) int { m, n := len(A), len(B) @@ -442,7 +441,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 动态规划 @@ -489,7 +488,7 @@ const findLength = (nums1, nums2) => { } ``` -TypeScript: +### TypeScript: > 动态规划: @@ -544,3 +543,4 @@ function findLength(nums1: number[], nums2: number[]): number { + diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 7142d75c09..74e94c842f 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -19,7 +19,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -82,8 +82,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int maxUncrossedLines(int[] nums1, int[] nums2) { @@ -106,7 +106,8 @@ Java: } ``` -Python: +### Python: + ```python class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: @@ -120,8 +121,7 @@ class Solution: return dp[-1][-1] ``` - -Golang: +### Go: ```go func maxUncrossedLines(A []int, B []int) int { @@ -152,7 +152,7 @@ func max(a, b int) int { } ``` -Rust: +### Rust: ```rust pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { @@ -173,7 +173,7 @@ pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { } ``` -JavaScript: +### JavaScript: ```javascript const maxUncrossedLines = (nums1, nums2) => { @@ -196,7 +196,7 @@ const maxUncrossedLines = (nums1, nums2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function maxUncrossedLines(nums1: number[], nums2: number[]): number { @@ -224,3 +224,4 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { + diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 68269b87c5..260b085e14 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -39,7 +39,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -136,7 +136,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java /* @@ -207,8 +207,9 @@ class Solution { } ``` -Python: +### Python: 2维DP + ```python class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @@ -252,7 +253,8 @@ class Solution: ``` -Go: +### Go: + ```Go func longestCommonSubsequence(text1 string, text2 string) int { t1 := len(text1) @@ -283,7 +285,8 @@ func max(a,b int)int { ``` -Javascript: +### JavaScript: + ```javascript const longestCommonSubsequence = (text1, text2) => { let dp = Array.from(Array(text1.length+1), () => Array(text2.length+1).fill(0)); @@ -302,7 +305,7 @@ const longestCommonSubsequence = (text1, text2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function longestCommonSubsequence(text1: string, text2: string): number { @@ -326,7 +329,8 @@ function longestCommonSubsequence(text1: string, text2: string): number { }; ``` -Rust: +### Rust: + ```rust pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { let (n, m) = (text1.len(), text2.len()); @@ -353,3 +357,4 @@ pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + From a0edc60d1fd08db91138daf430a97894a354bd98 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 16:21:21 +0800 Subject: [PATCH 0658/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E8=B7=9D=E7=A6=BB=E7=B3=BB=E5=88=97=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 --- ...26\350\276\221\350\267\235\347\246\273.md" | 19 +++++++++++-------- ...04\345\255\220\345\272\217\345\210\227.md" | 18 ++++++++++-------- ...40\351\231\244\346\223\215\344\275\234.md" | 16 +++++++++------- ...11\346\255\245\351\223\272\345\236\253.md" | 8 ++------ 4 files changed, 32 insertions(+), 29 deletions(-) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 703e891311..1ed9a86083 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -40,8 +40,8 @@ exection -> execution (插入 'u') * 0 <= word1.length, word2.length <= 500 * word1 和 word2 由小写英文字母组成 -# 算法公开课 -**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -227,8 +227,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java public int minDistance(String word1, String word2) { int m = word1.length(); @@ -256,7 +256,8 @@ public int minDistance(String word1, String word2) { } ``` -Python: +### Python: + ```python class Solution: def minDistance(self, word1: str, word2: str) -> int: @@ -274,7 +275,8 @@ class Solution: return dp[-1][-1] ``` -Go: +### Go: + ```Go func minDistance(word1 string, word2 string) int { m, n := len(word1), len(word2) @@ -310,8 +312,8 @@ func Min(args ...int) int { } ``` +### Javascript: -Javascript: ```javascript const minDistance = (word1, word2) => { let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0)); @@ -338,7 +340,7 @@ const minDistance = (word1, word2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function minDistance(word1: string, word2: string): number { @@ -373,7 +375,7 @@ function minDistance(word1: string, word2: string): number { }; ``` -C: +### C: ```c @@ -405,3 +407,4 @@ int minDistance(char * word1, char * word2){ + diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 8c82880d58..d925c5dede 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -157,8 +157,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int numDistinct(String s, String t) { @@ -182,7 +182,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def numDistinct(self, s: str, t: str) -> int: @@ -200,7 +201,8 @@ class Solution: return dp[-1][-1] ``` -Python3: +### Python3: + ```python class SolutionDP2: """ @@ -234,7 +236,8 @@ class SolutionDP2: return dp[-1] ``` -Go: +### Go: + ```go func numDistinct(s string, t string) int { dp:= make([][]int,len(s)+1) @@ -259,8 +262,8 @@ func numDistinct(s string, t string) int { } ``` +### Javascript: -Javascript: ```javascript const numDistinct = (s, t) => { let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0)); @@ -283,7 +286,7 @@ const numDistinct = (s, t) => { }; ``` -TypeScript: +### TypeScript: ```typescript function numDistinct(s: string, t: string): number { @@ -312,9 +315,8 @@ function numDistinct(s: string, t: string): number { ``` - -

+ diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 48d15b0ba9..505a4e33ee 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -17,9 +17,9 @@ * 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea" -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列,还是为了编辑距离做铺垫 | LeetCode:583.两个字符串的删除操(https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[LeetCode:583.两个字符串的删除操](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -143,8 +143,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // dp数组中存储word1和word2最长相同子序列的长度 class Solution { @@ -215,8 +215,8 @@ class Solution { } ``` +### Python: -Python: ```python class Solution: def minDistance(self, word1: str, word2: str) -> int: @@ -234,7 +234,8 @@ class Solution: return dp[-1][-1] ``` -Go: +### Go: + ```go func minDistance(word1 string, word2 string) int { dp := make([][]int, len(word1)+1) @@ -267,7 +268,8 @@ func min(a, b int) int { return b } ``` -Javascript: +### Javascript: + ```javascript // 方法一 var minDistance = (word1, word2) => { @@ -309,7 +311,7 @@ var minDistance = function (word1, word2) { }; ``` -TypeScript: +### TypeScript: > dp版本一: diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" index b8adffc775..50287ce20b 100644 --- "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" +++ "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" @@ -163,8 +163,8 @@ else { ## 其他语言版本 +### Java: -Java: ```java class Solution { public int minDistance(String word1, String word2) { @@ -193,11 +193,6 @@ class Solution { } ``` -Python: - - -Go: - @@ -205,3 +200,4 @@ Go: + From 4760924db0d9d5f53719ed36fe0b3d201cfb3c6f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 16:25:35 +0800 Subject: [PATCH 0659/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200516.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97=200647.?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2=20=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...07\345\255\220\345\272\217\345\210\227.md" | 13 ++++++------ ...36\346\226\207\345\255\220\344\270\262.md" | 21 ++++++++++++------- ...22\346\200\273\347\273\223\347\257\207.md" | 1 + 3 files changed, 20 insertions(+), 15 deletions(-) diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index fcdd57b0f4..8092758389 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -152,8 +152,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java public class Solution { @@ -175,8 +174,7 @@ public class Solution { } ``` - -Python: +### Python: ```python class Solution: @@ -193,7 +191,7 @@ class Solution: return dp[0][-1] ``` -Go: +### Go: ```Go func longestPalindromeSubseq(s string) int { @@ -222,7 +220,7 @@ func longestPalindromeSubseq(s string) int { } ``` -Javascript: +### Javascript: ```javascript const longestPalindromeSubseq = (s) => { @@ -247,7 +245,7 @@ const longestPalindromeSubseq = (s) => { }; ``` -TypeScript: +### TypeScript: ```typescript function longestPalindromeSubseq(s: string): number { @@ -281,3 +279,4 @@ function longestPalindromeSubseq(s: string): number { + diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 084b9f74e6..fdf83736a0 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -26,11 +26,13 @@ 提示:输入的字符串长度不会超过 1000 。 -## 暴力解法 +## 思路 + +### 暴力解法 两层for循环,遍历区间起始位置和终止位置,然后还需要一层遍历判断这个区间是不是回文。所以时间复杂度:O(n^3) -## 动态规划 +### 动态规划 动规五部曲: @@ -187,7 +189,7 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(n^2) -## 双指针法 +### 双指针法 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 @@ -231,7 +233,7 @@ public: ## 其他语言版本 -Java: +### Java: 动态规划: @@ -337,7 +339,7 @@ class Solution { } ``` -Python: +### Python: > 动态规划: ```python @@ -390,7 +392,8 @@ class Solution: return res ``` -Go: +### Go: + ```Go func countSubstrings(s string) int { res:=0 @@ -416,7 +419,8 @@ func countSubstrings(s string) int { } ``` -Javascript +### Javascript: + > 动态规划 ```javascript const countSubstrings = (s) => { @@ -462,7 +466,7 @@ const countSubstrings = (s) => { } ``` -TypeScript: +### TypeScript: > 动态规划: @@ -524,3 +528,4 @@ function expandRange(s: string, left: number, right: number): number { + diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index 8a1531f8be..c86376d349 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -136,3 +136,4 @@ + From b3d1a88fde09afa258b64812cf776674a024c534 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 20:38:47 +0800 Subject: [PATCH 0660/1533] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\244\247\345\205\203\347\264\240II.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 3fd4b3b6db..a090f32c3e 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -266,6 +266,24 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` +Rust +```rust +impl Solution { + pub fn next_greater_elements(nums: Vec) -> Vec { + let mut ans = vec![-1; nums.len() * 2]; + let mut stack = vec![]; + let double = nums.repeat(2); + for (idx, &i) in double.iter().enumerate() { + while !stack.is_empty() && double[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + ans[pos] = i; + } + stack.push(idx); + } + ans.into_iter().take(nums.len()).collect() + } +} +```

From f3f549d317113718f61509a85937853d3a81a7f7 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 22:13:12 +0800 Subject: [PATCH 0661/1533] =?UTF-8?q?Update=200042.=E6=8E=A5=E9=9B=A8?= =?UTF-8?q?=E6=B0=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index db66095da2..833a7613e2 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -926,6 +926,56 @@ int trap(int* height, int heightSize) { * 空间复杂度 O(1) +Rust + +双指针 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let n = height.len(); + let mut max_left = vec![0; height.len()]; + let mut max_right = vec![0; height.len()]; + max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| { + let lmax = lm.max(height[idx]); + let rmax = rm.max(height[n - 1 - idx]); + *x = lmax; *y = rmax; + (lmax, rmax) + }); + height.iter().enumerate().fold(0, |acc, (idx, x)| { + let h = max_left[idx].min(max_right[idx]); + if h > 0 { h - x + acc } else { acc } + }) + } +} +``` + +单调栈 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let mut stack = vec![]; + let mut ans = 0; + for (right_pos, &right_h) in height.iter().enumerate() { + while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h { + let mid_pos = stack.pop().unwrap(); + if !stack.is_empty() { + let left_pos = *stack.last().unwrap(); + let left_h = height[left_pos]; + let top = std::cmp::min(left_h, right_h); + if top > height[mid_pos] { + ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32; + } + } + } + stack.push(right_pos); + } + ans + } +} +``` +

From 7aa2a3efea391bc30047c31d17b179fcdb99221d Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sun, 9 Jul 2023 14:30:01 +0800 Subject: [PATCH 0662/1533] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index f9a8350818..bc82a860cc 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -670,6 +670,61 @@ function largestRectangleArea(heights: number[]): number { ``` +Rust + +双指针预处理 +```rust + +impl Solution { + pub fn largest_rectangle_area(v: Vec) -> i32 { + let n = v.len(); + let mut left_smaller_idx = vec![-1; n]; + let mut right_smaller_idx = vec![n as i32; n]; + for i in 1..n { + let mut mid = i as i32 - 1; + while mid >= 0 && v[mid as usize] >= v[i] { + mid = left_smaller_idx[mid as usize]; + } + left_smaller_idx[i] = mid; + } + for i in (0..n-1).rev() { + let mut mid = i + 1; + while mid < n && v[mid] >= v[i] { + mid = right_smaller_idx[mid] as usize; + } + right_smaller_idx[i] = mid as i32; + } + let mut res = 0; + for (idx, &e) in v.iter().enumerate() { + res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); + } + dbg!(res) + } +} +``` + +单调栈 +```rust +impl Solution { + pub fn largest_rectangle_area1(mut v: Vec) -> i32 { + v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 + v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 + let mut res = 0; + let mut stack = vec![]; // 递增的栈 + for (idx, &e) in v.iter().enumerate() { + while !stack.is_empty() && v[*stack.last().unwrap()] > e { + let pos = stack.pop().unwrap(); + let prev_pos = *stack.last().unwrap(); + let s = (idx - prev_pos - 1) as i32 * v[pos]; + res = res.max(s); + } + stack.push(idx); + } + res + } +} +``` +

From 0dc6bb5669981d25069fc338566e73b3a8e97828 Mon Sep 17 00:00:00 2001 From: han Date: Tue, 25 Jul 2023 16:24:55 +0800 Subject: [PATCH 0663/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A059.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\227\213\347\237\251\351\230\265II.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index f03fcdad35..73e9e4daea 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -688,6 +688,58 @@ public class Solution { } ``` +### Ruby#: +```ruby +def generate_matrix(n) + result = Array.new(n) { Array.new(n, 0) } + #循环次数 + loop_times = 0 + #步长 + step = n - 1 + val = 1 + + + while loop_times < n / 2 + #模拟从左向右 + for i in 0..step - 1 + #行数不变,列数变 + result[loop_times][i+loop_times] = val + val += 1 + end + + #模拟从上到下 + for i in 0..step - 1 + #列数不变,行数变 + result[i+loop_times][n-loop_times-1] = val + val += 1 + end + + #模拟从右到左 + for i in 0..step - 1 + #行数不变,列数变 + result[n-loop_times-1][n-loop_times-i-1] = val + val += 1 + end + + #模拟从下到上 + for i in 0..step - 1 + #列数不变,行数变 + result[n-loop_times-i-1][loop_times] = val + val += 1 + end + + loop_times += 1 + step -= 2 + end + + #如果是奇数,则填充最后一个元素 + result[n/2][n/2] = n**2 if n % 2 + + return result + +end +``` +

From 3933b49cb36eaacb1fca0d40861386722b32347e Mon Sep 17 00:00:00 2001 From: han Date: Wed, 26 Jul 2023 17:21:15 +0800 Subject: [PATCH 0664/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A01002.=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6=20Ruby=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\347\224\250\345\255\227\347\254\246.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index a53148b313..81b07b3943 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -518,6 +518,54 @@ impl Solution { } ``` +Ruby: +```ruby +def common_chars(words) + result = [] + #统计所有字符串里字符出现的最小频率 + hash = {} + #初始化标识 + is_first = true + + words.each do |word| + #记录共同字符 + chars = [] + word.split('').each do |chr| + #第一个字符串初始化 + if is_first + chars << chr + else + #字母之前出现过的最小次数 + if hash[chr] != nil && hash[chr] > 0 + hash[chr] -= 1 + chars << chr + end + end + end + + is_first = false + #清除hash,更新字符最小频率 + hash.clear + chars.each do |chr| + if hash[chr] != nil + hash[chr] += 1 + else + hash[chr] = 1 + end + end + end + + #字符最小频率hash转换为字符数组 + hash.keys.each do |key| + for i in 0..hash[key] - 1 + result << key + end + end + + return result +end +``` +

From 3f0966382e37245b96e7967f95e725f37c2cbdc6 Mon Sep 17 00:00:00 2001 From: leslieCHUENGT <1278207976@qq.com> Date: Thu, 27 Jul 2023 09:56:18 +0800 Subject: [PATCH 0665/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00463=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E7=9A=84=E5=91=A8=E9=95=BF=20TypeScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\347\232\204\345\221\250\351\225\277.md" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 18f1d01eb2..35b89dcf87 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -300,6 +300,104 @@ var islandPerimeter = function(grid) { }; ``` +TypeScript: + +```typescript +/** + * 方法一:深度优先搜索(DFS) + * @param grid 二维网格地图,其中 grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域 + * @returns 岛屿的周长 + */ +function islandPerimeter(grid: number[][]): number { + // 处理特殊情况:网格为空或行列数为 0,直接返回 0 + if (!grid || grid.length === 0 || grid[0].length === 0) { + return 0; + } + + // 获取网格的行数和列数 + const rows = grid.length; + const cols = grid[0].length; + let perimeter = 0; // 岛屿的周长 + + /** + * 深度优先搜索函数 + * @param i 当前格子的行索引 + * @param j 当前格子的列索引 + */ + const dfs = (i: number, j: number) => { + // 如果当前位置超出网格范围,或者当前位置是水域(grid[i][j] === 0),则周长增加1 + if (i < 0 || i >= rows || j < 0 || j >= cols || grid[i][j] === 0) { + perimeter++; + return; + } + + // 如果当前位置已经访问过(grid[i][j] === -1),则直接返回 + if (grid[i][j] === -1) { + return; + } + + // 标记当前位置为已访问(-1),避免重复计算 + grid[i][j] = -1; + + // 继续搜索上、下、左、右四个方向 + dfs(i + 1, j); + dfs(i - 1, j); + dfs(i, j + 1); + dfs(i, j - 1); + }; + + // 遍历整个网格,找到第一个陆地格子(grid[i][j] === 1),并以此为起点进行深度优先搜索 + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === 1) { + dfs(i, j); + break; + } + } + } + + return perimeter; +} + +/** + * 方法二:遍历每个陆地格子,统计周长 + * @param grid 二维网格地图,其中 grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域 + * @returns 岛屿的周长 + */ +function islandPerimeter(grid: number[][]): number { + // 处理特殊情况:网格为空或行列数为 0,直接返回 0 + if (!grid || grid.length === 0 || grid[0].length === 0) { + return 0; + } + + // 获取网格的行数和列数 + const rows = grid.length; + const cols = grid[0].length; + let perimeter = 0; // 岛屿的周长 + + // 遍历整个网格 + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + // 如果当前格子是陆地(grid[i][j] === 1) + if (grid[i][j] === 1) { + perimeter += 4; // 周长先加上4个边 + + // 判断当前格子的上方是否也是陆地,如果是,则周长减去2个边 + if (i > 0 && grid[i - 1][j] === 1) { + perimeter -= 2; + } + + // 判断当前格子的左方是否也是陆地,如果是,则周长减去2个边 + if (j > 0 && grid[i][j - 1] === 1) { + perimeter -= 2; + } + } + } + } + + return perimeter; +} +```

From 7c9fcfe09d91fa4e5bc66ee6ab16fac7446426e0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:12:11 +0800 Subject: [PATCH 0666/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=20O(n)=E8=B6=85=E6=97=B6?= =?UTF-8?q?=20=E6=8B=8D=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 --- ...57\345\244\232\345\244\247\357\274\237.md" | 22 +++++-------------- ...50\350\277\231\351\207\214\357\274\201.md" | 16 ++++---------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index a488c0bad4..8be48f38c2 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -13,7 +13,7 @@ 计算机究竟1s可以执行多少次操作呢? 接下来探讨一下这个问题。 -# 超时是怎么回事 +## 超时是怎么回事 ![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117.png) @@ -25,7 +25,7 @@ 如果n的规模已经足够让O(n)的算法运行时间超过了1s,就应该考虑log(n)的解法了。 -# 从硬件配置看计算机的性能 +## 从硬件配置看计算机的性能 计算机的运算速度主要看CPU的配置,以2015年MacPro为例,CPU配置:2.7 GHz Dual-Core Intel Core i5 。 @@ -44,7 +44,7 @@ 所以我们的程序在计算机上究竟1s真正能执行多少次操作呢? -# 做个测试实验 +## 做个测试实验 在写测试程序测1s内处理多大数量级数据的时候,有三点需要注意: @@ -155,7 +155,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 -# 完整测试代码 +## 完整测试代码 ```CPP #include @@ -212,7 +212,7 @@ int main() { ``` -# 总结 +## 总结 本文详细分析了在leetcode上做题程序为什么会有超时,以及从硬件配置上大体知道CPU的执行速度,然后亲自做一个实验来看看O(n)的算法,跑一秒钟,这个n究竟是做大,最后给出不同时间复杂度,一秒内可以运算出来的n的大小。 @@ -220,17 +220,6 @@ int main() { 这样,大家应该对程序超时时候的数据规模有一个整体的认识了。 -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -238,3 +227,4 @@ Go: + diff --git "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index c479dddc67..95c7567c75 100644 --- "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -8,6 +8,8 @@ 所以重新整理的时间复杂度文章,正式和大家见面啦! +# 时间复杂度 + ## 究竟什么是时间复杂度 **时间复杂度是一个函数,它定性描述该算法的运行时间**。 @@ -145,7 +147,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 **当然这不是这道题目的最优解,我仅仅是用这道题目来讲解一下时间复杂度**。 -# 总结 +## 总结 本篇讲解了什么是时间复杂度,复杂度是用来干什么,以及数据规模对时间复杂度的影响。 @@ -157,17 +159,6 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 如果感觉「代码随想录」很不错,赶快推荐给身边的朋友同学们吧,他们发现和「代码随想录」相见恨晚! -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -175,3 +166,4 @@ Go: + From e0c5da76e6a616a1cf3da18059c59947a90f877d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:18:47 +0800 Subject: [PATCH 0667/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E6=A0=88=E7=B3=BB=E5=88=97=E9=A2=98=E7=9B=AE=20?= =?UTF-8?q?=E6=8E=92=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 --- ...2.\346\216\245\351\233\250\346\260\264.md" | 20 +++++++-------- ...47\347\232\204\347\237\251\345\275\242.md" | 25 ++++++++++--------- ...4\345\244\247\345\205\203\347\264\240I.md" | 17 +++++++------ ...\345\244\247\345\205\203\347\264\240II.md" | 19 ++++++++------ ...17\346\227\245\346\270\251\345\272\246.md" | 15 +++++------ 5 files changed, 53 insertions(+), 43 deletions(-) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 833a7613e2..4f0682ba6f 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -29,7 +29,7 @@ * 输出:9 -# 思路 +## 思路 接雨水问题在面试中还是常见题目的,有必要好好讲一讲。 @@ -39,7 +39,7 @@ * 动态规划 * 单调栈 -## 暴力解法 +### 暴力解法 本题暴力解法也是也是使用双指针。 @@ -137,7 +137,7 @@ public: 力扣后面修改了后台测试数据,所以以上暴力解法超时了。 -## 双指针优化 +### 双指针优化 在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。 @@ -184,7 +184,7 @@ public: }; ``` -## 单调栈解法 +### 单调栈解法 关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -194,7 +194,7 @@ public: 而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。 -### 准备工作 +#### 准备工作 那么本题使用单调栈有如下几个问题: @@ -248,7 +248,7 @@ stack st; // 存着下标,计算的时候用下标对应的柱子高度 明确了如上几点,我们再来看处理逻辑。 -### 单调栈处理逻辑 +#### 单调栈处理逻辑 以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -596,7 +596,7 @@ class Solution: ``` -### Go +### Go: ```go func trap(height []int) int { @@ -802,7 +802,7 @@ var trap = function(height) { }; ``` -### TypeScript +### TypeScript: 暴力解法: @@ -925,8 +925,7 @@ int trap(int* height, int heightSize) { * 时间复杂度 O(n) * 空间复杂度 O(1) - -Rust +### Rust: 双指针 @@ -980,3 +979,4 @@ impl Solution { + diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index bc82a860cc..4d949941cb 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -20,7 +20,7 @@ * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 -# 思路 +## 思路 本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解! @@ -28,7 +28,7 @@ 我们先来看一下暴力解法的解法: -## 暴力解法 +### 暴力解法 ```CPP class Solution { @@ -55,7 +55,7 @@ public: 如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。 -## 双指针解法 +### 双指针解法 本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。 @@ -98,7 +98,7 @@ public: }; ``` -## 单调栈 +### 单调栈 本地单调栈的解法和接雨水的题目是遥相呼应的。 @@ -169,7 +169,7 @@ public: } }; -``` +``` 细心的录友会发现,我在 height数组上后,都加了一个元素0, 为什么这么做呢? @@ -229,7 +229,7 @@ public: ## 其他语言版本 -Java: +### Java: 暴力解法: ```java @@ -335,7 +335,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -468,7 +468,7 @@ class Solution: ``` -Go: +### Go: > 单调栈 @@ -506,7 +506,8 @@ func largestRectangleArea(heights []int) int { ``` -JavaScript: +### JavaScript: + ```javascript //双指针 js中运行速度最快 var largestRectangleArea = function(heights) { @@ -581,7 +582,7 @@ var largestRectangleArea = function(heights) { return maxArea; }; ``` -TypeScript: +### TypeScript: > 暴力法(会超时): @@ -669,8 +670,7 @@ function largestRectangleArea(heights: number[]): number { }; ``` - -Rust +### Rust: 双指针预处理 ```rust @@ -730,3 +730,4 @@ impl Solution { + diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 411a47df9b..6bcafafba2 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -37,7 +37,7 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位 * nums1和nums2中所有整数 互不相同 * nums1 中的所有整数同样出现在 nums2 中 -# 思路 +## 思路 做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html) @@ -191,7 +191,8 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 -Java +### Java + ```java class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { @@ -248,7 +249,8 @@ class Solution { } } ``` -Python3: +### Python3 + ```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: @@ -269,7 +271,7 @@ class Solution: return result ``` -Go: +### Go > 未精简版本 ```go @@ -335,7 +337,7 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int { } ``` -JavaScript: +### JavaScript ```JS var nextGreaterElement = function (nums1, nums2) { @@ -358,7 +360,7 @@ var nextGreaterElement = function (nums1, nums2) { }; ``` -TypeScript: +### TypeScript ```typescript function nextGreaterElement(nums1: number[], nums2: number[]): number[] { @@ -387,7 +389,7 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` -Rust +### Rust ```rust impl Solution { @@ -419,3 +421,4 @@ impl Solution { + diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index a090f32c3e..023e4d7e0e 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -22,7 +22,7 @@ * -10^9 <= nums[i] <= 10^9 -# 思路 +## 思路 做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。 @@ -138,7 +138,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] nextGreaterElements(int[] nums) { @@ -162,7 +163,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: class Solution: @@ -196,7 +198,8 @@ class Solution: stack.append(i) return ans ``` -Go: +### Go: + ```go func nextGreaterElements(nums []int) []int { length := len(nums) @@ -218,7 +221,7 @@ func nextGreaterElements(nums []int) []int { } ``` -JavaScript: +### JavaScript: ```JS /** @@ -242,7 +245,7 @@ var nextGreaterElements = function (nums) { return res; }; ``` -TypeScript: +### TypeScript: ```typescript function nextGreaterElements(nums: number[]): number[] { @@ -266,7 +269,8 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` -Rust +### Rust: + ```rust impl Solution { pub fn next_greater_elements(nums: Vec) -> Vec { @@ -290,3 +294,4 @@ impl Solution { + diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index d2da37371a..fc1a80631c 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -211,8 +211,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java class Solution { @@ -270,7 +269,8 @@ class Solution { } ``` -Python: +### Python: + > 未精简版本 ```python @@ -307,7 +307,7 @@ class Solution: return answer ``` -Go: +### Go: > 暴力法 @@ -384,7 +384,7 @@ func dailyTemperatures(num []int) []int { } ``` -JavaScript: +### JavaScript: ```javascript // 版本一 @@ -429,7 +429,7 @@ var dailyTemperatures = function(temperatures) { }; ``` -TypeScript: +### TypeScript: > 精简版: @@ -455,7 +455,7 @@ function dailyTemperatures(temperatures: number[]): number[] { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -482,3 +482,4 @@ impl Solution { + From dc9fb7cb0bc7bff583e66bf3e3699af961f60a8c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:28:14 +0800 Subject: [PATCH 0668/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=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 --- ...13\350\275\254\346\225\260\347\273\204.md" | 15 ++++++------- ...3.\347\247\273\345\212\250\351\233\266.md" | 21 +++++++++---------- ...\345\272\217\346\225\260\347\273\204II.md" | 3 +-- ...61\350\204\211\346\225\260\347\273\204.md" | 17 ++++++++------- ...72\347\216\260\346\254\241\346\225\260.md" | 16 +++++++------- ...27\347\232\204\346\225\260\345\255\227.md" | 17 ++++++++------- 6 files changed, 46 insertions(+), 43 deletions(-) diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" index 3581969439..d60612e92b 100644 --- "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" +++ "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" @@ -33,7 +33,7 @@ 向右旋转 2 步: [3,99,-1,-100]。 -# 思路 +## 思路 这道题目在字符串里其实很常见,我把字符串反转相关的题目列一下: @@ -83,9 +83,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -106,7 +106,7 @@ class Solution { } ``` -## Python +### Python 方法一:局部翻转 + 整体翻转 ```python @@ -139,7 +139,7 @@ class Solution: # 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。 ``` -## Go +### Go ```go func rotate(nums []int, k int) { @@ -157,7 +157,7 @@ func reverse(nums []int){ } ``` -## JavaScript +### JavaScript ```js var rotate = function (nums, k) { @@ -178,7 +178,7 @@ var rotate = function (nums, k) { }; ``` -## TypeScript +### TypeScript ```typescript function rotate(nums: number[], k: number): void { @@ -205,3 +205,4 @@ function reverseByRange(nums: number[], left: number, right: number): void { + diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index 22d6428c10..ee3f429150 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -3,10 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -# 动态规划:一样的套路,再求一次完全平方数 - -# 283. 移动零 +# 283. 移动零:动态规划:一样的套路,再求一次完全平方数 [力扣题目链接](https://leetcode.cn/problems/move-zeroes/) @@ -22,7 +19,7 @@ 尽量减少操作次数。 -# 思路 +## 思路 做这道题目之前,大家可以做一做[27.移除元素](https://programmercarl.com/0027.移除元素.html) @@ -58,9 +55,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java public void moveZeroes(int[] nums) { @@ -77,7 +74,7 @@ public void moveZeroes(int[] nums) { } ``` -Python: +### Python: ```python def moveZeroes(self, nums: List[int]) -> None: @@ -100,7 +97,7 @@ Python: fast += 1 ``` -Go: +### Go: ```go func moveZeroes(nums []int) { @@ -116,7 +113,8 @@ func moveZeroes(nums []int) { } ``` -JavaScript: +### JavaScript: + ```javascript var moveZeroes = function(nums) { let slow = 0; @@ -133,7 +131,7 @@ var moveZeroes = function(nums) { }; ``` -TypeScript: +### TypeScript: ```typescript function moveZeroes(nums: number[]): void { @@ -159,3 +157,4 @@ function moveZeroes(nums: number[]): void { + diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" index c4654f16d1..72be8fa732 100644 --- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" +++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" @@ -147,8 +147,6 @@ class Solution { } ``` -### java - ```java //方法一:采用额外的数组空间 class Solution { @@ -384,3 +382,4 @@ function sortArrayByParityII(nums: number[]): number[] { + diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" index f43a210837..48c29eb479 100644 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" @@ -33,7 +33,7 @@ * 输出:true -# 思路 +## 思路 判断是山峰,主要就是要严格的保存左边到中间,和右边到中间是递增的。 @@ -71,9 +71,9 @@ public: 如果想系统学一学双指针的话, 可以看一下这篇[双指针法:总结篇!](https://programmercarl.com/双指针总结.html) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -101,7 +101,7 @@ class Solution { } ``` -## Python3 +### Python3 ```python class Solution: @@ -118,7 +118,7 @@ class Solution: ``` -## Go +### Go ```go func validMountainArray(arr []int) bool { @@ -142,7 +142,7 @@ func validMountainArray(arr []int) bool { } ``` -## JavaScript +### JavaScript ```js var validMountainArray = function(arr) { @@ -157,7 +157,7 @@ var validMountainArray = function(arr) { }; ``` -## TypeScript +### TypeScript ```typescript function validMountainArray(arr: number[]): boolean { @@ -177,7 +177,7 @@ function validMountainArray(arr: number[]): boolean { }; ``` -## C# +### C# ```csharp public class Solution { @@ -201,3 +201,4 @@ public class Solution { + diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index 1a7a001989..83ebbcb7bc 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -31,7 +31,7 @@ * -1000 <= arr[i] <= 1000 -# 思路 +## 思路 这道题目数组在是哈希法中的经典应用,如果对数组在哈希法中的使用还不熟悉的同学可以看这两篇:[数组在哈希法中的应用](https://programmercarl.com/0242.有效的字母异位词.html)和[哈希法:383. 赎金信](https://programmercarl.com/0383.赎金信.html) @@ -71,9 +71,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -97,7 +97,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: 数组在哈西法的应用 class Solution: @@ -133,10 +134,8 @@ class Solution: ``` +### JavaScript: -Go: - -JavaScript: ``` javascript // 方法一:使用数组记录元素出现次数 var uniqueOccurrences = function(arr) { @@ -171,7 +170,7 @@ var uniqueOccurrences = function(arr) { }; ``` -TypeScript: +### TypeScript: > 借用数组: @@ -209,3 +208,4 @@ function uniqueOccurrences(arr: number[]): boolean { + diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 7c26876957..c706ba216e 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -39,7 +39,7 @@ * 2 <= nums.length <= 500 * 0 <= nums[i] <= 100 -# 思路 +## 思路 两层for循环暴力查找,时间复杂度明显为$O(n^2)$。 @@ -113,9 +113,9 @@ public: 可以排序之后加哈希,时间复杂度为$O(n\log n)$ -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```Java public int[] smallerNumbersThanCurrent(int[] nums) { @@ -136,7 +136,8 @@ public int[] smallerNumbersThanCurrent(int[] nums) { } ``` -Python: +### Python: + ```python class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: @@ -151,7 +152,8 @@ class Solution: return res ``` -Go: +### Go: + ```go func smallerNumbersThanCurrent(nums []int) []int { // map,key[数组中出现的数] value[比这个数小的个数] @@ -180,7 +182,8 @@ func smallerNumbersThanCurrent(nums []int) []int { } ``` -JavaScript: +### JavaScript: + ```javascript // 方法一:使用哈希表记录位置 var smallerNumbersThanCurrent = function(nums) { @@ -217,7 +220,7 @@ var smallerNumbersThanCurrent = function(nums) { }; ``` -TypeScript: +### TypeScript: > 暴力法: From 56f37806cac317895ca1785b2e526343ff07426d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:32:16 +0800 Subject: [PATCH 0669/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88?= =?UTF-8?q?=E5=B8=8C=E8=A1=A8=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20?= =?UTF-8?q?=E6=8E=92=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 --- ...04\345\255\227\347\254\246\344\270\262.md" | 15 +++++----- ...36\346\226\207\351\223\276\350\241\250.md" | 1 + ...70\347\224\250\345\255\227\347\254\246.md" | 28 ++++++++++++------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" index a507638c35..e07ab746d9 100644 --- "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" @@ -29,7 +29,7 @@ 提示:可以假设 s 和 t 长度相同。 -# 思路 +## 思路 字符串没有说都是小写字母之类的,所以用数组不合适了,用map来做映射。 @@ -61,9 +61,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -87,7 +87,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -110,7 +110,7 @@ class Solution: return True ``` -## Go +### Go ```go func isIsomorphic(s string, t string) bool { @@ -132,7 +132,7 @@ func isIsomorphic(s string, t string) bool { } ``` -## JavaScript +### JavaScript ```js var isIsomorphic = function(s, t) { @@ -156,7 +156,7 @@ var isIsomorphic = function(s, t) { }; ``` -## TypeScript +### TypeScript ```typescript function isIsomorphic(s: string, t: string): boolean { @@ -183,3 +183,4 @@ function isIsomorphic(s: string, t: string): boolean { + diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" index 18b397e32f..fef942fc49 100644 --- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" +++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" @@ -432,3 +432,4 @@ function reverseList(head: ListNode | null): ListNode | null { + diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index a53148b313..1138d7fc2f 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -30,7 +30,7 @@ words[i] 由小写英文字母组成 -# 思路 +## 思路 这道题意一起就有点绕,不是那么容易懂,其实就是26个小写字符中有字符 在所有字符串里都出现的话,就输出,重复的也算。 @@ -140,7 +140,7 @@ public: ## 其他语言版本 -Java: +### Java: ```Java class Solution { @@ -174,7 +174,8 @@ class Solution { } } ``` -Python +### Python + ```python class Solution: def commonChars(self, words: List[str]) -> List[str]: @@ -218,7 +219,8 @@ class Solution: return l ``` -javaScript +### JavaScript + ```js var commonChars = function (words) { let res = [] @@ -285,7 +287,8 @@ var commonChars = function(words) { } ``` -TypeScript +### TypeScript + ```ts console.time("test") let str: string = "" @@ -321,7 +324,8 @@ TypeScript return str.split("") ``` -GO +### GO + ```golang func commonChars(words []string) []string { length:=len(words) @@ -357,7 +361,8 @@ func min(a,b int)int{ } ``` -Swift: +### Swift: + ```swift func commonChars(_ words: [String]) -> [String] { var res = [String]() @@ -397,7 +402,8 @@ func commonChars(_ words: [String]) -> [String] { } ``` -C: +### C: + ```c //若两个哈希表定义为char数组(每个单词的最大长度不会超过100,因此可以用char表示),可以提高时间和空间效率 void updateHashTable(int* hashTableOne, int* hashTableTwo) { @@ -449,7 +455,8 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){ return ret; } ``` -Scala: +### Scala: + ```scala object Solution { def commonChars(words: Array[String]): List[String] = { @@ -483,7 +490,7 @@ object Solution { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -522,3 +529,4 @@ impl Solution { + From bbb2a60f8a68bf03b466e3b567ada9cfcd06d24f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:37:16 +0800 Subject: [PATCH 0670/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20?= =?UTF-8?q?=E6=8E=92=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 --- ...70\345\220\214\347\232\204\346\240\221.md" | 21 +++++++++------- ...02\347\202\271\346\214\207\351\222\210.md" | 19 +++++++------- ...60\345\255\227\344\271\213\345\222\214.md" | 25 +++++++++++-------- ...21\345\217\230\345\271\263\350\241\241.md" | 18 +++++++------ 4 files changed, 47 insertions(+), 36 deletions(-) diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" index 96acacf61c..56a6c8840f 100644 --- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" +++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" @@ -19,7 +19,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png) -# 思路 +## 思路 在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。 @@ -115,7 +115,7 @@ public: 当然我可以把如上代码整理如下: -## 递归 +### 递归 ```CPP class Solution { @@ -134,7 +134,7 @@ public: }; ``` -## 迭代法 +### 迭代法 ```CPP class Solution { @@ -166,9 +166,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 递归法 @@ -205,7 +205,8 @@ class Solution { } } ``` -Python: +### Python: + ```python # 递归法 class Solution: @@ -236,7 +237,8 @@ class Solution: que.append(rightNode.right) return True ``` -Go: +### Go: + > 递归法 ```go func isSameTree(p *TreeNode, q *TreeNode) bool { @@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool { } ``` -JavaScript: +### JavaScript: > 递归法 @@ -296,7 +298,7 @@ var isSameTree = (p, q) => { }; ``` -TypeScript: +### TypeScript: > 递归法-先序遍历 @@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" index 31bb6822cc..003ef75afe 100644 --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -30,7 +30,7 @@ struct Node { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png) -# 思路 +## 思路 注意题目提示内容,: * 你只能使用常量级额外空间。 @@ -38,7 +38,7 @@ struct Node { 基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。 -## 递归 +### 递归 一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。 @@ -83,7 +83,7 @@ public: }; ``` -## 迭代(层序遍历) +### 迭代(层序遍历) 本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 @@ -114,9 +114,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 递归法 @@ -169,7 +169,7 @@ class Solution { } ``` -## Python +### Python ```python # 递归法 @@ -210,7 +210,7 @@ class Solution: nodePre.next = None # 本层最后一个节点指向None return root ``` -## Go +### Go ```go // 迭代法 func connect(root *Node) *Node { @@ -259,7 +259,7 @@ func connect(root *Node) *Node { } ``` -## JavaScript +### JavaScript ```js const connect = root => { @@ -287,7 +287,7 @@ const connect = root => { }; ``` -## TypeScript +### TypeScript (注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’) @@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null { + diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" index 445e108aa9..ebb36071cf 100644 --- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" +++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" @@ -10,7 +10,7 @@ [力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/) -# 思路 +## 思路 本题和[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii)是类似的思路,做完这道题,可以顺便把[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii) 和 [112.路径总和](https://programmercarl.com/0112.路径总和.html#_112-路径总和) 做了。 @@ -24,7 +24,7 @@ 那么先按递归三部曲来分析: -## 递归三部曲 +### 递归三部曲 如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html) @@ -116,7 +116,7 @@ path.pop_back(); // 回溯 ``` **把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。 -## 整体C++代码 +整体C++代码 关键逻辑分析完了,整体C++代码如下: @@ -162,16 +162,16 @@ public: }; ``` -# 总结 +## 总结 过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。 **我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!** -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -219,7 +219,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def sumNumbers(self, root: TreeNode) -> int: @@ -246,7 +247,7 @@ class Solution: backtrace(root) return res ``` -Go: +### Go: ```go func sumNumbers(root *TreeNode) int { @@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) { -JavaScript: +### JavaScript: + ```javascript var sumNumbers = function(root) { const listToInt = path => { @@ -315,7 +317,7 @@ var sumNumbers = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function sumNumbers(root: TreeNode | null): number { @@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number { }; ``` -C: +### C: ```c //sum记录总和 @@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){ + diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" index 0f81745c36..57e56b8fe3 100644 --- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" +++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" @@ -28,7 +28,7 @@ * 树节点的数目在 1 到 10^4 之间。 * 树节点的值互不相同,且在 1 到 10^5 之间。 -# 思路 +## 思路 这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。 @@ -71,9 +71,10 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 + +### Java: -Java: ```java class Solution { ArrayList res = new ArrayList(); @@ -99,7 +100,8 @@ class Solution { } } ``` -Python: +### Python: + ```python class Solution: def balanceBST(self, root: TreeNode) -> TreeNode: @@ -121,7 +123,7 @@ class Solution: traversal(root) return getTree(res, 0, len(res) - 1) ``` -Go: +### Go: ```go /** @@ -163,7 +165,8 @@ func balanceBST(root *TreeNode) *TreeNode { ``` -JavaScript: +### JavaScript: + ```javascript var balanceBST = function(root) { const res = []; @@ -188,7 +191,7 @@ var balanceBST = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function balanceBST(root: TreeNode | null): TreeNode | null { @@ -218,3 +221,4 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null + From e9b0d46f3535546063192c170606bffa8ff75a32 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:41:58 +0800 Subject: [PATCH 0671/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=B4=AA?= =?UTF-8?q?=E5=BF=83=E4=B8=8E=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=20?= =?UTF-8?q?=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=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 --- ...36\346\226\207\345\255\220\344\270\262.md" | 23 ++++++++++--------- ...\345\233\236\346\226\207\344\270\262II.md" | 13 ++++++----- ...a2\345\217\202\350\256\256\351\231\242.md" | 17 +++++++------- ...27\347\232\204\344\270\252\346\225\260.md" | 13 ++++++----- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index b1987b87b3..614f60514c 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -30,17 +30,17 @@ * 输出:"a" -# 思路 +## 思路 本题和[647.回文子串](https://programmercarl.com/0647.回文子串.html) 差不多是一样的,但647.回文子串更基本一点,建议可以先做647.回文子串 -## 暴力解法 +### 暴力解法 两层for循环,遍历区间起始位置和终止位置,然后判断这个区间是不是回文。 时间复杂度:O(n^3) -## 动态规划 +### 动态规划 动规五部曲: @@ -208,7 +208,7 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(n^2) -## 双指针 +### 双指针 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 @@ -258,9 +258,9 @@ public: -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 双指针 动态规划 @@ -327,7 +327,7 @@ class Solution { } ``` -Python: +### Python: ```python class Solution: @@ -377,7 +377,7 @@ class Solution: return s[start:end] ``` -Go: +### Go: ```go func longestPalindrome(s string) string { @@ -411,7 +411,7 @@ func longestPalindrome(s string) string { ``` -JavaScript: +### JavaScript: ```js //动态规划解法 @@ -527,7 +527,7 @@ var longestPalindrome = function(s) { }; ``` -C: +### C: 动态规划: ```c @@ -615,7 +615,7 @@ char * longestPalindrome(char * s){ } ``` -C#: +### C#: 動態規則: ```c# @@ -681,3 +681,4 @@ public class Solution { + diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" index 9b164dfbb5..eb91a1899f 100644 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -34,7 +34,7 @@ * 1 <= s.length <= 2000 * s 仅由小写英文字母组成 -# 思路 +## 思路 我们在讲解回溯法系列的时候,讲过了这道题目[回溯算法:131.分割回文串](https://programmercarl.com/0131.分割回文串.html)。 @@ -201,9 +201,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return dp[-1] ``` -## Go +### Go ```go func minCut(s string) int { @@ -330,7 +330,7 @@ func min(i, j int) int { } ``` -## JavaScript +### JavaScript ```js var minCut = function(s) { @@ -376,3 +376,4 @@ var minCut = function(s) { + diff --git "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" index a1420a66db..db6b43df8e 100644 --- "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" +++ "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" @@ -42,7 +42,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利。 -# 思路 +## 思路 这道题 题意太绕了,我举一个更形象的例子给大家捋顺一下。 @@ -70,7 +70,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 如果对贪心算法理论基础还不了解的话,可以看看这篇:[关于贪心算法,你该了解这些!](https://programmercarl.com/贪心算法理论基础.html) ,相信看完之后对贪心就有基本的了解了。 -# 代码实现 +## 代码实现 实现代码,在每一轮循环的过程中,去过模拟优先消灭身后的对手,其实是比较麻烦的。 @@ -111,9 +111,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -145,7 +145,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -173,7 +173,7 @@ class Solution: return "Radiant" if R else "Dire" ``` -## Go +### Go ```go @@ -214,7 +214,7 @@ func predictPartyVictory(senateStr string) string { } ``` -## JavaScript +### JavaScript ```js var predictPartyVictory = function(senateStr) { @@ -244,7 +244,7 @@ var predictPartyVictory = function(senateStr) { }; ``` -## TypeScript +### TypeScript ```typescript function predictPartyVictory(senate: string): string { @@ -287,3 +287,4 @@ function predictPartyVictory(senate: string): string { + diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" index da80a4e039..0277f24989 100644 --- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" +++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" @@ -24,7 +24,7 @@ * 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。 -# 思路 +## 思路 这道题可以说是 [300.最长上升子序列](https://programmercarl.com/0300.最长上升子序列.html) 的进阶版本 @@ -221,9 +221,9 @@ public: 还有O(nlog n)的解法,使用树状数组,今天有点忙就先不写了,感兴趣的同学可以自行学习一下,这里有我之前写的树状数组系列博客:https://blog.csdn.net/youngyangyang04/category_871105.html (十年前的陈年老文了) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return result; ``` -## Go +### Go ```go @@ -332,7 +332,7 @@ func findNumberOfLIS(nums []int) int { } ``` -## JavaScript +### JavaScript ```js var findNumberOfLIS = function(nums) { @@ -364,3 +364,4 @@ var findNumberOfLIS = function(nums) { + From a5eb340ee630091b199407bea8d3191b546708a0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:48:04 +0800 Subject: [PATCH 0672/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=BE?= =?UTF-8?q?=E8=AE=BA=20=E5=B9=B6=E6=9F=A5=E9=9B=86=20=E6=A8=A1=E6=8B=9F=20?= =?UTF-8?q?=E4=BD=8D=E8=BF=90=E7=AE=97=20=E9=A2=9D=E5=A4=96=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\200\344\270\252\346\216\222\345\210\227.md" | 13 +++++++------ ...15\225\350\257\215\346\216\245\351\276\231.md" | 15 +++++++-------- ...61\277\347\232\204\345\221\250\351\225\277.md" | 13 ++++++++----- ...77\224\345\233\236\345\216\237\347\202\271.md" | 15 ++++++++------- ...06\227\344\275\231\350\277\236\346\216\245.md" | 13 +++++++------ ...\227\344\275\231\350\277\236\346\216\245II.md" | 11 ++++++----- ...25\260\347\233\256\346\216\222\345\272\217.md" | 15 ++++++++------- 7 files changed, 51 insertions(+), 44 deletions(-) diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" index 34aa1086c0..3cfb673a29 100644 --- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" +++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" @@ -34,7 +34,7 @@ * 输出:[1] -# 思路 +## 思路 一些同学可能手动写排列的顺序,都没有写对,那么写程序的话思路一定是有问题的了,我这里以1234为例子,把全排列都列出来。可以参考一下规律所在: @@ -92,9 +92,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -159,7 +159,7 @@ class Solution { } ``` -## Python +### Python >直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数 ```python class Solution: @@ -191,7 +191,7 @@ class Solution: """ ``` -## Go +### Go ```go //卡尔的解法 @@ -216,7 +216,7 @@ func reverse(a []int,begin,end int){ } ``` -## JavaScript +### JavaScript ```js //卡尔的解法(吐槽一下JavaScript的sort和其他语言的不太一样,只想到了拷贝数组去排序再替换原数组来实现nums的[i + 1, nums.length)升序排序) @@ -272,3 +272,4 @@ var nextPermutation = function(nums) { + diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 20ad518295..97bc66d096 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -29,7 +29,7 @@ * 解释:endWord "cog" 不在字典中,所以无法进行转换。 -# 思路 +## 思路 以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。 @@ -97,9 +97,9 @@ public: 当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java public int ladderLength(String beginWord, String endWord, List wordList) { @@ -196,7 +196,7 @@ class Solution { } ``` -## Python +### Python ``` class Solution: @@ -221,7 +221,7 @@ class Solution: queue.append(newWord) return 0 ``` -## Go +### Go ```go func ladderLength(beginWord string, endWord string, wordList []string) int { wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0 @@ -274,7 +274,7 @@ func getCandidates(word string) []string { } ``` -## JavaScript +### JavaScript ```javascript var ladderLength = function(beginWord, endWord, wordList) { // 将wordList转成Set,提高查询速度 @@ -310,7 +310,7 @@ var ladderLength = function(beginWord, endWord, wordList) { }; ``` -## TypeScript +### TypeScript ```typescript function ladderLength( beginWord: string, @@ -364,4 +364,3 @@ function diffonechar(word1: string, word2: string): boolean { - diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 18f1d01eb2..14fa98dc10 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -90,7 +90,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 解法一 @@ -191,8 +191,8 @@ class Solution { ``` -Python: -### 解法1: +### Python: + 扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。 ```python @@ -228,7 +228,8 @@ class Solution: ``` -Go: +### Go: + ```go func islandPerimeter(grid [][]int) int { m, n := len(grid), len(grid[0]) @@ -249,7 +250,8 @@ func islandPerimeter(grid [][]int) int { } ``` -JavaScript: +### JavaScript: + ```javascript //解法一 var islandPerimeter = function(grid) { @@ -305,3 +307,4 @@ var islandPerimeter = function(grid) { + diff --git "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" index ab4bf1525c..f0d3339173 100644 --- "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" +++ "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" @@ -29,7 +29,7 @@ -# 思路 +## 思路 这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法啥的,哈哈。 @@ -64,9 +64,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 时间复杂度:O(n) @@ -86,7 +86,7 @@ class Solution { } ``` -## Python +### Python ```python # 时间复杂度:O(n) @@ -107,7 +107,7 @@ class Solution: return x == 0 and y == 0 ``` -## Go +### Go ```go func judgeCircle(moves string) bool { @@ -131,7 +131,7 @@ func judgeCircle(moves string) bool { } ``` -## JavaScript +### JavaScript ```js // 时间复杂度:O(n) @@ -150,7 +150,7 @@ var judgeCircle = function(moves) { ``` -## TypeScript +### TypeScript ```ts var judgeCircle = function (moves) { @@ -185,3 +185,4 @@ var judgeCircle = function (moves) { + diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index c4d62d9b46..8124cc7eea 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -25,7 +25,7 @@ * edges 中无重复元素 * 给定的图是连通的  -# 思路 +## 思路 这道题目也是并查集基础题目。 @@ -150,9 +150,9 @@ public: 可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -205,7 +205,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -256,7 +256,7 @@ class Solution: return [] ``` -## Go +### Go ```go @@ -312,7 +312,7 @@ func findRedundantConnection(edges [][]int) []int { } ``` -## JavaScript +### JavaScript ```js const n = 1005; @@ -365,3 +365,4 @@ var findRedundantConnection = function(edges) { + diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 8c56afdc08..31b2ad247e 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -213,9 +213,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java @@ -335,7 +335,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -426,7 +426,7 @@ class Solution: return self.getRemoveEdge(edges) ``` -## Go +### Go ```go @@ -527,7 +527,7 @@ func findRedundantDirectedConnection(edges [][]int) []int { ``` -## JavaScript +### JavaScript ```js const N = 1010; // 如题:二维数组大小的在3到1000范围内 @@ -623,3 +623,4 @@ var findRedundantDirectedConnection = function(edges) { + diff --git "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" index b898b7f25c..cc7a7007c7 100644 --- "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" +++ "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" @@ -46,7 +46,7 @@ -# 思路 +## 思路 这道题其实是考察如何计算一个数的二进制中1的数量。 @@ -87,7 +87,7 @@ int bitCount(int n) { 下面我就使用方法二,来做这道题目: -## C++代码 + ```C++ class Solution { @@ -116,9 +116,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -151,7 +151,7 @@ class Solution { -## Python +### Python ```python class Solution: @@ -167,7 +167,7 @@ class Solution: return count ``` -## Go +### Go ```go func sortByBits(arr []int) []int { @@ -205,7 +205,7 @@ func bitCount(n int) int { } ``` -## JavaScript +### JavaScript ```js var sortByBits = function(arr) { @@ -227,3 +227,4 @@ var sortByBits = function(arr) { + From 9693ad4e456bf583b96e1f8a5cb965ee0f6b4f63 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 27 Jul 2023 15:40:30 +0800 Subject: [PATCH 0673/1533] Update --- README.md | 25 +- problems/qita/join.md | 249 ++++++++++++++++++ ...16\346\234\254\351\241\271\347\233\256.md" | 15 -- ...72\344\272\214\345\217\211\346\240\221.md" | 2 + ...CM\346\250\241\345\274\217\357\274\237.md" | 4 + ...43\347\240\201\351\243\216\346\240\274.md" | 2 +- 6 files changed, 263 insertions(+), 34 deletions(-) create mode 100644 problems/qita/join.md delete mode 100644 "problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" diff --git a/README.md b/README.md index dd70d8cb6b..e5eb97bc7a 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ > 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) > 2. **正式出版** :[《代码随想录》](https://programmercarl.com/other/publish.html) 。 > 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 -> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 +> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.programmercarl.com/other/gongkaike.html) 。 > 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 > 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 > 7. **学习社区** :一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 -> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。 +> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://www.programmercarl.com/qita/join.html)了解提交代码的方式。 > 9. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境! @@ -51,19 +51,12 @@ ## 如何使用该刷题攻略 -电脑端还看不到留言,大家可以在公众号[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png),左下角有「刷题攻略」,这是手机版刷题攻略,看完就会发现有很多录友(代码随想录的朋友们)在文章下留言打卡,这份刷题顺序和题解已经陪伴了上万录友了,同时也说明文章的质量是经过上万人的考验! - -欢迎每一位学习算法的小伙伴加入到这个学习阵营来! - -**目前已经更新了,数组-> 链表-> 哈希表->字符串->栈与队列->树->回溯->贪心,八个专题了,正在讲解动态规划!** +按照先面的排列顺序,从数组开始刷起就可以了,顺序都安排好了,按顺序刷就好。 在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,最这个专题的归纳总结。 如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 - -目前「代码随想录」刷题攻略更新了:**200多篇文章,精讲了200道经典算法题目,共60w字的详细图解,大部分题目都搭配了20分钟左右的视频讲解**,视频质量很好,口碑很好,大家可以去看看,视频列表:[代码随想录视频讲解](https://www.bilibili.com/video/BV1fA4y1o715)。 - **这里每一篇题解,都是精品,值得仔细琢磨**。 我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。 @@ -100,14 +93,11 @@ * [程序员应该用什么用具来写文档?](./problems/前序/程序员写文档工具.md) * 求职 + * [ACM模式练习网站,卡码网](https://kamacoder.com/) * [程序员的简历应该这么写!!(附简历模板)](./problems/前序/程序员简历.md) + * [【专业技能】应该这样写!](https://programmercarl.com/other/jianlizhuanye.html) + * [【项目经历】应该这样写!](https://programmercarl.com/other/jianlixiangmu.html) * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) - * [北京有这些互联网公司,你都知道么?](./problems/前序/北京互联网公司总结.md) - * [上海有这些互联网公司,你都知道么?](./problems/前序/上海互联网公司总结.md) - * [深圳有这些互联网公司,你都知道么?](./problems/前序/深圳互联网公司总结.md) - * [广州有这些互联网公司,你都知道么?](./problems/前序/广州互联网公司总结.md) - * [成都有这些互联网公司,你都知道么?](./problems/前序/成都互联网公司总结.md) - * [杭州有这些互联网公司,你都知道么?](./problems/前序/杭州互联网公司总结.md) * 算法性能分析 * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) @@ -506,7 +496,7 @@ # 关于作者 -大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 +大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。 @@ -527,4 +517,3 @@
- diff --git a/problems/qita/join.md b/problems/qita/join.md new file mode 100644 index 0000000000..3b3e2d4029 --- /dev/null +++ b/problems/qita/join.md @@ -0,0 +1,249 @@ + + +# 如何在Github上提交PR(pull request) + + +* 如何提交代码 +* 合入不规范 + * 提交信息不规范 + * Markdown 代码格式 + * pull request里的commit数量 + * 代码注释 + * 说明具体是哪种方法 + * 代码规范 + * 代码逻辑 + * 处理冲突 + +以下在 [https://github.com/youngyangyang04/leetcode-master](https://github.com/youngyangyang04/leetcode-master) 上提交pr为为例 + +## 如何合入代码 + +首先来说一说如何合入代码,不少录友还不太会使用github,所以这里也做一下科普。 + +我特意申请一个新的Github账号,给大家做一个示范。 + +需要强调一下,一个commit就只更新一道题目,不要很多题目一起放在一个commit里,那样就很乱。 + +首先LeetCode-Master每天都有更新,如何保持你fork到自己的仓库是最新的版本呢。 + +点击这里Fetch upstream。 + +
+ +点击之后,这里就会显示最新的信息了 +
+ +注意这时是你的远端仓库为最新版本,本地还不是最新的,本地要git pull一下。 + +基于最新的版本,大家在去提交代码。 + +如何提交代码呢,首先把自己的代码提交到自己的fork的远端仓库中,然后open pull request,如图: + +
+ +点击 open pull request之后,就是如下画面,一个pull request有多个commit。 + +
+ +然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 + +
+ +此时你就提交成功了,我会在项目中的pull requests 处理列表里看到你的请求。 +
+ +然后如果你发现自己的代码没有合入多半是有问题,如果有问题都有会在pull request里给出留言的, + +## 注意事项 + +### 提交信息不规范 + + +大家提交代码的时候有两个地方需要写备注,一个是commit,一个是pull request,pull request包好多个commit。 + +commit 说清楚本文件多了哪些修改,而pull request则是对本次合入的所有commit做一个总结性描述。 + +commit备注,举例:添加Rust python3,那么commit备注就是:添加0001两数之和 Rust python3 版本 + +而pull request 如果只有一个commit,那么就也是:添加0001两数之和 Rust python3 版本 + +如果是多个commit ,则把本次commit都描述一遍。 + +### Markdown 语法 + +关于 Markdown 代码格式,例如 添加C++代码,需要有代码块语法 + +\`\`\`C++ +C++代码 +\`\`\` + +例如这个commit,在添加java代码的时候,就直接添加代码 +
+ +正确的格式应该是这样: +
+ +一般发现问题,我也会在代码中给出评论: + +
+ +这样大家也可以学习一些 提交代码的规范方面的知识 + + +有的录友 是添加的代码块语法,但没有标记是哪种语言,这样的话 代码就不会针对某种语言高亮显示了,也比较影响阅读,例如: + +
+ +提交python代码的话,要注释好,是python2还是python3 + +例如这样: + +
+ +当然python2的话,只这么写就行 + +\`\`\`python +python代码 +\`\`\` + +### pull request里的commit数量 + + +有的录友是一个pull request 里有很多commit (一个commit是一道题目的代码)。 + +有的录友是一个pull request 里有有一个commit。 + +
+ +其实如果大家是平时一天写了两三道题目的话,那么分三个commit,一个pull request提交上来就行。 + +一个pull request 一个commit也可以,这样大家就会麻烦一点。 + +但注意一个pull request也不要放太多的commit,一旦有一道题目代码不合格,我没有合入,就这个pull request里影响其他所有代码的合入了。 + +### 代码注释 + +提交的代码最好要有注释,这样也方便读者理解。 + +例如这位录友,在提交Java代码的时候,按照题解的意思对Java版本的代码进行的注释,这就很棒👍 + +
+ +
+ +当然如果大家感觉 已有的代码 不符合以上要求的话,例如 代码思路不够清晰不够规范,注释不够友好,依然欢迎提交优化代码,要记得详细注释哦。 + +
+ +### 说明具体是哪种方法 + +有的题解有两种甚至三四种解法,在添加代码的时候,注释上也清楚具体是哪一种方法的版本。 + +下面这位录友做的就很好 + +
+ + +
+ +有的题解,是一起给出了多道题目的讲解,例如项目中0102.二叉树的层序遍历.md 中有八道题目,那么大家添加代码的时候 应该在代码注释上,或者 直接写上 是哪个题目的代码。 + + +### 代码规范 + + +大家提交代码要规范,当然代码可以在力扣上运行通过是最基本的。 + +虽然我主张没有绝对正确的代码风格,但既然是给LeetCode-Master提交代码,尽量遵循Google编程规范。 + +经常看我的代码的录友应该都知道,我的代码格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 + +大家提交代码的时候遇到规范性问题,例如哪里应该由空格,哪里没有空格,可以参考我的代码来。 + +有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 + +
+ +**代码规范从你我做起!** + + +### 代码逻辑 + +**提交的代码要按照题解思路来写**。 + +虽然大家自己发挥想象空间是好的,但是题解还是要一脉相承,读者看完题解,发现代码和题解不是一个思路的话,那和重新读代码有啥区别了。 + +所以和题解不是一个思路的代码,除非详细注释了自己的思路 或者 写一段自己代码的描述说明思路和优化的地方,否则我就不会通过合入了哈。 + +大家的代码 最好也将关键地方放上注释,这样有助于别人快速理解你的代码。 + + +### 处理冲突 + +在合入的过程中还要处理冲突的代码, 理解大家代码的思路,解决冲突,然后在力扣提交一下,确保是没问题。 + +例如同一道题目, 一位录友提交了, 我还没处理如何,另一位录友也对这道题也提交了代码,这样就会发生冲突 +
+ +大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。 +
+ +一天晚分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] + +那么在处理冲突的时候 保留谁的代码,删点谁的代码呢? + +我一定是看谁 代码逻辑和题解一致,代码风格好,注释友好,就保留谁的。 + +所以例如当你想提交Java代码的时候,即使发现该题解已经有Java版本了,只要你的代码写的好,一样可以提交,我评审合格一样可以合入代码库。 + + +### 不要做额外修改 + +确保这种额外文件不要提交。 + +
+ +还有添加不同方法的时候,直接用正文格式写,哪种方法就可以了,不要添加目录 ,例如这样,这样整篇文章目录结构就有影响了。 + +
+ +前面不要加 `## 前序遍历(迭代法)`,直接写`前序遍历(迭代法)`就可以了。 + +当然这里也没有给代码块标记上对应的语言,应该是 + +\`\`\` Go +Go语言代码 +\`\`\` + + +## 对代码保持敬畏 + +有的录友甚至提交的代码并不是本题的代码,虽然我是鼓励大家提交代码的,但是大家贡献代码的时候也要对 自己的代码有敬畏之心,自己的代码是要给很多读者看的。 + +* 代码运行无误 +* 写的够不够简洁 +* 注释清不清晰 +* 备注规不规范 + +这也是培养大家以后协调工作的一种能力。 + +## 优化 + +目前 leetcode-master中大部分题解已经补充了其他语言,但如果你发现了可以优化的地方,依然可以提交PR来优化。 + +甚至发现哪里有语病,也欢迎提交PR来修改,例如下面:就是把【下表】 纠正为【下标】 + +
+ +不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 + +
+ +## 特别注意 + +git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 + +提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 + + + diff --git "a/problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" "b/problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" deleted file mode 100644 index 76a2c61e8a..0000000000 --- "a/problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" +++ /dev/null @@ -1,15 +0,0 @@ - -优化已有代码 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210821161813.png) - -**push代码之前 一定要 先pull最新代码**,否则提交的pr可能会有删除其他录友代码的操作。 - -一个pr 不要修改过多文件,因为一旦有一个 文件修改有问题,就不能合入,影响其他文件的合入了。 - -git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 - -提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 - - -不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210927113149.png) diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" index b64464031f..48781eda88 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" @@ -1,6 +1,8 @@ # 力扣上如何自己构造二叉树输入用例? +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + 经常有录友问,二叉树的题目中输入用例在ACM模式下应该怎么构造呢? 力扣上的题目,输入用例就给了一个数组,怎么就能构造成二叉树呢? diff --git "a/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" "b/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" index 0b9d230ff9..0012f88e67 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" @@ -5,6 +5,10 @@ 什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。 + +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + + 而力扣上是核心代码模式,就是把要处理的数据都已经放入容器里,可以直接写逻辑,例如这样: ```CPP diff --git "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" index b508346080..8fd8b40ca9 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" @@ -29,7 +29,7 @@ 这里我简单说一说规范问题。 -**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:googlec++编程规范,就可以领取。(涉及到微信后台的回复,没更改) +**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:编程规范,就可以领取。 **具体的规范要以自己团队风格为主**,融入团队才是最重要的。 From 7ee623f4da4e6615f867d2456a6ec174362f0100 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 27 Jul 2023 16:27:19 +0800 Subject: [PATCH 0674/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00349.=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20Ruby?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\344\272\244\351\233\206.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 8daf5a35ba..26a9286dc9 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -465,6 +465,25 @@ object Solution { } ``` + +###Ruby +```ruby +def intersection(nums1, nums2) + hash = {} + result = {} + + nums1.each do |num| + hash[num] = 1 if hash[num].nil? + end + + nums2.each do |num| + #取nums1和nums2交集 + result[num] = 1 if hash[num] != nil + end + + return result.keys +end +``` ## 相关题目 * [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/) From c6cf76bc00dfcb53a89115fe58ce0dde99390a53 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 28 Jul 2023 10:31:10 +0800 Subject: [PATCH 0675/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=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 --- ...44\346\225\260\344\271\213\345\222\214.md" | 3 +- ...73\351\231\244\345\205\203\347\264\240.md" | 9 ++- ...47\345\255\220\345\272\217\345\222\214.md" | 14 ++-- ...72\346\227\213\347\237\251\351\230\265.md" | 2 +- ...\346\227\213\347\237\251\351\230\265II.md" | 2 +- ...15\345\220\214\350\267\257\345\276\204.md" | 4 +- ...\345\220\214\350\267\257\345\276\204II.md" | 4 +- "problems/0090.\345\255\220\351\233\206II.md" | 1 + ...11\346\220\234\347\264\242\346\240\221.md" | 4 +- ...14\347\232\204\345\215\225\350\257\215.md" | 3 +- ...\344\275\263\346\227\266\346\234\272IV.md" | 1 - ...7.\345\271\277\346\220\234\347\211\210.md" | 2 +- ...3.\347\247\273\345\212\250\351\233\266.md" | 1 + ...11\346\216\222\350\241\214\347\250\213.md" | 3 +- ...64\346\225\260\346\213\206\345\210\206.md" | 5 +- ...11\345\222\214\345\255\220\351\233\206.md" | 3 +- ...\345\244\247\345\205\203\347\264\240II.md" | 1 - ...66\344\272\214\345\217\211\346\240\221.md" | 3 +- ...00\345\244\247\351\235\242\347\247\257.md" | 23 +++--- ...14\345\210\206\346\237\245\346\211\276.md" | 5 +- ...53\346\211\213\347\273\255\350\264\271.md" | 5 +- ...75\347\232\204\350\267\257\345\276\204.md" | 11 +-- ...47\344\272\272\345\267\245\345\262\233.md" | 7 +- ...04\345\255\227\347\254\246\344\270\262.md" | 7 +- ...55\345\277\203\350\212\202\347\202\271.md" | 3 +- ...30\345\234\250\350\267\257\345\276\204.md" | 1 + ...11\346\255\245\351\223\272\345\236\253.md" | 1 - ...54\345\255\227\347\254\246\344\270\262.md" | 5 +- ...22\346\200\273\347\273\223\347\257\207.md" | 3 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 + ...37\345\210\227\346\200\273\347\273\223.md" | 1 + ...06\350\256\262\350\247\243\357\274\211.md" | 3 +- ...27\346\263\225\346\250\241\346\235\277.md" | 73 ++++++++++--------- ...47\241\20001\350\203\214\345\214\205-2.md" | 2 + ...50\346\200\273\347\273\223\347\257\207.md" | 1 + 35 files changed, 116 insertions(+), 102 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index bf1e173ec6..712bc3f0df 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -318,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] { }; ``` -### php: +### PhP: ```php function twoSum(array $nums, int $target): array @@ -501,3 +501,4 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){ + diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index ce9eccf00b..40ee3a2eae 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -152,10 +152,10 @@ public: ## 相关题目推荐 -* 26.删除排序数组中的重复项 -* 283.移动零 -* 844.比较含退格的字符串 -* 977.有序数组的平方 +* [26.删除排序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/) +* [283.移动零](https://leetcode.cn/problems/move-zeroes/) +* [844.比较含退格的字符串](https://leetcode.cn/problems/backspace-string-compare/) +* [977.有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) ## 其他语言版本 @@ -444,3 +444,4 @@ public class Solution { + diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index fe4e4ed3e7..639c54bc7e 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -16,11 +16,13 @@ - 输出: 6 - 解释:  连续子数组  [4,-1,2,1] 的和最大,为  6。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -## 暴力解法 +## 思路 + +### 暴力解法 暴力解法的思路,第一层 for 就是设置起始位置,第二层 for 循环遍历数组寻找最大值 @@ -48,7 +50,7 @@ public: 以上暴力的解法 C++勉强可以过,其他语言就不确定了。 -## 贪心解法 +### 贪心解法 **贪心贪的是哪里呢?** @@ -104,7 +106,7 @@ public: 当然题目没有说如果数组为空,应该返回什么,所以数组为空的话返回啥都可以了。 -## 常见误区 +### 常见误区 误区一: @@ -122,7 +124,7 @@ public: 其实并不会,因为还有一个变量 result 一直在更新 最大的连续和,只要有更大的连续和出现,result 就更新了,那么 result 已经把 4 更新了,后面 连续和变成 3,也不会对最后结果有影响。 -## 动态规划 +### 动态规划 当然本题还可以用动态规划来做,在代码随想录动态规划章节我会详细介绍,如果大家想在想看,可以直接跳转:[动态规划版本详解](https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index a38e8237b9..d855f1a166 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -6,7 +6,7 @@ -## 54.螺旋矩阵 +# 54.螺旋矩阵 [力扣题目链接](https://leetcode.cn/problems/spiral-matrix/) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 73e9e4daea..78d9385a23 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -688,7 +688,7 @@ public class Solution { } ``` -### Ruby#: +### Ruby: ```ruby def generate_matrix(n) result = Array.new(n) { Array.new(n, 0) } diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 5111e30e10..985c7575af 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -50,9 +50,9 @@ * 1 <= m, n <= 100 * 题目数据保证答案小于等于 2 * 10^9 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index cb305b4189..3d243a7a8f 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -46,9 +46,9 @@ * 1 <= m, n <= 100 * obstacleGrid[i][j] 为 0 或 1 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 88c8bdade6..13080cd9e7 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 90.子集II [力扣题目链接](https://leetcode.cn/problems/subsets-ii/) diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 368a5747b1..8d58cc5a87 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -16,9 +16,9 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 19ccb725a0..f0ff674636 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -433,7 +433,7 @@ class Solution { } ``` -### python: +### Python: (版本一)先删除空白,然后整个反转,最后单词反转。 **因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)** @@ -974,4 +974,3 @@ char * reverseWords(char * s){ - diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index d4dc769893..e4c5c48400 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -529,4 +529,3 @@ impl Solution { - diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index c20fe4f1aa..cd3ae70d71 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -197,7 +197,6 @@ class Solution { } ``` -## 其他语言版本 ### Python BFS solution ```python @@ -244,3 +243,4 @@ class Solution: ``` + diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index ee3f429150..42232cc0c6 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 283. 移动零:动态规划:一样的套路,再求一次完全平方数 [力扣题目链接](https://leetcode.cn/problems/move-zeroes/) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 3798b48d87..2795e31384 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -31,7 +31,7 @@ ## 算法公开课 -**如果对回溯算法基础还不了解的话,我还特意录制了一期视频,[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) ,相信结合视频再看本篇题解,更有助于大家对本题的理解。** ## 思路 @@ -793,4 +793,3 @@ impl Solution { - diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 3ff8dedb03..cba82f6cae 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -22,9 +22,9 @@ * 说明: 你可以假设 n 不小于 2 且不大于 58。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -473,3 +473,4 @@ object Solution { + diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 0657c010bb..2b2be1037d 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -4,7 +4,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 416. 分割等和子集 +# 416. 分割等和子集 [力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/) @@ -730,4 +730,3 @@ object Solution { - diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 023e4d7e0e..d211a68072 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -294,4 +294,3 @@ impl Solution { - diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 44092aaebc..18839a2638 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -19,7 +19,7 @@ 注意: 合并必须从两个树的根节点开始。 -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 @@ -793,3 +793,4 @@ impl Solution { + diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 37a601bcf8..186f044c40 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -22,7 +22,7 @@ * 输出:6 * 解释:答案不应该是 11 ,因为岛屿只能包含水平或垂直这四个方向上的 1 。 -# 思路 +## 思路 注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 @@ -38,7 +38,7 @@ * [DFS理论基础](https://programmercarl.com/图论深搜理论基础.html) * [BFS理论基础](https://programmercarl.com/图论广搜理论基础.html) -## DFS +### DFS 很多同学,写dfs其实也是凭感觉来,有的时候dfs函数中写终止条件才能过,有的时候 dfs函数不写终止添加也能过! @@ -134,7 +134,7 @@ public: 以上两种写法的区别,我在题解: [DFS,BDF 你没注意的细节都给你列出来了!LeetCode:200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/)做了详细介绍。 -## BFS +### BFS 关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://programmercarl.com/图论广搜理论基础.html) @@ -188,9 +188,9 @@ public: ``` -# 其它语言版本 -## Java -### DFS +## 其它语言版本 +### Java +#### DFS ```java // DFS class Solution { @@ -236,7 +236,7 @@ class Solution { ``` -### BFS +#### BFS ```java //BFS class Solution { @@ -290,7 +290,7 @@ class Solution { } } ``` -### DFS 優化(遇到島嶼後,就把他淹沒) +#### DFS 優化(遇到島嶼後,就把他淹沒) ```java //这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。 public int maxAreaOfIsland(int[][] grid) { @@ -319,8 +319,8 @@ public int dfs(int[][] grid,int i,int j){ } ``` -## Python -### BFS +### Python +#### BFS ```python class Solution: def __init__(self): @@ -359,7 +359,7 @@ class Solution: self.count += 1 queue.append((new_x, new_y)) ``` -### DFS +#### DFS ```python class Solution: def __init__(self): @@ -394,3 +394,4 @@ class Solution: + diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 52abf57851..2fc754bb4e 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -150,8 +150,8 @@ public: * [35.搜索插入位置](https://programmercarl.com/0035.搜索插入位置.html) * [34.在排序数组中查找元素的第一个和最后一个位置](https://programmercarl.com/0034.%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.html) -* 69.x 的平方根 -* 367.有效的完全平方数 +* [69.x 的平方根](https://leetcode.cn/problems/sqrtx/) +* [367.有效的完全平方数](https://leetcode.cn/problems/valid-perfect-square/) @@ -766,3 +766,4 @@ object Solution { + diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" index 52b2be3bcc..db39864908 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" @@ -39,7 +39,7 @@ 本题相对于[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html),多添加了一个条件就是手续费。 -## 贪心算法 +### 贪心算法 在[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html)中使用贪心策略不用关心具体什么时候买卖,只要收集每天的正利润,最后稳稳的就是最大利润了。 @@ -93,7 +93,7 @@ public: 大家也可以发现,情况三,那块代码是可以删掉的,我是为了让代码表达清晰,所以没有精简。 -## 动态规划 +### 动态规划 我在公众号「代码随想录」里将在下一个系列详细讲解动态规划,所以本题解先给出我的C++代码(带详细注释),感兴趣的同学可以自己先学习一下。 @@ -364,3 +364,4 @@ object Solution { + diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index 2ea4ae4760..ec8288c608 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -149,7 +149,7 @@ public: ``` -# 总结 +## 总结 本题是比较基础的深度优先搜索模板题,这种有向图路径问题,最合适使用深搜,当然本题也可以使用广搜,但广搜相对来说就麻烦了一些,需要记录一下路径。 @@ -159,7 +159,7 @@ public: ## 其他语言版本 -Java +### Java ```Java // 深度优先遍历 @@ -190,7 +190,8 @@ class Solution { } ``` -Python +### Python + ```python class Solution: def __init__(self): @@ -216,9 +217,9 @@ class Solution: self.path.pop() # 回溯 ``` -### Go + +

- diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 9112fabde2..d78798253a 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -29,7 +29,7 @@ * 输出: 4 * 解释: 没有0可以让我们变成1,面积依然为 4。 -# 思路 +## 思路 本题的一个暴力想法,应该是遍历地图尝试 将每一个 0 改成1,然后去搜索地图中的最大的岛屿面积。 @@ -219,9 +219,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -286,4 +286,3 @@ class Solution { - diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" index ac56f6f990..c7f5220288 100644 --- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -38,7 +38,7 @@ 本文将给出 空间复杂度O(n)的栈模拟方法 以及空间复杂度是O(1)的双指针方法。 -## 普通方法(使用栈的思路) +### 普通方法(使用栈的思路) 这道题目一看就是要使用栈的节奏,这种匹配(消除)问题也是栈的擅长所在,跟着一起刷题的同学应该知道,在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html),我就已经提过了一次使用栈来做类似的事情了。 @@ -100,7 +100,7 @@ public: * 时间复杂度:O(n + m) * 空间复杂度:O(n + m) -## 优化方法(从后向前双指针) +### 优化方法(从后向前双指针) 当然还可以有使用 O(1) 的空间复杂度来解决该问题。 @@ -289,7 +289,7 @@ class Solution { } ``` -### python +### Python ```python class Solution: @@ -591,3 +591,4 @@ impl Solution { + diff --git "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" index d44c14769e..9bcc7ef9ca 100644 --- "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" +++ "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 1791.找出星型图的中心节点 [题目链接](https://leetcode.cn/problems/find-center-of-star-graph/) @@ -55,7 +56,7 @@ public: return -1; } }; -``` +``` 以上代码中没有使用 unordered_map,因为遍历的时候,开辟新空间会浪费时间,而采用 vector,这是 空间换时间的一种策略。 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 5f1d894333..29e50ab8b0 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -138,3 +138,4 @@ public: + diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" index 50287ce20b..1982d44943 100644 --- "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" +++ "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" @@ -200,4 +200,3 @@ class Solution { - diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 008b7915c3..a3fb7ab330 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -142,7 +142,7 @@ class Solution { } ``` -### python: +### Python: (版本一)使用切片 ```python @@ -338,7 +338,7 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) { ``` -### PHP +### PHP: ```php function reverseLeftWords($s, $n) { @@ -418,4 +418,3 @@ impl Solution { - diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index c86376d349..e28bfd0472 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -40,7 +40,7 @@ 好啦,我们再一起回顾一下,动态规划专题中我们都讲了哪些内容。 -## 动划基础 +## 动态规划基础 * [关于动态规划,你该了解这些!](https://programmercarl.com/动态规划理论基础.html) * [动态规划:斐波那契数](https://programmercarl.com/0509.斐波那契数.html) @@ -136,4 +136,3 @@ - diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 6127016111..7f6fd114b4 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -9,6 +9,8 @@ -------------------------- +# 本周小结!(回溯算法系列一) + ## 周一 本周我们正式开始了回溯算法系列,那么首先当然是概述。 diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index 31ce955489..e7f8ef86c6 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 栈与队列总结篇 ## 栈与队列的理论基础 diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index fdb4f58ba4..4291c80c72 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -196,7 +196,7 @@ impl Solution{ } ``` -### Go: +### Go Go中slice的`append`操作和C++中vector的扩容机制基本相同。 @@ -213,4 +213,3 @@ Go中slice的`append`操作和C++中vector的扩容机制基本相同。 - diff --git "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" index 59de69dfd7..21b7f93b1c 100644 --- "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" +++ "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" @@ -3,8 +3,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+# 算法模板 -## 二分查找法 +## 算法模板 + +### 二分查找法 ```CPP class Solution { @@ -29,7 +32,7 @@ public: ``` -## KMP +### KMP ```CPP void kmp(int* next, const string& s){ @@ -47,7 +50,7 @@ void kmp(int* next, const string& s){ } ``` -## 二叉树 +### 二叉树 二叉树的定义: @@ -60,7 +63,7 @@ struct TreeNode { }; ``` -### 深度优先遍历(递归) +#### 深度优先遍历(递归) 前序遍历(中左右) ```CPP @@ -90,7 +93,7 @@ void traversal(TreeNode* cur, vector& vec) { } ``` -### 深度优先遍历(迭代法) +#### 深度优先遍历(迭代法) 相关题解:[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md) @@ -170,7 +173,7 @@ vector postorderTraversal(TreeNode* root) { return result; } ``` -### 广度优先遍历(队列) +#### 广度优先遍历(队列) 相关题解:[0102.二叉树的层序遍历](https://programmercarl.com/0102.二叉树的层序遍历.html) @@ -208,7 +211,7 @@ vector> levelOrder(TreeNode* root) { * [0111.二叉树的最小深度(迭代法)](https://programmercarl.com/0111.二叉树的最小深度.html) * [0222.完全二叉树的节点个数(迭代法)](https://programmercarl.com/0222.完全二叉树的节点个数.html) -### 二叉树深度 +#### 二叉树深度 ```CPP int getDepth(TreeNode* node) { @@ -217,7 +220,7 @@ int getDepth(TreeNode* node) { } ``` -### 二叉树节点数量 +#### 二叉树节点数量 ```CPP int countNodes(TreeNode* root) { @@ -226,7 +229,7 @@ int countNodes(TreeNode* root) { } ``` -## 回溯算法 +### 回溯算法 ```CPP void backtracking(参数) { if (终止条件) { @@ -243,7 +246,7 @@ void backtracking(参数) { ``` -## 并查集 +### 并查集 ```CPP int n = 1005; // 根据题意而定 @@ -278,9 +281,9 @@ void backtracking(参数) { (持续补充ing) ## 其他语言版本 -JavaScript: +### JavaScript: -## 二分查找法 +#### 二分查找法 使用左闭右闭区间 @@ -322,7 +325,7 @@ var search = function (nums, target) { }; ``` -## KMP +#### KMP ```javascript var kmp = function (next, s) { @@ -340,9 +343,9 @@ var kmp = function (next, s) { } ``` -## 二叉树 +#### 二叉树 -### 深度优先遍历(递归) +##### 深度优先遍历(递归) 二叉树节点定义: @@ -387,7 +390,7 @@ var postorder = function (root, list) { } ``` -### 深度优先遍历(迭代) +##### 深度优先遍历(迭代) 前序遍历(中左右): @@ -447,7 +450,7 @@ var postorderTraversal = function (root) { }; ``` -### 广度优先遍历(队列) +##### 广度优先遍历(队列) ```javascript var levelOrder = function (root) { @@ -469,7 +472,7 @@ var levelOrder = function (root) { }; ``` -### 二叉树深度 +##### 二叉树深度 ```javascript var getDepth = function (node) { @@ -478,7 +481,7 @@ var getDepth = function (node) { } ``` -### 二叉树节点数量 +##### 二叉树节点数量 ```javascript var countNodes = function (root) { @@ -487,7 +490,7 @@ var countNodes = function (root) { } ``` -## 回溯算法 +#### 回溯算法 ```javascript function backtracking(参数) { @@ -505,7 +508,7 @@ function backtracking(参数) { ``` -## 并查集 +#### 并查集 ```javascript let n = 1005; // 根据题意而定 @@ -536,9 +539,9 @@ function backtracking(参数) { } ``` -TypeScript: +### TypeScript: -## 二分查找法 +#### 二分查找法 使用左闭右闭区间 @@ -580,7 +583,7 @@ var search = function (nums: number[], target: number): number { }; ``` -## KMP +#### KMP ```typescript var kmp = function (next: number[], s: number): void { @@ -598,9 +601,9 @@ var kmp = function (next: number[], s: number): void { } ``` -## 二叉树 +#### 二叉树 -### 深度优先遍历(递归) +##### 深度优先遍历(递归) 二叉树节点定义: @@ -650,7 +653,7 @@ var postorder = function (root: TreeNode | null, list: number[]): void { } ``` -### 深度优先遍历(迭代) +##### 深度优先遍历(迭代) 前序遍历(中左右): @@ -710,7 +713,7 @@ var postorderTraversal = function (root: TreeNode | null): number[] { }; ``` -### 广度优先遍历(队列) +##### 广度优先遍历(队列) ```typescript var levelOrder = function (root: TreeNode | null): number[] { @@ -732,7 +735,7 @@ var levelOrder = function (root: TreeNode | null): number[] { }; ``` -### 二叉树深度 +##### 二叉树深度 ```typescript var getDepth = function (node: TreNode | null): number { @@ -741,7 +744,7 @@ var getDepth = function (node: TreNode | null): number { } ``` -### 二叉树节点数量 +##### 二叉树节点数量 ```typescript var countNodes = function (root: TreeNode | null): number { @@ -750,7 +753,7 @@ var countNodes = function (root: TreeNode | null): number { } ``` -## 回溯算法 +#### 回溯算法 ```typescript function backtracking(参数) { @@ -768,7 +771,7 @@ function backtracking(参数) { ``` -## 并查集 +#### 并查集 ```typescript let n: number = 1005; // 根据题意而定 @@ -801,9 +804,9 @@ function backtracking(参数) { Java: +### Python: -Python: -## 二分查找法 +#### 二分查找法 ```python def binarysearch(nums, target): low = 0 @@ -823,7 +826,7 @@ def binarysearch(nums, target): return -1 ``` -## KMP +#### KMP ```python def kmp(self, a, s): diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 019947e536..f60e261be0 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -3,8 +3,10 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 动态规划:01背包理论基础(滚动数组) + ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index dacd4dee5d..3f2f5c9792 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 链表总结篇 From 5dc3022bc5c6aa84fdd1ce255fad723200ffe059 Mon Sep 17 00:00:00 2001 From: slaier <30682486+slaier@users.noreply.github.com> Date: Sat, 29 Jul 2023 06:48:13 +0000 Subject: [PATCH 0676/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E6=95=B0=20C=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\345\277\253\344\271\220\346\225\260.md" | 68 ++++++++++++++----- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 4a77e2b67c..719672a281 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -423,29 +423,61 @@ impl Solution { ### C: ```C -typedef struct HashNodeTag { - int key; /* num */ - struct HashNodeTag *next; -}HashNode; - -/* Calcualte the hash key */ -static inline int hash(int key, int size) { - int index = key % size; - return (index > 0) ? (index) : (-index); -} - -/* Calculate the sum of the squares of its digits*/ -static inline int calcSquareSum(int num) { - unsigned int sum = 0; - while(num > 0) { - sum += (num % 10) * (num % 10); - num = num/10; +int get_sum(int n) { + int sum = 0; + div_t n_div = { .quot = n }; + while (n_div.quot != 0) { + n_div = div(n_div.quot, 10); + sum += n_div.rem * n_div.rem; } return sum; } +// (版本1)使用数组 +bool isHappy(int n) { + // sum = a1^2 + a2^2 + ... ak^2 + // first round: + // 1 <= k <= 10 + // 1 <= sum <= 1 + 81 * 9 = 730 + // second round: + // 1 <= k <= 3 + // 1 <= sum <= 36 + 81 * 2 = 198 + // third round: + // 1 <= sum <= 81 * 2 = 162 + // fourth round: + // 1 <= sum <= 81 * 2 = 162 + + uint8_t visited[163] = { 0 }; + int sum = get_sum(get_sum(n)); + int next_n = sum; + + while (next_n != 1) { + sum = get_sum(next_n); + + if (visited[sum]) return false; + + visited[sum] = 1; + next_n = sum; + }; + + return true; +} + +// (版本2)使用快慢指针 +bool isHappy(int n) { + int slow = n; + int fast = n; + + do { + slow = get_sum(slow); + fast = get_sum(get_sum(fast)); + } while (slow != fast); + + return (fast == 1); +} +``` -Scala: +### Scala: ```scala object Solution { // 引入mutable From dca8209aaf3276ba0d639165d13a27b21a24c498 Mon Sep 17 00:00:00 2001 From: Binbin Date: Mon, 31 Jul 2023 14:17:07 +0800 Subject: [PATCH 0677/1533] Fix typos and cleanups --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 2 +- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 2 +- ...72\257\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...23\210\345\270\214\350\241\250\346\200\273\347\273\223.md" | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index cdf33c5812..37d5614e71 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -73,7 +73,7 @@ candidates 中的数字可以无限制重复被选取。 如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[17.电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html) -**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我再讲解排列的时候就重点介绍**。 +**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我在讲解排列的时候会重点介绍**。 代码如下: diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index c15d1ac177..3de48ce3da 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -68,7 +68,7 @@ public: ### 哈希解法 -因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。 +因为题目说只有小写字母,那可以采用空间换取时间的哈希策略,用一个长度为26的数组来记录magazine里字母出现的次数。 然后再用ransomNote去验证这个数组是否包含了ransomNote所需要的所有字母。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 76bd331bd4..3f1d10126e 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -17,7 +17,7 @@ 如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex,例如:[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html) -**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我再讲解排列的时候就重点介绍**。 +**注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路,后面我在讲解排列的时候会重点介绍**。 最后还给出了本题的剪枝优化,如下: diff --git "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" index 6750636305..465ef9d1da 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" @@ -16,7 +16,7 @@ **一般来说哈希表都是用来快速判断一个元素是否出现集合里**。 -对于哈希表,要知道**哈希函数**和**哈希碰撞**在哈希表中的作用. +对于哈希表,要知道**哈希函数**和**哈希碰撞**在哈希表中的作用。 哈希函数是把传入的key映射到符号表的索引上。 @@ -88,7 +88,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 map是一种``的结构,本题可以用key保存数值,用value在保存数值所在的下标。所以使用map最为合适。 -C++提供如下三种map::(详情请看[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)) +C++提供如下三种map:(详情请看[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)) * std::map * std::multimap From 5bd60c95b709ee5f56399c70ea6b93bd46d38a6b Mon Sep 17 00:00:00 2001 From: Martin <87409284+martinchiu@users.noreply.github.com> Date: Mon, 31 Jul 2023 22:44:59 +0800 Subject: [PATCH 0678/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2054.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5=20Javascript=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\346\227\213\347\237\251\351\230\265.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index d855f1a166..c62eb2b12c 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -200,6 +200,58 @@ class Solution { } ``` +### Javascript +``` +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + let m = matrix.length + let n = matrix[0].length + + let startX = startY = 0 + let i = 0 + let arr = new Array(m*n).fill(0) + let offset = 1 + let loop = mid = Math.floor(Math.min(m,n) / 2) + while (loop--) { + let row = startX + let col = startY + // --> + for (; col < n + startY - offset; col++) { + arr[i++] = matrix[row][col] + } + // down + for (; row < m + startX - offset; row++) { + arr[i++] = matrix[row][col] + } + // <-- + for (; col > startY; col--) { + arr[i++] = matrix[row][col] + } + for (; row > startX; row--) { + arr[i++] = matrix[row][col] + } + startX++ + startY++ + offset += 2 + } + if (Math.min(m, n) % 2 === 1) { + if (n > m) { + for (let j = mid; j < mid + n - m + 1; j++) { + arr[i++] = matrix[mid][j] + } + } else { + for (let j = mid; j < mid + m - n + 1; j++) { + arr[i++] = matrix[j][mid] + } + } + } + return arr +}; +``` +

From 3f428289b2d6cc2cd75b163d1127ff374f7de116 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 5 Aug 2023 15:55:18 +0800 Subject: [PATCH 0679/1533] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?.md=20=E4=BB=A3=E7=A0=81=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 56 ------------------- 1 file changed, 56 deletions(-) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 9b76229af0..9fb6a6b061 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -725,62 +725,6 @@ impl Solution { } ``` -Rust - -双指针预处理 -```rust - -impl Solution { - pub fn largest_rectangle_area(v: Vec) -> i32 { - let n = v.len(); - let mut left_smaller_idx = vec![-1; n]; - let mut right_smaller_idx = vec![n as i32; n]; - for i in 1..n { - let mut mid = i as i32 - 1; - while mid >= 0 && v[mid as usize] >= v[i] { - mid = left_smaller_idx[mid as usize]; - } - left_smaller_idx[i] = mid; - } - for i in (0..n-1).rev() { - let mut mid = i + 1; - while mid < n && v[mid] >= v[i] { - mid = right_smaller_idx[mid] as usize; - } - right_smaller_idx[i] = mid as i32; - } - let mut res = 0; - for (idx, &e) in v.iter().enumerate() { - res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); - } - dbg!(res) - } -} -``` - -单调栈 -```rust -impl Solution { - pub fn largest_rectangle_area1(mut v: Vec) -> i32 { - v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 - v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 - let mut res = 0; - let mut stack = vec![]; // 递增的栈 - for (idx, &e) in v.iter().enumerate() { - while !stack.is_empty() && v[*stack.last().unwrap()] > e { - let pos = stack.pop().unwrap(); - let prev_pos = *stack.last().unwrap(); - let s = (idx - prev_pos - 1) as i32 * v[pos]; - res = res.max(s); - } - stack.push(idx); - } - res - } -} -``` - -

From dc65f95f7beecb1f6b2ecf49665b4f3a89c6c904 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Sat, 5 Aug 2023 17:05:35 +0800 Subject: [PATCH 0680/1533] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- ...001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 712bc3f0df..4e44d0c3f6 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -37,20 +37,20 @@ [242. 有效的字母异位词](https://www.programmercarl.com/0242.有效的字母异位词.html) 这道题目是用数组作为哈希表来解决哈希问题,[349. 两个数组的交集](https://www.programmercarl.com/0349.两个数组的交集.html)这道题目是通过set作为哈希表来解决哈希问题。 -首先我在强调一下 **什么时候使用哈希法**,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法。 +首先我再强调一下 **什么时候使用哈希法**,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法。 本题呢,我就需要一个集合来存放我们遍历过的元素,然后在遍历数组的时候去询问这个集合,某元素是否遍历过,也就是 是否出现在这个集合。 那么我们就应该想到使用哈希法了。 -因为本地,我们不仅要知道元素有没有遍历过,还要知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。 +因为本题,我们不仅要知道元素有没有遍历过,还要知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。 再来看一下使用数组和set来做哈希法的局限。 * 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。 * set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。 -此时就要选择另一种数据结构:map ,map是一种key value的存储结构,可以用key保存数值,用value在保存数值所在的下标。 +此时就要选择另一种数据结构:map ,map是一种key value的存储结构,可以用key保存数值,用value再保存数值所在的下标。 C++中map,有三种类型: From d4810327aed53170c21c23f06e1fb6c54a184337 Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Sun, 6 Aug 2023 10:25:37 +0800 Subject: [PATCH 0681/1533] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index f99102ab45..177c3878ba 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -52,7 +52,7 @@ 也就是由前后相同的子串组成。 -那么既然前面有相同的子串,后面有相同的子串,用 s + s,这样组成的字符串中,后面的子串做前串,前后的子串做后串,就一定还能组成一个s,如图: +那么既然前面有相同的子串,后面有相同的子串,用 s + s,这样组成的字符串中,后面的子串做前串,前面的子串做后串,就一定还能组成一个s,如图: ![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728104931.png) From 692647b03a1e14d895bbb24b5b98286c17100c73 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Sun, 6 Aug 2023 21:27:09 +0800 Subject: [PATCH 0682/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 90c3733449..1c262c873d 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -41,7 +41,7 @@ ## 思路 -本题咋眼一看好像和[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)差不多,其实差很多。 +本题乍眼一看好像和[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)差不多,其实差很多。 **本题是使用哈希法的经典题目,而[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)并不合适使用哈希法**,因为三数之和和四数之和这两道题目使用哈希法在不超时的情况下做到对结果去重是很困难的,很有多细节需要处理。 From 1f9f9c224cafb201ae71668a330a82a01105cfd5 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Tue, 8 Aug 2023 13:43:15 +0800 Subject: [PATCH 0683/1533] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...015.\344\270\211\346\225\260\344\271\213\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 4951c90cf1..91fc9d685a 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -171,7 +171,7 @@ public: #### a的去重 -说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right] +说到去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right] a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳过去。 @@ -181,7 +181,7 @@ a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳 其实不一样! -都是和 nums[i]进行比较,是比较它的前一个,还是比较他的后一个。 +都是和 nums[i]进行比较,是比较它的前一个,还是比较它的后一个。 如果我们的写法是 这样: @@ -191,7 +191,7 @@ if (nums[i] == nums[i + 1]) { // 去重操作 } ``` -那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。 +那我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。 **我们要做的是 不能有重复的三元组,但三元组内的元素是可以重复的!** From eb75312e199aa9fa2a1c4f0212e2bcafd6676ddf Mon Sep 17 00:00:00 2001 From: guyinfei Date: Tue, 8 Aug 2023 16:02:54 +0800 Subject: [PATCH 0684/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0=20JAVA=E7=89=88=E6=9C=AC=20?= =?UTF-8?q?=E7=9B=B8=E5=90=91=E5=8F=8C=E6=8C=87=E9=92=88=E6=B3=95=EF=BC=88?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BA=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 40ee3a2eae..1138ffd495 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -197,6 +197,26 @@ class Solution { } ``` +```java +// 相向双指针法(版本二) +class Solution { + public int removeElement(int[] nums, int val) { + int left = 0; + int right = nums.length - 1; + while(left <= right){ + if(nums[left] == val){ + nums[left] = nums[right]; + right--; + }else { + // 这里兼容了right指针指向的值与val相等的情况 + left++; + } + } + return left; + } +} +``` + ### Python: From d3eb234491ba63256ab8451ca16fd1084d24b8ff Mon Sep 17 00:00:00 2001 From: Jining Jiang <58833386+jjnnzb@users.noreply.github.com> Date: Tue, 8 Aug 2023 23:51:25 +0800 Subject: [PATCH 0685/1533] =?UTF-8?q?fix:=20=E8=A1=A8=E8=BF=B0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 应该是“其他”,而不是“其其他” --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 7511ac8715..79ad26a859 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -28,7 +28,7 @@ ![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407.png) -至于背包九讲其其他背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 +至于背包九讲其他背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 而完全背包又是也是01背包稍作变化而来,即:完全背包的物品数量是无限的。 From 6167408cd29d884bb8b74cc73b05ce7b6aac87ed Mon Sep 17 00:00:00 2001 From: han Date: Wed, 9 Aug 2023 12:06:32 +0800 Subject: [PATCH 0686/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00018.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 28c20b7acd..17715b2e6f 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -649,6 +649,54 @@ object Solution { } } ``` +### Ruby: + +```ruby +def four_sum(nums, target) + #结果集 + result = [] + nums = nums.sort! + + for i in 0..nums.size - 1 + return result if i > 0 && nums[i] > target && nums[i] >= 0 + #对a进行去重 + next if i > 0 && nums[i] == nums[i - 1] + + for j in i + 1..nums.size - 1 + break if nums[i] + nums[j] > target && nums[i] + nums[j] >= 0 + #对b进行去重 + next if j > i + 1 && nums[j] == nums[j - 1] + left = j + 1 + right = nums.size - 1 + while left < right + sum = nums[i] + nums[j] + nums[left] + nums[right] + if sum > target + right -= 1 + elsif sum < target + left += 1 + else + result << [nums[i], nums[j], nums[left], nums[right]] + + #对c进行去重 + while left < right && nums[left] == nums[left + 1] + left += 1 + end + + #对d进行去重 + while left < right && nums[right] == nums[right - 1] + right -= 1 + end + + right -= 1 + left += 1 + end + end + end + end + + return result +end +```

From 7ae878753b3d700d14a3bfd0c55194c618b62a03 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 10 Aug 2023 10:21:29 +0800 Subject: [PATCH 0687/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A01047.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20Ruby?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\207\215\345\244\215\351\241\271.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index ad54f0f88e..ffe1353039 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -475,6 +475,26 @@ impl Solution { } ``` +### Ruby + +```ruby +def remove_duplicates(s) + #数组模拟栈 + stack = [] + s.each_char do |chr| + if stack.empty? + stack.push chr + else + head = stack.pop + #重新进栈 + stack.push head, chr if head != chr + end + end + + return stack.join +end +``` +

From 86f2dcb46bbd7e243bee746c37f7cdeb448f004c Mon Sep 17 00:00:00 2001 From: TonySu Date: Thu, 10 Aug 2023 17:24:51 +0800 Subject: [PATCH 0688/1533] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4?= =?UTF-8?q?=E7=BB=95=E7=9A=84=E5=8C=BA=E5=9F=9F.md,=20=E5=A2=9E=E5=8A=A0Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC=E7=9A=84DFS=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Python3 版本的DFS解法,在leetcode上测试通过。 --- ...25\347\232\204\345\214\272\345\237\237.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index e244873ba3..e8a1f02f69 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -385,6 +385,55 @@ class Solution { } } ``` +### Python3 + +```Python +// 深度优先遍历 +class Solution: + dir_list = [(0, 1), (0, -1), (1, 0), (-1, 0)] + def solve(self, board: List[List[str]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + row_size = len(board) + column_size = len(board[0]) + visited = [[False] * column_size for _ in range(row_size)] + # 从边缘开始,将边缘相连的O改成A。然后遍历所有,将A改成O,O改成X + # 第一行和最后一行 + for i in range(column_size): + if board[0][i] == "O" and not visited[0][i]: + self.dfs(board, 0, i, visited) + if board[row_size-1][i] == "O" and not visited[row_size-1][i]: + self.dfs(board, row_size-1, i, visited) + + # 第一列和最后一列 + for i in range(1, row_size-1): + if board[i][0] == "O" and not visited[i][0]: + self.dfs(board, i, 0, visited) + if board[i][column_size-1] == "O" and not visited[i][column_size-1]: + self.dfs(board, i, column_size-1, visited) + + for i in range(row_size): + for j in range(column_size): + if board[i][j] == "A": + board[i][j] = "O" + elif board[i][j] == "O": + board[i][j] = "X" + + + def dfs(self, board, x, y, visited): + if visited[x][y] or board[x][y] == "X": + return + visited[x][y] = True + board[x][y] = "A" + for i in range(4): + new_x = x + self.dir_list[i][0] + new_y = y + self.dir_list[i][1] + if new_x >= len(board) or new_y >= len(board[0]) or new_x < 0 or new_y < 0: + continue + self.dfs(board, new_x, new_y, visited) + +```

From 92051fac670c4c1e776d0afad4adadbc9c6dacfd Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Sun, 13 Aug 2023 13:59:49 +0800 Subject: [PATCH 0689/1533] =?UTF-8?q?Update=200225.=E7=94=A8=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\237\345\210\227\345\256\236\347\216\260\346\240\210.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 13b742f839..3de300c781 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -111,7 +111,7 @@ public: } }; ``` -* 时间复杂度: push为O(n),其他为O(1) +* 时间复杂度: pop为O(n),其他为O(1) * 空间复杂度: O(n) ## 优化 @@ -158,7 +158,7 @@ public: } }; ``` -* 时间复杂度: push为O(n),其他为O(1) +* 时间复杂度: pop为O(n),其他为O(1) * 空间复杂度: O(n) From 0f458de25416863e592d419a35f535405610f9bf Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Sun, 13 Aug 2023 22:59:49 -0400 Subject: [PATCH 0690/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=EF=BC=88hashArray=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java解法(hashArray) --- ...04\347\232\204\344\272\244\351\233\206.md" | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 26a9286dc9..138067e360 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -121,7 +121,7 @@ public: ## 其他语言版本 ### Java: - +版本一:使用HashSet ```Java import java.util.HashSet; import java.util.Set; @@ -159,7 +159,28 @@ class Solution { } } ``` - +版本二:使用Hash數組 +```java +class Solution { + public int[] intersection(int[] nums1, int[] nums2) { + int[] hash1 = new int[1002]; + int[] hash2 = new int[1002]; + for(int i : nums1) + hash1[i]++; + for(int i : nums2) + hash2[i]++; + List resList = new ArrayList<>(); + for(int i = 0; i < 1002; i++) + if(hash1[i] > 0 && hash2[i] > 0) + resList.add(i); + int index = 0; + int res[] = new int[resList.size()]; + for(int i : resList) + res[index++] = i; + return res; + } +} +``` ### Python3: (版本一) 使用字典和集合 From 146c206ca1b28cdf7526d76c797ee0278b276080 Mon Sep 17 00:00:00 2001 From: Pika-Lee Date: Sun, 13 Aug 2023 23:21:06 -0700 Subject: [PATCH 0691/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit correct the text fault --- ...\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 184dba604c..e2c6d83c59 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -150,7 +150,7 @@ 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 -**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用非递归的方式来实现的。 +**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用递归的方式来实现的。 而广度优先遍历的实现一般使用队列来实现,这也是队列先进先出的特点所决定的,因为需要先进先出的结构,才能一层一层的来遍历二叉树。 From 6a2f01055c784d54059568f970b44d650be04e87 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 15 Aug 2023 10:09:47 +0800 Subject: [PATCH 0692/1533] Update --- ...11\346\216\222\350\241\214\347\250\213.md" | 7 +- problems/qita/acm.md | 89 +++++++++++++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 problems/qita/acm.md diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 3798b48d87..794a67fa40 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -237,13 +237,10 @@ public: ```cpp for (pair& target : targets[result[result.size() - 1]]) ``` -pair里要有const,因为map中的key是不可修改的,所以是`pair`。 -如果不加const,也可以复制一份pair,例如这么写: +一定要加上引用即 `& target`,因为后面有对 target.second 做减减操作,如果没有引用,单纯复制,这个结果就没记录下来,那最后的结果就不对了。 -```cpp -for (pairtarget : targets[result[result.size() - 1]]) -``` +加上引用之后,就必须在 string 前面加上 const,因为map中的key 是不可修改了,这就是语法规定了。 ## 总结 diff --git a/problems/qita/acm.md b/problems/qita/acm.md new file mode 100644 index 0000000000..1be0e924de --- /dev/null +++ b/problems/qita/acm.md @@ -0,0 +1,89 @@ + +# 如何练习ACM模式输入输入模式 | 如何准备笔试 | 卡码网 + +卡码网地址:[https://kamacoder.com](https://kamacoder.com) + +## 为什么卡码网 + +录友们在求职的时候会发现,很多公司的笔试题和面试题都是ACM模式, 而大家习惯去力扣刷题,力扣是核心代码模式。 + +当大家在做ACM模式的算法题的时候,需要自己处理数据的输入输出,**如果没有接触过的话,还是挺难的**。 + +[知识星球](https://programmercarl.com/other/kstar.html)里很多录友的日常打卡中,都表示被 ACM模式折磨过: + +

+ +
+ +
+ +
+ +
+ +所以我正式推出:**卡码网**([https://kamacoder.com](https://kamacoder.com)),**专门帮助大家练习ACM模式**。 + +那么之前大家去哪里练习ACM模式呢? + +去牛客做笔试真题,结果发现 ACM模式没练出来,题目倒是巨难,一点思路都没有,代码更没有写,ACM模式无从练起。 + +去洛谷,POJ上练习? 结果发现 题目超多,不知道从哪里开始刷,也没有一个循序渐进的刷题顺序。 + +**而卡码网上有我精选+制作的25道题目**!我还把25题的后台测试数据制作了一遍,保证大家练习的效果。 + +为什么题目不多,只有25道? + +因为大家练习ACM模式不需要那么多题目,有一个循序渐进的练习过程就好了。 + +这25道题目包含了数组、链表、哈希表、字符串、二叉树、动态规划以及图的的题目,常见的输入输出方式都覆盖了。 + +**这是最精华的25道题目**!。 + +## 卡码网长什么样 + +来看看这极简的界面,没有烂七八糟的功能,只有刷题! + +
+ +在「状态」这里可以看到 大家提交的代码和判题记录,目前卡码网([https://kamacoder.com](https://kamacoder.com))几乎无时无刻都有卡友在提交代码。 +看看大家周六晚上都在做什么,刷哪些题目。 + +
+ + +提交代码的界面是这样的,**目前支持所有主流刷题语言**。 + +
+ +## 题解 + +基本大家来卡码网([https://kamacoder.com](https://kamacoder.com))练习ACM模式,都是对输入输出不够了解的,所以想看现成的题解,看看究竟是怎么处理的。 + +所以我用C++把卡码网上25道题目的题解都写了,并发布到Github上: + +[https://github.com/youngyangyang04/kamacoder-solutions](https://github.com/youngyangyang04/kamacoder-solutions) + +
+ +**欢迎去Github上star,欢迎fork,也欢迎来Github仓库贡献其他语言版本,成为contributor**。 + +如果不懂如何和开源项目提交代码,[可以看这里](https://www.programmercarl.com/qita/join.html) + +目前已经有两位录友贡献C和Java版本了。 + +
+ +期待在Github(https://github.com/youngyangyang04/kamacoder-solutions) 的contributors上也出现你的头像。 + +目前题解只有C++代码吗? + +当然不是,大多数题目已经有了 Java、python、C版本。 **其他语言版本,就给录友们成为contributor的机会了**。 + +## 最后 + +卡码网地址:[https://kamacoder.com](https://kamacoder.com) + +快去体验吧,笔试之前最好 把卡码网25道题目都刷完。 + +期待录友们成为最早一批把卡码网刷爆的coder! + diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 184dba604c..e2c6d83c59 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -150,7 +150,7 @@ 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 -**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用非递归的方式来实现的。 +**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用递归的方式来实现的。 而广度优先遍历的实现一般使用队列来实现,这也是队列先进先出的特点所决定的,因为需要先进先出的结构,才能一层一层的来遍历二叉树。 From 77f448ec961c4547fb2e2a70356020e760ed7c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Wed, 16 Aug 2023 14:38:49 +0800 Subject: [PATCH 0693/1533] =?UTF-8?q?0332.=E9=87=8D=E6=96=B0=E5=AE=89?= =?UTF-8?q?=E6=8E=92=E8=A1=8C=E7=A8=8B=EF=BC=8C=E6=B7=BB=E5=8A=A0java?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index fcdeae7bde..2498fbfe53 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -261,6 +261,84 @@ for (pair& target : targets[result[result.size() - 1]]) ### Java +```java +/* 首先遍历所有机票,将机票转换成map,其中起点作为key,终点按照字典顺序插入到终点的列表中 + 然后进入递归,递归中首先在result中加入当前位置,如果票用完了就说明这条路走通了 + 递归中遍历从当前位置出发的终点(也就是map中key为当前位置的列表),因为列表是有序的,因此只要出现能走通的路径,这条路径就是字典排序最低的 + 在遍历中,首先从map中移除当前的机票,将遍历到的终点作为起点进入下一层的递归中,如果发现当前机票往后走走不通,再把当前的机票加到map中*/ +class Solution { + //key为起点,value是有序的终点的列表 + Map> ticketMap = new HashMap<>(); + LinkedList result = new LinkedList<>(); + + public List findItinerary(List> tickets) { + //遍历tickets,存入ticketMap中 + for (List ticket : tickets) { + addNew(ticket.get(0), ticket.get(1)); + } + deal("JFK"); + return result; + } + + boolean deal(String currentLocation) { + result.add(currentLocation); + //机票全部用完,找到最小字符路径 + if (ticketMap.isEmpty()) { + return true; + } + //当前位置的终点列表 + LinkedList targetLocations = ticketMap.get(currentLocation); + //机票没用完,但是没有从当前位置出发的机票了,说明这条路走不通 + if (targetLocations == null) { + return false; + } + //终点列表中遍历到的终点 + String targetLocation; + //遍历从当前位置出发的机票 + for (int i = 0; i < targetLocations.size(); i++) { + targetLocation = targetLocations.get(i); + //删除map中的机票,如果当前位置只有一个终点,直接删除k,v,有多个终点则删除终点列表中当前的终点 + if (targetLocations.size() == 1) { + ticketMap.remove(currentLocation); + } else { + targetLocations.remove(i); + } + //递归 + if (deal(targetLocation)) { + return true; + } else { + //路线走不通,将机票重新加到map中 + addNew(currentLocation, targetLocation); + result.removeLast(); + } + } + return false; + } + + /** + * 在map中添加新元素 + * + * @param start 起点 + * @param end 终点 + */ + void addNew(String start, String end) { + LinkedList startAllEnd = ticketMap.get(start); + if (startAllEnd != null) { + for (int i = 0; i < startAllEnd.size() + 1; i++) { + if (i == startAllEnd.size() || end.compareTo(startAllEnd.get(i)) < 0) { + startAllEnd.add(i, end); + break; + } + } + } else { + LinkedList ends = new LinkedList<>(); + ends.add(end); + ticketMap.put(start, ends); + } + } +} +``` + ```java class Solution { private LinkedList res; From 00f2aa679318c12921ce974168ad3b7728bc0ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Wed, 16 Aug 2023 15:28:28 +0800 Subject: [PATCH 0694/1533] =?UTF-8?q?0051.N=E7=9A=87=E5=90=8E=EF=BC=8Cjava?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 6bc4fa78c6..71c40238ec 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -345,6 +345,58 @@ class Solution { } } ``` +```java +//该方法主要特点在于用一个一维数组暂存皇后的位置,数组下标代表行,数组下标的值代表列,并初始化一个长度为n-1,值全为'.'的模板字符串 +//在找到合法方案时,遍历数组,在模板字符串下标为数组值的位置插入Q +class Solution { + //结果 + List> result = new ArrayList<>(); + //储存皇后的位置,下标为行,值为列 + int[] queenIndex; + //棋盘中一行的字符串模板 + String template; + int size; + + public List> solveNQueens(int n) { + size = n; + template = ".".repeat(n - 1); + queenIndex = new int[n]; + deal(0); + return result; + } + + void deal(int index) { + //遍历当前行的所有位置(尝试在这行每个位置放置皇后) + for (int i = 0; i < size; i++) { + queenIndex[index] = i; + //检查在当前位置放置皇后是否合法 + if (check(index, i)) { + //如果当前的行是最后一行,就说明当前皇后的摆放已经是完整并且合法的,在结果集中加入当前的摆放方案 + if (index == size - 1) { + List tmp = new ArrayList<>(size); + //遍历当前的皇后位置,在模板字符串对应的位置加入Q,再加入到结果集中 + for (Integer integer : queenIndex) { + tmp.add(new StringBuilder(template).insert(integer, "Q").toString()); + } + result.add(tmp); + return; + } + //如果当前不是最后一行,还需要进入下一行 + deal(index + 1); + } + } + } + + boolean check(int rowIndex, int columnIndex) { + for (int i = 0; i < rowIndex; i++) { + if (queenIndex[i] == columnIndex || (Math.abs(queenIndex[i] - columnIndex) == Math.abs(i - rowIndex))) { + return false; + } + } + return true; + } +} +``` ### Python From b66eb1c7fddaafdf28e510bbfe59fdb5107c67b3 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 18 Aug 2023 09:35:21 +0800 Subject: [PATCH 0695/1533] Update --- ...\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 2 +- ...\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- ...\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index ce9a247c1f..51d030e784 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -18,7 +18,7 @@ * [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) * [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) * [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) -* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) * [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) * [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) * [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 9db7409e04..edb5ea3ea7 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -133,7 +133,7 @@ left与right的逻辑处理; // 中 ![236.二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415105872.png) -就像图中一样直接返回7,多美滋滋。 +就像图中一样直接返回7。 但事实上还要遍历根节点右子树(即使此时已经找到了目标节点了),也就是图中的节点4、15、20。 diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 4bee585ba8..5bdbcbc7e0 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -30,7 +30,7 @@ ### 暴力排序 -最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下: +最直观的想法,莫过于:每个数平方之后,排个序,代码如下: ```CPP class Solution { From 4bd8f3ea32bb189de5919745e531a964b5fcd008 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 16:07:53 +0800 Subject: [PATCH 0696/1533] =?UTF-8?q?Update=200496.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\244\247\345\205\203\347\264\240I.md" | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 6bcafafba2..e02cfbd103 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -392,25 +392,33 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { ### Rust ```rust +use std::collections::HashMap; impl Solution { pub fn next_greater_element(nums1: Vec, nums2: Vec) -> Vec { - let mut ans = vec![-1; nums1.len()]; - use std::collections::HashMap; - let mut map = HashMap::new(); - for (idx, &i) in nums1.iter().enumerate() { - map.insert(i, idx); + let (mut res, mut map) = (vec![-1; nums1.len()], HashMap::new()); + if nums1.is_empty() { + return res; } + + nums1.into_iter().enumerate().for_each(|(v, k)| { + map.insert(k, v); + }); + let mut stack = vec![]; - for (idx, &i) in nums2.iter().enumerate() { - while !stack.is_empty() && nums2[*stack.last().unwrap()] < i { - let pos = stack.pop().unwrap(); - if let Some(&jdx) = map.get(&nums2[pos]) { - ans[jdx] = i; + for (i, &value) in nums2.iter().enumerate() { + while let Some(&top) = stack.last() { + if value <= nums2[top] { + break; + } + let stacked_index = stack.pop().unwrap(); + if let Some(&mapped_index) = map.get(&nums2[stacked_index]) { + res[mapped_index] = value; } } - stack.push(idx); + stack.push(i); } - ans + + res } } ``` From 7aae4f81bd83749333400144fa474c918addb7f4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 16:29:16 +0800 Subject: [PATCH 0697/1533] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md=20about=20ru?= =?UTF-8?q?st?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\244\247\345\205\203\347\264\240II.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index d211a68072..e732b8203c 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -289,6 +289,28 @@ impl Solution { } ``` +> 版本二: + +```rust +impl Solution { + pub fn next_greater_elements(nums: Vec) -> Vec { + let (mut stack, mut res) = (vec![], vec![-1; nums.len()]); + + for i in 0..nums.len() * 2 { + while let Some(&top) = stack.last() { + if nums[i % nums.len()] <= nums[top] { + break; + } + let saved_index = stack.pop().unwrap(); + res[saved_index] = nums[i % nums.len()]; + } + stack.push(i % nums.len()); + } + + res + } +} +```

From 264b72f3c63c937e27262b7e92fba027eed6e93c Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 18:10:48 +0800 Subject: [PATCH 0698/1533] =?UTF-8?q?Update=200797.=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E7=9A=84=E8=B7=AF=E5=BE=84.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...75\347\232\204\350\267\257\345\276\204.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index ec8288c608..9d14bd7caa 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -217,7 +217,29 @@ class Solution: self.path.pop() # 回溯 ``` +### Rust + +```rust +impl Solution { + pub fn all_paths_source_target(graph: Vec>) -> Vec> { + let (mut res, mut path) = (vec![], vec![0]); + Self::dfs(&graph, &mut path, &mut res, 0); + res + } + pub fn dfs(graph: &Vec>, path: &mut Vec, res: &mut Vec>, node: usize) { + if node == graph.len() - 1 { + res.push(path.clone()); + return; + } + for &v in &graph[node] { + path.push(v); + Self::dfs(graph, path, res, v as usize); + path.pop(); + } + } +} +```

From 0f1505600e2c0ab505db9111923bad63ab4e41c1 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 19:26:18 +0800 Subject: [PATCH 0699/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md=20about=20rus?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\345\271\277\346\220\234\347\211\210.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 5b9d90aa31..8bbedb5965 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -240,6 +240,47 @@ class Solution: ``` +### Rust + +```rust + +use std::collections::VecDeque; +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + pub fn num_islands(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + let mut res = 0; + for (i, chars) in grid.iter().enumerate() { + for (j, &c) in chars.iter().enumerate() { + if !visited[i][j] && c == '1' { + res += 1; + Self::bfs(&grid, &mut visited, (i as i32, j as i32)); + } + } + } + res + } + + pub fn bfs(grid: &Vec>, visited: &mut Vec>, (x, y): (i32, i32)) { + let mut queue = VecDeque::new(); + queue.push_back((x, y)); + visited[x as usize][y as usize] = true; + while let Some((cur_x, cur_y)) = queue.pop_front() { + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (cur_x + dx, cur_y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + let (nx, ny) = (nx as usize, ny as usize); + if grid[nx][ny] == '1' && !visited[nx][ny] { + visited[nx][ny] = true; + queue.push_back((nx as i32, ny as i32)); + } + } + } + } +} +```

From 0134c6c13c2543eb4b7ffb1ca83056b4e0f0be3f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 19:30:02 +0800 Subject: [PATCH 0700/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md=20about?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index f610e32330..aaead2a7b7 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -279,6 +279,42 @@ class Solution: return result ``` +Rust: + + +use std::collections::VecDeque; +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + pub fn num_islands(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + let mut res = 0; + for (i, chars) in grid.iter().enumerate() { + for (j, &c) in chars.iter().enumerate() { + if !visited[i][j] && c == '1' { + res += 1; + Self::dfs(&grid, &mut visited, (i as i32, j as i32)); + } + } + } + res + } + + pub fn dfs(grid: &Vec>, visited: &mut Vec>, (x, y): (i32, i32)) { + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + let (nx, ny) = (nx as usize, ny as usize); + if grid[nx][ny] == '1' && !visited[nx][ny] { + visited[nx][ny] = true; + Self::dfs(grid, visited, (nx as i32, ny as i32)); + } + } + } + +} +

From b751d1f24d19daba1ff3b3ce4c39377d6d187a74 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 19:30:59 +0800 Subject: [PATCH 0701/1533] Update --- ...5\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index aaead2a7b7..905c0979fc 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -282,7 +282,7 @@ class Solution: Rust: -use std::collections::VecDeque; +```rust impl Solution { const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; pub fn num_islands(grid: Vec>) -> i32 { @@ -312,8 +312,8 @@ impl Solution { } } } - } +```

From eedb042024fe26857c038a8bfc7490c9829462e5 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:00:22 +0800 Subject: [PATCH 0702/1533] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 186f044c40..9ce4e56c37 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -390,6 +390,55 @@ class Solution: if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]): self.dfs(grid, visited, new_x, new_y) ``` + +### Rust + +dfs: 版本一 + +```rust +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn max_area_of_island(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + + let mut res = 0; + for (i, nums) in grid.iter().enumerate() { + for (j, &num) in nums.iter().enumerate() { + if !visited[i][j] && num == 1 { + let mut count = 1; + visited[i][j] = true; + Self::dfs(&grid, &mut visited, (i as i32, j as i32), &mut count); + res = res.max(count); + } + } + } + + res + } + + pub fn dfs( + grid: &Vec>, + visited: &mut [Vec], + (x, y): (i32, i32), + count: &mut i32, + ) { + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + let (nx, ny) = (nx as usize, ny as usize); + if !visited[nx][ny] && grid[nx][ny] == 1 { + visited[nx][ny] = true; + *count += 1; + Self::dfs(grid, visited, (nx as i32, ny as i32), count); + } + } + } +} +``` +

From d02c6a7d178ed420dfa404e091b6f7da0118185f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:09:24 +0800 Subject: [PATCH 0703/1533] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 9ce4e56c37..2d25c54ce0 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -439,6 +439,51 @@ impl Solution { } ``` +dfs: 版本二 + +```rust +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn max_area_of_island(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + + let mut res = 0; + for (i, nums) in grid.iter().enumerate() { + for (j, &num) in nums.iter().enumerate() { + if !visited[i][j] && num == 1 { + let mut count = 0; + Self::dfs(&grid, &mut visited, (i as i32, j as i32), &mut count); + res = res.max(count); + } + } + } + + res + } + + pub fn dfs( + grid: &Vec>, + visited: &mut [Vec], + (x, y): (i32, i32), + count: &mut i32, + ) { + if visited[x as usize][y as usize] || grid[x as usize][y as usize] == 0 { + return; + } + visited[x as usize][y as usize] = true; + *count += 1; + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + Self::dfs(grid, visited, (nx, ny), count); + } + } +} +``` +

From c7ec1d7705b108257983f1b192126f2bec2888b4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:27:28 +0800 Subject: [PATCH 0704/1533] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 2d25c54ce0..b8b2ea72f5 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -484,6 +484,53 @@ impl Solution { } ``` +bfs: + +```rust +use std::collections::VecDeque; +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn max_area_of_island(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + + let mut res = 0; + for (i, nums) in grid.iter().enumerate() { + for (j, &num) in nums.iter().enumerate() { + if !visited[i][j] && num == 1 { + let mut count = 0; + Self::bfs(&grid, &mut visited, (i as i32, j as i32), &mut count); + res = res.max(count); + } + } + } + + res + } + + pub fn bfs(grid: &[Vec], visited: &mut [Vec], (x, y): (i32, i32), count: &mut i32) { + let mut queue = VecDeque::new(); + queue.push_back((x, y)); + visited[x as usize][y as usize] = true; + *count += 1; + while let Some((cur_x, cur_y)) = queue.pop_front() { + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (cur_x + dx, cur_y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + let (nx, ny) = (nx as usize, ny as usize); + if !visited[nx][ny] && grid[nx][ny] == 1 { + visited[nx][ny] = true; + queue.push_back((nx as i32, ny as i32)); + *count += 1; + } + } + } + } +} +``` +

From 1a54008ef0456c41c9c20a2b0bb4438c284bee1f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:32:55 +0800 Subject: [PATCH 0705/1533] Apply suggestions from code review --- ...32\204\346\234\200\345\244\247\351\235\242\347\247\257.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index b8b2ea72f5..3dd181a3ef 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -418,7 +418,7 @@ impl Solution { } pub fn dfs( - grid: &Vec>, + grid: &[Vec], visited: &mut [Vec], (x, y): (i32, i32), count: &mut i32, @@ -463,7 +463,7 @@ impl Solution { } pub fn dfs( - grid: &Vec>, + grid: &[Vec], visited: &mut [Vec], (x, y): (i32, i32), count: &mut i32, From 90ce46042d3835cb8ecff1689253e2786816ea9a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 23:08:18 +0800 Subject: [PATCH 0706/1533] =?UTF-8?q?Update=201020.=E9=A3=9E=E5=9C=B0?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\232\204\346\225\260\351\207\217.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 7538d774d2..1ef0796d98 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -491,6 +491,44 @@ class Solution: return ans ``` +### Rust + +dfs: + +```rust +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn num_enclaves(mut grid: Vec>) -> i32 { + for i in 0..grid.len() { + for j in 0..grid[0].len() { + if (i == 0 || i == grid.len() - 1 || j == 0 || j == grid[0].len() - 1) + && grid[i][j] == 1 + { + Self::dfs(&mut grid, (i as i32, j as i32)); + } + } + } + grid.iter() + .map(|nums| nums.iter().filter(|&&num| num == 1).count() as i32) + .sum() + } + + pub fn dfs(grid: &mut [Vec], (x, y): (i32, i32)) { + grid[x as usize][y as usize] = 0; + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + if grid[nx as usize][ny as usize] == 0 { + continue; + } + Self::dfs(grid, (nx, ny)); + } + } +} +``` ## 类似题目 From 1bc7ca5f8f364e5ae560854d32e6b96cb74408ec Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 23:15:44 +0800 Subject: [PATCH 0707/1533] =?UTF-8?q?Update=201020.=E9=A3=9E=E5=9C=B0?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\232\204\346\225\260\351\207\217.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 1ef0796d98..29c237e57b 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -530,6 +530,51 @@ impl Solution { } ``` +bfs: + +```rust +use std::collections::VecDeque; +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn num_enclaves(mut grid: Vec>) -> i32 { + for i in 0..grid.len() { + for j in 0..grid[0].len() { + if (i == 0 || i == grid.len() - 1 || j == 0 || j == grid[0].len() - 1) + && grid[i][j] == 1 + { + // Self::dfs(&mut grid, (i as i32, j as i32)); + Self::bfs(&mut grid, (i as i32, j as i32)); + } + } + } + grid.iter() + .map(|nums| nums.iter().filter(|&&num| num == 1).count() as i32) + .sum() + } + + pub fn bfs(grid: &mut [Vec], (x, y): (i32, i32)) { + let mut queue = VecDeque::new(); + queue.push_back((x, y)); + grid[x as usize][y as usize] = 0; + while let Some((cur_x, cur_y)) = queue.pop_front() { + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (cur_x + dx, cur_y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + + if grid[nx as usize][ny as usize] == 0 { + continue; + } + queue.push_back((nx, ny)); + grid[nx as usize][ny as usize] = 0; + } + } + } +} +``` + ## 类似题目 From 88ca946a7dd25e78eb50730a1285476691152337 Mon Sep 17 00:00:00 2001 From: KingArthur0205 Date: Tue, 22 Aug 2023 15:00:45 +0800 Subject: [PATCH 0708/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200034.=E5=9C=A8?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md=20C?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\344\270\252\344\275\215\347\275\256.md" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index e5266cd9ea..b412816686 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -739,6 +739,60 @@ class Solution { } ``` +### C +```c +int searchLeftBorder(int *nums, int numsSize, int target) { + int left = 0, right = numsSize - 1; + // 记录leftBorder没有被赋值的情况 + int leftBorder = -1; + // 边界为[left, right] + while (left <= right) { + // 更新middle值,等同于middle = (left + right) / 2 + int middle = left + ((right - left) >> 1); + // 若当前middle所指为target,将左边界设为middle,并向左继续寻找左边界 + if (nums[middle] == target) { + leftBorder = middle; + right = middle - 1; + } else if (nums[middle] > target) { + right = middle - 1; + } else { + left = middle + 1; + } + } + return leftBorder; +} +int searchRightBorder(int *nums, int numsSize, int target) { + int left = 0, right = numsSize - 1; + // 记录rightBorder没有被赋值的情况 + int rightBorder = -1; + while (left <= right) { + int middle = left + ((right - left) >> 1); + // 若当前middle所指为target,将右边界设为middle,并向右继续寻找右边界 + if (nums[middle] == target) { + rightBorder = middle; + left = middle + 1; + } else if (nums[middle] > target) { + right = middle - 1; + } else { + left = middle + 1; + } + } + return rightBorder; +} + +int* searchRange(int* nums, int numsSize, int target, int* returnSize){ + int leftBorder = searchLeftBorder(nums, numsSize, target); + int rightBorder = searchRightBorder(nums, numsSize, target); + + // 定义返回数组及数组大小 + *returnSize = 2; + int *resNums = (int*)malloc(sizeof(int) * 2); + resNums[0] = leftBorder; + resNums[1] = rightBorder; + return resNums; +} +``` +

From b985828838d53dc28eaf11316a2be5e0469e92d9 Mon Sep 17 00:00:00 2001 From: KingArthur0205 Date: Wed, 23 Aug 2023 12:19:29 +0800 Subject: [PATCH 0709/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00283.=E6=BF=80?= =?UTF-8?q?=E5=8A=A8=E9=9B=B6=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...83.\347\247\273\345\212\250\351\233\266.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index 42232cc0c6..fc708844bd 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -151,6 +151,24 @@ function moveZeroes(nums: number[]): void { }; ``` +### C + +```c +void moveZeroes(int* nums, int numsSize){ + int fastIndex = 0, slowIndex = 0; + for (; fastIndex < numsSize; fastIndex++) { + if (nums[fastIndex] != 0) { + nums[slowIndex++] = nums[fastIndex]; + } + } + + // 将slowIndex之后的元素变为0 + for (; slowIndex < numsSize; slowIndex++) { + nums[slowIndex] = 0; + } +} +``` + From 660d7bcdc436c758e6094e178c8193eda7120418 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 23 Aug 2023 15:12:47 +0800 Subject: [PATCH 0710/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BB=A3=E7=A0=81=E9=9A=8F=E6=83=B3=E5=BD=95=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=85=AC=E5=BC=80=E8=AF=BE=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\247\273\351\231\244\345\205\203\347\264\240.md" | 1 - "problems/0028.\345\256\236\347\216\260strStr.md" | 11 ++++++----- .../0042.\346\216\245\351\233\250\346\260\264.md" | 5 ++++- ...\267\263\350\267\203\346\270\270\346\210\217II.md" | 1 - ...\236\272\346\227\213\347\237\251\351\230\265II.md" | 1 - ...44\270\215\345\220\214\350\267\257\345\276\204.md" | 1 + ...\270\215\345\220\214\350\267\257\345\276\204II.md" | 1 + ...45\244\247\347\232\204\347\237\251\345\275\242.md" | 4 ++++ ...47\232\204\345\255\220\345\272\217\345\210\227.md" | 3 +++ ...47\232\204\345\255\220\346\225\260\347\273\204.md" | 1 - ...47\255\211\345\222\214\345\255\220\351\233\206.md" | 2 ++ ...6\233\264\345\244\247\345\205\203\347\264\240I.md" | 4 ++++ ...\233\264\345\244\247\345\205\203\347\264\240II.md" | 5 +++++ ...46\226\207\345\255\220\345\272\217\345\210\227.md" | 4 ++++ ...45\233\236\346\226\207\345\255\220\344\270\262.md" | 5 ++++- ...46\217\222\345\205\245\346\223\215\344\275\234.md" | 1 - ...46\257\217\346\227\245\346\270\251\345\272\246.md" | 4 ++++ ...46\240\221\346\200\273\347\273\223\347\257\207.md" | 1 - ...47\254\246\344\270\262\346\200\273\347\273\223.md" | 1 - ...47\220\206\350\256\272\345\237\272\347\241\200.md" | 3 +++ ...51\223\276\350\241\250\347\233\270\344\272\244.md" | 1 - 21 files changed, 45 insertions(+), 15 deletions(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 40ee3a2eae..3337d42e46 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -444,4 +444,3 @@ public class Solution { - diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 53b57fd534..207a047d60 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -27,16 +27,16 @@ 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。 +## 算法公开课 -## 思路 - -本题是KMP 经典题目。 - -以下文字如果看不进去,可以看我的B站视频: +本题是KMP 经典题目。以下文字如果看不进去,可以看[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html),相信结合视频再看本篇题解,更有助于大家对本题的理解。 * [帮你把KMP算法学个通透!B站(理论篇)](https://www.bilibili.com/video/BV1PD4y1o7nd/) * [帮你把KMP算法学个通透!(求next数组代码篇)](https://www.bilibili.com/video/BV1M5411j7Xx) + +## 思路 + KMP的经典思想就是:**当出现字符串不匹配时,可以记录一部分之前已经匹配的文本内容,利用这些信息避免从头再去做匹配。** 本篇将以如下顺序来讲解KMP, @@ -1362,3 +1362,4 @@ impl Solution { + diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 1f1a543b35..760ebc34a4 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -28,6 +28,10 @@ * 输入:height = [4,2,0,3,2,5] * 输出:9 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调栈,经典来袭!LeetCode:42.接雨水](https://www.bilibili.com/video/BV1uD4y1u75P/),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 @@ -1029,4 +1033,3 @@ impl Solution { - diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 02c8e4862e..fbd848665b 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -469,4 +469,3 @@ impl Solution { - diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 78d9385a23..515c4fb513 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -744,4 +744,3 @@ end - diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 985c7575af..b7ce542ebe 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -559,3 +559,4 @@ public class Solution + diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 3d243a7a8f..8b842858e7 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -739,3 +739,4 @@ object Solution { + diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 9b76229af0..47ed0f3385 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -20,6 +20,10 @@ * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调栈,又一次经典来袭! LeetCode:84.柱状图中最大的矩形](https://www.bilibili.com/video/BV1Ns4y1o7uB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解! diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index d925c5dede..b1de0cebbe 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -21,6 +21,9 @@ * 0 <= s.length, t.length <= 1000 * s 和 t 由英文字母组成 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列,为了编辑距离做铺垫 | LeetCode:115.不同的子序列](https://www.bilibili.com/video/BV1fG4y1m75Q/),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 4b1d0e9608..afce264695 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -563,4 +563,3 @@ public class Solution { - diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 2b2be1037d..81eb4c807a 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -4,6 +4,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 416. 分割等和子集 [力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/) @@ -730,3 +731,4 @@ object Solution { + diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 6bcafafba2..f01d8333a3 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -37,6 +37,10 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位 * nums1和nums2中所有整数 互不相同 * nums1 中的所有整数同样出现在 nums2 中 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调栈,套上一个壳子就有点绕了| LeetCode:496.下一个更大元素](https://www.bilibili.com/video/BV1jA411m7dX/),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index d211a68072..3bddd29877 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -21,6 +21,10 @@ * 1 <= nums.length <= 10^4 * -10^9 <= nums[i] <= 10^9 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调栈,成环了可怎么办?LeetCode:503.下一个更大元素II](https://www.bilibili.com/video/BV15y4y1o7Dw/),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 @@ -294,3 +298,4 @@ impl Solution { + diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index 8092758389..7df2aa3d15 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -27,6 +27,10 @@ * 1 <= s.length <= 1000 * s 只包含小写英文字母 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划再显神通,LeetCode:516.最长回文子序列](https://www.bilibili.com/video/BV1d8411K7W6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + ## 思路 diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index fdf83736a0..0ed49bc1af 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -26,6 +26,10 @@ 提示:输入的字符串长度不会超过 1000 。 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,字符串性质决定了DP数组的定义 | LeetCode:647.回文子串](https://www.bilibili.com/video/BV17G4y1y7z9/),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 ### 暴力解法 @@ -528,4 +532,3 @@ function expandRange(s: string, left: number, right: number): number { - diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 511d161c70..1042b5a740 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -697,4 +697,3 @@ impl Solution { - diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index fc1a80631c..fdb11c63e3 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -17,6 +17,10 @@ 提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调栈,你该了解的,这里都讲了!LeetCode:739.每日温度](https://www.bilibili.com/video/BV1my4y1Z7jj/),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + ## 思路 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" index 829495438d..739184bbf7 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -163,4 +163,3 @@ - diff --git "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" index 5c2f016471..df4db78729 100644 --- "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" +++ "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" @@ -128,4 +128,3 @@ KMP算法是字符串查找最重要的算法,但彻底理解KMP并不容易 - diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index 0b90da6a7e..cac2929251 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -11,6 +11,9 @@ 贪心算法大纲 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法理论基础!](https://www.bilibili.com/video/BV1WK4y1R71x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 什么是贪心 diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 833eadca6a..5de9da5c06 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -508,4 +508,3 @@ object Solution { - From a99b89d002cf237ed7da4b8eabd3d142efe2e849 Mon Sep 17 00:00:00 2001 From: TFTree <83645868+TFTree@users.noreply.github.com> Date: Thu, 24 Aug 2023 08:22:19 +0800 Subject: [PATCH 0711/1533] =?UTF-8?q?=E5=B9=B6=E6=9F=A5=E9=9B=86=E9=85=8D?= =?UTF-8?q?=E5=9B=BE=E5=8B=98=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 正确的图片应该是9指向6而不是9指向2 --- ...\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 5cd753e9ba..973c661e42 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -292,7 +292,7 @@ join(2, 9); 此时我们生成的的有向图为: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230526122203.png) +![](https://cdn.tftree.top/others/jion.jpg) 有录友可能想,`join(3, 8)` 在图中为什么 将 元素1 连向元素 3 而不是将 元素 8 连向 元素 3 呢? From d69a1b161a902f99a2e65b04fa8fe8b977f1973f Mon Sep 17 00:00:00 2001 From: TFTree <83645868+TFTree@users.noreply.github.com> Date: Thu, 24 Aug 2023 08:55:23 +0800 Subject: [PATCH 0712/1533] =?UTF-8?q?=E5=B9=B6=E6=9F=A5=E9=9B=86=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=8B=98=E8=AF=AF(=E4=B8=8D?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=BE=E7=89=87)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改代码使其与图片内容吻合 --- ...233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 5cd753e9ba..2f61b09d2f 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -285,9 +285,8 @@ join(1, 8); join(3, 8); join(1, 7); join(8, 5); -join(6, 2); join(2, 9); - +join(6, 2); ``` 此时我们生成的的有向图为: From aa1cc80e3cc394ca5b0207db84c1a3a790d035da Mon Sep 17 00:00:00 2001 From: KingArthur0205 Date: Thu, 24 Aug 2023 10:47:38 +0800 Subject: [PATCH 0713/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E8=8A=82=E7=82=B9C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...347\220\206\350\256\272\345\237\272\347\241\200.md" | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index da5a1b0244..9dc4524269 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -241,6 +241,16 @@ impl ListNode { ``` +### C: + +```c +typedef struct ListNodeT { + int val; + struct ListNodeT next; +} ListNode; +``` + +

From 1e207bfc6b2f546c8a38ed005416076d0da9ef46 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 24 Aug 2023 20:47:01 +0800 Subject: [PATCH 0714/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 8d448739c0..a7f00ffed9 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -764,6 +764,35 @@ object Solution { } ``` +### Ruby + +```ruby + +def combine(n, k) + result = [] + path = [] + backtracking(result, path, n, 1, k) + return result +end + +#剪枝优化 +def backtracking(result, path, n, j, k) + if path.size == k + result << path.map {|item| item} + return + end + + for i in j..(n-(k - path.size)) + 1 + #处理节点 + path << i + backtracking(result, path, n, i + 1, k) + #回溯,撤销处理过的节点 + path.pop + end +end + +``` +

From 2393f86448a3ae64514bfc4dea3ab0bb2d5fc8f0 Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 11:17:01 +0800 Subject: [PATCH 0715/1533] =?UTF-8?q?=E6=94=B9=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\213\350\275\254\345\255\227\347\254\246\344\270\262.md" | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index a3fb7ab330..f58135eb2b 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -120,8 +120,9 @@ class Solution { ``` ```java -//解法二:空间复杂度:O(1)。用原始数组来进行反转操作 -//思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个 +// 解法二 +// 空间复杂度:O(n)。String 的 toCharArray() 方法底层会 new 一个和原字符串相同大小的 char 数组 +// 思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个 class Solution { public String reverseLeftWords(String s, int n) { char[] chars = s.toCharArray(); @@ -418,3 +419,4 @@ impl Solution { + From a03b6a37de06660adabcb44fc32fdaed2fc54046 Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 11:52:13 +0800 Subject: [PATCH 0716/1533] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index f58135eb2b..310f2092e1 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -418,5 +418,4 @@ impl Solution {

- - + \ No newline at end of file From a2f4933faa0dcaf2d4de49ab3d951f1e20261ca0 Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 11:54:02 +0800 Subject: [PATCH 0717/1533] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 310f2092e1..a516213c3c 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -418,4 +418,4 @@ impl Solution {

- \ No newline at end of file + From fd5a171e886df7c9cd8090272f272b4e2a16fc5d Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 20:15:48 +0800 Subject: [PATCH 0718/1533] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=A4=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 0c1a526f38..bbd793d1ac 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -311,7 +311,7 @@ class Solution { ``` ```java -//解法三:双反转+移位,在原始数组上进行反转。空间复杂度O(1) +//解法三:双反转+移位,String 的 toCharArray() 方法底层会 new 一个和原字符串相同大小的 char 数组,空间复杂度:O(n) class Solution { /** * 思路: @@ -976,3 +976,4 @@ char * reverseWords(char * s){ + From 43e0fd9d7712453fd884d4a319c89e7f8edd0c9b Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 20:23:05 +0800 Subject: [PATCH 0719/1533] =?UTF-8?q?151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=20-=20Ja?= =?UTF-8?q?va=E9=94=99=E8=AF=AF=E6=B3=A8=E9=87=8A=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 1 - 1 file changed, 1 deletion(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index bbd793d1ac..111c07e4f5 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -976,4 +976,3 @@ char * reverseWords(char * s){ - From ca197783d20194a10202d9033e5b1ec6da3c9d2a Mon Sep 17 00:00:00 2001 From: Siqi Cao <90129698+gitcsq@users.noreply.github.com> Date: Sun, 27 Aug 2023 14:13:18 -0400 Subject: [PATCH 0720/1533] =?UTF-8?q?Update=20=E4=BF=AE=E6=AD=A3=200225.?= =?UTF-8?q?=E7=94=A8=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=20python?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\345\256\236\347\216\260\346\240\210.md" | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 13b742f839..14b7beb916 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -454,13 +454,34 @@ class MyStack: def top(self) -> int: """ + 写法一: 1. 首先确认不空 - 2. 我们仅有in会存放数据,所以返回第一个即可 + 2. 我们仅有in会存放数据,所以返回第一个即可(这里实际上用到了栈) + 写法二: + 1. 首先确认不空 + 2. 因为队列的特殊性,FIFO,所以我们只有在pop()的时候才会使用queue_out + 3. 先把queue_in中的所有元素(除了最后一个),依次出列放进queue_out + 4. 交换in和out,此时out里只有一个元素 + 5. 把out中的pop出来,即是原队列的最后一个,并使用temp变量暂存 + 6. 把temp追加到queue_in的末尾 """ + # 写法一: + # if self.empty(): + # return None + + # return self.queue_in[-1] # 这里实际上用到了栈,因为直接获取了queue_in的末尾元素 + + # 写法二: if self.empty(): return None + + for i in range(len(self.queue_in) - 1): + self.queue_out.append(self.queue_in.popleft()) - return self.queue_in[-1] + self.queue_in, self.queue_out = self.queue_out, self.queue_in + temp = self.queue_out.popleft() + self.queue_in.append(temp) + return temp def empty(self) -> bool: @@ -488,9 +509,19 @@ class MyStack: return self.que.popleft() def top(self) -> int: + # 写法一: + # if self.empty(): + # return None + # return self.que[-1] + + # 写法二: if self.empty(): return None - return self.que[-1] + for i in range(len(self.que)-1): + self.que.append(self.que.popleft()) + temp = self.que.popleft() + self.que.append(temp) + return temp def empty(self) -> bool: return not self.que From a01c3f78cf18a0eed0e236bdb48369c662e10b12 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 28 Aug 2023 15:25:11 +0800 Subject: [PATCH 0721/1533] Update --- ...\236\272\346\227\213\347\237\251\351\230\265II.md" | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 78d9385a23..cab955b609 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -211,10 +211,6 @@ class Solution: ```javascript -/** - * @param {number} n - * @return {number[][]} - */ var generateMatrix = function(n) { let startX = startY = 0; // 起始位置 let loop = Math.floor(n/2); // 旋转圈数 @@ -226,11 +222,11 @@ var generateMatrix = function(n) { while (loop--) { let row = startX, col = startY; // 上行从左到右(左闭右开) - for (; col < startY + n - offset; col++) { + for (; col < n - offset; col++) { res[row][col] = count++; } // 右列从上到下(左闭右开) - for (; row < startX + n - offset; row++) { + for (; row < n - offset; row++) { res[row][col] = count++; } // 下行从右到左(左闭右开) @@ -247,7 +243,7 @@ var generateMatrix = function(n) { startY++; // 更新offset - offset += 2; + offset += 1; } // 如果n为奇数的话,需要单独给矩阵最中间的位置赋值 if (n % 2 === 1) { @@ -255,6 +251,7 @@ var generateMatrix = function(n) { } return res; }; + ``` From 99eb035fee885decbd4438145c3f48d28e981e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Tue, 29 Aug 2023 11:12:04 +0800 Subject: [PATCH 0722/1533] =?UTF-8?q?Update=200051.N=E7=9A=87=E5=90=8E.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 52 -------------------- 1 file changed, 52 deletions(-) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 71c40238ec..6bc4fa78c6 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -345,58 +345,6 @@ class Solution { } } ``` -```java -//该方法主要特点在于用一个一维数组暂存皇后的位置,数组下标代表行,数组下标的值代表列,并初始化一个长度为n-1,值全为'.'的模板字符串 -//在找到合法方案时,遍历数组,在模板字符串下标为数组值的位置插入Q -class Solution { - //结果 - List> result = new ArrayList<>(); - //储存皇后的位置,下标为行,值为列 - int[] queenIndex; - //棋盘中一行的字符串模板 - String template; - int size; - - public List> solveNQueens(int n) { - size = n; - template = ".".repeat(n - 1); - queenIndex = new int[n]; - deal(0); - return result; - } - - void deal(int index) { - //遍历当前行的所有位置(尝试在这行每个位置放置皇后) - for (int i = 0; i < size; i++) { - queenIndex[index] = i; - //检查在当前位置放置皇后是否合法 - if (check(index, i)) { - //如果当前的行是最后一行,就说明当前皇后的摆放已经是完整并且合法的,在结果集中加入当前的摆放方案 - if (index == size - 1) { - List tmp = new ArrayList<>(size); - //遍历当前的皇后位置,在模板字符串对应的位置加入Q,再加入到结果集中 - for (Integer integer : queenIndex) { - tmp.add(new StringBuilder(template).insert(integer, "Q").toString()); - } - result.add(tmp); - return; - } - //如果当前不是最后一行,还需要进入下一行 - deal(index + 1); - } - } - } - - boolean check(int rowIndex, int columnIndex) { - for (int i = 0; i < rowIndex; i++) { - if (queenIndex[i] == columnIndex || (Math.abs(queenIndex[i] - columnIndex) == Math.abs(i - rowIndex))) { - return false; - } - } - return true; - } -} -``` ### Python From 7d770507c3217eb69bd0b15ec4a8eb6bfad56010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88?= <76643786+Projecthappy@users.noreply.github.com> Date: Tue, 29 Aug 2023 11:23:38 +0800 Subject: [PATCH 0723/1533] =?UTF-8?q?=E5=AE=8C=E5=96=84java=E6=96=B0?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 151 +++++++++--------- 1 file changed, 73 insertions(+), 78 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 2498fbfe53..ab144f43ce 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -261,84 +261,6 @@ for (pair& target : targets[result[result.size() - 1]]) ### Java -```java -/* 首先遍历所有机票,将机票转换成map,其中起点作为key,终点按照字典顺序插入到终点的列表中 - 然后进入递归,递归中首先在result中加入当前位置,如果票用完了就说明这条路走通了 - 递归中遍历从当前位置出发的终点(也就是map中key为当前位置的列表),因为列表是有序的,因此只要出现能走通的路径,这条路径就是字典排序最低的 - 在遍历中,首先从map中移除当前的机票,将遍历到的终点作为起点进入下一层的递归中,如果发现当前机票往后走走不通,再把当前的机票加到map中*/ -class Solution { - //key为起点,value是有序的终点的列表 - Map> ticketMap = new HashMap<>(); - LinkedList result = new LinkedList<>(); - - public List findItinerary(List> tickets) { - //遍历tickets,存入ticketMap中 - for (List ticket : tickets) { - addNew(ticket.get(0), ticket.get(1)); - } - deal("JFK"); - return result; - } - - boolean deal(String currentLocation) { - result.add(currentLocation); - //机票全部用完,找到最小字符路径 - if (ticketMap.isEmpty()) { - return true; - } - //当前位置的终点列表 - LinkedList targetLocations = ticketMap.get(currentLocation); - //机票没用完,但是没有从当前位置出发的机票了,说明这条路走不通 - if (targetLocations == null) { - return false; - } - //终点列表中遍历到的终点 - String targetLocation; - //遍历从当前位置出发的机票 - for (int i = 0; i < targetLocations.size(); i++) { - targetLocation = targetLocations.get(i); - //删除map中的机票,如果当前位置只有一个终点,直接删除k,v,有多个终点则删除终点列表中当前的终点 - if (targetLocations.size() == 1) { - ticketMap.remove(currentLocation); - } else { - targetLocations.remove(i); - } - //递归 - if (deal(targetLocation)) { - return true; - } else { - //路线走不通,将机票重新加到map中 - addNew(currentLocation, targetLocation); - result.removeLast(); - } - } - return false; - } - - /** - * 在map中添加新元素 - * - * @param start 起点 - * @param end 终点 - */ - void addNew(String start, String end) { - LinkedList startAllEnd = ticketMap.get(start); - if (startAllEnd != null) { - for (int i = 0; i < startAllEnd.size() + 1; i++) { - if (i == startAllEnd.size() || end.compareTo(startAllEnd.get(i)) < 0) { - startAllEnd.add(i, end); - break; - } - } - } else { - LinkedList ends = new LinkedList<>(); - ends.add(end); - ticketMap.put(start, ends); - } - } -} -``` - ```java class Solution { private LinkedList res; @@ -423,6 +345,79 @@ class Solution { } ``` +```java +/* 该方法是对第二个方法的改进,主要变化在于将某点的所有终点变更为链表的形式,优点在于 + 1.添加终点时直接在对应位置添加节点,避免了TreeMap增元素时的频繁调整 + 2.同时每次对终点进行增加删除查找时直接通过下标操作,避免hashMap反复计算hash*/ +class Solution { + //key为起点,value是有序的终点的列表 + Map> ticketMap = new HashMap<>(); + LinkedList result = new LinkedList<>(); + int total; + + public List findItinerary(List> tickets) { + total = tickets.size() + 1; + //遍历tickets,存入ticketMap中 + for (List ticket : tickets) { + addNew(ticket.get(0), ticket.get(1)); + } + deal("JFK"); + return result; + } + + boolean deal(String currentLocation) { + result.add(currentLocation); + //机票全部用完,找到最小字符路径 + if (result.size() == total) { + return true; + } + //当前位置的终点列表 + LinkedList targetLocations = ticketMap.get(currentLocation); + //没有从当前位置出发的机票了,说明这条路走不通 + if (targetLocations != null && !targetLocations.isEmpty()) { + //终点列表中遍历到的终点 + String targetLocation; + //遍历从当前位置出发的机票 + for (int i = 0; i < targetLocations.size(); i++) { + targetLocation = targetLocations.get(i); + //删除终点列表中当前的终点 + targetLocations.remove(i); + //递归 + if (deal(targetLocation)) { + return true; + } + //路线走不通,将机票重新加回去 + targetLocations.add(i, targetLocation); + result.removeLast(); + } + } + return false; + } + + /** + * 在map中按照字典顺序添加新元素 + * + * @param start 起点 + * @param end 终点 + */ + void addNew(String start, String end) { + LinkedList startAllEnd = ticketMap.getOrDefault(start, new LinkedList<>()); + if (!startAllEnd.isEmpty()) { + for (int i = 0; i < startAllEnd.size(); i++) { + if (end.compareTo(startAllEnd.get(i)) < 0) { + startAllEnd.add(i, end); + return; + } + } + startAllEnd.add(startAllEnd.size(), end); + } else { + startAllEnd.add(end); + ticketMap.put(start, startAllEnd); + } + } +} +``` + ### Python 回溯 使用used数组 From 4d532348fc1714de1cb07c0e28710b1228188aab Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 29 Aug 2023 19:12:30 +0800 Subject: [PATCH 0724/1533] Update --- ...77\347\232\204\345\221\250\351\225\277.md" | 31 +++++++++++++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 2 -- ...06\350\256\272\345\237\272\347\241\200.md" | 1 - 3 files changed, 31 insertions(+), 3 deletions(-) diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 48e64d0bd2..2e954e30ad 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -10,6 +10,37 @@ [力扣题目链接](https://leetcode.cn/problems/island-perimeter/) +给定一个 row x col 的二维网格地图 grid ,其中:grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域。 + +网格中的格子 水平和垂直 方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。 + +岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。 + + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230829180848.png) + +* 输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] +* 输出:16 +* 解释:它的周长是上面图片中的 16 个黄色的边 + + +示例 2: + +* 输入:grid = [[1]] +* 输出:4 + +示例 3: + +* 输入:grid = [[1,0]] +* 输出:4 + +提示: + +* row == grid.length +* col == grid[i].length +* 1 <= row, col <= 100 +* grid[i][j] 为 0 或 1 + ## 思路 岛屿问题最容易让人想到BFS或者DFS,但是这道题还真的没有必要,别把简单问题搞复杂了。 diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 5cd753e9ba..bc520e8ee8 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ # 并查集理论基础 -图论中,关于深搜和广搜我们在这里:[钥匙和房间](https://mp.weixin.qq.com/s/E9NlJy9PW1oJuD8N2EURoQ) 已经更新完毕了。 - 接下来我们来讲一下并查集,首先当然是并查集理论基础。 ## 背景 diff --git "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 1f3a13728f..64ca181b90 100644 --- "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,7 +6,6 @@ # 广度优先搜索理论基础 -> 号外!!代码随想录图论内容已经计划开更了! 在[深度优先搜索](https://programmercarl.com/图论深搜理论基础.html)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 From 076d87605e29d2474f89d2e7312d2a9a2fc69818 Mon Sep 17 00:00:00 2001 From: Shixiaocaia <68102662+shixiaocaia@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:26:53 +0800 Subject: [PATCH 0725/1533] =?UTF-8?q?Update=200797.=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E7=9A=84=E8=B7=AF=E5=BE=84=20Go=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...75\347\232\204\350\267\257\345\276\204.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index ec8288c608..5c209f6030 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -217,6 +217,34 @@ class Solution: self.path.pop() # 回溯 ``` +### Go + +```go +func allPathsSourceTarget(graph [][]int) [][]int { + result := make([][]int, 0) + + var trace func(path []int, step int) + trace = func(path []int, step int){ + // 从0遍历到length-1 + if step == len(graph) - 1{ + tmp := make([]int, len(path)) + copy(tmp, path) + result = append(result, tmp) + return + } + + for i := 0; i < len(graph[step]); i++{ + next := append(path, graph[step][i]) + trace(next, graph[step][i]) + } + } + // 从0开始,开始push 0进去 + trace([]int{0}, 0) + return result +} + +``` +

From 0bfd53394e9062d284586319a50aaf6cc23755eb Mon Sep 17 00:00:00 2001 From: Shixiaocaia <68102662+shixiaocaia@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:35:06 +0800 Subject: [PATCH 0726/1533] =?UTF-8?q?Update=200797.=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E7=9A=84=E8=B7=AF=E5=BE=84=20Go=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新函数名为dfs --- ...57\350\203\275\347\232\204\350\267\257\345\276\204.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index 5c209f6030..e97a6d0dbd 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -223,8 +223,8 @@ class Solution: func allPathsSourceTarget(graph [][]int) [][]int { result := make([][]int, 0) - var trace func(path []int, step int) - trace = func(path []int, step int){ + var dfs func(path []int, step int) + dfs = func(path []int, step int){ // 从0遍历到length-1 if step == len(graph) - 1{ tmp := make([]int, len(path)) @@ -235,11 +235,11 @@ func allPathsSourceTarget(graph [][]int) [][]int { for i := 0; i < len(graph[step]); i++{ next := append(path, graph[step][i]) - trace(next, graph[step][i]) + dfs(next, graph[step][i]) } } // 从0开始,开始push 0进去 - trace([]int{0}, 0) + dfs([]int{0}, 0) return result } From 6dcc537cef392a24caa636667f1bd4730d9f2898 Mon Sep 17 00:00:00 2001 From: Shixiaocaia <68102662+shixiaocaia@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:56:10 +0800 Subject: [PATCH 0727/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88=20Go=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index f610e32330..ad4a6ede9b 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -219,7 +219,7 @@ class Solution { } ``` -Python: +### Python: ```python # 版本一 @@ -279,6 +279,56 @@ class Solution: return result ``` +### Go + +```go +func numIslands(grid [][]byte) int { + // 用1标记已访问 + visited := make([][]int, len(grid)) + for i := 0; i < len(visited); i++{ + visited[i] = make([]int, len(grid[0])) + } + + var bfs func(x, y int) + bfs = func(x, y int){ + stack := make([][]int, 0) + stack = append(stack, []int{x, y}) + moveX := []int{1, -1, 0, 0} + moveY := []int{0, 0, 1, -1} + + for len(stack) != 0{ + node := stack[len(stack) - 1] + stack = stack[:len(stack) - 1] + + for i := 0; i < 4; i++{ + dx := moveX[i] + node[0] + dy := moveY[i] + node[1] + if dx < 0 || dx >= len(grid) || dy < 0 || dy >= len(grid[0]) || visited[dx][dy] == 1{ + continue + } + visited[dx][dy] = 1 + if grid[dx][dy] == '1'{ + stack = append(stack, []int{dx,dy}) + } + } + } + } + + result := 0 + for i := 0; i < len(grid); i++{ + for j := 0; j < len(grid[0]); j++{ + if visited[i][j] == 0 && grid[i][j] == '1'{ + bfs(i, j) + visited[i][j] = 1 + result++ + } + } + } + + return result +} +``` +

From 6141fc92bb04dc9f09402b77341562efb8b952d7 Mon Sep 17 00:00:00 2001 From: han Date: Wed, 30 Aug 2023 16:28:31 +0800 Subject: [PATCH 0728/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00017.=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?= =?UTF-8?q?=E5=90=88=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\347\273\204\345\220\210.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index b3ba1e5e5c..90296efdc1 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -694,6 +694,44 @@ object Solution { } ``` +### Ruby +```ruby +def letter_combinations(digits) + letter_map = { + 2 => ['a','b','c'], + 3 => ['d','e','f'], + 4 => ['g','h','i'], + 5 => ['j','k','l'], + 6 => ['m','n','o'], + 7 => ['p','q','r','s'], + 8 => ['t','u','v'], + 9 => ['w','x','y','z'] + } + + result = [] + path = [] + + return result if digits.size == 0 + + backtracking(result, letter_map, digits.split(''), path, 0) + result +end + +def backtracking(result, letter_map, digits, path, index) + if path.size == digits.size + result << path.join('') + return + end + + hash[digits[index].to_i].each do |chr| + path << chr + #index + 1代表处理下一个数字 + backtracking(result, letter_map, digits, path, index + 1) + #回溯,撤销处理过的数字 + path.pop + end +end +```

From 9d32bab764755465017422aadab557c612e604e3 Mon Sep 17 00:00:00 2001 From: jinjjm <1185838762@qq.com> Date: Thu, 31 Aug 2023 22:14:16 +0800 Subject: [PATCH 0729/1533] =?UTF-8?q?js=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\345\271\277\346\220\234\347\211\210.md" | 36 ++++++++++++++++++ ...7.\346\267\261\346\220\234\347\211\210.md" | 33 ++++++++++++++++ ...00\345\244\247\351\235\242\347\247\257.md" | 38 +++++++++++++++++++ ...75\347\232\204\350\267\257\345\276\204.md" | 22 +++++++++++ 4 files changed, 129 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 5b9d90aa31..5acdf6e2d7 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -239,6 +239,42 @@ class Solution: visited[next_i][next_j] = True ``` +### JavaScript +```javascript +var numIslands = function (grid) { + let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 + let bfs = (grid, visited, x, y) => { + let queue = []; + queue.push([x, y]); + visited[x][y] = true; + while (queue.length) { + let top = queue.shift();//取出队列头部元素 + console.log(top) + for (let i = 0; i < 4; i++) { + let nextX = top[0] + dir[i][0] + let nextY = top[1] + dir[i][1] + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) + continue; + if (!visited[nextX][nextY] && grid[nextX][nextY] === "1") { + queue.push([nextX, nextY]) + visited[nextX][nextY] = true + } + } + } + } + let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false)) + let res = 0 + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[i].length; j++) { + if (!visited[i][j] && grid[i][j] === "1") { + ++res; + bfs(grid, visited, i, j); + } + } + } + return res +}; +```

diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index f610e32330..b8f2c992c3 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -278,6 +278,39 @@ class Solution: return result ``` +### JavaScript + +```javascript +var numIslands = function (grid) { + let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 + + let dfs = (grid, visited, x, y) => { + for (let i = 0; i < 4; i++) { + let nextX = x + dir[i][0] + let nextY = y + dir[i][1] + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) + continue; + if (!visited[nextX][nextY] && grid[nextX][nextY] === "1") { + visited[nextX][nextY] = true + dfs(grid,visited,nextX,nextY) + } + } + } + let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false)) + + let res = 0 + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[i].length; j++) { + if (!visited[i][j] && grid[i][j] === "1") { + ++res; + visited[i][j] = true; + dfs(grid, visited, i, j); + } + } + } + return res +}; +```

diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 186f044c40..894f3a7555 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -390,6 +390,44 @@ class Solution: if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]): self.dfs(grid, visited, new_x, new_y) ``` +### JavaScript +```javascript +var maxAreaOfIsland = function (grid) { + let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 + + let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false)) + + let dfs = (grid, visited, x, y, m) => { + for (let i = 0; i < 4; i++) { + let nextX = x + dir[i][0] + let nextY = y + dir[i][1] + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) + continue; + if (!visited[nextX][nextY] && grid[nextX][nextY] === 1) { + visited[nextX][nextY] = true + m = dfs(grid, visited, nextX, nextY,m+1) + } + } + return m + } + + let max = 0 + + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[i].length; j++) { + if (!visited[i][j] && grid[i][j] === 1) { + // 深度优先 + visited[i][j] = true; + let m = dfs(grid, visited, i, j, 1); + if (m > max) max = m; + } + } + } + return max +}; +``` + +

diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index ec8288c608..6b47c42eea 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -217,6 +217,28 @@ class Solution: self.path.pop() # 回溯 ``` +### JavaScript +```javascript +var allPathsSourceTarget = function(graph) { + let res=[],path=[] + + function dfs(graph,start){ + if(start===graph.length-1){ + res.push([...path]) + return; + } + for(let i=0;i From 12cc9a310b3ef7aaa0857a73f5eb12084403c9bc Mon Sep 17 00:00:00 2001 From: Pika-Lee Date: Mon, 4 Sep 2023 03:52:43 -0700 Subject: [PATCH 0730/1533] =?UTF-8?q?Update=200235.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E7=A5=96=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改文中描述,与图片对应(当然也可以修改图片) --- ...21\345\205\254\345\205\261\347\245\226\345\205\210.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 2b8af06017..b7e92e4e80 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -50,15 +50,15 @@ 因为是有序树,所有 如果 中间节点是 q 和 p 的公共祖先,那么 中节点的数组 一定是在 [p, q]区间的。即 中节点 > p && 中节点 < q 或者 中节点 > q && 中节点 < p。 -那么只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是q 和 p的公共祖先。 那问题来了,**一定是最近公共祖先吗**? +那么只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是p 和 q的公共祖先。 那问题来了,**一定是最近公共祖先吗**? -如图,我们从根节点搜索,第一次遇到 cur节点是数值在[p, q]区间中,即 节点5,此时可以说明 p 和 q 一定分别存在于 节点 5的左子树,和右子树中。 +如图,我们从根节点搜索,第一次遇到 cur节点是数值在[q, p]区间中,即 节点5,此时可以说明 q 和 p 一定分别存在于 节点 5的左子树,和右子树中。 ![235.二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926164214.png) -此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为q的祖先, 如果从节点5继续向右遍历则错过成为p的祖先。 +此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为p的祖先, 如果从节点5继续向右遍历则错过成为q的祖先。 -所以当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[p, q]区间中,那么cur就是 p和q的最近公共祖先。 +所以当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[q, p]区间中,那么cur就是 q和p的最近公共祖先。 理解这一点,本题就很好解了。 From 9ac418bf220641a1c076f576aed36c02b0f30010 Mon Sep 17 00:00:00 2001 From: Pika-Lee Date: Mon, 4 Sep 2023 03:56:33 -0700 Subject: [PATCH 0731/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert change of this file --- ...\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index e2c6d83c59..184dba604c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -150,7 +150,7 @@ 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 -**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用递归的方式来实现的。 +**之前我们讲栈与队列的时候,就说过栈其实就是递归的一种实现结构**,也就说前中后序遍历的逻辑其实都是可以借助栈使用非递归的方式来实现的。 而广度优先遍历的实现一般使用队列来实现,这也是队列先进先出的特点所决定的,因为需要先进先出的结构,才能一层一层的来遍历二叉树。 From 7e7e0cd0c71e5fb5d4032122ddec4ab70bfa6875 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 5 Sep 2023 10:22:26 +0800 Subject: [PATCH 0732/1533] Update --- README.md | 8 ++++++++ ...5\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- ...5\217\211\346\220\234\347\264\242\346\240\221.md" | 4 ++-- ...5\211\262\345\233\236\346\226\207\344\270\262.md" | 2 +- ...216\257\345\275\242\351\223\276\350\241\250II.md" | 2 +- ...5\256\236\347\216\260\351\230\237\345\210\227.md" | 2 +- ...5\205\250\345\271\263\346\226\271\346\225\260.md" | 2 +- ...1\233\266\351\222\261\345\205\221\346\215\242.md" | 2 +- ...11\223\345\256\266\345\212\253\350\210\215III.md" | 2 +- ...6\221\206\345\212\250\345\272\217\345\210\227.md" | 1 - ...5\242\236\345\255\220\345\272\217\345\210\227.md" | 2 +- .../0494.\347\233\256\346\240\207\345\222\214.md" | 2 +- ...4\270\255\347\232\204\344\274\227\346\225\260.md" | 2 +- ...233\266\351\222\261\345\205\221\346\215\242II.md" | 2 +- ...0\277\224\345\233\236\345\216\237\347\202\271.md" | 2 +- ...6\217\222\345\205\245\346\223\215\344\275\234.md" | 2 +- problems/qita/kstar.md | 4 ++-- problems/qita/server.md | 4 ++-- ...5\234\250\350\277\231\351\207\214\357\274\201.md" | 4 ---- "problems/\345\211\215\345\272\217/vim.md" | 4 ++-- ...4\273\243\347\240\201\351\243\216\346\240\274.md" | 2 +- ...0\257\221\350\277\220\350\241\214\357\274\237.md" | 2 +- ...5\205\254\345\217\270\346\200\273\347\273\223.md" | 3 --- ...5\272\217\345\221\230\347\256\200\345\216\206.md" | 7 ------- ...5\244\215\346\235\202\345\272\246\357\274\201.md" | 2 +- ...0\275\254\345\255\227\347\254\246\344\270\262.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 6 ++---- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 12 ++++++------ ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 6 ------ ...5\221\250\346\234\253\346\200\273\347\273\223.md" | 4 ++-- ...7\273\223\347\263\273\345\210\227\344\270\200.md" | 2 +- ...5\233\236\346\272\257\346\200\273\347\273\223.md" | 2 +- ...7\220\206\350\256\262\350\247\243\357\274\211.md" | 4 ++-- ...5\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +- ...6\263\225\346\200\273\347\273\223\347\257\207.md" | 4 ++-- 42 files changed, 57 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index d3b4fe6603..bf55a0041b 100644 --- a/README.md +++ b/README.md @@ -393,6 +393,14 @@ * [图论:1020.飞地的数量](./problems/1020.飞地的数量.md) * [图论:130.被围绕的区域](./problems/0130.被围绕的区域.md) * [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md) +* [图论:827.最大人工岛](./problems/0827.最大人工岛.md) +* [图论:127. 单词接龙](./problems/0127.单词接龙.md) +* [图论:841.钥匙和房间](./problems/841.钥匙和房间) +* [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md) +* [图论:并查集理论基础](./problems/) +* [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md) +* [图论:684.冗余连接](./problems/0684.冗余连接.md) +* [图论:685.冗余连接II](./problems/0685.冗余连接II.md) (持续更新中....) diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 8d58cc5a87..d109165b50 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -159,7 +159,7 @@ public: 可以看出我依然还是用动规五部曲来进行分析,会把题目的方方面面都覆盖到! -**而且具体这五部分析是我自己平时总结的经验,找不出来第二个的,可能过一阵子 其他题解也会有动规五部曲了,哈哈**。 +**而且具体这五部分析是我自己平时总结的经验,找不出来第二个的,可能过一阵子 其他题解也会有动规五部曲了**。 当时我在用动规五部曲讲解斐波那契的时候,一些录友和我反应,感觉讲复杂了。 diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index a48ec0656b..0a64266e2f 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -249,9 +249,9 @@ public: 这道题目是一个简单题,但对于没接触过的同学还是有难度的。 -所以初学者刚开始学习算法的时候,看到简单题目没有思路很正常,千万别怀疑自己智商,学习过程都是这样的,大家智商都差不多,哈哈。 +所以初学者刚开始学习算法的时候,看到简单题目没有思路很正常,千万别怀疑自己智商,学习过程都是这样的,大家智商都差不多。 -只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。 +只要把基本类型的题目都做过,总结过之后,思路自然就开阔了,加油💪 ## 其他语言版本 diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index ca73e9f7ca..80ac284309 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -300,7 +300,7 @@ public: 所以本题应该是一道hard题目了。 -**可能刷过这道题目的录友都没感受到自己原来克服了这么多难点,就把这道题目AC了**,这应该叫做无招胜有招,人码合一,哈哈哈。 +**可能刷过这道题目的录友都没感受到自己原来克服了这么多难点,就把这道题目AC了**,这应该叫做无招胜有招,人码合一。 diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index d20101a706..a643fd7091 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -187,7 +187,7 @@ public: ## 总结 -这次可以说把环形链表这道题目的各个细节,完完整整的证明了一遍,说这是全网最详细讲解不为过吧,哈哈。 +这次可以说把环形链表这道题目的各个细节,完完整整的证明了一遍,说这是全网最详细讲解不为过吧。 ## 其他语言版本 diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index c510fc12a6..2437401060 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -127,7 +127,7 @@ public: 工作中如果发现某一个功能自己要经常用,同事们可能也会用到,自己就花点时间把这个功能抽象成一个好用的函数或者工具类,不仅自己方便,也方便了同事们。 -同事们就会逐渐认可你的工作态度和工作能力,自己的口碑都是这么一点一点积累起来的!在同事圈里口碑起来了之后,你就发现自己走上了一个正循环,以后的升职加薪才少不了你!哈哈哈 +同事们就会逐渐认可你的工作态度和工作能力,自己的口碑都是这么一点一点积累起来的!在同事圈里口碑起来了之后,你就发现自己走上了一个正循环,以后的升职加薪才少不了你! diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index a0c138ee80..a0e62d4800 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -156,7 +156,7 @@ public: 但如果没有按照「代码随想录」的题目顺序来做的话,做动态规划或者做背包问题,上来就做这道题,那还是挺难的! -经过前面的训练这道题已经是简单题了,哈哈哈 +经过前面的训练这道题已经是简单题了 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 f32fd13e23..eae4ab3ac0 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" @@ -169,7 +169,7 @@ public: 这也是大多数同学学习动态规划的苦恼所在,有的时候递推公式很简单,难在遍历顺序上! -但最终又可以稀里糊涂的把题目过了,也不知道为什么这样可以过,反正就是过了,哈哈 +但最终又可以稀里糊涂的把题目过了,也不知道为什么这样可以过,反正就是过了。 那么这篇文章就把遍历顺序分析的清清楚楚。 diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 96d487e215..3646488912 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -216,7 +216,7 @@ public: 只不过平时我们习惯了在一维数组或者二维数组上推导公式,一下子换成了树,就需要对树的遍历方式足够了解! -大家还记不记得我在讲解贪心专题的时候,讲到这道题目:[贪心算法:我要监控二叉树!](https://programmercarl.com/0968.监控二叉树.html),这也是贪心算法在树上的应用。**那我也可以把这个算法起一个名字,叫做树形贪心**,哈哈哈 +大家还记不记得我在讲解贪心专题的时候,讲到这道题目:[贪心算法:我要监控二叉树!](https://programmercarl.com/0968.监控二叉树.html),这也是贪心算法在树上的应用。**那我也可以把这个算法起一个名字,叫做树形贪心**。 “树形贪心”词汇从此诞生,来自「代码随想录」 diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 943dfe39b6..5203d7d61a 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -4,7 +4,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-> 本周讲解了[贪心理论基础](https://programmercarl.com/贪心算法理论基础.html),以及第一道贪心的题目:[贪心算法:分发饼干](https://programmercarl.com/0455.分发饼干.html),可能会给大家一种贪心算法比较简单的错觉,好了,接下来几天的题目难度要上来了,哈哈。 # 376. 摆动序列 diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index e2011372f8..eb0be005ed 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -100,7 +100,7 @@ for (int i = startIndex; i < nums.size(); i++) { } ``` -**对于已经习惯写回溯的同学,看到递归函数上面的`uset.insert(nums[i]);`,下面却没有对应的pop之类的操作,应该很不习惯吧,哈哈** +**对于已经习惯写回溯的同学,看到递归函数上面的`uset.insert(nums[i]);`,下面却没有对应的pop之类的操作,应该很不习惯吧** **这也是需要注意的点,`unordered_set uset;` 是记录本层元素是否重复使用,新的一层uset都会重新定义(清空),所以要知道uset只负责本层!** diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 4a4e966c14..a90a38f71d 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -51,7 +51,7 @@ 如果跟着「代码随想录」一起学过[回溯算法系列](https://programmercarl.com/回溯总结.html)的录友,看到这道题,应该有一种直觉,就是感觉好像回溯法可以爆搜出来。 -事实确实如此,下面我也会给出相应的代码,只不过会超时,哈哈。 +事实确实如此,下面我也会给出相应的代码,只不过会超时。 这道题目咋眼一看和动态规划背包啥的也没啥关系。 diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index efbabc4ab6..29b0926d4d 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -280,7 +280,7 @@ public: * [二叉树:前中后序迭代法](https://programmercarl.com/二叉树的迭代遍历.html) * [二叉树:前中后序统一风格的迭代方式](https://programmercarl.com/二叉树的统一迭代法.html) -下面我给出其中的一种中序遍历的迭代法,其中间处理逻辑一点都没有变(我从递归法直接粘过来的代码,连注释都没改,哈哈) +下面我给出其中的一种中序遍历的迭代法,其中间处理逻辑一点都没有变(我从递归法直接粘过来的代码,连注释都没改) 代码如下: diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 7c9f0fcefb..5c6efdcb3f 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -184,7 +184,7 @@ public: * 空间复杂度: O(m) -是不是发现代码如此精简,哈哈 +是不是发现代码如此精简 ## 总结 diff --git "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" index f0d3339173..ef58739122 100644 --- "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" +++ "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" @@ -31,7 +31,7 @@ ## 思路 -这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法啥的,哈哈。 +这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法之类的。 其实就是,x,y坐标,初始为0,然后: * if (moves[i] == 'U') y++; diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 1042b5a740..265bbb5b0d 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -82,7 +82,7 @@ if (root == NULL) { 此时要明确,需要遍历整棵树么? -别忘了这是搜索树,遍历整棵搜索树简直是对搜索树的侮辱,哈哈。 +别忘了这是搜索树,遍历整棵搜索树简直是对搜索树的侮辱。 搜索树是有方向了,可以根据插入元素的数值,决定递归方向。 diff --git a/problems/qita/kstar.md b/problems/qita/kstar.md index 86983cab18..7d1b045c8e 100644 --- a/problems/qita/kstar.md +++ b/problems/qita/kstar.md @@ -14,7 +14,7 @@
-星球里的录友都可以得到我1V1的指导,**我已经详细回答了7000+个问题**: (这个回答问题数量,可以看出我有劳模的潜质 哈哈) +星球里的录友都可以得到我1V1的指导,**我已经详细回答了7000+个问题**: (这个回答问题数量,可以看出我有劳模的潜质 )
@@ -45,7 +45,7 @@
-一些录友当初也是进来 白嫖一波资料,就退款跑了 哈哈哈,不过后面又加回来,例如这位录友: +一些录友当初也是进来 白嫖一波资料,就退款跑了,不过后面又加回来,例如这位录友:
diff --git a/problems/qita/server.md b/problems/qita/server.md index d2e76d36af..7e214d7993 100644 --- a/problems/qita/server.md +++ b/problems/qita/server.md @@ -8,7 +8,7 @@ 这真是一个好问题,而且我一句两句还说不清楚,所以就专门发文来讲一讲。 -同时我还录制的一期视频,哈哈我的视频号,大家可以关注一波。 +同时我还录制的一期视频,我的视频号,大家可以关注一波。 一说到服务器,可能很多人都说搞分布式,做计算,搞爬虫,做程序后台服务,多人合作等等。 @@ -57,7 +57,7 @@ https://github.com/youngyangyang04/fileHttpServer -感兴趣的录友可以去学习一波,顺便给个star 哈哈 +感兴趣的录友可以去学习一波,顺便给个star。 ## 网站 diff --git "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index 95c7567c75..d212e8beae 100644 --- "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -4,10 +4,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-相信每一位录友都接触过时间复杂度,「代码随想录」已经也讲了一百多道经典题目了,是时候对时间复杂度来一个深度的剖析了,很早之前就写过一篇,当时文章还没有人看,Carl感觉有价值的东西值得让更多的人看到,哈哈。 - -所以重新整理的时间复杂度文章,正式和大家见面啦! - # 时间复杂度 ## 究竟什么是时间复杂度 diff --git "a/problems/\345\211\215\345\272\217/vim.md" "b/problems/\345\211\215\345\272\217/vim.md" index 5c5910d206..136a499338 100644 --- "a/problems/\345\211\215\345\272\217/vim.md" +++ "b/problems/\345\211\215\345\272\217/vim.md" @@ -7,7 +7,7 @@ 但这里我并不是说IDE不好用,IDE在 代码跟踪,引用跳转等等其实是很给力的,效率比vim高。 -我用vim的话,如果需要跟踪代码的话,就用ctag去跳转,虽然很不智能(是基于规则匹配,不是语义匹配),但加上我自己的智能就也能用(这里真的要看对代码的把握程度了,哈哈哈) +我用vim的话,如果需要跟踪代码的话,就用ctag去跳转,虽然很不智能(是基于规则匹配,不是语义匹配),但加上我自己的智能就也能用(这里真的要看对代码的把握程度了) 所以连跟踪代码都不用IDE的话,其他方面那我就更用不上IDE了。 @@ -99,5 +99,5 @@ Github地址:[https://github.com/youngyangyang04/PowerVim](https://github.com/ Gitee地址:[https://gitee.com/programmercarl/power-vim](https://gitee.com/programmercarl/power-vim) -最后,因为这个vim配置因为我一直没有宣传,所以star数量很少,哈哈哈,录友们去给个star吧,真正的开发利器,值得顶起来! +最后,因为这个vim配置因为我一直没有宣传,所以star数量很少,录友们去给个star吧,真正的开发利器,值得顶起来! diff --git "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" index 8fd8b40ca9..95f4f129da 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" @@ -21,7 +21,7 @@ * 第二种情况:这个项目没赚到钱,半死不活的,代码还没有设计也没有规范,这样对技术人员的伤害就非常大了。 -**而不注重代码风格的团队,99.99%都是第二种情况**,如果你赶上了第一种情况,那就恭喜你了,本文下面的内容可以不用看了,哈哈。 +**而不注重代码风格的团队,99.99%都是第二种情况**,如果你赶上了第一种情况,那就恭喜你了,本文下面的内容可以不用看了。 ## 代码规范 diff --git "a/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" index bcef3886e5..5b57d214db 100644 --- "a/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" @@ -53,7 +53,7 @@ int main() { 代码中可以看出,其实就是定义个main函数,构造个输入用例,然后定义一个solution变量,调用minCostClimbingStairs函数就可以了。 -此时大家就可以随意构造测试数据,然后想怎么打日志就怎么打日志,没有找不出的bug,哈哈。 +此时大家就可以随意构造测试数据,然后想怎么打日志就怎么打日志,没有找不出的bug。 diff --git "a/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index a10cab0654..7b42868e8f 100644 --- "a/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ "b/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -94,9 +94,6 @@ * 自如网 * 汽车之家 -## 总结 - -可能是我写总结写习惯了,什么文章都要有一个总结,哈哈,那么我就总结一下。 北京的互联网氛围绝对是最好的(暂不讨论户口和房价问题),大家如果看了[深圳原来有这么多互联网公司,你都知道么?](https://programmercarl.com/前序/深圳互联网公司总结.html)这篇之后,**会发现北京互联网外企和二线互联网公司数量多的优势,在深圳的互联网公司断档比较严重,如果去不了为数不多的一线公司,可选择的余地就非常少了,而北京选择的余地就很多!** diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" index 3e25b895f0..fc06c713a0 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" @@ -113,13 +113,6 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 **好的简历是敲门砖,同时也不要在简历上花费过多的精力,好的简历以及面试技巧都是锦上添花**,真的求得心得的offer靠的还是真才实学。 -如何真才实学呢? 跟着「代码随想录」一起刷题呀,哈哈 - -大家此时可以再重审一遍自己的简历,如果发现哪里的不足,面试前要多准备多练习。 - -就酱,「代码随想录」就是这么干货,Carl多年积累的简历技巧都毫不保留的写出来了,如果感觉对你有帮助,就宣传一波「代码随想录」吧,值得大家的关注! - - -----------------------
diff --git "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" index d8f07f1c59..1ea1e65be8 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" @@ -131,7 +131,7 @@ int function3(int x, int n) { return function3(x, n / 2) * function3(x, n / 2); } ``` -可以看出这道题目非常简单,但是又很考究算法的功底,特别是对递归的理解,这也是我面试别人的时候用过的一道题,所以整个情景我才写的如此逼真,哈哈。 +可以看出这道题目非常简单,但是又很考究算法的功底,特别是对递归的理解,这也是我面试别人的时候用过的一道题,所以整个情景我才写的如此逼真。 大厂面试的时候最喜欢用“简单题”来考察候选人的算法功底,注意这里的“简单题”可并不一定真的简单哦! diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index a3fb7ab330..82a968d431 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -69,7 +69,7 @@ public: * 时间复杂度: O(n) * 空间复杂度:O(1) -是不是发现这代码也太简单了,哈哈。 +是不是发现这代码也太简单了。 ## 总结 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index ff8f67d47d..594656ca23 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -135,7 +135,7 @@ public: 看完这篇文章,去leetcode上怒刷五题,文章中 编号107题目的样例图放错了(原谅我匆忙之间总是手抖),但不影响大家理解。 -只有同学发现leetcode上“515. 在每个树行中找最大值”,也是层序遍历的应用,依然可以分分钟解决,所以就是一鼓作气解决六道了,哈哈。 +只有同学发现leetcode上“515. 在每个树行中找最大值”,也是层序遍历的应用,依然可以分分钟解决,所以就是一鼓作气解决六道了。 **层序遍历遍历相对容易一些,只要掌握基本写法(也就是框架模板),剩下的就是在二叉树每一行遍历的时候做做逻辑修改。** diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index f183924eee..bbb14502c0 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -70,7 +70,7 @@ **但可以遍历一遍就可以求众数集合,使用了适时清空结果集的方法**,这个方法还是很巧妙的。相信仔细读了文章的同学会惊呼其巧妙! -**所以大家不要看题目简单了,就不动手做了,我选的题目,一般不会简单到不用动手的程度,哈哈**。 +**所以大家不要看题目简单了,就不动手做了,我选的题目,一般不会简单到不用动手的程度**。 ## 周六 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 7f6fd114b4..77db9708c0 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -98,7 +98,7 @@ 对于回溯法的整体框架,网上搜的文章这块一般都说不清楚,按照天上掉下来的代码对着讲解,不知道究竟是怎么来的,也不知道为什么要这么写。 -所以,录友们刚开始学回溯法,起跑姿势就很标准了,哈哈。 +所以,录友们刚开始学回溯法,起跑姿势就很标准了。 下周依然是回溯法,难度又要上升一个台阶了。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 3f1d10126e..9a447648d8 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -148,7 +148,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 **我讲解每一种问题,都会和其他问题作对比,做分析,所以只要跟着细心琢磨相信对回溯又有新的认识**。 -最近这两天题目有点难度,刚刚开始学回溯算法的话,按照现在这个每天一题的速度来,确实有点快,学起来吃力非常正常,这些题目都是我当初学了好几个月才整明白的,哈哈。 +最近这两天题目有点难度,刚刚开始学回溯算法的话,按照现在这个每天一题的速度来,确实有点快,学起来吃力非常正常,这些题目都是我当初学了好几个月才整明白的。 **所以大家能跟上的话,已经很优秀了!** diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 2918246a77..9209efda71 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -66,7 +66,7 @@ ## 周四 -这道题目:[贪心算法:K次取反后最大化的数组和](https://programmercarl.com/1005.K次取反后最大化的数组和.html)就比较简单了,哈哈,用简单题来讲一讲贪心的思想。 +这道题目:[贪心算法:K次取反后最大化的数组和](https://programmercarl.com/1005.K次取反后最大化的数组和.html)就比较简单了,用简单题来讲一讲贪心的思想。 **这里其实用了两次贪心!** @@ -76,8 +76,6 @@ 第二次贪心:局部最优:只找数值最小的正整数进行反转,当前数值可以达到最大(例如正整数数组{5, 3, 1},反转1 得到-1 比 反转5得到的-5 大多了),全局最优:整个 数组和 达到最大。 -[贪心算法:K次取反后最大化的数组和](https://programmercarl.com/1005.K次取反后最大化的数组和.html)中的代码,最后while处理K的时候,其实直接判断奇偶数就可以了,文中给出的方式太粗暴了,哈哈,Carl大意了。 - 例外一位录友留言给出一个很好的建议,因为文中是使用快排,仔细看题,**题目中限定了数据范围是正负一百,所以可以使用桶排序**,这样时间复杂度就可以优化为$O(n)$了。但可能代码要复杂一些了。 @@ -85,7 +83,7 @@ 大家会发现本周的代码其实都简单,但思路却很巧妙,并不容易写出来。 -如果是第一次接触的话,其实很难想出来,就是接触过之后就会了,所以大家不用感觉自己想不出来而烦躁,哈哈。 +如果是第一次接触的话,其实很难想出来,就是接触过之后就会了,所以大家不用感觉自己想不出来而烦躁。 相信此时大家现在对贪心算法又有一个新的认识了,加油💪 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" index 203e448655..cd3b2f1346 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -107,7 +107,7 @@ 本周讲解的内容都是经常被大家忽略的知识点,而通常这种知识点,才最能发现一位候选人的编程功底。 -因为之前一直都是在持续更新算法题目的文章,这周说一说算法性能分析,感觉也是换了换口味,哈哈。 +因为之前一直都是在持续更新算法题目的文章,这周说一说算法性能分析,感觉也是换了换口味。 同时大家也会发现,**大厂面试官最喜欢用“简单题”(就是看起来很简单,其实非常考验技术功底的题目),而不是要手撕红黑树之类的**。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index fe4cc4a2ab..91f9656f48 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -95,7 +95,7 @@ 虽然有时候感觉贪心就是常识,但如果真正是常识性的题目,其实是模拟题,就不是贪心算法了!例如[贪心算法:加油站](https://programmercarl.com/0134.加油站.html)中的贪心方法一,其实我就认为不是贪心算法,而是直接从全局最优的角度上来模拟,因为方法里没有体现局部最优的过程。 -而且大家也会发现,贪心并没有想象中的那么简单,贪心往往妙的出其不意,触不及防!哈哈 +而且大家也会发现,贪心并没有想象中的那么简单,贪心往往妙的出其不意,触不及防!
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 7436295af3..b37bba80dc 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -93,7 +93,7 @@ public: 本周的主题就是用贪心算法来解决区间问题,经过本周的学习,大家应该对区间的各种合并分割有一定程度的了解了。 -其实很多区间的合并操作看起来都是常识,其实贪心算法有时候就是常识,哈哈,但也别小看了贪心算法。 +其实很多区间的合并操作看起来都是常识,其实贪心算法有时候就是常识,但也别小看了贪心算法。 在[贪心算法:合并区间](https://programmercarl.com/0056.合并区间.html)中就说过,对于贪心算法,很多同学都是:「如果能凭常识直接做出来,就会感觉不到自己用了贪心, 一旦第一直觉想不出来, 可能就一直想不出来了」。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 831ce348a1..4cab00ccbd 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -18,7 +18,7 @@ 后序我们在讲解动规的题目时候,都离不开这五步! -本周都是简单题目,大家可能会感觉 按照这五部来好麻烦,凭感觉随手一写,直接就过,越到后面越会感觉,凭感觉这个事还是不靠谱的,哈哈。 +本周都是简单题目,大家可能会感觉 按照这五部来好麻烦,凭感觉随手一写,直接就过,越到后面越会感觉,凭感觉这个事还是不靠谱的。 最后我们讲了动态规划题目应该如何debug,相信一些录友做动规的题目,一旦报错也是凭感觉来改。 @@ -30,7 +30,7 @@ 2. 我打印dp数组的日志了么? 3. 打印出来了dp数组和我想的一样么? -哈哈,专治各种代码写出来了但AC不了的疑难杂症。 +专治各种代码写出来了但AC不了的疑难杂症。 ## 周二 @@ -60,7 +60,7 @@ for (int i = 3; i <= n; i++) { // 注意i是从3开始的 } ``` -这个可以是面试的一个小问题,哈哈,考察候选人对dp[i]定义的理解程度。 +这个可以是面试的一个小问题,考察候选人对dp[i]定义的理解程度。 这道题目还可以继续深化,就是一步一个台阶,两个台阶,三个台阶,直到 m个台阶,有多少种方法爬到n阶楼顶。 @@ -86,7 +86,7 @@ public: 代码中m表示最多可以爬m个台阶。 -**以上代码不能运行哈,我主要是为了体现只要把m换成2,粘过去,就可以AC爬楼梯这道题,不信你就粘一下试试,哈哈**。 +**以上代码不能运行哈,我主要是为了体现只要把m换成2,粘过去,就可以AC爬楼梯这道题,不信你就粘一下试试**。 **此时我就发现一个绝佳的大厂面试题**,第一道题就是单纯的爬楼梯,然后看候选人的代码实现,如果把dp[0]的定义成1了,就可以发难了,为什么dp[0]一定要初始化为1,此时可能候选人就要强行给dp[0]应该是1找各种理由。那这就是一个考察点了,对dp[i]的定义理解的不深入。 @@ -97,7 +97,7 @@ public: **其实大厂面试最喜欢问题的就是这种简单题,然后慢慢变化,在小细节上考察候选人**。 -这道绝佳的面试题我没有用过,如果录友们有面试别人的需求,就把这个套路拿去吧,哈哈哈。 +这道绝佳的面试题我没有用过,如果录友们有面试别人的需求,就把这个套路拿去吧。 我在[通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html)中,以我自己面试别人的真实经历,通过求x的n次方 这么简单的题目,就可以考察候选人对算法性能以及递归的理解深度,录友们可以看看,绝对有收获! @@ -138,7 +138,7 @@ public: }; ``` -这么写看上去比较顺,但是就是感觉和题目描述的不太符。哈哈,也没有必要这么细扣题意了,大家只要知道,题目的意思反正就是要不是第一步不花费,要不是最后一步不花费,都可以。 +这么写看上去比较顺,但是就是感觉和题目描述的不太符。也没有必要这么细扣题意了,大家只要知道,题目的意思反正就是要不是第一步不花费,要不是最后一步不花费,都可以。 ## 总结 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 71cb49a9db..ebc720caa0 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -151,12 +151,6 @@ n为5时候的dp数组状态如图: 我现在也陷入了纠结,题目一简单,就会有录友和我反馈说题目太简单了,题目一难,阅读量就特别低。 -我也好难那,哈哈哈。 - **但我还会坚持规划好的路线,难度循序渐进,并以面试经典题目为准,该简单的时候就是简单,同时也不会因为阅读量低就放弃有难度的题目!**。 -录友们看到这是不是得给个Carl点个赞啊[让我看看]。 - -预告,我们下周正式开始讲解背包问题,经典的不能再经典,也是比较难的一类动态规划的题目了,录友们上车抓稳咯。 -
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index e74dd6bea8..26d805bb3a 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -187,7 +187,7 @@ return {val2, val1}; 因为平时我们习惯了在一维数组或者二维数组上推导公式,一下子换成了树,就需要对树的遍历方式足够了解! -大家还记不记得我在讲解贪心专题的时候,讲到这道题目:[贪心算法:我要监控二叉树!](https://programmercarl.com/0968.监控二叉树.html),这也是贪心算法在树上的应用。**那我也可以把这个算法起一个名字,叫做树形贪心**,哈哈哈 +大家还记不记得我在讲解贪心专题的时候,讲到这道题目:[贪心算法:我要监控二叉树!](https://programmercarl.com/0968.监控二叉树.html),这也是贪心算法在树上的应用。**那我也可以把这个算法起一个名字,叫做树形贪心** “树形贪心”词汇从此诞生,来自「代码随想录」 @@ -301,7 +301,7 @@ public: 那么这里每一种情况 我在文章中都做了详细的介绍。 -周四我们开始讲解股票系列了,大家应该预测到了,我们下周的主题就是股票! 哈哈哈,多么浮躁的一个系列!敬请期待吧! +周四我们开始讲解股票系列了,大家应该预测到了,我们下周的主题就是股票!敬请期待吧! **代码随想录温馨提醒:投资有风险,入市需谨慎!** diff --git "a/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" "b/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" index f65f549705..52e2fec11f 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" @@ -139,7 +139,7 @@ public: 看完这篇文章,去leetcode上怒刷五题,文章中 编号107题目的样例图放错了(原谅我匆忙之间总是手抖),但不影响大家理解。 -只有同学发现leetcode上“515. 在每个树行中找最大值”,也是层序遍历的应用,依然可以分分钟解决,所以就是一鼓作气解决六道了,哈哈。 +只有同学发现leetcode上“515. 在每个树行中找最大值”,也是层序遍历的应用,依然可以分分钟解决,所以就是一鼓作气解决六道了。 **层序遍历遍历相对容易一些,只要掌握基本写法(也就是框架模板),剩下的就是在二叉树每一行遍历的时候做做逻辑修改。** diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index f2e46829cb..35b4c3f907 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -347,7 +347,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 **这里我明确给出了棋盘的宽度就是for循环的长度,递归的深度就是棋盘的高度,这样就可以套进回溯法的模板里了**。 -相信看完本篇[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)也没那么难了,传说已经不是传说了,哈哈。 +相信看完本篇[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)也没那么难了,传说已经不是传说了。 diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 4291c80c72..5f9b46b373 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -154,13 +154,13 @@ public: 所以对于两种使用数组的方法一和方法三,也不好确定谁优,但一定都没有使用方法二链表的效率高! -一波分析之后,对于[贪心算法:根据身高重建队列](https://programmercarl.com/0406.根据身高重建队列.html) ,大家就安心使用链表吧!别折腾了,哈哈,相当于我替大家折腾了一下。 +一波分析之后,对于[贪心算法:根据身高重建队列](https://programmercarl.com/0406.根据身高重建队列.html) ,大家就安心使用链表吧!别折腾了,相当于我替大家折腾了一下。 ## 总结 大家应该发现了,编程语言中一个普通容器的insert,delete的使用,都可能对写出来的算法的有很大影响! -如果抛开语言谈算法,除非从来不用代码写算法纯分析,**否则的话,语言功底不到位O(n)的算法可以写出O(n^2)的性能**,哈哈。 +如果抛开语言谈算法,除非从来不用代码写算法纯分析,**否则的话,语言功底不到位O(n)的算法可以写出O(n^2)的性能**。 相信在这里学习算法的录友们,都是想在软件行业长远发展的,都是要从事编程的工作,那么一定要深耕好一门编程语言,这个非常重要! diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index ac4c4a1cbe..a006cb0e20 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -173,7 +173,7 @@ int main() { 这个区别,我将在后面讲解具体leetcode题目中给大家介绍,因为这块如果不结合具题目,单纯的介绍原理估计很多同学会越看越懵! -别急,下一篇就是了!哈哈 +别急,下一篇就是了! 最后,**又可以出一道面试题了,就是纯完全背包,要求先用二维dp数组实现,然后再用一维dp数组实现,最后再问,两个for循环的先后是否可以颠倒?为什么?** 这个简单的完全背包问题,估计就可以难住不少候选人了。 diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" index c375503d71..d49cdc5fb1 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" @@ -17,7 +17,7 @@ 在刚刚讲过的回溯系列中,大家可以发现我是严格按照框架难度顺序循序渐进讲解的,**和贪心又不一样,因为回溯法如果题目顺序没选好,刷题效果会非常差!** -同样回溯系列也不允许简单困难交替着来,因为前后题目都是有因果关系的,**相信跟着刷过回溯系列的录友们都会明白我的良苦用心,哈哈**。 +同样回溯系列也不允许简单困难交替着来,因为前后题目都是有因果关系的,**相信跟着刷过回溯系列的录友们都会明白我的良苦用心**。 **每个系列都有每个系列的特点,我都会根据特点有所调整,大家看我每天的推送的题目,都不是随便找一个到就推送的,都是先有整体规划,然后反复斟酌具体题目的结果**。 @@ -116,7 +116,7 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 周总结里会对每周的题目中大家的疑问、相关难点或者笔误之类的进行复盘和总结。 -如果大家发现文章哪里有问题,那么在周总结里或者文章评论区一定进行了修正,保证不会因为我的笔误或者理解问题而误导大家,哈哈。 +如果大家发现文章哪里有问题,那么在周总结里或者文章评论区一定进行了修正,保证不会因为我的笔误或者理解问题而误导大家。 所以周总结一定要看! From 10b693a84d2aba4df15a78d716265de372113633 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 5 Sep 2023 19:29:34 +0800 Subject: [PATCH 0733/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E8=B7=9D=E7=A6=BB=E5=85=AC=E5=BC=80=E8=AF=BE=E9=93=BE?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 2b5a1925dc..76553151e4 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -41,7 +41,7 @@ exection -> execution (插入 'u') * word1 和 word2 由小写英文字母组成 ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1qv4y1q78f/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -465,4 +465,3 @@ impl Solution { - From 486792cba5260965a82285414425d3a1dab30eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E6=97=A0=E7=BC=BA?= Date: Wed, 6 Sep 2023 20:46:04 +0800 Subject: [PATCH 0734/1533] Update acm.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改错别字 --- problems/qita/acm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/qita/acm.md b/problems/qita/acm.md index 1be0e924de..999283564a 100644 --- a/problems/qita/acm.md +++ b/problems/qita/acm.md @@ -1,5 +1,5 @@ -# 如何练习ACM模式输入输入模式 | 如何准备笔试 | 卡码网 +# 如何练习ACM模式输入输出模式 | 如何准备笔试 | 卡码网 卡码网地址:[https://kamacoder.com](https://kamacoder.com) From 839afd119b4a22700a374f555a0ee8d1eebd9495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=98=94?= Date: Sat, 9 Sep 2023 10:23:43 +0800 Subject: [PATCH 0735/1533] test --- ...\204\346\234\200\345\260\217\346\267\261\345\272\246.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 61f9beb785..8b9d92e612 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -173,12 +173,12 @@ private: int result; void getdepth(TreeNode* node, int depth) { // 函数递归终止条件 - if (root == nullptr) { + if (node == nullptr) { return; } // 中,处理逻辑:判断是不是叶子结点 - if (root -> left == nullptr && root->right == nullptr) { - res = min(res, depth); + if (node -> left == nullptr && node->right == nullptr) { + result = min(result, depth); } if (node->left) { // 左 getdepth(node->left, depth + 1); From 655a513b3145aef9d96db0cdc3983b47572d19cf Mon Sep 17 00:00:00 2001 From: zl127LiamLi <143149260+zl127LiamLi@users.noreply.github.com> Date: Sat, 9 Sep 2023 20:42:36 -0500 Subject: [PATCH 0736/1533] 841 python3 add BFS solution.md --- ...31\345\222\214\346\210\277\351\227\264.md" | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index 00156d4ec3..b4785d1b02 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -324,7 +324,7 @@ class Solution { ### python3 ```python - +# 深度搜索优先 class Solution: def dfs(self, key: int, rooms: List[List[int]] , visited : List[bool] ) : if visited[key] : @@ -346,6 +346,31 @@ class Solution: return False return True +# 广度搜索优先 +class Solution: + def canVisitAllRooms(self, rooms: List[List[int]]) -> bool: + visited = [False] * len(rooms) + self.bfs(rooms, 0, visited) + + for room in visited: + if room == False: + return False + + return True + + def bfs(self, rooms, index, visited): + q = collections.deque() + q.append(index) + + visited[0] = True + + while len(q) != 0: + index = q.popleft() + for nextIndex in rooms[index]: + if visited[nextIndex] == False: + q.append(nextIndex) + visited[nextIndex] = True + ``` From 4b704d3efd411677309ca6769eb8281560111aa4 Mon Sep 17 00:00:00 2001 From: wisuky <48423733+wisuky@users.noreply.github.com> Date: Tue, 12 Sep 2023 20:06:47 +0800 Subject: [PATCH 0737/1533] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strSt?= =?UTF-8?q?r.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0028.实现strStr.md --- "problems/0028.\345\256\236\347\216\260strStr.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 53b57fd534..04fef7fd00 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -207,7 +207,7 @@ next数组就是一个前缀表(prefix table)。 ### 前缀表与next数组 -很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? +很多KMP算法的实现都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? next数组就可以是前缀表,但是很多实现都是把前缀表统一减一(右移一位,初始位置为-1)之后作为next数组。 From a0462c4c2539c0b9f65acfc39b32e0bcd9d5a303 Mon Sep 17 00:00:00 2001 From: CH Ye Date: Fri, 15 Sep 2023 16:06:48 -0400 Subject: [PATCH 0738/1533] =?UTF-8?q?#=20=E6=B7=BB=E5=8A=A0=E4=BA=86=20197?= =?UTF-8?q?1.=20=E5=AF=BB=E6=89=BE=E5=9B=BE=E4=B8=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E8=B7=AF=E5=BE=84=20=E7=9A=84Python=E5=B9=B6?= =?UTF-8?q?=E6=9F=A5=E9=9B=86=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\230\345\234\250\350\267\257\345\276\204.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 29e50ab8b0..5660233cf6 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -134,6 +134,22 @@ public: } }; ``` + +PYTHON并查集解法如下: +```PYTHON +class Solution: + def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: + p = [i for i in range(n)] + def find(i): + if p[i] != i: + p[i] = find(p[i]) + return p[i] + for u, v in edges: + p[find(u)] = find(v) + return find(source) == find(destination) +``` + +

From 23ca9e43e5a15bdbbf69744515b94736ac1d3b0d Mon Sep 17 00:00:00 2001 From: CH Ye Date: Fri, 15 Sep 2023 16:09:38 -0400 Subject: [PATCH 0739/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86684.?= =?UTF-8?q?=E5=86=97=E4=BD=99=E8=BF=9E=E6=8E=A5=20=E7=9A=84Python=E5=B9=B6?= =?UTF-8?q?=E6=9F=A5=E9=9B=86=E7=AE=80=E6=B4=81=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\227\344\275\231\350\277\236\346\216\245.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 8124cc7eea..f5e84223b1 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -256,6 +256,23 @@ class Solution: return [] ``` +### Python简洁写法: + +```python +class Solution: + def findRedundantConnection(self, edges: List[List[int]]) -> List[int]: + n = len(edges) + p = [i for i in range(n+1)] + def find(i): + if p[i] != i: + p[i] = find(p[i]) + return p[i] + for u, v in edges: + if p[find(u)] == find(v): + return [u, v] + p[find(u)] = find(v) +``` + ### Go ```go From f1a65d7e2df65506a8165cd95ac3aed680cf5a11 Mon Sep 17 00:00:00 2001 From: ShuangmingMa <52561813+ShuangmingMa@users.noreply.github.com> Date: Sat, 16 Sep 2023 18:40:07 +0800 Subject: [PATCH 0740/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200503.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20go?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=AD=E7=9A=84=E4=BB=A3=E7=A0=81=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index c001df5076..b788968cee 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -207,7 +207,7 @@ class Solution: ```go func nextGreaterElements(nums []int) []int { length := len(nums) - result := make([]int,length,length) + result := make([]int,length) for i:=0;i Date: Sat, 16 Sep 2023 20:35:17 +0800 Subject: [PATCH 0741/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200042.=E6=8E=A5?= =?UTF-8?q?=E9=9B=A8=E6=B0=B4=20Go=E7=89=88=E6=9C=AC=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E6=A0=88=E5=8E=8B=E7=BC=A9=E7=89=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 760ebc34a4..517ccb0b39 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -704,6 +704,45 @@ func min(x, y int) int { } ``` +单调栈压缩版: + +```go +func trap(height []int) int { + stack := make([]int, 0) + res := 0 + + // 无需事先将第一个柱子的坐标入栈,因为它会在该for循环的最后入栈 + for i := 0; i < len(height); i ++ { + // 满足栈不为空并且当前柱子高度大于栈顶对应的柱子高度的情况时 + for len(stack) > 0 && height[stack[len(stack) - 1]] < height[i] { + // 获得凹槽高度 + mid := height[stack[len(stack) - 1]] + // 凹槽坐标出栈 + stack = stack[: len(stack) - 1] + + // 如果栈不为空则此时栈顶元素为左侧柱子坐标 + if len(stack) > 0 { + // 求得雨水高度 + h := min(height[i], height[stack[len(stack) - 1]]) - mid + // 求得雨水宽度 + w := i - stack[len(stack) - 1] - 1 + res += h * w + } + } + // 如果栈为空或者当前柱子高度小于等于栈顶对应的柱子高度时入栈 + stack = append(stack, i) + } + return res +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} +``` + ### JavaScript: ```javascript From c26fddc702a333a3ed36fa10ebbf0f71d617ce7b Mon Sep 17 00:00:00 2001 From: ShuangmingMa <52561813+ShuangmingMa@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:33:02 +0800 Subject: [PATCH 0742/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200084.=E6=9F=B1?= =?UTF-8?q?=E7=8A=B6=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9?= =?UTF-8?q?=E5=BD=A2=20Go=E5=8D=95=E8=B0=83=E6=A0=88=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index b54429ed22..97dab4f398 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -478,36 +478,34 @@ class Solution: ```go func largestRectangleArea(heights []int) int { - // 声明max并初始化为0 - max := 0 - // 使用切片实现栈 - stack := make([]int, 0) - // 数组头部加入0 - heights = append([]int{0}, heights...) - // 数组尾部加入0 - heights = append(heights, 0) - // 初始化栈,序号从0开始 - stack = append(stack, 0) - for i := 1; i < len(heights); i++ { - // 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势 - for heights[stack[len(stack)-1]] > heights[i] { - // mid 是top - mid := stack[len(stack)-1] - // 出栈 - stack = stack[0 : len(stack)-1] - // left是top的下一位元素,i是将要入栈的元素 - left := stack[len(stack)-1] - // 高度x宽度 - tmp := heights[mid] * (i - left - 1) - if tmp > max { - max = tmp - } - } - stack = append(stack, i) - } - return max + max := 0 + // 使用切片实现栈 + stack := make([]int, 0) + // 数组头部加入0 + heights = append([]int{0}, heights...) + // 数组尾部加入0 + heights = append(heights, 0) + // 初始化栈,序号从0开始 + stack = append(stack, 0) + for i := 1; i < len(heights); i ++ { + // 结束循环条件为:当即将入栈元素>top元素,也就是形成非单调递增的趋势 + for heights[stack[len(stack) - 1]] > heights[i] { + // mid 是top + mid := stack[len(stack) - 1] + // 出栈 + stack = stack[0 : len(stack) - 1] + // left是top的下一位元素,i是将要入栈的元素 + left := stack[len(stack) - 1] + // 高度x宽度 + tmp := heights[mid] * (i - left - 1) + if tmp > max { + max = tmp + } + } + stack = append(stack, i) + } + return max } - ``` ### JavaScript: From babb21fad0ac7e9213ef811a1a04415561fd1acf Mon Sep 17 00:00:00 2001 From: km_yang Date: Mon, 18 Sep 2023 14:36:17 +0800 Subject: [PATCH 0743/1533] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=EF=BC=9A=E2=80=9C=E5=9C=A8=E2=80=9D->=E2=80=9C?= =?UTF-8?q?=E5=86=8D=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 64ca181b90..1174e239c1 100644 --- "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -9,7 +9,7 @@ 在[深度优先搜索](https://programmercarl.com/图论深搜理论基础.html)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 -广搜(bfs)是一圈一圈的搜索过程,和深搜(dfs)是一条路跑到黑然后在回溯。 +广搜(bfs)是一圈一圈的搜索过程,和深搜(dfs)是一条路跑到黑然后再回溯。 ## 广搜的使用场景 From 631b333c084b98bf23f8565789452f01a4fa61ae Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 19 Sep 2023 16:21:37 +0800 Subject: [PATCH 0744/1533] Update --- ...042.\346\216\245\351\233\250\346\260\264.md" | 4 ++-- ...63\350\267\203\346\270\270\346\210\217II.md" | 12 ++++++------ ...4\345\220\210\346\200\273\345\222\214III.md" | 4 ++-- ...\253\345\206\267\345\206\273\346\234\237.md" | 4 ++-- ...\255\347\232\204\344\274\227\346\225\260.md" | 2 +- ...\211\346\220\234\347\264\242\346\240\221.md" | 17 ----------------- ...\211\346\255\245\351\223\272\345\236\253.md" | 2 +- "problems/\345\211\215\345\272\217/vim.md" | 4 ++-- ...\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 9 files changed, 17 insertions(+), 34 deletions(-) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 760ebc34a4..1090467dc1 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -39,8 +39,8 @@ 本文深度讲解如下三种方法: -* 双指针法 -* 动态规划 +* 暴力解法 +* 双指针优化 * 单调栈 ### 暴力解法 diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index fbd848665b..aab27b27ee 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -35,13 +35,13 @@ 但思路是相似的,还是要看最大覆盖范围。 -本题要计算最小步数,那么就要想清楚什么时候步数才一定要加一呢? +本题要计算最少步数,那么就要想清楚什么时候步数才一定要加一呢? -贪心的思路,局部最优:当前可移动距离尽可能多走,如果还没到终点,步数再加一。整体最优:一步尽可能多走,从而达到最小步数。 +贪心的思路,局部最优:当前可移动距离尽可能多走,如果还没到终点,步数再加一。整体最优:一步尽可能多走,从而达到最少步数。 思路虽然是这样,但在写代码的时候还不能真的能跳多远就跳多远,那样就不知道下一步最远能跳到哪里了。 -**所以真正解题的时候,要从覆盖范围出发,不管怎么跳,覆盖范围内一定是可以跳到的,以最小的步数增加覆盖范围,覆盖范围一旦覆盖了终点,得到的就是最小步数!** +**所以真正解题的时候,要从覆盖范围出发,不管怎么跳,覆盖范围内一定是可以跳到的,以最小的步数增加覆盖范围,覆盖范围一旦覆盖了终点,得到的就是最少步数!** **这里需要统计两个覆盖范围,当前这一步的最大覆盖和下一步最大覆盖**。 @@ -144,7 +144,7 @@ public: 但代码又十分简单,贪心就是这么巧妙。 -理解本题的关键在于:**以最小的步数增加最大的覆盖范围,直到覆盖范围覆盖了终点**,这个范围内最小步数一定可以跳到,不用管具体是怎么跳的,不纠结于一步究竟跳一个单位还是两个单位。 +理解本题的关键在于:**以最小的步数增加最大的覆盖范围,直到覆盖范围覆盖了终点**,这个范围内最少步数一定可以跳到,不用管具体是怎么跳的,不纠结于一步究竟跳一个单位还是两个单位。 ## 其他语言版本 @@ -276,9 +276,9 @@ class Solution: for i in range(len(nums)): # 遍历数组 for j in range(nums[i] + 1): # 在当前位置能够跳跃的范围内遍历 if i + j < len(nums): # 确保下一跳的位置不超过数组范围 - result[i + j] = min(result[i + j], result[i] + 1) # 更新到达下一跳位置的最小步数 + result[i + j] = min(result[i + j], result[i] + 1) # 更新到达下一跳位置的最少步数 - return result[-1] # 返回到达最后一个位置的最小步数 + return result[-1] # 返回到达最后一个位置的最少步数 ``` diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index 4de7dc58f3..c0cb8860c2 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -209,11 +209,11 @@ private: vector path; // 符合条件的结果 void backtracking(int targetSum, int k, int sum, int startIndex) { if (sum > targetSum) { // 剪枝操作 - return; // 如果path.size() == k 但sum != targetSum 直接返回 + return; } if (path.size() == k) { if (sum == targetSum) result.push_back(path); - return; + return; // 如果path.size() == k 但sum != targetSum 直接返回 } for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) { // 剪枝 sum += i; // 处理 diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index f4093b67e6..0eb66fb543 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -240,9 +240,9 @@ class Solution { // 因为马上dp[0]和dp[2]的数据都会变 int temp = dp[0]; int temp1 = dp[2]; - dp[0] = Math.max(dp[0], Math.max(dp[3], dp[1]) - prices[i-1]); + dp[0] = Math.max(dp[0], Math.max(dp[3], dp[1]) - prices[i]); dp[1] = Math.max(dp[1], dp[3]); - dp[2] = temp + prices[i-1]; + dp[2] = temp + prices[i]; dp[3] = temp1; } return Math.max(dp[3],Math.max(dp[1],dp[2])); diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 29b0926d4d..8881200f5d 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -261,7 +261,7 @@ public: vector findMode(TreeNode* root) { count = 0; maxCount = 0; - TreeNode* pre = NULL; // 记录前一个节点 + pre = NULL; // 记录前一个节点 result.clear(); searchBST(root); diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 2cfbfefc60..001865585d 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -330,23 +330,6 @@ class Solution: return root ``` -递归法(版本二)精简 -```python -class Solution: - def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode: - if root is None: - return None - if root.val < low: - return self.trimBST(root.right, low, high) - if root.val > high: - return self.trimBST(root.left, low, high) - root.left = self.trimBST(root.left, low, high) - root.right = self.trimBST(root.right, low, high) - return root - - -``` - 迭代法 ```python class Solution: diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" index 1982d44943..442a505573 100644 --- "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" +++ "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" @@ -66,7 +66,7 @@ if (s[i - 1] == t[j - 1]) { ## 两个字符串的删除操作 -[动态规划:583.两个字符串的删除操作](https://programmercarl.com/0583.两个字符串的删除操作.html)给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。 +[动态规划:583.两个字符串的删除操作](https://programmercarl.com/0583.两个字符串的删除操作.html)给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最少步数,每步可以删除任意一个字符串中的一个字符。 本题和[动态规划:115.不同的子序列](https://programmercarl.com/0115.不同的子序列.html)相比,其实就是两个字符串可以都可以删除了,情况虽说复杂一些,但整体思路是不变的。 diff --git "a/problems/\345\211\215\345\272\217/vim.md" "b/problems/\345\211\215\345\272\217/vim.md" index 136a499338..581019995a 100644 --- "a/problems/\345\211\215\345\272\217/vim.md" +++ "b/problems/\345\211\215\345\272\217/vim.md" @@ -21,11 +21,11 @@ 使用VIM的话,本地,服务器,开发机,一刀流,无缝切换,爽不。 -IDE那么很吃内存,打开个IDE卡半天,用VIM就很轻便了,秒开有木有! +IDE那么很吃内存,打开个IDE卡半天,用VIM就很轻便了,秒开! 而且在我们日常开发中,工作年头多了,都会发现没有纯粹的C++,Java开发啥的,就是 C++也得写,Java也得写,有时候写Go起个http服务,写Python处理一下数据,写shell搞个自动部署,编译啥的。 **总是就是啥语言就得写,一些以项目需求为导向!** -写语言还要切换不同的IDE,熟悉不同的操作姿势,想想是不是很麻烦。 +写语言还要切换不同的IDE,熟悉不同的操作规则,想想是不是很麻烦。 听说好像现在有的IDE可以支持很多语言了,这个我还不太了解,但能确定的是,IDE支持的语言再多,也不会有vim多。 diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 54341ce227..2c14f49037 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -289,7 +289,7 @@ join(6, 2); 此时我们生成的的有向图为: -![](https://cdn.tftree.top/others/jion.jpg) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230910203210.png) 有录友可能想,`join(3, 8)` 在图中为什么 将 元素1 连向元素 3 而不是将 元素 8 连向 元素 3 呢? From e14fb76097d117785c0a3af852eaff7d5a174966 Mon Sep 17 00:00:00 2001 From: Junqiao Date: Sun, 24 Sep 2023 10:43:51 +0800 Subject: [PATCH 0745/1533] =?UTF-8?q?Update=200035.=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\344\275\215\347\275\256.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 04dd0cac76..80b7e40e4a 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -258,6 +258,37 @@ public int searchInsert(int[] nums, int target) { +### C# + +```go +public int SearchInsert(int[] nums, int target) { + + var left = 0; + var right = nums.Length - 1; + + while (left <= right) { + + var curr = (left + right) / 2; + + if (nums[curr] == target) + { + return curr; + } + + if (target > nums[curr]) { + left = curr + 1; + } + else { + right = curr - 1; + } + } + + return left; +} +``` + + + ### Golang ```go @@ -500,3 +531,4 @@ int searchInsert(int* nums, int numsSize, int target){ + From 949dd979f7ca88a12a9b364b38e4da2457b27691 Mon Sep 17 00:00:00 2001 From: Junqiao Date: Sun, 24 Sep 2023 11:43:43 +0800 Subject: [PATCH 0746/1533] =?UTF-8?q?Update=200034.=E5=9C=A8=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\344\270\252\344\275\215\347\275\256.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index b412816686..3bf90e3bbb 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -329,6 +329,67 @@ class Solution { } ``` +### C# + +```c# +public int[] SearchRange(int[] nums, int target) { + + var leftBorder = GetLeftBorder(nums, target); + var rightBorder = GetRightBorder(nums, target); + + if (leftBorder == -2 || rightBorder == -2) { + return new int[] {-1, -1}; + } + + if (rightBorder - leftBorder >=2) { + return new int[] {leftBorder + 1, rightBorder - 1}; + } + + return new int[] {-1, -1}; + +} + +public int GetLeftBorder(int[] nums, int target){ + var left = 0; + var right = nums.Length - 1; + var leftBorder = -2; + + while (left <= right) { + var mid = (left + right) / 2; + + if (target <= nums[mid]) { + right = mid - 1; + leftBorder = right; + } + else { + left = mid + 1; + } + } + + return leftBorder; +} + +public int GetRightBorder(int[] nums, int target){ + var left = 0; + var right = nums.Length - 1; + var rightBorder = -2; + + while (left <= right) { + var mid = (left + right) / 2; + + if (target >= nums[mid]) { + left = mid + 1; + rightBorder = left; + } + else { + right = mid - 1; + } + } + + return rightBorder; +} +``` + ### Python From afc9e07d516f7c284163ee71d9b64ed46ab11408 Mon Sep 17 00:00:00 2001 From: Xuan <854674282@qq.com> Date: Mon, 25 Sep 2023 10:21:33 +0800 Subject: [PATCH 0747/1533] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增0343.整数拆分.md PHP 版本 --- ...64\346\225\260\346\213\206\345\210\206.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index cba82f6cae..2e17caf553 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -469,6 +469,34 @@ object Solution { } ``` + +### PHP +```php +class Solution { + + /** + * @param Integer $n + * @return Integer + */ + function integerBreak($n) { + if($n == 0 || $n == 1) return 0; + if($n == 2) return 1; + + $dp = []; + $dp[0] = 0; + $dp[1] = 0; + $dp[2] = 1; + for($i=3;$i<=$n;$i++){ + for($j = 1;$j <= $i/2; $j++){ + $dp[$i] = max(($i-$j)*$j, $dp[$i-$j]*$j, $dp[$i]); + } + } + + return $dp[$n]; + } +} +``` +

From c2b6be723aab6f2b78fa5a0f3198bf6d37f0edbd Mon Sep 17 00:00:00 2001 From: zirs97 Date: Mon, 25 Sep 2023 22:54:57 +0800 Subject: [PATCH 0748/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A30019.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84Java=E4=BB=A3=E7=A0=81fast?= =?UTF-8?q?Index=E7=A7=BB=E5=8A=A8=E7=9A=84=E9=94=99=E8=AF=AF=E9=83=A8?= =?UTF-8?q?=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 1c95ad5b2f..dbd053d141 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -105,8 +105,8 @@ public ListNode removeNthFromEnd(ListNode head, int n){ ListNode fastIndex = dummyNode; ListNode slowIndex = dummyNode; - //只要快慢指针相差 n 个结点即可 - for (int i = 0; i < n ; i++){ + // 只要快慢指针相差 n 个结点即可 + for (int i = 0; i <= n ; i++){ fastIndex = fastIndex.next; } From 5117a288422612c5e4a845d7ecdecff8448334d0 Mon Sep 17 00:00:00 2001 From: Bird Date: Thu, 28 Sep 2023 22:08:12 +0800 Subject: [PATCH 0749/1533] =?UTF-8?q?feat(0213=20=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D=E2=85=A1):=20=E4=BD=BFGo=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=9B=B4=E7=AC=A6=E5=90=88Go=E4=B9=A0=E6=83=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\256\266\345\212\253\350\210\215II.md" | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index cd9d596d70..385c58675e 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -225,32 +225,38 @@ class Solution: // 打家劫舍Ⅱ 动态规划 // 时间复杂度O(n) 空间复杂度O(n) func rob(nums []int) int { - if len(nums) == 1 { - return nums[0] + // 如果长度为0或1,那么有没有环的限制都一样 + if len(nums) <= 1 { + return robWithoutCircle(nums) } - if len(nums) == 2 { - return max(nums[0], nums[1]) - } - - result1 := robRange(nums, 0) - result2 := robRange(nums, 1) - return max(result1, result2) + + // 否则,去头或去尾,取最大 + res1 := robWithoutCircle(nums[:len(nums)-1]) + res2 := robWithoutCircle(nums[1:]) + + return max(res1, res2) } -// 偷盗指定的范围 -func robRange(nums []int, start int) int { +// 原始的打家劫舍版 +func robWithoutCircle(nums []int) int { + switch len(nums) { + case 0: return 0 + case 1: return nums[0] + } dp := make([]int, len(nums)) - dp[1] = nums[start] - - for i := 2; i < len(nums); i++ { - dp[i] = max(dp[i - 2] + nums[i - 1 + start], dp[i - 1]) + dp[0]=nums[0] + dp[1] = max(nums[0], nums[1]) + + for i:=2; i b { +func max(a, b int ) int { + if a>b { return a } return b From 546d3b601a2be997855a0d9da2c5af567ebbfaa5 Mon Sep 17 00:00:00 2001 From: Bird Date: Thu, 28 Sep 2023 22:13:00 +0800 Subject: [PATCH 0750/1533] =?UTF-8?q?feat(1356=20=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B61=E6=8E=92=E5=BA=8F):=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8Go=E5=86=85=E7=BD=AE=E6=8E=92=E5=BA=8F=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\233\256\346\216\222\345\272\217.md" | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" index cc7a7007c7..c2455cf043 100644 --- "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" +++ "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" @@ -171,36 +171,29 @@ class Solution: ```go func sortByBits(arr []int) []int { - var tmp int - for i := 0; i < len(arr); i++ { - for j := i+1; j < len(arr); j++ { - // 冒泡排序的手法,但是排序的规则从比大小变成了比位运算1的个数 - if isCmp(arr[i], arr[j]) { - tmp = arr[i] - arr[i] = arr[j] - arr[j] = tmp - } - } + // 是否arr[i]<=arr[j] + // 先比较1的数量,后比较值本身 + cmp := func(i, j int) bool { + c1, c2 := bitCount(arr[i]), bitCount(arr[j]) + if c1 == c2 { + return arr[i] <= arr[j] + } + return c1 <= c2 } - return arr -} -func isCmp(a, b int) bool { - bitA := bitCount(a) - bitB := bitCount(b) - if bitA == bitB { - return a > b - } else { - return bitA > bitB - } + // 调用库函数 + // 第一个参数是待排序切片,第二个是第i位是否小于第j位的函数 + sort.Slice(arr, cmp) + + return arr } -func bitCount(n int) int { - count := 0 - for n != 0 { - n &= (n-1) // 清除最低位的1 +func bitCount(num int) (count int) { + for num != 0 { + num &= num-1 // 每次运算将最右侧的1变成0 count++ } + return count } ``` From 82b20f2e6918a5c0b14dc3e46835f149d5c4e57a Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 3 Oct 2023 11:03:12 +0800 Subject: [PATCH 0751/1533] Update --- ...\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 1d5ca6afb6..cdfe4a96d2 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -90,7 +90,7 @@ public: 大家不要脑洞模拟哈,可以亲自构造一些测试数据试一试,就发现其实没有问题。 -数组都为负数,result记录的就是最小的负数,如果数组里有int最小值,那么最终result就是int最小值。 +数组都为负数,result记录的就是最大的负数,如果数组里有int最小值,那么最终result就是int最小值。 ## 总结 From e68557e03a312f6aa99c49a7fe7701b8befaa30c Mon Sep 17 00:00:00 2001 From: w830207 <44136372+w830207@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:22:36 +0800 Subject: [PATCH 0752/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A30001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8Cdart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 儘量給定類型 - 使用HashMap取代不適合的List --- ...44\346\225\260\344\271\213\345\222\214.md" | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 4e44d0c3f6..1785fe5ff1 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -402,16 +402,18 @@ public class Solution { ### Dart: ```dart -List twoSum(List nums, int target) { - var tmp = []; - for (var i = 0; i < nums.length; i++) { - var rest = target - nums[i]; - if(tmp.contains(rest)){ - return [tmp.indexOf(rest), i]; - } - tmp.add(nums[i]); +import 'dart:collection'; + +List twoSum(List nums, int target) { + HashMap hashMap = HashMap(); + for (int i = 0; i < nums.length; i++) { + int rest = target - nums[i]; + if (hashMap.containsKey(rest)) { + return [hashMap[rest]!, i]; } - return [0 , 0]; + hashMap.addEntries({nums[i]: i}.entries); + } + return []; } ``` From a8366adf1cbb7a8155a67f2d6d26db9a562acb6a Mon Sep 17 00:00:00 2001 From: wngtk <100271394+wngtk@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:49:16 +0800 Subject: [PATCH 0753/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix typo --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index f60e261be0..7827aa958d 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -133,7 +133,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 所以从后往前循环,每次取得状态不会和之前取得状态重合,这样每种物品就只取一次了。 -**那么问题又来了,为什么二维dp数组历的时候不用倒序呢?** +**那么问题又来了,为什么二维dp数组遍历的时候不用倒序呢?** 因为对于二维dp,dp[i][j]都是通过上一层即dp[i - 1][j]计算而来,本层的dp[i][j]并不会被覆盖! From 48692ef223361dbb3cc4cedf763c5ca9567c150f Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 4 Oct 2023 17:15:42 +0800 Subject: [PATCH 0754/1533] Update --- README.md | 1 + ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...36\346\272\257\346\200\273\347\273\223.md" | 2 +- ...47\241\20001\350\203\214\345\214\205-1.md" | 52 +++++++++++++++++++ ...47\241\20001\350\203\214\345\214\205-2.md" | 47 ++++++++++++++++- ...14\345\205\250\350\203\214\345\214\205.md" | 39 ++++++++++++++ 6 files changed, 140 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bf55a0041b..b43fa38cea 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ * 编程语言 * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) + * [C++语言基础课](https://kamacoder.com/course.php?course_id=1) * 项目 * [基于跳表的轻量级KV存储引擎](https://github.com/youngyangyang04/Skiplist-CPP) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 9a447648d8..2d20a19702 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -31,7 +31,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)第一个树形结构没有画出startIndex的作用,**这里这里纠正一下,准确的树形结构如图所示:** -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123202227835.png) +![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) ## 周二 diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index 35b4c3f907..5d4c945075 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -116,8 +116,8 @@ void backtracking(参数) { **注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路**。 树形结构如下: +![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118152521990.png) 最后还给出了本题的剪枝优化,如下: diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 79ad26a859..bc5f2e5d67 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -8,6 +8,9 @@ # 动态规划:01背包理论基础 + +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的。 + ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -261,6 +264,55 @@ int main() { ``` +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的,代码如下: + +```CPP + +//二维dp数组实现 +#include +using namespace std; + +int n, bagweight;// bagweight代表行李箱空间 +void solve() { + vector weight(n, 0); // 存储每件物品所占空间 + vector value(n, 0); // 存储每件物品价值 + for(int i = 0; i < n; ++i) { + cin >> weight[i]; + } + for(int j = 0; j < n; ++j) { + cin >> value[j]; + } + // dp数组, dp[i][j]代表行李箱空间为j的情况下,从下标为[0, i]的物品里面任意取,能达到的最大价值 + vector> dp(weight.size(), vector(bagweight + 1, 0)); + + // 初始化, 因为需要用到dp[i - 1]的值 + // j < weight[0]已在上方被初始化为0 + // j >= weight[0]的值就初始化为value[0] + for (int j = weight[0]; j <= bagweight; j++) { + dp[0][j] = value[0]; + } + + for(int i = 1; i < weight.size(); i++) { // 遍历科研物品 + for(int j = 0; j <= bagweight; j++) { // 遍历行李箱容量 + // 如果装不下这个物品,那么就继承dp[i - 1][j]的值 + if (j < weight[i]) dp[i][j] = dp[i - 1][j]; + // 如果能装下,就将值更新为 不装这个物品的最大值 和 装这个物品的最大值 中的 最大值 + // 装这个物品的最大值由容量为j - weight[i]的包任意放入序号为[0, i - 1]的最大值 + 该物品的价值构成 + else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + } + } + cout << dp[weight.size() - 1][bagweight] << endl; +} + +int main() { + while(cin >> n >> bagweight) { + solve(); + } + return 0; +} + +``` + ## 总结 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index f60e261be0..3e2134b419 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -6,6 +6,7 @@ # 动态规划:01背包理论基础(滚动数组) +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习 ## 算法公开课 @@ -13,7 +14,6 @@ ## 思路 - 昨天[动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html)中是用二维dp数组来讲解01背包。 今天我们就来说一说滚动数组,其实在前面的题目中我们已经用到过滚动数组了,就是把二维dp降为一维dp,一些录友当时还表示比较困惑。 @@ -159,6 +159,8 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 +C++代码如下: + ```CPP void test_1_wei_bag_problem() { vector weight = {1, 3, 4}; @@ -181,6 +183,49 @@ int main() { ``` +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的,代码如下: + +```CPP +// 一维dp数组实现 +#include +#include +using namespace std; + +int main() { + // 读取 M 和 N + int M, N; + cin >> M >> N; + + vector costs(M); + vector values(M); + + for (int i = 0; i < M; i++) { + cin >> costs[i]; + } + for (int j = 0; j < M; j++) { + cin >> values[j]; + } + + // 创建一个动态规划数组dp,初始值为0 + vector dp(N + 1, 0); + + // 外层循环遍历每个类型的研究材料 + for (int i = 0; i < M; ++i) { + // 内层循环从 N 空间逐渐减少到当前研究材料所占空间 + for (int j = N; j >= costs[i]; --j) { + // 考虑当前研究材料选择和不选择的情况,选择最大值 + dp[j] = max(dp[j], dp[j - costs[i]] + values[i]); + } + } + + // 输出dp[N],即在给定 N 行李空间可以携带的研究材料最大价值 + cout << dp[N] << endl; + + return 0; +} + +``` + 可以看出,一维dp 的01背包,要比二维简洁的多! 初始化 和 遍历顺序相对简单了。 **所以我倾向于使用一维dp数组的写法,比较直观简洁,而且空间复杂度还降了一个数量级!** diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index a006cb0e20..a173c6ddf9 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -7,6 +7,8 @@ # 动态规划:完全背包理论基础 +本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的。 + ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -162,6 +164,43 @@ int main() { ``` +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的,C++代码如下: + +```cpp +#include +#include +using namespace std; + +// 先遍历背包,再遍历物品 +void test_CompletePack(vector weight, vector value, int bagWeight) { + + vector dp(bagWeight + 1, 0); + + for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + for(int i = 0; i < weight.size(); i++) { // 遍历物品 + if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); + } + } + cout << dp[bagWeight] << endl; +} +int main() { + int N, V; + cin >> N >> V; + vector weight; + vector value; + for (int i = 0; i < N; i++) { + int w; + int v; + cin >> w >> v; + weight.push_back(w); + value.push_back(v); + } + test_CompletePack(weight, value, V); + return 0; +} +``` + + ## 总结 From 9ca0d38d989f4712bf9e45bb11f7ff8d7452e037 Mon Sep 17 00:00:00 2001 From: INSSRumia <52827806+INSSRumia@users.noreply.github.com> Date: Thu, 5 Oct 2023 01:09:05 +0800 Subject: [PATCH 0755/1533] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0450删除二叉搜索树中的节点 C# 版本 --- ...55\347\232\204\350\212\202\347\202\271.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 18e5cb4cb1..13c25023a2 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -769,6 +769,38 @@ impl Solution { } ``` +### C# + +> 递归法: +```C# + public TreeNode DeleteNode(TreeNode root, int key) { + // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == null) return null; + if(key == root.val) { + //第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 + if(root.left == null && root.right == null) return null; + //第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 + if (root.left == null && root.right != null) return root.right; + //第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 + if (root.left != null && root.right == null) return root.left; + //第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 + // 并返回删除节点右孩子为新的根节点。 + if(root.left != null && root.right != null) { + TreeNode leftNode = root.right; // 找右子树最左面的节点 + while(leftNode.left != null) + leftNode = leftNode.left; + leftNode.left = root.left; // 把要删除的节点(root)左子树放在leftNode的左孩子的位置 + return root.right; // 返回旧root的右孩子作为新root + } + } + + if(root.val > key) root.left = DeleteNode(root.left, key); + if(root.val < key) root.right = DeleteNode(root.right, key); + + return root; + } +``` +

From 8973967dd10fbea86996f9df413aa1559c1dac53 Mon Sep 17 00:00:00 2001 From: ShuangmingMa <52561813+ShuangmingMa@users.noreply.github.com> Date: Sun, 8 Oct 2023 15:05:51 +0800 Subject: [PATCH 0756/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200392.=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97.md=20Go=E4=BA=8C=E7=BB=B4D?= =?UTF-8?q?P=E6=A0=BC=E5=BC=8F=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=8A=A0Go?= =?UTF-8?q?=E4=B8=80=E7=BB=B4DP=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 0392.判断子序列.md Go二维DP格式,并增加Go一维DP解法 --- ...55\345\255\220\345\272\217\345\210\227.md" | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 12d3fa481f..cb9323c8a7 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -240,26 +240,45 @@ function isSubsequence(s: string, t: string): boolean { ### Go: +二维DP: + ```go func isSubsequence(s string, t string) bool { - dp := make([][]int,len(s)+1) - for i:=0;i= 1; j -- { + if t[i - 1] == s[j - 1] { + dp[j] = dp[j - 1] + 1 + } + } + } + return dp[len(s)] == len(s) +} +``` + + +### Rust: ```rust impl Solution { From d9e08a1bbfda8295273d99699c77267dcde85696 Mon Sep 17 00:00:00 2001 From: ShuangmingMa <52561813+ShuangmingMa@users.noreply.github.com> Date: Sun, 8 Oct 2023 15:16:06 +0800 Subject: [PATCH 0757/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200392.=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97.md=20Go=E4=BA=8C=E7=BB=B4D?= =?UTF-8?q?P=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 0392.判断子序列.md Go二维DP格式 --- ...\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index cb9323c8a7..f7863c9499 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -250,7 +250,7 @@ func isSubsequence(s string, t string) bool { } for i := 1; i < len(dp); i ++{ for j := 1; j < len(dp[i]); j ++{ - if s[i - 1] == t[j-1] { + if s[i - 1] == t[j - 1] { dp[i][j] = dp[i - 1][j - 1] +1 }else{ dp[i][j] = dp[i][j - 1] From be11452af6c87e47243c8bb65aa86e517e1f4ab8 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 8 Oct 2023 08:36:54 -0400 Subject: [PATCH 0758/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0python=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index c7649971d3..43eb66e172 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -278,6 +278,40 @@ class Solution: return result ``` +```python +# 我们用三个状态去标记每一个格子 +# 0 代表海水 +# 1 代表陆地 +# 2 代表已经访问的陆地 +class Solution: + def traversal(self, grid, i, j): + m = len(grid) + n = len(grid[0]) + + if i < 0 or j < 0 or i >= m or j >= n: + return # 越界了 + elif grid[i][j] == "2" or grid[i][j] == "0": + return + + grid[i][j] = "2" + self.traversal(grid, i - 1, j) # 往上走 + self.traversal(grid, i + 1, j) # 往下走 + self.traversal(grid, i, j - 1) # 往左走 + self.traversal(grid, i, j + 1) # 往右走 + + def numIslands(self, grid: List[List[str]]) -> int: + res = 0 + + + for i in range(len(grid)): + for j in range(len(grid[0])): + if grid[i][j] == "1": + res += 1 + self.traversal(grid, i, j) + + return res +``` + ### JavaScript ```javascript From 4a9c5a4385d6ac0f17981ad8fa8cd77c67585345 Mon Sep 17 00:00:00 2001 From: shengwang Date: Mon, 9 Oct 2023 11:00:53 +0800 Subject: [PATCH 0759/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\250\344\270\255\347\232\204\350\212\202\347\202\271.md" | 4 ++-- ...31\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index c612c4b3b9..36f9b0cc9a 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -51,7 +51,7 @@ class Solution { public: ListNode* swapPairs(ListNode* head) { ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点 - dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作 + dummyHead->next = head; // 将虚拟头结点指向head,这样方便后面做删除操作 ListNode* cur = dummyHead; while(cur->next != nullptr && cur->next->next != nullptr) { ListNode* tmp = cur->next; // 记录临时节点 @@ -160,7 +160,7 @@ class Solution { class Solution { public ListNode swapPairs(ListNode head) { ListNode dumyhead = new ListNode(-1); // 设置一个虚拟头结点 - dumyhead.next = head; // 将虚拟头结点指向head,这样方面后面做删除操作 + dumyhead.next = head; // 将虚拟头结点指向head,这样方便后面做删除操作 ListNode cur = dumyhead; ListNode temp; // 临时节点,保存两个节点后面的节点 ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点 diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index c8f802a183..780f9c36ea 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -127,7 +127,7 @@ class Solution { public: ListNode* removeElements(ListNode* head, int val) { ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点 - dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作 + dummyHead->next = head; // 将虚拟头结点指向head,这样方便后面做删除操作 ListNode* cur = dummyHead; while (cur->next != NULL) { if(cur->next->val == val) { From 7f1c040b282aed31f7c4e6d97721daa61c603c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=BA=9A=E6=9D=B0?= <105789281+zyj1112@users.noreply.github.com> Date: Wed, 11 Oct 2023 15:42:12 +0800 Subject: [PATCH 0760/1533] =?UTF-8?q?Update=200054.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\346\227\213\347\237\251\351\230\265.md" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index c62eb2b12c..59771a671f 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -251,6 +251,60 @@ var spiralOrder = function(matrix) { return arr }; ``` +### Python + +```python +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + if len(matrix)==0 or len(matrix[0])==0 : # 判定List是否为空 + return [] + row, col = len(matrix), len(matrix[0]) # 行数,列数 + loop = min(row, col) // 2 # 循环轮数 + stx, sty = 0, 0 # 起始x,y坐标 + i, j =0, 0 + count = 0 # 计数 + offset = 1 # 每轮减少的格子数 + result = [0]*(row*col) + while loop>0 :# 左闭右开 + i, j = stx, sty + while j < col - offset : # 从左到右 + result[count] = matrix[i][j] + count += 1 + j += 1 + while i < row - offset : # 从上到下 + result[count] = matrix[i][j] + count += 1 + i += 1 + while j>sty : # 从右到左 + result[count] = matrix[i][j] + count += 1 + j -= 1 + while i>stx : # 从下到上 + result[count] = matrix[i][j] + count += 1 + i -= 1 + stx += 1 + sty += 1 + offset += 1 + loop -= 1 + if min(row, col) % 2 == 1 : # 判定是否需要填充多出来的一行 + i = stx + if row < col : + while i < stx + col - row + 1 : + result[count] = matrix[stx][i] + count += 1 + i += 1 + else : + while i < stx + row - col + 1 : + result[count] = matrix[i][stx] + count += 1 + i += 1 + return result +``` From 4a3e9c6bbea4acbb73c6decd2693ba0aec164a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=BA=9A=E6=9D=B0?= <105789281+zyj1112@users.noreply.github.com> Date: Thu, 12 Oct 2023 11:11:25 +0800 Subject: [PATCH 0761/1533] =?UTF-8?q?Update=200054.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 59771a671f..44a7749d3f 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -260,7 +260,7 @@ class Solution(object): :type matrix: List[List[int]] :rtype: List[int] """ - if len(matrix)==0 or len(matrix[0])==0 : # 判定List是否为空 + if len(matrix) == 0 or len(matrix[0]) == 0 : # 判定List是否为空 return [] row, col = len(matrix), len(matrix[0]) # 行数,列数 loop = min(row, col) // 2 # 循环轮数 @@ -268,7 +268,7 @@ class Solution(object): i, j =0, 0 count = 0 # 计数 offset = 1 # 每轮减少的格子数 - result = [0]*(row*col) + result = [0] * (row * col) while loop>0 :# 左闭右开 i, j = stx, sty while j < col - offset : # 从左到右 From 284143192c1c2e39bb77a19dfcbebf000270ed16 Mon Sep 17 00:00:00 2001 From: HUAWEI Date: Thu, 12 Oct 2023 17:52:17 +0800 Subject: [PATCH 0762/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97=E2=80=9C=E8=B7=AF=E5=BE=84=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index bcce931476..05b55b5b16 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -89,7 +89,7 @@ if (x == graph.size() - 1) { // 找到符合条件的一条路径 for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 ``` -接下来就是将 选中的x所连接的节点,加入到 单一路劲来。 +接下来就是将 选中的x所连接的节点,加入到 单一路径来。 ```C++ path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 From 77d6e986bb37cb4cd6bb5b9e3bc039f608191902 Mon Sep 17 00:00:00 2001 From: slaier Date: Sun, 15 Oct 2023 21:27:03 +0800 Subject: [PATCH 0763/1533] =?UTF-8?q?=E4=BC=98=E5=8C=960332=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B=20C=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 遍历所有ticket查找机场为O(n), 修改为使用hash表查找. --- ...11\346\216\222\350\241\214\347\250\213.md" | 130 +++++++++++------- 1 file changed, 84 insertions(+), 46 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index ab144f43ce..d1fd46f68a 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -652,62 +652,100 @@ function findItinerary(tickets: string[][]): string[] { ### C ```C -char **result; -bool *used; -int g_found; - -int cmp(const void *str1, const void *str2) -{ - const char **tmp1 = *(char**)str1; - const char **tmp2 = *(char**)str2; - int ret = strcmp(tmp1[0], tmp2[0]); - if (ret == 0) { - return strcmp(tmp1[1], tmp2[1]); +typedef struct { + char *name; /* key */ + int cnt; /* 记录到达机场是否飞过了 */ + UT_hash_handle hh; /* makes this structure hashable */ +} to_airport_t; + +typedef struct { + char *name; /* key */ + to_airport_t *to_airports; + UT_hash_handle hh; /* makes this structure hashable */ +} from_airport_t; + +void to_airport_destroy(to_airport_t *airports) { + to_airport_t *airport, *tmp; + HASH_ITER(hh, airports, airport, tmp) { + HASH_DEL(airports, airport); + free(airport); } - return ret; } -void backtracting(char *** tickets, int ticketsSize, int* returnSize, char *start, char **result, bool *used) -{ - if (*returnSize == ticketsSize + 1) { - g_found = 1; - return; +void from_airport_destroy(from_airport_t *airports) { + from_airport_t *airport, *tmp; + HASH_ITER(hh, airports, airport, tmp) { + to_airport_destroy(airport->to_airports); + HASH_DEL(airports, airport); + free(airport); + } +} + +int name_sort(to_airport_t *a, to_airport_t *b) { + return strcmp(a->name, b->name); +} + +bool backtracking(from_airport_t *airports, int target_path_len, char **path, + int path_len) { + if (path_len == target_path_len) return true; + + from_airport_t *from_airport = NULL; + HASH_FIND_STR(airports, path[path_len - 1], from_airport); + if (!from_airport) return false; + + for (to_airport_t *to_airport = from_airport->to_airports; + to_airport != NULL; to_airport = to_airport->hh.next) { + if (to_airport->cnt == 0) continue; + to_airport->cnt--; + path[path_len] = to_airport->name; + if (backtracking(airports, target_path_len, path, path_len + 1)) + return true; + to_airport->cnt++; } + return false; +} + +char **findItinerary(char ***tickets, int ticketsSize, int *ticketsColSize, + int *returnSize) { + from_airport_t *airports = NULL; + + // 记录映射关系 for (int i = 0; i < ticketsSize; i++) { - if ((used[i] == false) && (strcmp(start, tickets[i][0]) == 0)) { - result[*returnSize] = (char*)malloc(sizeof(char) * 4); - memcpy(result[*returnSize], tickets[i][1], sizeof(char) * 4); - (*returnSize)++; - used[i] = true; - /*if ((*returnSize) == ticketsSize + 1) { - return; - }*/ - backtracting(tickets, ticketsSize, returnSize, tickets[i][1], result, used); - if (g_found) { - return; - } - (*returnSize)--; - used[i] = false; + from_airport_t *from_airport = NULL; + to_airport_t *to_airport = NULL; + HASH_FIND_STR(airports, tickets[i][0], from_airport); + if (!from_airport) { + from_airport = malloc(sizeof(from_airport_t)); + from_airport->name = tickets[i][0]; + from_airport->to_airports = NULL; + HASH_ADD_KEYPTR(hh, airports, from_airport->name, + strlen(from_airport->name), from_airport); + } + HASH_FIND_STR(from_airport->to_airports, tickets[i][1], to_airport); + if (!to_airport) { + to_airport = malloc(sizeof(to_airport_t)); + to_airport->name = tickets[i][1]; + to_airport->cnt = 0; + HASH_ADD_KEYPTR(hh, from_airport->to_airports, to_airport->name, + strlen(to_airport->name), to_airport); } + to_airport->cnt++; } - return; -} -char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, int* returnSize){ - if (tickets == NULL || ticketsSize <= 0) { - return NULL; + // 机场排序 + for (from_airport *from_airport = airports; from_airport != NULL; + from_airport = from_airport->hh.next) { + HASH_SRT(hh, from_airport->to_airports, name_sort); } - result = malloc(sizeof(char*) * (ticketsSize + 1)); - used = malloc(sizeof(bool) * ticketsSize); - memset(used, false, sizeof(bool) * ticketsSize); - result[0] = malloc(sizeof(char) * 4); - memcpy(result[0], "JFK", sizeof(char) * 4); - g_found = 0; - *returnSize = 1; - qsort(tickets, ticketsSize, sizeof(tickets[0]), cmp); - backtracting(tickets, ticketsSize, returnSize, "JFK", result, used); + + char **path = malloc(sizeof(char *) * (ticketsSize + 1)); + path[0] = "JFK"; // 起始机场 + backtracking(airports, ticketsSize + 1, path, 1); + + from_airport_destroy(airports); + *returnSize = ticketsSize + 1; - return result; + return path; } ``` From a78f67721ad16532e3e914ffe5fc13799a61a0b8 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Tue, 17 Oct 2023 07:37:39 -0400 Subject: [PATCH 0764/1533] =?UTF-8?q?add=20python=20=E6=A8=A1=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 1174e239c1..b2181052cf 100644 --- "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -110,6 +110,38 @@ if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') { // 如果节点没被 ``` 就可以通过 [200.岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/) 这道题目,大家可以去体验一下。 +## 其他语言版本 + +### Python +```python +from collections import deque + +dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 创建方向元素 + +def bfs(grid, visited, x, y): + + queue = deque() # 初始化队列 + queue.append((x, y)) # 放入第一个元素/起点 + visited[x][y] = True # 标记为访问过的节点 + + while queue: # 遍历队列里的元素 + + curx, cury = queue.popleft() # 取出第一个元素 + + for dx, dy in dir: # 遍历四个方向 + + nextx, nexty = curx + dx, cury + dy + + if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]): # 越界了,直接跳过 + continue + + if not visited[nextx][nexty]: # 如果节点没被访问过 + queue.append((nextx, nexty)) # 加入队列 + visited[nextx][nexty] = True # 标记为访问过的节点 + +``` + + ## 总结 当然广搜还有很多细节需要注意的地方,后面我会针对广搜的题目还做针对性的讲解,因为在理论篇讲太多细节,可能会让刚学广搜的录友们越看越懵,所以细节方面针对具体题目在做讲解。 From 82e12be81e34a19987e34163a99df1fa8f7c74ac Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 19 Oct 2023 10:52:34 +0800 Subject: [PATCH 0765/1533] Update --- README.md | 3 ++- .../0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" | 1 + ...25\260\347\273\204\347\232\204\344\272\244\351\233\206.md" | 2 +- ...41\200\345\256\214\345\205\250\350\203\214\345\214\205.md" | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b43fa38cea..60890f228a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ > 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) > 2. **正式出版** :[《代码随想录》](https://programmercarl.com/other/publish.html) 。 > 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 -> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.programmercarl.com/other/gongkaike.html) 。 +> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 > 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 > 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 > 7. **学习社区** :一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 @@ -75,6 +75,7 @@ * 编程语言 * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) * [C++语言基础课](https://kamacoder.com/course.php?course_id=1) + * [Java语言基础课](https://kamacoder.com/course.php?course_id=2) * 项目 * [基于跳表的轻量级KV存储引擎](https://github.com/youngyangyang04/Skiplist-CPP) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 76553151e4..777b851cca 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -41,6 +41,7 @@ exection -> execution (插入 'u') * word1 和 word2 由小写英文字母组成 ## 算法公开课 + **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1qv4y1q78f/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 138067e360..d3a1e12f54 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -74,7 +74,7 @@ public: }; ``` -* 时间复杂度: O(mn) +* 时间复杂度: O(n + m) m 是最后要把 set转成vector * 空间复杂度: O(n) ## 拓展 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index a173c6ddf9..c6c856978a 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -7,7 +7,7 @@ # 动态规划:完全背包理论基础 -本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的。 +本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problem.php?id=1052)去练习,题意是一样的。 ## 算法公开课 @@ -164,7 +164,7 @@ int main() { ``` -本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的,C++代码如下: +本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problem.php?id=1052)去练习,题意是一样的,C++代码如下: ```cpp #include From a14785b0f42e2b8f67224c699044471a132e761b Mon Sep 17 00:00:00 2001 From: caveman <948238104@qq.com> Date: Thu, 19 Oct 2023 14:39:30 +0800 Subject: [PATCH 0766/1533] =?UTF-8?q?=E6=A0=88=E4=B8=8E=E9=98=9F=E5=88=97?= =?UTF-8?q?=E6=80=BB=E7=BB=93-=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...351\230\237\345\210\227\346\200\273\347\273\223.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index e7f8ef86c6..4e08a887ee 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -25,8 +25,8 @@ 这个问题有两个陷阱: -* 陷阱1:栈是容器适配器,底层容器使用不同的容器,导致栈内数据在内存中是不是连续分布。 -* 陷阱2:缺省情况下,默认底层容器是deque,那么deque的在内存中的数据分布是什么样的呢? 答案是:不连续的,下文也会提到deque。 +* 陷阱1:栈是容器适配器,底层容器使用不同的容器,导致栈内数据在内存中不一定是连续分布的。 +* 陷阱2:缺省情况下,默认底层容器是deque,那么deque在内存中的数据分布是什么样的呢? 答案是:不连续的,下文也会提到deque。 所以这就是考察候选者基础知识扎不扎实的好问题。 @@ -42,7 +42,7 @@ ### 栈在系统中的应用 -如果还记得编译原理的话,编译器在 词法分析的过程中处理括号、花括号等这个符号的逻辑,就是使用了栈这种数据结构。 +如果还记得编译原理的话,编译器在词法分析的过程中处理括号、花括号等这个符号的逻辑,就是使用了栈这种数据结构。 再举个例子,linux系统中,cd这个进入目录的命令我们应该再熟悉不过了。 @@ -70,8 +70,8 @@ cd a/b/c/../../ 先来分析一下 这里有三种不匹配的情况, -1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。 -2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。 +1. 第一种情况,字符串里左方向的括号多余了,所以不匹配。 +2. 第二种情况,括号没有多余,但是括号的类型没有匹配上。 3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。 这里还有一些技巧,在匹配左括号的时候,右括号先入栈,就只需要比较当前元素和栈顶相不相等就可以了,比左括号先入栈代码实现要简单的多了! From 0cc023d180315f9abd9f4116afd3a71d0486e7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Fri, 20 Oct 2023 09:41:12 +0800 Subject: [PATCH 0767/1533] =?UTF-8?q?Update=20=E5=9B=BE=E8=AE=BA=E5=B9=BF?= =?UTF-8?q?=E6=90=9C=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index b2181052cf..b631f4f5c5 100644 --- "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -110,6 +110,21 @@ if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') { // 如果节点没被 ``` 就可以通过 [200.岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/) 这道题目,大家可以去体验一下。 + + + +## 总结 + +当然广搜还有很多细节需要注意的地方,后面我会针对广搜的题目还做针对性的讲解,因为在理论篇讲太多细节,可能会让刚学广搜的录友们越看越懵,所以细节方面针对具体题目在做讲解。 + +本篇我们重点讲解了广搜的使用场景,广搜的过程以及广搜的代码框架。 + +其实在二叉树章节的[层序遍历](https://programmercarl.com/0102.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86.html)中,我们也讲过一次广搜,相当于是广搜在二叉树这种数据结构上的应用。 + +这次则从图论的角度上再详细讲解一次广度优先遍历。 + +相信看完本篇,大家会对广搜有一个基础性的认识,后面再来做对应的题目就会得心应手一些。 + ## 其他语言版本 ### Python @@ -142,19 +157,6 @@ def bfs(grid, visited, x, y): ``` -## 总结 - -当然广搜还有很多细节需要注意的地方,后面我会针对广搜的题目还做针对性的讲解,因为在理论篇讲太多细节,可能会让刚学广搜的录友们越看越懵,所以细节方面针对具体题目在做讲解。 - -本篇我们重点讲解了广搜的使用场景,广搜的过程以及广搜的代码框架。 - -其实在二叉树章节的[层序遍历](https://programmercarl.com/0102.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86.html)中,我们也讲过一次广搜,相当于是广搜在二叉树这种数据结构上的应用。 - -这次则从图论的角度上再详细讲解一次广度优先遍历。 - -相信看完本篇,大家会对广搜有一个基础性的认识,后面再来做对应的题目就会得心应手一些。 - -

From 194a827f8e4a2778084dafcd4e32e58a15e11712 Mon Sep 17 00:00:00 2001 From: mask <1429855087@qq.com> Date: Sun, 22 Oct 2023 11:47:42 +0800 Subject: [PATCH 0768/1533] =?UTF-8?q?Update=201971.=E5=AF=BB=E6=89=BE?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8=E8=B7=AF?= =?UTF-8?q?=E5=BE=84.md=20=E5=A2=9E=E5=8A=A0Java=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\345\234\250\350\267\257\345\276\204.md" | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 5660233cf6..27ee9147a8 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -135,7 +135,64 @@ public: }; ``` +## 其他语言版本 + +### Java: + +```java +class Solution { + + int[] father; + public boolean validPath(int n, int[][] edges, int source, int destination) { + father = new int[n]; + init(); + for (int i = 0; i < edges.length; i++) { + join(edges[i][0], edges[i][1]); + } + + return isSame(source, destination); + } + + // 并查集初始化 + public void init() { + for (int i = 0; i < father.length; i++) { + father[i] = i; + } + } + + // 并查集里寻根的过程 + public int find(int u) { + if (u == father[u]) { + return u; + } else { + father[u] = find(father[u]); + return father[u]; + } + } + + // 判断 u 和 v是否找到同一个根 + public boolean isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; + } + + // 将v->u 这条边加入并查集 + public void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + + father[v] = u; + } + +} +``` + +### Python: + PYTHON并查集解法如下: + ```PYTHON class Solution: def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool: @@ -154,4 +211,3 @@ class Solution: - From 097051e1bbd06e3302f832c8e0d9179cc54d6c02 Mon Sep 17 00:00:00 2001 From: dxiaofeng <106021460+dxiaofeng@users.noreply.github.com> Date: Tue, 24 Oct 2023 10:52:23 +0800 Subject: [PATCH 0769/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A30347.=E5=89=8DK?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0Java=E7=AE=80?= =?UTF-8?q?=E6=98=93=E7=89=88=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index b3063111b4..4830f9a3f2 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -193,7 +193,7 @@ class Solution { class Solution { public int[] topKFrequent(int[] nums, int k) { // 优先级队列,为了避免复杂 api 操作,pq 存储数组 - // lambda 表达式设置优先级队列从大到小存储 o1 - o2 为从大到小,o2 - o1 反之 + // lambda 表达式设置优先级队列从大到小存储 o1 - o2 为从小到大,o2 - o1 反之 PriorityQueue pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]); int[] res = new int[k]; // 答案数组为 k 个元素 Map map = new HashMap<>(); // 记录元素出现次数 @@ -204,6 +204,7 @@ class Solution { tmp[0] = x.getKey(); tmp[1] = x.getValue(); pq.offer(tmp); + // 下面的代码是根据小根堆实现的,我只保留优先队列的最后的k个,只要超出了k我就将最小的弹出,剩余的k个就是答案 if(pq.size() > k) { pq.poll(); } From 770c138a9aeb1340ec0721fefc8686d9aedb9f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB?= <2507932331@qq.com> Date: Tue, 24 Oct 2023 17:06:10 +0800 Subject: [PATCH 0770/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20111=EF=BC=9A?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=20Java=E9=A2=98=E8=A7=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\260\217\346\267\261\345\272\246.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 8b9d92e612..c4e55a0740 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -268,6 +268,34 @@ class Solution { } ``` +```java +class Solution { + /** + * 递归法(思路来自二叉树最大深度的递归法) + * 该题求最小深度,最小深度为根节点到叶子节点的深度,所以在迭代到每个叶子节点时更新最小值。 + */ + int depth = 0; + // 定义最小深度,初始化最大值 + int minDepth = Integer.MAX_VALUE; + public int minDepth(TreeNode root) { + dep(root); + return minDepth == Integer.MAX_VALUE ? 0 : minDepth; + } + void dep(TreeNode root){ + if(root == null) return ; + // 递归开始,深度增加 + depth++; + dep(root.left); + dep(root.right); + // 该位置表示递归到叶子节点了,需要更新最小深度minDepth + if(root.left == null && root.right == null) + minDepth = Math.min(minDepth , depth); + // 递归结束,深度减小 + depth--; + } +} +``` + ```Java class Solution { /** From c4da16fd1abd3d3609eaa2f4cddf5d69dcaf320f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E7=BB=8D=E8=BE=B0?= Date: Tue, 24 Oct 2023 20:20:38 -0400 Subject: [PATCH 0771/1533] =?UTF-8?q?Problem=200827=20=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=BA=E5=B7=A5=E5=B2=9B=E5=B1=BF=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?python=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\272\345\267\245\345\262\233.md" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index d78798253a..7206bf09e8 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -282,6 +282,68 @@ class Solution { } ``` +### Python + +```Python +class Solution(object): + # 可能的移动方向 + DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)] + + def exploreIsland(self, row, col, grid, visited, island_id): + """ + 从给定的单元格开始,使用深度优先搜索探索岛屿。 + """ + if (row < 0 or row >= len(grid) or col < 0 or col >= len(grid[0]) or + visited[row][col] or grid[row][col] == 0): + return 0 + + visited[row][col] = True + grid[row][col] = island_id + island_size = 1 + for dr, dc in self.DIRECTIONS: + island_size += self.exploreIsland(row + dr, col + dc, grid, visited, island_id) + return island_size + + def largestIsland(self, grid): + """ + 通过最多将一个0更改为1,找到可以形成的最大岛屿的大小。 + """ + rows, cols = len(grid), len(grid[0]) + island_sizes = {} + island_id = 2 # 从2开始标记岛屿(因为0代表水,1代表未被发现的陆地) + is_all_land = True + visited = [[False] * cols for _ in range(rows)] + + # 标记每个岛屿并存储其大小 + for r in range(rows): + for c in range(cols): + if grid[r][c] == 0: + is_all_land = False + elif not visited[r][c] and grid[r][c] == 1: + island_size = self.exploreIsland(r, c, grid, visited, island_id) + island_sizes[island_id] = island_size + island_id += 1 + + # 如果整个网格是陆地,则返回其大小 + if is_all_land: + return rows * cols + + # 计算可以通过将一个0更改为1来形成的最大岛屿 + max_island_size = 0 + for r in range(rows): + for c in range(cols): + if grid[r][c] == 0: + adjacent_islands = set() + for dr, dc in self.DIRECTIONS: + nr, nc = r + dr, c + dc + if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] > 1: + adjacent_islands.add(grid[nr][nc]) + new_island_size = sum(island_sizes[island] for island in adjacent_islands) + 1 + max_island_size = max(max_island_size, new_island_size) + + return max_island_size +``` +

From 28d50d34c69714bbd11453451fc3fb7dedaad369 Mon Sep 17 00:00:00 2001 From: huawuque404 Date: Thu, 26 Oct 2023 19:15:25 +0800 Subject: [PATCH 0772/1533] Error changing Java code --- ...225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index dbd053d141..28bb61eea4 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -110,7 +110,7 @@ public ListNode removeNthFromEnd(ListNode head, int n){ fastIndex = fastIndex.next; } - while (fastIndex.next != null){ + while (fastIndex != null){ fastIndex = fastIndex.next; slowIndex = slowIndex.next; } From 54144f3a6b59bb8be22111a489d60edc49eeffa9 Mon Sep 17 00:00:00 2001 From: Relsola Date: Fri, 27 Oct 2023 19:49:03 +0800 Subject: [PATCH 0773/1533] =?UTF-8?q?Update=2098.=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0js=E5=92=8Cts=E8=BF=AD=E4=BB=A3=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 0a64266e2f..184060a5cd 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -595,6 +595,43 @@ var isValidBST = function (root) { }; ``` +> 迭代法: + +```JavaScript +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +let pre = null; +var isValidBST = function (root) { + const queue = []; + let cur = root; + let pre = null; + while (cur !== null || queue.length !== 0) { + if (cur !== null) { + queue.push(cur); + cur = cur.left; + } else { + cur = queue.pop(); + if (pre !== null && cur.val <= pre.val) { + return false; + } + pre = cur; + cur = cur.right; + } + } + return true; +}; +``` + ### TypeScript > 辅助数组解决: @@ -637,6 +674,30 @@ function isValidBST(root: TreeNode | null): boolean { }; ``` +> 迭代法: + +```TypeScript +function isValidBST(root: TreeNode | null): boolean { + const queue: TreeNode[] = []; + let cur: TreeNode | null = root; + let pre: TreeNode | null = null; + while (cur !== null || queue.length !== 0) { + if (cur !== null) { + queue.push(cur); + cur = cur.left; + } else { + cur = queue.pop()!; + if (pre !== null && cur!.val <= pre.val) { + return false; + } + pre = cur; + cur = cur!.right; + } + } + return true; +} +``` + ### Scala 辅助数组解决: From 1f9f95c0b285f3953f77e1111c37e01e1683e2f8 Mon Sep 17 00:00:00 2001 From: yunfeidog <1844025705@qq.com> Date: Sat, 28 Oct 2023 17:56:52 +0800 Subject: [PATCH 0774/1533] =?UTF-8?q?Update0239=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC=EF=BC=9A=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=8D=95=E8=B0=83=E9=98=9F=E5=88=97=E5=B9=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?C++=E5=8D=95=E8=B0=83=E9=98=9F=E5=88=97=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...43\346\234\200\345\244\247\345\200\274.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 6f420479eb..dbd036eefc 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -65,6 +65,10 @@ public: 这么个队列香不香,要是有现成的这种数据结构是不是更香了! **可惜了,没有! 我们需要自己实现这么个队列。** +> 其实有这种数据结构的,在C++中,可以使用可以使用multiset这种数据结构作为单调队列 +> + 多重集合(`multiset`) 用以有序地存储元素的容器。允许存在相等的元素。 +> +> 在遍历原数组的时候,只需要把窗口的头元素加入到multiset中,然后把窗口的尾元素删除即可。因为multiset是有序的,并且提供了*rbegin(),可以直接获取窗口最大值。 然后再分析一下,队列里的元素一定是要排序的,而且要最大值放在出队口,要不然怎么知道最大值呢。 @@ -839,6 +843,24 @@ impl Solution { } ``` +### C++ +使用multiset作为单调队列 +```cpp +class Solution { +public: + vector maxSlidingWindow(vector& nums, int k) { + multiset st; + vector ans; + for (int i = 0; i < nums.size(); i++) { + if (i >= k) st.erase(st.find(nums[i - k])); + st.insert(nums[i]); + if (i >= k - 1) ans.push_back(*st.rbegin()); + } + return ans; + } +}; +``` +

From 1eb14f9fe287c4bacbdf788841b58e1819c3c0db Mon Sep 17 00:00:00 2001 From: xin Date: Sat, 28 Oct 2023 18:29:48 +0800 Subject: [PATCH 0775/1533] =?UTF-8?q?add:=201035.=E4=B8=8D=E7=9B=B8?= =?UTF-8?q?=E4=BA=A4=E7=BA=BF=E6=96=B0=E5=A2=9Etypescript=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E6=95=B0=E7=BB=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\344\272\244\347\232\204\347\272\277.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index ff9e5dc4e3..cccc7f8c59 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -221,6 +221,8 @@ const maxUncrossedLines = (nums1, nums2) => { ### TypeScript: +> 二维数组 + ```typescript function maxUncrossedLines(nums1: number[], nums2: number[]): number { /** @@ -243,6 +245,33 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { }; ``` +> 滚动数组 +```typescript +function maxUncrossedLines(nums1: number[], nums2: number[]): number { + const len1 = nums1.length + const len2 = nums2.length + + const dp: number[] = new Array(len2 + 1).fill(0) + + for (let i = 1; i <= len1; i++) { + let prev: number = 0; + let temp: number = 0; + for (let j = 1; j <= len2; j++) { + // 保存当前状态未计算前的值 + temp = dp[j] + // 使用没有累加的值进行累加 + if (nums1[i - 1] === nums2[j - 1]) dp[j] = prev + 1 + // dp[j] 表示之前的 dp[i][j-1],dp[j-1] 表示 dp[i-1][j] + else dp[j] = Math.max(dp[j], dp[j - 1]) + // 下一个元素使用前一个状态未计算的值 + prev = temp + } + } + return dp[len2] +} +``` + +

From 22ac5d47548c0f6d8d1dcc2f0649c5bb297862c5 Mon Sep 17 00:00:00 2001 From: xin Date: Sat, 28 Oct 2023 18:31:15 +0800 Subject: [PATCH 0776/1533] =?UTF-8?q?fix:=200053.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=92=8C=E5=8E=9Ftypescript=E8=BF=87?= =?UTF-8?q?=E4=B8=8D=E4=BA=86leetcode=EF=BC=8C=E6=9B=B4=E6=96=B0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index f1b6470985..70ad7a8482 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -226,18 +226,20 @@ object Solution { ```typescript function maxSubArray(nums: number[]): number { - /** - dp[i]:以nums[i]结尾的最大和 - */ - const dp: number[] = [] - dp[0] = nums[0]; - let resMax: number = 0; - for (let i = 1; i < nums.length; i++) { - dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]); - resMax = Math.max(resMax, dp[i]); + const len = nums.length + if (len === 1) return nums[0] + + const dp: number[] = new Array(len) + let resMax: number = dp[0] = nums[0] + + for (let i = 1; i < len; i++) { + dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]) + // 注意值为负数的情况 + if (dp[i] > resMax) resMax = dp[i] } - return resMax; -}; + + return resMax +} ``` From ddaba36a1257d04feea3d695f9dd072e3911ced2 Mon Sep 17 00:00:00 2001 From: xin <59196051+xin0907@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:35:39 +0800 Subject: [PATCH 0777/1533] =?UTF-8?q?Update=201035.=E4=B8=8D=E7=9B=B8?= =?UTF-8?q?=E4=BA=A4=E7=9A=84=E7=BA=BF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新 1035.不相交的线 typescript 代码注释 --- ...215\347\233\270\344\272\244\347\232\204\347\272\277.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index cccc7f8c59..e0625a2ba3 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -257,13 +257,14 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { let prev: number = 0; let temp: number = 0; for (let j = 1; j <= len2; j++) { - // 保存当前状态未计算前的值 + // 备份一下当前状态(经过上层迭代后的) temp = dp[j] - // 使用没有累加的值进行累加 + // prev 相当于 dp[j-1](累加了上层的状态) + // 如果单纯 dp[j-1] 则不会包含上层状态 if (nums1[i - 1] === nums2[j - 1]) dp[j] = prev + 1 // dp[j] 表示之前的 dp[i][j-1],dp[j-1] 表示 dp[i-1][j] else dp[j] = Math.max(dp[j], dp[j - 1]) - // 下一个元素使用前一个状态未计算的值 + // 继续使用上一层状态更新参数用于当前层下一个状态 prev = temp } } From ef1c72a36b6fcc11ad2e45ac48f12630f24e6cc9 Mon Sep 17 00:00:00 2001 From: xin Date: Sun, 29 Oct 2023 17:40:58 +0800 Subject: [PATCH 0778/1533] =?UTF-8?q?update:=20=E4=BC=98=E5=8C=96=200392.?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97=20typescript=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index f7863c9499..8d1a020869 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -221,21 +221,19 @@ function isSubsequence(s: string, t: string): boolean { /** dp[i][j]: s的前i-1个,t的前j-1个,最长公共子序列的长度 */ - const sLen: number = s.length, - tLen: number = t.length; - const dp: number[][] = new Array(sLen + 1).fill(0) - .map(_ => new Array(tLen + 1).fill(0)); + const sLen = s.length + const tLen = t.length + const dp: number[][] = new Array(sLen + 1).fill(0).map(_ => new Array(tLen + 1).fill(0)) + for (let i = 1; i <= sLen; i++) { for (let j = 1; j <= tLen; j++) { - if (s[i - 1] === t[j - 1]) { - dp[i][j] = dp[i - 1][j - 1] + 1; - } else { - dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); - } + if (s[i - 1] === t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1 + // 只需要取 j-2 的 dp 值即可,不用考虑 i-2 + else dp[i][j] = dp[i][j - 1] } } - return dp[sLen][tLen] === s.length; -}; + return dp[sLen][tLen] === s.length +} ``` ### Go: From 74417c7695011c3c49d702f17cde22dfbce7aef3 Mon Sep 17 00:00:00 2001 From: xin Date: Sun, 29 Oct 2023 17:43:18 +0800 Subject: [PATCH 0779/1533] =?UTF-8?q?add:=200392.=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97=20typescript=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 8d1a020869..ebd567cbb7 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -216,6 +216,8 @@ const isSubsequence = (s, t) => { ### TypeScript: +> 二维数组 + ```typescript function isSubsequence(s: string, t: string): boolean { /** @@ -236,6 +238,31 @@ function isSubsequence(s: string, t: string): boolean { } ``` +> 滚动数组 +```typescript +function isSubsequence(s: string, t: string): boolean { + const sLen = s.length + const tLen = t.length + const dp: number[] = new Array(tLen + 1).fill(0) + + for (let i = 1; i <= sLen; i++) { + let prev: number = 0; + let temp: number = 0; + for (let j = 1; j <= tLen; j++) { + // 备份一下当前状态(经过上层迭代后的) + temp = dp[j] + // prev 相当于 dp[j-1](累加了上层的状态) + // 如果单纯 dp[j-1] 则不会包含上层状态 + if (s[i - 1] === t[j - 1]) dp[j] = prev + 1 + else dp[j] = dp[j - 1] + // 继续使用上一层状态更新参数用于当前层下一个状态 + prev = temp + } + } + return dp[tLen] === sLen +} +``` + ### Go: 二维DP: From 0c2948f0e6e5311ff04b8e1759019770b3cbbae3 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 30 Oct 2023 11:29:04 +0800 Subject: [PATCH 0780/1533] Update --- ...13\346\260\264\346\265\201\351\227\256\351\242\230.md" | 8 ++++---- ...34\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 35f3b4d6e3..6777e2d974 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -180,14 +180,14 @@ public: // 从最上最下行的节点出发,向高处遍历 for (int i = 0; i < n; i++) { - dfs (heights, pacific, i, 0); // 遍历最上行,接触太平洋 - dfs (heights, atlantic, i, m - 1); // 遍历最下行,接触大西洋 + dfs (heights, pacific, i, 0); // 遍历最左列,接触太平洋 + dfs (heights, atlantic, i, m - 1); // 遍历最右列,接触大西 } // 从最左最右列的节点出发,向高处遍历 for (int j = 0; j < m; j++) { - dfs (heights, pacific, 0, j); // 遍历最左列,接触太平洋 - dfs (heights, atlantic, n - 1, j); // 遍历最右列,接触大西洋 + dfs (heights, pacific, 0, j); // 遍历最上行,接触太平洋 + dfs (heights, atlantic, n - 1, j); // 遍历最下行,接触大西洋 } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index af4073f077..7f847f8fae 100644 --- "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -142,7 +142,7 @@ void dfs(参数) 通常我们递归的时候,我们递归搜索需要了解哪些参数,其实也可以在写递归函数的时候,发现需要什么参数,再去补充就可以。 -一般情况,深搜需要 二维数组数组结构保存所有路径,需要一维数组保存单一路径,这种保存结果的数组,我们可以定义一个全局遍历,避免让我们的函数参数过多。 +一般情况,深搜需要 二维数组数组结构保存所有路径,需要一维数组保存单一路径,这种保存结果的数组,我们可以定义一个全局变量,避免让我们的函数参数过多。 例如这样: From c35a886d35eef5870ba2ab862b6c2da99e98bc31 Mon Sep 17 00:00:00 2001 From: Xiong Gu Date: Mon, 30 Oct 2023 21:09:36 -0400 Subject: [PATCH 0781/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00827.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=BA=E5=B7=A5=E5=B2=9BPython3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\272\345\267\245\345\262\233.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index d78798253a..71b90a7810 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -282,6 +282,71 @@ class Solution { } ``` +### Python + +```python + +class Solution: + def largestIsland(self, grid: List[List[int]]) -> int: + visited = set() #标记访问过的位置 + m, n = len(grid), len(grid[0]) + res = 0 + island_size = 0 #用于保存当前岛屿的尺寸 + directions = [[0, 1], [0, -1], [1, 0], [-1, 0]] #四个方向 + islands_size = defaultdict(int) #保存每个岛屿的尺寸 + + def dfs(island_num, r, c): + visited.add((r, c)) + grid[r][c] = island_num #访问过的位置标记为岛屿编号 + nonlocal island_size + island_size += 1 + for i in range(4): + nextR = r + directions[i][0] + nextC = c + directions[i][1] + if (nextR not in range(m) or #行坐标越界 + nextC not in range(n) or #列坐标越界 + (nextR, nextC) in visited): #坐标已访问 + continue + if grid[nextR][nextC] == 1: #遇到有效坐标,进入下一个层搜索 + dfs(island_num, nextR, nextC) + + island_num = 2 #初始岛屿编号设为2, 因为grid里的数据有0和1, 所以从2开始编号 + all_land = True #标记是否整个地图都是陆地 + for r in range(m): + for c in range(n): + if grid[r][c] == 0: + all_land = False #地图里不全是陆地 + if (r, c) not in visited and grid[r][c] == 1: + island_size = 0 #遍历每个位置前重置岛屿尺寸为0 + dfs(island_num, r, c) + islands_size[island_num] = island_size #保存当前岛屿尺寸 + island_num += 1 #下一个岛屿编号加一 + if all_land: + return m * n #如果全是陆地, 返回地图面积 + + count = 0 #某个位置0变成1后当前岛屿尺寸 + #因为后续计算岛屿面积要往四个方向遍历,但某2个或3个方向的位置可能同属于一个岛, + #所以为避免重复累加,把已经访问过的岛屿编号加入到这个集合 + visited_island = set() #保存访问过的岛屿 + for r in range(m): + for c in range(n): + if grid[r][c] == 0: + count = 1 #把由0转换为1的位置计算到面积里 + visited_island.clear() #遍历每个位置前清空集合 + for i in range(4): + nearR = r + directions[i][0] + nearC = c + directions[i][1] + if nearR not in range(m) or nearC not in range(n): #周围位置越界 + continue + if grid[nearR][nearC] in visited_island: #岛屿已访问 + continue + count += islands_size[grid[nearR][nearC]] #累加连在一起的岛屿面积 + visited_island.add(grid[nearR][nearC]) #标记当前岛屿已访问 + res = max(res, count) + return res + +``` +

From 99b8b5744ef3aadb97da6ea9049ed2c1f53dbdc5 Mon Sep 17 00:00:00 2001 From: "Farmer.Chillax" <48387781+FarmerChillax@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:02:44 +0800 Subject: [PATCH 0782/1533] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\274\202\344\275\215\350\257\215.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index f47d8b05b6..6eed90a73a 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -181,6 +181,31 @@ func isAnagram(s string, t string) bool { } ``` +Go 写法二(只对字符串遍历一次) +```go +func isAnagram(s string, t string) bool { + if len(s) != len(t) { + return false + } + records := [26]int{} + for index := 0; index < len(s); index++ { + if s[index] == t[index] { + continue + } + sCharIndex := s[index] - 'a' + records[sCharIndex]++ + tCharIndex := t[index] - 'a' + records[tCharIndex]-- + } + for _, record := range records { + if record != 0 { + return false + } + } + return true +} +``` + ### JavaScript: ```js From 6323296d76b6807e0794f233a93dda9e786ec0fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sat, 4 Nov 2023 16:27:32 +0800 Subject: [PATCH 0783/1533] =?UTF-8?q?Update=200239.=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\217\243\346\234\200\345\244\247\345\200\274.md" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index dbd036eefc..19ac12619e 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -62,13 +62,9 @@ public: 每次窗口移动的时候,调用que.pop(滑动窗口中移除元素的数值),que.push(滑动窗口添加元素的数值),然后que.front()就返回我们要的最大值。 -这么个队列香不香,要是有现成的这种数据结构是不是更香了! +这么个队列香不香,要是有现成的这种数据结构是不是更香了! -**可惜了,没有! 我们需要自己实现这么个队列。** -> 其实有这种数据结构的,在C++中,可以使用可以使用multiset这种数据结构作为单调队列 -> + 多重集合(`multiset`) 用以有序地存储元素的容器。允许存在相等的元素。 -> -> 在遍历原数组的时候,只需要把窗口的头元素加入到multiset中,然后把窗口的尾元素删除即可。因为multiset是有序的,并且提供了*rbegin(),可以直接获取窗口最大值。 +其实在C++中,可以使用 multiset 来模拟这个过程,文末提供这个解法仅针对C++,以下讲解我们还是靠自己来实现这个单调队列。 然后再分析一下,队列里的元素一定是要排序的,而且要最大值放在出队口,要不然怎么知道最大值呢。 @@ -845,6 +841,10 @@ impl Solution { ### C++ 使用multiset作为单调队列 + +多重集合(`multiset`) 用以有序地存储元素的容器。允许存在相等的元素。 + +在遍历原数组的时候,只需要把窗口的头元素加入到multiset中,然后把窗口的尾元素删除即可。因为multiset是有序的,并且提供了*rbegin(),可以直接获取窗口最大值。 ```cpp class Solution { public: From 559434172cacdda8f1cb324bd161eab21bf58bfe Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:10:36 +0800 Subject: [PATCH 0784/1533] =?UTF-8?q?Update=20028.=E5=AE=9E=E7=8E=B0strStr?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 629ff014dd..bf4ad600c3 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -1358,6 +1358,72 @@ impl Solution { } ``` +>前缀表统一不减一 +```C# +public int StrStr(string haystack, string needle) +{ + if (string.IsNullOrEmpty(needle)) + return 0; + + if (needle.Length > haystack.Length || string.IsNullOrEmpty(haystack)) + return -1; + + return KMP(haystack, needle); +} + +public int KMP(string haystack, string needle) +{ + int[] next = GetNext(needle); + int i = 0, j = 0; + while (i < haystack.Length) + { + if (haystack[i] == needle[j]) + { + i++; + j++; + } + if (j == needle.Length) + return i-j; + else if (i < haystack.Length && haystack[i] != needle[j]) + if (j != 0) + { + j = next[j - 1]; + } + else + { + i++; + } + } + return -1; +} + +public int[] GetNext(string needle) +{ + int[] next = new int[needle.Length]; + next[0] = 0; + int i = 1, j = 0; + while (i < needle.Length) + { + if (needle[i] == needle[j]) + { + next[i++] = ++j; + } + else + { + if (j == 0) + { + next[i++] = 0; + } + else + { + j = next[j - 1]; + } + } + } + return next; +} +``` +

From 6316fff080e11124c6d56d45f931207cb0bfd9d4 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:13:15 +0800 Subject: [PATCH 0785/1533] =?UTF-8?q?Update=20707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\351\223\276\350\241\250.md" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index c27f0107fb..a08227d9fc 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -1485,6 +1485,77 @@ impl MyLinkedList { } ``` +### C# +```C# +class ListNode +{ + public int val; + public ListNode next; + public ListNode(int val) { this.val = val; } +} +public class MyLinkedList +{ + ListNode dummyHead; + int count; + + public MyLinkedList() + { + dummyHead = new ListNode(0); + count = 0; + } + + public int Get(int index) + { + if (index < 0 || count <= index) return -1; + ListNode current = dummyHead; + for (int i = 0; i <= index; i++) + { + current = current.next; + } + return current.val; + } + + public void AddAtHead(int val) + { + AddAtIndex(0, val); + } + + public void AddAtTail(int val) + { + AddAtIndex(count, val); + } + + public void AddAtIndex(int index, int val) + { + if (index > count) return; + index = Math.Max(0, index); + count++; + ListNode tmp1 = dummyHead; + for (int i = 0; i < index; i++) + { + tmp1 = tmp1.next; + } + ListNode tmp2 = new ListNode(val); + tmp2.next = tmp1.next; + tmp1.next = tmp2; + } + + public void DeleteAtIndex(int index) + { + + if (index >= count || index < 0) return; + var tmp1 = dummyHead; + for (int i = 0; i < index; i++) + { + tmp1 = tmp1.next; + } + tmp1.next = tmp1.next.next; + count--; + + } +} +``` +

From 87a6e78e8106d769576ddc48770deb245f830fd0 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:15:40 +0800 Subject: [PATCH 0786/1533] =?UTF-8?q?Update=200024.=E4=BA=A4=E6=8D=A2?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E8=8A=82=E7=82=B9=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index 36f9b0cc9a..57034f4795 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -459,6 +459,40 @@ impl Solution { } ``` +### C# +```C# +// 虚拟头结点 +public ListNode SwapPairs(ListNode head) +{ + var dummyHead = new ListNode(); + dummyHead.next = head; + ListNode cur = dummyHead; + while (cur.next != null && cur.next.next != null) + { + ListNode tmp1 = cur.next; + ListNode tmp2 = cur.next.next.next; + + cur.next = cur.next.next; + cur.next.next = tmp1; + cur.next.next.next = tmp2; + + cur = cur.next.next; + } + return dummyHead.next; +} +``` +``` C# +// 递归 +public ListNode SwapPairs(ListNode head) +{ + if (head == null || head.next == null) return head; + var cur = head.next; + head.next = SwapPairs(head.next.next); + cur.next = head; + return cur; +} +``` +

From 451c045a6e293bd038815400de660d3d6d091873 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:17:40 +0800 Subject: [PATCH 0787/1533] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=980?= =?UTF-8?q?2.07=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...223\276\350\241\250\347\233\270\344\272\244.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 5de9da5c06..adeaa413aa 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -502,6 +502,20 @@ object Solution { } } ``` +### C# +```C# +public ListNode GetIntersectionNode(ListNode headA, ListNode headB) +{ + if (headA == null || headB == null) return null; + ListNode cur1 = headA, cur2 = headB; + while (cur1 != cur2) + { + cur1 = cur1 == null ? headB : cur1.next; + cur2 = cur2 == null ? headA : cur2.next; + } + return cur1; +} +```

From 4500cbd9bd1d603c99482fdccbfe1deff7417c15 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:20:04 +0800 Subject: [PATCH 0788/1533] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=20LINQ=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 111c07e4f5..d15bb5f321 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -972,6 +972,13 @@ char * reverseWords(char * s){ } ``` +### C# +```C# LINQ高级方法 +public string ReverseWords(string s) { + return string.Join(' ', s.Trim().Split(' ',StringSplitOptions.RemoveEmptyEntries).Reverse()); +} +``` +

From 69cc985a7c45fb41a901c26b2868d481f8b3e206 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:40:37 +0800 Subject: [PATCH 0789/1533] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\345\255\227\347\254\246\344\270\262.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 177c3878ba..3245d94897 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -681,6 +681,32 @@ impl Solution { } } ``` +### C# +```C# +// 前缀表不减一 +public bool RepeatedSubstringPattern(string s) +{ + if (s.Length == 0) + return false; + int[] next = GetNext(s); + int len = s.Length; + if (next[len - 1] != 0 && len % (len - next[len - 1]) == 0) return true; + return false; +} +public int[] GetNext(string s) +{ + int[] next = Enumerable.Repeat(0, s.Length).ToArray(); + for (int i = 1, j = 0; i < s.Length; i++) + { + while (j > 0 && s[i] != s[j]) + j = next[j - 1]; + if (s[i] == s[j]) + j++; + next[i] = j; + } + return next; +} +```

From 4134c34c52d8ae16c9c60cd9778e1f98c6ed5a0b Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Wed, 8 Nov 2023 21:07:35 +0800 Subject: [PATCH 0790/1533] =?UTF-8?q?Update=200704.=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=9F=A5=E6=89=BE.md=20about=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\210\206\346\237\245\346\211\276.md" | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index ba5e538c74..46f529db52 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -481,41 +481,37 @@ func search(nums: [Int], target: Int) -> Int { ### **Rust:** -```rust -# (版本一)左闭右闭区间 +(版本一)左闭右开区间 +```rust impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { - let mut left:usize = 0; - let mut right:usize = nums.len() - 1; - while left as i32 <= right as i32{ - let mid = (left + right) / 2; - if nums[mid] < target { - left = mid + 1; - } else if nums[mid] > target { - right = mid - 1; - } else { - return mid as i32; + let (mut left, mut right) = (0, nums.len()); + while left < right { + let mid = (right + left) / 2; + match nums[mid].cmp(&target) { + Ordering::Less => left = mid + 1, + Ordering::Greater => right = mid, + Ordering::Equal => return mid as i32, } } -1 } } +``` -# (版本二)左闭右开区间 +//(版本二)左闭右闭区间 +```rust impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { - let mut left:usize = 0; - let mut right:usize = nums.len(); - while left < right { - let mid = (left + right) / 2; - if nums[mid] < target { - left = mid + 1; - } else if nums[mid] > target { - right = mid; - } else { - return mid as i32; + let (mut left, mut right) = (0, nums.len()); + while left <= right { + let mid = (right + left) / 2; + match nums[mid].cmp(&target) { + Ordering::Less => left = mid + 1, + Ordering::Greater => right = mid - 1, + Ordering::Equal => return mid as i32, } } -1 From 040f94472bbdd2212131661a85f4083fd6bc10be Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Wed, 8 Nov 2023 21:09:36 +0800 Subject: [PATCH 0791/1533] Apply suggestions from code review --- .../0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 46f529db52..590ec1b139 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -484,6 +484,7 @@ func search(nums: [Int], target: Int) -> Int { (版本一)左闭右开区间 ```rust +use std::cmp::Ordering; impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { let (mut left, mut right) = (0, nums.len()); @@ -503,6 +504,7 @@ impl Solution { //(版本二)左闭右闭区间 ```rust +use std::cmp::Ordering; impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { let (mut left, mut right) = (0, nums.len()); From 2caee6fe9ff27270346e2d27032b0cae3f59eba5 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 9 Nov 2023 14:32:56 +0800 Subject: [PATCH 0792/1533] Update --- README.md | 6 +- ...0.\347\210\254\346\245\274\346\242\257.md" | 7 +- ...14\345\214\205\347\211\210\346\234\254.md" | 171 ++---- ...77\346\215\242\346\225\260\345\255\227.md" | 176 +++++++ ...13\345\255\227\347\254\246\344\270\262.md" | 181 +++++++ ...46\347\235\200\345\233\236\346\272\257.md" | 2 +- ...77\346\215\242\347\251\272\346\240\274.md" | 493 ++---------------- ...54\345\255\227\347\254\246\344\270\262.md" | 435 ++++------------ ...47\241\20001\350\203\214\345\214\205-1.md" | 2 +- ...47\241\20001\350\203\214\345\214\205-2.md" | 2 +- ...32\351\207\215\350\203\214\345\214\205.md" | 399 ++------------ ...14\345\205\250\350\203\214\345\214\205.md" | 2 +- 12 files changed, 614 insertions(+), 1262 deletions(-) create mode 100644 "problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" create mode 100644 "problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" diff --git a/README.md b/README.md index 60890f228a..c1296ed3db 100644 --- a/README.md +++ b/README.md @@ -152,9 +152,9 @@ 1. [字符串:344.反转字符串](./problems/0344.反转字符串.md) 2. [字符串:541.反转字符串II](./problems/0541.反转字符串II.md) -3. [字符串:替换空格](./problems/剑指Offer05.替换空格.md) +3. [字符串:替换数字](./problems/kama54.替换数字.md) 4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) -5. [字符串:左旋转字符串](./problems/剑指Offer58-II.左旋转字符串.md) +5. [字符串:右旋字符串](./problems/kama55.右旋字符串.md) 6. [帮你把KMP算法学个通透](./problems/0028.实现strStr.md) 8. [字符串:459.重复的子字符串](./problems/0459.重复的子字符串.md) 9. [字符串:总结篇!](./problems/字符串总结.md) @@ -165,7 +165,7 @@ 1. [数组:27.移除元素](./problems/0027.移除元素.md) 2. [字符串:344.反转字符串](./problems/0344.反转字符串.md) -3. [字符串:替换空格](./problems/剑指Offer05.替换空格.md) +3. [字符串:替换数字](./problems/kama54.替换数字.md) 4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) 5. [链表:206.翻转链表](./problems/0206.翻转链表.md) 6. [链表:19.删除链表的倒数第 N 个结点](./problems/0019.删除链表的倒数第N个节点.md) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 1a1f7e31b5..7a59221dc6 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -165,9 +165,12 @@ public: 这道题目还可以继续深化,就是一步一个台阶,两个台阶,三个台阶,直到 m个台阶,有多少种方法爬到n阶楼顶。 -这又有难度了,这其实是一个完全背包问题,但力扣上没有这种题目,所以后续我在讲解背包问题的时候,今天这道题还会从背包问题的角度上来再讲一遍。 如果想提前看一下,可以看这篇:[70.爬楼梯完全背包版本](https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85%E7%89%88%E6%9C%AC.html) +这又有难度了,这其实是一个完全背包问题,但力扣上没有这种题目,大家可以去卡码网去做一下 [57. 爬楼梯](https://kamacoder.com/problempage.php?pid=1067) -这里我先给出我的实现代码: + +所以后续我在讲解背包问题的时候,今天这道题还会从背包问题的角度上来再讲一遍。 如果想提前看一下,可以看这篇:[70.爬楼梯完全背包版本](https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85%E7%89%88%E6%9C%AC.html) + +这里我先给出本题的代码: ```CPP class Solution { diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 4ca7a3710b..0da1ebeca5 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -4,30 +4,34 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 70. 爬楼梯 +# 70. 爬楼梯(进阶版) -[力扣题目链接](https://leetcode.cn/problems/climbing-stairs/) +[卡码网:57. 爬楼梯](https://kamacoder.com/problempage.php?pid=1067) -假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 +假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 -每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? +每次你可以爬至多m (1 <= m < n)个台阶。你有多少种不同的方法可以爬到楼顶呢? -注意:给定 n 是一个正整数。 +注意:给定 n 是一个正整数。 -示例 1: -输入: 2 -输出: 2 -解释: 有两种方法可以爬到楼顶。 -1. 1 阶 + 1 阶 -2. 2 阶 +输入描述:输入共一行,包含两个正整数,分别表示n, m + +输出描述:输出一个整数,表示爬到楼顶的方法数。 + +输入示例:3 2 + +输出示例:3 + +提示: + +当 m = 2,n = 3 时,n = 3 这表示一共有三个台阶,m = 2 代表你每次可以爬一个台阶或者两个台阶。 + +此时你有三种方法可以爬到楼顶。 + +* 1 阶 + 1 阶 + 1 阶段 +* 1 阶 + 2 阶 +* 2 阶 + 1 阶 -示例 2: -输入: 3 -输出: 3 -解释: 有三种方法可以爬到楼顶。 -1. 1 阶 + 1 阶 + 1 阶 -2. 1 阶 + 2 阶 -3. 2 阶 + 1 阶 ## 思路 @@ -35,11 +39,13 @@ **这次终于讲到了背包问题,我选择带录友们再爬一次楼梯!** -这道题目 我们在[动态规划:爬楼梯](https://programmercarl.com/0070.爬楼梯.html) 中已经讲过一次了,原题其实是一道简单动规的题目。 +这道题目 我们在[动态规划:爬楼梯](https://programmercarl.com/0070.爬楼梯.html) 中已经讲过一次了,这次我又给本题加点料,力扣上没有原题,所以可以在卡码网[57. 爬楼梯](https://kamacoder.com/problempage.php?pid=1067)上来刷这道题目。 -既然这么简单为什么还要讲呢,其实本题稍加改动就是一道面试好题。 +我们之前做的 爬楼梯 是只能至多爬两个台阶。 -**改为:一步一个台阶,两个台阶,三个台阶,.......,直到 m个台阶。问有多少种不同的方法可以爬到楼顶呢?** +这次**改为:一步一个台阶,两个台阶,三个台阶,.......,直到 m个台阶。问有多少种不同的方法可以爬到楼顶呢?** + +这又有难度了,这其实是一个完全背包问题。 1阶,2阶,.... m阶就是物品,楼顶就是背包。 @@ -86,27 +92,31 @@ 以上分析完毕,C++代码如下: ```CPP -class Solution { -public: - int climbStairs(int n) { +#include +#include +using namespace std; +int main() { + int n, m; + while (cin >> n >> m) { vector dp(n + 1, 0); dp[0] = 1; - for (int i = 1; i <= n; i++) { // 遍历背包 - for (int j = 1; j <= m; j++) { // 遍历物品 + for (int i = 1; i <= n; i++) { // 遍历物品 + for (int j = 1; j <= m; j++) { // 遍历背包 if (i - j >= 0) dp[i] += dp[i - j]; } } - return dp[n]; + cout << dp[n] << endl; } -}; +} ``` -* 时间复杂度: O(nm) +* 时间复杂度: O(n * m) * 空间复杂度: O(n) +代码中m表示最多可以爬m个台阶,代码中把m改成2就是 力扣:70.爬楼梯的解题思路。 +**当然注意 力扣是 核心代码模式,卡码网是ACM模式** -代码中m表示最多可以爬m个台阶,代码中把m改成2就是本题70.爬楼梯可以AC的代码了。 ## 总结 @@ -129,123 +139,22 @@ public: ### Java: -```java -class Solution { - public int climbStairs(int n) { - int[] dp = new int[n + 1]; - int m = 2; //有兩個物品:itme1重量爲一,item2重量爲二 - dp[0] = 1; - - for (int i = 1; i <= n; i++) { // 遍历背包 - for (int j = 1; j <= m; j++) { //遍历物品 - if (i >= j) //當前的背包容量 大於 物品重量的時候,我們才需要記錄當前的這個裝得方法(方法數+) - dp[i] += dp[i - j]; - } - } - - return dp[n]; - } -} -``` ### Python3: -```python -class Solution: - def climbStairs(self, n: int) -> int: - dp = [0]*(n + 1) - dp[0] = 1 - m = 2 - # 遍历背包 - for j in range(n + 1): - # 遍历物品 - for step in range(1, m + 1): - if j >= step: - dp[j] += dp[j - step] - return dp[n] -``` ### Go: -```go -func climbStairs(n int) int { - //定义 - dp := make([]int, n+1) - //初始化 - dp[0] = 1 - // 本题物品只有两个1,2 - m := 2 - // 遍历顺序 - for j := 1; j <= n; j++ { //先遍历背包 - for i := 1; i <= m; i++ { //再遍历物品 - if j >= i { - dp[j] += dp[j-i] - } - //fmt.Println(dp) - } - } - return dp[n] -} -``` ### JavaScript: -```javascript -var climbStairs = function(n) { - const dp = new Array(n + 1).fill(0); - const m = 2; - dp[0] = 1; - for(let i = 1; i <= n; i++){ - for(let j = 1; j <= m; j++){ - if(i >= j) { - dp[i] += dp[i - j]; - } - } - } - return dp[n]; -}; -``` ### TypeScript: -```typescript -function climbStairs(n: number): number { - const m: number = 2; // 本题m为2 - const dp: number[] = new Array(n + 1).fill(0); - dp[0] = 1; - // 遍历背包 - for (let i = 1; i <= n; i++) { - // 遍历物品 - for (let j = 1; j <= m; j++) { - if (j <= i) { - dp[i] += dp[i - j]; - } - } - } - return dp[n]; -}; -``` ### Rust: -```rust -impl Solution { - pub fn climb_stairs(n: i32) -> i32 { - let (n, m) = (n as usize, 2); - let mut dp = vec![0; n + 1]; - dp[0] = 1; - for i in 1..=n { - for j in 1..=m { - if i >= j { - dp[i] += dp[i - j]; - } - } - } - dp[n] - } -} -```

diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" new file mode 100644 index 0000000000..80c87951aa --- /dev/null +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -0,0 +1,176 @@ + +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ + +# 替换数字 + +[卡码网题目链接](https://kamacoder.com/problempage.php?pid=1064) + +给定一个字符串 s,它包含小写字母和数字字符,请编写一个函数,将字符串中的字母字符保持不变,而将每个数字字符替换为number。 + +例如,对于输入字符串 "a1b2c3",函数应该将其转换为 "anumberbnumbercnumber"。 + +对于输入字符串 "a5b",函数应该将其转换为 "anumberb" + +输入:一个字符串 s,s 仅包含小写字母和数字字符。 + +输出:打印一个新的字符串,其中每个数字字符都被替换为了number + +样例输入:a1b2c3 + +样例输出:anumberbnumbercnumber + +数据范围:1 <= s.length < 10000。 + +## 思路 + +如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java刷题的录友,一定要使用辅助空间,因为Java里的string不能修改) + +首先扩充数组到每个数字字符替换成 "number" 之后的大小。 + +例如 字符串 "a5b" 的长度为3,那么 将 数字字符变成字符串 "number" 之后的字符串为 "anumberb" 长度为 8。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030165201.png) + +然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030173058.png) + +有同学问了,为什么要从后向前填充,从前向后填充不行么? + +从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素整体向后移动。 + +**其实很多数组填充类的问题,其做饭都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** + +这么做有两个好处: + +1. 不用申请新数组。 +2. 从后向前填充元素,避免了从前向后填充元素时,每次添加元素都要将添加元素之后的所有元素向后移动的问题。 + +C++代码如下: + +```CPP +#include +using namespace std; +int main() { + string s; + while (cin >> s) { + int count = 0; // 统计数字的个数 + int sOldSize = s.size(); + for (int i = 0; i < s.size(); i++) { + if (s[i] >= '0' && s[i] <= '9') { + count++; + } + } + // 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小 + s.resize(s.size() + count * 5); + int sNewSize = s.size(); + // 从后先前将空格替换为"number" + for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) { + if (s[j] > '9' || s[j] < '0') { + s[i] = s[j]; + } else { + s[i] = 'r'; + s[i - 1] = 'e'; + s[i - 2] = 'b'; + s[i - 3] = 'm'; + s[i - 4] = 'u'; + s[i - 5] = 'n'; + i -= 5; + } + } + cout << s << endl; + } +} + + +``` + +* 时间复杂度:O(n) +* 空间复杂度:O(1) + +此时算上本题,我们已经做了七道双指针相关的题目了分别是: + +* [27.移除元素](https://programmercarl.com/0027.移除元素.html) +* [15.三数之和](https://programmercarl.com/0015.三数之和.html) +* [18.四数之和](https://programmercarl.com/0018.四数之和.html) +* [206.翻转链表](https://programmercarl.com/0206.翻转链表.html) +* [142.环形链表II](https://programmercarl.com/0142.环形链表II.html) +* [344.反转字符串](https://programmercarl.com/0344.反转字符串.html) + +## 拓展 + +这里也给大家拓展一下字符串和数组有什么差别, + +字符串是若干字符组成的有限序列,也可以理解为是一个字符数组,但是很多语言对字符串做了特殊的规定,接下来我来说一说C/C++中的字符串。 + +在C语言中,把一个字符串存入一个数组时,也把结束符 '\0'存入数组,并以此作为该字符串是否结束的标志。 + +例如这段代码: + +``` +char a[5] = "asd"; +for (int i = 0; a[i] != '\0'; i++) { +} +``` + +在C++中,提供一个string类,string类会提供 size接口,可以用来判断string类字符串是否结束,就不用'\0'来判断是否结束。 + +例如这段代码: + +``` +string a = "asd"; +for (int i = 0; i < a.size(); i++) { +} +``` + +那么vector< char > 和 string 又有什么区别呢? + +其实在基本操作上没有区别,但是 string提供更多的字符串处理的相关接口,例如string 重载了+,而vector却没有。 + +所以想处理字符串,我们还是会定义一个string类型。 + + +## 其他语言版本 + +### C: + +### Java: + + +### Go: + + + +### python: + +### JavaScript: + + +### TypeScript: + + +### Swift: + + +### Scala: + + +### PHP: + + +### Rust: + + + +

+ + + + diff --git "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" new file mode 100644 index 0000000000..afd29d7376 --- /dev/null +++ "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -0,0 +1,181 @@ + +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ + +# 右旋字符串 + +[卡码网题目链接](https://kamacoder.com/problempage.php?pid=1065) + +字符串的右旋转操作是把字符串尾部的若干个字符转移到字符串的前面。给定一个字符串 s 和一个正整数 k,请编写一个函数,将字符串中的后面 k 个字符移到字符串的前面,实现字符串的右旋转操作。 + +例如,对于输入字符串 "abcdefg" 和整数 2,函数应该将其转换为 "fgabcde"。 + +输入:输入共包含两行,第一行为一个正整数 k,代表右旋转的位数。第二行为字符串 s,代表需要旋转的字符串。 + +输出:输出共一行,为进行了右旋转操作后的字符串。 + +样例输入: + +``` +2 +abcdefg +``` + +样例输出: + +``` +fgabcde +``` + +数据范围:1 <= k < 10000, 1 <= s.length < 10000; + + +## 思路 + +为了让本题更有意义,提升一下本题难度:**不能申请额外空间,只能在本串上操作**。 (Java不能在字符串上修改,所以使用java一定要开辟新空间) + +不能使用额外空间的话,模拟在本串操作要实现右旋转字符串的功能还是有点困难的。 + +那么我们可以想一下上一题目[字符串:花式反转还不够!](https://programmercarl.com/0151.翻转字符串里的单词.html)中讲过,使用整体反转+局部反转就可以实现反转单词顺序的目的。 + + +本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106170143.png) + + +右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106171557.png) + +此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172058.png) + +其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 + +整体代码如下: + +```CPP +// 版本一 +#include +#include +using namespace std; +int main() { + int n; + string s; + cin >> n; + cin >> s; + int len = s.size(); //获取长度 + + reverse(s.begin(), s.end()); // 整体反转 + reverse(s.begin(), s.begin() + n); // 先反转前一段,长度n + reverse(s.begin() + n, s.end()); // 再反转后一段 + + cout << s << endl; + +} +``` + +那么整体反正的操作放在下面,先局部反转行不行? + +可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172534.png) + +代码如下: + +```CPP +// 版本二 +#include +#include +using namespace std; +int main() { + int n; + string s; + cin >> n; + cin >> s; + int len = s.size(); //获取长度 + reverse(s.begin(), s.begin() + len - n); // 先反转前一段,长度len-n ,注意这里是和版本一的区别 + reverse(s.begin() + len - n, s.end()); // 再反转后一段 + reverse(s.begin(), s.end()); // 整体反转 + cout << s << endl; + +} +``` + + +## 拓展 + +大家在做剑指offer的时候,会发现 剑指offer的题目是左反转,那么左反转和右反转 有什么区别呢? + +其实思路是一样一样的,就是反转的区间不同而已。如果本题是左旋转n,那么实现代码如下: + +```CPP +#include +#include +using namespace std; +int main() { + int n; + string s; + cin >> n; + cin >> s; + int len = s.size(); //获取长度 + reverse(s.begin(), s.begin() + n); // 反转第一段长度为n + reverse(s.begin() + n, s.end()); // 反转第二段长度为len-n + reverse(s.begin(), s.end()); // 整体反转 + cout << s << endl; + +} +``` + +大家可以感受一下 这份代码和 版本二的区别, 其实就是反转的区间不同而已。 + +那么左旋转的话,可以不可以先整体反转,例如想版本一的那样呢? + +当然可以。 + + + + +## 其他语言版本 + +### Java: + + + +### Python: + + +### Go: + + +### JavaScript: + + +### TypeScript: + + +### Swift: + + + +### PHP: + + +### Scala: + + +### Rust: + + + + +

+ + + diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" index 9bf7405d06..67570bc8d6 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" @@ -621,7 +621,7 @@ func _binaryTreePaths3(_ root: TreeNode, res: inout [String], paths: inout [Int] > 100.相同的树 -```rsut +```rust use std::cell::RefCell; use std::rc::Rc; impl Solution { diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index fed08a5346..040be29983 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -5,73 +5,92 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 题目:剑指Offer 05.替换空格 +# 替换数字 -[力扣题目链接](https://leetcode.cn/problems/ti-huan-kong-ge-lcof/) +力扣已经将剑指offer题目下架,所以我在卡码网上给大家提供类似的题目来练习 -请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 +[卡码网题目链接](https://kamacoder.com/problempage.php?pid=1064) -示例 1: -输入:s = "We are happy." -输出:"We%20are%20happy." +给定一个字符串 s,它包含小写字母和数字字符,请编写一个函数,将字符串中的字母字符保持不变,而将每个数字字符替换为number。 + +例如,对于输入字符串 "a1b2c3",函数应该将其转换为 "anumberbnumbercnumber"。 + +对于输入字符串 "a5b",函数应该将其转换为 "anumberb" + +输入:一个字符串 s,s 仅包含小写字母和数字字符。 + +输出:打印一个新的字符串,其中每个数字字符都被替换为了number + +样例输入:a1b2c3 + +样例输出:anumberbnumbercnumber + +数据范围:1 <= s.length < 10000。 ## 思路 -如果想把这道题目做到极致,就不要只用额外的辅助空间了! +如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java刷题的录友,一定要使用辅助空间,因为Java里的string不能修改) + +首先扩充数组到每个数字字符替换成 "number" 之后的大小。 + +例如 字符串 "a5b" 的长度为3,那么 将 数字字符变成字符串 "number" 之后的字符串为 "anumberb" 长度为 8。 -首先扩充数组到每个空格替换成"%20"之后的大小。 +如图: -然后从后向前替换空格,也就是双指针法,过程如下: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030165201.png) -i指向新长度的末尾,j指向旧长度的末尾。 +然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 -![替换空格](https://code-thinking.cdn.bcebos.com/gifs/%E6%9B%BF%E6%8D%A2%E7%A9%BA%E6%A0%BC.gif) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030173058.png) 有同学问了,为什么要从后向前填充,从前向后填充不行么? -从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素向后移动。 +从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素整体向后移动。 -**其实很多数组填充类的问题,都可以先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** +**其实很多数组填充类的问题,其做饭都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** 这么做有两个好处: 1. 不用申请新数组。 2. 从后向前填充元素,避免了从前向后填充元素时,每次添加元素都要将添加元素之后的所有元素向后移动的问题。 -时间复杂度,空间复杂度均超过100%的用户。 - - - C++代码如下: ```CPP -class Solution { -public: - string replaceSpace(string s) { - int count = 0; // 统计空格的个数 +#include +using namespace std; +int main() { + string s; + while (cin >> s) { + int count = 0; // 统计数字的个数 int sOldSize = s.size(); for (int i = 0; i < s.size(); i++) { - if (s[i] == ' ') { + if (s[i] >= '0' && s[i] <= '9') { count++; } } - // 扩充字符串s的大小,也就是每个空格替换成"%20"之后的大小 - s.resize(s.size() + count * 2); + // 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小 + s.resize(s.size() + count * 5); int sNewSize = s.size(); - // 从后先前将空格替换为"%20" + // 从后先前将空格替换为"number" for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) { - if (s[j] != ' ') { + if (s[j] > '9' || s[j] < '0') { s[i] = s[j]; } else { - s[i] = '0'; - s[i - 1] = '2'; - s[i - 2] = '%'; - i -= 2; + s[i] = 'r'; + s[i - 1] = 'e'; + s[i - 2] = 'b'; + s[i - 3] = 'm'; + s[i - 4] = 'u'; + s[i - 5] = 'n'; + i -= 5; } } - return s; + cout << s << endl; } -}; +} + + ``` * 时间复杂度:O(n) @@ -123,442 +142,32 @@ for (int i = 0; i < a.size(); i++) { ### C: -```C -char* replaceSpace(char* s){ - //统计空格数量 - int count = 0; - int len = strlen(s); - for (int i = 0; i < len; i++) { - if (s[i] == ' ') { - count++; - } - } - - //为新数组分配空间 - int newLen = len + count * 2; - char* result = malloc(sizeof(char) * newLen + 1); - //填充新数组并替换空格 - for (int i = len - 1, j = newLen - 1; i >= 0; i--, j--) { - if (s[i] != ' ') { - result[j] = s[i]; - } else { - result[j--] = '0'; - result[j--] = '2'; - result[j] = '%'; - } - } - result[newLen] = '\0'; - - return result; -} -``` - ### Java: -```Java -//使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制 -public static String replaceSpace(String s) { - if (s == null) { - return null; - } - //选用 StringBuilder 单线程使用,比较快,选不选都行 - StringBuilder sb = new StringBuilder(); - //使用 sb 逐个复制 s ,碰到空格则替换,否则直接复制 - for (int i = 0; i < s.length(); i++) { - //s.charAt(i) 为 char 类型,为了比较需要将其转为和 " " 相同的字符串类型 - //if (" ".equals(String.valueOf(s.charAt(i)))){} - if (s.charAt(i) == ' ') { - sb.append("%20"); - } else { - sb.append(s.charAt(i)); - } - } - return sb.toString(); - } - -//方式二:双指针法 -public String replaceSpace(String s) { - if(s == null || s.length() == 0){ - return s; - } - //扩充空间,空格数量2倍 - StringBuilder str = new StringBuilder(); - for (int i = 0; i < s.length(); i++) { - if(s.charAt(i) == ' '){ - str.append(" "); - } - } - //若是没有空格直接返回 - if(str.length() == 0){ - return s; - } - //有空格情况 定义两个指针 - int left = s.length() - 1;//左指针:指向原始字符串最后一个位置 - s += str.toString(); - int right = s.length()-1;//右指针:指向扩展字符串的最后一个位置 - char[] chars = s.toCharArray(); - while(left>=0){ - if(chars[left] == ' '){ - chars[right--] = '0'; - chars[right--] = '2'; - chars[right] = '%'; - }else{ - chars[right] = chars[left]; - } - left--; - right--; - } - return new String(chars); -} -``` ### Go: -```go -// 遍历添加 -func replaceSpace(s string) string { - b := []byte(s) - result := make([]byte, 0) - for i := 0; i < len(b); i++ { - if b[i] == ' ' { - result = append(result, []byte("%20")...) - } else { - result = append(result, b[i]) - } - } - return string(result) -} - -// 原地修改 -func replaceSpace(s string) string { - b := []byte(s) - length := len(b) - spaceCount := 0 - // 计算空格数量 - for _, v := range b { - if v == ' ' { - spaceCount++ - } - } - // 扩展原有切片 - resizeCount := spaceCount * 2 - tmp := make([]byte, resizeCount) - b = append(b, tmp...) - i := length - 1 - j := len(b) - 1 - for i >= 0 { - if b[i] != ' ' { - b[j] = b[i] - i-- - j-- - } else { - b[j] = '0' - b[j-1] = '2' - b[j-2] = '%' - i-- - j = j - 3 - } - } - return string(b) -} -``` - ### python: -因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1) - -(版本一)转换成列表,并且添加相匹配的空间,然后进行填充 -```python -class Solution: - def replaceSpace(self, s: str) -> str: - counter = s.count(' ') - - res = list(s) - # 每碰到一个空格就多拓展两个格子,1 + 2 = 3个位置存’%20‘ - res.extend([' '] * counter * 2) - - # 原始字符串的末尾,拓展后的末尾 - left, right = len(s) - 1, len(res) - 1 - - while left >= 0: - if res[left] != ' ': - res[right] = res[left] - right -= 1 - else: - # [right - 2, right), 左闭右开 - res[right - 2: right + 1] = '%20' - right -= 3 - left -= 1 - return ''.join(res) - -``` -(版本二)添加空列表,添加匹配的结果 -```python -class Solution: - def replaceSpace(self, s: str) -> str: - res = [] - for i in range(len(s)): - if s[i] == ' ': - res.append('%20') - else: - res.append(s[i]) - return ''.join(res) -``` -(版本三)使用切片 -```python -class Solution: - def replaceSpace(self, s: str) -> str: - n = len(s) - for e, i in enumerate(s[::-1]): - print(i, e) - if i == " ": - s = s[: n - (e + 1)] + "%20" + s[n - e:] - print("") - return s -``` -(版本四)使用join + split -```python -class Solution: - def replaceSpace(self, s: str) -> str: - return "%20".join(s.split(" ")) -``` -(版本五)使用replace -```python -class Solution: - def replaceSpace(self, s: str) -> str: - return s.replace(' ', '%20') -``` ### JavaScript: -```js -/** - * @param {string} s - * @return {string} - */ - var replaceSpace = function(s) { - // 字符串转为数组 - const strArr = Array.from(s); - let count = 0; - - // 计算空格数量 - for(let i = 0; i < strArr.length; i++) { - if (strArr[i] === ' ') { - count++; - } - } - - let left = strArr.length - 1; - let right = strArr.length + count * 2 - 1; - - while(left >= 0) { - if (strArr[left] === ' ') { - strArr[right--] = '0'; - strArr[right--] = '2'; - strArr[right--] = '%'; - left--; - } else { - strArr[right--] = strArr[left--]; - } - } - - // 数组转字符串 - return strArr.join(''); -}; -``` ### TypeScript: -```typescript -function replaceSpace(s: string): string { - let arr: string[] = s.split(''); - let spaceNum: number = 0; - let oldLength: number = arr.length; - for (let i = 0; i < oldLength; i++) { - if (arr[i] === ' ') { - spaceNum++; - } - } - arr.length = oldLength + 2 * spaceNum; - let cur: number = oldLength - 1; - for (let i = arr.length - 1; i >= 0; i--, cur--) { - if (arr[cur] !== ' ') { - arr[i] = arr[cur] - } else { - arr[i] = '0'; - arr[--i] = '2'; - arr[--i] = '%'; - } - } - return arr.join(''); -}; -``` ### Swift: -```swift -func replaceSpace(_ s: String) -> String { - var strArr = Array(s) - var count = 0 - - // 统计空格的个数 - for i in strArr { - if i == " " { - count += 1 - } - } - // left 指向旧数组的最后一个元素 - var left = strArr.count - 1 - // right 指向扩容后数组的最后一个元素(这里还没对数组进行实际上的扩容) - var right = strArr.count + count * 2 - 1 - - // 实际对数组扩容 - for _ in 0..<(count * 2) { - strArr.append(" ") - } - - while left < right { - if strArr[left] == " " { - strArr[right] = "0" - strArr[right - 1] = "2" - strArr[right - 2] = "%" - left -= 1 - right -= 3 - } else { - strArr[right] = strArr[left] - left -= 1 - right -= 1 - } - } - - return String(strArr) -} -``` ### Scala: -方式一: 双指针 -```scala -object Solution { - def replaceSpace(s: String): String = { - var count = 0 - s.foreach(c => if (c == ' ') count += 1) // 统计空格的数量 - val sOldSize = s.length // 旧数组字符串长度 - val sNewSize = s.length + count * 2 // 新数组字符串长度 - val res = new Array[Char](sNewSize) // 新数组 - var index = sNewSize - 1 // 新数组索引 - // 逆序遍历 - for (i <- (0 until sOldSize).reverse) { - if (s(i) == ' ') { - res(index) = '0' - index -= 1 - res(index) = '2' - index -= 1 - res(index) = '%' - } else { - res(index) = s(i) - } - index -= 1 - } - res.mkString - } -} -``` -方式二: 使用一个集合,遇到空格就添加%20 -```scala -object Solution { - import scala.collection.mutable.ListBuffer - def replaceSpace(s: String): String = { - val res: ListBuffer[Char] = ListBuffer[Char]() - for (i <- s.indices) { - if (s(i) == ' ') { - res += '%' - res += '2' - res += '0' - }else{ - res += s(i) - } - } - res.mkString - } -} -``` -方式三: 使用map -```scala -object Solution { - def replaceSpace(s: String): String = { - s.map(c => if(c == ' ') "%20" else c).mkString - } - } -``` ### PHP: -```php -function replaceSpace($s){ - $sLen = strlen($s); - $moreLen = $this->spaceLen($s) * 2; - - $head = $sLen - 1; - $tail = $sLen + $moreLen - 1; - - $s = $s . str_repeat(' ', $moreLen); - while ($head != $tail) { - if ($s[$head] == ' ') { - $s[$tail--] = '0'; - $s[$tail--] = '2'; - $s[$tail] = '%'; - } else { - $s[$tail] = $s[$head]; - } - $head--; - $tail--; - } - return $s; -} -// 统计空格个数 -function spaceLen($s){ - $count = 0; - for ($i = 0; $i < strlen($s); $i++) { - if ($s[$i] == ' ') { - $count++; - } - } - return $count; -} -``` ### Rust: -```Rust -impl Solution { - pub fn replace_space(s: String) -> String { - let mut len: usize = s.len(); - let mut s = s.chars().collect::>(); - let mut count = 0; - for i in &s { - if i.is_ascii_whitespace() { - count += 1; - } - } - let mut new_len = len + count * 2; - s.resize(new_len, ' '); - while len < new_len { - len -= 1; - new_len -= 1; - if s[len].is_ascii_whitespace() { - s[new_len] = '0'; - s[new_len - 1] = '2'; - s[new_len - 2] = '%'; - new_len -= 2; - } - else { s[new_len] = s[len] } - } - s.iter().collect::() - } -} -```

diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 43a0fe0849..138cf3a88d 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -5,413 +5,174 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-> 反转个字符串还有这么多用处? +# 右旋字符串 -# 题目:剑指Offer58-II.左旋转字符串 +力扣已经将剑指offer题目下架,所以在卡码网上给大家提供类似的题目来练习 -[力扣题目链接](https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/) +[卡码网题目链接](https://kamacoder.com/problempage.php?pid=1065) -字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。 +字符串的右旋转操作是把字符串尾部的若干个字符转移到字符串的前面。给定一个字符串 s 和一个正整数 k,请编写一个函数,将字符串中的后面 k 个字符移到字符串的前面,实现字符串的右旋转操作。 -示例 1: -输入: s = "abcdefg", k = 2 -输出: "cdefgab" +例如,对于输入字符串 "abcdefg" 和整数 2,函数应该将其转换为 "fgabcde"。 -示例 2: -输入: s = "lrloseumgh", k = 6 -输出: "umghlrlose" +输入:输入共包含两行,第一行为一个正整数 k,代表右旋转的位数。第二行为字符串 s,代表需要旋转的字符串。 -限制: -1 <= k < s.length <= 10000 +输出:输出共一行,为进行了右旋转操作后的字符串。 -## 思路 +样例输入: -为了让本题更有意义,提升一下本题难度:**不能申请额外空间,只能在本串上操作**。 +``` +2 +abcdefg +``` -不能使用额外空间的话,模拟在本串操作要实现左旋转字符串的功能还是有点困难的。 +样例输出: +``` +fgabcde +``` -那么我们可以想一下上一题目[字符串:花式反转还不够!](https://programmercarl.com/0151.翻转字符串里的单词.html)中讲过,使用整体反转+局部反转就可以实现反转单词顺序的目的。 +数据范围:1 <= k < 10000, 1 <= s.length < 10000; -这道题目也非常类似,依然可以通过局部反转+整体反转 达到左旋转的目的。 -具体步骤为: +## 思路 -1. 反转区间为前n的子串 -2. 反转区间为n到末尾的子串 -3. 反转整个字符串 +为了让本题更有意义,提升一下本题难度:**不能申请额外空间,只能在本串上操作**。 (Java不能在字符串上修改,所以使用java一定要开辟新空间) -最后就可以达到左旋n的目的,而不用定义新的字符串,完全在本串上操作。 +不能使用额外空间的话,模拟在本串操作要实现右旋转字符串的功能还是有点困难的。 -例如 :示例1中 输入:字符串abcdefg,n=2 +那么我们可以想一下上一题目[字符串:花式反转还不够!](https://programmercarl.com/0151.翻转字符串里的单词.html)中讲过,使用整体反转+局部反转就可以实现反转单词顺序的目的。 -如图: - +本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) -最终得到左旋2个单元的字符串:cdefgab +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106170143.png) -思路明确之后,那么代码实现就很简单了 -C++代码如下: +右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: -```CPP -class Solution { -public: - string reverseLeftWords(string s, int n) { - reverse(s.begin(), s.begin() + n); - reverse(s.begin() + n, s.end()); - reverse(s.begin(), s.end()); - return s; - } -}; -``` -* 时间复杂度: O(n) -* 空间复杂度:O(1) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106171557.png) -是不是发现这代码也太简单了。 +此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: -## 总结 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172058.png) +其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 -此时我们已经反转好多次字符串了,来一起回顾一下吧。 +整体代码如下: -在这篇文章[344.反转字符串](https://programmercarl.com/0344.反转字符串.html),第一次讲到反转一个字符串应该怎么做,使用了双指针法。 +```CPP +// 版本一 +#include +#include +using namespace std; +int main() { + int n; + string s; + cin >> n; + cin >> s; + int len = s.size(); //获取长度 -然后发现[541. 反转字符串II](https://programmercarl.com/0541.反转字符串II.html),这里开始给反转加上了一些条件,当需要固定规律一段一段去处理字符串的时候,要想想在for循环的表达式上做做文章。 + reverse(s.begin(), s.end()); // 整体反转 + reverse(s.begin(), s.begin() + n); // 先反转前一段,长度n + reverse(s.begin() + n, s.end()); // 再反转后一段 -后来在[151.翻转字符串里的单词](https://programmercarl.com/0151.翻转字符串里的单词.html)中,要对一句话里的单词顺序进行反转,发现先整体反转再局部反转 是一个很妙的思路。 + cout << s << endl; -最后再讲到本题,本题则是先局部反转再 整体反转,与[151.翻转字符串里的单词](https://programmercarl.com/0151.翻转字符串里的单词.html)类似,但是也是一种新的思路。 +} +``` -好了,反转字符串一共就介绍到这里,相信大家此时对反转字符串的常见操作已经很了解了。 +那么整体反正的操作放在下面,先局部反转行不行? -## 题外话 +可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: -一些同学热衷于使用substr,来做这道题。 -其实使用substr 和 反转 时间复杂度是一样的 ,都是O(n),但是使用substr申请了额外空间,所以空间复杂度是O(n),而反转方法的空间复杂度是O(1)。 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172534.png) -**如果想让这套题目有意义,就不要申请额外空间。** +代码如下: +```CPP +// 版本二 +#include +#include +using namespace std; +int main() { + int n; + string s; + cin >> n; + cin >> s; + int len = s.size(); //获取长度 + reverse(s.begin(), s.begin() + len - n); // 先反转前一段,长度len-n ,注意这里是和版本一的区别 + reverse(s.begin() + len - n, s.end()); // 再反转后一段 + reverse(s.begin(), s.end()); // 整体反转 + cout << s << endl; -## 其他语言版本 +} +``` -### Java: -```java -class Solution { - public String reverseLeftWords(String s, int n) { - int len=s.length(); - StringBuilder sb=new StringBuilder(s); - reverseString(sb,0,n-1); - reverseString(sb,n,len-1); - return sb.reverse().toString(); - } - public void reverseString(StringBuilder sb, int start, int end) { - while (start < end) { - char temp = sb.charAt(start); - sb.setCharAt(start, sb.charAt(end)); - sb.setCharAt(end, temp); - start++; - end--; - } - } +## 拓展 + +大家在做剑指offer的时候,会发现 剑指offer的题目是左反转,那么左反转和右反转 有什么区别呢? + +其实思路是一样一样的,就是反转的区间不同而已。如果本题是左旋转n,那么实现代码如下: + +```CPP +#include +#include +using namespace std; +int main() { + int n; + string s; + cin >> n; + cin >> s; + int len = s.size(); //获取长度 + reverse(s.begin(), s.begin() + n); // 反转第一段长度为n + reverse(s.begin() + n, s.end()); // 反转第二段长度为len-n + reverse(s.begin(), s.end()); // 整体反转 + cout << s << endl; + } ``` -```java -// 解法二 -// 空间复杂度:O(n)。String 的 toCharArray() 方法底层会 new 一个和原字符串相同大小的 char 数组 -// 思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个 -class Solution { - public String reverseLeftWords(String s, int n) { - char[] chars = s.toCharArray(); - reverse(chars, 0, chars.length - 1); - reverse(chars, 0, chars.length - 1 - n); - reverse(chars, chars.length - n, chars.length - 1); - return new String(chars); - } - - public void reverse(char[] chars, int left, int right) { - while (left < right) { - chars[left] ^= chars[right]; - chars[right] ^= chars[left]; - chars[left] ^= chars[right]; - left++; - right--; - } - } -``` +大家可以感受一下 这份代码和 版本二的区别, 其实就是反转的区间不同而已。 -### Python: -(版本一)使用切片 +那么左旋转的话,可以不可以先整体反转,例如想版本一的那样呢? -```python -class Solution: - def reverseLeftWords(self, s: str, n: int) -> str: - return s[n:] + s[:n] -``` -(版本二)使用reversed + join - -```python -class Solution: - def reverseLeftWords(self, s: str, n: int) -> str: - s = list(s) - s[0:n] = list(reversed(s[0:n])) - s[n:] = list(reversed(s[n:])) - s.reverse() - - return "".join(s) +当然可以。 -``` -(版本三)自定义reversed函数 - -```python -class Solution: - def reverseLeftWords(self, s: str, n: int) -> str: - s_list = list(s) - - self.reverse(s_list, 0, n - 1) - self.reverse(s_list, n, len(s_list) - 1) - self.reverse(s_list, 0, len(s_list) - 1) - - return ''.join(s_list) - - def reverse(self, s, start, end): - while start < end: - s[start], s[end] = s[end], s[start] - start += 1 - end -= 1 -``` -(版本四)使用 模 +下标 -```python 3 -class Solution: - def reverseLeftWords(self, s: str, n: int) -> str: - new_s = '' - for i in range(len(s)): - j = (i+n)%len(s) - new_s = new_s + s[j] - return new_s -``` -(版本五)使用 模 + 切片 - -```python 3 -class Solution: - def reverseLeftWords(self, s: str, n: int) -> str: - l = len(s) - # 复制输入字符串与它自己连接 - s = s + s - - # 计算旋转字符串的起始索引 - k = n % (l * 2) - - # 从连接的字符串中提取旋转后的字符串并返回 - return s[k : k + l] +## 其他语言版本 + +### Java: + + + +### Python: -``` ### Go: -```go -func reverseLeftWords(s string, n int) string { - b := []byte(s) - // 1. 反转前n个字符 - // 2. 反转第n到end字符 - // 3. 反转整个字符 - reverse(b, 0, n-1) - reverse(b, n, len(b)-1) - reverse(b, 0, len(b)-1) - return string(b) -} -// 切片是引用传递 -func reverse(b []byte, left, right int){ - for left < right{ - b[left], b[right] = b[right],b[left] - left++ - right-- - } -} -``` ### JavaScript: -```javascript -var reverseLeftWords = function(s, n) { - const length = s.length; - let i = 0; - while (i < length - n) { - s = s[length - 1] + s; - i++; - } - return s.slice(0, length); -}; -``` - -版本二(在原字符串上操作): - -```js -/** - * @param {string} s - * @param {number} n - * @return {string} - */ -var reverseLeftWords = function (s, n) { - /** Utils */ - function reverseWords(strArr, start, end) { - let temp; - while (start < end) { - temp = strArr[start]; - strArr[start] = strArr[end]; - strArr[end] = temp; - start++; - end--; - } - } - /** Main code */ - let strArr = s.split(''); - let length = strArr.length; - reverseWords(strArr, 0, length - 1); - reverseWords(strArr, 0, length - n - 1); - reverseWords(strArr, length - n, length - 1); - return strArr.join(''); -}; -``` ### TypeScript: -```typescript -function reverseLeftWords(s: string, n: number): string { - /** Utils */ - function reverseWords(strArr: string[], start: number, end: number): void { - let temp: string; - while (start < end) { - temp = strArr[start]; - strArr[start] = strArr[end]; - strArr[end] = temp; - start++; - end--; - } - } - /** Main code */ - let strArr: string[] = s.split(''); - let length: number = strArr.length; - reverseWords(strArr, 0, length - 1); - reverseWords(strArr, 0, length - n - 1); - reverseWords(strArr, length - n, length - 1); - return strArr.join(''); -}; -``` -方法二: -```typescript -// 拼接两个字符串,截取符合要求的部分 -function reverseLeftWords(s: string, n: number): string { - return (s+s).slice(n,s.length+n); -}; -``` ### Swift: -```swift -func reverseLeftWords(_ s: String, _ n: Int) -> String { - var ch = Array(s) - let len = ch.count - // 反转区间[0, n - 1] - reverseString(&ch, startIndex: 0, endIndex: n - 1) - // 反转区间[n, len - 1] - reverseString(&ch, startIndex: n, endIndex: len - 1) - // 反转区间[0, len - 1],也就是整个字符串反转 - reverseString(&ch, startIndex: 0, endIndex: len - 1) - return String(ch) -} - -func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) { - var start = startIndex - var end = endIndex - while start < end { - (s[start], s[end]) = (s[end], s[start]) - start += 1 - end -= 1 - } -} -``` ### PHP: -```php -function reverseLeftWords($s, $n) { - $this->reverse($s,0,$n-1); //反转区间为前n的子串 - $this->reverse($s,$n,strlen($s)-1); //反转区间为n到末尾的子串 - $this->reverse($s,0,strlen($s)-1); //反转整个字符串 - return $s; -} - -// 按指定进行翻转 【array、string都可】 -function reverse(&$s, $start, $end) { - for ($i = $start, $j = $end; $i < $j; $i++, $j--) { - $tmp = $s[$i]; - $s[$i] = $s[$j]; - $s[$j] = $tmp; - } -} -``` ### Scala: -```scala -object Solution { - def reverseLeftWords(s: String, n: Int): String = { - var str = s.toCharArray // 转换为Array - // abcdefg => ba cdefg - reverseString(str, 0, n - 1) - // ba cdefg => ba gfedc - reverseString(str, n, str.length - 1) - // ba gfedc => cdefgab - reverseString(str, 0, str.length - 1) - // 最终返回,return关键字可以省略 - new String(str) - } - // 翻转字符串 - def reverseString(s: Array[Char], start: Int, end: Int): Unit = { - var (left, right) = (start, end) - while (left < right) { - var tmp = s(left) - s(left) = s(right) - s(right) = tmp - left += 1 - right -= 1 - } - } -} -``` ### Rust: -```Rust -impl Solution { - pub fn reverse(s: &mut Vec, mut begin: usize, mut end: usize){ - while begin < end { - let temp = s[begin]; - s[begin] = s[end]; - s[end] = temp; - begin += 1; - end -= 1; - } - } - pub fn reverse_left_words(s: String, n: i32) -> String { - let len = s.len(); - let mut s = s.chars().collect::>(); - let n = n as usize; - Self::reverse(&mut s, 0, n - 1); - Self::reverse(&mut s, n, len - 1); - Self::reverse(&mut s, 0, len - 1); - s.iter().collect::() - } -} -``` diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index bc5f2e5d67..09fb97a9d8 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -9,7 +9,7 @@ # 动态规划:01背包理论基础 -本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的。 +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的。 ## 算法公开课 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 0b4f8450c2..674166a199 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -6,7 +6,7 @@ # 动态规划:01背包理论基础(滚动数组) -本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习 +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习 ## 算法公开课 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index 50c2e5bf8c..b19b52731a 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -7,6 +7,8 @@ # 动态规划:关于多重背包,你该了解这些! +本题力扣上没有原题,大家可以去[卡码网第56题](https://kamacoder.com/problempage.php?pid=1066)去练习,题意是一样的。 + 之前我们已经系统的讲解了01背包和完全背包,如果没有看过的录友,建议先把如下三篇文章仔细阅读一波。 * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) @@ -53,79 +55,96 @@ 毫无区别,这就转成了一个01背包问题了,且每个物品只用一次。 -这种方式来实现多重背包的代码如下: + +练习题目:[卡码网第56题,多重背包](https://kamacoder.com/problempage.php?pid=1066) + +代码如下: ```CPP -void test_multi_pack() { - vector weight = {1, 3, 4}; - vector value = {15, 20, 30}; - vector nums = {2, 3, 2}; - int bagWeight = 10; - for (int i = 0; i < nums.size(); i++) { - while (nums[i] > 1) { // nums[i]保留到1,把其他物品都展开 +// 超时了 +#include +#include +using namespace std; +int main() { + int bagWeight,n; + cin >> bagWeight >> n; + vector weight(n, 0); + vector value(n, 0); + vector nums(n, 0); + for (int i = 0; i < n; i++) cin >> weight[i]; + for (int i = 0; i < n; i++) cin >> value[i]; + for (int i = 0; i < n; i++) cin >> nums[i]; + + for (int i = 0; i < n; i++) { + while (nums[i] > 1) { // 物品数量不是一的,都展开 weight.push_back(weight[i]); value.push_back(value[i]); nums[i]--; } } - + vector dp(bagWeight + 1, 0); - for(int i = 0; i < weight.size(); i++) { // 遍历物品 + for(int i = 0; i < weight.size(); i++) { // 遍历物品,注意此时的物品数量不是n for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量 dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); } - for (int j = 0; j <= bagWeight; j++) { - cout << dp[j] << " "; - } - cout << endl; } cout << dp[bagWeight] << endl; - -} -int main() { - test_multi_pack(); } +``` + +大家去提交之后,发现这个解法超时了,为什么呢,哪里耗时呢? + +耗时就在 这段代码: +```CPP +for (int i = 0; i < n; i++) { + while (nums[i] > 1) { // 物品数量不是一的,都展开 + weight.push_back(weight[i]); + value.push_back(value[i]); + nums[i]--; + } +} ``` -* 时间复杂度:O(m × n × k),m:物品种类个数,n背包容量,k单类物品数量 +如果物品数量很多的话,C++中,这种操作十分费时,主要消耗在vector的动态底层扩容上。(其实这里也可以优化,先把 所有物品数量都计算好,一起申请vector的空间。 + -也有另一种实现方式,就是把每种商品遍历的个数放在01背包里面在遍历一遍。 +这里也有另一种实现方式,就是把每种商品遍历的个数放在01背包里面在遍历一遍。 代码如下:(详看注释) +```CPP +#include +#include +using namespace std; +int main() { + int bagWeight,n; + cin >> bagWeight >> n; + vector weight(n, 0); + vector value(n, 0); + vector nums(n, 0); + for (int i = 0; i < n; i++) cin >> weight[i]; + for (int i = 0; i < n; i++) cin >> value[i]; + for (int i = 0; i < n; i++) cin >> nums[i]; -```CPP -void test_multi_pack() { - vector weight = {1, 3, 4}; - vector value = {15, 20, 30}; - vector nums = {2, 3, 2}; - int bagWeight = 10; vector dp(bagWeight + 1, 0); - - for(int i = 0; i < weight.size(); i++) { // 遍历物品 + for(int i = 0; i < n; i++) { // 遍历物品 for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量 // 以上为01背包,然后加一个遍历个数 for (int k = 1; k <= nums[i] && (j - k * weight[i]) >= 0; k++) { // 遍历个数 dp[j] = max(dp[j], dp[j - k * weight[i]] + k * value[i]); } } - // 打印一下dp数组 - for (int j = 0; j <= bagWeight; j++) { - cout << dp[j] << " "; - } - cout << endl; } + cout << dp[bagWeight] << endl; } -int main() { - test_multi_pack(); -} ``` -* 时间复杂度:O(m × n × k),m:物品种类个数,n背包容量,k单类物品数量 +时间复杂度:O(m × n × k),m:物品种类个数,n背包容量,k单类物品数量 从代码里可以看出是01背包里面在加一个for循环遍历一个每种商品的数量。 和01背包还是如出一辙的。 @@ -146,320 +165,14 @@ int main() { ### Java: -```Java -public void testMultiPack1(){ - // 版本一:改变物品数量为01背包格式 - List weight = new ArrayList<>(Arrays.asList(1, 3, 4)); - List value = new ArrayList<>(Arrays.asList(15, 20, 30)); - List nums = new ArrayList<>(Arrays.asList(2, 3, 2)); - int bagWeight = 10; - - for (int i = 0; i < nums.size(); i++) { - while (nums.get(i) > 1) { // 把物品展开为i - weight.add(weight.get(i)); - value.add(value.get(i)); - nums.set(i, nums.get(i) - 1); - } - } - - int[] dp = new int[bagWeight + 1]; - for(int i = 0; i < weight.size(); i++) { // 遍历物品 - for(int j = bagWeight; j >= weight.get(i); j--) { // 遍历背包容量 - dp[j] = Math.max(dp[j], dp[j - weight.get(i)] + value.get(i)); - } - System.out.println(Arrays.toString(dp)); - } -} - -public void testMultiPack2(){ - // 版本二:改变遍历个数 - int[] weight = new int[] {1, 3, 4}; - int[] value = new int[] {15, 20, 30}; - int[] nums = new int[] {2, 3, 2}; - int bagWeight = 10; - - int[] dp = new int[bagWeight + 1]; - for(int i = 0; i < weight.length; i++) { // 遍历物品 - for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量 - // 以上为01背包,然后加一个遍历个数 - for (int k = 1; k <= nums[i] && (j - k * weight[i]) >= 0; k++) { // 遍历个数 - dp[j] = Math.max(dp[j], dp[j - k * weight[i]] + k * value[i]); - } - System.out.println(Arrays.toString(dp)); - } - } -} -``` ### Python: -改变物品数量为01背包格式(无参版) -```python -def test_multi_pack(): - weight = [1, 3, 4] - value = [15, 20, 30] - nums = [2, 3, 2] - bagWeight = 10 - - # 将数量大于1的物品展开 - for i in range(len(nums)): - while nums[i] > 1: - weight.append(weight[i]) - value.append(value[i]) - nums[i] -= 1 - - dp = [0] * (bagWeight + 1) - for i in range(len(weight)): # 遍历物品 - for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - for j in range(bagWeight + 1): - print(dp[j], end=" ") - print() - - print(dp[bagWeight]) - - -test_multi_pack() - -``` - - -改变遍历个数(无参版) -```python -def test_multi_pack(): - weight = [1, 3, 4] - value = [15, 20, 30] - nums = [2, 3, 2] - bagWeight = 10 - dp = [0] * (bagWeight + 1) - - for i in range(len(weight)): # 遍历物品 - for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 - # 以上为01背包,然后加一个遍历个数 - for k in range(1, nums[i] + 1): # 遍历个数 - if j - k * weight[i] >= 0: - dp[j] = max(dp[j], dp[j - k * weight[i]] + k * value[i]) - - # 打印一下dp数组 - for j in range(bagWeight + 1): - print(dp[j], end=" ") - print() - - print(dp[bagWeight]) - - -test_multi_pack() - -``` - - -改变物品数量为01背包格式(有参版) -```python -def test_multi_pack(weight, value, nums, bagWeight): - # 将数量大于1的物品展开 - for i in range(len(nums)): - while nums[i] > 1: - weight.append(weight[i]) - value.append(value[i]) - nums[i] -= 1 - - dp = [0] * (bagWeight + 1) - for i in range(len(weight)): # 遍历物品 - for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - for j in range(bagWeight + 1): - print(dp[j], end=" ") - print() - - print(dp[bagWeight]) - - - - -if __name__ == "__main__": - weight = [1, 3, 4] - value = [15, 20, 30] - nums = [2, 3, 2] - bagWeight = 10 - test_multi_pack(weight, value, nums, bagWeight) -``` - - -改变遍历个数(有参版) -```python -def test_multi_pack(weight, value, nums, bagWeight): - dp = [0] * (bagWeight + 1) - - for i in range(len(weight)): # 遍历物品 - for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 - # 以上为01背包,然后加一个遍历个数 - for k in range(1, nums[i] + 1): # 遍历个数 - if j - k * weight[i] >= 0: - dp[j] = max(dp[j], dp[j - k * weight[i]] + k * value[i]) - - # 使用 join 函数打印 dp 数组 - print(' '.join(str(dp[j]) for j in range(bagWeight + 1))) - - print(dp[bagWeight]) - - - - - -if __name__ == "__main__": - weight = [1, 3, 4] - value = [15, 20, 30] - nums = [2, 3, 2] - bagWeight = 10 - test_multi_pack(weight, value, nums, bagWeight) - -``` ### Go: -```go -package theory - -import "log" - -// 多重背包可以化解为 01 背包 -func multiplePack(weight, value, nums []int, bagWeight int) int { - - for i := 0; i < len(nums); i++ { - for nums[i] > 1 { - weight = append(weight, weight[i]) - value = append(value, value[i]) - nums[i]-- - } - } - log.Println(weight) - log.Println(value) - - res := make([]int, bagWeight+1) - for i := 0; i < len(weight); i++ { - for j := bagWeight; j >= weight[i]; j-- { - res[j] = getMax(res[j], res[j-weight[i]]+value[i]) - } - log.Println(res) - } - - return res[bagWeight] -} -``` - -> 单元测试 - -```go -package theory - -import "testing" - -func Test_multiplePack(t *testing.T) { - type args struct { - weight []int - value []int - nums []int - bagWeight int - } - tests := []struct { - name string - args args - want int - }{ - { - name: "one", - args: args{ - weight: []int{1, 3, 4}, - value: []int{15, 20, 30}, - nums: []int{2, 3, 2}, - bagWeight: 10, - }, - want: 90, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := multiplePack(tt.args.weight, tt.args.value, tt.args.nums, tt.args.bagWeight); got != tt.want { - t.Errorf("multiplePack() = %v, want %v", got, tt.want) - } - }) - } -} -``` - -> 输出 - -``` -=== RUN Test_multiplePack -=== RUN Test_multiplePack/one -2022/03/02 21:09:05 [1 3 4 1 3 3 4] -2022/03/02 21:09:05 [15 20 30 15 20 20 30] -2022/03/02 21:09:05 [0 15 15 15 15 15 15 15 15 15 15] -2022/03/02 21:09:05 [0 15 15 20 35 35 35 35 35 35 35] -2022/03/02 21:09:05 [0 15 15 20 35 45 45 50 65 65 65] -2022/03/02 21:09:05 [0 15 30 30 35 50 60 60 65 80 80] -2022/03/02 21:09:05 [0 15 30 30 35 50 60 60 70 80 80] -2022/03/02 21:09:05 [0 15 30 30 35 50 60 60 70 80 80] -2022/03/02 21:09:05 [0 15 30 30 35 50 60 60 70 80 90] ---- PASS: Test_multiplePack (0.00s) - --- PASS: Test_multiplePack/one (0.00s) -PASS -``` ### TypeScript: -> 版本一(改变数据源): - -```typescript -function testMultiPack() { - const bagSize: number = 10; - const weightArr: number[] = [1, 3, 4], - valueArr: number[] = [15, 20, 30], - amountArr: number[] = [2, 3, 2]; - for (let i = 0, length = amountArr.length; i < length; i++) { - while (amountArr[i] > 1) { - weightArr.push(weightArr[i]); - valueArr.push(valueArr[i]); - amountArr[i]--; - } - } - const goodsNum: number = weightArr.length; - const dp: number[] = new Array(bagSize + 1).fill(0); - // 遍历物品 - for (let i = 0; i < goodsNum; i++) { - // 遍历背包容量 - for (let j = bagSize; j >= weightArr[i]; j--) { - dp[j] = Math.max(dp[j], dp[j - weightArr[i]] + valueArr[i]); - } - } - console.log(dp); -} -testMultiPack(); -``` - -> 版本二(改变遍历方式): - -```typescript -function testMultiPack() { - const bagSize: number = 10; - const weightArr: number[] = [1, 3, 4], - valueArr: number[] = [15, 20, 30], - amountArr: number[] = [2, 3, 2]; - const goodsNum: number = weightArr.length; - const dp: number[] = new Array(bagSize + 1).fill(0); - // 遍历物品 - for (let i = 0; i < goodsNum; i++) { - // 遍历物品个数 - for (let j = 0; j < amountArr[i]; j++) { - // 遍历背包容量 - for (let k = bagSize; k >= weightArr[i]; k--) { - dp[k] = Math.max(dp[k], dp[k - weightArr[i]] + valueArr[i]); - } - } - } - console.log(dp); -} -testMultiPack(); -``` diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index c6c856978a..92e944cc49 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -7,7 +7,7 @@ # 动态规划:完全背包理论基础 -本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problem.php?id=1052)去练习,题意是一样的。 +本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problempage.php?pid=1052)去练习,题意是一样的。 ## 算法公开课 From f520b1dc172dafbc4361c48e3df22b8da399a2c3 Mon Sep 17 00:00:00 2001 From: Wang ChaoLan <68094392+Junior-W@users.noreply.github.com> Date: Thu, 9 Nov 2023 17:20:12 +0800 Subject: [PATCH 0793/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20kama55.=E5=8F=B3?= =?UTF-8?q?=E6=97=8B=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\255\227\347\254\246\344\270\262.md" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index afd29d7376..f384193001 100644 --- "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -145,8 +145,70 @@ int main() { ## 其他语言版本 ### Java: +```Java +// 版本一 +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n = Integer.parseInt(in.nextLine()); + String s = in.nextLine(); + + int len = s.length(); //获取字符串长度 + char[] chars = s.toCharArray(); + reverseString(chars, 0, len - 1); //反转整个字符串 + reverseString(chars, 0, n - 1); //反转前一段字符串,此时的字符串首尾尾是0,n - 1 + reverseString(chars, n, len - 1); //反转后一段字符串,此时的字符串首尾尾是n,len - 1 + + System.out.println(chars); + + } + + public static void reverseString(char[] ch, int start, int end) { + //异或法反转字符串,参照题目 344.反转字符串的解释 + while (start < end) { + ch[start] ^= ch[end]; + ch[end] ^= ch[start]; + ch[start] ^= ch[end]; + start++; + end--; + } + } +} + + +// 版本二 +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n = Integer.parseInt(in.nextLine()); + String s = in.nextLine(); + int len = s.length(); //获取字符串长度 + char[] chars = s.toCharArray(); + reverseString(chars, 0, len - n - 1); //反转前一段字符串,此时的字符串首尾是0,len - n - 1 + reverseString(chars, len - n, len - 1); //反转后一段字符串,此时的字符串首尾是len - n,len - 1 + reverseString(chars, 0, len - 1); //反转整个字符串 + System.out.println(chars); + + } + + public static void reverseString(char[] ch, int start, int end) { + //异或法反转字符串,参照题目 344.反转字符串的解释 + while (start < end) { + ch[start] ^= ch[end]; + ch[end] ^= ch[start]; + ch[start] ^= ch[end]; + start++; + end--; + } + } +} +``` ### Python: From 139a4db4859f5b251d8df1e40d92c30587f44d28 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 10 Nov 2023 17:52:08 +0800 Subject: [PATCH 0794/1533] Update --- ...24\345\217\221\346\265\201\347\250\213.md" | 60 ++++----- ...CM\346\250\241\345\274\217\357\274\237.md" | 114 +++++------------- 2 files changed, 58 insertions(+), 116 deletions(-) diff --git "a/problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" "b/problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" index 9616958dc5..ed526897e5 100644 --- "a/problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" +++ "b/problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" @@ -1,5 +1,7 @@ # 揭秘互联网大厂研发流程 +[B站:揭秘互联网大厂研发流程](https://www.bilibili.com/video/BV1KR4y1H7ST) + 很多录友会很好奇这么个事: * 大厂的研发流程应该是什么样的呢 @@ -8,17 +10,6 @@ 这次给大家介绍一波大厂的研发流程,让大家明明白白。 -同时我已经录制了视频,昨晚已经在B站首发了。 - -视频里讲的更清楚一点,因为我是没有草稿,先录的视频,然后在根据视频写的文章,写文章的时候,发现我视频里讲的太多了,码字实在是码不过来,所以这篇文字稿中每一个研发环节的解释稍稍精简了一下。 - -如果详细了解研发流程就直接看视频吧,**别告诉卡哥,你还没关注 「代码随想录」的B站,还不赶紧关注一波[机智]**。 - -重要重要重要: **要给三连呀!** - -[B站:揭秘互联网大厂研发流程](https://www.bilibili.com/video/BV1KR4y1H7ST) - - 其实对于几十人或者上百人一起开发一个项目的话,一个规范的研发流程是很重要的。 有的同学可能想,哪有这么多流程啊,就是写完代码,跑一下,没问题,然后就上线了。 @@ -33,15 +24,15 @@ 看需求文档,我们要根据需求文档来确定我们究竟要做什么。 -一些同学可能感觉 为什么还要用一个需求文档呢,你就告诉我做啥我就做啥不就完事了。 +一些同学可能感觉 为什么还要用一个需求文档呢,你就告诉我做啥我就做啥不就完事了? -其实需求文档一方面是倒逼产品经理去系统性的思考这个需求究竟有哪些功能,用来满足哪些用户的需求。 +需求文档一方面是**倒逼产品经理去系统性的思考这个需求究竟有哪些功能**,用来满足哪些用户的需求。 -另一方面,是保证我们在研发的时候,研发出来的功能是满足需求文档里所描述的。 +另一方面是**保证我们在研发的时候,研发出来的功能是满足需求文档里所描述的**。 如果是口头对接的话,很有可能就是你做出来的东西,产品经理看完感觉:这和我说的需求不一样啊!!这和我想的不一样啊!! -这样就是两个人相互甩锅,那这究竟是谁的锅呢。都没有一个证据,对吧。 +这样就是两个人相互“甩锅”,那这究竟是谁的锅呢。都没有一个证据,对吧。 所以说,有一个需求文档很重要。 @@ -67,13 +58,13 @@ 你说你一周的时间就能把它开发完,那为什么是一周呢,为什么不是两天,为什么不是两周呢。 -其实 和上面的领导汇报你的工作的时候 都要把自己的工作进行量化。 +其实 和上面的领导汇报你的工作的时候 **都要把自己的工作进行量化**。 那么这个功能有哪些难点,我们要克服这个难点,所需要花费的时间,都要有一个大体的量化。 -这样才能量化我们自己的工作,**领导其实不知道你的工作是简单 还是困难, 领导只在意最终结果**,所以你要展现给领导你的工作是有难度的是有挑战的。 +这样才能量化我们自己的工作,**领导其实不知道你的工作是简单 还是困难, 领导只在意最终结果**,所以你需要展现给领导你的工作是有难度的是有挑战的。 -而且这些也是我们年底用来晋升或者评职称的素材。 +而且**这些也是我们年底用来晋升或者评职称的素材**。 如果这些东西你自己都不在乎的话,谁还会帮你在乎呢。 @@ -108,7 +99,7 @@ 所以只要有交互,就要确定协议的数据格式。 -定协议要考虑到兼容,要考虑易于维护。 +**定协议要考虑到兼容,要考虑易于维护**。 ## 6.设计数据结构和算法 @@ -122,13 +113,19 @@ 为什么会这样呢? 一个很简单的例子,互联网研发讲究其实就是要快,例如一个功能2天就要开发完,如果算法都要自己去写的话,等都写完了,花都谢了。 +最关键的是,**你实现的算法 极大概率没有现成的算法接口安全性高**。 + +**开发中要学会才在巨人的肩膀上**。 + ## 7.预估一下容量 特别是后端开发,要估计出 我们自己模块大体需要多大磁盘,多大内存,多大带宽,多少核CPU。 这也是没有做过研发工作的同学经常忽略的,**因为大家好像默认 磁盘、内存、带宽、cpu是无穷的**。 -其实我们在设计一个模块的时候,这些都要评估的,不能模块一上线,把机器直接打爆了,例如 直接把带宽打满了,不仅影响自己的模块功能,还影响了机器上其他模块的运行。 +其实我们在设计一个模块的时候,这些都要评估的,不能模块一上线,把机器直接打爆了。 + +例如 直接把带宽打满了,不仅影响自己的模块功能,还影响了机器上其他模块的运行。 ## 8.考虑部署 @@ -139,7 +136,9 @@ 还有就是要弹性可伸缩,即我们的模块可不可以直接 部署多台机器来提高承载能力。 -如果用户量突然上来了,或者流量突然上来了,可以通过快速部署多台机器来抗住流量。而不是模块只能在单机上跑,多部署几台就发生各种问题。 +如果用户量突然上来了,或者流量突然上来了,可以通过快速部署多台机器来抗住流量。 + +而不是模块只能在单机上跑,多部署几台就发生各种问题。 **这才能说明是有足够强的风险意识的**。 @@ -170,19 +169,19 @@ 这里就有很多沟通工作了,因为其他同学可能手头有自己的活,那么就要协调一个时间来一起测试。 -这一步也是很费时间的,其费时关键是要等,要等其他同学有空和你联调,而且往往不是联调一次就能解决问题的。 +这一步也是很费时间的,**其费时关键是要等,要等其他同学有空和你联调或者是别人等你**,而且往往不是联调一次就能解决问题的。 所以 在评估开发时间的时候 也要考虑到联调的时间。 -这也是大厂研发效率低的地方,但上百人开发的项目,这种沟通上消耗也是在所难免的。 +这也是大厂研发效率低的地方,但上百人开发的项目,**这种沟通上消耗也是在所难免的**。 ## 13.交给测试 自己的代码,自己测 一般都测不出什么问题,需要交给测试同学来给你测一测。 -这里如果测试同学测出问题,你就要判断确实有问题还是 测试方式不对,如果有问题就要修改,在提给测试,反反复复这么几轮,直到测试同学测试没问题了。 +这里如果测试同学测出问题,你就要判断确实有问题还是 测试方式不对,如果有问题就要修改,再提给测试,反反复复这么几轮,直到测试同学测试没问题了。 -这个过程也是累心的。 +**这个过程也是累心的**。 ## 14.code review @@ -202,12 +201,15 @@ 其实合入主干是很重要的,经常是自己的代码没问题,但合入主干之后就有问题了。 -一般就是合入主干的时候有冲突,例如你从主干拉出一个分支,另一个同学从主干拉出一个分支,而且两个分支修改了同一个模块,如果另一个同学提前合入主干,你在合入主干的时候就会有代码冲突。 +一般就是合入主干的时候有冲突,例如你从主干拉出一个分支,另一个同学从主干拉出一个分支,而且两个分支修改了同一个模块,如果另一个同学提前合入主干,你再合入主干的时候就会有代码冲突。 在解决代码冲突的时候 就会修改别人的代码,这个过程很容易产生新的bug。 + **一般合入主干之后,测试同学还要重新跑一个全量测试,才能放心发布**。 +如果跑全量测试没有问题的话,才会松一口气(懂的人都懂)。 + ## 16.发布 最后一步就是发布了。 @@ -218,15 +220,15 @@ 用大白话来讲,就是把 本地的代码或者某台机器的代码,编译成可执行文件,然后更新到 线上的服务器(一个独立的集群,专门处理线上的流量)并运行起来。 -上线是最重要的一步了,也很容易出问题,因为一个大型项目,其上线的过程都非常复杂(要更新上百台机器的集群),而且要考虑线上新版和旧版本的兼容问题。 +上线是最重要的一步了,也很容易出问题,因为一个大型项目,其上线的过程都非常复杂(要更新上百台机器的集群),而且**要考虑线上新版和旧版本的兼容问题**。 这也是为什么大厂项目都选择深夜上线,**因为深夜在线用户最少,如果出问题,影响的用户会比较少,可以快速修复**。 所以大家经常看到 某大厂程序员深夜上线发布版本之类的。 -# 总结 +## 总结 -好了,整整讲了十六个步骤!​把大厂研发流程中 具体都有哪一步,为什么要这么做,都分析的很清楚了。 +好了,整整讲了十六个步骤!把大厂研发流程中 具体都有哪一步,为什么要这么做,都分析的很清楚了。 不过在大厂也不是所有部门都按照这个流程来的,每个部门都有自己玩法,各个部门也不太统一。 diff --git "a/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" "b/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" index 0012f88e67..313264fba2 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" @@ -1,120 +1,60 @@ # 什么是核心代码模式,什么又是ACM模式? -现在很多企业都在牛客上进行面试,**很多录友和我反馈说搞不懂牛客上输入代码的ACM模式**。 +很多录友刷了不少题了,到现在也没有搞清楚什么是 ACM模式,什么是核心代码模式。 -什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。 +平时大家在力扣上刷题,就是 核心代码模式,即给你一个函数,直接写函数实现,例如这样: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193631.png) -**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 - +而ACM模式,是程序头文件,main函数,数据的输入输出都要自己处理,例如这样: -而力扣上是核心代码模式,就是把要处理的数据都已经放入容器里,可以直接写逻辑,例如这样: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193743.png) -```CPP -class Solution { -public: - int minimumTotal(vector>& triangle) { +大家可以发现 右边代码框什么都没有,程序从头到尾都需要自己实现,本题如果写完代码是这样的: (细心的录友可以发现和力扣上刷题是不一样的) - } -}; -``` +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193931.png) -**如果大家从一开始学习算法就一直在力扣上的话,突然切到牛客网上的ACM模式会很不适应**。 -因为我上学的时候就搞ACM,在POJ(北大的在线判题系统)和ZOJ(浙大的在线判题系统)上刷过6、7百道题目了,对这种ACM模式就很熟悉。 +**如果大家从一开始学习算法就一直在力扣上的话,突然切到ACM模式会非常不适应**。 -接下来我给大家讲一下ACM模式应该如何写。 +知识星球里也有很多录友,因为不熟悉ACM模式在面试的过程中吃了不少亏。 -这里我拿牛客上 腾讯2020校园招聘-后台 的面试题目来举一个例子,本题我不讲解题思路,只是拿本题为例讲解ACM输入输出格式。 -题目描述: +
-由于业绩优秀,公司给小Q放了 n 天的假,身为工作狂的小Q打算在在假期中工作、锻炼或者休息。他有个奇怪的习惯:不会连续两天工作或锻炼。只有当公司营业时,小Q才能去工作,只有当健身房营业时,小Q才能去健身,小Q一天只能干一件事。给出假期中公司,健身房的营业情况,求小Q最少需要休息几天。 +
-输入描述: -第一行一个整数 表示放假天数 -第二行 n 个数 每个数为0或1,第 i 个数表示公司在第 i 天是否营业 -第三行 n 个数 每个数为0或1,第 i 个数表示健身房在第 i 天是否营业 -(1为营业 0为不营业) +
-输出描述: -一个整数,表示小Q休息的最少天数 +
-示例一: -输入: -4 -1 1 0 0 -0 1 1 0 +
-输出: -2 +## 面试究竟怎么考? +笔试的话,基本都是 ACM模式。 -这道题如果要是力扣上的核心代码模式,OJ应该直接给出如下代码: +面试的话,看情况,有的面试官会让你写一个函数实现就可以,此时就是核心代码模式。 -```CPP -class Solution { -public: - int getDays(vector& work, vector& gym) { - // 处理逻辑 - } -}; -``` +有的面试官会 给你一个编辑器,让你写完代码运行一下看看输出结果,此时就是ACM模式。 -以上代码中我们直接写核心逻辑就行了,work数组,gym数组都是填好的,直接拿来用就行,处理完之后 return 结果就完事了。 +有的录友想,那我平时在力扣刷题,写的是核心代码模式,我也可以运行,为啥一定要用ACM模式。 -那么看看ACM模式我们要怎么写呢。 +**大家在力扣刷题刷多了,已经忘了程序是如何运行的了**,力扣上写的代码,脱离力扣环境,那个函数,你怎么运行呢? -ACM模式要求写出来的代码是直接可以本地运行的,所以我们需要自己写include哪些库函数,构造输入用例,构造输出用例。 +想让程序在本地运行起来,是不是需要补充 库函数,是不是要补充main函数,是不是要补充数据的输入和输出。 那不就是ACM模式了。 -拿本题来说,为了让代码可以运行,需要include这些库函数: +综合来看,** ACM模式更考察综合代码能力, 核心代码模式是更聚焦算法的实现逻辑**。 -```CPP -#include -#include -using namespace std; -``` +## 去哪练习ACM模式? +这里给大家推荐卡码网: [kamacoder.com](https://kamacoder.com/) -然后开始写主函数,来处理输入用例了,示例一 是一个完整的测试用例,一般我们测了一个用例还要测第二个用例,所以用:while(cin>>n) 来输入数据。 +你只要能把卡码网首页的25道题目 都刷了 ,就把所有的ACM输入输出方式都练习到位了,不会有任何盲区。 -这里输入的n就是天数,得到天数之后,就可以来构造work数组和gym数组了。 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109195056.png) -此时就已经完成了输入用例构建,然后就是处理逻辑了,最后返回结果。 +而且你不用担心,题目难度太大,直接给自己劝退,**卡码网的前25道题目都是我精心制作的,难度也是循序渐进的**,大家去刷一下就知道了。 -完整代码如下: -```CPP -#include -#include -using namespace std; -int main() { - int n; - while (cin >> n) { - vector gym(n); - vector work(n); - for (int i = 0; i < n; i++) cin >> work[i]; - for (int i = 0; i < n; i++) cin >> gym[i]; - int result = 0; - - // 处理逻辑 - - cout << result << endl; - } - return 0; -} -``` - -可以看出ACM模式要比核心代码模式多写不少代码,相对来说ACM模式更锻炼代码能力,而核心代码模式是把侧重点完全放在算法逻辑上。 - -**国内企业现在很多都用牛客来进行面试,所以这种ACM模式大家还有必要熟悉一下**,以免面试的时候因为输入输出搞不懂而错失offer。 - -如果大家有精力的话,也可以去POJ上去刷刷题,POJ是ACM选手首选OJ,输入模式也是ACM模式。 - - - - - ------------------------ -
From 09835e74442c5dfdd118e13ffc43c06da5e419d7 Mon Sep 17 00:00:00 2001 From: to_Geek Date: Fri, 10 Nov 2023 22:00:29 +0800 Subject: [PATCH 0795/1533] =?UTF-8?q?=F0=9F=93=9D=20Update=200383.?= =?UTF-8?q?=E8=B5=8E=E9=87=91=E4=BF=A1.md=20with=20python3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0383.\350\265\216\351\207\221\344\277\241.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index 3de48ce3da..b800c2320d 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -214,6 +214,19 @@ class Solution: return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) ``` +(版本六)使用count(简单易懂) + +```python3 +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + for char in ransomNote: + if char in magazine and ransomNote.count(char) <= magazine.count(char): + continue + else: + return False + return True +``` + ### Go: ```go From 8945fd98094efe154431cb51a7edf01e3b50af60 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sat, 11 Nov 2023 09:12:17 +0800 Subject: [PATCH 0796/1533] =?UTF-8?q?Update.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\275\222\351\201\215\345\216\206.md" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 730b18ac6d..6dad6e56de 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -565,6 +565,60 @@ impl Solution { } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; + +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + res.Add(cur.val); + Traversal(cur.left, res); + Traversal(cur.right, res); +} +``` +```C# +// 中序遍历 +public IList InorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + Traversal(cur.left, res); + res.Add(cur.val); + Traversal(cur.right, res); +} +``` +```C# +// 后序遍历 +public IList PostorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + Traversal(cur.left, res); + Traversal(cur.right, res); + res.Add(cur.val); +} +``` +

From 8435164338531108f6919bd64185db52a5eebd4f Mon Sep 17 00:00:00 2001 From: way <1012370241@qq.com> Date: Sat, 11 Nov 2023 11:16:33 +0800 Subject: [PATCH 0797/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0kama54.=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E5=AD=97=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\277\346\215\242\346\225\260\345\255\227.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 80c87951aa..d68bbc2c98 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -143,6 +143,23 @@ for (int i = 0; i < a.size(); i++) { ### Java: +```java +import java.util.Scanner; + +class Main { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String s = in.nextLine(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + if (Character.isDigit(s.charAt(i))) { + sb.append("number"); + }else sb.append(s.charAt(i)); + } + System.out.println(sb); + } +} +``` ### Go: From 5d9d37f01d59b2fa95001018fb24e4b74e2a04f9 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sun, 12 Nov 2023 09:15:59 +0800 Subject: [PATCH 0798/1533] =?UTF-8?q?Update.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86=EF=BC=8CC#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\344\273\243\351\201\215\345\216\206.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 69007995f0..e39709b882 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -695,6 +695,67 @@ impl Solution { } } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var st = new Stack(); + var res = new List(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Pop(); + res.Add(node.val); + if (node.right != null) + st.Push(node.right); + if (node.left != null) + st.Push(node.left); + } + return res; +} + +// 中序遍历 +public IList InorderTraversal(TreeNode root) +{ + var st = new Stack(); + var res = new List(); + var cur = root; + while (st.Count != 0 || cur != null) + { + if (cur != null) + { + st.Push(cur); + cur = cur.left; + } + else + { + cur = st.Pop(); + res.Add(cur.val); + cur = cur.right; + } + } + return res; +} +// 后序遍历 +public IList PostorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var cur = st.Pop(); + res.Add(cur.val); + if (cur.left != null) st.Push(cur.left); + if (cur.right != null) st.Push(cur.right); + } + res.Reverse(0, res.Count()); + return res; +} +```

From 8e463e0b64b02656c41202df52efe249319034e7 Mon Sep 17 00:00:00 2001 From: yihug <31957705+DanielChungYi@users.noreply.github.com> Date: Sun, 12 Nov 2023 12:15:44 +0900 Subject: [PATCH 0799/1533] =?UTF-8?q?[typo=20fix]:=20Update=20kama54.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 80c87951aa..6147f2d1c6 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -46,7 +46,7 @@ 从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素整体向后移动。 -**其实很多数组填充类的问题,其做饭都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** +**其实很多数组填充类的问题,其做法都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** 这么做有两个好处: From ce2ffd073c20fa72c07df54b4566d285172276c4 Mon Sep 17 00:00:00 2001 From: to_Geek Date: Sun, 12 Nov 2023 23:44:17 +0800 Subject: [PATCH 0800/1533] =?UTF-8?q?=F0=9F=93=9D=20Update=200344.?= =?UTF-8?q?=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20with=20pytho?= =?UTF-8?q?n3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...275\254\345\255\227\347\254\246\344\270\262.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 8a4fed4574..44184c53bc 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -250,6 +250,20 @@ class Solution: s[:] = [s[i] for i in range(len(s) - 1, -1, -1)] ``` + +(版本七) 使用reverse() + +```python +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + # 原地反转,无返回值 + s.reverse() + +``` + ### Go: ```Go From 436bd5a1a25d317824af0405bba0d023a7619a16 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Mon, 13 Nov 2023 09:21:04 +0800 Subject: [PATCH 0801/1533] =?UTF-8?q?Update.=E7=BB=9F=E4=B8=80=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\350\277\255\344\273\243\346\263\225.md" | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 8089af64ec..ad79424da8 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -741,6 +741,99 @@ impl Solution{ } } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Peek(); + if (node == null) + { + st.Pop(); + node = st.Peek(); + st.Pop(); + res.Add(node.val); + } + else + { + st.Pop(); + if (node.right != null) st.Push(node.right); + if (node.left != null) st.Push(node.left); + st.Push(node); + st.Push(null); + } + } + return res; +} +``` +```C# +// 中序遍历 +public IList InorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Peek(); + if (node == null) + { + st.Pop(); + node = st.Peek(); + st.Pop(); + res.Add(node.val); + } + else + { + st.Pop(); + if (node.right != null) st.Push(node.right); + st.Push(node); + st.Push(null); + if (node.left != null) st.Push(node.left); + } + } + return res; +} +``` + +```C# +// 后序遍历 +public IList PostorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + if (root == null) return res; + st.Push(root); + while (st.Count != 0) + { + var node = st.Peek(); + if (node == null) + { + st.Pop(); + node = st.Peek(); + st.Pop(); + res.Add(node.val); + } + else + { + st.Pop(); + if (node.left != null) st.Push(node.left); + if (node.right != null) st.Push(node.right); + st.Push(node); + st.Push(null); + } + } + res.Reverse(0, res.Count); + return res; +} +```

From aad5029a1cb33461905df368c951bceeb2fbc797 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 14 Nov 2023 09:11:17 +0800 Subject: [PATCH 0802/1533] =?UTF-8?q?Update=20102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 51d030e784..e977279c56 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -462,6 +462,32 @@ impl Solution { } } ``` +### C# +```C# +public IList> LevelOrder(TreeNode root) +{ + var res = new List>(); + var que = new Queue(); + if (root == null) return res; + que.Enqueue(root); + while (que.Count != 0) + { + var size = que.Count; + var vec = new List(); + for (int i = 0; i < size; i++) + { + var cur = que.Dequeue(); + vec.Add(cur.val); + if (cur.left != null) que.Enqueue(cur.left); + if (cur.right != null) que.Enqueue(cur.right); + } + res.Add(vec); + + + } + return res; +} +``` **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** @@ -798,6 +824,31 @@ impl Solution { } } ``` +### C# +```C# +public IList> LevelOrderBottom(TreeNode root) +{ + var res = new List>(); + var que = new Queue(); + if (root == null) return res; + que.Enqueue(root); + while (que.Count != 0) + { + var size = que.Count; + var vec = new List(); + for (int i = 0; i < size; i++) + { + var cur = que.Dequeue(); + vec.Add(cur.val); + if (cur.left != null) que.Enqueue(cur.left); + if (cur.right != null) que.Enqueue(cur.right); + } + res.Add(vec); + } + res.Reverse(); + return res; +} +``` ## 199.二叉树的右视图 From bc988fb846ab845e61a748aad6e9ec79f0ab2339 Mon Sep 17 00:00:00 2001 From: changknn Date: Tue, 14 Nov 2023 09:58:16 +0800 Subject: [PATCH 0803/1533] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c1296ed3db..143a8ba205 100644 --- a/README.md +++ b/README.md @@ -397,7 +397,7 @@ * [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md) * [图论:827.最大人工岛](./problems/0827.最大人工岛.md) * [图论:127. 单词接龙](./problems/0127.单词接龙.md) -* [图论:841.钥匙和房间](./problems/841.钥匙和房间) +* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md) * [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md) * [图论:并查集理论基础](./problems/) * [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md) From 08a77b9e060985cbd25205144b8e86d57d8b61d4 Mon Sep 17 00:00:00 2001 From: changknn Date: Tue, 14 Nov 2023 11:00:01 +0800 Subject: [PATCH 0804/1533] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 143a8ba205..15b10bfa7a 100644 --- a/README.md +++ b/README.md @@ -399,7 +399,7 @@ * [图论:127. 单词接龙](./problems/0127.单词接龙.md) * [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md) * [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md) -* [图论:并查集理论基础](./problems/) +* [图论:并查集理论基础](./problems/图论并查集理论基础.md) * [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md) * [图论:684.冗余连接](./problems/0684.冗余连接.md) * [图论:685.冗余连接II](./problems/0685.冗余连接II.md) From 103256a36a20fa48514f3d6cb2516fecda681c68 Mon Sep 17 00:00:00 2001 From: xin Date: Wed, 15 Nov 2023 08:34:47 +0800 Subject: [PATCH 0805/1533] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B00104.?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=E9=87=8C=E7=9A=84=20n=20=E5=8F=89=E6=A0=91=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E6=B7=B1=E5=BA=A6=E7=9A=84=20ts=20=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E5=8E=9F=E6=9C=AC=E4=BB=A3=E7=A0=81=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20leetcode=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 0504437521..b115762e45 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -738,26 +738,36 @@ function maxDepth(root: TreeNode | null): number { ```typescript // 后续遍历(自下而上) -function maxDepth(root: TreeNode | null): number { - if (root === null) return 0; - return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; -}; +function maxDepth(root: Node | null): number { + if (root === null) return 0 + let depth = 0 + for (let i = 0; i < root.children.length; i++) { + depth = Math.max(depth, maxDepth(root.children[i])) + } + return depth + 1 +} // 前序遍历(自上而下) -function maxDepth(root: TreeNode | null): number { - function recur(node: TreeNode | null, count: number) { - if (node === null) { - resMax = resMax > count ? resMax : count; - return; +function maxDepth(root: Node | null): number { + if (root === null) return 0 + + let depth: number = 0 + const queue: Array = [] + queue.push(root) + + while (queue.length > 0) { + let len = queue.length + depth++ + for (let i = 0; i < len; i++) { + // 当前层遍历 + let curNode: Node | null = queue.shift()! + for (let j = 0; j < curNode.children.length; j++) { + if (curNode.children[j]) queue.push(curNode.children[j]) + } } - recur(node.left, count + 1); - recur(node.right, count + 1); } - let resMax: number = 0; - let count: number = 0; - recur(root, count); - return resMax; -}; + return depth +} ``` From dba4e9d00ec28ea484715bd5fa341ae8aed9ed98 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 15 Nov 2023 08:57:19 +0800 Subject: [PATCH 0806/1533] =?UTF-8?q?Update226.=E7=BF=BB=E8=BD=AC=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E7=AE=80=E5=8C=96C#=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\344\272\214\345\217\211\346\240\221.md" | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 1151778364..8691953a3e 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -1018,25 +1018,13 @@ public class Solution { ```csharp //迭代 public class Solution { - public TreeNode InvertTree(TreeNode root) { - if (root == null) return null; - Stack stack=new Stack(); - stack.Push(root); - while(stack.Count>0) - { - TreeNode node = stack.Pop(); - swap(node); - if(node.right!=null) stack.Push(node.right); - if(node.left!=null) stack.Push(node.left); - } - return root; - } - - public void swap(TreeNode node) { - TreeNode temp = node.left; - node.left = node.right; - node.right = temp; - } +public TreeNode InvertTree(TreeNode root) { + if(root == null) return root; + (root.left,root.right) = (root.right, root.left); + InvertTree(root.left); + InvertTree(root.right); + return root; +} } ``` From b51573efa69a3e4fead79bbcba62c77f7ecedb09 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 15 Nov 2023 09:16:04 +0800 Subject: [PATCH 0807/1533] =?UTF-8?q?Update101.=E5=AF=B9=E7=A7=B0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\344\272\214\345\217\211\346\240\221.md" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 36b39740d0..1b777dd527 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -897,6 +897,53 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public bool IsSymmetric(TreeNode root) +{ + if (root == null) return true; + return Compare(root.left, root.right); +} +public bool Compare(TreeNode left, TreeNode right) +{ + if(left == null && right != null) return false; + else if(left != null && right == null ) return false; + else if(left == null && right == null) return true; + else if(left.val != right.val) return false; + + var outside = Compare(left.left, right.right); + var inside = Compare(left.right, right.left); + + return outside&&inside; +} +``` +``` C# +// 迭代法 +public bool IsSymmetric(TreeNode root) +{ + if (root == null) return true; + var st = new Stack(); + st.Push(root.left); + st.Push(root.right); + while (st.Count != 0) + { + var left = st.Pop(); + var right = st.Pop(); + if (left == null && right == null) + continue; + + if ((left == null || right == null || (left.val != right.val))) + return false; + + st.Push(left.left); + st.Push(right.right); + st.Push(left.right); + st.Push(right.left); + } + return true; +} +```

From 6df2c5ec5cc4659d5d129b8e71e3c66fbb156784 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 15 Nov 2023 14:20:37 +0800 Subject: [PATCH 0808/1533] Update --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" | 2 +- ...\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 09fb97a9d8..413d984c08 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -264,7 +264,7 @@ int main() { ``` -本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的,代码如下: +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的,代码如下: ```CPP diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 674166a199..5630bc99d1 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -183,7 +183,7 @@ int main() { ``` -本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problem.php?id=1046)去练习,题意是一样的,代码如下: +本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的,代码如下: ```CPP // 一维dp数组实现 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 92e944cc49..efc56c50ac 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -164,7 +164,7 @@ int main() { ``` -本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problem.php?id=1052)去练习,题意是一样的,C++代码如下: +本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problempage.php?pid=1052)去练习,题意是一样的,C++代码如下: ```cpp #include From 3ffca338cafa5ba72d81048d762211488f7d19f7 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Thu, 16 Nov 2023 09:12:06 +0800 Subject: [PATCH 0809/1533] =?UTF-8?q?Update104.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 0504437521..ab3ce52eff 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -1022,6 +1022,61 @@ impl Solution { max_depth } ``` +### C# +```C# +// 递归法 +public int MaxDepth(TreeNode root) { + if(root == null) return 0; + + int leftDepth = MaxDepth(root.left); + int rightDepth = MaxDepth(root.right); + + return 1 + Math.Max(leftDepth, rightDepth); +} +``` +```C# +// 前序遍历 +int result = 0; +public int MaxDepth(TreeNode root) +{ + if (root == null) return result; + GetDepth(root, 1); + return result; +} +public void GetDepth(TreeNode root, int depth) +{ + result = depth > result ? depth : result; + if (root.left == null && root.right == null) return; + + if (root.left != null) + GetDepth(root.left, depth + 1); + if (root.right != null) + GetDepth(root.right, depth + 1); + return; +} +``` +```C# +// 迭代法 +public int MaxDepth(TreeNode root) +{ + int depth = 0; + Queue que = new(); + if (root == null) return depth; + que.Enqueue(root); + while (que.Count != 0) + { + int size = que.Count; + depth++; + for (int i = 0; i < size; i++) + { + var node = que.Dequeue(); + if (node.left != null) que.Enqueue(node.left); + if (node.right != null) que.Enqueue(node.right); + } + } + return depth; +} +```

From bb66452ece363f599b89761269e7311a4654404a Mon Sep 17 00:00:00 2001 From: gdstzmy <79707886+gdstzmy@users.noreply.github.com> Date: Thu, 16 Nov 2023 15:04:46 +0800 Subject: [PATCH 0810/1533] =?UTF-8?q?Update=20kama54.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index d68bbc2c98..a8c557929b 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -162,6 +162,27 @@ class Main { ``` ### Go: +````go +package main + +import "fmt" + +func main(){ + var strByte []byte + + fmt.Scanln(&strByte) + + for i := 0; i < len(strByte); i++{ + if strByte[i] <= '9' && strByte[i] >= '0' { + inserElement := []byte{'n','u','m','b','e','r'} + strByte = append(strByte[:i], append(inserElement, strByte[i+1:]...)...) + i = i + len(inserElement) -1 + } + } + + fmt.Printf(string(strByte)) +} +```` From 972ca2de45f2a85baa055122944cb9c6aa4bfdcc Mon Sep 17 00:00:00 2001 From: gdstzmy <79707886+gdstzmy@users.noreply.github.com> Date: Thu, 16 Nov 2023 16:46:36 +0800 Subject: [PATCH 0811/1533] =?UTF-8?q?Update=20kama55.=E5=8F=B3=E6=97=8B?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\255\227\347\254\246\344\270\262.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index f384193001..17c97eaa3c 100644 --- "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -214,6 +214,34 @@ public class Main { ### Go: +```go +package main +import "fmt" + +func reverse (strByte []byte, l, r int){ + for l < r { + strByte[l], strByte[r] = strByte[r], strByte[l] + l++ + r-- + } +} + + +func main(){ + var str string + var target int + + fmt.Scanln(&target) + fmt.Scanln(&str) + strByte := []byte(str) + + reverse(strByte, 0, len(strByte) - 1) + reverse(strByte, 0, target - 1) + reverse(strByte, target, len(strByte) - 1) + + fmt.Printf(string(strByte)) +} +``` ### JavaScript: From cfc78c3c57d5d9a8c79df710c36979700fd43064 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Fri, 17 Nov 2023 19:33:16 +0800 Subject: [PATCH 0812/1533] =?UTF-8?q?Update111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\260\217\346\267\261\345\272\246.md" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index c4e55a0740..b27df4a626 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -708,6 +708,49 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public int MinDepth(TreeNode root) +{ + if (root == null) return 0; + int left = MinDepth(root.left); + int right = MinDepth(root.right); + if (root.left == null && root.right != null) + return 1+right; + else if(root.left!=null && root.right == null) + return 1+left; + + int res = 1 + Math.Min(left, right); + return res; +} +``` +```C# +// 迭代 +public int MinDepth(TreeNode root) +{ + if (root == null) return 0; + int depth = 0; + var que = new Queue(); + que.Enqueue(root); + while (que.Count > 0) + { + int size = que.Count; + depth++; + for (int i = 0; i < size; i++) + { + var node = que.Dequeue(); + if (node.left != null) + que.Enqueue(node.left); + if (node.right != null) + que.Enqueue(node.right); + if (node.left == null && node.right == null) + return depth; + } + } + return depth; +} +```

From 0843f2282dbcc9597bf156870149632d80def198 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sat, 18 Nov 2023 09:48:26 +0800 Subject: [PATCH 0813/1533] =?UTF-8?q?Update=20222.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E9=80=92=E5=BD=92?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\347\202\271\344\270\252\346\225\260.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index d54f9b85d2..ef6e88aaae 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -867,6 +867,31 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public int CountNodes(TreeNode root) +{ + if (root == null) return 0; + var left = root.left; + var right = root.right; + int leftDepth = 0, rightDepth = 0; + while (left != null) + { + left = left.left; + leftDepth++; + } + while (right != null) + { + right = right.right; + rightDepth++; + } + if (leftDepth == rightDepth) + return (int)Math.Pow(2, leftDepth+1) - 1; + return CountNodes(root.left) + CountNodes(root.right) + 1; + +} +```

From eb0504e5305ae37f3f6f569ebde423206ea11b0f Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sun, 19 Nov 2023 08:49:19 +0800 Subject: [PATCH 0814/1533] =?UTF-8?q?Update=20110.=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\344\272\214\345\217\211\346\240\221.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index c7df9c8f19..41669bff7f 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -908,6 +908,31 @@ impl Solution { } } ``` +### C# +```C# +public bool IsBalanced(TreeNode root) +{ + return GetHeight(root) == -1 ? false : true; +} +public int GetHeight(TreeNode root) +{ + if (root == null) return 0; + int left = GetHeight(root.left); + if (left == -1) return -1; + int right = GetHeight(root.right); + if (right == -1) return -1; + int res; + if (Math.Abs(left - right) > 1) + { + res = -1; + } + else + { + res = 1 + Math.Max(left, right); + } + return res; +} +```

From a9923f809d7f22b5271a09764788aa6f16fa430e Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Mon, 20 Nov 2023 09:48:41 +0800 Subject: [PATCH 0815/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#=E9=80=92=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\234\211\350\267\257\345\276\204.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 44c0fd85e3..9ba165c798 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -900,6 +900,43 @@ impl Solution { } } ``` +### C# +```C# +public IList BinaryTreePaths(TreeNode root) +{ + List path = new(); + List res = new(); + if (root == null) return res; + Traversal(root, path, res); + return res; +} +public void Traversal(TreeNode node, List path, List res) +{ + path.Add(node.val); + if (node.left == null && node.right == null) + { + string sPath = ""; + for (int i = 0; i < path.Count - 1; i++) + { + sPath += path[i].ToString(); + sPath += "->"; + } + sPath += path[path.Count - 1].ToString(); + res.Add(sPath); + return; + } + if (node.left != null) + { + Traversal(node.left, path, res); + path.RemoveAt(path.Count-1); + } + if (node.right != null) + { + Traversal(node.right, path, res); + path.RemoveAt(path.Count-1); + } +} +```

From 34de1fd8b0fde25caef3626bf6a848f080607aed Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 21 Nov 2023 09:07:56 +0800 Subject: [PATCH 0816/1533] =?UTF-8?q?Update=20404.=E5=B7=A6=E5=8F=B6?= =?UTF-8?q?=E5=AD=90=E4=B9=8B=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E9=80=92=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\266\345\255\220\344\271\213\345\222\214.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index c1ad602d5f..6dfcc886bc 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -651,6 +651,23 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public int SumOfLeftLeaves(TreeNode root) +{ + if (root == null) return 0; + + int leftValue = SumOfLeftLeaves(root.left); + if (root.left != null && root.left.left == null && root.left.right == null) + { + leftValue += root.left.val; + } + int rightValue = SumOfLeftLeaves(root.right); + return leftValue + rightValue; + +} +```

From b1d56c8cffe8fb25fc9bbf7d8f5aa768491bb68d Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 22 Nov 2023 10:02:57 +0800 Subject: [PATCH 0817/1533] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91?= =?UTF-8?q?=E5=B7=A6=E4=B8=8B=E6=96=B9=E7=9A=84=E5=80=BC=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E9=80=92=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\350\247\222\347\232\204\345\200\274.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index 7ef934cc88..0e2f426633 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -684,6 +684,38 @@ impl Solution { } } ``` +### C# +```C# +//递归 +int maxDepth = -1; +int res = 0; +public int FindBottomLeftValue(TreeNode root) +{ + Traversal(root, 0); + return res; +} +public void Traversal(TreeNode root, int depth) +{ + if (root.left == null && root.right == null) + { + if (depth > maxDepth) + { + maxDepth = depth; + res = root.val; + } + return; + } + if (root.left != null) + { + Traversal(root.left, depth + 1); + } + if (root.right != null) + { + Traversal(root.right, depth + 1); + } + return; +} +```

From 14906d78cb92a8bbfd034634c80daf1a5fa355f0 Mon Sep 17 00:00:00 2001 From: han854 <1229369094@qq.com> Date: Wed, 22 Nov 2023 16:08:00 +0800 Subject: [PATCH 0818/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A30070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...05\250\350\203\214\345\214\205\347\211\210\346\234\254.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 0da1ebeca5..26af256973 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -100,8 +100,8 @@ int main() { while (cin >> n >> m) { vector dp(n + 1, 0); dp[0] = 1; - for (int i = 1; i <= n; i++) { // 遍历物品 - for (int j = 1; j <= m; j++) { // 遍历背包 + for (int i = 1; i <= n; i++) { // 遍历背包 + for (int j = 1; j <= m; j++) { // 遍历物品 if (i - j >= 0) dp[i] += dp[i - j]; } } From 0d993075669743879b59c8a549783604dbbb7b2f Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:38:13 +0800 Subject: [PATCH 0819/1533] =?UTF-8?q?Update0112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E9=80=92?= =?UTF-8?q?=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\267\257\345\276\204\346\200\273\345\222\214.md" | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index be03f719e1..f1ce7637ba 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -1511,6 +1511,17 @@ impl Solution { } } +``` +### C# +```C# +// 0112.路径总和 +// 递归 +public bool HasPathSum(TreeNode root, int targetSum) +{ + if (root == null) return false; + if (root.left == null && root.right == null && targetSum == root.val) return true; + return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val); +} ```

From fe99dff9d328277913ab36703472e2f8a20b8fb6 Mon Sep 17 00:00:00 2001 From: John Li Date: Thu, 23 Nov 2023 19:01:59 +0800 Subject: [PATCH 0820/1533] =?UTF-8?q?Updata=20494=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index a90a38f71d..1a4dda2b0a 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -325,10 +325,8 @@ class Solution { // 初始化最上行(dp[0][j]),当nums[0] == j时(注意nums[0]和j都一定是大于等于零的,因此不需要判断等于-j时的情况),有唯一一种取法可取到j,dp[0][j]此时等于1 // 其他情况dp[0][j] = 0 // java整数数组默认初始值为0 - for(int j = 0; j <= left; j++) { - if(nums[0] == j) { - dp[0][j] = 1; - } + if (nums[0] <= left) { + dp[0][nums[0]] = 1; } // 初始化最左列(dp[i][0]) From 9b40338d80bf51f41a14b5579bb61663ebb41d8c Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 24 Nov 2023 09:46:55 +0800 Subject: [PATCH 0821/1533] Update --- "problems/kama53.\345\257\273\345\256\235.md" | 155 ++++++++++++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 52 ++++-- 3 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 "problems/kama53.\345\257\273\345\256\235.md" diff --git "a/problems/kama53.\345\257\273\345\256\235.md" "b/problems/kama53.\345\257\273\345\256\235.md" new file mode 100644 index 0000000000..b52ac374be --- /dev/null +++ "b/problems/kama53.\345\257\273\345\256\235.md" @@ -0,0 +1,155 @@ + + +如果你的图相对较小且比较密集,而且你更注重简单性和空间效率,数组实现可能更合适。 + +如果你的图规模较大,尤其是在稀疏图中,而且你更注重时间效率和通用性,优先级队列实现可能更合适。 + +其关键 在于弄清楚 minDist 的定义 + +```CPP + +#include +#include +#include +#include + +using namespace std; + +// 定义图的邻接矩阵表示 +const int INF = INT_MAX; // 表示无穷大 +typedef vector> Graph; + +// 使用Prim算法找到最小生成树 +void primMST(const Graph& graph, int startVertex) { + int V = graph.size(); + + // 存储顶点是否在最小生成树中 + vector inMST(V, false); + + // 存储最小生成树的边权重 + vector key(V, INF); + + // 优先队列,存储边权重和目标顶点 + priority_queue, vector>, greater>> pq; + + // 初始顶点的权重设为0,加入优先队列 + key[startVertex] = 0; + pq.push({0, startVertex}); + + while (!pq.empty()) { + // 从优先队列中取出权重最小的边 + int u = pq.top().second; + pq.pop(); + + // 将顶点u标记为在最小生成树中 + inMST[u] = true; + + // 遍历u的所有邻居 + for (int v = 0; v < V; ++v) { + // 如果v未在最小生成树中,且u到v的权重小于v的当前权重 + if (!inMST[v] && graph[u][v] < key[v]) { + // 更新v的权重为u到v的权重 + key[v] = graph[u][v]; + // 将(u, v)添加到最小生成树 + pq.push({key[v], v}); + } + } + } + + // 输出最小生成树的边 + cout << "Edges in the Minimum Spanning Tree:\n"; + for (int i = 1; i < V; ++i) { + cout << i << " - " << key[i] << " - " << i << "\n"; + } +} + +int main() { + // 例子:无向图的邻接矩阵表示 + Graph graph = { + {0, 2, 0, 6, 0}, + {2, 0, 3, 8, 5}, + {0, 3, 0, 0, 7}, + {6, 8, 0, 0, 9}, + {0, 5, 7, 9, 0} + }; + + // 从顶点0开始运行Prim算法 + primMST(graph, 0); + + return 0; +} +``` + + +```CPP +#include +#include +#include + +using namespace std; + +// 定义图的邻接矩阵表示 +const int INF = INT_MAX; // 表示无穷大 +typedef vector> Graph; + +// 使用Prim算法找到最小生成树 +void primMST(const Graph& graph, int startVertex) { + int V = graph.size(); + + // 存储顶点是否在最小生成树中 + vector inMST(V, false); + + // 存储每个顶点的权重 + vector key(V, INF); + + // 初始化起始顶点的权重为0 + key[startVertex] = 0; + + // 存储最小生成树的边权重 + vector parent(V, -1); + + // 构建最小生成树 + for (int count = 0; count < V - 1; ++count) { + // 从未在最小生成树中的顶点中找到权重最小的顶点 + int u = -1; + for (int v = 0; v < V; ++v) { + if (!inMST[v] && (u == -1 || key[v] < key[u])) { + u = v; + } + } + + // 将顶点u标记为在最小生成树中 + inMST[u] = true; + + // 更新u的邻居的权重和父节点 + for (int v = 0; v < V; ++v) { + if (graph[u][v] != 0 && !inMST[v] && graph[u][v] < key[v]) { + key[v] = graph[u][v]; + parent[v] = u; + } + } + } + + // 输出最小生成树的边 + cout << "Edges in the Minimum Spanning Tree:\n"; + for (int i = 1; i < V; ++i) { + cout << parent[i] << " - " << key[i] << " - " << i << "\n"; + } +} + +int main() { + // 例子:无向图的邻接矩阵表示 + Graph graph = { + {0, 2, 0, 6, 0}, + {2, 0, 3, 8, 5}, + {0, 3, 0, 0, 7}, + {6, 8, 0, 0, 9}, + {0, 5, 7, 9, 0} + }; + + // 从顶点0开始运行Prim算法 + primMST(graph, 0); + + return 0; +} +``` diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 3055875a11..e426c65791 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -42,7 +42,7 @@ 如果hashCode得到的数值大于 哈希表的大小了,也就是大于tableSize了,怎么办呢? -此时为了保证映射出来的索引数值都落在哈希表上,我们会在再次对数值做一个取模的操作,就要我们就保证了学生姓名一定可以映射到哈希表上了。 +此时为了保证映射出来的索引数值都落在哈希表上,我们会在再次对数值做一个取模的操作,这样我们就保证了学生姓名一定可以映射到哈希表上了。 此时问题又来了,哈希表我们刚刚说过,就是一个数组。 diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 2c14f49037..347bf58f13 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -274,27 +274,53 @@ join(3, 2); 通过以上讲解之后,我在带大家一步一步去画一下,并查集内部数据连接方式。 -注意:为了让录友们了解基础并查集的操作,不至于混乱,**以下模拟过程中不考虑路径压缩的过程**,了解基础并查集操作后,路径压缩也很容易理解。 +1、`join(1, 8);` -我们先通过一些列的join操作,将两两元素分别放入同一个集合中。 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122112727.png) -```CPP -join(1, 8); -join(3, 8); -join(1, 7); -join(8, 5); -join(2, 9); -join(6, 2); -``` -此时我们生成的的有向图为: +2、`join(3, 8);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230910203210.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122113857.png) 有录友可能想,`join(3, 8)` 在图中为什么 将 元素1 连向元素 3 而不是将 元素 8 连向 元素 3 呢? 这一点 我在 「常见误区」标题下已经详细讲解了,因为在`join(int u, int v)`函数里 要分别对 u 和 v 寻根之后再进行关联。 +3、`join(1, 7);` + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122114108.png) + + +4、`join(8, 5);` + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122114847.png) + +这里8的根是3,那么 5 应该指向 8 的根 3,这里的原因,我们在上面「常见误区」已经讲过了。 但 为什么 图中 8 又直接指向了 3 了呢? + +**因为路经压缩了** + +即如下代码在寻找跟的过程中,会有路径压缩,减少 下次查询的路径长度。 + +``` +// 并查集里寻根的过程 +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 +} +``` + +5、`join(2, 9);` + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122115000.png) + +6、`join(6, 9);` + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122115404.png) + +这里为什么是 2 指向了 6,因为 9的根为 2,所以用2指向6。 + + + 大家看懂这个有向图后,相信应该知道如下函数的返回值了。 ```CPP @@ -309,6 +335,8 @@ true false ``` + + ## 拓展 From 734616dff0402cf35029835fcf71c1ce2527509e Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Fri, 24 Nov 2023 19:01:11 +0800 Subject: [PATCH 0822/1533] =?UTF-8?q?Update=200106.=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\200\240\344\272\214\345\217\211\346\240\221.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index 0144fc5c05..dc517480a6 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -1228,6 +1228,19 @@ impl Solution { } } ``` +### C# +```C# +public TreeNode BuildTree(int[] inorder, int[] postorder) +{ + if (inorder.Length == 0 || postorder.Length == null) return null; + int rootValue = postorder.Last(); + TreeNode root = new TreeNode(rootValue); + int delimiterIndex = Array.IndexOf(inorder, rootValue); + root.left = BuildTree(inorder.Take(delimiterIndex).ToArray(), postorder.Take(delimiterIndex).ToArray()); + root.right = BuildTree(inorder.Skip(delimiterIndex + 1).ToArray(), postorder.Skip(delimiterIndex).Take(inorder.Length - delimiterIndex - 1).ToArray()); + return root; +} +```

From cbb3cf962c4d223296d760cfaaad89e484b77f86 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sat, 25 Nov 2023 09:55:40 +0800 Subject: [PATCH 0823/1533] =?UTF-8?q?Update=200654.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\247\344\272\214\345\217\211\346\240\221.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 77d7980f3d..6b343840f9 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -582,6 +582,21 @@ impl Solution { } } ``` +### C# +```C# +public TreeNode ConstructMaximumBinaryTree(int[] nums) +{ + if (nums.Length == 0) return null; + int rootValue = nums.Max(); + TreeNode root = new TreeNode(rootValue); + int rootIndex = Array.IndexOf(nums, rootValue); + + root.left = ConstructMaximumBinaryTree(nums.Take(rootIndex).ToArray()); + root.right = ConstructMaximumBinaryTree(nums.Skip(rootIndex + 1).ToArray()); + return root; + +} +```

From 26816ec04f90ffcfcf7c0377ac456938dddaa2e2 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sun, 26 Nov 2023 10:29:53 +0800 Subject: [PATCH 0824/1533] =?UTF-8?q?Update=200617.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...271\266\344\272\214\345\217\211\346\240\221.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 18839a2638..9efeb56db9 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -788,6 +788,20 @@ impl Solution { } } ``` +### C# +```C# +public TreeNode MergeTrees(TreeNode root1, TreeNode root2) +{ + if (root1 == null) return root2; + if (root2 == null) return root1; + + root1.val += root2.val; + root1.left = MergeTrees(root1.left, root2.left); + root1.right = MergeTrees(root1.right, root2.right); + + return root1; +} +```

From ca89ca30975554cd3130eb6a2f6101c80723f99f Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 27 Nov 2023 10:16:15 +0800 Subject: [PATCH 0825/1533] =?UTF-8?q?Update=200700.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\346\220\234\347\264\242.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index bc21bab872..65e6921915 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -464,6 +464,28 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public TreeNode SearchBST(TreeNode root, int val) +{ + if (root == null || root.val == val) return root; + if (root.val > val) return SearchBST(root.left, val); + if (root.val < val) return SearchBST(root.right, val); + return null; +} +// 迭代 +public TreeNode SearchBST(TreeNode root, int val) +{ + while (root != null) + { + if (root.val > val) root = root.left; + else if (root.val < val) root = root.right; + else return root; + } + return null; +} +```

From 7aef42def84ef94f198eedd85a946130319264da Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 28 Nov 2023 10:17:25 +0800 Subject: [PATCH 0826/1533] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...217\211\346\220\234\347\264\242\346\240\221.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 184060a5cd..319ad1aaa1 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -791,6 +791,20 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public long val = Int64.MinValue; +public bool IsValidBST(TreeNode root) +{ + if (root == null) return true; + bool left = IsValidBST(root.left); + if (root.val > val) val = root.val; + else return false; + bool right = IsValidBST(root.right); + return left && right; +} +```

From b19af70230cd250a6a675037c79bd693812c5dfe Mon Sep 17 00:00:00 2001 From: jycoast <42469825+jycoast@users.noreply.github.com> Date: Tue, 28 Nov 2023 10:38:26 +0800 Subject: [PATCH 0827/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97=E2=80=9C=E5=8F=88=E5=AD=A9=E5=AD=90=E2=80=9D=E4=B8=BA?= =?UTF-8?q?=E2=80=9C=E5=8F=B3=E5=AD=A9=E5=AD=90=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 3646488912..f616ec7417 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -266,7 +266,7 @@ class Solution { // 3.状态标记递归 // 执行用时:0 ms , 在所有 Java 提交中击败了 100% 的用户 - // 不偷:Max(左孩子不偷,左孩子偷) + Max(又孩子不偷,右孩子偷) + // 不偷:Max(左孩子不偷,左孩子偷) + Max(右孩子不偷,右孩子偷) // root[0] = Math.max(rob(root.left)[0], rob(root.left)[1]) + // Math.max(rob(root.right)[0], rob(root.right)[1]) // 偷:左孩子不偷+ 右孩子不偷 + 当前节点偷 From 1043b7740efd324cdfa53655cd897bb16ec4d159 Mon Sep 17 00:00:00 2001 From: Echo0701 <133868458+Echo0701@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:51:45 +0800 Subject: [PATCH 0828/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=20Java=20=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\214\205\347\211\210\346\234\254.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 26af256973..93a336ab93 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -139,6 +139,30 @@ int main() { ### Java: +```Java +import java.util.Scanner; +class climbStairs{ + public static void main(String [] args){ + Scanner sc=new Scanner(System.in); + int m, n; + while(sc.hasNextInt()){ + // 从键盘输入参数,中间用空格隔开 + n = sc.nextInt(); + m = sc.nextInt(); + + // 求排列问题,先遍历背包再遍历物品 + int[] dp = new int[n + 1]; + dp[0] = 1; + for (int j = 1; j <= n; j++) { + for (int i = 1; i <= m; i++) { + if (j - i >= 0) dp[j] += dp[j - i]; + } + } + System.out.println(dp[n]); + } + } +} +``` ### Python3: From 71dd313eebbda6936ee31323798ba6ca8f5d3941 Mon Sep 17 00:00:00 2001 From: Echo0701 <133868458+Echo0701@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:06:53 +0800 Subject: [PATCH 0829/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=20Java=20=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...05\250\350\203\214\345\214\205\347\211\210\346\234\254.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 93a336ab93..f8337cc048 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -143,9 +143,9 @@ int main() { import java.util.Scanner; class climbStairs{ public static void main(String [] args){ - Scanner sc=new Scanner(System.in); + Scanner sc = new Scanner(System.in); int m, n; - while(sc.hasNextInt()){ + while (sc.hasNextInt()) { // 从键盘输入参数,中间用空格隔开 n = sc.nextInt(); m = sc.nextInt(); From bc7f72cad70d1459f290a85f02447c43939606d3 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 29 Nov 2023 09:05:34 +0800 Subject: [PATCH 0830/1533] =?UTF-8?q?Update=20530.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=B7=AE=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\347\273\235\345\257\271\345\267\256.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 56911858dd..f08df577d9 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -647,6 +647,27 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public class Solution +{ + public List res = new List(); + public int GetMinimumDifference(TreeNode root) + { + Traversal(root); + return res.SelectMany((x, i) => res.Skip(i + 1).Select(y => Math.Abs(x - y))).Min(); + + } + public void Traversal(TreeNode root) + { + if (root == null) return; + Traversal(root.left); + res.Add(root.val); + Traversal(root.right); + } +} +```

From 26df976d6297777e5f984746f72ad9f453e13e0b Mon Sep 17 00:00:00 2001 From: QZ-z <55394467+QZ-z@users.noreply.github.com> Date: Wed, 29 Nov 2023 13:17:40 +0800 Subject: [PATCH 0831/1533] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改java程序快指针代码,删除循环条件中的= --- ...225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 28bb61eea4..f508b523e3 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -106,7 +106,7 @@ public ListNode removeNthFromEnd(ListNode head, int n){ ListNode slowIndex = dummyNode; // 只要快慢指针相差 n 个结点即可 - for (int i = 0; i <= n ; i++){ + for (int i = 0; i < n ; i++){ fastIndex = fastIndex.next; } From 22ae299a273f69ebbc7411309b9dfee3c2e2a9d3 Mon Sep 17 00:00:00 2001 From: Echo0701 <133868458+Echo0701@users.noreply.github.com> Date: Thu, 30 Nov 2023 18:48:52 +0800 Subject: [PATCH 0832/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=A4=9A?= =?UTF-8?q?=E9=87=8D=E8=83=8C=E5=8C=85=E7=9A=84=20Java=20=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\351\207\215\350\203\214\345\214\205.md" | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index b19b52731a..5d6440e3bf 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -165,7 +165,43 @@ int main() { ### Java: - +```Java +import java.util.Scanner; +class multi_pack{ + public static void main(String [] args) { + Scanner sc = new Scanner(System.in); + + /** + * bagWeight:背包容量 + * n:物品种类 + */ + int bagWeight, n; + + //获取用户输入数据,中间用空格隔开,回车键换行 + bagWeight = sc.nextInt(); + n = sc.nextInt(); + int[] weight = new int[n]; + int[] value = new int[n]; + int[] nums = new int[n]; + for (int i = 0; i < n; i++) weight[i] = sc.nextInt(); + for (int i = 0; i < n; i++) value[i] = sc.nextInt(); + for (int i = 0; i < n; i++) nums[i] = sc.nextInt(); + + int[] dp = new int[bagWeight + 1]; + + //先遍历物品再遍历背包,作为01背包处理 + for (int i = 0; i < n; i++) { + for (int j = bagWeight; j >= weight[i]; j--) { + //遍历每种物品的个数 + for (int k = 1; k <= nums[i] && (j - k * weight[i]) >= 0; k++) { + dp[j] = Math.max(dp[j], dp[j - k * weight[i]] + k * value[i]); + } + } + } + System.out.println(dp[bagWeight]); + } +} +``` ### Python: ### Go: From 0ed113487080dd4cf99d3048863f67c267af96aa Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 3 Dec 2023 09:41:53 +0800 Subject: [PATCH 0833/1533] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\344\274\227\346\225\260.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 8881200f5d..5b26d580b2 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -1009,6 +1009,46 @@ pub fn find_mode(root: Option>>) -> Vec { res } ``` +### C# +```C# +// 递归 +public class Solution +{ + public List res = new List(); + public int count = 0; + public int maxCount = 0; + public TreeNode pre = null; + public int[] FindMode(TreeNode root) + { + SearchBST(root); + return res.ToArray(); + } + public void SearchBST(TreeNode root) + { + if (root == null) return; + SearchBST(root.left); + if (pre == null) + count = 1; + else if (pre.val == root.val) + count++; + else + count = 1; + + pre = root; + if (count == maxCount) + { + res.Add(root.val); + } + else if (count > maxCount) + { + res.Clear(); + res.Add(root.val); + maxCount = count; + } + SearchBST(root.right); + } +} +```

From a0e2c5ceb06d9199bc6f2ce2eb0aa87a8ebf8fe4 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 4 Dec 2023 09:42:53 +0800 Subject: [PATCH 0834/1533] =?UTF-8?q?Update0236.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88=EF=BC=8C=E6=8F=90=E4=BA=A4C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\205\254\345\205\261\347\245\226\345\205\210.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index edb5ea3ea7..0e99f1c2fb 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -431,6 +431,19 @@ impl Solution { } } ``` +### C# +```C# +public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) +{ + if (root == null || root == p || root == q) return root; + TreeNode left = LowestCommonAncestor(root.left, p, q); + TreeNode right = LowestCommonAncestor(root.right, p, q); + if (left != null && right != null) return root; + if (left == null && right != null) return right; + if (left != null && right == null) return left; + return null; +} +```

From 09e230bef32642e1e00f6223b83749a93989d70c Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 5 Dec 2023 09:08:03 +0800 Subject: [PATCH 0835/1533] =?UTF-8?q?Update0235.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E7=A5=96=E5=85=88=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index b7e92e4e80..b27a231e77 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -513,6 +513,31 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) +{ + if (root.val > p.val && root.val > q.val) + return LowestCommonAncestor(root.left, p, q); + if (root.val < p.val && root.val < q.val) + return LowestCommonAncestor(root.right, p, q); + return root; +} +// 迭代 +public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) +{ + while (root != null) + { + if (root.val > p.val && root.val > q.val) + root = root.left; + else if (root.val < p.val && root.val < q.val) + root = root.right; + else return root; + } + return null; +} +```

From 29b7e23ac9d3c063e4e2c2e3ccf90cdf49eacf10 Mon Sep 17 00:00:00 2001 From: block <1181882120@qq.com> Date: Tue, 5 Dec 2023 09:57:09 +0800 Subject: [PATCH 0836/1533] Update minimum deepth doc --- ...\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index b27df4a626..465233493d 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -50,7 +50,7 @@ ![111.二叉树的最小深度](https://code-thinking.cdn.bcebos.com/pics/111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.png) -这就重新审题了,题目中说的是:**最小深度是从根节点到最近叶子节点的最短路径上的节点数量。**,注意是**叶子节点**。 +这就重新审题了,题目中说的是:**最小深度是从根节点到最近叶子节点的最短路径上的节点数量。**注意是**叶子节点**。 什么是叶子节点,左右孩子都为空的节点才是叶子节点! From a18bca36254163df96b811b8598f2d07081369ec Mon Sep 17 00:00:00 2001 From: LePing Huang <94521862+hlp777@users.noreply.github.com> Date: Tue, 5 Dec 2023 16:52:40 +0800 Subject: [PATCH 0837/1533] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 最新力扣新增测试案例 nums = [0] 预期输出 1 实际输出 0 就是nums.length == 0 时 要return 1 --- ...\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 677c72c6dd..64f75291f5 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -130,7 +130,7 @@ public: class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; - int res = 0; + int res = 1; Arrays.fill(dp, 1); for (int i = 1; i < dp.length; i++) { for (int j = 0; j < i; j++) { From 35322cb120cd2d64e1e760eb6662fa68dded9e2b Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 6 Dec 2023 09:05:08 +0800 Subject: [PATCH 0838/1533] =?UTF-8?q?Update0701.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\217\222\345\205\245\346\223\215\344\275\234.md" | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 265bbb5b0d..98e60d5f5f 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -691,6 +691,17 @@ impl Solution { } } ``` +### C# +``` C# +// 递归 +public TreeNode InsertIntoBST(TreeNode root, int val) { + if (root == null) return new TreeNode(val); + + if (root.val > val) root.left = InsertIntoBST(root.left, val); + if (root.val < val) root.right = InsertIntoBST(root.right, val); + return root; +} +```

From be7fd6753a8b2a00ce24f1766c67e13f1d6ef69f Mon Sep 17 00:00:00 2001 From: freshield Date: Fri, 8 Dec 2023 06:56:25 +0800 Subject: [PATCH 0839/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=AB=A0=E8=8A=82=20->=20?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=95=B0=E5=AD=97=E7=9A=84GO=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E5=8F=8C=E6=8C=87=E9=92=88=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index d5abbc4d8a..a7e0c7f099 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -183,6 +183,54 @@ func main(){ fmt.Printf(string(strByte)) } ```` +Go使用双指针解法 +````go +package main + +import "fmt" + +func replaceNumber(strByte []byte) string { + // 查看有多少字符 + numCount, oldSize := 0, len(strByte) + for i := 0; i < len(strByte); i++ { + if (strByte[i] <= '9') && (strByte[i] >= '0') { + numCount ++ + } + } + // 增加长度 + for i := 0; i < numCount; i++ { + strByte = append(strByte, []byte(" ")...) + } + tmpBytes := []byte("number") + // 双指针从后遍历 + leftP, rightP := oldSize-1, len(strByte)-1 + for leftP < rightP { + rightShift := 1 + // 如果是数字则加入number + if (strByte[leftP] <= '9') && (strByte[leftP] >= '0') { + for i, tmpByte := range tmpBytes { + strByte[rightP-len(tmpBytes)+i+1] = tmpByte + } + rightShift = len(tmpBytes) + } else { + strByte[rightP] = strByte[leftP] + } + // 更新指针 + rightP -= rightShift + leftP -= 1 + } + return string(strByte) +} + +func main(){ + var strByte []byte + fmt.Scanln(&strByte) + + newString := replaceNumber(strByte) + + fmt.Println(newString) +} +```` From 7692d6cc97a999016aa0bd023433a2e0975e262d Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 8 Dec 2023 08:08:10 +0800 Subject: [PATCH 0840/1533] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\346\220\234\347\264\242\346\240\221.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 001865585d..229a2ab148 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -567,6 +567,23 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public TreeNode TrimBST(TreeNode root, int low, int high) +{ + if (root == null) return null; + if (root.val < low) + return TrimBST(root.right, low, high); + + if (root.val > high) + return TrimBST(root.left, low, high); + + root.left = TrimBST(root.left, low, high); + root.right = TrimBST(root.right, low, high); + return root; +} +```

From 3427281196aadee968f41e92ca9beca7aaf81211 Mon Sep 17 00:00:00 2001 From: freshield Date: Fri, 8 Dec 2023 12:36:49 +0800 Subject: [PATCH 0841/1533] =?UTF-8?q?Fix:=20=E4=BF=AE=E6=AD=A3markdown?= =?UTF-8?q?=E4=B8=AD=E7=9A=84tab=E4=B8=BA=E7=A9=BA=E6=A0=BC=EF=BC=8C?= =?UTF-8?q?=E6=8E=92=E9=99=A4=E6=98=BE=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index a7e0c7f099..0f8daa2175 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -190,45 +190,45 @@ package main import "fmt" func replaceNumber(strByte []byte) string { - // 查看有多少字符 - numCount, oldSize := 0, len(strByte) - for i := 0; i < len(strByte); i++ { - if (strByte[i] <= '9') && (strByte[i] >= '0') { - numCount ++ - } - } - // 增加长度 - for i := 0; i < numCount; i++ { - strByte = append(strByte, []byte(" ")...) - } - tmpBytes := []byte("number") - // 双指针从后遍历 - leftP, rightP := oldSize-1, len(strByte)-1 - for leftP < rightP { - rightShift := 1 - // 如果是数字则加入number - if (strByte[leftP] <= '9') && (strByte[leftP] >= '0') { - for i, tmpByte := range tmpBytes { - strByte[rightP-len(tmpBytes)+i+1] = tmpByte - } - rightShift = len(tmpBytes) - } else { - strByte[rightP] = strByte[leftP] - } - // 更新指针 - rightP -= rightShift - leftP -= 1 - } - return string(strByte) + // 查看有多少字符 + numCount, oldSize := 0, len(strByte) + for i := 0; i < len(strByte); i++ { + if (strByte[i] <= '9') && (strByte[i] >= '0') { + numCount ++ + } + } + // 增加长度 + for i := 0; i < numCount; i++ { + strByte = append(strByte, []byte(" ")...) + } + tmpBytes := []byte("number") + // 双指针从后遍历 + leftP, rightP := oldSize-1, len(strByte)-1 + for leftP < rightP { + rightShift := 1 + // 如果是数字则加入number + if (strByte[leftP] <= '9') && (strByte[leftP] >= '0') { + for i, tmpByte := range tmpBytes { + strByte[rightP-len(tmpBytes)+i+1] = tmpByte + } + rightShift = len(tmpBytes) + } else { + strByte[rightP] = strByte[leftP] + } + // 更新指针 + rightP -= rightShift + leftP -= 1 + } + return string(strByte) } func main(){ - var strByte []byte - fmt.Scanln(&strByte) - + var strByte []byte + fmt.Scanln(&strByte) + newString := replaceNumber(strByte) - fmt.Println(newString) + fmt.Println(newString) } ```` From 5c20612aba9755626fe75688d367ebe68714d22e Mon Sep 17 00:00:00 2001 From: eat to 160 pounds <2915390277@qq.com> Date: Fri, 8 Dec 2023 20:27:07 +0800 Subject: [PATCH 0842/1533] =?UTF-8?q?feat:0322=E9=87=8D=E6=96=B0=E5=AE=89?= =?UTF-8?q?=E6=8E=92=E8=A1=8C=E7=A8=8B=20=E6=B7=BB=E5=8A=A0JavaScript?= =?UTF-8?q?=E7=89=88=E6=9C=AC=20=E5=A4=84=E7=90=86=E5=AF=B9=E8=B1=A1key?= =?UTF-8?q?=E6=97=A0=E5=BA=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index d1fd46f68a..1473ed0505 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -562,6 +562,7 @@ func findItinerary(tickets [][]string) []string { ``` ### Javascript + ```Javascript var findItinerary = function(tickets) { @@ -605,6 +606,74 @@ var findItinerary = function(tickets) { ``` +**javascript版本二 处理对象key无序问题** + +```javascript +/** + * @param {string[][]} tickets + * @return {string[]} + */ +var findItinerary = function (tickets) { + const ans = ["JFK"]; + let map = {}; + // 整理每个站点的终点站信息 + tickets.forEach((t) => { + let targets = map[t[0]]; + if (!targets) { + targets = { [t[1]]: 0 }; + map[t[0]] = targets; + } + targets[t[1]] = (targets[t[1]] || 0) + 1; + }); + // 按照key字典序排序对象 + const sortObject = (obj) => { + const newObj = {}; + const keys = Object.keys(obj); + keys.sort((k1, k2) => (k1 < k2 ? -1 : 1)); + keys.forEach((key) => { + if (obj[key] !== null && typeof obj[key] === "object") { + newObj[key] = sortObject(obj[key]); + } else { + newObj[key] = obj[key]; + } + }); + return newObj; + }; + const backtrack = (tickets, targets) => { + if (ans.length === tickets.length + 1) { + return true; + } + const target = targets[ans[ans.length - 1]]; + // 没有下一站 + if (!target) { + return false; + } + // 或者在这里排序 + // const keyList = Object.keys(target).sort((k1, k2) => (k1 < k2 ? -1 : 1)); + const keyList = Object.keys(target); + for (const key of keyList) { + // 判断当前站是否还能飞 + if (target[key] > 0) { + target[key]--; + ans.push(key); + // 对象key有序 此时的行程就是字典序最小的 直接跳出 + if (backtrack(tickets, targets)) { + return true; + } + target[key]++; + ans.pop(); + } + } + return false; + }; + map = sortObject(map); + backtrack(tickets, map); + return ans; +}; +``` + + + ### TypeScript ```typescript @@ -901,3 +970,4 @@ impl Solution { + From c0c5e38d9ffe16161aceb6658ec515bd50ca57f3 Mon Sep 17 00:00:00 2001 From: eat to 160 pounds <2915390277@qq.com> Date: Fri, 8 Dec 2023 21:51:19 +0800 Subject: [PATCH 0843/1533] =?UTF-8?q?feat:=200051.N=E7=9A=87=E5=90=8E=20?= =?UTF-8?q?=E6=9B=B4=E7=AE=80=E6=B4=81=E6=B8=85=E6=99=B0=E7=9A=84javascrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 114 ++++++++++--------- 1 file changed, 60 insertions(+), 54 deletions(-) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 6bc4fa78c6..f629079372 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -453,60 +453,66 @@ func isValid(n, row, col int, chessboard [][]string) bool { ### Javascript ```Javascript -var solveNQueens = function(n) { - function isValid(row, col, chessBoard, n) { - - for(let i = 0; i < row; i++) { - if(chessBoard[i][col] === 'Q') { - return false - } - } - - for(let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { - if(chessBoard[i][j] === 'Q') { - return false - } - } - - for(let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) { - if(chessBoard[i][j] === 'Q') { - return false - } - } - return true - } - - function transformChessBoard(chessBoard) { - let chessBoardBack = [] - chessBoard.forEach(row => { - let rowStr = '' - row.forEach(value => { - rowStr += value - }) - chessBoardBack.push(rowStr) - }) - - return chessBoardBack - } - - let result = [] - function backtracing(row,chessBoard) { - if(row === n) { - result.push(transformChessBoard(chessBoard)) - return - } - for(let col = 0; col < n; col++) { - if(isValid(row, col, chessBoard, n)) { - chessBoard[row][col] = 'Q' - backtracing(row + 1,chessBoard) - chessBoard[row][col] = '.' - } - } - } - let chessBoard = new Array(n).fill([]).map(() => new Array(n).fill('.')) - backtracing(0,chessBoard) - return result - +/** + * @param {number} n + * @return {string[][]} + */ +var solveNQueens = function (n) { + const ans = []; + const path = []; + const matrix = new Array(n).fill(0).map(() => new Array(n).fill(".")); + // 判断是否能相互攻击 + const canAttack = (matrix, row, col) => { + let i; + let j; + // 判断正上方和正下方是否有皇后 + for (i = 0, j = col; i < n; i++) { + if (matrix[i][j] === "Q") { + return true; + } + } + // 判断正左边和正右边是否有皇后 + for (i = row, j = 0; j < n; j++) { + if (matrix[i][j] === "Q") { + return true; + } + } + // 判断左上方是否有皇后 + for (i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { + if (matrix[i][j] === "Q") { + return true; + } + } + // 判断右上方是否有皇后 + for (i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) { + if (matrix[i][j] === "Q") { + return true; + } + } + return false; + }; + const backtrack = (matrix, row, col) => { + if (path.length === matrix.length) { + ans.push(path.slice()); + return; + } + for (let i = row; i < matrix.length; i++) { + for (let j = col; j < matrix.length; j++) { + // 当前位置会导致互相攻击 继续下一轮搜索 + if (canAttack(matrix, i, j)) { + continue; + } + matrix[i][j] = "Q"; + path.push(matrix[i].join("")); + // 另起一行搜索 同一行只能有一个皇后 + backtrack(matrix, i + 1, 0); + matrix[i][j] = "."; + path.pop(); + } + } + }; + backtrack(matrix, 0, 0); + return ans; }; ``` From 0e55c1b424c4dac5cb78c304f412fce9fec14e47 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 9 Dec 2023 09:35:59 +0800 Subject: [PATCH 0844/1533] =?UTF-8?q?Update0108.=E5=B0=86=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\346\220\234\347\264\242\346\240\221.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index e699005edb..4ccfc5f1e1 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -530,6 +530,23 @@ impl Solution { } } ``` +### C# +```csharp +// 递归 +public TreeNode SortedArrayToBST(int[] nums) +{ + return Traversal(nums, 0, nums.Length - 1); +} +public TreeNode Traversal(int[] nums, int left, int right) +{ + if (left > right) return null; + int mid = left + (right - left) / 2; + TreeNode node = new TreeNode(nums[mid]); + node.left = Traversal(nums, left, mid - 1); + node.right = Traversal(nums, mid + 1, right); + return node; +} +```

From e59dbae465c5247ff044a7d7fecd0b1cb6e22efe Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 10 Dec 2023 09:25:18 +0800 Subject: [PATCH 0845/1533] =?UTF-8?q?update538.=E6=8A=8A=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA=E7=B4=AF?= =?UTF-8?q?=E5=8A=A0=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\347\264\257\345\212\240\346\240\221.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index c403c98f24..07cae1adb9 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -529,6 +529,23 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public class Solution +{ + int pre = 0; + public TreeNode ConvertBST(TreeNode root) + { + if (root == null) return null; + ConvertBST(root.right); + root.val += pre; + pre = root.val; + ConvertBST(root.left); + return root; + } +} +```

From 8de2e21d59d9fe68a9ac237affecf07b780c103f Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 11 Dec 2023 09:35:55 +0800 Subject: [PATCH 0846/1533] =?UTF-8?q?Update0077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 54 ++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index a7f00ffed9..8bca6f2496 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -792,7 +792,59 @@ def backtracking(result, path, n, j, k) end ``` - +### C# +```C# +// 暴力 +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> Combine(int n, int k) + { + BackTracking(n, k, 1); + return res; + } + public void BackTracking(int n, int k, int start) + { + if (path.Count == k) + { + res.Add(new List(path)); + return; + } + for (int i = start; i <= n; i++) + { + path.Add(i); + BackTracking(n, k, i + 1); + path.RemoveAt(path.Count - 1); + } + } +} +// 剪枝 +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> Combine(int n, int k) + { + BackTracking(n, k, 1); + return res; + } + public void BackTracking(int n, int k, int start) + { + if (path.Count == k) + { + res.Add(new List(path)); + return; + } + for (int i = start; i <= n - (k - path.Count) + 1; i++) + { + path.Add(i); + BackTracking(n, k, i + 1); + path.RemoveAt(path.Count - 1); + } + } +} +```

From d3e61f36abf8b40fa9afac4e7c414f13d8bfcec7 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 12 Dec 2023 09:20:04 +0800 Subject: [PATCH 0847/1533] =?UTF-8?q?Update0216.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C3=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\220\210\346\200\273\345\222\214III.md" | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index c0cb8860c2..ac28f9fcfa 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -633,7 +633,66 @@ object Solution { } } ``` - +### C# +```csharp +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum3(int k, int n) + { + BackTracking(k, n, 0, 1); + return res; + } + public void BackTracking(int k, int n, int sum, int start) + { + if (path.Count == k) + { + if (sum == n) + res.Add(new List(path)); + return; + } + for (int i = start; i <= 9; i++) + { + sum += i; + path.Add(i); + BackTracking(k, n, sum, i + 1); + sum -= i; + path.RemoveAt(path.Count - 1); + } + } +} +// 剪枝 +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum3(int k, int n) + { + BackTracking(k, n, 0, 1); + return res; + } + public void BackTracking(int k, int n, int sum, int start) + { + if (sum > n) + return; + if (path.Count == k) + { + if (sum == n) + res.Add(new List(path)); + return; + } + for (int i = start; i <= 9 - (k - path.Count) + 1; i++) + { + sum += i; + path.Add(i); + BackTracking(k, n, sum, i + 1); + sum -= i; + path.RemoveAt(path.Count - 1); + } + } +} +``` From c14df2f5779314078744090b62d9745f34de1d24 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 13 Dec 2023 09:45:51 +0800 Subject: [PATCH 0848/1533] =?UTF-8?q?Update0017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\347\273\204\345\220\210.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index 90296efdc1..a77bce4643 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -732,6 +732,38 @@ def backtracking(result, letter_map, digits, path, index) end end ``` +### C# +```C# +public class Solution +{ + public IList res = new List(); + public string s; + public string[] letterMap = new string[10] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; + public IList LetterCombinations(string digits) + { + if (digits.Length == 0) + return res; + BackTracking(digits, 0); + return res; + } + public void BackTracking(string digits, int index) + { + if (index == digits.Length) + { + res.Add(s); + return; + } + int digit = digits[index] - '0'; + string letters = letterMap[digit]; + for (int i = 0; i < letters.Length; i++) + { + s += letters[i]; + BackTracking(digits, index + 1); + s = s.Substring(0, s.Length - 1); + } + } +} +```

From 3b507ac7552655dc98ea198259103280e4601804 Mon Sep 17 00:00:00 2001 From: keepgogogo <57135073+keepgogogo@users.noreply.github.com> Date: Wed, 13 Dec 2023 19:04:26 +0800 Subject: [PATCH 0849/1533] =?UTF-8?q?0017.=E7=94=B5=E8=AF=9D=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88=E7=9A=84?= =?UTF-8?q?Java=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A=E4=B8=AD=E5=B0=86Strin?= =?UTF-8?q?gBuilder=E5=86=99=E6=88=90=E4=BA=86StringBuild?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index 90296efdc1..076bb771ed 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -260,7 +260,7 @@ class Solution { } - //每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuild + //每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuilder StringBuilder temp = new StringBuilder(); //比如digits如果为"23",num 为0,则str表示2对应的 abc From dcc37da6034f3ad8166c9f06ee2ab75a3ad204e7 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 14 Dec 2023 10:01:34 +0800 Subject: [PATCH 0850/1533] =?UTF-8?q?Update0039.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E7=BB=BC=E5=90=88=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\346\200\273\345\222\214.md" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 37d5614e71..81558cc12c 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -598,6 +598,66 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum(int[] candidates, int target) + { + BackTracking(candidates, target, 0, 0); + return res; + } + public void BackTracking(int[] candidates, int target, int start, int sum) + { + if (sum > target) return; + if (sum == target) + { + res.Add(new List(path)); + return; + } + for (int i = start; i < candidates.Length; i++) + { + sum += candidates[i]; + path.Add(candidates[i]); + BackTracking(candidates, target, i, sum); + sum -= candidates[i]; + path.RemoveAt(path.Count - 1); + } + } +} + +// 剪枝优化 +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> CombinationSum(int[] candidates, int target) + { + Array.Sort(candidates); + BackTracking(candidates, target, 0, 0); + return res; + } + public void BackTracking(int[] candidates, int target, int start, int sum) + { + if (sum > target) return; + if (sum == target) + { + res.Add(new List(path)); + return; + } + for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++) + { + sum += candidates[i]; + path.Add(candidates[i]); + BackTracking(candidates, target, i, sum); + sum -= candidates[i]; + path.RemoveAt(path.Count - 1); + } + } +} +```

From dd287478a1eaa1b2110a12775b3f72f77e9ed23a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=84=E6=B7=B1=E8=80=81=E8=90=8C=E6=96=B0?= <98251397+Kiro-Young@users.noreply.github.com> Date: Thu, 14 Dec 2023 14:50:53 +0800 Subject: [PATCH 0851/1533] =?UTF-8?q?Fix:=20=E4=BF=AE=E6=AD=A3=E6=A0=88?= =?UTF-8?q?=E4=B8=8E=E9=98=9F=E5=88=97=E6=80=BB=E7=BB=93.md=E7=9A=84?= =?UTF-8?q?=E4=B8=80=E5=A4=84=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正一处栈与队列总结.md的错别字,“本地”应该为“本题” --- ...\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index 4e08a887ee..06a7827050 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -115,7 +115,7 @@ cd a/b/c/../../ **单调队列不是一成不变的,而是不同场景不同写法**,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 -**不要以为本地中的单调队列实现就是固定的写法。** +**不要以为本题中的单调队列实现就是固定的写法。** 我们用deque作为单调队列的底层数据结构,C++中deque是stack和queue默认的底层实现容器(这个我们之前已经讲过),deque是可以两边扩展的,而且deque里元素并不是严格的连续分布的。 From a9ed0d367200a4e85eed5f93a728fbed0f7f7b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=84=E6=B7=B1=E8=80=81=E8=90=8C=E6=96=B0?= <98251397+Kiro-Young@users.noreply.github.com> Date: Thu, 14 Dec 2023 14:59:16 +0800 Subject: [PATCH 0852/1533] =?UTF-8?q?Fix:=200347.=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md=20Java=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81=E9=A3=8E=E6=A0=BC=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix: 0347.前K个高频元素.md Java部分代码风格规范改进;今天看示例代码觉得有点挤得难受,仔细看发现是未简化的讲解部分代码不少地方缺空格太拥挤,所以前来加一些空格便于阅读,未修改代码及其逻辑,仅方便阅读改进代码风格添加了些许空格 --- ...30\351\242\221\345\205\203\347\264\240.md" | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 4830f9a3f2..94b29eba3e 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -145,43 +145,43 @@ public: class Solution { //解法1:基于大顶堆实现 public int[] topKFrequent1(int[] nums, int k) { - Map map = new HashMap<>();//key为数组元素值,val为对应出现次数 - for(int num:nums){ - map.put(num,map.getOrDefault(num,0)+1); + Map map = new HashMap<>(); //key为数组元素值,val为对应出现次数 + for (int num : nums) { + map.put(num, map.getOrDefault(num,0) + 1); } - //在优先队列中存储二元组(num,cnt),cnt表示元素值num在数组中的出现次数 + //在优先队列中存储二元组(num, cnt),cnt表示元素值num在数组中的出现次数 //出现次数按从队头到队尾的顺序是从大到小排,出现次数最多的在队头(相当于大顶堆) - PriorityQueue pq = new PriorityQueue<>((pair1, pair2)->pair2[1]-pair1[1]); - for(Map.Entry entry:map.entrySet()){//大顶堆需要对所有元素进行排序 - pq.add(new int[]{entry.getKey(),entry.getValue()}); + PriorityQueue pq = new PriorityQueue<>((pair1, pair2) -> pair2[1] - pair1[1]); + for (Map.Entry entry : map.entrySet()) {//大顶堆需要对所有元素进行排序 + pq.add(new int[]{entry.getKey(), entry.getValue()}); } int[] ans = new int[k]; - for(int i=0;i map = new HashMap<>();//key为数组元素值,val为对应出现次数 - for(int num:nums){ - map.put(num,map.getOrDefault(num,0)+1); + Map map = new HashMap<>(); //key为数组元素值,val为对应出现次数 + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); } - //在优先队列中存储二元组(num,cnt),cnt表示元素值num在数组中的出现次数 + //在优先队列中存储二元组(num, cnt),cnt表示元素值num在数组中的出现次数 //出现次数按从队头到队尾的顺序是从小到大排,出现次数最低的在队头(相当于小顶堆) - PriorityQueue pq = new PriorityQueue<>((pair1,pair2)->pair1[1]-pair2[1]); - for(Map.Entry entry:map.entrySet()){//小顶堆只需要维持k个元素有序 - if(pq.size()pq.peek()[1]){//当前元素出现次数大于小顶堆的根结点(这k个元素中出现次数最少的那个) - pq.poll();//弹出队头(小顶堆的根结点),即把堆里出现次数最少的那个删除,留下的就是出现次数多的了 - pq.add(new int[]{entry.getKey(),entry.getValue()}); + PriorityQueue pq = new PriorityQueue<>((pair1, pair2) -> pair1[1] - pair2[1]); + for (Map.Entry entry : map.entrySet()) { //小顶堆只需要维持k个元素有序 + if (pq.size() < k) { //小顶堆元素个数小于k个时直接加 + pq.add(new int[]{entry.getKey(), entry.getValue()}); + } else { + if (entry.getValue() > pq.peek()[1]) { //当前元素出现次数大于小顶堆的根结点(这k个元素中出现次数最少的那个) + pq.poll(); //弹出队头(小顶堆的根结点),即把堆里出现次数最少的那个删除,留下的就是出现次数多的了 + pq.add(new int[]{entry.getKey(), entry.getValue()}); } } } int[] ans = new int[k]; - for(int i=k-1;i>=0;i--){//依次弹出小顶堆,先弹出的是堆的根,出现次数少,后面弹出的出现次数多 + for (int i = k - 1; i >= 0; i--) { //依次弹出小顶堆,先弹出的是堆的根,出现次数少,后面弹出的出现次数多 ans[i] = pq.poll()[0]; } return ans; @@ -197,8 +197,8 @@ class Solution { PriorityQueue pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]); int[] res = new int[k]; // 答案数组为 k 个元素 Map map = new HashMap<>(); // 记录元素出现次数 - for(int num : nums) map.put(num, map.getOrDefault(num, 0) + 1); - for(var x : map.entrySet()) { // entrySet 获取 k-v Set 集合 + for (int num : nums) map.put(num, map.getOrDefault(num, 0) + 1); + for (var x : map.entrySet()) { // entrySet 获取 k-v Set 集合 // 将 kv 转化成数组 int[] tmp = new int[2]; tmp[0] = x.getKey(); @@ -209,7 +209,7 @@ class Solution { pq.poll(); } } - for(int i = 0; i < k; i ++) { + for (int i = 0; i < k; i++) { res[i] = pq.poll()[0]; // 获取优先队列里的元素 } return res; From 2cd982fd0dd478fb20989ee2dd8c1d28377f5829 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 15 Dec 2023 10:36:34 +0800 Subject: [PATCH 0853/1533] =?UTF-8?q?Update0040.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C2=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\210\346\200\273\345\222\214II.md" | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 33e4a46f51..994b04b82f 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -773,7 +773,39 @@ object Solution { } } ``` - +### C# +```csharp +public class Solution +{ + public List> res = new List>(); + public List path = new List(); + public IList> CombinationSum2(int[] candidates, int target) + { + + Array.Sort(candidates); + BackTracking(candidates, target, 0, 0); + return res; + } + public void BackTracking(int[] candidates, int target, int start, int sum) + { + if (sum > target) return; + if (sum == target) + { + res.Add(new List(path)); + return; + } + for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++) + { + if (i > start && candidates[i] == candidates[i - 1]) continue; + sum += candidates[i]; + path.Add(candidates[i]); + BackTracking(candidates, target, i + 1, sum); + sum -= candidates[i]; + path.RemoveAt(path.Count - 1); + } + } +} +```

From ed62561bc0c2e132380a2c5a548a87ad1461db3a Mon Sep 17 00:00:00 2001 From: RelishCoding <122661763+RelishCoding@users.noreply.github.com> Date: Fri, 15 Dec 2023 20:18:40 +0800 Subject: [PATCH 0854/1533] =?UTF-8?q?=E4=BC=98=E5=8C=96Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=BB=9A=E5=8A=A8=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 个人觉得原解法中有冗余代码,具体原因一开始放issue里了:https://github.com/youngyangyang04/leetcode-master/issues/2364 --- ...4.\347\233\256\346\240\207\345\222\214.md" | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 1a4dda2b0a..2d38b4d06a 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -275,19 +275,23 @@ class Solution { public int findTargetSumWays(int[] nums, int target) { int sum = 0; for (int i = 0; i < nums.length; i++) sum += nums[i]; - //如果target过大 sum将无法满足 - if ( target < 0 && sum < -target) return 0; - if ((target + sum) % 2 != 0) return 0; - int size = (target + sum) / 2; - if(size < 0) size = -size; - int[] dp = new int[size + 1]; + + //如果target的绝对值大于sum,那么是没有方案的 + if (Math.abs(target) > sum) return 0; + //如果(target+sum)除以2的余数不为0,也是没有方案的 + if ((target + sum) % 2 == 1) return 0; + + int bagSize = (target + sum) / 2; + int[] dp = new int[bagSize + 1]; dp[0] = 1; + for (int i = 0; i < nums.length; i++) { - for (int j = size; j >= nums[i]; j--) { + for (int j = bagSize; j >= nums[i]; j--) { dp[j] += dp[j - nums[i]]; } } - return dp[size]; + + return dp[bagSize]; } } ``` From 69a38ff1d30e4eb245f6dcebf5c5d177b08e3ea4 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 16 Dec 2023 09:26:57 +0800 Subject: [PATCH 0855/1533] =?UTF-8?q?Update0131.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\345\233\236\346\226\207\344\270\262.md" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 80ac284309..1d230ca8e4 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -847,6 +847,50 @@ object Solution { } } ``` +### CSharp +```csharp +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> Partition(string s) + { + BackTracking(s, 0); + return res; + } + public void BackTracking(string s, int start) + { + if (start >= s.Length) + { + res.Add(new List(path)); + return; + } + + for (int i = start; i < s.Length; i++) + { + if (IsPalindrome(s, start, i)) + { + path.Add(s.Substring(start, i - start + 1)); + } + else + { + continue; + } + BackTracking(s, i + 1); + path.RemoveAt(path.Count - 1); + } + } + public bool IsPalindrome(string s, int start, int end) + { + for (int i = start, j = end; i < j; i++, j--) + { + if (s[i] != s[j]) + return false; + } + return true; + } +} +```

From 1675ea6fbee7adb3b1aefd20ea4cb9aa7ff23232 Mon Sep 17 00:00:00 2001 From: HQWQF <47740975+HQWQF@users.noreply.github.com> Date: Sat, 16 Dec 2023 16:57:54 +0800 Subject: [PATCH 0856/1533] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md=20=E4=BF=AE=E5=A4=8D=E6=B2=A1=E6=9C=89delete=20dum?= =?UTF-8?q?myHead=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit new了一个虚拟头结点,但没有delete它 --- ...41\250\344\270\255\347\232\204\350\212\202\347\202\271.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index 57034f4795..405f4183e9 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -63,7 +63,9 @@ public: cur = cur->next->next; // cur移动两位,准备下一轮交换 } - return dummyHead->next; + ListNode* result = dummyHead->next; + delete dummyHead; + return result; } }; ``` From 32843da4dc1ed914ff5e030e1c09fd4d76b0ff73 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 17 Dec 2023 09:49:30 +0800 Subject: [PATCH 0857/1533] =?UTF-8?q?Update0093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\216\237IP\345\234\260\345\235\200.md" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 59cd92da59..c662957a10 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -799,6 +799,53 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public IList res = new List(); + public IList RestoreIpAddresses(string s) + { + if (s.Length < 4 || s.Length > 12) return res; + BackTracking(s, 0, 0); + return res; + } + public void BackTracking(string s, int start, int pointSum) + { + if (pointSum == 3) + { + if (IsValid(s, start, s.Length - 1)) + { + res.Add(s); + } + return; + } + for (int i = start; i < s.Length; i++) + { + if (IsValid(s, start, i)) + { + s = s.Insert(i + 1, "."); + BackTracking(s, i + 2, pointSum + 1); + s = s.Remove(i + 1, 1); + } + else break; + } + } + public bool IsValid(string s, int start, int end) + { + if (start > end) return false; + if (s[start] == '0' && start != end) return false; + int num = 0; + for (int i = start; i <= end; i++) + { + if (s[i] > '9' || s[i] < '0') return false; + num = num * 10 + s[i] - '0'; + if (num > 255) return false; + } + return true; + } +} +```

From 2d92b509e0689ce90e4460578de9772a459d3092 Mon Sep 17 00:00:00 2001 From: keepgogogo <57135073+keepgogogo@users.noreply.github.com> Date: Sun, 17 Dec 2023 14:00:00 +0800 Subject: [PATCH 0858/1533] =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=AF=87=209=20=E5=88=86=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2?= =?UTF-8?q?=EF=BC=8CJava=E9=A2=98=E8=A7=A3=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E4=BE=9B=E4=BD=BF=E7=94=A8=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=E4=BC=98=E5=8C=96=E5=9B=9E=E6=96=87=E4=B8=B2?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E7=9A=84=E5=AE=9E=E7=8E=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\345\233\236\346\226\207\344\270\262.md" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 1d230ca8e4..ca342d4bc6 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -349,6 +349,65 @@ class Solution { } ``` +### Java +回溯+动态规划优化回文串判断 +```Java +class Solution { + List> result; + LinkedList path; + boolean[][] dp; + + public List> partition(String s) { + result = new ArrayList<>(); + char[] str = s.toCharArray(); + path = new LinkedList<>(); + dp = new boolean[str.length + 1][str.length + 1]; + isPalindrome(str); + backtracking(s, 0); + return result; + } + + public void backtracking(String str, int startIndex) { + if (startIndex >= str.length()) { + //如果起始位置大于s的大小,说明找到了一组分割方案 + result.add(new ArrayList<>(path)); + } else { + for (int i = startIndex; i < str.length(); ++i) { + if (dp[startIndex][i]) { + //是回文子串,进入下一步递归 + //先将当前子串保存入path + path.addLast(str.substring(startIndex, i + 1)); + //起始位置后移,保证不重复 + backtracking(str, i + 1); + path.pollLast(); + } else { + //不是回文子串,跳过 + continue; + } + } + } + } + + //通过动态规划判断是否是回文串,参考动态规划篇 52 回文子串 + public void isPalindrome(char[] str) { + for (int i = 0; i <= str.length; ++i) { + dp[i][i] = true; + } + for (int i = 1; i < str.length; ++i) { + for (int j = i; j >= 0; --j) { + if (str[j] == str[i]) { + if (i - j <= 1) { + dp[j][i] = true; + } else if (dp[j + 1][i - 1]) { + dp[j][i] = true; + } + } + } + } + } +} +``` + ### Python 回溯 基本版 ```python From 5b5a05ada59333813312ebfe2f3f258cb9ea791f Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 18 Dec 2023 09:23:42 +0800 Subject: [PATCH 0859/1533] =?UTF-8?q?Update0078.=E5=AD=90=E9=9B=86?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0078.\345\255\220\351\233\206.md" | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 5f3654deae..06547e3df5 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -443,6 +443,27 @@ object Solution { } } ``` +### C# +```csharp +public class Solution { + public IList> res = new List>(); + public IList path = new List(); + public IList> Subsets(int[] nums) { + BackTracking(nums, 0); + return res; + } + public void BackTracking(int[] nums, int start){ + res.Add(new List(path)); + if(start > nums.Length) return; + for (int i = start; i < nums.Length; i++) + { + path.Add(nums[i]); + BackTracking(nums, i + 1); + path.RemoveAt(path.Count - 1); + } + } +} +```

From 7fdb7b17024bbfd7636dbc8100acb1687445b40b Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 19 Dec 2023 09:32:06 +0800 Subject: [PATCH 0860/1533] =?UTF-8?q?Update0090.=E5=AD=90=E9=9B=862?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0090.\345\255\220\351\233\206II.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 13080cd9e7..9fc334a4d8 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -640,6 +640,31 @@ object Solution { } } ``` +### C# +```c# +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> SubsetsWithDup(int[] nums) + { + Array.Sort(nums); + BackTracking(nums, 0); + return res; + } + public void BackTracking(int[] nums, int start) + { + res.Add(new List(path)); + for (int i = start; i < nums.Length; i++) + { + if (i > start && nums[i] == nums[i - 1]) continue; + path.Add(nums[i]); + BackTracking(nums, i + 1); + path.RemoveAt(path.Count - 1); + } + } +} +```

From f9342095c9b7d79d8c6debc165a193ddc3e39169 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 20 Dec 2023 09:26:14 +0800 Subject: [PATCH 0861/1533] =?UTF-8?q?Update0491.=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\345\255\220\345\272\217\345\210\227.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index eb0be005ed..1aa69a3669 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -614,6 +614,32 @@ object Solution { } } ``` +### C# +```csharp +public class Solution { + public IList> res = new List>(); + public IList path = new List(); + public IList> FindSubsequences(int[] nums) { + BackTracking(nums, 0); + return res; + } + public void BackTracking(int[] nums, int start){ + if(path.Count >= 2){ + res.Add(new List(path)); + } + HashSet hs = new HashSet(); + for(int i = start; i < nums.Length; i++){ + if(path.Count > 0 && path[path.Count - 1] > nums[i] || hs.Contains(nums[i])){ + continue; + } + hs.Add(nums[i]); + path.Add(nums[i]); + BackTracking(nums, i + 1); + path.RemoveAt(path.Count - 1); + } + } +} +```

From 4ee74f12892497dce83ba6a2d7c74056d2d9216b Mon Sep 17 00:00:00 2001 From: block <1181882120@qq.com> Date: Wed, 20 Dec 2023 18:11:23 +0800 Subject: [PATCH 0862/1533] Correction document statement --- ...\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index dff1823c0c..f22c67b137 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -65,7 +65,7 @@ **回溯法解决的问题都可以抽象为树形结构**,是的,我指的是所有回溯法的问题都可以抽象为树形结构! -因为回溯法解决的都是在集合中递归查找子集,**集合的大小就构成了树的宽度,递归的深度,都构成的树的深度**。 +因为回溯法解决的都是在集合中递归查找子集,**集合的大小就构成了树的宽度,递归的深度就构成了树的深度**。 递归就要有终止条件,所以必然是一棵高度有限的树(N叉树)。 From eff7a7a9b27cf76d530a253bdfb0b9824e768a3a Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 21 Dec 2023 09:36:09 +0800 Subject: [PATCH 0863/1533] =?UTF-8?q?Update0046.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6.\345\205\250\346\216\222\345\210\227.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index 1f5263a79e..15e6ae162a 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -486,6 +486,37 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public IList> res = new List>(); + public IList path = new List(); + public IList> Permute(int[] nums) + { + var used = new bool[nums.Length]; + BackTracking(nums, used); + return res; + } + public void BackTracking(int[] nums, bool[] used) + { + if (path.Count == nums.Length) + { + res.Add(new List(path)); + return; + } + for (int i = 0; i < nums.Length; i++) + { + if (used[i]) continue; + used[i] = true; + path.Add(nums[i]); + BackTracking(nums, used); + used[i] = false; + path.RemoveAt(path.Count - 1); + } + } +} +```

From 25a330fcdbc4b2e6958efa51143ce31f476dc60a Mon Sep 17 00:00:00 2001 From: cherrypicker Date: Thu, 21 Dec 2023 15:29:18 +0800 Subject: [PATCH 0864/1533] =?UTF-8?q?Update0704.=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=9F=A5=E6=89=BE=20Go=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加注释、计算区间中点mid时使用位运算代替除法运算 --- ...14\345\210\206\346\237\245\346\211\276.md" | 72 ++++++++++++------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 590ec1b139..c3783aa33f 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -251,40 +251,60 @@ class Solution: (版本一)左闭右闭区间 ```go +// 时间复杂度 O(logn) func search(nums []int, target int) int { - high := len(nums)-1 - low := 0 - for low <= high { - mid := low + (high-low)/2 - if nums[mid] == target { - return mid - } else if nums[mid] > target { - high = mid-1 - } else { - low = mid+1 - } - } - return -1 + // 初始化左右边界 + left := 0 + right := len(nums) - 1 + + // 循环逐步缩小区间范围 + for left <= right { + // 求区间中点 + mid := left + (right-left)>>1 + + // 根据 nums[mid] 和 target 的大小关系 + // 调整区间范围 + if nums[mid] == target { + return mid + } else if nums[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } + } + + // 在输入数组内没有找到值等于 target 的元素 + return -1 } ``` (版本二)左闭右开区间 ```go +// 时间复杂度 O(logn) func search(nums []int, target int) int { - high := len(nums) - low := 0 - for low < high { - mid := low + (high-low)/2 - if nums[mid] == target { - return mid - } else if nums[mid] > target { - high = mid - } else { - low = mid+1 - } - } - return -1 + // 初始化左右边界 + left := 0 + right := len(nums) + + // 循环逐步缩小区间范围 + for left < right { + // 求区间中点 + mid := left + (right-left)>>1 + + // 根据 nums[mid] 和 target 的大小关系 + // 调整区间范围 + if nums[mid] == target { + return mid + } else if nums[mid] < target { + left = mid + 1 + } else { + right = mid + } + } + + // 在输入数组内没有找到值等于 target 的元素 + return -1 } ``` From 4516aaab6096e84f5fd7b526a135225c1b862570 Mon Sep 17 00:00:00 2001 From: cherrypicker Date: Thu, 21 Dec 2023 15:36:44 +0800 Subject: [PATCH 0865/1533] =?UTF-8?q?Update0027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=20Go=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加注释、代码格式化 --- ...73\351\231\244\345\205\203\347\264\240.md" | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 8522e785b9..dbde3d198c 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -256,17 +256,24 @@ class Solution: ### Go: ```go +// 快慢指针法 +// 时间复杂度 O(n) +// 空间复杂度 O(1) func removeElement(nums []int, val int) int { - length:=len(nums) - res:=0 - for i:=0;i Date: Thu, 21 Dec 2023 14:22:44 -0800 Subject: [PATCH 0866/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Kama54.=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E5=AD=97=E7=9A=84Python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\233\277\346\215\242\346\225\260\345\255\227.md" | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 0f8daa2175..2b3d53de22 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -235,7 +235,15 @@ func main(){ ### python: - +```Python +class Solution: + def change(self, s): + lst = list(s) # Python里面的string也是不可改的,所以也是需要额外空间的。空间复杂度:O(n)。 + for i in range(len(lst)): + if lst[i].isdigit(): + lst[i] = "number" + return ''.join(lst) +``` ### JavaScript: From 1fbe05532511a96e48b118172ed5f9e2f21ad7c6 Mon Sep 17 00:00:00 2001 From: cherrypicker Date: Fri, 22 Dec 2023 10:02:23 +0800 Subject: [PATCH 0867/1533] =?UTF-8?q?Update0232.=E7=94=A8=E6=A0=88?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97=20=E4=BC=98=E5=8C=96Go?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用切片模拟栈的实现,即 MyStack --- ...36\347\216\260\351\230\237\345\210\227.md" | 85 ++++++++++++------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 2437401060..41933ca4ba 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -235,52 +235,77 @@ class MyQueue: ### Go: ```Go +// 通过切片实现一个栈 +// 由于只是辅助实现算法题目,因此不做异常情况处理 +type MyStack []int + +func (s *MyStack) Push(v int) { + *s = append(*s, v) +} + +func (s *MyStack) Pop() int { + val := (*s)[len(*s)-1] + *s = (*s)[:len(*s)-1] + return val +} + +func (s *MyStack) Peek() int { + return (*s)[len(*s)-1] +} + +func (s *MyStack) Size() int { + return len(*s) +} + +func (s *MyStack) Empty() bool { + return s.Size() == 0 +} + +// ---------- 分界线 ---------- + type MyQueue struct { - stackIn []int //输入栈 - stackOut []int //输出栈 + stackIn *MyStack + stackOut *MyStack } + func Constructor() MyQueue { - return MyQueue{ - stackIn: make([]int, 0), - stackOut: make([]int, 0), + return MyQueue { + stackIn: &MyStack{}, + stackOut: &MyStack{}, } } -// 往输入栈做push -func (this *MyQueue) Push(x int) { - this.stackIn = append(this.stackIn, x) + +func (this *MyQueue) Push(x int) { + this.stackIn.Push(x) } -// 在输出栈做pop,pop时如果输出栈数据为空,需要将输入栈全部数据导入,如果非空,则可直接使用 + func (this *MyQueue) Pop() int { - inLen, outLen := len(this.stackIn), len(this.stackOut) - if outLen == 0 { - if inLen == 0 { - return -1 - } - for i := inLen - 1; i >= 0; i-- { - this.stackOut = append(this.stackOut, this.stackIn[i]) - } - this.stackIn = []int{} //导出后清空 - outLen = len(this.stackOut) //更新长度值 - } - val := this.stackOut[outLen-1] - this.stackOut = this.stackOut[:outLen-1] - return val + this.fillStackOut() + return this.stackOut.Pop() } + func (this *MyQueue) Peek() int { - val := this.Pop() - if val == -1 { - return -1 - } - this.stackOut = append(this.stackOut, val) - return val + this.fillStackOut() + return this.stackOut.Peek() } + func (this *MyQueue) Empty() bool { - return len(this.stackIn) == 0 && len(this.stackOut) == 0 + return this.stackIn.Empty() && this.stackOut.Empty() +} + +// fillStackOut 填充输出栈 +func (this *MyQueue) fillStackOut() { + if this.stackOut.Empty() { + for !this.stackIn.Empty() { + val := this.stackIn.Pop() + this.stackOut.Push(val) + } + } } ``` From f134c39c868c3b8a48389db6e11fe412904087b2 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 22 Dec 2023 10:17:21 +0800 Subject: [PATCH 0868/1533] =?UTF-8?q?Update=200047.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=972=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\205\250\346\216\222\345\210\227II.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index 4fed8a5c8c..7f2c363889 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -521,6 +521,38 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public List> res = new List>(); + public List path = new List(); + public IList> PermuteUnique(int[] nums) + { + Array.Sort(nums); + BackTracking(nums, new bool[nums.Length]); + return res; + } + public void BackTracking(int[] nums, bool[] used) + { + if (nums.Length == path.Count) + { + res.Add(new List(path)); + return; + } + for (int i = 0; i < nums.Length; i++) + { + if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) continue; + if (used[i]) continue; + path.Add(nums[i]); + used[i] = true; + BackTracking(nums, used); + path.RemoveAt(path.Count - 1); + used[i] = false; + } + } +} +```

From 9319ce9cdcab03027644ce1cff57b4060d53bc99 Mon Sep 17 00:00:00 2001 From: cherrypicker Date: Sat, 23 Dec 2023 08:34:38 +0800 Subject: [PATCH 0869/1533] =?UTF-8?q?Update0020.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E6=8B=AC=E5=8F=B7=20Go=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原有的实现已经足够简洁清晰,这个版本的代码增加了注释,提升了可读性。 --- ...10\347\232\204\346\213\254\345\217\267.md" | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index 045c79ee7f..17fbe2be2d 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -218,23 +218,41 @@ class Solution: ### Go: ```Go +// 思路: 使用栈来进行括号的匹配 +// 时间复杂度 O(n) +// 空间复杂度 O(n) func isValid(s string) bool { - hash := map[byte]byte{')':'(', ']':'[', '}':'{'} - stack := make([]byte, 0) - if s == "" { - return true - } - - for i := 0; i < len(s); i++ { - if s[i] == '(' || s[i] == '[' || s[i] == '{' { - stack = append(stack, s[i]) - } else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] { - stack = stack[:len(stack)-1] - } else { - return false - } - } - return len(stack) == 0 + // 使用切片模拟栈的行为 + stack := make([]rune, 0) + + // m 用于记录某个右括号对应的左括号 + m := make(map[rune]rune) + m[')'] = '(' + m[']'] = '[' + m['}'] = '{' + + // 遍历字符串中的 rune + for _, c := range s { + // 左括号直接入栈 + if c == '(' || c == '[' || c == '{' { + stack = append(stack, c) + } else { + // 如果是右括号,先判断栈内是否还有元素 + if len(stack) == 0 { + return false + } + // 再判断栈顶元素是否能够匹配 + peek := stack[len(stack)-1] + if peek != m[c] { + return false + } + // 模拟栈顶弹出 + stack = stack[:len(stack)-1] + } + } + + // 若栈中不再包含元素,则能完全匹配 + return len(stack) == 0 } ``` From 69103f6c86fddc3d10ffe921dce8b93e259f71e2 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 23 Dec 2023 10:11:06 +0800 Subject: [PATCH 0870/1533] =?UTF-8?q?Update0051.N=E7=9A=87=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index f629079372..1e1085401d 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -865,6 +865,60 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public List> res = new(); + public IList> SolveNQueens(int n) + { + char[][] chessBoard = new char[n][]; + for (int i = 0; i < n; i++) + { + chessBoard[i] = new char[n]; + for (int j = 0; j < n; j++) + { + chessBoard[i][j] = '.'; + } + } + BackTracking(n, 0, chessBoard); + return res; + } + public void BackTracking(int n, int row, char[][] chessBoard) + { + if (row == n) + { + res.Add(chessBoard.Select(x => new string(x)).ToList()); + return; + } + for (int col = 0; col < n; col++) + { + if (IsValid(row, col, chessBoard, n)) + { + chessBoard[row][col] = 'Q'; + BackTracking(n, row + 1, chessBoard); + chessBoard[row][col] = '.'; + } + } + } + public bool IsValid(int row, int col, char[][] chessBoard, int n) + { + for (int i = 0; i < row; i++) + { + if (chessBoard[i][col] == 'Q') return false; + } + for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) + { + if (chessBoard[i][j] == 'Q') return false; + } + for (int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) + { + if (chessBoard[i][j] == 'Q') return false; + } + return true; + } +} +```

From 3bd387df7cde50b6fada28aa824841f4c842921d Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 24 Dec 2023 09:42:46 +0800 Subject: [PATCH 0871/1533] =?UTF-8?q?Update0037.=E8=A7=A3=E6=95=B0?= =?UTF-8?q?=E7=8B=AC=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\350\247\243\346\225\260\347\213\254.md" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index 1763063eb7..d96e59dfeb 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -756,6 +756,59 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public void SolveSudoku(char[][] board) + { + BackTracking(board); + } + public bool BackTracking(char[][] board) + { + for (int i = 0; i < board.Length; i++) + { + for (int j = 0; j < board[0].Length; j++) + { + if (board[i][j] != '.') continue; + for (char k = '1'; k <= '9'; k++) + { + if (IsValid(board, i, j, k)) + { + board[i][j] = k; + if (BackTracking(board)) return true; + board[i][j] = '.'; + } + } + return false; + } + + } + return true; + } + public bool IsValid(char[][] board, int row, int col, char val) + { + for (int i = 0; i < 9; i++) + { + if (board[i][col] == val) return false; + } + for (int i = 0; i < 9; i++) + { + if (board[row][i] == val) return false; + } + int startRow = (row / 3) * 3; + int startCol = (col / 3) * 3; + for (int i = startRow; i < startRow + 3; i++) + { + for (int j = startCol; j < startCol + 3; j++) + { + if (board[i][j] == val) return false; + } + } + return true; + } +} +```

From 689c086f3a4094e3eeb5e639b69126c29b626561 Mon Sep 17 00:00:00 2001 From: ZhangLiPeng <2353375282@qq.com> Date: Sun, 24 Dec 2023 14:44:08 +0800 Subject: [PATCH 0872/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E6=80=BB=E7=BB=93=E7=AF=87.md=20=E7=9A=84=E6=96=87?= =?UTF-8?q?=E5=AD=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index 3f2f5c9792..72d2b7ad87 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -22,7 +22,7 @@ ### 虚拟头结点 -在[链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html)中,我们讲解了链表操作中一个非常总要的技巧:虚拟头节点。 +在[链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html)中,我们讲解了链表操作中一个非常重要的技巧:虚拟头节点。 链表的一大问题就是操作当前节点必须要找前一个节点才能操作。这就造成了,头结点的尴尬,因为头结点没有前一个节点了。 From 5fd8c8e6a7d2c8a5f25a0c5aaa0a942f1be3a24f Mon Sep 17 00:00:00 2001 From: ZhangLiPeng <2353375282@qq.com> Date: Sun, 24 Dec 2023 17:10:16 +0800 Subject: [PATCH 0873/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E6=80=BB=E7=BB=93=E7=AF=87.md=20=E7=9A=84=E6=96=87?= =?UTF-8?q?=E5=AD=97=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index 72d2b7ad87..b2b1b7795f 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -32,7 +32,7 @@ ### 链表的基本操作 -在[链表:一道题目考察了常见的五个操作!](https://programmercarl.com/0707.设计链表.html)中,我们通设计链表把链表常见的五个操作练习了一遍。 +在[链表:一道题目考察了常见的五个操作!](https://programmercarl.com/0707.设计链表.html)中,我们通过设计链表把链表常见的五个操作练习了一遍。 这是练习链表基础操作的非常好的一道题目,考察了: From 9cea1567fe338ec4842276c4e27e9d4933893bd3 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 25 Dec 2023 09:35:23 +0800 Subject: [PATCH 0874/1533] =?UTF-8?q?Update0455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\217\221\351\245\274\345\271\262.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index c9c1a8525d..778adc94a3 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -378,6 +378,28 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindContentChildren(int[] g, int[] s) + { + Array.Sort(g); + Array.Sort(s); + int index = s.Length - 1; + int res = 0; + for (int i = g.Length - 1; i >=0; i--) + { + if(index >=0 && s[index]>=g[i]) + { + res++; + index--; + } + } + return res; + } +} +```

From 95e8b279aef0dab41fce7210ad4f80fd74f8dca8 Mon Sep 17 00:00:00 2001 From: markwang Date: Mon, 25 Dec 2023 15:36:54 +0800 Subject: [PATCH 0875/1533] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.md" | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index b27a231e77..08d25080db 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -48,7 +48,7 @@ 在有序树里,如果判断一个节点的左子树里有p,右子树里有q呢? -因为是有序树,所有 如果 中间节点是 q 和 p 的公共祖先,那么 中节点的数组 一定是在 [p, q]区间的。即 中节点 > p && 中节点 < q 或者 中节点 > q && 中节点 < p。 +因为是有序树,所以 如果 中间节点是 q 和 p 的公共祖先,那么 中节点的数组 一定是在 [p, q]区间的。即 中节点 > p && 中节点 < q 或者 中节点 > q && 中节点 < p。 那么只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是p 和 q的公共祖先。 那问题来了,**一定是最近公共祖先吗**? @@ -328,28 +328,35 @@ class Solution: ``` ### Go -递归法: +递归法 ```go func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { - if root == nil { - return nil + if root.Val > p.Val && root.Val > q.Val { + return lowestCommonAncestor(root.Left, p, q) + } else if root.Val < p.Val && root.Val < q.Val { + return lowestCommonAncestor(root.Right, p, q) + } else { + return root } - for { - if root.Val > p.Val && root.Val > q.Val { +} +``` + +迭代法 +```go +func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { + for root != nil { + if root.Val > p.Val && root.Val > q.Val { root = root.Left - } - if root.Val < p.Val && root.Val < q.Val { + } else if root.Val < p.Val && root.Val < q.Val { root = root.Right - } - if (root.Val - p.Val) * (root.Val - q.Val) <= 0 { + } else { return root } } - return root + return nil } ``` - ### JavaScript 递归法: From b22425c859269c5862149a8f736bcf0c3e5e00eb Mon Sep 17 00:00:00 2001 From: Relsola Date: Mon, 25 Dec 2023 22:55:12 +0800 Subject: [PATCH 0876/1533] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=90=8D=E9=94=99=E8=AF=AF=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=92=8C=E5=8F=AF=E8=AF=BB=E6=80=A7=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 97dab4f398..b836705ab5 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -515,27 +515,31 @@ func largestRectangleArea(heights []int) int { var largestRectangleArea = function(heights) { const len = heights.length; const minLeftIndex = new Array(len); - const maxRigthIndex = new Array(len); + const maxRightIndex = new Array(len); // 记录每个柱子 左边第一个小于该柱子的下标 minLeftIndex[0] = -1; // 注意这里初始化,防止下面while死循环 for(let i = 1; i < len; i++) { let t = i - 1; // 这里不是用if,而是不断向左寻找的过程 - while(t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t]; + while (t >= 0 && heights[t] >= heights[i]) { + t = minLeftIndex[t]; + } minLeftIndex[i] = t; } // 记录每个柱子 右边第一个小于该柱子的下标 - maxRigthIndex[len - 1] = len; // 注意这里初始化,防止下面while死循环 + maxRightIndex[len - 1] = len; // 注意这里初始化,防止下面while死循环 for(let i = len - 2; i >= 0; i--){ let t = i + 1; // 这里不是用if,而是不断向右寻找的过程 - while(t < len && heights[t] >= heights[i]) t = maxRigthIndex[t]; - maxRigthIndex[i] = t; + while (t <= n && heights[t] > heights[i]) { + t = maxRightIndex[t]; + } + maxRightIndex[i] = t; } // 求和 let maxArea = 0; for(let i = 0; i < len; i++){ - let sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1); + let sum = heights[i] * (maxRightIndex[i] - minLeftIndex[i] - 1); maxArea = Math.max(maxArea , sum); } return maxArea; @@ -543,27 +547,37 @@ var largestRectangleArea = function(heights) { //单调栈 var largestRectangleArea = function(heights) { - let maxArea = 0; - const stack = []; - heights = [0,...heights,0]; // 数组头部加入元素0 数组尾部加入元素0 - for(let i = 0; i < heights.length; i++){ - if(heights[i] > heights[stack[stack.length-1]]){ // 情况三 - stack.push(i); - } else if(heights[i] === heights[stack[stack.length-1]]){ // 情况二 - stack.pop(); // 这个可以加,可以不加,效果一样,思路不同 - stack.push(i); - } else { // 情况一 - while(heights[i] < heights[stack[stack.length-1]]){// 当前bar比栈顶bar矮 - const stackTopIndex = stack.pop();// 栈顶元素出栈,并保存栈顶bar的索引 - let w = i - stack[stack.length -1] - 1; - let h = heights[stackTopIndex] + let maxArea = 0; + const stack = [0]; + heights.push(0); + const n = heights.length; + + for (let i = 1; i < n; i++) { + let top = stack.at(-1); + // 情况三 + if (heights[top] < heights[i]) { + stack.push(i); + } + // 情况二 + if (heights[top] === heights[i]) { + stack.pop(); // 这个可以加,可以不加,效果一样,思路不同 + stack.push(i); + } + // 情况一 + if (heights[top] > heights[i]) { + while (stack.length > 0 && heights[top] > heights[i]) { + // 栈顶元素出栈,并保存栈顶bar的索引 + const h = heights[stack.pop()]; + const left = stack.at(-1) ?? -1; + const w = i - left - 1; // 计算面积,并取最大面积 - maxArea = Math.max(maxArea, w * h); - } - stack.push(i);// 当前bar比栈顶bar高了,入栈 - } - } - return maxArea; + maxArea = Math.max(maxArea, w * h); + top = stack.at(-1); + } + stack.push(i); + } + } + return maxArea; }; //单调栈 简洁 From b01155fbf24ee11b8a45aa9b7e0f67f2398509c2 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 26 Dec 2023 09:11:30 +0800 Subject: [PATCH 0877/1533] =?UTF-8?q?docs:=E4=BF=AE=E6=94=B9=E9=94=99?= =?UTF-8?q?=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 5203d7d61a..ceea31fe50 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -64,7 +64,7 @@ 在计算是否有峰值的时候,大家知道遍历的下标 i ,计算 prediff(nums[i] - nums[i-1]) 和 curdiff(nums[i+1] - nums[i]),如果`prediff < 0 && curdiff > 0` 或者 `prediff > 0 && curdiff < 0` 此时就有波动就需要统计。 -这是我们思考本题的一个大题思路,但本题要考虑三种情况: +这是我们思考本题的一个大体思路,但本题要考虑三种情况: 1. 情况一:上下坡中有平坡 2. 情况二:数组首尾两端 From 7924803206a0dc2b06b6d6f3c7281d9f56970a71 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 26 Dec 2023 09:12:03 +0800 Subject: [PATCH 0878/1533] =?UTF-8?q?Update0376.=E6=91=86=E5=8A=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\212\250\345\272\217\345\210\227.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index ceea31fe50..5c2241c805 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -692,6 +692,27 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int WiggleMaxLength(int[] nums) + { + if (nums.Length < 2) return nums.Length; + int curDiff = 0, preDiff = 0, res = 1; + for (int i = 0; i < nums.Length - 1; i++) + { + curDiff = nums[i + 1] - nums[i]; + if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) + { + res++; + preDiff = curDiff; + } + } + return res; + } +} +```

From 54c3a74618182fc480334e1105bc3030948e208d Mon Sep 17 00:00:00 2001 From: block <1181882120@qq.com> Date: Tue, 26 Dec 2023 10:52:24 +0800 Subject: [PATCH 0879/1533] Update max array doc --- ...\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index bed11c7a19..27cabdc559 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -16,7 +16,7 @@ 示例 1: * 输入:A = [4,2,3], K = 1 * 输出:5 -* 解释:选择索引 (1,) ,然后 A 变为 [4,-2,3]。 +* 解释:选择索引 (1) ,然后 A 变为 [4,-2,3]。 示例 2: * 输入:A = [3,-1,0,2], K = 3 From a937b7674623ecbbe24e61d20d6af7d771ab6304 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 27 Dec 2023 09:49:34 +0800 Subject: [PATCH 0880/1533] =?UTF-8?q?Update0053.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E8=B4=AA=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\345\255\220\345\272\217\345\222\214.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 639c54bc7e..78c8b38222 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -406,6 +406,26 @@ object Solution { } } ``` +### C# +**贪心** +```csharp +public class Solution +{ + public int MaxSubArray(int[] nums) + { + int res = Int32.MinValue; + int count = 0; + for (int i = 0; i < nums.Length; i++) + { + count += nums[i]; + res = Math.Max(res, count); + if (count < 0) count = 0; + } + return res; + } +} +``` +

From 2368a622773736ee74f8d432f8cb891ec2f3c74c Mon Sep 17 00:00:00 2001 From: Relsola Date: Wed, 27 Dec 2023 19:23:37 +0800 Subject: [PATCH 0881/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9ETS=E6=B7=B1=E5=BA=A6=E6=90=9C=E7=B4=A2=E4=BC=98?= =?UTF-8?q?=E5=85=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 43eb66e172..a4b93c3d4e 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -346,6 +346,46 @@ var numIslands = function (grid) { }; ``` +### TypeScript + +```TypeScript +function numIslands(grid: string[][]): number { + // 四个方向 + const dir: number[][] = [[0, 1], [1, 0], [-1, 0], [0, -1]]; + const [m, n]: [number, number] = [grid.length, grid[0].length]; + + function dfs(grid: string[][], visited: boolean[][], x: number, y: number) { + for (let i = 0; i < 4; i++) { + let nextX: number = x + dir[i][0]; + let nextY: number = y + dir[i][1]; + // 越界了,直接跳过 + if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) { + continue; + } + // 没有访问过同时是陆地 + if (!visited[nextX][nextY] && grid[nextX][nextY] === '1') { + visited[nextX][nextY] = true; + dfs(grid, visited, nextX, nextY); + } + } + } + + const visited: boolean[][] = Array.from({ length: m }, _ => new Array(n).fill(false)); + + let result: number = 0; + for (let i = 0; i < m; i++) { + for (let k = 0; k < n; k++) { + if (!visited[i][k] && grid[i][k] === '1') { + ++result; // 遇到没访问过的陆地,+1 + visited[i][k] = true; + dfs(grid, visited, i, k); // 将与其链接的陆地都标记上 true + } + } + } + return result; +} +``` + ### Go ```go From 22ebe65604be3b53c53503f64d1aa7ed3b0b220f Mon Sep 17 00:00:00 2001 From: Relsola Date: Wed, 27 Dec 2023 19:51:37 +0800 Subject: [PATCH 0882/1533] =?UTF-8?q?Update=20200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\345\271\277\346\220\234\347\211\210.md" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index e8ed60db68..a7dd117b3e 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -276,6 +276,52 @@ var numIslands = function (grid) { }; ``` +### TypeScript + +```TypeScript +function numIslands2(grid: string[][]): number { + // 四个方向 + const dir: number[][] = [[0, 1], [1, 0], [-1, 0], [0, -1]]; + const [m, n]: [number, number] = [grid.length, grid[0].length]; + + function dfs(grid: string[][], visited: boolean[][], x: number, y: number) { + const queue: number[][] = [[x, y]]; + while (queue.length !== 0) { + //取出队列头部元素 + const top: number[] = queue.shift()!; + for (let i = 0; i < 4; i++) { + const nextX: number = top[0] + dir[i][0]; + const nextY: number = top[1] + dir[i][1]; + // 越界了,直接跳过 + if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) { + continue; + } + if (!visited[nextX][nextY] && grid[nextX][nextY] === '1') { + queue.push([nextX, nextY]); + // 只要加入队列立刻标记 + visited[nextX][nextY] = true; + } + } + } + } + + const visited: boolean[][] = Array.from({ length: m }, _ => new Array(n).fill(false)); + + let result = 0; + for (let i = 0; i < m; i++) { + for (let k = 0; k < n; k++) { + if (!visited[i][k] && grid[i][k] === '1') { + ++result; // 遇到没访问过的陆地,+1 + visited[i][k] = true; + dfs(grid, visited, i, k); // 将与其链接的陆地都标记上 true + } + } + } + return result; +} +``` + + ### Rust ```rust From 6eadd3eafc7c55b2bd945041e4752c09b20b69ed Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 28 Dec 2023 10:03:01 +0800 Subject: [PATCH 0883/1533] =?UTF-8?q?Update.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA2?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\200\344\275\263\346\227\266\346\234\272II.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index 2c2ab225fd..69706e369a 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -406,6 +406,21 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int MaxProfit(int[] prices) + { + int res = 0; + for (int i = 0; i < prices.Length - 1; i++) + { + res += Math.Max(0, prices[i + 1] - prices[i]); + } + return res; + } +} +```

From c0c03a88831bc6b1ed20e8cedb068cec7e624181 Mon Sep 17 00:00:00 2001 From: ENJOY <954395302@qq.com> Date: Thu, 28 Dec 2023 16:03:23 +0800 Subject: [PATCH 0884/1533] =?UTF-8?q?0704.=E4=BA=8C=E5=88=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE.md=EF=BC=9A=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index c3783aa33f..31e89ae344 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -2,7 +2,7 @@ -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 704. 二分查找 From b997d8108cd8fcf7b00e4b073f301f0bb9c447d2 Mon Sep 17 00:00:00 2001 From: ENJOY <954395302@qq.com> Date: Thu, 28 Dec 2023 16:49:49 +0800 Subject: [PATCH 0885/1533] =?UTF-8?q?=E5=85=B3=E4=BA=8E=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=EF=BC=8C=E4=BD=A0=E4=B8=8D=E7=9F=A5?= =?UTF-8?q?=E9=81=93=E7=9A=84=E9=83=BD=E5=9C=A8=E8=BF=99=E9=87=8C=EF=BC=81?= =?UTF-8?q?.md=EF=BC=9A=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...234\250\350\277\231\351\207\214\357\274\201.md" | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index d212e8beae..a1e553d0bc 100644 --- "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -10,7 +10,7 @@ **时间复杂度是一个函数,它定性描述该算法的运行时间**。 -我们在软件开发中,时间复杂度就是用来方便开发者估算出程序运行的答题时间。 +我们在软件开发中,时间复杂度就是用来方便开发者估算出程序运行的大体时间。 那么该如何估计程序运行时间呢,通常会估算算法的操作单元数量来代表程序消耗的时间,这里默认CPU的每个单元运行消耗的时间都是相同的。 @@ -80,13 +80,13 @@ O(2*n^2 + 10*n) O(n^2 + n) ``` -只保留保留最高项,去掉数量级小一级的n (因为n^2 的数据规模远大于n),最终简化为: +只保留最高项,去掉数量级小一级的n (因为n^2 的数据规模远大于n),最终简化为: ``` O(n^2) ``` -如果这一步理解有困难,那也可以做提取n的操作,变成O(n(n+1)),省略加法常数项后也就别变成了: +如果这一步理解有困难,那也可以做提取n的操作,变成O(n(n+1)),省略加法常数项后也就变成了: ``` O(n^2) @@ -96,7 +96,7 @@ O(n^2) 也可以用另一种简化的思路,其实当n大于40的时候, 这个复杂度会恒小于O(3 × n^2), -O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项系数最终时间复杂度也是O(n^2)。 +O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),3 × n^2省略掉常数项系数,最终时间复杂度也是O(n^2)。 ## O(log n)中的log是以什么为底? @@ -127,7 +127,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 接下来再想一下其他解题思路。 -先排对n个字符串按字典序来排序,排序后n个字符串就是有序的,意味着两个相同的字符串就是挨在一起,然后在遍历一遍n个字符串,这样就找到两个相同的字符串了。 +先对n个字符串按字典序来排序,排序后n个字符串就是有序的,意味着两个相同的字符串就是挨在一起,然后再遍历一遍n个字符串,这样就找到两个相同的字符串了。 那看看这种算法的时间复杂度,快速排序时间复杂度为O(nlog n),依然要考虑字符串的长度是m,那么快速排序每次的比较都要有m次的字符比较的操作,就是O(m × n × log n)。 @@ -139,13 +139,13 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 所以先把字符串集合排序再遍历一遍找到两个相同字符串的方法要比直接暴力枚举的方式更快。 -这就是我们通过分析两种算法的时间复杂度得来的。 +这就是我们通过分析两种算法的时间复杂度得来的结论。 **当然这不是这道题目的最优解,我仅仅是用这道题目来讲解一下时间复杂度**。 ## 总结 -本篇讲解了什么是时间复杂度,复杂度是用来干什么,以及数据规模对时间复杂度的影响。 +本篇讲解了什么是时间复杂度,复杂度是用来干什么的,以及数据规模对时间复杂度的影响。 还讲解了被大多数同学忽略的大O的定义以及log究竟是以谁为底的问题。 From df11fce6e858ba356eb0a5c8c400634f37188434 Mon Sep 17 00:00:00 2001 From: wangzilong Date: Fri, 29 Dec 2023 10:56:48 +0800 Subject: [PATCH 0886/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20go=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=A2=9E=E5=8A=A0=E7=9B=B4=E6=8E=A5=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=8E=9F=E9=93=BE=E8=A1=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 780f9c36ea..d6d7e6c2ef 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -330,7 +330,40 @@ class Solution: ``` ### Go: +直接使用原链表 +```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func removeElements(head *ListNode, val int) *ListNode { + + //依旧是先定义逻辑 + + //如果原链表的头节点为val的话,head=head.next,且为持续过程,防止头节点后面的节点也为Val + //这里前置循环 并且要判定head 是否为nil,防止出错 + for head != nil && head.Val == val {//由于leetcode代码运行方式,for循环条件判断前后顺序不能修改,下面的for循环也同样如此 + head = head.Next + } + cur := head + + for cur != nil && cur.Next != nil { + if cur.Next.Val == val { + cur.Next = cur.Next.Next + } else { + cur = cur.Next + } + } + return head + +} + +``` +虚拟头节点方式: ```go /** * Definition for singly-linked list. From 71f51fcde4f28abedc24f46c923e9a47b21799b9 Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Fri, 29 Dec 2023 20:15:58 +1300 Subject: [PATCH 0887/1533] =?UTF-8?q?Update=20=E9=93=BE=E8=A1=A8=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20=E6=B7=BB=E5=8A=A0C#=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 9dc4524269..88e41d7d3b 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -250,9 +250,32 @@ typedef struct ListNodeT { } ListNode; ``` +### C# + +```c# +public class Node +{ + // 节点存储的数据 + public T Data { get; set; } + + // 指向下一个节点的引用 + public Node Next { get; set; } + + // 节点的构造函数,用于初始化节点 + public Node(T data) + { + Data = data; + Next = null; // 初始时没有下一个节点,因此设为 null + } +} +``` + + + + +

- From e209d3514302398515061906102cf3a42a4588ad Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 29 Dec 2023 16:45:31 +0800 Subject: [PATCH 0888/1533] =?UTF-8?q?Update0055.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8F=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\263\350\267\203\346\270\270\346\210\217.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index bedb09abee..086fd64f5e 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -258,6 +258,23 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public bool CanJump(int[] nums) + { + int cover = 0; + if (nums.Length == 1) return true; + for (int i = 0; i <= cover; i++) + { + cover = Math.Max(i + nums[i], cover); + if (cover >= nums.Length - 1) return true; + } + return false; + } +} +```

From a66e5e39ba70ab6d3fecaa29296d19245582ca00 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 30 Dec 2023 10:16:43 +0800 Subject: [PATCH 0889/1533] =?UTF-8?q?Update.0045=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8F2=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\203\346\270\270\346\210\217II.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index aab27b27ee..e006caa2ca 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -464,6 +464,27 @@ impl Solution { } } ``` +### C# +```csharp +// 版本二 +public class Solution +{ + public int Jump(int[] nums) + { + int cur = 0, next = 0, step = 0; + for (int i = 0; i < nums.Length - 1; i++) + { + next = Math.Max(next, i + nums[i]); + if (i == cur) + { + cur = next; + step++; + } + } + return step; + } +} +```

From 85f8efeef6085920de4905787ae5773012e053a9 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 31 Dec 2023 09:35:51 +0800 Subject: [PATCH 0890/1533] =?UTF-8?q?Update1005.K=E6=AC=A1=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\346\225\260\347\273\204\345\222\214.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index bed11c7a19..182402d25a 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -322,6 +322,29 @@ object Solution { } ``` +### C# +```csharp +public class Solution +{ + public int LargestSumAfterKNegations(int[] nums, int k) + { + int res = 0; + Array.Sort(nums, (a, b) => Math.Abs(b) - Math.Abs(a)); + for (int i = 0; i < nums.Length; i++) + { + if (nums[i] < 0 && k > 0) + { + nums[i] *= -1; + k--; + } + } + if (k % 2 == 1) nums[nums.Length - 1] *= -1; + foreach (var item in nums) res += item; + return res; + } +} +``` +

From c9bff6e42c5f61f1f1ca873131eb2de4aff01003 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 1 Jan 2024 10:34:33 +0800 Subject: [PATCH 0891/1533] =?UTF-8?q?Update0134.=E5=8A=A0=E6=B2=B9?= =?UTF-8?q?=E7=AB=99=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\345\212\240\346\262\271\347\253\231.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index 2f9539e8a4..c093023d5f 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -630,6 +630,29 @@ object Solution { } } ``` +### C# +```csharp +// 贪心算法,方法二 +public class Solution +{ + public int CanCompleteCircuit(int[] gas, int[] cost) + { + int curSum = 0, totalSum = 0, start = 0; + for (int i = 0; i < gas.Length; i++) + { + curSum += gas[i] - cost[i]; + totalSum += gas[i] - cost[i]; + if (curSum < 0) + { + start = i + 1; + curSum = 0; + } + } + if (totalSum < 0) return -1; + return start; + } +} +```

From 4f2b2b2618d522d73ef15ac200799453691170c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=9B=20=E9=99=88?= <761050293@qq.com> Date: Tue, 2 Jan 2024 09:50:19 +0800 Subject: [PATCH 0892/1533] =?UTF-8?q?Update0135.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E7=B3=96=E6=9E=9C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\217\221\347\263\226\346\236\234.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index d130bd6820..210f4995dc 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -370,6 +370,35 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int Candy(int[] ratings) + { + int[] candies = new int[ratings.Length]; + for (int i = 0; i < candies.Length; i++) + { + candies[i] = 1; + } + for (int i = 1; i < ratings.Length; i++) + { + if (ratings[i] > ratings[i - 1]) + { + candies[i] = candies[i - 1] + 1; + } + } + for (int i = ratings.Length - 2; i >= 0; i--) + { + if (ratings[i] > ratings[i + 1]) + { + candies[i] = Math.Max(candies[i], candies[i + 1] + 1); + } + } + return candies.Sum(); + } +} +```

From e5c4e9ec16aedbbc403135997cbf01e05d69ed62 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 2 Jan 2024 12:07:19 +0800 Subject: [PATCH 0893/1533] Update --- ...0.\347\210\254\346\245\274\346\242\257.md" | 2 - ...11\346\220\234\347\264\242\346\240\221.md" | 2 +- ...04\345\255\220\346\225\260\347\273\204.md" | 2 +- "problems/kama53.\345\257\273\345\256\235.md" | 573 ++++++++++++++---- 4 files changed, 464 insertions(+), 115 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 7a59221dc6..3bd9b001be 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -215,8 +215,6 @@ public: 所以不要轻视简单题,那种凭感觉就刷过去了,其实和没掌握区别不大,只有掌握方法论并说清一二三,才能触类旁通,举一反三哈! -就酱,循序渐进学算法,认准「代码随想录」! - ## 其他语言版本 diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index e699005edb..9566b7b89a 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -38,7 +38,7 @@ 题目中说要转换为一棵高度平衡二叉搜索树。为什么强调要平衡呢? -因为只要给我们一个有序数组,如果强调平衡,都可以以线性结构来构造二叉搜索树。 +因为只要给我们一个有序数组,如果不强调平衡,都可以以线性结构来构造二叉搜索树。 例如 有序数组[-10,-3,0,5,9] 就可以构造成这样的二叉搜索树,如图。 diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index afce264695..5934d5e364 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -100,7 +100,7 @@ public: 窗口就是 满足其和 ≥ s 的长度最小的 连续 子数组。 -窗口的起始位置如何移动:如果当前窗口的值大于s了,窗口就要向前移动了(也就是该缩小了)。 +窗口的起始位置如何移动:如果当前窗口的值大于等于s了,窗口就要向前移动了(也就是该缩小了)。 窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,也就是for循环里的索引。 diff --git "a/problems/kama53.\345\257\273\345\256\235.md" "b/problems/kama53.\345\257\273\345\256\235.md" index b52ac374be..08fc9a18c3 100644 --- "a/problems/kama53.\345\257\273\345\256\235.md" +++ "b/problems/kama53.\345\257\273\345\256\235.md" @@ -1,155 +1,506 @@ +思考一下边的权值为负数的情况 -如果你的图相对较小且比较密集,而且你更注重简单性和空间效率,数组实现可能更合适。 -如果你的图规模较大,尤其是在稀疏图中,而且你更注重时间效率和通用性,优先级队列实现可能更合适。 +# 寻宝 -其关键 在于弄清楚 minDist 的定义 +[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) -```CPP +题目描述: -#include -#include -#include -#include +在世界的某个区域,有一些分散的神秘岛屿,每个岛屿上都有一种珍稀的资源或者宝藏。你是一名探险者,决定前往这些岛屿,但为了节省时间和资源,你希望规划一条最短的路径,以便在探索这些岛屿时尽量减少旅行的距离。 -using namespace std; +给定一张地图,其中包括了所有的岛屿,以及它们之间的距离。每个岛屿都需要被至少访问一次,你的目标是规划一条最短路径,以最小化探索路径的总距离,同时确保访问了所有岛屿。 + +输入描述: + +第一行包含两个整数V 和 E,V代表顶点数,E代表边数 。顶点编号是从1到V。例如:V=2,一个有两个顶点,分别是1和2。 + +接下来共有 E 行,每行三个整数 v1,v2 和 val,v1 和 v2 为边的起点和终点,val代表边的权值。 + +输出描述: + +输出联通所有岛屿的最小路径总距离 + +输入示例: + +``` +7 11 +1 2 1 +1 3 1 +1 5 2 +2 6 1 +2 4 2 +2 3 2 +3 4 1 +4 5 1 +5 6 2 +5 7 1 +6 7 1 +``` + +输出示例: + +6 + + +## 解题思路 + +本题是最小生成树的模板题,那么我们来讲一讲最小生成树。 + +最小生成树 可以使用 prim算法 也可以使用 kruskal算法计算出来。 + +本篇我们先讲解 prim算法。 + +最小生成树是所有节点的最小连通子图, 即:以最小的成本(边的权值)将图中所有节点链接到一起。 + +图中有n个节点,那么一定可以用 n - 1 条边将所有节点连接到一起。 + +那么如何选择 这 n-1 条边 就是 最小生成树算法的任务所在。 + +例如本题示例中的无向有权图为: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231206164306.png) + +那么在这个图中,如何选取 n-1 条边 使得 图中所有节点连接到一起,并且边的权值和最小呢? + +(图中为n为7,即7个节点,那么只需要 n-1 即 6条边就可以讲所有顶点连接到一起) + +prim算法 是从节点的角度 采用贪心的策略 每次寻找距离 最小生成树最近的节点 并加入到最小生成树中。 + +prim算法核心就是三步,我称为**prim三部曲**,大家一定要熟悉这三步,代码相对会好些很多: + +1. 第一步,选距离生成树最近节点 +2. 第二步,最近节点加入生成树 +3. 第三步,更新非生成树节点到生成树的距离(即更新minDist数组) + +现在录友们会对这三步很陌生,不知道这是干啥的,没关系,下面将会画图举例来带大家把这**prim三部曲**理解到位。 + +在prim算法中,有一个数组特别重要,这里我起名为:minDist。 + +刚刚我有讲过 “每次寻找距离 最小生成树最近的节点 并加入到最小生成树中”,那么如何寻找距离最小生成树最近的节点呢? + +这就用到了 minDist 数组, 它用来作什么呢? + +**minDist数组 用来记录 每一个节点距离最小生成树的最近距离**。 理解这一点非常重要,这也是 prim算法最核心要点所在,很多录友看不懂prim算法的代码,都是因为没有理解透 这个数组的含义。 + +接下来,我们来通过一步一步画图,来带大家巩固 **prim三部曲** 以及 minDist数组 的作用。 + +(**示例中节点编号是从1开始,所以为了让大家看的不晕,minDist数组下标我也从 1 开始计数,下标0 就不使用了,这样 下标和节点标号就可以对应上了,避免大家搞混**) + + +### 1 初始状态 + +minDist 数组 里的数值初始化为 最大数,因为本题 节点距离不会超过 10000,所以 初始化最大数为 10001就可以。 + +相信这里录友就要问了,为什么这么做? + +现在 还没有最小生成树,默认每个节点距离最小生成树是最大的,这样后面我们在比较的时候,发现更近的距离,才能更新到 minDist 数组上。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231215105603.png) + +开始构造最小生成树 + +### 2 + +1、prim三部曲,第一步:选距离生成树最近节点 + +选择距离最小生成树最近的节点,加入到最小生成树,刚开始还没有最小生成树,所以随便选一个节点加入就好(因为每一个节点一定会在最小生成树里,所以随便选一个就好),那我们选择节点1 (符合遍历数组的习惯,第一个遍历的也是节点1) + +2、prim三部曲,第二步:最近节点加入生成树 + +此时 节点1 已经算最小生成树的节点。 + +3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) + +接下来,我们要更新所有节点距离最小生成树的距离,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102048.png) + + +注意下标0,我们就不管它了,下标 1 与节点 1 对应,这样可以避免大家把节点搞混。 + +此时所有非生成树的节点距离 最小生成树(节点1)的距离都已经跟新了 。 + +* 节点2 与 节点1 的距离为1,比原先的 距离值10001小,所以更新minDist[2]。 +* 节点3 和 节点1 的距离为1,比原先的 距离值10001小,所以更新minDist[3]。 +* 节点5 和 节点1 的距离为2,比原先的 距离值10001小,所以更新minDist[5]。 + +**注意图中我标记了 minDist数组里更新的权值**,是哪两个节点之间的权值,例如 minDist[2] =1 ,这个 1 是 节点1 与 节点2 之间的连线,清楚这一点对最后我们记录 最小生成树的权值总和很重要。 + +(我在后面依然会不断重复 prim三部曲,可能基础好的录友会感觉有点啰嗦,但也是让大家感觉这三部曲求解的过程) + +### 3 + +1、prim三部曲,第一步:选距离生成树最近节点 + +选取一个距离 最小生成树(节点1) 最近的非生成树里的节点,节点2,3,5 距离 最小生成树(节点1) 最近,选节点 2(其实选 节点3或者节点5都可以,距离一样的)加入最小生成树。 + +2、prim三部曲,第二步:最近节点加入生成树 + +此时 节点1 和 节点2,已经算最小生成树的节点。 + + +3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) + +接下来,我们要更新节点距离最小生成树的距离,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102431.png) + +此时所有非生成树的节点距离 最小生成树(节点1、节点2)的距离都已经跟新了 。 + +* 节点3 和 节点2 的距离为2,和原先的距离值1 小,所以不用更新。 +* 节点4 和 节点2 的距离为2,比原先的距离值10001小,所以更新minDist[4]。 +* 节点5 和 节点2 的距离为10001(不连接),所以不用更新。 +* 节点6 和 节点2 的距离为1,比原先的距离值10001小,所以更新minDist[6]。 + +### 4 + +1、prim三部曲,第一步:选距离生成树最近节点 + +选择一个距离 最小生成树(节点1、节点2) 最近的非生成树里的节点,节点3,6 距离 最小生成树(节点1、节点2) 最近,选节点3 (选节点6也可以,距离一样)加入最小生成树。 + +2、prim三部曲,第二步:最近节点加入生成树 + +此时 节点1 、节点2 、节点3 算是最小生成树的节点。 + + +3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) + +接下来更新节点距离最小生成树的距离,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102457.png) + +所有非生成树的节点距离 最小生成树(节点1、节点2、节点3 )的距离都已经跟新了 。 + +* 节点 4 和 节点 3的距离为 1,和原先的距离值 2 小,所以更新minDist[3]为1。 + +上面为什么我们只比较 节点4 和 节点3 的距离呢? + +因为节点3加入 最小生成树后,非 生成树节点 只有 节点 4 和 节点3是链接的,所以需要重新更新一下 节点4距离最小生成树的距离,其他节点距离最小生成树的距离 都不变。 + +### 5 + +1、prim三部曲,第一步:选距离生成树最近节点 + +继续选择一个距离 最小生成树(节点1、节点2、节点3) 最近的非生成树里的节点,为了巩固大家对 minDist数组的理解,这里我再啰嗦一遍: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231217213516.png) + +**minDist数组 是记录了 所有非生成树节点距离生成树的最小距离**,所以 从数组里我们能看出来,非生成树节点 4 和 节点 6 距离 生成树最近。 + + +任选一个加入生成树,我们选 节点4(选节点6也行) 。 + +**注意**,我们根据 minDist数组,选取距离 生成树 最近的节点 加入生成树,那么 **minDist数组里记录的其实也是 最小生成树的边的权值**(我在图中把权值对应的是哪两个节点也标记出来了)。 + +如果大家不理解,可以跟着我们下面的讲解,看 minDist数组的变化, minDist数组 里记录的权值对应的哪条边。 + +理解这一点很重要,因为 最后我们要求 最小生成树里所有边的权值和。 + +2、prim三部曲,第二步:最近节点加入生成树 + +此时 节点1、节点2、节点3、节点4 算是 最小生成树的节点。 + +3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) + +接下来更新节点距离最小生成树的距离,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102618.png) -// 定义图的邻接矩阵表示 -const int INF = INT_MAX; // 表示无穷大 -typedef vector> Graph; +minDist数组已经更新了 所有非生成树的节点距离 最小生成树(节点1、节点2、节点3、节点4 )的距离 。 -// 使用Prim算法找到最小生成树 -void primMST(const Graph& graph, int startVertex) { - int V = graph.size(); +* 节点 5 和 节点 4的距离为 1,和原先的距离值 2 小,所以更新minDist[4]为1。 - // 存储顶点是否在最小生成树中 - vector inMST(V, false); +### 6 - // 存储最小生成树的边权重 - vector key(V, INF); +1、prim三部曲,第一步:选距离生成树最近节点 - // 优先队列,存储边权重和目标顶点 - priority_queue, vector>, greater>> pq; +继续选距离 最小生成树(节点1、节点2、节点3、节点4 )最近的非生成树里的节点,只有 节点 5 和 节点6。 - // 初始顶点的权重设为0,加入优先队列 - key[startVertex] = 0; - pq.push({0, startVertex}); - while (!pq.empty()) { - // 从优先队列中取出权重最小的边 - int u = pq.top().second; - pq.pop(); +选节点5 (选节点6也可以)加入 生成树。 - // 将顶点u标记为在最小生成树中 - inMST[u] = true; +2、prim三部曲,第二步:最近节点加入生成树 - // 遍历u的所有邻居 - for (int v = 0; v < V; ++v) { - // 如果v未在最小生成树中,且u到v的权重小于v的当前权重 - if (!inMST[v] && graph[u][v] < key[v]) { - // 更新v的权重为u到v的权重 - key[v] = graph[u][v]; - // 将(u, v)添加到最小生成树 - pq.push({key[v], v}); +节点1、节点2、节点3、节点4、节点5 算是 最小生成树的节点。 + +3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) + +接下来更新节点距离最小生成树的距离,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102646.png) + +minDist数组已经更新了 所有非生成树的节点距离 最小生成树(节点1、节点2、节点3、节点4 、节点5)的距离 。 + +* 节点 6 和 节点 5 距离为 2,比原先的距离值 1 大,所以不更新 +* 节点 7 和 节点 5 距离为 1,比原先的距离值 10001小,更新 minDist[7] + +### 7 + +1、prim三部曲,第一步:选距离生成树最近节点 + +继续选距离 最小生成树(节点1、节点2、节点3、节点4 、节点5)最近的非生成树里的节点,只有 节点 6 和 节点7。 + +2、prim三部曲,第二步:最近节点加入生成树 + +选节点6 (选节点7也行,距离一样的)加入生成树。 + +3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) + +节点1、节点2、节点3、节点4、节点5、节点6 算是 最小生成树的节点 ,接下来更新节点距离最小生成树的距离,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102732.png) + +这里就不在重复描述了,大家类推,最后,节点7加入生成树,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102820.png) + +### 最后 + +最后我们就生成了一个 最小生成树, 绿色的边将所有节点链接到一起,并且 保证权值是最小的,因为我们在更新 minDist 数组的时候,都是选距离 最小生成树最近的点 加入到树中。 + +讲解上面的模拟过程的时候,我已经强调多次 minDist数组 是记录了 所有非生成树节点距离生成树的最小距离。 + +最后,minDist数组 也就是记录的是最小生成树所有边的权值。 + +我在图中,特别把 每条边的权值对应的是哪两个节点 标记出来(例如minDist[7] = 1,对应的是节点5 和 节点7之间的边,而不是 节点6 和 节点7),为了就是让大家清楚, minDist里的每一个值 对应的是哪条边。 + +那么我们要求最小生成树里边的权值总和 就是 把 最后的 minDist 数组 累加一起。 + +以下代码,我对 prim三部曲,做了重点注释,大家根据这三步,就可以 透彻理解prim。 + +```CPP +#include +#include +using namespace std; +int main() { + int v, e; + int x, y, k; + cin >> v >> e; + // 填一个默认最大值,题目描述val最大为10000 + vector> grid(v + 1, vector(v + 1, 10001)); + while (e--) { + cin >> x >> y >> k; + // 因为是双向图,所以两个方向都要填上 + grid[x][y] = k; + grid[y][x] = k; + + } + // 所有节点到最小生成树的最小距离 + vector minDist(v + 1, 10001); + + // 这个节点是否在树里 + vector isInTree(v + 1, false); + + // 我们只需要循环 n-1次,建立 n - 1条边,就可以把n个节点的图连在一起 + for (int i = 1; i < v; i++) { + + // 1、prim三部曲,第一步:选距离生成树最近节点 + int cur = -1; // 选中哪个节点 加入最小生成树 + for (int j = 1; j <= v; j++) { // 1 - v,顶点编号,这里下标从1开始 + // 选取最小生成树节点的条件: + // (1)不在最小生成树里 + // (2)距离最小生成树最近的节点 + // (3)只要不在最小生成树里,先默认选一个节点 ,在比较 哪一个是最小的 + // 理解条件3 很重要,才能理解这段代码:(cur == -1 || minDist[j] < minDist[cur]) + if (!isInTree[j] && (cur == -1 || minDist[j] < minDist[cur])) { + cur = j; + } + } + // 2、prim三部曲,第二步:最近节点(cur)加入生成树 + isInTree[cur] = true; + + // 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) + // cur节点加入之后, 最小生成树加入了新的节点,那么所有节点到 最小生成树的距离(即minDist数组)需要更新一下 + // 由于cur节点是新加入到最小生成树,那么只需要关心与 cur 相连的 非生成树节点 的距离 是否比 原来 非生成树节点到生成树节点的距离更小了呢 + for (int j = 1; j <= v; j++) { + // 更新的条件: + // (1)节点是 非生成树里的节点 + // (2)与cur相连的某节点的权值 比 该某节点距离最小生成树的距离小 + // 很多录友看到自己 就想不明白什么意思,其实就是 cur 是新加入 最小生成树的节点,那么 所有非生成树的节点距离生成树节点的最近距离 由于 cur的新加入,需要更新一下数据了 + if (!isInTree[j] && grid[cur][j] < minDist[j]) { + minDist[j] = grid[cur][j]; } } } - - // 输出最小生成树的边 - cout << "Edges in the Minimum Spanning Tree:\n"; - for (int i = 1; i < V; ++i) { - cout << i << " - " << key[i] << " - " << i << "\n"; + // 统计结果 + int result = 0; + for (int i = 2; i <= v; i++) { // 不计第一个顶点,因为统计的是边的权值,v个节点有 v-1条边 + result += minDist[i]; } + cout << result << endl; + } +``` -int main() { - // 例子:无向图的邻接矩阵表示 - Graph graph = { - {0, 2, 0, 6, 0}, - {2, 0, 3, 8, 5}, - {0, 3, 0, 0, 7}, - {6, 8, 0, 0, 9}, - {0, 5, 7, 9, 0} - }; - - // 从顶点0开始运行Prim算法 - primMST(graph, 0); - - return 0; +## 拓展 + +上面讲解的是记录了最小生成树 所有边的权值,如果让打印出来 最小生成树的每条边呢? 或者说 要把这个最小生成树画出来呢? + + +此时我们就需要把 最小生成树里每一条边记录下来。 + +此时有两个问题: + +* 1、用什么结构来记录 +* 2、如何记录 + +如果记录边,其实就是记录两个节点就可以,两个节点连成一条边。 + +如何记录两个节点呢? + +我们使用一维数组就可以记录。 parent[节点编号] = 节点编号, 这样就把一条边记录下来了。(当然如果节点编号非常大,可以考虑使用map) + +使用一维数组记录是有向边,不过我们这里不需要记录方向,所以只关注两条边是连接的就行。 + +parent数组初始化代码: + +```CPP +vector parent(v + 1, -1); +``` + +接下来就是第二个问题,如何记录? + +我们再来回顾一下 prim三部曲, + +1. 第一步,选距离生成树最近节点 +2. 第二步,最近节点加入生成树 +3. 第三步,更新非生成树节点到生成树的距离(即更新minDist数组) + +大家先思考一下,我们是在第几步,可以记录 最小生成树的边呢? + +在本面上半篇 我们讲解过:“我们根据 minDist数组,选组距离 生成树 最近的节点 加入生成树,那么 **minDist数组里记录的其实也是 最小生成树的边的权值**。” + +既然 minDist数组 记录了 最小生成树的边,是不是就是在更新 minDist数组 的时候,去更新parent数组来记录一下对应的边呢。 + + +所以 在 prim三部曲中的第三步,更新 parent数组,代码如下: + +```CPP +for (int j = 1; j <= v; j++) { + if (!isInTree[j] && grid[cur][j] < minDist[j]) { + minDist[j] = grid[cur][j]; + parent[j] = cur; // 记录最小生成树的边 (注意数组指向的顺序很重要) + } } ``` +代码中注释中,我强调了 数组指向的顺序很重要。 因为不少录友在这里会写成这样: `parent[cur] = j` 。 -```CPP -#include -#include -#include +这里估计大家会疑惑了,parent[节点编号A] = 节点编号B, 就表示A 和 B 相连,我们这里就不用在意方向,代码中 为什么 只能 `parent[j] = cur` 而不能 `parent[cur] = j` 这么写呢? -using namespace std; +如果写成 `parent[cur] = j`,在 for 循环中,有多个 j 满足要求, 那么 parent[cur] 就会被反复覆盖,因为 cur 是一个固定值。 -// 定义图的邻接矩阵表示 -const int INF = INT_MAX; // 表示无穷大 -typedef vector> Graph; +举个例子,cur = 1, 在 for循环中,可能 就 j = 2, j = 3,j =4 都符合条件,那么本来应该记录 节点1 与 节点 2、节点3、节点4相连的。 -// 使用Prim算法找到最小生成树 -void primMST(const Graph& graph, int startVertex) { - int V = graph.size(); +如果 `parent[cur] = j` 这么写,最后更新的逻辑是 parent[1] = 2, parent[1] = 3, parent[1] = 4, 最后只能记录 节点1 与节点 4 相连,其他相连情况都被覆盖了。 - // 存储顶点是否在最小生成树中 - vector inMST(V, false); +如果这么写 `parent[j] = cur`, 那就是 parent[2] = 1, parent[3] = 1, parent[4] = 1 ,这样 才能完整表示出 节点1 与 其他节点都是链接的,才没有被覆盖。 - // 存储每个顶点的权重 - vector key(V, INF); +主要问题也是我们使用了一维数组来记录。 - // 初始化起始顶点的权重为0 - key[startVertex] = 0; +如果是二维数组,来记录两个点链接,例如 parent[节点编号A][节点编号B] = 1 ,parent[节点编号B][节点编号A] = 1,来表示 节点A 与 节点B 相连,那就没有上面说的这个注意事项了,当然这么做的话,就是多开辟的内存空间。 - // 存储最小生成树的边权重 - vector parent(V, -1); +以下是输出最小生成树边的代码,不算最后输出, 就额外添加了两行代码,我都注释标记了: - // 构建最小生成树 - for (int count = 0; count < V - 1; ++count) { - // 从未在最小生成树中的顶点中找到权重最小的顶点 - int u = -1; - for (int v = 0; v < V; ++v) { - if (!inMST[v] && (u == -1 || key[v] < key[u])) { - u = v; +```CPP +#include +#include +using namespace std; +int main() { + int v, e; + int x, y, k; + cin >> v >> e; + vector> grid(v + 1, vector(v + 1, 10001)); + while (e--) { + cin >> x >> y >> k; + grid[x][y] = k; + grid[y][x] = k; + } + + vector minDist(v + 1, 10001); + vector isInTree(v + 1, false); + + //加上初始化 + vector parent(v + 1, -1); + + for (int i = 1; i < v; i++) { + int cur = -1; + for (int j = 1; j <= v; j++) { + if (!isInTree[j] && (cur == -1 || minDist[j] < minDist[cur])) { + cur = j; } } + isInTree[cur] = true; + for (int j = 1; j <= v; j++) { + if (!isInTree[j] && grid[cur][j] < minDist[j]) { + minDist[j] = grid[cur][j]; - // 将顶点u标记为在最小生成树中 - inMST[u] = true; - - // 更新u的邻居的权重和父节点 - for (int v = 0; v < V; ++v) { - if (graph[u][v] != 0 && !inMST[v] && graph[u][v] < key[v]) { - key[v] = graph[u][v]; - parent[v] = u; + parent[j] = cur; // 记录边 } } } - - // 输出最小生成树的边 - cout << "Edges in the Minimum Spanning Tree:\n"; - for (int i = 1; i < V; ++i) { - cout << parent[i] << " - " << key[i] << " - " << i << "\n"; + // 输出 最小生成树边的链接情况 + for (int i = 1; i <= v; i++) { + cout << i "->" parent[i] << endl; } } +``` + +按照本题示例,代码输入如下: -int main() { - // 例子:无向图的邻接矩阵表示 - Graph graph = { - {0, 2, 0, 6, 0}, - {2, 0, 3, 8, 5}, - {0, 3, 0, 0, 7}, - {6, 8, 0, 0, 9}, - {0, 5, 7, 9, 0} - }; - - // 从顶点0开始运行Prim算法 - primMST(graph, 0); - - return 0; -} ``` +1->-1 +2->1 +3->1 +4->3 +5->4 +6->2 +7->5 +``` + +注意,这里是无向图,我在输出上添加了箭头仅仅是为了方便大家看出是边的意思。 + +大家可以和我们本题最后生成的最小生成树的图 去对比一下 边的链接情况: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231229115714.png) + +绿色的边 是最小生成树,和我们的 输出完全一致。 + +## 总结 + +此时我就把prim算法讲解完毕了,我们再来回顾一下。 + +关于 prim算法,我自创了三部曲,来帮助大家理解: + +1. 第一步,选距离生成树最近节点 +2. 第二步,最近节点加入生成树 +3. 第三步,更新非生成树节点到生成树的距离(即更新minDist数组) + +大家只要理解这三部曲, prim算法 至少是可以写出一个框架出来,然后在慢慢补充细节,这样不至于 自己在写prim的时候 两眼一抹黑 完全凭感觉去写。 +这也为什么很多录友感觉 prim算法比较难,而且每次学会来,隔一段时间 又不会写了,主要是 没有一个纲领。 + +理解这三部曲之后,更重要的 就是理解 minDist数组。 + +**minDist数组 是prim算法的灵魂,它帮助 prim算法完成最重要的一步,就是如何找到 距离最小生成树最近的点**。 + +再来帮大家回顾 minDist数组 的含义:记录 每一个节点距离最小生成树的最近距离。 + +理解 minDist数组 ,至少大家看prim算法的代码不会懵。 + +也正是 因为 minDist数组 的作用,我们根据 minDist数组,选取距离 生成树 最近的节点 加入生成树,那么 **minDist数组里记录的其实也是 最小生成树的边的权值**。 + +所以我们求 最小生成树的权值和 就是 计算后的 minDist数组 数值总和。 + +最后我们拓展了如何求职 最小生成树 的每一条边,其实 添加的代码很简单,主要是理解 为什么使用 parent数组 来记录边 以及 在哪里 更新parent数组。 + +同时,因为使用一维数组,数组的下标和数组 如何赋值很重要,不要搞反,导师结果被覆盖。 + +好了,以上为总结,录友们学习愉快。 + + + + From c736cffe22143965ffc50896ca5b11a3e65df536 Mon Sep 17 00:00:00 2001 From: zhqiao Date: Tue, 2 Jan 2024 21:16:29 +0800 Subject: [PATCH 0894/1533] =?UTF-8?q?0377=20=E6=96=B0=E5=A2=9Epython=20?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4DP=E6=95=B0=E7=BB=84=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\346\200\273\345\222\214\342\205\243.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index d9699c5407..05f852b1c6 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -206,6 +206,34 @@ class Solution: ``` +二维DP版 +```python +class Solution: + def combinationSum4(self, nums: List[int], target: int) -> int: + # dp[][j]和为j的组合的总数 + dp = [[0] * (target+1) for _ in nums] + + for i in range(len(nums)): + dp[i][0] = 1 + + # 这里不能初始化dp[0][j]。dp[0][j]的值依赖于dp[-1][j-nums[0]] + + for j in range(1, target+1): + for i in range(len(nums)): + + if j - nums[i] >= 0: + dp[i][j] = ( + # 不放nums[i] + # i = 0 时,dp[-1][j]恰好为0,所以没有特殊处理 + dp[i-1][j] + + # 放nums[i]。对于和为j的组合,只有试过全部物品,才能知道有几种组合方式。所以取最后一个物品dp[-1][j-nums[i]] + dp[-1][j-nums[i]] + ) + else: + dp[i][j] = dp[i-1][j] + return dp[-1][-1] +``` + ### Go: ```go From 499d2afb618298ddb3e0e4f8d88f6f13284f0d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=9B=20=E9=99=88?= <761050293@qq.com> Date: Wed, 3 Jan 2024 09:21:34 +0800 Subject: [PATCH 0895/1533] =?UTF-8?q?Update=200860.=E6=9F=A0=E6=AA=AC?= =?UTF-8?q?=E6=B0=B4=E6=89=BE=E9=9B=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\346\260\264\346\211\276\351\233\266.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index 50a0c31a50..db70112d8f 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -397,6 +397,46 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public bool LemonadeChange(int[] bills) + { + int five = 0, ten = 0, twenty = 0; + foreach (var bill in bills) + { + if (bill == 5) five++; + if (bill == 10) + { + if (five == 0) return false; + five--; + ten++; + } + if (bill == 20) + { + if (ten > 0 && five > 0) + { + ten--; + five--; + twenty++; + } + else if (five >= 3) + { + five -= 3; + twenty++; + } + else + { + return false; + } + + } + } + return true; + } +} +```

From d479e755cbd39cfdc5b3dd9ba97f3c5ffdf84604 Mon Sep 17 00:00:00 2001 From: Relsola Date: Wed, 3 Jan 2024 20:31:05 +0800 Subject: [PATCH 0896/1533] =?UTF-8?q?update=20130.=E8=A2=AB=E5=9B=B4?= =?UTF-8?q?=E7=BB=95=E7=9A=84=E5=8C=BA=E5=9F=9F=EF=BC=8C417.=E5=A4=AA?= =?UTF-8?q?=E5=B9=B3=E6=B4=8B=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E6=96=B0=E5=A2=9E=20JS=20=E5=B9=BF=E6=90=9C?= =?UTF-8?q?=E5=92=8C=E6=B7=B1=E6=90=9C=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\232\204\345\214\272\345\237\237.md" | 126 ++++++++++++++++++ ...64\346\265\201\351\227\256\351\242\230.md" | 111 +++++++++++++++ 2 files changed, 237 insertions(+) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index e8a1f02f69..8014c0c8bf 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -435,6 +435,132 @@ class Solution: ``` +### JavaScript +```JavaScript +/** + * @description 深度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function dfs(board, x, y) { + board[x][y] = 'A'; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + dfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + dfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + dfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + dfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} + +/** + * @description 广度搜索优先 + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +function solve(board) { + const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + const [rowSize, colSize] = [board.length, board[0].length]; + + function bfs(board, x, y) { + board[x][y] = 'A'; + const stack = [y, x]; + + while (stack.length !== 0) { + const top = [stack.pop(), stack.pop()]; + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + top[0]; + const nextY = dir[i][1] + top[1]; + + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + + if (board[nextX][nextY] === 'O') { + board[nextX][nextY] = 'A'; + stack.push(nextY, nextX); + } + } + } + + for (let i = 0; i < 4; i++) { + const nextX = dir[i][0] + x; + const nextY = dir[i][1] + y; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (board[nextX][nextY] === 'O') { + dfs(board, nextX, nextY); + } + } + } + + for (let i = 0; i < rowSize; i++) { + if (board[i][0] === 'O') { + bfs(board, i, 0); + } + if (board[i][colSize - 1] === 'O') { + bfs(board, i, colSize - 1); + } + } + + for (let i = 1; i < colSize - 1; i++) { + if (board[0][i] === 'O') { + bfs(board, 0, i); + } + if (board[rowSize - 1][i] === 'O') { + bfs(board, rowSize - 1, i); + } + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (board[i][k] === 'A') { + board[i][k] = 'O'; + } else if (board[i][k] === 'O') { + board[i][k] = 'X'; + } + } + } +} +``` +

diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 6777e2d974..53ae14edfe 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -450,6 +450,117 @@ class Solution: return ans ``` +### JavaScript +```JavaScript +/** + * @description 深度搜索优先 + * @param {number[][]} heights + * @return {number[][]} + */ +function pacificAtlantic(heights) { + const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + const [rowSize, colSize] = [heights.length, heights[0].length]; + const visited = Array.from({ length: rowSize }, _ => + Array.from({ length: colSize }, _ => new Array(2).fill(false)) + ); + const result = []; + + function dfs(heights, visited, x, y, sign) { + if (visited[x][y][sign]) { + return; + } + visited[x][y][sign] = true; + for (let i = 0; i < 4; i++) { + const nextX = x + dir[i][0]; + const nextY = y + dir[i][1]; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (heights[x][y] > heights[nextX][nextY]) { + continue; + } + dfs(heights, visited, nextX, nextY, sign); + } + } + + for (let i = 0; i < rowSize; i++) { + dfs(heights, visited, i, 0, 0); + dfs(heights, visited, i, colSize - 1, 1); + } + + for (let i = 0; i < colSize; i++) { + dfs(heights, visited, 0, i, 0); + dfs(heights, visited, rowSize - 1, i, 1); + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (visited[i][k][0] && visited[i][k][1]) { + result.push([i, k]); + } + } + } + + return result; +} + +/** + * @description 广度搜索优先 + * @param {number[][]} heights + * @return {number[][]} + */ +function pacificAtlantic(heights) { + const dir = [[-1, 0], [0, -1], [1, 0], [0, 1]]; + const [rowSize, colSize] = [heights.length, heights[0].length]; + const visited = Array.from({ length: rowSize }, _ => + Array.from({ length: colSize }, _ => new Array(2).fill(false)) + ); + const result = []; + + function bfs(heights, visited, x, y, sign) { + if (visited[x][y][sign]) { + return; + } + visited[x][y][sign] = true; + const stack = [y, x]; + while (stack.length !== 0) { + [x, y] = [stack.pop(), stack.pop()]; + for (let i = 0; i < 4; i++) { + const nextX = x + dir[i][0]; + const nextY = y + dir[i][1]; + if (nextX < 0 || nextX >= rowSize || nextY < 0 || nextY >= colSize) { + continue; + } + if (heights[x][y] > heights[nextX][nextY] || visited[nextX][nextY][sign]) { + continue; + } + visited[nextX][nextY][sign] = true; + stack.push(nextY, nextX); + } + } + } + + for (let i = 0; i < rowSize; i++) { + bfs(heights, visited, i, 0, 0); + bfs(heights, visited, i, colSize - 1, 1); + } + + for (let i = 0; i < colSize; i++) { + bfs(heights, visited, 0, i, 0); + bfs(heights, visited, rowSize - 1, i, 1); + } + + for (let i = 0; i < rowSize; i++) { + for (let k = 0; k < colSize; k++) { + if (visited[i][k][0] && visited[i][k][1]) { + result.push([i, k]); + } + } + } + + return result; +} +``` From b05f66d853a42afa17bb98963c2fdf65c3bcd6a8 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 4 Jan 2024 10:41:09 +0800 Subject: [PATCH 0897/1533] =?UTF-8?q?Update0406.=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=AE=A1=E7=A8=BF=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\273\272\351\230\237\345\210\227.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 9cd78fac62..b0b02c1454 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -396,6 +396,29 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int[][] ReconstructQueue(int[][] people) + { + Array.Sort(people, (a, b) => + { + if (a[0] == b[0]) + { + return a[1] - b[1]; + } + return b[0] - a[0]; + }); + var res = new List(); + for (int i = 0; i < people.Length; i++) + { + res.Insert(people[i][1], people[i]); + } + return res.ToArray(); + } +} +```

From 709b5babf0c01f4aa35034ab45f948531e15964e Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 4 Jan 2024 11:12:29 +0800 Subject: [PATCH 0898/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DC#=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=AB=98=E4=BA=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\277\345\233\236\346\226\207\345\255\220\344\270\262.md" | 4 ++-- ...\204\345\255\227\346\257\215\347\273\204\345\220\210.md" | 2 +- ...\250\344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- "problems/0028.\345\256\236\347\216\260strStr.md" | 2 +- ...\216\344\270\200\344\270\252\344\275\215\347\275\256.md" | 2 +- ...062.\344\270\215\345\220\214\350\267\257\345\276\204.md" | 2 +- "problems/0070.\347\210\254\346\245\274\346\242\257.md" | 2 +- "problems/0077.\347\273\204\345\220\210.md" | 2 +- "problems/0090.\345\255\220\351\233\206II.md" | 2 +- ...\214\345\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- ...\271\347\247\260\344\272\214\345\217\211\346\240\221.md" | 2 +- ...\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 4 ++-- ...\204\346\234\200\345\244\247\346\267\261\345\272\246.md" | 6 +++--- ...\204\351\200\240\344\272\214\345\217\211\346\240\221.md" | 2 +- ...\263\350\241\241\344\272\214\345\217\211\346\240\221.md" | 2 +- ...\204\346\234\200\345\260\217\346\267\261\345\272\246.md" | 4 ++-- ...112.\350\267\257\345\276\204\346\200\273\345\222\214.md" | 2 +- ...\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 2 +- ...\204\350\212\202\347\202\271\344\270\252\346\225\260.md" | 2 +- ...\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- ...\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- ...\204\346\211\200\346\234\211\350\267\257\345\276\204.md" | 2 +- ...\246\345\217\266\345\255\220\344\271\213\345\222\214.md" | 2 +- ...\221\344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- ...\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 2 +- ...\221\344\270\255\347\232\204\344\274\227\346\225\260.md" | 2 +- ...\220\346\263\242\351\202\243\345\245\221\346\225\260.md" | 4 ++-- ...\246\344\270\213\350\247\222\347\232\204\345\200\274.md" | 2 +- ...\200\345\260\217\347\273\235\345\257\271\345\267\256.md" | 2 +- ...\242\344\270\272\347\264\257\345\212\240\346\240\221.md" | 2 +- ...\210\345\271\266\344\272\214\345\217\211\346\240\221.md" | 2 +- ...\200\345\244\247\344\272\214\345\217\211\346\240\221.md" | 2 +- ...\214\345\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- ...\221\344\270\255\347\232\204\346\220\234\347\264\242.md" | 2 +- ...707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 2 +- ...\261\350\264\271\347\210\254\346\245\274\346\242\257.md" | 2 +- ...\237\344\270\200\350\277\255\344\273\243\346\263\225.md" | 6 +++--- ...\204\350\277\255\344\273\243\351\201\215\345\216\206.md" | 2 +- ...\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 6 +++--- ....07.\351\223\276\350\241\250\347\233\270\344\272\244.md" | 2 +- 40 files changed, 50 insertions(+), 50 deletions(-) diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index 614f60514c..b13f9ac356 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -618,7 +618,7 @@ char * longestPalindrome(char * s){ ### C#: 動態規則: -```c# +```csharp public class Solution { public string LongestPalindrome(string s) { @@ -648,7 +648,7 @@ public class Solution { ``` 雙指針: -```C# +```csharp public class Solution { int maxlenth = 0; int left = 0; diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index 8a16008abf..cbd99f8d93 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -733,7 +733,7 @@ def backtracking(result, letter_map, digits, path, index) end ``` ### C# -```C# +```csharp public class Solution { public IList res = new List(); diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index 405f4183e9..b2a830a746 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -462,7 +462,7 @@ impl Solution { ``` ### C# -```C# +```csharp // 虚拟头结点 public ListNode SwapPairs(ListNode head) { diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index bf4ad600c3..25b8179951 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -1359,7 +1359,7 @@ impl Solution { ``` >前缀表统一不减一 -```C# +```csharp public int StrStr(string haystack, string needle) { if (string.IsNullOrEmpty(needle)) diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index 3bf90e3bbb..22936fef13 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -331,7 +331,7 @@ class Solution { ### C# -```c# +```csharp public int[] SearchRange(int[] nums, int target) { var leftBorder = GetLeftBorder(nums, target); diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index b7ce542ebe..5131fdbbbd 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -537,7 +537,7 @@ object Solution { ### c# -```c# +```csharp public class Solution { public int UniquePaths(int m, int n) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 7a59221dc6..d300683e1c 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -470,7 +470,7 @@ object Solution { ### C# -```c# +```csharp public class Solution { public int ClimbStairs(int n) { if(n<=2) return n; diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 8bca6f2496..103fb627f5 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -793,7 +793,7 @@ end ``` ### C# -```C# +```csharp // 暴力 public class Solution { diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 9fc334a4d8..1bb63a34d6 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -641,7 +641,7 @@ object Solution { } ``` ### C# -```c# +```csharp public class Solution { public IList> res = new List>(); diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 319ad1aaa1..88e1628243 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -792,7 +792,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public long val = Int64.MinValue; public bool IsValidBST(TreeNode root) diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 1b777dd527..8442f0ab9e 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -898,7 +898,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public bool IsSymmetric(TreeNode root) { diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index e977279c56..4411b5609f 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -463,7 +463,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public IList> LevelOrder(TreeNode root) { var res = new List>(); @@ -825,7 +825,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public IList> LevelOrderBottom(TreeNode root) { var res = new List>(); diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index b89dbf457c..f4c6cfc8f4 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -1033,7 +1033,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归法 public int MaxDepth(TreeNode root) { if(root == null) return 0; @@ -1044,7 +1044,7 @@ public int MaxDepth(TreeNode root) { return 1 + Math.Max(leftDepth, rightDepth); } ``` -```C# +```csharp // 前序遍历 int result = 0; public int MaxDepth(TreeNode root) @@ -1065,7 +1065,7 @@ public void GetDepth(TreeNode root, int depth) return; } ``` -```C# +```csharp // 迭代法 public int MaxDepth(TreeNode root) { diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index dc517480a6..0e0ab1d74f 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -1229,7 +1229,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public TreeNode BuildTree(int[] inorder, int[] postorder) { if (inorder.Length == 0 || postorder.Length == null) return null; diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index 41669bff7f..40fdcd143d 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -909,7 +909,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public bool IsBalanced(TreeNode root) { return GetHeight(root) == -1 ? false : true; diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 465233493d..6d1632d593 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -709,7 +709,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public int MinDepth(TreeNode root) { @@ -725,7 +725,7 @@ public int MinDepth(TreeNode root) return res; } ``` -```C# +```csharp // 迭代 public int MinDepth(TreeNode root) { diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index f1ce7637ba..28134a7cf3 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -1513,7 +1513,7 @@ impl Solution { ``` ### C# -```C# +```csharp // 0112.路径总和 // 递归 public bool HasPathSum(TreeNode root, int targetSum) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index d15bb5f321..520f17a7ec 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -973,7 +973,7 @@ char * reverseWords(char * s){ ``` ### C# -```C# LINQ高级方法 +```csharp LINQ高级方法 public string ReverseWords(string s) { return string.Join(' ', s.Trim().Split(' ',StringSplitOptions.RemoveEmptyEntries).Reverse()); } diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index ef6e88aaae..d93d2a3381 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -868,7 +868,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public int CountNodes(TreeNode root) { diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index b27a231e77..cec8a1dd2b 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -514,7 +514,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 0e99f1c2fb..049f70c7ae 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -432,7 +432,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root; diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 9ba165c798..4c6c92c5b4 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -901,7 +901,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public IList BinaryTreePaths(TreeNode root) { List path = new(); diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 6dfcc886bc..3d0f5a8abf 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -652,7 +652,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public int SumOfLeftLeaves(TreeNode root) { diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 13c25023a2..8922a14e14 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -772,7 +772,7 @@ impl Solution { ### C# > 递归法: -```C# +```csharp public TreeNode DeleteNode(TreeNode root, int key) { // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root == null) return null; diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 3245d94897..311e3a695e 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -682,7 +682,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 前缀表不减一 public bool RepeatedSubstringPattern(string s) { diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 5b26d580b2..20627d1ad6 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -1010,7 +1010,7 @@ pub fn find_mode(root: Option>>) -> Vec { } ``` ### C# -```C# +```csharp // 递归 public class Solution { diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 0c073db5bb..71c022bd34 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -439,7 +439,7 @@ object Solution { 动态规划: -```c# +```csharp public class Solution { public int Fib(int n) @@ -459,7 +459,7 @@ public class Solution 递归: -```c# +```csharp public class Solution { public int Fib(int n) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index 0e2f426633..3cdcc80163 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -685,7 +685,7 @@ impl Solution { } ``` ### C# -```C# +```csharp //递归 int maxDepth = -1; int res = 0; diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index f08df577d9..82b3f5d4e9 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -648,7 +648,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public class Solution { diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index 07cae1adb9..7fcb5efd32 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -530,7 +530,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public class Solution { diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 9efeb56db9..3478a2af5d 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -789,7 +789,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public TreeNode MergeTrees(TreeNode root1, TreeNode root2) { if (root1 == null) return root2; diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 6b343840f9..f54558a68c 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -583,7 +583,7 @@ impl Solution { } ``` ### C# -```C# +```csharp public TreeNode ConstructMaximumBinaryTree(int[] nums) { if (nums.Length == 0) return null; diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 229a2ab148..916013c52c 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -568,7 +568,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public TreeNode TrimBST(TreeNode root, int low, int high) { diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 65e6921915..9efb1e0519 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -465,7 +465,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 递归 public TreeNode SearchBST(TreeNode root, int val) { diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index a08227d9fc..fecdbc3cad 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -1486,7 +1486,7 @@ impl MyLinkedList { ``` ### C# -```C# +```csharp class ListNode { public int val; diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 6fb518c371..d13ff19f5a 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -500,7 +500,7 @@ object Solution { ### C# -```c# +```csharp public class Solution { public int MinCostClimbingStairs(int[] cost) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index ad79424da8..ee4899b1b1 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -742,7 +742,7 @@ impl Solution{ } ``` ### C# -```C# +```csharp // 前序遍历 public IList PreorderTraversal(TreeNode root) { @@ -772,7 +772,7 @@ public IList PreorderTraversal(TreeNode root) return res; } ``` -```C# +```csharp // 中序遍历 public IList InorderTraversal(TreeNode root) { @@ -803,7 +803,7 @@ public IList InorderTraversal(TreeNode root) } ``` -```C# +```csharp // 后序遍历 public IList PostorderTraversal(TreeNode root) { diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index e39709b882..35a01a7fbe 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -696,7 +696,7 @@ impl Solution { } ``` ### C# -```C# +```csharp // 前序遍历 public IList PreorderTraversal(TreeNode root) { diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 6dad6e56de..92a8341fae 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -566,7 +566,7 @@ impl Solution { ``` ### C# -```C# +```csharp // 前序遍历 public IList PreorderTraversal(TreeNode root) { @@ -584,7 +584,7 @@ public void Traversal(TreeNode cur, IList res) Traversal(cur.right, res); } ``` -```C# +```csharp // 中序遍历 public IList InorderTraversal(TreeNode root) { @@ -601,7 +601,7 @@ public void Traversal(TreeNode cur, IList res) Traversal(cur.right, res); } ``` -```C# +```csharp // 后序遍历 public IList PostorderTraversal(TreeNode root) { diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index adeaa413aa..d0967b8b86 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -503,7 +503,7 @@ object Solution { } ``` ### C# -```C# +```csharp public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; From 55d676e194180cb33564a7b3019068ccd1ad834f Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 5 Jan 2024 14:08:10 +0800 Subject: [PATCH 0899/1533] =?UTF-8?q?Update=200452.=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86?= =?UTF-8?q?=E6=B0=94=E7=90=83=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...225\347\210\206\346\260\224\347\220\203.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 90cd7085d1..cd57f83b26 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -332,6 +332,24 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindMinArrowShots(int[][] points) + { + if (points.Length == 0) return 0; + Array.Sort(points, (a, b) => a[0].CompareTo(b[0])); + int count = 1; + for (int i = 1; i < points.Length; i++) + { + if (points[i][0] > points[i - 1][1]) count++; + else points[i][1] = Math.Min(points[i][1], points[i - 1][1]); + } + return count; + } +} +```

From 6add8c3287ade5bbfefd6d7042863376cff1ca39 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 6 Jan 2024 09:33:13 +0800 Subject: [PATCH 0900/1533] =?UTF-8?q?Update=200435.=E6=97=A0=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E5=8C=BA=E9=97=B4=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\217\240\345\214\272\351\227\264.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index c307532e4f..1a33f98ea6 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -441,6 +441,27 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int EraseOverlapIntervals(int[][] intervals) + { + if (intervals.Length == 0) return 0; + Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1])); + int res = 1, end = intervals[0][1]; + for (int i = 1; i < intervals.Length; i++) + { + if (end <= intervals[i][0]) + { + end = intervals[i][1]; + res++; + } + } + return intervals.Length - res; + } +} +```

From 30af534ee95204e51da960ba3787bfa3b90d14b9 Mon Sep 17 00:00:00 2001 From: dengyongchi Date: Sat, 6 Jan 2024 10:56:02 +0800 Subject: [PATCH 0901/1533] =?UTF-8?q?feat:=20=E5=85=B3=E4=BA=8E=E5=9B=BE?= =?UTF-8?q?=E7=AC=AC1971=E9=A2=98js=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\345\234\250\350\267\257\345\276\204.md" | 121 ++++++++++++++---- 1 file changed, 99 insertions(+), 22 deletions(-) diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 27ee9147a8..132b0181c0 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -4,30 +4,28 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-# 1971. 寻找图中是否存在路径 +# 1971. 寻找图中是否存在路径 [题目链接](https://leetcode.cn/problems/find-if-path-exists-in-graph/) -有一个具有 n个顶点的 双向 图,其中每个顶点标记从 0 到 n - 1(包含 0 和 n - 1)。图中的边用一个二维整数数组 edges 表示,其中 edges[i] = [ui, vi] 表示顶点 ui 和顶点 vi 之间的双向边。 每个顶点对由 最多一条 边连接,并且没有顶点存在与自身相连的边。 +有一个具有 n 个顶点的 双向 图,其中每个顶点标记从 0 到 n - 1(包含 0 和 n - 1)。图中的边用一个二维整数数组 edges 表示,其中 edges[i] = [ui, vi] 表示顶点 ui 和顶点 vi 之间的双向边。 每个顶点对由 最多一条 边连接,并且没有顶点存在与自身相连的边。 请你确定是否存在从顶点 start 开始,到顶点 end 结束的 有效路径 。 -给你数组 edges 和整数 n、start和end,如果从 start 到 end 存在 有效路径 ,则返回 true,否则返回 false 。 - - -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220705101442.png) +给你数组 edges 和整数 n、start 和 end,如果从 start 到 end 存在 有效路径 ,则返回 true,否则返回 false 。 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220705101442.png) 提示: -* 1 <= n <= 2 * 10^5 -* 0 <= edges.length <= 2 * 10^5 -* edges[i].length == 2 -* 0 <= ui, vi <= n - 1 -* ui != vi -* 0 <= start, end <= n - 1 -* 不存在双向边 -* 不存在指向顶点自身的边 +- 1 <= n <= 2 \* 10^5 +- 0 <= edges.length <= 2 \* 10^5 +- edges[i].length == 2 +- 0 <= ui, vi <= n - 1 +- ui != vi +- 0 <= start, end <= n - 1 +- 不存在双向边 +- 不存在指向顶点自身的边 ## 思路 @@ -70,7 +68,7 @@ void join(int u, int v) { } ``` -以上模板中,只要修改 n 大小就可以,本题n不会超过2 * 10^5。 +以上模板中,只要修改 n 大小就可以,本题 n 不会超过 2 \* 10^5。 并查集主要有三个功能。 @@ -80,17 +78,17 @@ void join(int u, int v) { 简单介绍并查集之后,我们再来看一下这道题目。 -为什么说这道题目是并查集基础题目,题目中各个点是双向图链接,那么判断 一个顶点到另一个顶点有没有有效路径其实就是看这两个顶点是否在同一个集合里。 +为什么说这道题目是并查集基础题目,题目中各个点是双向图链接,那么判断 一个顶点到另一个顶点有没有有效路径其实就是看这两个顶点是否在同一个集合里。 -如何算是同一个集合呢,有边连在一起,就算是一个集合。 +如何算是同一个集合呢,有边连在一起,就算是一个集合。 -此时我们就可以直接套用并查集模板。 +此时我们就可以直接套用并查集模板。 -使用join(int u, int v)将每条边加入到并查集。 +使用 join(int u, int v)将每条边加入到并查集。 -最后 isSame(int u, int v) 判断是否是同一个根 就可以了。 +最后 isSame(int u, int v) 判断是否是同一个根 就可以了。 -C++代码如下: +C++代码如下: ```CPP class Solution { @@ -191,7 +189,7 @@ class Solution { ### Python: -PYTHON并查集解法如下: +PYTHON 并查集解法如下: ```PYTHON class Solution: @@ -206,6 +204,85 @@ class Solution: return find(source) == find(destination) ``` +### Javascript: + +Javascript 并查集解法如下: + +```Javascript +class unionF{ + constructor(n){ + this.count = n + this.roots = new Array(n).fill(0).map((item,index)=>index) + } + + findRoot(x){ + if(this.roots[x]!==x){ + this.roots[x] = this.findRoot(this.roots[x]) + } + return this.roots[x] + } + + union(x,y){ + const rx = this.findRoot(x) + const ry = this.findRoot(y) + this.roots[rx] = ry + this.count-- + } + + isConnected(x,y){ + return this.findRoot(x)===this.findRoot(y) + } +} + +var validPath = function(n, edges, source, destination) { + const UF = new unionF(n) + for(const [s,t] of edges){ + UF.union(s,t) + } + return UF.isConnected(source,destination) +}; +``` + +Javascript 双向 bfs 解法如下: + +```Javascript +var validPath = function(n, edges, source, destination) { + const graph = new Array(n).fill(0).map(()=>[]) + for(const [s,t] of edges){ + graph[s].push(t) + graph[t].push(s) + } + + const visited = new Array(n).fill(false) + function bfs(start,end,graph){ + const startq = [start] + const endq = [end] + while(startq.length&&endq.length){ + const slen = startq.length + for(let i = 0;i From 39f25c499818ef9da78a1c09264738e68292b96f Mon Sep 17 00:00:00 2001 From: CUI Date: Sat, 6 Jan 2024 15:01:08 +0800 Subject: [PATCH 0902/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0go=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E8=B4=AA=E5=BF=83=E4=BC=98=E5=8C=96=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=9C=A80045.=E8=B7=B3=E8=B7=83=E6=B8=B8=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\203\346\270\270\346\210\217II.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index e006caa2ca..d290f55e80 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -285,6 +285,34 @@ class Solution: ### Go + +```go +/** + * @date: 2024 Jan 06 + * @time: 13:44 + * @author: Chris +**/ +// 贪心算法优化版 + +// 记录步骤规则:每超过上一次可达最大范围,需要跳跃一次,次数+1 +// 记录位置:i == lastDistance + 1 +func jump(nums []int) int { + // 根据题目规则,初始位置为nums[0] + lastDistance := 0 // 上一次覆盖范围 + curDistance := 0 // 当前覆盖范围(可达最大范围) + minStep := 0 // 记录最少跳跃次数 + + for i := 0; i < len(nums); i++ { + if i == lastDistance+1 { // 在上一次可达范围+1的位置,记录步骤 + minStep++ // 跳跃次数+1 + lastDistance = curDistance // 记录时才可以更新 + } + curDistance = max(nums[i]+i, curDistance) // 更新当前可达的最大范围 + } + return minStep +} +``` + ```go // 贪心版本一 func jump(nums []int) int { From 93ab9befe85fdd3056765b3414b8b442a7f2258d Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:51:02 +1300 Subject: [PATCH 0903/1533] =?UTF-8?q?Update=200225.=E7=94=A8=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20=E5=A2=9E=E5=8A=A0C#?= =?UTF-8?q?=E5=8D=95=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\345\256\236\347\216\260\346\240\210.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index d89bf44bf4..6900e66869 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -1046,6 +1046,8 @@ class MyStack() { ### C#: +> 双队列 + ```csharp public class MyStack { Queue queue1; @@ -1080,6 +1082,54 @@ public class MyStack { } ``` +> 单队列 + +```c# +/* + * @lc app=leetcode id=225 lang=csharp + * 版本二:单队列 + * [225] Implement Stack using Queues + */ + +// @lc code=start +public class MyStack { + Queue myQueue; + public MyStack() { + myQueue = new Queue(); + } + + public void Push(int x) { + myQueue.Enqueue(x); + } + + //使用一个队列实现 + public int Pop() { + //一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。 + for (var i = 0; i < myQueue.Count-1; i++) + { + myQueue.Enqueue(myQueue.Dequeue()); + } + return myQueue.Dequeue(); + } + + //复用Pop()的代码 + public int Top() { + int res = Pop(); + myQueue.Enqueue(res); + return res; + } + + public bool Empty() { + return (myQueue.Count == 0); + } +} + +// @lc code=end + +``` + + + ### PHP: > 双队列 @@ -1203,3 +1253,4 @@ impl MyStack { + From e4db0542d4d8703df031e9e6a158a02a3467b219 Mon Sep 17 00:00:00 2001 From: nuo Date: Sun, 7 Jan 2024 05:07:06 +0800 Subject: [PATCH 0904/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E5=8E=9F?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BD=BF=E7=94=A8=E4=BA=86=E9=9D=9E=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=9C=9F=E5=B8=B8=E9=87=8F=E5=A3=B0=E6=98=8E=E6=95=B0?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现将int next[needle.size()];替换为vector next(needle.size()); --- "problems/0028.\345\256\236\347\216\260strStr.md" | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index bf4ad600c3..27ef761c5d 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -425,8 +425,8 @@ public: if (needle.size() == 0) { return 0; } - int next[needle.size()]; - getNext(next, needle); + vector next(needle.size()); + getNext(&next[0], needle); int j = -1; // // 因为next数组里记录的起始位置为-1 for (int i = 0; i < haystack.size(); i++) { // 注意i就从0开始 while(j >= 0 && haystack[i] != needle[j + 1]) { // 不匹配 @@ -524,8 +524,8 @@ public: if (needle.size() == 0) { return 0; } - int next[needle.size()]; - getNext(next, needle); + vector next(needle.size()); + getNext(&next[0], needle); int j = 0; for (int i = 0; i < haystack.size(); i++) { while(j > 0 && haystack[i] != needle[j]) { @@ -1428,4 +1428,3 @@ public int[] GetNext(string needle) - From 20f331f9043725ad9c39d27bf9912c441b6bcf48 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 7 Jan 2024 10:04:45 +0800 Subject: [PATCH 0905/1533] =?UTF-8?q?Update=200763.=E5=88=92=E5=88=86?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E5=8C=BA=E9=97=B4=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\345\214\272\351\227\264.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 41314456c3..4e9ec578bc 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -404,6 +404,32 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public IList PartitionLabels(string s) + { + int[] location = new int[27]; + for (int i = 0; i < s.Length; i++) + { + location[s[i] - 'a'] = i; + } + List res = new List(); + int left = 0, right = 0; + for (int i = 0; i < s.Length; i++) + { + right = Math.Max(right, location[s[i] - 'a']); + if (i == right) + { + res.Add(right - left + 1); + left = i + 1; + } + } + return res; + } +} +```

From 98827c14d0efdceaeb6ebed14f82c4c196e01f5c Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 8 Jan 2024 10:10:12 +0800 Subject: [PATCH 0906/1533] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8C=BA=E9=97=B4=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\345\271\266\345\214\272\351\227\264.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 95781b1a1d..122e783a27 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -336,6 +336,32 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int[][] Merge(int[][] intervals) + { + if (intervals.Length == 0) + return intervals; + Array.Sort(intervals, (a, b) => a[0] - b[0]); + List> res = new List>(); + res.Add(intervals[0].ToList()); + for (int i = 1; i < intervals.Length; i++) + { + if (res[res.Count - 1][1] >= intervals[i][0]) + { + res[res.Count - 1][1] = Math.Max(res[res.Count - 1][1], intervals[i][1]); + } + else + { + res.Add(intervals[i].ToList()); + } + } + return res.Select(x => x.ToArray()).ToArray(); + } +} +```

From 474392c521ceb53ea8eba405fb4335771047ba14 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 8 Jan 2024 15:35:22 +0800 Subject: [PATCH 0907/1533] Update --- "problems/kama53.\345\257\273\345\256\235.md" | 3 - problems/qita/shejimoshi.md | 55 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 problems/qita/shejimoshi.md diff --git "a/problems/kama53.\345\257\273\345\256\235.md" "b/problems/kama53.\345\257\273\345\256\235.md" index 08fc9a18c3..3a4b8b274c 100644 --- "a/problems/kama53.\345\257\273\345\256\235.md" +++ "b/problems/kama53.\345\257\273\345\256\235.md" @@ -1,7 +1,4 @@ -思考一下边的权值为负数的情况 - - # 寻宝 [卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) diff --git a/problems/qita/shejimoshi.md b/problems/qita/shejimoshi.md new file mode 100644 index 0000000000..cf980e45f6 --- /dev/null +++ b/problems/qita/shejimoshi.md @@ -0,0 +1,55 @@ + +关于设计模式的学习,大家应该还是看书或者看博客,但却没有一个边学边练的学习环境。 + +学完了一种设计模式 是不是应该去练一练? + +所以卡码网 针对 23种设计,**推出了 23道编程题目,来帮助大家练习设计模式**。 + +

+ +这里的23到编程题目对应了 23种这几模式。 例如第一题,小明的购物车,就是单例模式: + +
+ +区别于网上其他教程,本教程的特点是: + +* **23种设计模式全覆盖**,涵盖了所有Gang of Four设计模式,包括创建型、结构型和行为型设计模式。 +* 通过23道简单而实用的例子,**以刷算法题的形式了解每种设计模式的概念、结构和应用场景**。 +* **为每个设计模式提供清晰的文字解释、结构图和代码演示**,帮助你更好地理解和实践。 +* **难度安排循序渐进**,从基础的、常用的设计模式逐步深入。 + +这样的一个学习体验,要收费吗? + +**免费的**! + +相信录友们可能还没有这种学习设计模式的体验,快去卡码网(kamacoder.com)上体验吧。 + +23道 设计模式的题目给大家出了,那么是不是得安排上对应的讲解? + +**当然安排**! + +针对每道题目,还给大家编写了一套 23种设计模式精讲,已经开源到Github上: + +> https://github.com/youngyangyang04/kama-DesignPattern + +支持Java,Python,Go,C++ 版本,也欢迎大家去Github上提交PR,补充其他语言版本。 + +所以题解也免费开放给录友! + +同时还给全部整理到PDF上,这份PDF,我们写的很用心了,来个大家截个图: + +
+ +
+ +
+ +
+ +关于设计模式的题目,大家现在就可以去 卡码网(kamacoder)去做了。 + +关于这23道题目对应 设计模式精讲 PDF,也免费分享给录友们,大家可以加我的企业微信获取: +
+ +已经有我企业微信的录友,直接发:设计模式,这四个字就好,我会直接发你。 + From 383239d847b25523c086618a1582fe508b804c2a Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:43:05 +0800 Subject: [PATCH 0908/1533] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/qita/join.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/qita/join.md b/problems/qita/join.md index 3b3e2d4029..a2698e66cf 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -156,9 +156,9 @@ python代码 虽然我主张没有绝对正确的代码风格,但既然是给LeetCode-Master提交代码,尽量遵循Google编程规范。 -经常看我的代码的录友应该都知道,我的代码格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 +经常看我的代码的录友应该都知道,我的代码风格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 -大家提交代码的时候遇到规范性问题,例如哪里应该由空格,哪里没有空格,可以参考我的代码来。 +大家提交代码的时候遇到规范性问题,例如哪里应该有空格,哪里没有空格,可以参考我的代码来。 有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 From 22f42b079bcdee143d32fc905636371cd0ad9aec Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 9 Jan 2024 10:13:33 +0800 Subject: [PATCH 0909/1533] Update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 15b10bfa7a..b25b102dfe 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) * [C++语言基础课](https://kamacoder.com/course.php?course_id=1) * [Java语言基础课](https://kamacoder.com/course.php?course_id=2) + * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) * 项目 * [基于跳表的轻量级KV存储引擎](https://github.com/youngyangyang04/Skiplist-CPP) From 6b38053a63c0a1896ac6ec7905fc17a0a770f91b Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 9 Jan 2024 10:47:42 +0800 Subject: [PATCH 0910/1533] =?UTF-8?q?Update=200738.=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\347\232\204\346\225\260\345\255\227.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index c2215cf633..400dc90daa 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -392,6 +392,30 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int MonotoneIncreasingDigits(int n) + { + char[] s = n.ToString().ToCharArray(); + int flag = s.Length; + for (int i = s.Length - 1; i > 0; i--) + { + if (s[i - 1] > s[i]) + { + flag = i; + s[i - 1]--; + } + } + for (int i = flag; i < s.Length; i++) + { + s[i] = '9'; + } + return int.Parse(new string(s)); + } +} +```

From f292cbaf2ed861021db70f6b4c2aba7d028f2d64 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 10 Jan 2024 10:02:48 +0800 Subject: [PATCH 0911/1533] =?UTF-8?q?Update=200968.=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\214\345\217\211\346\240\221.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index be04bd4755..305f3ae555 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -726,6 +726,31 @@ impl Solution { } } +``` +### C# +```csharp +public class Solution +{ + public int res = 0; + public int MinCameraCover(TreeNode root) + { + if (Traversal(root) == 0) res++; + return res; + } + public int Traversal(TreeNode cur) + { + if (cur == null) return 2; + int left = Traversal(cur.left); + int right = Traversal(cur.right); + if (left == 2 && right == 2) return 0; + else if (left == 0 || right == 0) + { + res++; + return 1; + } + else return 2; + } +} ```

From 650e33bc709f59df007625ff524906c4bd59585c Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Thu, 11 Jan 2024 14:50:59 +1300 Subject: [PATCH 0912/1533] =?UTF-8?q?update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20=E5=A2=9E=E5=8A=A0C#?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\220\206\350\256\272\345\237\272\347\241\200.md" | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index e2c6d83c59..50d592a24c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -302,9 +302,19 @@ impl TreeNode { } } } -``` +``` +```c# +public class TreeNode +{ + public int val; + public TreeNode left; + public TreeNode right; + public TreeNode(int x) { val = x; } +} +```

+ From f6557d7ddc15aff15dfe4b5dcc956aca98e7324c Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 15:31:33 +0800 Subject: [PATCH 0913/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 71 ++++++++----------- 1 file changed, 31 insertions(+), 40 deletions(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index a4b93c3d4e..7431fbefba 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -389,50 +389,41 @@ function numIslands(grid: string[][]): number { ### Go ```go + +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} + func numIslands(grid [][]byte) int { - // 用1标记已访问 - visited := make([][]int, len(grid)) - for i := 0; i < len(visited); i++{ - visited[i] = make([]int, len(grid[0])) - } + res := 0 - var bfs func(x, y int) - bfs = func(x, y int){ - stack := make([][]int, 0) - stack = append(stack, []int{x, y}) - moveX := []int{1, -1, 0, 0} - moveY := []int{0, 0, 1, -1} - - for len(stack) != 0{ - node := stack[len(stack) - 1] - stack = stack[:len(stack) - 1] - - for i := 0; i < 4; i++{ - dx := moveX[i] + node[0] - dy := moveY[i] + node[1] - if dx < 0 || dx >= len(grid) || dy < 0 || dy >= len(grid[0]) || visited[dx][dy] == 1{ - continue - } - visited[dx][dy] = 1 - if grid[dx][dy] == '1'{ - stack = append(stack, []int{dx,dy}) - } - } - } - } + visited := make([][]bool, len(grid)) + for i := 0; i < len(grid); i++ { + visited[i] = make([]bool, len(grid[0])) + } - result := 0 - for i := 0; i < len(grid); i++{ - for j := 0; j < len(grid[0]); j++{ - if visited[i][j] == 0 && grid[i][j] == '1'{ - bfs(i, j) - visited[i][j] = 1 - result++ - } - } - } + for i, rows := range grid { + for j, v := range rows { + if v == '1' && !visited[i][j] { + res++ + dfs(grid, visited, i, j) + } + } + } + + return res +} + +func dfs(grid [][]byte, visited [][]bool, i, j int) { + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + if grid[x][y] == '1' && !visited[x][y] { + visited[x][y] = true + dfs(grid, visited, x, y) + } + } - return result } ``` From 0dc7f2f76565da3796830987274f7e8c7e2e4002 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 16:56:54 +0800 Subject: [PATCH 0914/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md=20for=20golan?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\345\271\277\346\220\234\347\211\210.md" | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index a7dd117b3e..5c77ed0597 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -321,11 +321,54 @@ function numIslands2(grid: string[][]): number { } ``` +### Go + +```go + +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} + +func numIslands(grid [][]byte) int { + res := 0 + + visited := make([][]bool, len(grid)) + for i := 0; i < len(grid); i++ { + visited[i] = make([]bool, len(grid[0])) + } + + for i, rows := range grid { + for j, v := range rows { + if v == '1' && !visited[i][j] { + res++ + bfs(grid, visited, i, j) + } + } + } + return res +} + +func bfs(grid [][]byte, visited [][]bool, i, j int) { + queue := [][2]int{{i, j}} + visited[i][j] = true // 标记已访问,循环中标记会导致重复 + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + for _, d := range DIRECTIONS { + x, y := cur[0]+d[0], cur[1]+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + if grid[x][y] == '1' && !visited[x][y] { + visited[x][y] = true + queue = append(queue, [2]int{x, y}) + } + } + } +} +``` ### Rust ```rust - use std::collections::VecDeque; impl Solution { const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; From 26fc4c41720aee859956309caa97bd6434120840 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 16:58:01 +0800 Subject: [PATCH 0915/1533] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 5c77ed0597..85471f73b5 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -199,7 +199,9 @@ class Solution { ### Python + BFS solution + ```python class Solution: def __init__(self): @@ -240,6 +242,7 @@ class Solution: ``` ### JavaScript + ```javascript var numIslands = function (grid) { let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 @@ -324,7 +327,6 @@ function numIslands2(grid: string[][]): number { ### Go ```go - var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} func numIslands(grid [][]byte) int { From 26c4004d74c2480a96722a563a5dd724cedb1e71 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 17:40:16 +0800 Subject: [PATCH 0916/1533] Apply suggestions from code review --- ...225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 7431fbefba..18442943ad 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -413,13 +413,13 @@ func numIslands(grid [][]byte) int { } func dfs(grid [][]byte, visited [][]bool, i, j int) { + visited[x][y] = true for _, d := range DIRECTIONS { x, y := i+d[0], j+d[1] if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { continue } if grid[x][y] == '1' && !visited[x][y] { - visited[x][y] = true dfs(grid, visited, x, y) } } From a0790023246d80328b377517a1d83b7580fa7dc7 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 18:00:20 +0800 Subject: [PATCH 0917/1533] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md=20for=20go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 74470ae5e6..a87acb8231 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -428,6 +428,150 @@ var maxAreaOfIsland = function (grid) { }; ``` +### Go + +dsf: 版本一 + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} +var count int = 0 + +func maxAreaOfIsland(grid [][]int) int { + res := 0 + visited := make([][]bool, len(grid)) + for i := 0; i < len(grid); i++ { + visited[i] = make([]bool, len(grid[0])) + } + + for i, rows := range grid { + for j, v := range rows { + if v == 1 && !visited[i][j] { + // 第一种写法,重制 count,必定有 1 个 + count = 1 + dfs(grid, visited, i, j) + res = max(res, count) + } + + } + } + + return res +} + +// 第一种写法 +func dfs(grid [][]int, visited [][]bool, i, j int) { + visited[i][j] = true // 标记已访问,循环中未标记会导致重复 + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + if grid[x][y] == 1 && !visited[x][y] { + count++ + dfs(grid, visited, x, y) + } + } +} +``` + +dfs:版本二 + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} +var count int = 0 + +func maxAreaOfIsland(grid [][]int) int { + res := 0 + visited := make([][]bool, len(grid)) + for i := 0; i < len(grid); i++ { + visited[i] = make([]bool, len(grid[0])) + } + + for i, rows := range grid { + for j, v := range rows { + if v == 1 && !visited[i][j] { + // 第二种写法 + count = 0 + dfs(grid, visited, i, j) + res = max(res, count) + } + + } + } + + return res +} + +// 第二种写法 +func dfs(grid [][]int, visited [][]bool, i, j int) { + if visited[i][j] || grid[i][j] == 0 { + return + } + visited[i][j] = true + count++ + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + dfs(grid, visited, x, y) + } +} +``` + +bfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} +var count int = 0 + +func maxAreaOfIsland(grid [][]int) int { + res := 0 + visited := make([][]bool, len(grid)) + for i := 0; i < len(grid); i++ { + visited[i] = make([]bool, len(grid[0])) + } + + for i, rows := range grid { + for j, v := range rows { + if v == 1 && !visited[i][j] { + // 第一种写法,重制 count,必定有 1 个 + // count = 1 + // 第二种写法以及 bfs + count = 0 + // dfs(grid, visited, i, j) + bfs(grid, visited, i, j) + res = max(res, count) + } + + } + } + + return res +} + +// bfs +func bfs(grid [][]int, visited [][]bool, i, j int) { + visited[i][j] = true + count++ + queue := [][2]int{{i, j}} + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + for _, d := range DIRECTIONS { + x, y := cur[0]+d[0], cur[1]+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + if grid[x][y] == 1 && !visited[x][y] { + count++ + queue = append(queue, [2]int{x, y}) + visited[x][y] = true + } + } + } +} +``` ### Rust From 909971eb8b9bb5befb1c6ef25b08b754d09b3435 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 18:01:34 +0800 Subject: [PATCH 0918/1533] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\204\346\234\200\345\244\247\351\235\242\347\247\257.md" | 4 ---- 1 file changed, 4 deletions(-) diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index a87acb8231..87b1b5bbbb 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -535,11 +535,7 @@ func maxAreaOfIsland(grid [][]int) int { for i, rows := range grid { for j, v := range rows { if v == 1 && !visited[i][j] { - // 第一种写法,重制 count,必定有 1 个 - // count = 1 - // 第二种写法以及 bfs count = 0 - // dfs(grid, visited, i, j) bfs(grid, visited, i, j) res = max(res, count) } From b13487089cb782412c156836be0dc00fef53d1da Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Thu, 11 Jan 2024 23:41:09 +1300 Subject: [PATCH 0919/1533] =?UTF-8?q?update=200116.=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88?= =?UTF-8?q?.md=20=E5=A2=9E=E5=8A=A0C#=E8=BF=AD=E4=BB=A3=E5=92=8C=E9=80=92?= =?UTF-8?q?=E5=BD=92=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\347\202\271\346\214\207\351\222\210.md" | 77 +++++++++++++++++++ ...22\345\275\222\351\201\215\345\216\206.md" | 1 + 2 files changed, 78 insertions(+) diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" index 003ef75afe..60ea9210a2 100644 --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -358,7 +358,84 @@ function connect(root: NodePro | null): NodePro | null { }; ``` +```csharp +//递归 +public class Solution { + public Node Connect(Node root) { + if (root == null) { + return null; + } + + ConnectNodes(root.left, root.right); + + return root; + } + + private void ConnectNodes(Node node1, Node node2) { + if (node1 == null || node2 == null) { + return; + } + + // 将左子节点的 next 指向右子节点 + node1.next = node2; + + // 递归连接当前节点的左右子节点 + ConnectNodes(node1.left, node1.right); + ConnectNodes(node2.left, node2.right); + + // 连接跨越父节点的两个子树 + ConnectNodes(node1.right, node2.left); + } +} + + +// 迭代 +public class Solution +{ + public Node Connect(Node root) + { + Queue que = new Queue(); + + if (root != null) + { + que.Enqueue(root); + } + while (que.Count > 0) + { + + var queSize = que.Count; + for (int i = 0; i < queSize; i++) + { + var cur = que.Dequeue(); + + // 当这个节点不是这一层的最后的节点 + if (i != queSize - 1) + { + // 当前节点指向下一个节点 + cur.next = que.Peek(); + } + // 否则指向空 + else + { + cur.next = null; + } + + if (cur.left != null) + { + que.Enqueue(cur.left); + } + if (cur.right != null) + { + que.Enqueue(cur.right); + } + } + } + + return root; + } +} +```

diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 6dad6e56de..b6dd7b6f1f 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -623,3 +623,4 @@ public void Traversal(TreeNode cur, IList res) + From e36c15a01cf93e4ea2d30545c485f6c6969650aa Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Thu, 11 Jan 2024 21:43:31 +0800 Subject: [PATCH 0920/1533] =?UTF-8?q?Update=20kama54.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 2b3d53de22..f5e30a704f 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -56,36 +56,36 @@ C++代码如下: ```CPP -#include +#include using namespace std; int main() { string s; while (cin >> s) { + int sOldIndex = s.size() - 1; int count = 0; // 统计数字的个数 - int sOldSize = s.size(); for (int i = 0; i < s.size(); i++) { if (s[i] >= '0' && s[i] <= '9') { count++; } } - // 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小 + // 扩充字符串s的大小,也就是将每个数字替换成"number"之后的大小 s.resize(s.size() + count * 5); - int sNewSize = s.size(); - // 从后先前将空格替换为"number" - for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) { - if (s[j] > '9' || s[j] < '0') { - s[i] = s[j]; + int sNewIndex = s.size() - 1; + // 从后往前将数字替换为"number" + while (sOldIndex >= 0) { + if (s[sOldIndex] >= '0' && s[sOldIndex] <= '9') { + s[sNewIndex--] = 'r'; + s[sNewIndex--] = 'e'; + s[sNewIndex--] = 'b'; + s[sNewIndex--] = 'm'; + s[sNewIndex--] = 'u'; + s[sNewIndex--] = 'n'; } else { - s[i] = 'r'; - s[i - 1] = 'e'; - s[i - 2] = 'b'; - s[i - 3] = 'm'; - s[i - 4] = 'u'; - s[i - 5] = 'n'; - i -= 5; + s[sNewIndex--] = s[sOldIndex]; } + sOldIndex--; } - cout << s << endl; + cout << s << endl; } } From 49690a1712142e0739797b34c7422b155b6ae4e5 Mon Sep 17 00:00:00 2001 From: WmW Date: Fri, 12 Jan 2024 16:43:39 +0800 Subject: [PATCH 0921/1533] =?UTF-8?q?update=20455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2;=20=E4=BB=A3=E7=A0=81=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\206\345\217\221\351\245\274\345\271\262.md" | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 778adc94a3..da4fd392b9 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -73,9 +73,9 @@ public: } }; ``` -* 时间复杂度:O(nlogn) -* 空间复杂度:O(1) +- 时间复杂度:O(nlogn) +- 空间复杂度:O(1) 从代码中可以看出我用了一个 index 来控制饼干数组的遍历,遍历饼干并没有再起一个 for 循环,而是采用自减的方式,这也是常用的技巧。 @@ -119,9 +119,9 @@ public: } }; ``` -* 时间复杂度:O(nlogn) -* 空间复杂度:O(1) +- 时间复杂度:O(nlogn) +- 空间复杂度:O(1) 细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。 @@ -177,7 +177,9 @@ class Solution { ``` ### Python + 贪心 大饼干优先 + ```python class Solution: def findContentChildren(self, g, s): @@ -192,7 +194,9 @@ class Solution: return result ``` + 贪心 小饼干优先 + ```python class Solution: def findContentChildren(self, g, s): @@ -229,7 +233,7 @@ func findContentChildren(g []int, s []int) int { ### Rust ```rust -pub fn find_content_children(mut children: Vec, mut cookie: Vec) -> i32 { +pub fn find_content_children(mut children: Vec, mut cookies: Vec) -> i32 { children.sort(); cookies.sort(); @@ -378,7 +382,9 @@ object Solution { } } ``` + ### C# + ```csharp public class Solution { From f25f062407a045d083e40d3174c8dc156636eb79 Mon Sep 17 00:00:00 2001 From: WmW Date: Fri, 12 Jan 2024 16:45:27 +0800 Subject: [PATCH 0922/1533] =?UTF-8?q?update=20455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2;=20=E4=BB=A3=E7=A0=81=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...210\206\345\217\221\351\245\274\345\271\262.md" | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index da4fd392b9..6ae206dba2 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -73,9 +73,9 @@ public: } }; ``` +* 时间复杂度:O(nlogn) +* 空间复杂度:O(1) -- 时间复杂度:O(nlogn) -- 空间复杂度:O(1) 从代码中可以看出我用了一个 index 来控制饼干数组的遍历,遍历饼干并没有再起一个 for 循环,而是采用自减的方式,这也是常用的技巧。 @@ -119,9 +119,9 @@ public: } }; ``` +* 时间复杂度:O(nlogn) +* 空间复杂度:O(1) -- 时间复杂度:O(nlogn) -- 空间复杂度:O(1) 细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。 @@ -177,9 +177,7 @@ class Solution { ``` ### Python - 贪心 大饼干优先 - ```python class Solution: def findContentChildren(self, g, s): @@ -194,9 +192,7 @@ class Solution: return result ``` - 贪心 小饼干优先 - ```python class Solution: def findContentChildren(self, g, s): @@ -382,9 +378,7 @@ object Solution { } } ``` - ### C# - ```csharp public class Solution { From 29eb6f680f73bcee4d7039864be8bf1d58179adf Mon Sep 17 00:00:00 2001 From: Chenxue3 <115330251+XueshanChen@users.noreply.github.com> Date: Fri, 12 Jan 2024 21:45:48 +1300 Subject: [PATCH 0923/1533] =?UTF-8?q?update=200104.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6.md:=20Ad?= =?UTF-8?q?d=20559.n=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=20C#=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index f4c6cfc8f4..1f55f197e5 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -1033,6 +1033,9 @@ impl Solution { } ``` ### C# + +0104.二叉树的最大深度 + ```csharp // 递归法 public int MaxDepth(TreeNode root) { @@ -1088,7 +1091,76 @@ public int MaxDepth(TreeNode root) } ``` +559.n叉树的最大深度 +递归法 +```csharp + /* + 递归法 + */ + public class Solution { + public int MaxDepth(Node root) { + int res = 0; + /* 终止条件 */ + if(root == null){ + return 0; + } + + /* logic */ + // 遍历当前节点的子节点 + for (int i = 0; i < root.children.Count; i++) + { + res = Math.Max(res, MaxDepth(root.children[i])); + } + return res + 1; + } + } + // @lc code=end +``` + 迭代法(层序遍历) +```csharp + /* + 迭代法 + */ + public class Solution + { + public int MaxDepth(Node root) + { + Queue que = new Queue(); // 使用泛型队列存储节点 + + int res = 0; + + if(root != null){ + que.Enqueue(root); // 将根节点加入队列 + } + while (que.Count > 0) + { + int size = que.Count; // 获取当前层的节点数 + res++; // 深度加一 + + for (int i = 0; i < size; i++) + { + // 每一层的遍历 + + var curNode = que.Dequeue(); // 取出队列中的节点 + for (int j = 0; j < curNode.children.Count; j++) + { + if (curNode.children[j] != null) + { + que.Enqueue(curNode.children[j]); // 将子节点加入队列 + } + } + } + } + + return res; // 返回树的最大深度 + + } + } +``` + +

+ From 8618c4491541b7ed4ef945952f1646909a6005d5 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 13 Jan 2024 10:50:06 +0800 Subject: [PATCH 0924/1533] =?UTF-8?q?Update=200062.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\220\214\350\267\257\345\276\204.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 5131fdbbbd..207a66ee80 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -536,8 +536,29 @@ object Solution { ``` ### c# +```csharp +// 二维数组 +public class Solution +{ + public int UniquePaths(int m, int n) + { + int[,] dp = new int[m, n]; + for (int i = 0; i < m; i++) dp[i, 0] = 1; + for (int j = 0; j < n; j++) dp[0, j] = 1; + for (int i = 1; i < m; i++) + { + for (int j = 1; j < n; j++) + { + dp[i, j] = dp[i - 1, j] + dp[i, j - 1]; + } + } + return dp[m - 1, n - 1]; + } +} +``` ```csharp +// 一维数组 public class Solution { public int UniquePaths(int m, int n) From 5fbb5fdc2ca45d5e210f4f7a043a9da52ca1a1d3 Mon Sep 17 00:00:00 2001 From: David Gao Date: Sat, 13 Jan 2024 20:44:21 -0500 Subject: [PATCH 0925/1533] =?UTF-8?q?0322.=E9=87=8D=E6=96=B0=E5=AE=89?= =?UTF-8?q?=E6=8E=92=E8=A1=8C=E7=A8=8B.md=20=E8=A7=A3=E5=86=B3python?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97=20=E5=9B=9E=E6=BA=AF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=AD=97=E5=85=B8=E6=96=B9=E6=B3=95=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 1473ed0505..036cfc51b5 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -449,34 +449,37 @@ class Solution: ``` 回溯 使用字典 ```python -from collections import defaultdict - class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]: - targets = defaultdict(list) # 构建机场字典 - for ticket in tickets: - targets[ticket[0]].append(ticket[1]) - for airport in targets: - targets[airport].sort() # 对目的地列表进行排序 - - path = ["JFK"] # 起始机场为"JFK" - self.backtracking(targets, path, len(tickets)) - return path - - def backtracking(self, targets, path, ticketNum): - if len(path) == ticketNum + 1: - return True # 找到有效行程 - - airport = path[-1] # 当前机场 - destinations = targets[airport] # 当前机场可以到达的目的地列表 - for i, dest in enumerate(destinations): - targets[airport].pop(i) # 标记已使用的机票 - path.append(dest) # 添加目的地到路径 - if self.backtracking(targets, path, ticketNum): - return True # 找到有效行程 - targets[airport].insert(i, dest) # 回溯,恢复机票 - path.pop() # 移除目的地 - return False # 没有找到有效行程 + self.adj = {} + + # sort by the destination alphabetically + # 根据航班每一站的重点字母顺序排序 + tickets.sort(key=lambda x:x[1]) + + # get all possible connection for each destination + # 罗列每一站的下一个可选项 + for u,v in tickets: + if u in self.adj: self.adj[u].append(v) + else: self.adj[u] = [v] + + # 从JFK出发 + self.result = [] + self.dfs("JFK") # start with JFK + + return self.result[::-1] # reverse to get the result + + def dfs(self, s): + # if depart city has flight and the flight can go to another city + while s in self.adj and len(self.adj[s]) > 0: + # 找到s能到哪里,选能到的第一个机场 + v = self.adj[s][0] # we go to the 1 choice of the city + # 在之后的可选项机场中去掉这个机场 + self.adj[s].pop(0) # get rid of this choice since we used it + # 从当前的新出发点开始 + self.dfs(v) # we start from the new airport + + self.result.append(s) # after append, it will back track to last node, thus the result list is in reversed order ``` 回溯 使用字典 逆序 From 08617fdff7d13cd4dcd8f512baccfdad5f64f766 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 14 Jan 2024 09:55:53 +0800 Subject: [PATCH 0926/1533] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=842=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\214\350\267\257\345\276\204II.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 8b842858e7..8c208ea865 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -734,6 +734,30 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int UniquePathsWithObstacles(int[][] obstacleGrid) + { + int m = obstacleGrid.Length; + int n = obstacleGrid[0].Length; + int[,] dp = new int[m, n]; + if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) return 0; + for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i, 0] = 1; + for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0, j] = 1; + for (int i = 1; i < m; i++) + { + for (int j = 1; j < n; j++) + { + if (obstacleGrid[i][j] == 1) continue; + dp[i, j] = dp[i - 1, j] + dp[i, j - 1]; + } + } + return dp[m - 1, n - 1]; + } +} +```

From 0076152aecfff72cae6c53f4c36b29faee62e47d Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 15 Jan 2024 09:52:08 +0800 Subject: [PATCH 0927/1533] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\225\260\346\213\206\345\210\206.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 2e17caf553..bbbd5c6379 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -496,6 +496,25 @@ class Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int IntegerBreak(int n) + { + int[] dp = new int[n + 1]; + dp[2] = 1; + for (int i = 3; i <= n; i++) + { + for (int j = 1; j <= i / 2; j++) + { + dp[i] = Math.Max(dp[i],Math.Max(j*(i-j),j*dp[i-j])); + } + } + return dp[n]; + } +} +```

From 4f1c28d3c1f1ee32c78dd0a6c96f228895a92b25 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 10:36:33 +0800 Subject: [PATCH 0928/1533] =?UTF-8?q?Update=201020.=E9=A3=9E=E5=9C=B0?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8F.md=20about=20golang?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\232\204\346\225\260\351\207\217.md" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 29c237e57b..679c1ca9cd 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -491,6 +491,60 @@ class Solution: return ans ``` +### Go + +dfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} +var count int = 0 + +func numEnclaves(grid [][]int) int { + rows, cols := len(grid), len(grid[0]) + // 行 + for i := range grid[0] { + if grid[0][i] == 1 { + dfs(grid, 0, i) + } + if grid[rows-1][i] == 1 { + dfs(grid, rows-1, i) + } + } + // 列 + for j := range grid { + if grid[j][0] == 1 { + dfs(grid, j, 0) + } + if grid[j][cols-1] == 1 { + dfs(grid, j, cols-1) + } + } + count = 0 + for i := range grid { + for j := range grid[0] { + if grid[i][j] == 1 { + dfs(grid, i, j) + } + } + } + return count +} + +func dfs(grid [][]int, i, j int) { + grid[i][j] = 0 + count++ + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + if grid[x][y] == 1 { + dfs(grid, x, y) + } + } +} +``` + ### Rust dfs: From 8823181498afb51aac908159d0cb1b31caa5b1fc Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 10:41:57 +0800 Subject: [PATCH 0929/1533] =?UTF-8?q?Update=201020.=E9=A3=9E=E5=9C=B0?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\232\204\346\225\260\351\207\217.md" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 679c1ca9cd..3488777ad2 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -545,6 +545,66 @@ func dfs(grid [][]int, i, j int) { } ``` +bfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} +var count int = 0 + +func numEnclaves(grid [][]int) int { + rows, cols := len(grid), len(grid[0]) + // 行 + for i := range grid[0] { + if grid[0][i] == 1 { + bfs(grid, 0, i) + } + if grid[rows-1][i] == 1 { + bfs(grid, rows-1, i) + } + } + // 列 + for j := range grid { + if grid[j][0] == 1 { + bfs(grid, j, 0) + } + if grid[j][cols-1] == 1 { + bfs(grid, j, cols-1) + } + } + count = 0 + for i := range grid { + for j := range grid[0] { + if grid[i][j] == 1 { + bfs(grid, i, j) + } + } + } + return count +} + +func bfs(grid [][]int, i, j int) { + queue := [][]int{} + queue = append(queue, []int{i, j}) + grid[i][j] = 0 + count++ + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + for _, d := range DIRECTIONS { + x, y := cur[0]+d[0], cur[1]+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + if grid[x][y] == 1 { + count++ + queue = append(queue, []int{x, y}) + grid[x][y] = 0 + } + } + } +} +``` + ### Rust dfs: From 73c06613f7f9614df170667aa6bef87f0dcca3d4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 17:24:43 +0800 Subject: [PATCH 0930/1533] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4?= =?UTF-8?q?=E7=BB=95=E7=9A=84=E5=8C=BA=E5=9F=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\232\204\345\214\272\345\237\237.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index 8014c0c8bf..e2185a1789 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -561,6 +561,58 @@ function solve(board) { } ``` +### Go + +```dfs +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} + +func solve(board [][]byte) { + rows, cols := len(board), len(board[0]) + // 列 + for i := 0; i < rows; i++ { + if board[i][0] == 'O' { + dfs(board, i, 0) + } + if board[i][cols-1] == 'O' { + dfs(board, i, cols-1) + } + } + // 行 + for j := 0; j < cols; j++ { + if board[0][j] == 'O' { + dfs(board, 0, j) + } + if board[rows-1][j] == 'O' { + dfs(board, rows-1, j) + } + } + + for _, r := range board { + for j, c := range r { + if c == 'A' { + r[j] = 'O' + } + if c == 'O' { + r[j] = 'X' + } + } + } +} + +func dfs(board [][]byte, i, j int) { + board[i][j] = 'A' + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(board) || y < 0 || y >= len(board[0]) { + continue + } + if board[x][y] == 'O' { + dfs(board, x, y) + } + } +} +``` +

From 52496f8fd6e65aa5073f48fcc6a545f18dffd218 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 17:29:34 +0800 Subject: [PATCH 0931/1533] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4?= =?UTF-8?q?=E7=BB=95=E7=9A=84=E5=8C=BA=E5=9F=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\232\204\345\214\272\345\237\237.md" | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index e2185a1789..352face80d 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -563,7 +563,9 @@ function solve(board) { ### Go -```dfs +dfs: + +```go var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} func solve(board [][]byte) { @@ -613,6 +615,64 @@ func dfs(board [][]byte, i, j int) { } ``` +bfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} + +func solve(board [][]byte) { + rows, cols := len(board), len(board[0]) + // 列 + for i := 0; i < rows; i++ { + if board[i][0] == 'O' { + bfs(board, i, 0) + } + if board[i][cols-1] == 'O' { + bfs(board, i, cols-1) + } + } + // 行 + for j := 0; j < cols; j++ { + if board[0][j] == 'O' { + bfs(board, 0, j) + } + if board[rows-1][j] == 'O' { + bfs(board, rows-1, j) + } + } + + for _, r := range board { + for j, c := range r { + if c == 'A' { + r[j] = 'O' + } + if c == 'O' { + r[j] = 'X' + } + } + } +} + +func bfs(board [][]byte, i, j int) { + queue := [][]int{{i, j}} + board[i][j] = 'A' + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + for _, d := range DIRECTIONS { + x, y := cur[0]+d[0], cur[1]+d[1] + if x < 0 || x >= len(board) || y < 0 || y >= len(board[0]) { + continue + } + if board[x][y] == 'O' { + board[x][y] = 'A' + queue = append(queue, []int{x, y}) + } + } + } +} +``` +

From 69377ddec1366c81dbe9343147e2c64de7bf3368 Mon Sep 17 00:00:00 2001 From: maolu Date: Mon, 15 Jan 2024 17:48:20 +0800 Subject: [PATCH 0932/1533] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=96=B0=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=EF=BC=9A=E8=9E=BA=E6=97=8B=E7=9F=A9=E9=98=B5=E5=AE=9A?= =?UTF-8?q?=E4=B9=894=E4=B8=AA=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++ .idea/encodings.xml | 4 ++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/leetcode-master.iml | 8 ++++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ ...72\346\227\213\347\237\251\351\230\265.md" | 41 +++++++++++++++++ ...\346\227\213\347\237\251\351\230\265II.md" | 44 +++++++++++++++++++ 9 files changed, 129 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/leetcode-master.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..13566b81b0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000..15a15b218a --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000..105ce2da2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml new file mode 100644 index 0000000000..d0876a78d0 --- /dev/null +++ b/.idea/leetcode-master.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..4d51924373 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..7c250acde5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..35eb1ddfbb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 44a7749d3f..85e6a9364b 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -306,6 +306,47 @@ class Solution(object): return result ``` +版本二:定义四个边界 +```python +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + if not matrix: + return [] + + rows = len(matrix) + cols = len(matrix[0]) + top, bottom, left, right = 0, rows - 1, 0, cols - 1 + print_list = [] + + while top <= bottom and left <= right: + # 从左到右 + for i in range(left, right + 1): + print_list.append(matrix[top][i]) + top += 1 + + # 从上到下 + for i in range(top, bottom + 1): + print_list.append(matrix[i][right]) + right -= 1 + + # 从右到左 + if top <= bottom: + for i in range(right, left - 1, -1): + print_list.append(matrix[bottom][i]) + bottom -= 1 + + # 从下到上 + if left <= right: + for i in range(bottom, top - 1, -1): + print_list.append(matrix[i][left]) + left += 1 + + return print_list +```

diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 58378ffcf2..8823a0d808 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -207,6 +207,50 @@ class Solution: return nums ``` +版本二:定义四个边界 +```python +class Solution(object): + def generateMatrix(self, n): + if n <= 0: + return [] + + # 初始化 n x n 矩阵 + matrix = [[0]*n for _ in range(n)] + + # 初始化边界和起始值 + top, bottom, left, right = 0, n-1, 0, n-1 + num = 1 + + while top <= bottom and left <= right: + # 从左到右填充上边界 + for i in range(left, right + 1): + matrix[top][i] = num + num += 1 + top += 1 + + # 从上到下填充右边界 + for i in range(top, bottom + 1): + matrix[i][right] = num + num += 1 + right -= 1 + + # 从右到左填充下边界 + + for i in range(right, left - 1, -1): + matrix[bottom][i] = num + num += 1 + bottom -= 1 + + # 从下到上填充左边界 + + for i in range(bottom, top - 1, -1): + matrix[i][left] = num + num += 1 + left += 1 + + return matrix +``` + ### JavaScript: ```javascript From 0e34b46a1777988cf1e618d4e227d02b7f1a82ae Mon Sep 17 00:00:00 2001 From: maolu Date: Mon, 15 Jan 2024 18:03:02 +0800 Subject: [PATCH 0933/1533] =?UTF-8?q?=E5=88=A0=E9=99=A4.idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 -------- .idea/encodings.xml | 4 ---- .idea/inspectionProfiles/profiles_settings.xml | 6 ------ .idea/leetcode-master.iml | 8 -------- .idea/misc.xml | 4 ---- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 7 files changed, 44 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/leetcode-master.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81b0..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 15a15b218a..0000000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2da2d..0000000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml deleted file mode 100644 index d0876a78d0..0000000000 --- a/.idea/leetcode-master.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 4d51924373..0000000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7c250acde5..0000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddfbb..0000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 47688ba24829cab212fa9d3d911199cbe8afee57 Mon Sep 17 00:00:00 2001 From: Chenxue3 <71321839+Chenxue3@users.noreply.github.com> Date: Mon, 15 Jan 2024 23:10:46 +1300 Subject: [PATCH 0934/1533] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91?= =?UTF-8?q?=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md=20-=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E8=BF=AD=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\350\247\222\347\232\204\345\200\274.md" | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index 3cdcc80163..d897bba13a 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -716,9 +716,55 @@ public void Traversal(TreeNode root, int depth) return; } ``` +```csharp +/* + * @lc app=leetcode id=513 lang=csharp + * 迭代法 + * [513] Find Bottom Left Tree Value + */ + +// @lc code=start +public class Solution +{ + public int FindBottomLeftValue(TreeNode root) + { + Queue que = new Queue(); + + if (root != null) + { + que.Enqueue(root); + } + + int ans = 0; + while (que.Count != 0) + { + + int size = que.Count; + for (var i = 0; i < size; i++) + { + var curNode = que.Peek(); + que.Dequeue(); + if(i == 0){ + ans = curNode.val; + } + if (curNode.left != null) + { + que.Enqueue(curNode.left); + } + if (curNode.right != null) + { + que.Enqueue(curNode.right); + } + } + + } + return ans; + } +} +// @lc code=end +```

- From 5ad279f73717ba8c7194a0b238a8bb5a0d97b2e7 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 15 Jan 2024 18:11:30 +0800 Subject: [PATCH 0935/1533] =?UTF-8?q?Update=200130.=E8=A2=AB=E5=9B=B4?= =?UTF-8?q?=E7=BB=95=E7=9A=84=E5=8C=BA=E5=9F=9F.md=20for=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\232\204\345\214\272\345\237\237.md" | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index 352face80d..1ddaaa7f83 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -593,6 +593,7 @@ func solve(board [][]byte) { for j, c := range r { if c == 'A' { r[j] = 'O' + continue } if c == 'O' { r[j] = 'X' @@ -645,6 +646,7 @@ func solve(board [][]byte) { for j, c := range r { if c == 'A' { r[j] = 'O' + continue } if c == 'O' { r[j] = 'X' @@ -673,6 +675,123 @@ func bfs(board [][]byte, i, j int) { } ``` +### Rust + +bfs: + +```rust +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn solve(board: &mut Vec>) { + let (rows, cols) = (board.len(), board[0].len()); + // 列 + for i in 0..rows { + if board[i][0] == 'O' { + Self::dfs(board, i, 0); + } + if board[i][cols - 1] == 'O' { + Self::dfs(board, i, cols - 1); + } + } + //行 + for j in 0..cols { + if board[0][j] == 'O' { + Self::dfs(board, 0, j); + } + if board[rows - 1][j] == 'O' { + Self::dfs(board, rows - 1, j); + } + } + + for v in board.iter_mut() { + for c in v.iter_mut() { + if *c == 'A' { + *c = 'O'; + continue; + } + if *c == 'O' { + *c = 'X'; + } + } + } + } + + pub fn dfs(board: &mut [Vec], i: usize, j: usize) { + board[i][j] = 'A'; + for (d1, d2) in Self::DIRECTIONS { + let (x, y) = (i as isize + d1, j as isize + d2); + if x < 0 || x >= board.len() as isize || y < 0 || y >= board[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if board[x][y] == 'O' { + Self::dfs(board, x, y); + } + } + } +} +``` + +bfs: + +```rust +use std::collections::VecDeque; +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn solve(board: &mut Vec>) { + let (rows, cols) = (board.len(), board[0].len()); + // 列 + for i in 0..rows { + if board[i][0] == 'O' { + Self::bfs(board, i, 0); + } + if board[i][cols - 1] == 'O' { + Self::bfs(board, i, cols - 1); + } + } + //行 + for j in 0..cols { + if board[0][j] == 'O' { + Self::bfs(board, 0, j); + } + if board[rows - 1][j] == 'O' { + Self::bfs(board, rows - 1, j); + } + } + + for v in board.iter_mut() { + for c in v.iter_mut() { + if *c == 'A' { + *c = 'O'; + continue; + } + if *c == 'O' { + *c = 'X'; + } + } + } + } + + pub fn bfs(board: &mut [Vec], i: usize, j: usize) { + let mut queue = VecDeque::from([(i, j)]); + board[i][j] = 'A'; + while let Some((i, j)) = queue.pop_front() { + for (d1, d2) in Self::DIRECTIONS { + let (x, y) = (i as isize + d1, j as isize + d2); + if x < 0 || x >= board.len() as isize || y < 0 || y >= board[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if board[x][y] == 'O' { + board[x][y] = 'A'; + queue.push_back((x, y)); + } + } + } + } +} +``` +

From c6d7c78dee47ee1eded34779c53227c3f49eeef8 Mon Sep 17 00:00:00 2001 From: Chenxue3 <71321839+Chenxue3@users.noreply.github.com> Date: Tue, 16 Jan 2024 01:37:06 +1300 Subject: [PATCH 0936/1533] =?UTF-8?q?0112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md=20=E6=B7=BB=E5=8A=A00113=20=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8CII=20C#=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 28134a7cf3..d45be3bd82 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -1523,8 +1523,64 @@ public bool HasPathSum(TreeNode root, int targetSum) return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val); } ``` +0113.路径总和: +```csharp +/* + * @lc app=leetcode id=113 lang=csharp + * 0113.路径总和 II + * [113] Path Sum II + * 递归法 + */ +public class Solution { + private List> result = new List>(); + private List path = new List(); + + // Main function to find paths with the given sum + public IList> PathSum(TreeNode root, int targetSum) { + result.Clear(); + path.Clear(); + if (root == null) return result.Select(list => list as IList).ToList(); + path.Add(root.val); // Add the root node to the path + traversal(root, targetSum - root.val); // Start the traversal + return result.Select(list => list as IList).ToList(); + } + + // Recursive function to traverse the tree and find paths + private void traversal(TreeNode node, int count) { + // If a leaf node is reached and the target sum is achieved + if (node.left == null && node.right == null && count == 0) { + result.Add(new List(path)); // Add a copy of the path to the result + return; + } + + // If a leaf node is reached and the target sum is not achieved, or if it's not a leaf node + if (node.left == null && node.right == null) return; + + // Traverse the left subtree + if (node.left != null) { + path.Add(node.left.val); + count -= node.left.val; + traversal(node.left, count); // Recursive call + count += node.left.val; // Backtrack + path.RemoveAt(path.Count - 1); // Backtrack + } + + // Traverse the right subtree + if (node.right != null) { + path.Add(node.right.val); + count -= node.right.val; + traversal(node.right, count); // Recursive call + count += node.right.val; // Backtrack + path.RemoveAt(path.Count - 1); // Backtrack + } + } +} + +// @lc code=end +```

+ From 57025dcf3765174b867bc9710b57acf8a85b5a92 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 16 Jan 2024 11:11:28 +0800 Subject: [PATCH 0937/1533] =?UTF-8?q?Update=200096.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index d109165b50..15b99083e0 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -328,6 +328,25 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int NumTrees(int n) + { + int[] dp = new int[n + 1]; + dp[0] = 1; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= i; j++) + { + dp[i] += dp[j - 1] * dp[i - j]; + } + } + return dp[n]; + } +} +```

From b19b832edf8cbe20ce3a9736e292c06b9d4fe979 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 16 Jan 2024 14:00:17 +0800 Subject: [PATCH 0938/1533] =?UTF-8?q?Update=200417.=E5=A4=AA=E5=B9=B3?= =?UTF-8?q?=E6=B4=8B=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 53ae14edfe..21f8a1ca03 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -562,7 +562,109 @@ function pacificAtlantic(heights) { } ``` +### go + +dfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} + +func pacificAtlantic(heights [][]int) [][]int { + res := make([][]int, 0) + pacific := make([][]bool, len(heights)) + atlantic := make([][]bool, len(heights)) + for i := 0; i < len(heights); i++ { + pacific[i] = make([]bool, len(heights[0])) + atlantic[i] = make([]bool, len(heights[0])) + } + // 列 + for i := 0; i < len(heights); i++ { + dfs(heights, pacific, i, 0) + dfs(heights, atlantic, i, len(heights[0])-1) + } + // 行 + for j := 0; j < len(heights[0]); j++ { + dfs(heights, pacific, 0, j) + dfs(heights, atlantic, len(heights)-1, j) + } + + for i := 0; i < len(heights); i++ { + for j := 0; j < len(heights[0]); j++ { + if pacific[i][j] && atlantic[i][j] { + res = append(res, []int{i, j}) + } + } + } + + return res +} + +func dfs(heights [][]int, visited [][]bool, i, j int) { + visited[i][j] = true + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[i][j] > heights[x][y] || visited[x][y] { + continue + } + + dfs(heights, visited, x, y) + } +} +``` +bfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} + +func pacificAtlantic(heights [][]int) [][]int { + res := make([][]int, 0) + pacific := make([][]bool, len(heights)) + atlantic := make([][]bool, len(heights)) + for i := 0; i < len(heights); i++ { + pacific[i] = make([]bool, len(heights[0])) + atlantic[i] = make([]bool, len(heights[0])) + } + // 列 + for i := 0; i < len(heights); i++ { + bfs(heights, pacific, i, 0) + bfs(heights, atlantic, i, len(heights[0])-1) + } + // 行 + for j := 0; j < len(heights[0]); j++ { + bfs(heights, pacific, 0, j) + bfs(heights, atlantic, len(heights)-1, j) + } + + for i := 0; i < len(heights); i++ { + for j := 0; j < len(heights[0]); j++ { + if pacific[i][j] && atlantic[i][j] { + res = append(res, []int{i, j}) + } + } + } + + return res +} + +func bfs(heights [][]int, visited [][]bool, i, j int) { + queue := make([][]int, 0) + queue = append(queue, []int{i, j}) + visited[i][j] = true + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + for _, d := range DIRECTIONS { + x, y := cur[0]+d[0], cur[1]+d[1] + if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[cur[0]][cur[1]] > heights[x][y] || visited[x][y] { + continue + } + queue = append(queue, []int{x, y}) + visited[x][y] = true + } + } +} +```

From f9c2d9677bdf6432c87b4c91b4596fc634599c93 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 16 Jan 2024 14:35:10 +0800 Subject: [PATCH 0939/1533] =?UTF-8?q?Update=200417.=E5=A4=AA=E5=B9=B3?= =?UTF-8?q?=E6=B4=8B=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 21f8a1ca03..8fe0f1b426 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -666,6 +666,110 @@ func bfs(heights [][]int, visited [][]bool, i, j int) { } ``` +### Rust + +dfs: + +```rust +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn pacific_atlantic(heights: Vec>) -> Vec> { + let (m, n) = (heights.len(), heights[0].len()); + let mut res = vec![]; + let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]); + + // 列 + for i in 0..m { + Self::dfs(&heights, &mut pacific, i, 0); + Self::dfs(&heights, &mut atlantic, i, n - 1); + } + + for j in 0..n { + Self::dfs(&heights, &mut pacific, 0, j); + Self::dfs(&heights, &mut atlantic, m - 1, j); + } + + for i in 0..m { + for j in 0..n { + if pacific[i][j] && atlantic[i][j] { + res.push(vec![i as i32, j as i32]); + } + } + } + + res + } + + pub fn dfs(heights: &[Vec], visited: &mut [Vec], i: usize, j: usize) { + visited[i][j] = true; + for (dx, dy) in Self::DIRECTIONS { + let (x, y) = (i as isize + dx, j as isize + dy); + if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if !visited[x][y] && heights[x][y] >= heights[i][j] { + Self::dfs(heights, visited, x, y); + } + } + } +} +``` + +bfs: + +```rust +use std::collections::VecDeque; + +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn pacific_atlantic(heights: Vec>) -> Vec> { + let (m, n) = (heights.len(), heights[0].len()); + let mut res = vec![]; + let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]); + + // 列 + for i in 0..m { + Self::bfs(&heights, &mut pacific, i, 0); + Self::bfs(&heights, &mut atlantic, i, n - 1); + } + + for j in 0..n { + Self::bfs(&heights, &mut pacific, 0, j); + Self::bfs(&heights, &mut atlantic, m - 1, j); + } + + for i in 0..m { + for j in 0..n { + if pacific[i][j] && atlantic[i][j] { + res.push(vec![i as i32, j as i32]); + } + } + } + + res + } + + pub fn bfs(heights: &[Vec], visited: &mut [Vec], i: usize, j: usize) { + let mut queue = VecDeque::from([(i, j)]); + visited[i][j] = true; + while let Some((i, j)) = queue.pop_front() { + for (dx, dy) in Self::DIRECTIONS { + let (x, y) = (i as isize + dx, j as isize + dy); + if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if !visited[x][y] && heights[x][y] >= heights[i][j] { + queue.push_back((x, y)); + visited[x][y] = true; + } + } + } + } +} +``` +

From 480d0d1f281e4ec87ff206cd399e469284fe8fca Mon Sep 17 00:00:00 2001 From: Yao Zu <1796381757@qq.com> Date: Tue, 16 Jan 2024 20:52:46 +0800 Subject: [PATCH 0940/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1.md=20Python=E7=89=88=E6=9C=AC=E5=85=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原代码块语言为python3,代码随想录网站无法正常渲染,改为python --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index b800c2320d..93ade51e45 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -216,7 +216,7 @@ class Solution: (版本六)使用count(简单易懂) -```python3 +```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: for char in ransomNote: From 064e582b1e74826e3b60b379aff9c3ee3c41d689 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 17 Jan 2024 10:37:45 +0800 Subject: [PATCH 0941/1533] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\345\222\214\345\255\220\351\233\206.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 81eb4c807a..71e01ae392 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -726,7 +726,35 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public bool CanPartition(int[] nums) + { + int sum = 0; + int[] dp = new int[10001]; + foreach (int num in nums) + { + sum += num; + } + if (sum % 2 == 1) return false; + int tartget = sum / 2; + for (int i = 0; i < nums.Length; i++) + { + for (int j = tartget; j >= nums[i]; j--) + { + dp[j] = Math.Max(dp[j], dp[j - nums[i]] + nums[i]); + } + } + if (dp[tartget] == tartget) + return true; + + return false; + } +} +```

From f0ac6f599c459a87c96f3adda3f9dce898bef810 Mon Sep 17 00:00:00 2001 From: WmW Date: Wed, 17 Jan 2024 10:48:03 +0800 Subject: [PATCH 0942/1533] =?UTF-8?q?update:=200435.=E6=97=A0=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E5=8C=BA=E9=97=B4=EF=BC=8Crust=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\217\240\345\214\272\351\227\264.md" | 191 ++++++++++-------- 1 file changed, 103 insertions(+), 88 deletions(-) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 1a33f98ea6..0bfda5fcaa 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -4,7 +4,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- # 435. 无重叠区间 [力扣题目链接](https://leetcode.cn/problems/non-overlapping-intervals/) @@ -16,19 +15,22 @@ 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。 示例 1: -* 输入: [ [1,2], [2,3], [3,4], [1,3] ] -* 输出: 1 -* 解释: 移除 [1,3] 后,剩下的区间没有重叠。 + +- 输入: [ [1,2], [2,3], [3,4], [1,3] ] +- 输出: 1 +- 解释: 移除 [1,3] 后,剩下的区间没有重叠。 示例 2: -* 输入: [ [1,2], [1,2], [1,2] ] -* 输出: 2 -* 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。 + +- 输入: [ [1,2], [1,2], [1,2] ] +- 输出: 2 +- 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。 示例 3: -* 输入: [ [1,2], [2,3] ] -* 输出: 0 -* 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 + +- 输入: [ [1,2], [2,3] ] +- 输出: 0 +- 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 ## 算法公开课 @@ -38,7 +40,7 @@ **相信很多同学看到这道题目都冥冥之中感觉要排序,但是究竟是按照右边界排序,还是按照左边界排序呢?** -其实都可以。主要就是为了让区间尽可能的重叠。 +其实都可以。主要就是为了让区间尽可能的重叠。 **我来按照右边界排序,从左向右记录非交叉区间的个数。最后用区间总数减去非交叉区间的个数就是需要移除的区间个数了**。 @@ -48,17 +50,17 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230201164134.png) -区间,1,2,3,4,5,6都按照右边界排好序。 +区间,1,2,3,4,5,6 都按照右边界排好序。 -当确定区间 1 和 区间2 重叠后,如何确定是否与 区间3 也重贴呢? +当确定区间 1 和 区间 2 重叠后,如何确定是否与 区间 3 也重贴呢? -就是取 区间1 和 区间2 右边界的最小值,因为这个最小值之前的部分一定是 区间1 和区间2 的重合部分,如果这个最小值也触达到区间3,那么说明 区间 1,2,3都是重合的。 +就是取 区间 1 和 区间 2 右边界的最小值,因为这个最小值之前的部分一定是 区间 1 和区间 2 的重合部分,如果这个最小值也触达到区间 3,那么说明 区间 1,2,3 都是重合的。 -接下来就是找大于区间1结束位置的区间,是从区间4开始。**那有同学问了为什么不从区间5开始?别忘了已经是按照右边界排序的了**。 +接下来就是找大于区间 1 结束位置的区间,是从区间 4 开始。**那有同学问了为什么不从区间 5 开始?别忘了已经是按照右边界排序的了**。 -区间4结束之后,再找到区间6,所以一共记录非交叉区间的个数是三个。 +区间 4 结束之后,再找到区间 6,所以一共记录非交叉区间的个数是三个。 -总共区间个数为6,减去非交叉区间的个数3。移除区间的最小数量就是3。 +总共区间个数为 6,减去非交叉区间的个数 3。移除区间的最小数量就是 3。 C++代码如下: @@ -84,8 +86,9 @@ public: } }; ``` -* 时间复杂度:O(nlog n) ,有一个快排 -* 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间 + +- 时间复杂度:O(nlog n) ,有一个快排 +- 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要 n 次递归调用。因此确实需要 O(n)的栈空间 大家此时会发现如此复杂的一个问题,代码实现却这么简单! @@ -93,11 +96,11 @@ public: ### 补充(1) -左边界排序可不可以呢? +左边界排序可不可以呢? -也是可以的,只不过 左边界排序我们就是直接求 重叠的区间,count为记录重叠区间数。 +也是可以的,只不过 左边界排序我们就是直接求 重叠的区间,count 为记录重叠区间数。 -```CPP +```CPP class Solution { public: static bool cmp (const vector& a, const vector& b) { @@ -108,9 +111,9 @@ public: sort(intervals.begin(), intervals.end(), cmp); int count = 0; // 注意这里从0开始,因为是记录重叠区间 int end = intervals[0][1]; // 记录区间分割点 - for (int i = 1; i < intervals.size(); i++) { + for (int i = 1; i < intervals.size(); i++) { if (intervals[i][0] >= end) end = intervals[i][1]; // 无重叠的情况 - else { // 重叠情况 + else { // 重叠情况 end = min(end, intervals[i][1]); count++; } @@ -120,9 +123,9 @@ public: }; ``` -其实代码还可以精简一下, 用 intervals[i][1] 替代 end变量,只判断 重叠情况就好 +其实代码还可以精简一下, 用 intervals[i][1] 替代 end 变量,只判断 重叠情况就好 -```CPP +```CPP class Solution { public: static bool cmp (const vector& a, const vector& b) { @@ -148,14 +151,14 @@ public: 本题其实和[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像,弓箭的数量就相当于是非交叉区间的数量,只要把弓箭那道题目代码里射爆气球的判断条件加个等号(认为[0,1][1,2]不是相邻区间),然后用总区间数减去弓箭数量 就是要移除的区间数量了。 -把[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)代码稍做修改,就可以AC本题。 +把[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)代码稍做修改,就可以 AC 本题。 ```CPP class Solution { public: // 按照区间右边界排序 static bool cmp (const vector& a, const vector& b) { - return a[1] < b[1]; // 右边界排序 + return a[1] < b[1]; // 右边界排序 } int eraseOverlapIntervals(vector>& intervals) { if (intervals.size() == 0) return 0; @@ -175,7 +178,8 @@ public: }; ``` -这里按照 左边界排序,或者按照右边界排序,都可以AC,原理是一样的。 +这里按照 左边界排序,或者按照右边界排序,都可以 AC,原理是一样的。 + ```CPP class Solution { public: @@ -204,8 +208,8 @@ public: ## 其他语言版本 +### Java -### Java ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { @@ -219,7 +223,7 @@ class Solution { continue; }else{ count++; - } + } } return intervals.length - count; } @@ -227,6 +231,7 @@ class Solution { ``` 按左边排序,不管右边顺序。相交的时候取最小的右边。 + ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { @@ -247,47 +252,53 @@ class Solution { } ``` -### Python +### Python + 贪心 基于左边界 + ```python class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: if not intervals: return 0 - + intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序 count = 0 # 记录重叠区间数量 - + for i in range(1, len(intervals)): if intervals[i][0] < intervals[i - 1][1]: # 存在重叠区间 intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界 count += 1 - + return count ``` -贪心 基于左边界 把452.用最少数量的箭引爆气球代码稍做修改 + +贪心 基于左边界 把 452.用最少数量的箭引爆气球代码稍做修改 + ```python class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: if not intervals: return 0 - + intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序 - + result = 1 # 不重叠区间数量,初始化为1,因为至少有一个不重叠的区间 - + for i in range(1, len(intervals)): if intervals[i][0] >= intervals[i - 1][1]: # 没有重叠 result += 1 else: # 重叠情况 intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界 - + return len(intervals) - result ``` -### Go + +### Go + ```go func eraseOverlapIntervals(intervals [][]int) int { sort.Slice(intervals, func(i, j int) bool { @@ -312,7 +323,9 @@ func min(a, b int) int { ``` ### Javascript + - 按右边界排序 + ```Javascript var eraseOverlapIntervals = function(intervals) { intervals.sort((a, b) => { @@ -329,27 +342,29 @@ var eraseOverlapIntervals = function(intervals) { count += 1 } } - + return intervals.length - count }; ``` + - 按左边界排序 + ```js -var eraseOverlapIntervals = function(intervals) { - // 按照左边界升序排列 - intervals.sort((a, b) => a[0] - b[0]) - let count = 1 - let end = intervals[intervals.length - 1][0] - // 倒序遍历,对单个区间来说,左边界越大越好,因为给前面区间的空间越大 - for(let i = intervals.length - 2; i >= 0; i--) { - if(intervals[i][1] <= end) { - count++ - end = intervals[i][0] - } +var eraseOverlapIntervals = function (intervals) { + // 按照左边界升序排列 + intervals.sort((a, b) => a[0] - b[0]); + let count = 1; + let end = intervals[intervals.length - 1][0]; + // 倒序遍历,对单个区间来说,左边界越大越好,因为给前面区间的空间越大 + for (let i = intervals.length - 2; i >= 0; i--) { + if (intervals[i][1] <= end) { + count++; + end = intervals[i][0]; } - // count 记录的是最大非重复区间的个数 - return intervals.length - count -} + } + // count 记录的是最大非重复区间的个数 + return intervals.length - count; +}; ``` ### TypeScript @@ -358,43 +373,43 @@ var eraseOverlapIntervals = function(intervals) { ```typescript function eraseOverlapIntervals(intervals: number[][]): number { - const length = intervals.length; - if (length === 0) return 0; - intervals.sort((a, b) => a[1] - b[1]); - let right: number = intervals[0][1]; - let count: number = 1; - for (let i = 1; i < length; i++) { - if (intervals[i][0] >= right) { - count++; - right = intervals[i][1]; - } + const length = intervals.length; + if (length === 0) return 0; + intervals.sort((a, b) => a[1] - b[1]); + let right: number = intervals[0][1]; + let count: number = 1; + for (let i = 1; i < length; i++) { + if (intervals[i][0] >= right) { + count++; + right = intervals[i][1]; } - return length - count; -}; + } + return length - count; +} ``` > 按左边界排序,从左往右遍历 ```typescript function eraseOverlapIntervals(intervals: number[][]): number { - if (intervals.length === 0) return 0; - intervals.sort((a, b) => a[0] - b[0]); - let right: number = intervals[0][1]; - let tempInterval: number[]; - let resCount: number = 0; - for (let i = 1, length = intervals.length; i < length; i++) { - tempInterval = intervals[i]; - if (tempInterval[0] >= right) { - // 未重叠 - right = tempInterval[1]; - } else { - // 有重叠,移除当前interval和前一个interval中右边界更大的那个 - right = Math.min(right, tempInterval[1]); - resCount++; - } + if (intervals.length === 0) return 0; + intervals.sort((a, b) => a[0] - b[0]); + let right: number = intervals[0][1]; + let tempInterval: number[]; + let resCount: number = 0; + for (let i = 1, length = intervals.length; i < length; i++) { + tempInterval = intervals[i]; + if (tempInterval[0] >= right) { + // 未重叠 + right = tempInterval[1]; + } else { + // 有重叠,移除当前interval和前一个interval中右边界更大的那个 + right = Math.min(right, tempInterval[1]); + resCount++; } - return resCount; -}; + } + return resCount; +} ``` ### Scala @@ -423,7 +438,7 @@ object Solution { ```Rust impl Solution { - pub fn erase_overlap_intervals(intervals: Vec>) -> i32 { + pub fn erase_overlap_intervals(mut intervals: Vec>) -> i32 { if intervals.is_empty() { return 0; } @@ -441,7 +456,9 @@ impl Solution { } } ``` + ### C# + ```csharp public class Solution { @@ -463,9 +480,7 @@ public class Solution } ``` -

- From 49e386d20ba95edeffba686584a6b123597ddbba Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 18 Jan 2024 10:28:55 +0800 Subject: [PATCH 0943/1533] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8F?= =?UTF-8?q?2=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\232\204\351\207\215\351\207\217II.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index cc66131734..4c3c01a037 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -472,6 +472,30 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int LastStoneWeightII(int[] stones) + { + int[] dp = new int[15001]; + int sum = 0; + foreach (int stone in stones) + { + sum += stone; + } + int target = sum / 2; + for (int i = 0; i < stones.Length; i++) + { + for (int j = target; j >= stones[i]; j--) + { + dp[j] = Math.Max(dp[j], dp[j - stones[i]] + stones[i]); + } + } + return sum - 2 * dp[target]; + } +} +```

From d8abbca4a877c4acd34744add2c69f940a4c5e38 Mon Sep 17 00:00:00 2001 From: qjd1774 Date: Thu, 18 Jan 2024 11:32:48 +0800 Subject: [PATCH 0944/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0kama54.=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E5=AD=97.md=20Javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 2b3d53de22..130c42f7a0 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -245,7 +245,60 @@ class Solution: return ''.join(lst) ``` ### JavaScript: +```js +const readline = require("readline"); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) + +function main() { + const num0 = "0".charCodeAt(); + const num9 = "9".charCodeAt(); + const a = "a".charCodeAt(); + const z = "z".charCodeAt(); + function isAZ(str) { + return str >= a && str <= z; + } + function isNumber(str) { + return str >= num0 && str <= num9; + } + rl.on("line", (input) => { + let n = 0; + for (let i = 0; i < input.length; i++) { + const val = input[i].charCodeAt(); + if (isNumber(val)) { + n+= 6; + } + if (isAZ(val)) { + n++; + } + } + const ans = new Array(n).fill(0); + let index = input.length - 1; + for (let i = n - 1; i >= 0; i--) { + const val = input[index].charCodeAt(); + if (isAZ(val)) { + ans[i] = input[index]; + } + if (isNumber(val)) { + ans[i] = "r"; + ans[i - 1] = "e"; + ans[i - 2] = "b"; + ans[i - 3] = "m"; + ans[i - 4] = "u"; + ans[i - 5] = "n"; + i -= 5; + } + index--; + } + console.log(ans.join("")); + }) +} +main(); +``` ### TypeScript: From 017a4ce4ad985c09fe31a3d92d6cf0dab24e0bf9 Mon Sep 17 00:00:00 2001 From: WmW Date: Thu, 18 Jan 2024 14:20:40 +0800 Subject: [PATCH 0945/1533] =?UTF-8?q?update:=200435.=E6=97=A0=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E5=8C=BA=E9=97=B4=EF=BC=8Crust=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\217\240\345\214\272\351\227\264.md" | 188 ++++++++---------- 1 file changed, 86 insertions(+), 102 deletions(-) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 0bfda5fcaa..cf9996ddef 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -4,6 +4,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 435. 无重叠区间 [力扣题目链接](https://leetcode.cn/problems/non-overlapping-intervals/) @@ -15,22 +16,19 @@ 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。 示例 1: - -- 输入: [ [1,2], [2,3], [3,4], [1,3] ] -- 输出: 1 -- 解释: 移除 [1,3] 后,剩下的区间没有重叠。 +* 输入: [ [1,2], [2,3], [3,4], [1,3] ] +* 输出: 1 +* 解释: 移除 [1,3] 后,剩下的区间没有重叠。 示例 2: - -- 输入: [ [1,2], [1,2], [1,2] ] -- 输出: 2 -- 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。 +* 输入: [ [1,2], [1,2], [1,2] ] +* 输出: 2 +* 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。 示例 3: - -- 输入: [ [1,2], [2,3] ] -- 输出: 0 -- 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 +* 输入: [ [1,2], [2,3] ] +* 输出: 0 +* 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 ## 算法公开课 @@ -40,7 +38,7 @@ **相信很多同学看到这道题目都冥冥之中感觉要排序,但是究竟是按照右边界排序,还是按照左边界排序呢?** -其实都可以。主要就是为了让区间尽可能的重叠。 +其实都可以。主要就是为了让区间尽可能的重叠。 **我来按照右边界排序,从左向右记录非交叉区间的个数。最后用区间总数减去非交叉区间的个数就是需要移除的区间个数了**。 @@ -50,17 +48,17 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230201164134.png) -区间,1,2,3,4,5,6 都按照右边界排好序。 +区间,1,2,3,4,5,6都按照右边界排好序。 -当确定区间 1 和 区间 2 重叠后,如何确定是否与 区间 3 也重贴呢? +当确定区间 1 和 区间2 重叠后,如何确定是否与 区间3 也重贴呢? -就是取 区间 1 和 区间 2 右边界的最小值,因为这个最小值之前的部分一定是 区间 1 和区间 2 的重合部分,如果这个最小值也触达到区间 3,那么说明 区间 1,2,3 都是重合的。 +就是取 区间1 和 区间2 右边界的最小值,因为这个最小值之前的部分一定是 区间1 和区间2 的重合部分,如果这个最小值也触达到区间3,那么说明 区间 1,2,3都是重合的。 -接下来就是找大于区间 1 结束位置的区间,是从区间 4 开始。**那有同学问了为什么不从区间 5 开始?别忘了已经是按照右边界排序的了**。 +接下来就是找大于区间1结束位置的区间,是从区间4开始。**那有同学问了为什么不从区间5开始?别忘了已经是按照右边界排序的了**。 -区间 4 结束之后,再找到区间 6,所以一共记录非交叉区间的个数是三个。 +区间4结束之后,再找到区间6,所以一共记录非交叉区间的个数是三个。 -总共区间个数为 6,减去非交叉区间的个数 3。移除区间的最小数量就是 3。 +总共区间个数为6,减去非交叉区间的个数3。移除区间的最小数量就是3。 C++代码如下: @@ -86,9 +84,8 @@ public: } }; ``` - -- 时间复杂度:O(nlog n) ,有一个快排 -- 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要 n 次递归调用。因此确实需要 O(n)的栈空间 +* 时间复杂度:O(nlog n) ,有一个快排 +* 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间 大家此时会发现如此复杂的一个问题,代码实现却这么简单! @@ -96,11 +93,11 @@ public: ### 补充(1) -左边界排序可不可以呢? +左边界排序可不可以呢? -也是可以的,只不过 左边界排序我们就是直接求 重叠的区间,count 为记录重叠区间数。 +也是可以的,只不过 左边界排序我们就是直接求 重叠的区间,count为记录重叠区间数。 -```CPP +```CPP class Solution { public: static bool cmp (const vector& a, const vector& b) { @@ -111,9 +108,9 @@ public: sort(intervals.begin(), intervals.end(), cmp); int count = 0; // 注意这里从0开始,因为是记录重叠区间 int end = intervals[0][1]; // 记录区间分割点 - for (int i = 1; i < intervals.size(); i++) { + for (int i = 1; i < intervals.size(); i++) { if (intervals[i][0] >= end) end = intervals[i][1]; // 无重叠的情况 - else { // 重叠情况 + else { // 重叠情况 end = min(end, intervals[i][1]); count++; } @@ -123,9 +120,9 @@ public: }; ``` -其实代码还可以精简一下, 用 intervals[i][1] 替代 end 变量,只判断 重叠情况就好 +其实代码还可以精简一下, 用 intervals[i][1] 替代 end变量,只判断 重叠情况就好 -```CPP +```CPP class Solution { public: static bool cmp (const vector& a, const vector& b) { @@ -151,14 +148,14 @@ public: 本题其实和[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像,弓箭的数量就相当于是非交叉区间的数量,只要把弓箭那道题目代码里射爆气球的判断条件加个等号(认为[0,1][1,2]不是相邻区间),然后用总区间数减去弓箭数量 就是要移除的区间数量了。 -把[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)代码稍做修改,就可以 AC 本题。 +把[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)代码稍做修改,就可以AC本题。 ```CPP class Solution { public: // 按照区间右边界排序 static bool cmp (const vector& a, const vector& b) { - return a[1] < b[1]; // 右边界排序 + return a[1] < b[1]; // 右边界排序 } int eraseOverlapIntervals(vector>& intervals) { if (intervals.size() == 0) return 0; @@ -178,8 +175,7 @@ public: }; ``` -这里按照 左边界排序,或者按照右边界排序,都可以 AC,原理是一样的。 - +这里按照 左边界排序,或者按照右边界排序,都可以AC,原理是一样的。 ```CPP class Solution { public: @@ -208,8 +204,8 @@ public: ## 其他语言版本 -### Java +### Java ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { @@ -223,7 +219,7 @@ class Solution { continue; }else{ count++; - } + } } return intervals.length - count; } @@ -231,7 +227,6 @@ class Solution { ``` 按左边排序,不管右边顺序。相交的时候取最小的右边。 - ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { @@ -252,53 +247,47 @@ class Solution { } ``` -### Python - +### Python 贪心 基于左边界 - ```python class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: if not intervals: return 0 - + intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序 count = 0 # 记录重叠区间数量 - + for i in range(1, len(intervals)): if intervals[i][0] < intervals[i - 1][1]: # 存在重叠区间 intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界 count += 1 - + return count ``` - -贪心 基于左边界 把 452.用最少数量的箭引爆气球代码稍做修改 - +贪心 基于左边界 把452.用最少数量的箭引爆气球代码稍做修改 ```python class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: if not intervals: return 0 - + intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序 - + result = 1 # 不重叠区间数量,初始化为1,因为至少有一个不重叠的区间 - + for i in range(1, len(intervals)): if intervals[i][0] >= intervals[i - 1][1]: # 没有重叠 result += 1 else: # 重叠情况 intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界 - + return len(intervals) - result ``` - -### Go - +### Go ```go func eraseOverlapIntervals(intervals [][]int) int { sort.Slice(intervals, func(i, j int) bool { @@ -323,9 +312,7 @@ func min(a, b int) int { ``` ### Javascript - - 按右边界排序 - ```Javascript var eraseOverlapIntervals = function(intervals) { intervals.sort((a, b) => { @@ -342,29 +329,27 @@ var eraseOverlapIntervals = function(intervals) { count += 1 } } - + return intervals.length - count }; ``` - - 按左边界排序 - ```js -var eraseOverlapIntervals = function (intervals) { - // 按照左边界升序排列 - intervals.sort((a, b) => a[0] - b[0]); - let count = 1; - let end = intervals[intervals.length - 1][0]; - // 倒序遍历,对单个区间来说,左边界越大越好,因为给前面区间的空间越大 - for (let i = intervals.length - 2; i >= 0; i--) { - if (intervals[i][1] <= end) { - count++; - end = intervals[i][0]; +var eraseOverlapIntervals = function(intervals) { + // 按照左边界升序排列 + intervals.sort((a, b) => a[0] - b[0]) + let count = 1 + let end = intervals[intervals.length - 1][0] + // 倒序遍历,对单个区间来说,左边界越大越好,因为给前面区间的空间越大 + for(let i = intervals.length - 2; i >= 0; i--) { + if(intervals[i][1] <= end) { + count++ + end = intervals[i][0] + } } - } - // count 记录的是最大非重复区间的个数 - return intervals.length - count; -}; + // count 记录的是最大非重复区间的个数 + return intervals.length - count +} ``` ### TypeScript @@ -373,43 +358,43 @@ var eraseOverlapIntervals = function (intervals) { ```typescript function eraseOverlapIntervals(intervals: number[][]): number { - const length = intervals.length; - if (length === 0) return 0; - intervals.sort((a, b) => a[1] - b[1]); - let right: number = intervals[0][1]; - let count: number = 1; - for (let i = 1; i < length; i++) { - if (intervals[i][0] >= right) { - count++; - right = intervals[i][1]; + const length = intervals.length; + if (length === 0) return 0; + intervals.sort((a, b) => a[1] - b[1]); + let right: number = intervals[0][1]; + let count: number = 1; + for (let i = 1; i < length; i++) { + if (intervals[i][0] >= right) { + count++; + right = intervals[i][1]; + } } - } - return length - count; -} + return length - count; +}; ``` > 按左边界排序,从左往右遍历 ```typescript function eraseOverlapIntervals(intervals: number[][]): number { - if (intervals.length === 0) return 0; - intervals.sort((a, b) => a[0] - b[0]); - let right: number = intervals[0][1]; - let tempInterval: number[]; - let resCount: number = 0; - for (let i = 1, length = intervals.length; i < length; i++) { - tempInterval = intervals[i]; - if (tempInterval[0] >= right) { - // 未重叠 - right = tempInterval[1]; - } else { - // 有重叠,移除当前interval和前一个interval中右边界更大的那个 - right = Math.min(right, tempInterval[1]); - resCount++; + if (intervals.length === 0) return 0; + intervals.sort((a, b) => a[0] - b[0]); + let right: number = intervals[0][1]; + let tempInterval: number[]; + let resCount: number = 0; + for (let i = 1, length = intervals.length; i < length; i++) { + tempInterval = intervals[i]; + if (tempInterval[0] >= right) { + // 未重叠 + right = tempInterval[1]; + } else { + // 有重叠,移除当前interval和前一个interval中右边界更大的那个 + right = Math.min(right, tempInterval[1]); + resCount++; + } } - } - return resCount; -} + return resCount; +}; ``` ### Scala @@ -456,9 +441,7 @@ impl Solution { } } ``` - ### C# - ```csharp public class Solution { @@ -480,6 +463,7 @@ public class Solution } ``` +

From e0b0078eea56b6af0e18487e428fa78ff93313ec Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 19 Jan 2024 10:02:55 +0800 Subject: [PATCH 0946/1533] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 2d38b4d06a..e7a05d45c3 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -585,6 +585,33 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindTargetSumWays(int[] nums, int target) + { + int sum = 0; + foreach (int num in nums) + { + sum += num; + } + if (Math.Abs(target) > sum) return 0; + if ((sum + target) % 2 == 1) return 0; + int bagSize = (sum + target) / 2; + int[] dp = new int[bagSize + 1]; + dp[0] = 1; + for (int i = 0; i < nums.Length; i++) + { + for (int j = bagSize; j >= nums[i]; j--) + { + dp[j] += dp[j - nums[i]]; + } + } + return dp[bagSize]; + } +} +```

From fddab1a7f4442e7cebf70f118b6157c6dfb3d67e Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 20 Jan 2024 09:51:00 +0800 Subject: [PATCH 0947/1533] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\344\270\200\345\222\214\351\233\266.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 8f6197ac5c..904d941e9d 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -533,6 +533,33 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindMaxForm(string[] strs, int m, int n) + { + int[,] dp = new int[m + 1, n + 1]; + foreach (string str in strs) + { + int zero = 0, one = 0; + foreach (char c in str) + { + if (c == '0') zero++; + else one++; + } + for (int i = m; i >= zero; i--) + { + for (int j = n; j >= one; j--) + { + dp[i, j] = Math.Max(dp[i, j], dp[i - zero, j - one] + 1); + } + } + } + return dp[m, n]; + } +} +```

From af4056827594bdc1db564a91cc86d196e5a48a6d Mon Sep 17 00:00:00 2001 From: EmmIons <48384706+EmmIons@users.noreply.github.com> Date: Sat, 20 Jan 2024 18:02:03 +0800 Subject: [PATCH 0948/1533] =?UTF-8?q?Update=200518.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit java版本二维dp数组代码逻辑优化, 原版本的二维dp数组和一维dp数组的逻辑并不对应. --- ...\351\222\261\345\205\221\346\215\242II.md" | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 5c6efdcb3f..fa10454164 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -224,20 +224,26 @@ class Solution { // 二维dp数组版本,方便理解 class Solution { public int change(int amount, int[] coins) { - int[][] dp = new int[coins.length][amount + 1]; - // 只有一种硬币的情况 - for (int i = 0; i <= amount; i += coins[0]) { - dp[0][i] = 1; + int[][] dp = new int[coins.length][amount+1]; + + // 初始化边界值 + for(int i = 0; i < coins.length; i++){ + // 第一列的初始值为1 + dp[i][0] = 1; + } + for(int j = coins[0]; j <= amount; j++){ + // 初始化第一行 + dp[0][j] += dp[0][j-coins[0]]; } - for (int i = 1; i < coins.length; i++) { - for (int j = 0; j <= amount; j++) { - // 第i种硬币使用0~k次,求和 - for (int k = 0; k * coins[i] <= j; k++) { - dp[i][j] += dp[i - 1][j - k * coins[i]]; - } + + for(int i = 1; i < coins.length; i++){ + for(int j = 1; j <= amount; j++){ + if(j < coins[i]) dp[i][j] = dp[i-1][j]; + else dp[i][j] = dp[i][j-coins[i]] + dp[i-1][j]; } } - return dp[coins.length - 1][amount]; + + return dp[coins.length-1][amount]; } } ``` From bcfa9202eba667e24df0512b842fa13669f36924 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 21 Jan 2024 10:29:39 +0800 Subject: [PATCH 0949/1533] =?UTF-8?q?Update=200518.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A22=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\351\222\261\345\205\221\346\215\242II.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 5c6efdcb3f..9fbe8ba32f 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -347,6 +347,26 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int Change(int amount, int[] coins) + { + int[] dp = new int[amount + 1]; + dp[0] = 1; + for (int i = 0; i < coins.Length; i++) + { + for (int j = coins[i]; j <= amount; j++) + { + if (j >= coins[i]) + dp[j] += dp[j - coins[i]]; + } + } + return dp[amount]; + } +} +```

From 68cdbdbb90f2d5a7a32278804f77663198e35a43 Mon Sep 17 00:00:00 2001 From: 0zz10 <56071597+0zz10@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:40:51 -0800 Subject: [PATCH 0950/1533] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit move second solution to 0496.下一个更大元素I --- ...\345\244\247\345\205\203\347\264\240II.md" | 21 ------------------- 1 file changed, 21 deletions(-) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index b788968cee..6df83fb208 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -170,7 +170,6 @@ class Solution { ### Python: ```python -# 方法 1: class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: dp = [-1] * len(nums) @@ -181,26 +180,6 @@ class Solution: stack.pop() stack.append(i%len(nums)) return dp - -# 方法 2: -class Solution: - def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: - stack = [] - # 创建答案数组 - ans = [-1] * len(nums1) - for i in range(len(nums2)): - while len(stack) > 0 and nums2[i] > nums2[stack[-1]]: - # 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常 - if nums2[stack[-1]] in nums1: - # 锁定 num1 检索的 index - index = nums1.index(nums2[stack[-1]]) - # 更新答案数组 - ans[index] = nums2[i] - # 弹出小元素 - # 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了 - stack.pop() - stack.append(i) - return ans ``` ### Go: From 17a6cb3989befb0d6124f2854c17854dd46d62a1 Mon Sep 17 00:00:00 2001 From: 0zz10 <56071597+0zz10@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:43:00 -0800 Subject: [PATCH 0951/1533] =?UTF-8?q?Update=200496.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move solution from 0503.下一个更大元素II --- ...4\345\244\247\345\205\203\347\264\240I.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 45797fb69a..d97a3e8482 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -256,6 +256,7 @@ class Solution { ### Python3 ```python +# 版本一 class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [-1]*len(nums1) @@ -273,6 +274,26 @@ class Solution: stack.pop() stack.append(i) return result + +# 版本二 +class Solution: + def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: + stack = [] + # 创建答案数组 + ans = [-1] * len(nums1) + for i in range(len(nums2)): + while len(stack) > 0 and nums2[i] > nums2[stack[-1]]: + # 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常 + if nums2[stack[-1]] in nums1: + # 锁定 num1 检索的 index + index = nums1.index(nums2[stack[-1]]) + # 更新答案数组 + ans[index] = nums2[i] + # 弹出小元素 + # 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了 + stack.pop() + stack.append(i) + return ans ``` ### Go From 9eb2bec1b01e0baa9254401c732a6fb86d55df39 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 22 Jan 2024 10:32:00 +0800 Subject: [PATCH 0952/1533] =?UTF-8?q?Update=200377.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E7=BB=BC=E5=90=884=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\346\200\273\345\222\214\342\205\243.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index 05f852b1c6..a840ec9bcc 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -312,6 +312,28 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int CombinationSum4(int[] nums, int target) + { + int[] dp = new int[target + 1]; + dp[0] = 1; + for (int i = 0; i <= target; i++) + { + for (int j = 0; j < nums.Length; j++) + { + if (i >= nums[j] && dp[i] < int.MaxValue - dp[i - nums[j]]) + { + dp[i] += dp[i - nums[j]]; + } + } + } + return dp[target]; + } +} +```

From d8f076ef514737ff4ad9247a7380ba2f7c8c7515 Mon Sep 17 00:00:00 2001 From: tlylt Date: Tue, 23 Jan 2024 07:41:36 +0800 Subject: [PATCH 0953/1533] Add two versions of intersection function in Go --- ...04\347\232\204\344\272\244\351\233\206.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 26a9286dc9..25c4e702a6 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -210,6 +210,8 @@ class Solution: ### Go: +(版本一)使用字典和集合 + ```go func intersection(nums1 []int, nums2 []int) []int { set:=make(map[int]struct{},0) // 用map模拟set @@ -230,6 +232,28 @@ func intersection(nums1 []int, nums2 []int) []int { } ``` +(版本二)使用数组 + +```go +func intersection(nums1 []int, nums2 []int) []int { + count1 := make([]int, 1001, 1001) + count2 := make([]int, 1001, 1001) + res := make([]int, 0) + for _, v := range nums1 { + count1[v] = 1 + } + for _, v := range nums2 { + count2[v] = 1 + } + for i := 0; i <= 1000; i++ { + if count1[i] + count2[i] == 2 { + res = append(res, i) + } + } + return res +} +``` + ### JavaScript: ```js From b0dbf080646b57157fa285559b748a2c8fc8a452 Mon Sep 17 00:00:00 2001 From: 0zz10 <56071597+0zz10@users.noreply.github.com> Date: Mon, 22 Jan 2024 20:45:36 -0800 Subject: [PATCH 0954/1533] =?UTF-8?q?Update=200127.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8E=A5=E9=BE=99.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix syntax highlight --- .../0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 97bc66d096..6f8933101e 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -198,7 +198,7 @@ class Solution { ### Python -``` +```python class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: wordSet = set(wordList) From e7e7c4665fe172bcaebed116bb16c3f39e0bc73c Mon Sep 17 00:00:00 2001 From: SteveL <66228787+stevenleon99@users.noreply.github.com> Date: Tue, 23 Jan 2024 21:51:52 -0500 Subject: [PATCH 0955/1533] =?UTF-8?q?Update=200059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit while (loop --) { i = startx; j = starty; // 下面开始的四个for就是模拟转了一圈 // 模拟填充上行从左到右(左闭右开) for (j; j < n - offset; j++) { res[i][j] = count++; } // 模拟填充右列从上到下(左闭右开) for (i; i < n - offset; i++) { res[i][j] = count++; } --- ...9.\350\236\272\346\227\213\347\237\251\351\230\265II.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 58378ffcf2..60281d48f6 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -87,11 +87,11 @@ public: // 下面开始的四个for就是模拟转了一圈 // 模拟填充上行从左到右(左闭右开) - for (j = starty; j < n - offset; j++) { - res[startx][j] = count++; + for (j; j < n - offset; j++) { + res[i][j] = count++; } // 模拟填充右列从上到下(左闭右开) - for (i = startx; i < n - offset; i++) { + for (i; i < n - offset; i++) { res[i][j] = count++; } // 模拟填充下行从右到左(左闭右开) From 671714fc4736f857977fdb355079bc1d7140ca44 Mon Sep 17 00:00:00 2001 From: Lina Date: Wed, 24 Jan 2024 21:23:21 +0000 Subject: [PATCH 0956/1533] =?UTF-8?q?=E6=8C=89=E7=85=A7=E8=A7=A3=E9=A2=98?= =?UTF-8?q?=E6=80=9D=E8=B7=AF=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E5=8F=8A?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\227\213\347\237\251\351\230\265II.md" | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 58378ffcf2..a34af5e3f5 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -138,42 +138,51 @@ public: ```Java class Solution { public int[][] generateMatrix(int n) { - int loop = 0; // 控制循环次数 - int[][] res = new int[n][n]; - int start = 0; // 每次循环的开始点(start, start) - int count = 1; // 定义填充数字 - int i, j; - - while (loop++ < n / 2) { // 判断边界后,loop从1开始 - // 模拟上侧从左到右 - for (j = start; j < n - loop; j++) { - res[start][j] = count++; + int[][] nums = new int[n][n]; + int startX = 0, startY = 0; // 每一圈的起始点 + int offset = 1; + int count = 1; // 矩阵中需要填写的数字 + int loop = 1; // 记录当前的圈数 + int i, j; // j 代表列, i 代表行; + + while (loop <= n / 2) { + + // 顶部 + // 左闭右开,所以判断循环结束时, j 不能等于 n - offset + for (j = startY; j < n - offset; j++) { + nums[startX][j] = count++; } - // 模拟右侧从上到下 - for (i = start; i < n - loop; i++) { - res[i][j] = count++; + // 右列 + // 左闭右开,所以判断循环结束时, i 不能等于 n - offset + for (i = startX; i < n - offset; i++) { + nums[i][j] = count++; } - // 模拟下侧从右到左 - for (; j >= loop; j--) { - res[i][j] = count++; + // 底部 + // 左闭右开,所以判断循环结束时, j != startY + for (; j > startY; j--) { + nums[i][j] = count++; } - // 模拟左侧从下到上 - for (; i >= loop; i--) { - res[i][j] = count++; + // 左列 + // 左闭右开,所以判断循环结束时, i != startX + for (; i > startX; i--) { + nums[i][j] = count++; } - start++; + startX++; + startY++; + offset++; + loop++; } - - if (n % 2 == 1) { - res[start][start] = count; + if (n % 2 == 1) { // n 为奇数时,单独处理矩阵中心的值 + nums[startX][startY] = count; } - - return res; + return nums; } } + + ``` ### python3: From e51ce46b75cae98e8b69bac491151f0a347d10e8 Mon Sep 17 00:00:00 2001 From: zhouzheng Date: Fri, 26 Jan 2024 11:18:54 +0800 Subject: [PATCH 0957/1533] =?UTF-8?q?update:=2070.=20=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=E8=BF=9B=E9=98=B6=E7=89=88=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=20go=20=E8=AF=AD=E8=A8=80=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\214\205\347\211\210\346\234\254.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index f8337cc048..4fa294cfe2 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -169,7 +169,32 @@ class climbStairs{ ### Go: +```go +func climbStairs(n int, m int) int { + dp := make([]int, n+1) + dp[0] = 1 + for i := 1; i <= n; i++ { + for j := 1; j <= m; j++ { + if i-j >= 0 { + dp[i] += dp[i-j] + } + } + } + return dp[n] +} +func main() { + // 读取输入n,m + reader := bufio.NewReader(os.Stdin) + input, _ := reader.ReadString('\n') + input = strings.TrimSpace(input) + nv := strings.Split(input, " ") + n, _ := strconv.Atoi(nv[0]) + m, _ := strconv.Atoi(nv[1]) + result := climbStairs(n, m) + fmt.Println(result) +} +``` ### JavaScript: From abc86e9a7d66851ada0e54f74efd50b9ce5da8f2 Mon Sep 17 00:00:00 2001 From: Heeqw <124508798+Heeqw@users.noreply.github.com> Date: Sat, 27 Jan 2024 17:05:04 +0800 Subject: [PATCH 0958/1533] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=980?= =?UTF-8?q?2.07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了一种java语言的同步移动方法 --- ...276\350\241\250\347\233\270\344\272\244.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index d0967b8b86..e2905d49be 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -105,6 +105,7 @@ public: ### Java: ```Java +(版本一)先行移动长链表实现同步移动 public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode curA = headA; @@ -149,6 +150,23 @@ public class Solution { } } + +(版本二) 合并链表实现同步移动 +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + // p1 指向 A 链表头结点,p2 指向 B 链表头结点 + ListNode p1 = headA, p2 = headB; + while (p1 != p2) { + // p1 走一步,如果走到 A 链表末尾,转到 B 链表 + if (p1 == null) p1 = headB; + else p1 = p1.next; + // p2 走一步,如果走到 B 链表末尾,转到 A 链表 + if (p2 == null) p2 = headA; + else p2 = p2.next; + } + return p1; + } +} ``` ### Python: From 8b35191534307420814a486d232b11027cc85504 Mon Sep 17 00:00:00 2001 From: qiuzidian <115708838+qiuzidian@users.noreply.github.com> Date: Mon, 29 Jan 2024 21:37:01 +0800 Subject: [PATCH 0959/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加c语言版本解法 --- ...\346\225\260\347\233\270\345\212\240II.md" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 1c262c873d..3baeade533 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -412,6 +412,76 @@ public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { return res; } ``` +### C: + +```c +// 哈希表大小 +const int HASH_SIZE = 101; + +typedef struct node +{ + int val; + int count; + struct node *next; +}node, *HashMap; + +// 哈希表插入 +void hash_insert(HashMap hashmap[], int val){ + int idx = val < 0 ? (-val) % HASH_SIZE : val % HASH_SIZE, count = 0; + node *p = hashmap[idx]; + while (p->next != NULL) { + p = p->next; + if (p->val == val){ + (p->count)++; + return; + } + } + node *new = malloc(sizeof(node)); + new->val = val; + new->count = 1; + new->next = NULL; + p->next = new; + return; +} + +// 哈希表查找 +int hash_search(HashMap hashmap[], int val){ + int idx = val < 0 ? (-val) % HASH_SIZE : val % HASH_SIZE; + node *p = hashmap[idx]; + while (p->next != NULL){ + p = p->next; + if (p->val == val) return p->count; + } + return 0; +} + +int fourSumCount(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums3, int nums3Size, int* nums4, int nums4Size){ + // 初始化哈希表 + HashMap hashmap[HASH_SIZE]; + for (int i = 0; i < HASH_SIZE; i++){ + hashmap[i] = malloc(sizeof(node)); + hashmap[i]->next = NULL; + } + + // 统计两个数组元素之和的负值和出现的次数,放到哈希表中 + int count = 0, num; + for (int i=0; i From 2f2e6856dbda03d1580b4301bf62dee04b0f641b Mon Sep 17 00:00:00 2001 From: qiuzidian <115708838+qiuzidian@users.noreply.github.com> Date: Mon, 29 Jan 2024 22:08:52 +0800 Subject: [PATCH 0960/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md=EF=BC=8C=E8=A7=84=E8=8C=83=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\225\260\347\233\270\345\212\240II.md" | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 3baeade533..a0bf84da83 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -418,20 +418,19 @@ public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { // 哈希表大小 const int HASH_SIZE = 101; -typedef struct node -{ +typedef struct node { int val; int count; struct node *next; -}node, *HashMap; +} node, *HashMap; // 哈希表插入 -void hash_insert(HashMap hashmap[], int val){ +void hash_insert(HashMap hashmap[], int val) { int idx = val < 0 ? (-val) % HASH_SIZE : val % HASH_SIZE, count = 0; node *p = hashmap[idx]; while (p->next != NULL) { p = p->next; - if (p->val == val){ + if (p->val == val) { (p->count)++; return; } @@ -445,10 +444,10 @@ void hash_insert(HashMap hashmap[], int val){ } // 哈希表查找 -int hash_search(HashMap hashmap[], int val){ +int hash_search(HashMap hashmap[], int val) { int idx = val < 0 ? (-val) % HASH_SIZE : val % HASH_SIZE; node *p = hashmap[idx]; - while (p->next != NULL){ + while (p->next != NULL) { p = p->next; if (p->val == val) return p->count; } @@ -458,23 +457,23 @@ int hash_search(HashMap hashmap[], int val){ int fourSumCount(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums3, int nums3Size, int* nums4, int nums4Size){ // 初始化哈希表 HashMap hashmap[HASH_SIZE]; - for (int i = 0; i < HASH_SIZE; i++){ + for (int i = 0; i < HASH_SIZE; i++) { hashmap[i] = malloc(sizeof(node)); hashmap[i]->next = NULL; } // 统计两个数组元素之和的负值和出现的次数,放到哈希表中 int count = 0, num; - for (int i=0; i Date: Mon, 29 Jan 2024 22:18:50 +0800 Subject: [PATCH 0961/1533] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1.md=EF=BC=8C=E6=B7=BB=E5=8A=A0c=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3.\350\265\216\351\207\221\344\277\241.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index b800c2320d..1dbf5de009 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -445,6 +445,25 @@ public bool CanConstruct(string ransomNote, string magazine) { return true; } +``` + +### C: + +```c +bool canConstruct(char* ransomNote, char* magazine) { + // 定义哈希映射数组 + int hashmap[26] = {0}; + // 对magazine中字符计数 + while (*magazine != '\0') hashmap[*magazine++ % 26]++; + // 遍历ransomNote,对应的字符自减,小于0说明该字符magazine没有或不足够表示 + while (*ransomNote != '\0') hashmap[*ransomNote++ % 26]--; + // 如果数组中存在负数,说明ransomNote不能由magazine里面的字符构成 + for (int i = 0; i < 26; i++) { + if (hashmap[i] < 0) return false; + } + return true; +} + ```

From 586b8efd38f2ee919c38b207d7fcb0a442def32c Mon Sep 17 00:00:00 2001 From: Heeqw <124508798+Heeqw@users.noreply.github.com> Date: Mon, 29 Jan 2024 23:39:32 +0800 Subject: [PATCH 0962/1533] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加java语言版本的双指针法 --- ...44\346\225\260\344\271\213\345\222\214.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 1785fe5ff1..80218cb535 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -133,6 +133,7 @@ public: ### Java: ```java +//使用哈希表 public int[] twoSum(int[] nums, int target) { int[] res = new int[2]; if(nums == null || nums.length == 0){ @@ -151,6 +152,43 @@ public int[] twoSum(int[] nums, int target) { return res; } ``` +```java +//使用双指针 +public int[] twoSum(int[] nums, int target) { + int m=0,n=0,k,board=0; + int[] res=new int[2]; + int[] tmp1=new int[nums.length]; + //备份原本下标的nums数组 + System.arraycopy(nums,0,tmp1,0,nums.length); + //将nums排序 + Arrays.sort(nums); + //双指针 + for(int i=0,j=nums.length-1;itarget) + j--; + else if(nums[i]+nums[j]==target){ + m=i; + n=j; + break; + } + } + //找到nums[m]在tmp1数组中的下标 + for(k=0;k Date: Mon, 29 Jan 2024 13:42:49 -0800 Subject: [PATCH 0963/1533] fix: Correct function type --- ...\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index b691a45a0b..2a11f9f4b7 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -298,7 +298,7 @@ class Solution: return self.traversal(root, p, q) ``` -迭代法(版本二)精简 +递归法(版本二)精简 ```python class Solution: def lowestCommonAncestor(self, root, p, q): From 1bd8143bb241fbe0b1ce998e740902da5bfe839e Mon Sep 17 00:00:00 2001 From: Heeqw <124508798+Heeqw@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:27:08 +0800 Subject: [PATCH 0964/1533] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了java语言版本的哈希解法 --- ...11\346\225\260\344\271\213\345\222\214.md" | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 91fc9d685a..bf165788f8 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -256,7 +256,7 @@ while (right > left) { ## 其他语言版本 ### Java: - +(版本一) 双指针 ```Java class Solution { public List> threeSum(int[] nums) { @@ -297,7 +297,43 @@ class Solution { } } ``` +(版本二) 使用哈希集合 +```Java +class Solution { + public List> threeSum(int[] nums) { + List> result = new ArrayList<>(); + Arrays.sort(nums); + for (int i = 0; i < nums.length; i++) { + // 如果第一个元素大于零,不可能凑成三元组 + if (nums[i] > 0) { + return result; + } + // 三元组元素a去重 + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + HashSet set = new HashSet<>(); + for (int j = i + 1; j < nums.length; j++) { + // 三元组元素b去重 + if (j > i + 2 && nums[j] == nums[j - 1] && nums[j - 1] == nums[j - 2]) { + continue; + } + + int c = -nums[i] - nums[j]; + if (set.contains(c)) { + result.add(Arrays.asList(nums[i], nums[j], c)); + set.remove(c); // 三元组元素c去重 + } else { + set.add(nums[j]); + } + } + } + return result; + } +} +``` ### Python: (版本一) 双指针 From 51481e11f50bb4596c15cc719313d43686a0987d Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:06:31 +0800 Subject: [PATCH 0965/1533] =?UTF-8?q?Update=200090.=E5=AD=90=E9=9B=86II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这里与“补充”部分重复了,建议删掉 --- "problems/0090.\345\255\220\351\233\206II.md" | 7 ------- 1 file changed, 7 deletions(-) diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 1bb63a34d6..6d618978a8 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -158,13 +158,6 @@ public: 其实这道题目的知识点,我们之前都讲过了,如果之前讲过的子集问题和去重问题都掌握的好,这道题目应该分分钟AC。 -当然本题去重的逻辑,也可以这么写 - -```cpp -if (i > startIndex && nums[i] == nums[i - 1] ) { - continue; -} -``` ## 其他语言版本 From e9a0259345d77c9b9d9e27132040da858d238f44 Mon Sep 17 00:00:00 2001 From: BanTanger <1290288968@qq.com> Date: Mon, 5 Feb 2024 12:28:07 +0800 Subject: [PATCH 0966/1533] =?UTF-8?q?=E7=AE=80=E5=8C=96=E5=88=86=E6=94=AF?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A6=82=E6=9E=9C=E7=94=A8=E4=BA=86?= =?UTF-8?q?=20return=20=E5=B0=B1=E4=B8=8D=E7=94=A8=20if=20else=20=E6=8E=A7?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\214\345\217\211\346\240\221.md" | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index 305f3ae555..9743ca2b80 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -369,8 +369,44 @@ class Solution { } ``` +简化分支版本: + +```java +class Solution { + static int ans; + public int minCameraCover(TreeNode root) { + ans = 0; // 初始化 + if(f(root) == 0) ans ++; + return ans; + } + // 定义 f 函数有三种返回值情况 + // 0:表示 x 节点没有被相机监控,只能依靠父节点放相机 + // 1:表示 x 节点被相机监控,但相机不是放在自身节点上 + // 2:表示 x 节点被相机监控,但相机放在自身节点上 + public static int f(TreeNode x) { + if(x == null) return 1; // 空树认为被监控,但没有相机 + // 左右递归到最深处 + int l = f(x.left); + int r = f(x.right); + // 有任意一个子节点为空,就需要当前节点放相机,不然以后没机会 + if(l == 0 || r == 0) { + ans ++; // 放相机 + return 2; + } + // 贪心策略,左右子树都被监控,且没有监控到当前节点, + // 那么最有利的情况就是将相机放置在当前节点父节点上, + // 因为这样能多监控可能的子树节点和父父节点 + if(l == 1 && r == 1) return 0; + // 剩下情况就是左右子树有可能为 2,即当前节点被监控 + return 1; + } +} +``` + + ### Python + 贪心(版本一) ```python class Solution: @@ -757,4 +793,3 @@ public class Solution - From f618d35d8e39830d643177eda182944741427a6f Mon Sep 17 00:00:00 2001 From: Daixing Zhou <43519171+zhoudaixing@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:59:20 +0800 Subject: [PATCH 0967/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A00347=E5=89=8DK?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0=E7=9A=84JavaScrip?= =?UTF-8?q?t=E4=BD=BF=E7=94=A8=E4=BC=98=E5=85=88=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\351\242\221\345\205\203\347\264\240.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 94b29eba3e..93d605f5fe 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -324,6 +324,34 @@ func topKFrequent(nums []int, k int) []int { ### JavaScript: +解法一: +Leetcode 提供了优先队列的库,具体文档可以参见 [@datastructures-js/priority-queue](https://github.com/datastructures-js/priority-queue/blob/v5/README.md)。 + +```js +var topKFrequent = function (nums, k) { + const map = new Map(); + const res = []; + //使用 map 统计元素出现频率 + for (const num of nums) { + map.set(num, (map.get(num) || 0) + 1); + } + //创建小顶堆 + const heap = new PriorityQueue({ + compare: (a, b) => a.value - b.value + }) + for (const [key, value] of map) { + heap.enqueue({ key, value }); + if (heap.size() > k) heap.dequeue(); + } + //处理输出 + while (heap.size()) res.push(heap.dequeue().key); + return res; +}; +``` + +解法二: +手写实现优先队列 + ```js // js 没有堆 需要自己构造 class Heap { From 3d90f1ed267ebc68f523c8221677115f74aa8912 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Mon, 5 Feb 2024 16:31:15 +0800 Subject: [PATCH 0968/1533] =?UTF-8?q?Update=2020201126=E8=B4=AA=E5=BF=83?= =?UTF-8?q?=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代码没有高亮显示 --- ...\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index cdfe4a96d2..3494a32074 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -69,7 +69,7 @@ 代码很简单,但是思路却比较难。还需要反复琢磨。 针对[贪心算法:最大子序和](https://programmercarl.com/0053.最大子序和.html)文章中给出的贪心代码如下; -``` +```cpp class Solution { public: int maxSubArray(vector& nums) { From c2cf9f854182541e915a94619fc8cba0b48445d1 Mon Sep 17 00:00:00 2001 From: Ryanhuang88 <145511591+Ryanhuang88@users.noreply.github.com> Date: Tue, 13 Feb 2024 22:02:49 -0500 Subject: [PATCH 0969/1533] Update join.md --- problems/qita/join.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/qita/join.md b/problems/qita/join.md index a2698e66cf..62dec674d5 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -111,7 +111,7 @@ python代码 有的录友是一个pull request 里有很多commit (一个commit是一道题目的代码)。 -有的录友是一个pull request 里有有一个commit。 +有的录友是一个pull request 里只有一个commit。

From 64edc63251e8862fc98cfe9635f360cccada4ceb Mon Sep 17 00:00:00 2001 From: Ryanhuang88 <145511591+Ryanhuang88@users.noreply.github.com> Date: Tue, 13 Feb 2024 22:07:05 -0500 Subject: [PATCH 0970/1533] =?UTF-8?q?Update=200685.=E5=86=97=E4=BD=99?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 31b2ad247e..0cad5ad581 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -81,7 +81,7 @@ for (int i = 0; i < n; i++) { } ``` -前两种入度为2的情况,一定是删除指向入度为2的节点的两条边其中的一条,如果删了一条,判断这个图是一个树,那么这条边就是答案,同时注意要从后向前遍历,因为如果两天边删哪一条都可以成为树,就删最后那一条。 +前两种入度为2的情况,一定是删除指向入度为2的节点的两条边其中的一条,如果删了一条,判断这个图是一个树,那么这条边就是答案,同时注意要从后向前遍历,因为如果两条边删哪一条都可以成为树,就删最后那一条。 代码如下: From e8e2f249632170cbd25ef874e9875c84784d1440 Mon Sep 17 00:00:00 2001 From: anini <1660470561@qq.com> Date: Sat, 17 Feb 2024 12:42:10 +0800 Subject: [PATCH 0971/1533] =?UTF-8?q?Update=201002.=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6.md=20=E6=9B=B4=E6=96=B0Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC-=E6=9B=B4=E6=96=B0=E4=B8=BA=E4=B8=8EC++?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 +++ ...70\347\224\250\345\255\227\347\254\246.md" | 68 +++++++++++-------- 2 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..13566b81b0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index 57fa7dae1f..8d81e3f8a0 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -327,37 +327,45 @@ var commonChars = function(words) { ### GO ```golang -func commonChars(words []string) []string { - length:=len(words) - fre:=make([][]int,0)//统计每个字符串的词频 - res:=make([]string,0) - //统计词频 - for i:=0;i 0 { + s := string('a' + i) // rune -> string + result = append(result, s) + hash[i]-- + } + } + + return result } -func min(a,b int)int{ - if a>b{ - return b - } - return a + +func min(a, b int) int { + if a < b { + return a + } + return b } ``` From df8878b79192527f3d8617e09c93227340d03c10 Mon Sep 17 00:00:00 2001 From: gdstzmy <79707886+gdstzmy@users.noreply.github.com> Date: Tue, 20 Feb 2024 18:09:07 +0800 Subject: [PATCH 0972/1533] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加go语言贪心法 --- ...247\345\255\220\345\272\217\345\222\214.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 78c8b38222..74ff2ca40d 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -230,7 +230,25 @@ class Solution: ``` ### Go +贪心法 +```go +func maxSubArray(nums []int) int { + max := nums[0] + count := 0 + for i := 0; i < len(nums); i++{ + count += nums[i] + if count > max{ + max = count + } + if count < 0 { + count = 0 + } + } + return max +} +``` +动态规划 ```go func maxSubArray(nums []int) int { maxSum := nums[0] From 13edf8d8aa64be5d4e34d64186098c734ba71035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Thu, 22 Feb 2024 14:40:15 +0800 Subject: [PATCH 0973/1533] Delete .idea/.gitignore --- .idea/.gitignore | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81b0..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml From 2c160f31c511429053e33f9c8216e49d90b98360 Mon Sep 17 00:00:00 2001 From: zhaohanyan Date: Thu, 22 Feb 2024 12:05:47 -0500 Subject: [PATCH 0974/1533] =?UTF-8?q?Update=20kama55.=E5=8F=B3=E6=97=8B?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加python解法,已在kama上测试通过 --- ...346\227\213\345\255\227\347\254\246\344\270\262.md" | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 17c97eaa3c..38e37322ee 100644 --- "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -211,7 +211,17 @@ public class Main { ``` ### Python: +```Python +#注意:python中字符串是不可变的,所以使用python一定要创建新的字符串 +#获取输入的数字k和字符串 +k = int(input()) +s = input() + +#通过切片反转第一段和第二段字符串 +s = s[len(s)-k:] + s[:len(s)-k] +print(s) +``` ### Go: ```go From c4ad2ec22e01bc0cdfde6dbbea91748e20bbdf16 Mon Sep 17 00:00:00 2001 From: zhaohanyan Date: Thu, 22 Feb 2024 12:10:18 -0500 Subject: [PATCH 0975/1533] =?UTF-8?q?Update=20kama55.=E5=8F=B3=E6=97=8B?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 注解更清晰 --- ...217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 38e37322ee..7137186042 100644 --- "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -212,13 +212,12 @@ public class Main { ### Python: ```Python -#注意:python中字符串是不可变的,所以使用python一定要创建新的字符串 - #获取输入的数字k和字符串 k = int(input()) s = input() #通过切片反转第一段和第二段字符串 +#注意:python中字符串是不可变的,所以也需要额外空间 s = s[len(s)-k:] + s[:len(s)-k] print(s) ``` From a45fd00a27d03000eb3b327d94abd007ee0ae305 Mon Sep 17 00:00:00 2001 From: lhp <932606153@qq.com> Date: Fri, 23 Feb 2024 10:54:04 +0800 Subject: [PATCH 0976/1533] =?UTF-8?q?0827:=E6=9C=80=E5=A4=A7=E4=BA=BA?= =?UTF-8?q?=E5=B7=A5=E5=B2=9B----=E6=B7=BB=E5=8A=A0js=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\272\345\267\245\345\262\233.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 4feb78ded7..3f32dd7aa1 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -348,6 +348,71 @@ class Solution: ``` +### JavaScript + +```JavaScript + +var largestIsland = function(grid) { +let res = 0; +const m = grid.length; +const n = grid[0].length; +const tag = new Array(n).fill().map(_ => new Array(m).fill(0)); +const area = new Map(); +const dir = [[0,1],[0,-1],[1,0],[-1,0]]; +const dfs = (grid,tag,x,y,mark) => { + let res = 1; + tag[x][y] = mark; + for(let i = 0; i < dir.length; i++) { + let nextX = x + dir[i][0]; + let nextY = y + dir[i][1]; + if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) { + continue; + } + if(grid[nextX][nextY] === 1 && tag[nextX][nextY] === 0) { + res += dfs(grid,tag,nextX,nextY,mark); + } + } + return res; +} +let mark = 2; +//将岛屿用mark标记 +for(let i = 0; i < m; i++) { + for(let j = 0; j < n; j++) { + if(grid[i][j] === 1 && tag[i][j] === 0) { + area.set(mark,dfs(grid,tag,i,j,mark)); + res = Math.max(res,area.get(mark)); + mark++; + } + } +} +//将一个非岛屿格子变为岛屿 +for(let i = 0; i < m; i++) { + for(let j = 0; j < n; j++) { + if(grid[i][j] === 0) { + let z = 1; + const connected = new Set(); + for(let k = 0; k < dir.length; k++) { + let nextX = i + dir[k][0]; + let nextY = j + dir[k][1]; + if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || tag[nextX][nextY] === 0 || connected.has(tag[nextX][nextY])) { + continue; + } + z += area.get(tag[nextX][nextY]); + connected.add(tag[nextX][nextY]); + } + res = Math.max(res,z); + } + } +} +return res; +}; + + +``` + + + +

From ea13bb2d2ef23790fa775386537692650b3cbc6c Mon Sep 17 00:00:00 2001 From: "435962415@qq.com" Date: Fri, 23 Feb 2024 12:00:50 +0800 Subject: [PATCH 0977/1533] =?UTF-8?q?=C2=96827.=E6=9C=80=E5=A4=A7=E4=BA=BA?= =?UTF-8?q?=E5=B7=A5=E5=B2=9B=20=E6=B7=BB=E5=8A=A0go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\272\345\267\245\345\262\233.md" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 4feb78ded7..cac67676b9 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -346,6 +346,97 @@ class Solution: return res +``` + +### Go + +```go +func largestIsland(grid [][]int) int { + dir := [][]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} + n := len(grid) + m := len(grid[0]) + area := 0 + visited := make([][]bool, n) + for i := 0; i < n; i++ { + visited[i] = make([]bool, m) + } + gridNum := make(map[int]int, 0) // 记录每一个岛屿的面积 + mark := 2 // 记录每个岛屿的编号 + isAllGrid := true + res := 0 // 标记是否整个地图都是陆地 + + var dfs func(grid [][]int, visited [][]bool, x, y, mark int) + dfs = func(grid [][]int, visited [][]bool, x, y, mark int) { + // 终止条件:访问过的节点 或者 遇到海水 + if visited[x][y] || grid[x][y] == 0 { + return + } + visited[x][y] = true // 标记访问过 + grid[x][y] = mark // 给陆地标记新标签 + area++ + for i := 0; i < 4; i++ { + nextX := x + dir[i][0] + nextY := y + dir[i][1] + if nextX < 0 || nextX >= len(grid) || nextY < 0 || nextY >= len(grid[0]) { + continue + } + dfs(grid, visited, nextX, nextY, mark) + } + } + + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + if grid[i][j] == 0 { + isAllGrid = false + } + if !visited[i][j] && grid[i][j] == 1 { + area = 0 + dfs(grid, visited, i, j, mark) // 将与其链接的陆地都标记上 true + gridNum[mark] = area // 记录每一个岛屿的面积 + mark++ // 更新下一个岛屿编号 + } + } + } + if isAllGrid { + return n * m + } + // 根据添加陆地的位置,计算周边岛屿面积之和 + visitedGrid := make(map[int]struct{}) // 标记访问过的岛屿 + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + count := 1 // 记录连接之后的岛屿数量 + visitedGrid = make(map[int]struct{}) // 每次使用时,清空 + if grid[i][j] == 0 { + for k := 0; k < 4; k++ { + // 计算相邻坐标 + nearI := i + dir[k][0] + nearJ := j + dir[k][1] + if nearI < 0 || nearI >= len(grid) || nearJ < 0 || nearJ >= len(grid[0]) { + continue + } + // 添加过的岛屿不要重复添加 + if _, ok := visitedGrid[grid[nearI][nearJ]]; ok { + continue + } + // 把相邻四面的岛屿数量加起来 + count += gridNum[grid[nearI][nearJ]] + // 标记该岛屿已经添加过 + visitedGrid[grid[nearI][nearJ]] = struct{}{} + } + } + res = max827(res, count) + } + } + return res +} + +func max827(x, y int) int { + if x > y { + return x + } + return y +} + ```

From b4f55e72745f4f6fbf27dd4bdb2201f1b430b9f2 Mon Sep 17 00:00:00 2001 From: zhangchang Date: Sat, 24 Feb 2024 22:46:14 +0800 Subject: [PATCH 0978/1533] =?UTF-8?q?=E4=BC=98=E5=8C=96Python=E9=80=92?= =?UTF-8?q?=E5=BD=92=E6=97=B6=E6=89=80=E9=9C=80=E8=A6=81=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=BC=80=E9=94=80=E5=92=8C=E6=97=B6=E9=97=B4=E6=B6=88?= =?UTF-8?q?=E8=80=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\275\222\351\201\215\345\216\206.md" | 54 +++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index edd55aad3c..4621d4a739 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -180,26 +180,34 @@ class Solution { class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: - if not root: - return [] - - left = self.preorderTraversal(root.left) - right = self.preorderTraversal(root.right) - - return [root.val] + left + right + res = [] + + def dfs(node): + if node is None: + return + + res.append(node.val) + dfs(node.left) + dfs(node.right) + + return res ``` ```python # 中序遍历-递归-LC94_二叉树的中序遍历 class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: - if root is None: - return [] - - left = self.inorderTraversal(root.left) - right = self.inorderTraversal(root.right) - - return left + [root.val] + right + res = [] + + def dfs(node): + if node is None: + return + + dfs(node.left) + res.append(node.val) + dfs(node.right) + + return res ``` ```python @@ -207,13 +215,17 @@ class Solution: # 后序遍历-递归-LC145_二叉树的后序遍历 class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: - if not root: - return [] - - left = self.postorderTraversal(root.left) - right = self.postorderTraversal(root.right) - - return left + right + [root.val] + res = [] + + def dfs(node): + if node is None: + return + + dfs(node.left) + dfs(node.right) + res.append(node.val) + + return res ``` ### Go: From f5fc6baddca9af176b77648229ae26fd4bae137e Mon Sep 17 00:00:00 2001 From: meifannao Date: Sun, 25 Feb 2024 22:12:35 +0800 Subject: [PATCH 0979/1533] =?UTF-8?q?0647.=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2=E6=B7=BB=E5=8A=A0Golang=E7=9A=84=E5=8F=8C=E6=8C=87?= =?UTF-8?q?=E9=92=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\226\207\345\255\220\344\270\262.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index d94d18c5c0..4887ff8311 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -397,6 +397,7 @@ class Solution: ``` ### Go: +> 动态规划: ```Go func countSubstrings(s string) int { @@ -422,6 +423,47 @@ func countSubstrings(s string) int { return res } ``` +> 动态规划:简洁版 +```Go +func countSubstrings(s string) int { + res := 0 + dp := make([][]bool, len(s)) + for i := 0; i < len(s); i++ { + dp[i] = make([]bool, len(s)) + } + + for i := len(s) - 1; i >= 0; i-- { + for j := i; j < len(s); j++ { + if s[i] == s[j] && (j-i <= 1 || dp[i+1][j-1]) { + res++ + dp[i][j] = true + } + } + } + return res +} +``` + +> 双指针法: +```Go +func countSubstrings(s string) int { + extend := func(i, j int) int { + res := 0 + for i >= 0 && j < len(s) && s[i] == s[j] { + i -- + j ++ + res ++ + } + return res + } + res := 0 + for i := 0; i < len(s); i++ { + res += extend(i, i) // 以i为中心 + res += extend(i, i+1) // 以i和i+1为中心 + } + return res +} +``` ### Javascript: From 9c5b5676ee221d11280f66b1456ae82822154871 Mon Sep 17 00:00:00 2001 From: Han <90488412+HanCai98@users.noreply.github.com> Date: Sun, 25 Feb 2024 22:48:39 -0500 Subject: [PATCH 0980/1533] =?UTF-8?q?correct=20typo=20in=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 18442943ad..83d295bd5b 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -37,7 +37,7 @@ 在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。 -那么如果把节点陆地所能遍历到的陆地都标记上呢,就可以使用 DFS,BFS或者并查集。 +那么如何把节点陆地所能遍历到的陆地都标记上呢,就可以使用 DFS,BFS或者并查集。 ### 深度优先搜索 From bf863d2d8c0e5316ba1d41e1a22f76f883ff8bc1 Mon Sep 17 00:00:00 2001 From: heroding77 <2441145504@qq.com> Date: Mon, 26 Feb 2024 16:04:47 +0800 Subject: [PATCH 0981/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00027=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0=20python3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index dbde3d198c..2a2005d736 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -253,6 +253,27 @@ class Solution: ``` +``` python 3 +# 相向双指针法 +# 时间复杂度 O(n) +# 空间复杂度 O(1) +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + n = len(nums) + left, right = 0, n - 1 + while left <= right: + while left <= right and nums[left] != val: + left += 1 + while left <= right and nums[right] == val: + right -= 1 + if left < right: + nums[left] = nums[right] + left += 1 + right -= 1 + return left + +``` + ### Go: ```go From 415b031025b725cf7ccd109b16716920afb7ccc6 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Mon, 26 Feb 2024 20:47:23 +0800 Subject: [PATCH 0982/1533] =?UTF-8?q?Update=200045.=E8=B7=B3=E8=B7=83?= =?UTF-8?q?=E6=B8=B8=E6=88=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0045.跳跃游戏||新增C语言实现 --- ...\350\267\203\346\270\270\346\210\217II.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index d290f55e80..c6433ea86c 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -492,7 +492,34 @@ impl Solution { } } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int jump(int* nums, int numsSize) { + if(numsSize == 1){ + return 0; + } + int count = 0; + // 记录当前能走的最远距离 + int curDistance = 0; + // 记录下一步能走的最远距离 + int nextDistance = 0; + for(int i = 0; i < numsSize; i++){ + nextDistance = max(i + nums[i], nextDistance); + // 下标到了当前的最大距离 + if(i == nextDistance){ + count++; + curDistance = nextDistance; + } + } + return count; +} +``` + ### C# + ```csharp // 版本二 public class Solution @@ -518,3 +545,4 @@ public class Solution + From 3fc2fb99545d84f7965bec15e14ac7ae98754c94 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:03:50 +0800 Subject: [PATCH 0983/1533] =?UTF-8?q?Update=20=E5=9B=BE=E8=AE=BA=E5=B9=B6?= =?UTF-8?q?=E6=9F=A5=E9=9B=86=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正错别字和语病 --- ...06\347\220\206\350\256\272\345\237\272\347\241\200.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 347bf58f13..a65d480760 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -108,7 +108,7 @@ bool isSame(int u, int v) { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602102619.png) -如果这棵多叉树高度很深的话,每次find函数 去寻找跟的过程就要递归很多次。 +如果这棵多叉树高度很深的话,每次find函数 去寻找根的过程就要递归很多次。 我们的目的只需要知道这些节点在同一个根下就可以,所以对这棵多叉树的构造只需要这样就可以了,如图: @@ -300,7 +300,7 @@ join(3, 2); **因为路经压缩了** -即如下代码在寻找跟的过程中,会有路径压缩,减少 下次查询的路径长度。 +即如下代码在寻找根的过程中,会有路径压缩,减少 下次查询的路径长度。 ``` // 并查集里寻根的过程 @@ -396,7 +396,7 @@ void join(int u, int v) { if (rank[u] <= rank[v]) father[u] = v; // rank小的树合入到rank大的树 else father[v] = u; - if (rank[u] == rank[v] && u != v) rank[v]++; // 如果两棵树高度相同,则v的高度+1因为,方面 if (rank[u] <= rank[v]) father[u] = v; 注意是 <= + if (rank[u] == rank[v] && u != v) rank[v]++; // 如果两棵树高度相同,则v的高度+1因为,上面 if (rank[u] <= rank[v]) father[u] = v; 注意是 <= } ``` @@ -423,7 +423,7 @@ void join(int u, int v) { 空间复杂度: O(n) ,申请一个father数组。 -关于时间复杂度,如果想精确表达出来需要繁琐的数学证明,就不在本篇讲解范围内了,大家感兴趣可以自己去深入去研究。 +关于时间复杂度,如果想精确表达出来需要繁琐的数学证明,就不在本篇讲解范围内了,大家感兴趣可以自己去深入研究。 这里做一个简单的分析思路。 From 94e50d08b9be536ade4694b78c2310bfbd43f128 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:36:59 +0800 Subject: [PATCH 0984/1533] =?UTF-8?q?Update=200685.=E5=86=97=E4=BD=99?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加cpp代码高亮 --- ...685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 0cad5ad581..c07dda3a78 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -69,7 +69,7 @@ edges[2][0] = 2,edges[2][1] = 3, 搞清楚之后,我们如何统计入度呢? -即 edges[i][1] 表示的节点都是 箭头指向的节点,即这个几点有一个入度! (如果想统计出度,那么就是 edges[i][0])。 +即 edges[i][1] 表示的节点都是 箭头指向的节点,即这个节点有一个入度! (如果想统计出度,那么就是 edges[i][0])。 所以,统计入度的代码如下: @@ -108,7 +108,7 @@ if (vec.size() > 0) { 可以定义一个函数,代码如下: -``` +```cpp // 在有向图里找到删除的那条边,使其变成树,返回值就是要删除的边 vector getRemoveEdge(const vector>& edges) ``` From c88104b1af80e11ce78ff84147016421b905910c Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 20:25:03 +0800 Subject: [PATCH 0985/1533] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加代码高亮 --- ...217\211\346\220\234\347\264\242\346\240\221.md" | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 916013c52c..6824c7e29a 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -79,7 +79,7 @@ public: 代码如下: -``` +```cpp TreeNode* trimBST(TreeNode* root, int low, int high) ``` @@ -87,7 +87,7 @@ TreeNode* trimBST(TreeNode* root, int low, int high) 修剪的操作并不是在终止条件上进行的,所以就是遇到空节点返回就可以了。 -``` +```cpp if (root == nullptr ) return nullptr; ``` @@ -97,7 +97,7 @@ if (root == nullptr ) return nullptr; 代码如下: -``` +```cpp if (root->val < low) { TreeNode* right = trimBST(root->right, low, high); // 寻找符合区间[low, high]的节点 return right; @@ -108,7 +108,7 @@ if (root->val < low) { 代码如下: -``` +```cpp if (root->val > high) { TreeNode* left = trimBST(root->left, low, high); // 寻找符合区间[low, high]的节点 return left; @@ -119,7 +119,7 @@ if (root->val > high) { 最后返回root节点,代码如下: -``` +```cpp root->left = trimBST(root->left, low, high); // root->left接入符合条件的左孩子 root->right = trimBST(root->right, low, high); // root->right接入符合条件的右孩子 return root; @@ -133,7 +133,7 @@ return root; 如下代码相当于把节点0的右孩子(节点2)返回给上一层, -``` +```cpp if (root->val < low) { TreeNode* right = trimBST(root->right, low, high); // 寻找符合区间[low, high]的节点 return right; @@ -142,7 +142,7 @@ if (root->val < low) { 然后如下代码相当于用节点3的左孩子 把下一层返回的 节点0的右孩子(节点2) 接住。 -``` +``` cpp root->left = trimBST(root->left, low, high); ``` From a518db03dd3265f53d98d2277f5da12323363958 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 20:26:06 +0800 Subject: [PATCH 0986/1533] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加代码高亮 --- ...\221\344\270\255\347\232\204\350\212\202\347\202\271.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 8922a14e14..60dae7b977 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -42,7 +42,7 @@ 代码如下: -``` +```cpp TreeNode* deleteNode(TreeNode* root, int key) ``` @@ -50,7 +50,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 -``` +```cpp if (root == nullptr) return root; ``` @@ -106,7 +106,7 @@ if (root->val == key) { 这里相当于把新的节点返回给上一层,上一层就要用 root->left 或者 root->right接住,代码如下: -``` +```cpp if (root->val > key) root->left = deleteNode(root->left, key); if (root->val < key) root->right = deleteNode(root->right, key); return root; From 6fdcc59aec48b8c3bb02dcc90b6709fd2ae566d2 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 20:26:57 +0800 Subject: [PATCH 0987/1533] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E6=93=8D=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加代码高亮 --- ...04\346\217\222\345\205\245\346\223\215\344\275\234.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 98e60d5f5f..5cb0de9911 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -59,7 +59,7 @@ 代码如下: -``` +```cpp TreeNode* insertIntoBST(TreeNode* root, int val) ``` @@ -69,7 +69,7 @@ TreeNode* insertIntoBST(TreeNode* root, int val) 代码如下: -``` +```cpp if (root == NULL) { TreeNode* node = new TreeNode(val); return node; @@ -88,7 +88,7 @@ if (root == NULL) { 代码如下: -``` +```cpp if (root->val > val) root->left = insertIntoBST(root->left, val); if (root->val < val) root->right = insertIntoBST(root->right, val); return root; @@ -120,7 +120,7 @@ public: 那么递归函数定义如下: -``` +```cpp TreeNode* parent; // 记录遍历节点的父节点 void traversal(TreeNode* cur, int val) ``` From 2da3afe8a9c576fdb7f4d784b92e0fd7ce9b3074 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 21:28:45 +0800 Subject: [PATCH 0988/1533] Update gitserver.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正错别字和语病 --- problems/qita/gitserver.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/qita/gitserver.md b/problems/qita/gitserver.md index 9ee06ae4d9..f410898860 100644 --- a/problems/qita/gitserver.md +++ b/problems/qita/gitserver.md @@ -138,7 +138,7 @@ passwd gitpassword 创建`.ssh` 目录,如果`.ssh` 已经存在了,可以忽略这一项 -为啥用配置ssh公钥呢,同学们记不记得我急使用github上传上传代码的时候也要把自己的公钥配置上github上 +为啥用配置ssh公钥呢,同学们记不记得我使用github上传代码的时候也要把自己的公钥配置上github上 这也是方面每次操作git仓库的时候不用再去输入密码 @@ -186,7 +186,7 @@ cd /home/git/.ssh/ cat id_rsa.pub >> authorized_keys ``` -如何看我们配置的密钥是否成功呢, 在客户点直接登录git服务器,看看是否是免密登陆 +如何看我们配置的密钥是否成功呢, 在客户端直接登录git服务器,看看是否是免密登陆 ``` ssh git@git服务器ip ``` From e361eddeddf3c86ed53b360861fb90703d7e95ce Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 21:58:01 +0800 Subject: [PATCH 0989/1533] =?UTF-8?q?Update=20=E5=9B=BE=E8=AE=BA=E5=B9=B6?= =?UTF-8?q?=E6=9F=A5=E9=9B=86=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正语病 --- ...\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index a65d480760..6a1456e950 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -396,7 +396,7 @@ void join(int u, int v) { if (rank[u] <= rank[v]) father[u] = v; // rank小的树合入到rank大的树 else father[v] = u; - if (rank[u] == rank[v] && u != v) rank[v]++; // 如果两棵树高度相同,则v的高度+1因为,上面 if (rank[u] <= rank[v]) father[u] = v; 注意是 <= + if (rank[u] == rank[v] && u != v) rank[v]++; // 如果两棵树高度相同,则v的高度+1,因为上面 if (rank[u] <= rank[v]) father[u] = v; 注意是 <= } ``` From bf256aad79dcb8efcab4adfa60cc05d3a9c03f1f Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 21:59:12 +0800 Subject: [PATCH 0990/1533] Update gitserver.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正语病 --- problems/qita/gitserver.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/qita/gitserver.md b/problems/qita/gitserver.md index f410898860..caf93ec6ec 100644 --- a/problems/qita/gitserver.md +++ b/problems/qita/gitserver.md @@ -138,7 +138,7 @@ passwd gitpassword 创建`.ssh` 目录,如果`.ssh` 已经存在了,可以忽略这一项 -为啥用配置ssh公钥呢,同学们记不记得我使用github上传代码的时候也要把自己的公钥配置上github上 +为啥用配置ssh公钥呢,同学们记不记得我使用github上传代码的时候也要把自己的公钥配置上传到github上 这也是方面每次操作git仓库的时候不用再去输入密码 From af5741fbaad53da909a759f3f16765aa0049278c Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 22:43:48 +0800 Subject: [PATCH 0991/1533] =?UTF-8?q?Update=20On=E7=9A=84=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=B1=85=E7=84=B6=E8=B6=85=E6=97=B6=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E6=AD=A4=E6=97=B6=E7=9A=84n=E7=A9=B6=E7=AB=9F=E6=98=AF?= =?UTF-8?q?=E5=A4=9A=E5=A4=A7=EF=BC=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正错别字 --- ...\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 8b0934c582..df48832358 100644 --- "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -29,7 +29,7 @@ 所以 1GHz = 10亿Hz,表示CPU可以一秒脉冲10亿次(有10亿个时钟周期),这里不要简单理解一个时钟周期就是一次CPU运算。 -例如1 + 2 = 3,cpu要执行四次才能完整这个操作,步骤一:把1放入寄存机,步骤二:把2放入寄存器,步骤三:做加法,步骤四:保存3。 +例如1 + 2 = 3,cpu要执行四次才能完整这个操作,步骤一:把1放入寄存器,步骤二:把2放入寄存器,步骤三:做加法,步骤四:保存3。 而且计算机的cpu也不会只运行我们自己写的程序上,同时cpu也要执行计算机的各种进程任务等等,我们的程序仅仅是其中的一个进程而已。 From 7ac6785eb3f7450ffda868ea3149bb7bdfa6b965 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 27 Feb 2024 22:49:34 +0800 Subject: [PATCH 0992/1533] =?UTF-8?q?Update=20On=E7=9A=84=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=B1=85=E7=84=B6=E8=B6=85=E6=97=B6=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E6=AD=A4=E6=97=B6=E7=9A=84n=E7=A9=B6=E7=AB=9F=E6=98=AF?= =?UTF-8?q?=E5=A4=9A=E5=A4=A7=EF=BC=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正语病 --- ...\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index df48832358..d5710d5ae2 100644 --- "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -52,7 +52,7 @@ * 火箭科学家需要大致知道一枚试射火箭的着陆点是在大海里还是在城市中; * 医学研究者需要知道一次药物测试是会杀死还是会治愈实验对象; -所以**任何开发计算机程序员的软件工程师都应该能够估计这个程序的运行时间是一秒钟还是一年**。 +所以**任何开发计算机程序的软件工程师都应该能够估计这个程序的运行时间是一秒钟还是一年**。 这个是最基本的,所以以上误差就不算事了。 From 190dd0e54a45208983d3e7c8a3437221b01f0270 Mon Sep 17 00:00:00 2001 From: Jamie He Date: Wed, 28 Feb 2024 06:06:07 +0000 Subject: [PATCH 0993/1533] =?UTF-8?q?used=20=E6=95=B0=E7=BB=84=E8=B6=85?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=20=E7=A7=BB=E9=99=A4=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=AD=94=E6=A1=88=E3=80=820332.=E9=87=8D=E6=96=B0=E5=AE=89?= =?UTF-8?q?=E6=8E=92=E8=A1=8C=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 26 ------------------- 1 file changed, 26 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 036cfc51b5..61fa82f552 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -419,32 +419,6 @@ class Solution { ``` ### Python -回溯 使用used数组 - -```python -class Solution: - def findItinerary(self, tickets: List[List[str]]) -> List[str]: - tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的 - used = [0] * len(tickets) - path = ['JFK'] - results = [] - self.backtracking(tickets, used, path, 'JFK', results) - return results[0] - - def backtracking(self, tickets, used, path, cur, results): - if len(path) == len(tickets) + 1: # 终止条件:路径长度等于机票数量+1 - results.append(path[:]) # 将当前路径添加到结果列表 - return True - - for i, ticket in enumerate(tickets): # 遍历机票列表 - if ticket[0] == cur and used[i] == 0: # 找到起始机场为cur且未使用过的机票 - used[i] = 1 # 标记该机票为已使用 - path.append(ticket[1]) # 将到达机场添加到路径中 - state = self.backtracking(tickets, used, path, ticket[1], results) # 递归搜索 - path.pop() # 回溯,移除最后添加的到达机场 - used[i] = 0 # 标记该机票为未使用 - if state: - return True # 只要找到一个可行路径就返回,不继续搜索 ``` 回溯 使用字典 From da5be794f9f7923197465570c56870a4dda735b7 Mon Sep 17 00:00:00 2001 From: Jamie He Date: Wed, 28 Feb 2024 06:10:39 +0000 Subject: [PATCH 0994/1533] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 61fa82f552..6c8a481435 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -418,8 +418,7 @@ class Solution { } ``` -### Python - +### Python ``` 回溯 使用字典 ```python From f926ac8f390f63236426ca29f2bfd63ad98685c9 Mon Sep 17 00:00:00 2001 From: Tiana <51739436+tianci-zhang@users.noreply.github.com> Date: Thu, 29 Feb 2024 18:45:42 +0800 Subject: [PATCH 0995/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88?= =?UTF-8?q?=E6=9C=AC.md=20Python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0070.爬楼梯完全背包版本.md Python3代码 --- ...203\214\345\214\205\347\211\210\346\234\254.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 4fa294cfe2..622b1117e7 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -165,7 +165,21 @@ class climbStairs{ ``` ### Python3: +```python3 +def climbing_stairs(n,m): + dp = [0]*(n+1) # 背包总容量 + dp[0] = 1 + # 排列题,注意循环顺序,背包在外物品在内 + for j in range(1,n+1): + for i in range(1,m+1): + if j>=i: + dp[j] += dp[j-i] # 这里i就是重量而非index + return dp[n] +if __name__ == '__main__': + n,m = list(map(int,input().split(' '))) + print(climbing_stairs(n,m)) +``` ### Go: From dd242cfbb5339ef8d03b0911da3b57d9b2da9bed Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 29 Feb 2024 19:52:04 +0800 Subject: [PATCH 0996/1533] =?UTF-8?q?Update=200435.=E6=97=A0=E9=87=8D?= =?UTF-8?q?=E5=8F=A0=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0435.无重叠区间C语言实现 --- ...15\345\217\240\345\214\272\351\227\264.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index cf9996ddef..b668e86046 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -441,7 +441,37 @@ impl Solution { } } ``` +### C + +```c +// 按照区间右边界排序 +int cmp(const void * var1, const void * var2){ + return (*(int **) var1)[1] - (*(int **) var2)[1]; +} + +int eraseOverlapIntervals(int** intervals, int intervalsSize, int* intervalsColSize) { + if(intervalsSize == 0){ + return 0; + } + qsort(intervals, intervalsSize, sizeof (int *), cmp); + // 记录非重叠的区间数量 + int count = 1; + // 记录区间分割点 + int end = intervals[0][1]; + for(int i = 1; i < intervalsSize; i++){ + if(end <= intervals[i][0]){ + end = intervals[i][1]; + count++; + } + } + return intervalsSize - count; +} +``` + + + ### C# + ```csharp public class Solution { @@ -468,3 +498,4 @@ public class Solution + From 3c4632315c64a4ed47857dd66a8e86bba77e19cd Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 29 Feb 2024 20:14:04 +0800 Subject: [PATCH 0997/1533] =?UTF-8?q?Update=200763.=E5=88=92=E5=88=86?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0763.划分字母区间增加C语言实现 --- ...27\346\257\215\345\214\272\351\227\264.md" | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 4e9ec578bc..8b0ca7b802 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -404,7 +404,38 @@ impl Solution { } } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int* partitionLabels(char* s, int* returnSize) { + // 记录每个字符最远出现的位置 + int last[26] = {0}; + int len = strlen(s); + for (int i = 0; i < len; ++i) { + last[s[i] - 'a'] = i; + } + int left = 0, right = 0; + int * partition = malloc(sizeof (int ) * len); + // 初始化值 + *returnSize = 0; + for(int i = 0; i < len; i++){ + right = max(right, last[s[i] - 'a']); + // 到达最远位置,加入答案,并且更新左边下标 + if(i == right){ + partition[(*returnSize)++] = right - left + 1; + left = i + 1; + } + } + return partition; +} +``` + + + ### C# + ```csharp public class Solution { @@ -435,4 +466,3 @@ public class Solution - From 48e65c1de98e473f9a3e93161f0f484c5b13ed1e Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 29 Feb 2024 20:44:07 +0800 Subject: [PATCH 0998/1533] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0056.合并区间新增C语言实现 --- ...10\345\271\266\345\214\272\351\227\264.md" | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 122e783a27..f9d6f65468 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -336,7 +336,49 @@ impl Solution { } } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +// 根据左边界进行排序 +int cmp(const void * var1, const void * var2){ + int *v1 = *(int **) var1; + int *v2 = *(int **) var2; + return v1[0] - v2[0]; +} + +int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) { + int ** result = malloc(sizeof (int *) * intervalsSize); + * returnColumnSizes = malloc(sizeof (int ) * intervalsSize); + for(int i = 0; i < intervalsSize; i++){ + result[i] = malloc(sizeof (int ) * 2); + } + qsort(intervals, intervalsSize, sizeof (int *), cmp); + int count = 0; + for(int i = 0; i < intervalsSize; i++){ + // 记录区间的左右边界 + int L = intervals[i][0], R = intervals[i][1]; + // 如果count为0或者前一区间的右区间小于此时的左边,加入结果中 + if (count == 0 || result[count - 1][1] < L) { + returnColumnSizes[0][count] = 2; + result[count][0] = L; + result[count][1] = R; + count++; + } + else{ // 更新右边界的值 + result[count - 1][1] = max(R, result[count - 1][1]); + } + } + *returnSize = count; + return result; +} +``` + + + ### C# + ```csharp public class Solution { @@ -367,4 +409,3 @@ public class Solution - From 64399ddf6f997b627615a82e791ff50bcf3c2811 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 29 Feb 2024 20:55:06 +0800 Subject: [PATCH 0999/1533] =?UTF-8?q?Update=200738.=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0738.单调递增的数字增加C语言实现 --- ...36\347\232\204\346\225\260\345\255\227.md" | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index 400dc90daa..4a8a6e087d 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -392,7 +392,33 @@ impl Solution { } } ``` +### C + +```c +int monotoneIncreasingDigits(int n) { + char str[11]; + // 将数字转换为字符串 + sprintf(str, "%d", n); + int len = strlen(str); + int flag = strlen(str); + for(int i = len - 1; i > 0; i--){ + if(str[i] < str[i - 1]){ + str[i - 1]--; + flag = i; + } + } + for(int i = flag; i < len; i++){ + str[i] = '9'; + } + // 字符串转数字 + return atoi(str); +} +``` + + + ### C# + ```csharp public class Solution { @@ -421,4 +447,3 @@ public class Solution - From a1e8af39a8ab834c08ba163be920c29add7d513f Mon Sep 17 00:00:00 2001 From: lavaicer <52038323+lavaicer@users.noreply.github.com> Date: Sat, 2 Mar 2024 01:31:30 +0000 Subject: [PATCH 1000/1533] =?UTF-8?q?=E4=BC=98=E5=8C=96=2019.=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?=20N=20=E4=B8=AA=E7=BB=93=E7=82=B9=20go=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index f508b523e3..d04df9f39a 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -165,20 +165,17 @@ class Solution: * } */ func removeNthFromEnd(head *ListNode, n int) *ListNode { - dummyHead := &ListNode{} - dummyHead.Next = head - cur := head - prev := dummyHead - i := 1 - for cur != nil { - cur = cur.Next - if i > n { - prev = prev.Next - } - i++ - } - prev.Next = prev.Next.Next - return dummyHead.Next + dummyNode := &ListNode{0, head} + fast, slow := dummyNode, dummyNode + for i := 0; i <= n; i++ { // 注意<=,否则快指针为空时,慢指针正好在倒数第n个上面 + fast = fast.Next + } + for fast != nil { + fast = fast.Next + slow = slow.Next + } + slow.Next = slow.Next.Next + return dummyNode.Next } ``` From 832e425ce6942a82115badb6d2286ec09690a5c5 Mon Sep 17 00:00:00 2001 From: "435962415@qq.com" Date: Sat, 2 Mar 2024 17:50:19 +0800 Subject: [PATCH 1001/1533] =?UTF-8?q?=C2=961971.=20=E5=AF=BB=E6=89=BE?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=20=E6=B7=BB=E5=8A=A0go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\345\234\250\350\267\257\345\276\204.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 132b0181c0..89a2a1fe87 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -284,6 +284,48 @@ var validPath = function(n, edges, source, destination) { }; ``` +### Go + +```go +func validPath(n int, edges [][]int, source int, destination int) bool { + n = 200005 + father := make([]int, n) + // 并查集初始化 + for i := 0; i < n; i++ { + father[i] = i + } + + var find func(u int) int // 并查集里寻根的过程 + find = func(u int) int { + // 如果根就是自己,直接返回 + // 如果根不是自己,就根据数组下标一层一层向下找 + if u == father[u] { + return u + } + return find(father[u]) + } + + var join func(u, v int) // 将 v->u 这条边加入并查集 + join = func(u, v int) { + u = find(u) + v = find(v) + if u == v { + return + } + father[v] = u + } + + for i := 0; i < len(edges); i++ { + join(edges[i][0], edges[i][1]) + } + + source = find(source) + destination = find(destination) + return source == destination +} + +``` +

From c1ecf1b8ada80975ea91cb8a2d70f740bad0106c Mon Sep 17 00:00:00 2001 From: yangzhaoMP Date: Wed, 6 Mar 2024 11:06:37 +0800 Subject: [PATCH 1002/1533] =?UTF-8?q?=E6=8E=A5=E9=9B=A8=E6=B0=B4=20Java=20?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 73d787b13c..6d92d2b380 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -440,6 +440,33 @@ class Solution { } ``` +双指针优化 +```java +class Solution { + public int trap(int[] height) { + if (height.length <= 2) { + return 0; + } + // 从两边向中间寻找最值 + int maxLeft = height[0], maxRight = height[height.length - 1]; + int l = 1, r = height.length - 2; + int res = 0; + while (l <= r) { + // 不确定上一轮是左边移动还是右边移动,所以两边都需更新最值 + maxLeft = Math.max(maxLeft, height[l]); + maxRight = Math.max(maxRight, height[r]); + // 最值较小的一边所能装的水量已定,所以移动较小的一边。 + if (maxLeft < maxRight) { + res += maxLeft - height[l ++]; + } else { + res += maxRight - height[r --]; + } + } + return res; + } +} +``` + 单调栈法 ```java From 571ddaa69d9ea4bd80dcad2b70ffc25ce84f78ae Mon Sep 17 00:00:00 2001 From: yangzhaoMP Date: Wed, 6 Mar 2024 11:41:56 +0800 Subject: [PATCH 1003/1533] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=20=E7=88=AC=E6=A5=BC=E6=A2=AF=E6=9C=80=E5=B0=8F=E8=B4=B9?= =?UTF-8?q?=E7=8E=87=20java=20=E7=8A=B6=E6=80=81=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...271\347\210\254\346\245\274\346\242\257.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index d13ff19f5a..6320ed89a7 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -244,6 +244,24 @@ class Solution { } ``` +```Java +// 状态压缩,使用三个变量来代替数组 +class Solution { + public int minCostClimbingStairs(int[] cost) { + // 以下三个变量分别表示前两个台阶的最少费用、前一个的、当前的。 + int beforeTwoCost = 0, beforeOneCost = 0, currentCost = 0; + // 前两个台阶不需要费用就能上到,因此从下标2开始;因为最后一个台阶需要跨越,所以需要遍历到cost.length + for (int i = 2; i <= cost.length; i ++) { + // 此处遍历的是cost[i - 1],不会越界 + currentCost = Math.min(beforeOneCost + cost[i - 1], beforeTwoCost + cost[i - 2]); + beforeTwoCost = beforeOneCost; + beforeOneCost = currentCost; + } + return currentCost; + } +} +``` + ### Python 动态规划(版本一) From 86ff0f2a35549d75d45ae873394514e812ab8436 Mon Sep 17 00:00:00 2001 From: yangzhaoMP Date: Wed, 6 Mar 2024 11:54:19 +0800 Subject: [PATCH 1004/1533] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=20=E4=B8=8D=E5=90=8C=E8=B7=AF=E5=BE=84=20java=20=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E5=8E=8B=E7=BC=A9=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...215\345\220\214\350\267\257\345\276\204.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 207a66ee80..32c64a12c9 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -285,6 +285,24 @@ public: } ``` +状态压缩 +```java +class Solution { + public int uniquePaths(int m, int n) { + // 在二维dp数组中,当前值的计算只依赖正上方和正左方,因此可以压缩成一维数组。 + int[] dp = new int[n]; + // 初始化,第一行只能从正左方跳过来,所以只有一条路径。 + Arrays.fill(dp, 1); + for (int i = 1; i < m; i ++) { + // 第一列也只有一条路,不用迭代,所以从第二列开始 + for (int j = 1; j < n; j ++) { + dp[j] += dp[j - 1]; // dp[j] = dp[j] (正上方)+ dp[j - 1] (正左方) + } + } + return dp[n - 1]; + } +} +``` ### Python 递归 From 9932bebde8bb219d3a63b1c3e5881e4c72efa9ad Mon Sep 17 00:00:00 2001 From: yangzhaoMP Date: Wed, 6 Mar 2024 12:56:20 +0800 Subject: [PATCH 1005/1533] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=20=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=20java=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" | 3 --- 1 file changed, 3 deletions(-) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index cbdf40e85c..dafdbbeda2 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -287,9 +287,6 @@ class Solution { return dp[1]; } } -``` -```Java - ``` ### Python: From 1474a28a53ffe259d61aefcc822fa7e3e4959d44 Mon Sep 17 00:00:00 2001 From: yangzhaoMP Date: Wed, 6 Mar 2024 14:59:28 +0800 Subject: [PATCH 1006/1533] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=20=E6=9C=80=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E5=BA=8F=E5=88=97=20java=20=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=E7=8A=B6=E6=80=81=E5=8E=8B=E7=BC=A9=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...222\345\242\236\345\272\217\345\210\227.md" | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 485e321c99..14a5e89557 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -186,7 +186,23 @@ public: return res; } ``` - +> 动态规划状态压缩 +```java +class Solution { + public int findLengthOfLCIS(int[] nums) { + // 记录以 前一个元素结尾的最长连续递增序列的长度 和 以当前 结尾的...... + int beforeOneMaxLen = 1, currentMaxLen = 0; + // res 赋最小值返回的最小值1 + int res = 1; + for (int i = 1; i < nums.length; i ++) { + currentMaxLen = nums[i] > nums[i - 1] ? beforeOneMaxLen + 1 : 1; + beforeOneMaxLen = currentMaxLen; + res = Math.max(res, currentMaxLen); + } + return res; + } +} +``` > 贪心法: ```Java From 58595806a0d8fec2c810231bfa96951784add6f0 Mon Sep 17 00:00:00 2001 From: yangzhaoMP Date: Wed, 6 Mar 2024 16:03:39 +0800 Subject: [PATCH 1007/1533] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=20=E5=88=A4=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97=20java=20?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E7=8A=B6=E6=80=81=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index ebd567cbb7..caca8cb86c 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -173,6 +173,63 @@ class Solution { } } ``` +> 修改遍历顺序后,可以利用滚动数组,对dp数组进行压缩 +```java +class Solution { + public boolean isSubsequence(String s, String t) { + // 修改遍历顺序,外圈遍历t,内圈遍历s。使得dp的推算只依赖正上方和左上方,方便压缩。 + int[][] dp = new int[t.length() + 1][s.length() + 1]; + for (int i = 1; i < dp.length; i++) { // 遍历t字符串 + for (int j = 1; j < dp[i].length; j++) { // 遍历s字符串 + if (t.charAt(i - 1) == s.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = dp[i - 1][j]; + } + } + System.out.println(Arrays.toString(dp[i])); + } + return dp[t.length()][s.length()] == s.length(); + } +} +``` +> 状态压缩 +```java +class Solution { + public boolean isSubsequence(String s, String t) { + int[] dp = new int[s.length() + 1]; + for (int i = 0; i < t.length(); i ++) { + // 需要使用上一轮的dp[j - 1],所以使用倒序遍历 + for (int j = dp.length - 1; j > 0; j --) { + // i遍历的是t字符串,j遍历的是dp数组,dp数组的长度比s的大1,因此需要减1。 + if (t.charAt(i) == s.charAt(j - 1)) { + dp[j] = dp[j - 1] + 1; + } + } + } + return dp[s.length()] == s.length(); + } +} +``` +> 将dp定义为boolean类型,dp[i]直接表示s.substring(0, i)是否为t的子序列 + +```java +class Solution { + public boolean isSubsequence(String s, String t) { + boolean[] dp = new boolean[s.length() + 1]; + // 表示 “” 是t的子序列 + dp[0] = true; + for (int i = 0; i < t.length(); i ++) { + for (int j = dp.length - 1; j > 0; j --) { + if (t.charAt(i) == s.charAt(j - 1)) { + dp[j] = dp[j - 1]; + } + } + } + return dp[dp.length - 1]; + } +} +``` ### Python: From 40b392588699b33a750101ab63adbb3b6f4f6860 Mon Sep 17 00:00:00 2001 From: OrangeLiuSSE <143183385+OrangeLiuSSE@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:45:09 +0800 Subject: [PATCH 1008/1533] =?UTF-8?q?Update=200347.=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\351\242\221\345\205\203\347\264\240.md" | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 93d605f5fe..b340e1855b 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -218,7 +218,7 @@ class Solution { ``` ### Python: - +解法一: ```python #时间复杂度:O(nlogk) #空间复杂度:O(n) @@ -246,6 +246,31 @@ class Solution: result[i] = heapq.heappop(pri_que)[1] return result ``` +解法二: +```python +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + # 使用字典统计数字出现次数 + time_dict = defaultdict(int) + for num in nums: + time_dict[num] += 1 + # 更改字典,key为出现次数,value为相应的数字的集合 + index_dict = defaultdict(list) + for key in time_dict: + index_dict[time_dict[key]].append(key) + # 排序 + key = list(index_dict.keys()) + key.sort() + result = [] + cnt = 0 + # 获取前k项 + while key and cnt != k: + result += index_dict[key[-1]] + cnt += len(index_dict[key[-1]]) + key.pop() + + return result[0: k] +``` ### Go: From d5ba33471ca102dd009aeae95365ea64bab5cbb4 Mon Sep 17 00:00:00 2001 From: code_more <59354339+worshipone@users.noreply.github.com> Date: Thu, 7 Mar 2024 15:40:05 +0800 Subject: [PATCH 1009/1533] =?UTF-8?q?Update=20kama54.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改kama54.替换数字中的java实例代码思路流程,与为C++代码保持一致 --- ...77\346\215\242\346\225\260\345\255\227.md" | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index f03e575d1d..092b1df052 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -146,17 +146,42 @@ for (int i = 0; i < a.size(); i++) { ```java import java.util.Scanner; -class Main { - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - String s = in.nextLine(); - StringBuilder sb = new StringBuilder(); +public class Main { + + public static String replaceNumber(String s) { + int count = 0; // 统计数字的个数 + int sOldSize = s.length(); for (int i = 0; i < s.length(); i++) { - if (Character.isDigit(s.charAt(i))) { - sb.append("number"); - }else sb.append(s.charAt(i)); + if(Character.isDigit(s.charAt(i))){ + count++; + } } - System.out.println(sb); + // 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小 + char[] newS = new char[s.length() + count * 5]; + int sNewSize = newS.length; + // 将旧字符串的内容填入新数组 + System.arraycopy(s.toCharArray(), 0, newS, 0, sOldSize); + // 从后先前将空格替换为"number" + for (int i = sNewSize - 1, j = sOldSize - 1; j < i; j--, i--) { + if (!Character.isDigit(newS[j])) { + newS[i] = newS[j]; + } else { + newS[i] = 'r'; + newS[i - 1] = 'e'; + newS[i - 2] = 'b'; + newS[i - 3] = 'm'; + newS[i - 4] = 'u'; + newS[i - 5] = 'n'; + i -= 5; + } + } + return new String(newS); + }; + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String s = scanner.next(); + System.out.println(replaceNumber(s)); + scanner.close(); } } ``` From d6a37be6f21083a1e4bcae7fe167c1bca583fee4 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 7 Mar 2024 20:03:10 +0800 Subject: [PATCH 1010/1533] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0494.目标和新增C语言实现 --- ...4.\347\233\256\346\240\207\345\222\214.md" | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index e7a05d45c3..8d1a34e5f5 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -585,7 +585,42 @@ impl Solution { } } ``` +```c +int getSum(int * nums, int numsSize){ + int sum = 0; + for(int i = 0; i < numsSize; i++){ + sum += nums[i]; + } + return sum; +} + +int findTargetSumWays(int* nums, int numsSize, int target) { + int sum = getSum(nums, numsSize); + int diff = sum - target; + // 两种情况不满足 + if(diff < 0 || diff % 2 != 0){ + return 0; + } + int bagSize = diff / 2; + int dp[numsSize + 1][bagSize + 1]; + dp[0][0] = 1; + for(int i = 1; i <= numsSize; i++){ + int num = nums[i - 1]; + for(int j = 0; j <= bagSize; j++){ + dp[i][j] = dp[i - 1][j]; + if(j >= num){ + dp[i][j] += dp[i - 1][j - num]; + } + } + } + return dp[numsSize][bagSize]; +} +``` + + + ### C# + ```csharp public class Solution { @@ -617,4 +652,3 @@ public class Solution - From 0a30cd7a2664cc8198635bf0c69447abe3c57fa4 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 7 Mar 2024 20:04:45 +0800 Subject: [PATCH 1011/1533] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0474.一和零新增C语言实现 --- ...4.\344\270\200\345\222\214\351\233\266.md" | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 904d941e9d..7e04ae1eb0 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -533,7 +533,39 @@ impl Solution { } } ``` +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int findMaxForm(char** strs, int strsSize, int m, int n) { + int dp[m + 1][n + 1]; + memset(dp, 0, sizeof (int ) * (m + 1) * (n + 1)); + for(int i = 0; i < strsSize; i++){ + // 统计0和1的数量 + int count0 = 0; + int count1 = 0; + char *str = strs[i]; + while (*str != '\0'){ + if(*str == '0'){ + count0++; + } else{ + count1++; + } + str++; + } + for(int j = m; j >= count0; j--){ + for(int k = n; k >= count1; k--){ + dp[j][k] = max(dp[j][k], dp[j - count0][k - count1] + 1); + } + } + } + return dp[m][n]; +} +``` + + + ### C# + ```csharp public class Solution { @@ -565,4 +597,3 @@ public class Solution - From 4e7637556b14e6600a73928269d324bc227e08ab Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 7 Mar 2024 20:13:39 +0800 Subject: [PATCH 1012/1533] =?UTF-8?q?Update=200518.=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0518.零钱兑换II新增C语言实现 --- ...\351\222\261\345\205\221\346\215\242II.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index da1c475565..59fdf6cd25 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -353,7 +353,28 @@ object Solution { } } ``` +## C + +```c +int change(int amount, int* coins, int coinsSize) { + int dp[amount + 1]; + memset(dp, 0, sizeof (dp)); + dp[0] = 1; + // 遍历物品 + for(int i = 0; i < coinsSize; i++){ + // 遍历背包 + for(int j = coins[i]; j <= amount; j++){ + dp[j] += dp[j - coins[i]]; + } + } + return dp[amount]; +} +``` + + + ### C# + ```csharp public class Solution { @@ -378,3 +399,4 @@ public class Solution + From 12365a3a4fcd4a38633921df6c942c8a7c7536f9 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 7 Mar 2024 20:15:51 +0800 Subject: [PATCH 1013/1533] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0474.\344\270\200\345\222\214\351\233\266.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 7e04ae1eb0..af50fa5cbc 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -533,6 +533,8 @@ impl Solution { } } ``` +## C + ```c #define max(a, b) ((a) > (b) ? (a) : (b)) @@ -597,3 +599,4 @@ public class Solution + From 702916f35626f98841b0700b8bce8688e8fc7c7a Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Thu, 7 Mar 2024 20:16:56 +0800 Subject: [PATCH 1014/1533] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 8d1a34e5f5..02edad4d8b 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -585,6 +585,8 @@ impl Solution { } } ``` +## C + ```c int getSum(int * nums, int numsSize){ int sum = 0; @@ -652,3 +654,4 @@ public class Solution + From a55dca64eda06deac1fac7e291df3a2694006cce Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Fri, 8 Mar 2024 22:40:51 +0800 Subject: [PATCH 1015/1533] =?UTF-8?q?=E6=96=B0=E5=A2=9EC=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\271\263\346\226\271\346\225\260.md" | 25 +++++++++++++++- ...66\351\222\261\345\205\221\346\215\242.md" | 30 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index a0e62d4800..f7c06dbd87 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -389,6 +389,30 @@ function numSquares(n: number): number { }; ``` +## C + +```c +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int numSquares(int n) { + int* dp = (int*)malloc(sizeof(int) * (n + 1)); + for (int j = 0; j < n + 1; j++) { + dp[j] = INT_MAX; + } + dp[0] = 0; + // 遍历背包 + for (int i = 0; i <= n; ++i) { + // 遍历物品 + for (int j = 1; j * j <= i; ++j) { + dp[i] = min(dp[i - j * j] + 1, dp[i]); + } + } + return dp[n]; +} +``` + + + ### Rust: ```rust @@ -439,4 +463,3 @@ impl Solution { - 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 eae4ab3ac0..156b5ff36d 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" @@ -352,6 +352,35 @@ func min(a, b int) int { ``` +## C + +```c +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int coinChange(int* coins, int coinsSize, int amount) { + int* dp = (int*)malloc(sizeof(int) * (amount + 1)); + for (int j = 0; j < amount + 1; j++) { + dp[j] = INT_MAX; + } + dp[0] = 0; + // 遍历背包 + for(int i = 0; i <= amount; i++){ + // 遍历物品 + for(int j = 0; j < coinsSize; j++){ + if(i - coins[j] >= 0 && dp[i - coins[j]] != INT_MAX){ + dp[i] = min(dp[i], dp[i - coins[j]] + 1); + } + } + } + if(dp[amount] == INT_MAX){ + return -1; + } + return dp[amount]; +} +``` + + + ### Rust: ```rust @@ -474,4 +503,3 @@ function coinChange(coins: number[], amount: number): number { - From 6bae30472cf1e0380c3a6652b877ff7777d3f993 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Fri, 8 Mar 2024 22:42:26 +0800 Subject: [PATCH 1016/1533] =?UTF-8?q?Update=200377.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C=E2=85=A3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增C语言实现 --- ...10\346\200\273\345\222\214\342\205\243.md" | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index a840ec9bcc..6f81bffef9 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -312,7 +312,28 @@ impl Solution { } } ``` +### C + +```c +int combinationSum4(int* nums, int numsSize, int target) { + int dp[target + 1]; + memset(dp, 0, sizeof (dp )); + dp[0] = 1; + for(int i = 0; i <= target; i++){ + for(int j = 0; j < numsSize; j++){ + if(i - nums[j] >= 0 && dp[i] < INT_MAX - dp[i - nums[j]]){ + dp[i] += dp[i - nums[j]]; + } + } + } + return dp[target]; +} +``` + + + ### C# + ```csharp public class Solution { @@ -340,4 +361,3 @@ public class Solution - From 40eda312e63be74f9f37c2697548b9c802969f9c Mon Sep 17 00:00:00 2001 From: paigeman <53284808+paigeman@users.noreply.github.com> Date: Sat, 9 Mar 2024 10:28:31 +0800 Subject: [PATCH 1017/1533] fix typos show some LaTeX formula correctly --- ...0\257\345\244\232\345\244\247\357\274\237.md" | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 8b0934c582..72ca4160dd 100644 --- "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -14,9 +14,9 @@ 也就是说程序运行的时间超过了规定的时间,一般OJ(online judge)的超时时间就是1s,也就是用例数据输入后最多要1s内得到结果,暂时还不清楚leetcode的判题规则,下文为了方便讲解,暂定超时时间就是1s。 -如果写出了一个$O(n)$的算法 ,其实可以估算出来n是多大的时候算法的执行时间就会超过1s了。 +如果写出了一个 $O(n)$ 的算法 ,其实可以估算出来n是多大的时候算法的执行时间就会超过1s了。 -如果n的规模已经足够让$O(n)$的算法运行时间超过了1s,就应该考虑log(n)的解法了。 +如果n的规模已经足够让 $O(n)$ 的算法运行时间超过了1s,就应该考虑log(n)的解法了。 ## 从硬件配置看计算机的性能 @@ -60,7 +60,7 @@ 测试硬件:2015年MacPro,CPU配置:2.7 GHz Dual-Core Intel Core i5 -实现三个函数,时间复杂度分别是 $O(n)$ , $O(n^2)$, $O(n\log n)$,使用加法运算来统一测试。 +实现三个函数,时间复杂度分别是 $O(n)$ , $O(n^2)$ , $O(n\log n)$ ,使用加法运算来统一测试。 ```CPP // O(n) @@ -126,19 +126,19 @@ int main() { ![程序超时2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729200018460-20230310124315093.png) -O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下$O(n^2)$ 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 +O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下 $O(n^2)$ 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 ![程序超时3](https://code-thinking-1253855093.file.myqcloud.com/pics/2020072919590970-20230310124318532.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 -在推测一下$O(n\log n)$的话, 1s可以处理的数据规模是什么呢? +在推测一下 $O(n\log n)$ 的话, 1s可以处理的数据规模是什么呢? -理论上应该是比 $O(n)$少一个数量级,因为$\log n$的复杂度 其实是很快,看一下实验数据。 +理论上应该是比 $O(n)$ 少一个数量级,因为 $\log n$ 的复杂度 其实是很快,看一下实验数据。 ![程序超时4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729195729407-20230310124322232.png) -$O(n\log n)$的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 +$O(n\log n)$ 的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 这是在我个人PC上测出来的数据,不能说是十分精确,但数量级是差不多的,大家也可以在自己的计算机上测一下。 @@ -263,7 +263,7 @@ public class TimeComplexity { ## 总结 -本文详细分析了在leetcode上做题程序为什么会有超时,以及从硬件配置上大体知道CPU的执行速度,然后亲自做一个实验来看看$O(n)$的算法,跑一秒钟,这个n究竟是做大,最后给出不同时间复杂度,一秒内可以运算出来的n的大小。 +本文详细分析了在leetcode上做题程序为什么会有超时,以及从硬件配置上大体知道CPU的执行速度,然后亲自做一个实验来看看 $O(n)$ 的算法,跑一秒钟,这个n究竟是做大,最后给出不同时间复杂度,一秒内可以运算出来的n的大小。 建议录友们也都自己做一做实验,测一测,看看是不是和我的测出来的结果差不多。 From d6bc92e2142fa0534f1b06b4c4f9895ae6c1ffeb Mon Sep 17 00:00:00 2001 From: paigeman <53284808+paigeman@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:16:19 +0800 Subject: [PATCH 1018/1533] fix typos show some LaTeX formula normally --- ...\220\345\221\250\346\234\253\346\200\273\347\273\223.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" index cd3b2f1346..461219f2da 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -54,10 +54,10 @@ 文中涉及如下问题: * 究竟什么是大O?大O表示什么意思?严格按照大O的定义来说,快排应该是$O(n^2)$的算法! -* $O(n^2)$的算法为什么有时候比$O(n)$的算法更优? +* $O(n^2)$ 的算法为什么有时候比 $O(n)$ 的算法更优? * 什么时间复杂度为什么可以忽略常数项? * 如何简化复杂的时间复杂度表达式,原理是什么? -* $O(\log n)$中的log究竟是以谁为底? +* $O(\log n)$ 中的log究竟是以谁为底? 这些问题大家可能懵懵懂懂的了解一些,但一细问又答不上来。 @@ -96,7 +96,7 @@ 文中给出了四个版本的代码实现,并逐一分析了其时间复杂度。 -此时大家就会发现,同一道题目,同样使用递归算法,有的同学会写出了O(n)的代码,有的同学就写出了$O(\log n)$的代码。 +此时大家就会发现,同一道题目,同样使用递归算法,有的同学会写出了O(n)的代码,有的同学就写出了 $O(\log n)$ 的代码。 其本质是要对递归的时间复杂度有清晰的认识,才能运用递归来有效的解决问题! From 356b2c6778eb9f8bec347cb6bf68648d4d749ee3 Mon Sep 17 00:00:00 2001 From: paigeman <53284808+paigeman@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:19:34 +0800 Subject: [PATCH 1019/1533] fix typos show some LaTeX formula normally --- ...\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" index 19384fd980..b669049293 100644 --- "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" @@ -32,7 +32,7 @@ 同样在工程实践中,计算机的内存空间也不是无限的,需要工程师对软件运行时所使用的内存有一个大体评估,这都需要用到算法空间复杂度的分析。 -来看一下例子,什么时候的空间复杂度是$O(1)$呢,C++代码如下: +来看一下例子,什么时候的空间复杂度是 $O(1)$ 呢,C++代码如下: ```CPP int j = 0; From 89ee2d263f02ea91ba8f5bc6e1fe73f38b306584 Mon Sep 17 00:00:00 2001 From: paigeman <53284808+paigeman@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:35:37 +0800 Subject: [PATCH 1020/1533] fix typo show some LaTeX formula correctly --- ...\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" index aacc456857..39513a9155 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" @@ -191,7 +191,7 @@ int main() 因为每次递归所需的空间都被压到调用栈里(这是内存管理里面的数据结构,和算法里的栈原理是一样的),一次递归结束,这个栈就是就是把本次递归的数据弹出去。所以这个栈最大的长度就是递归的深度。 -此时可以分析这段递归的空间复杂度,从代码中可以看出每次递归所需要的空间大小都是一样的,所以每次递归中需要的空间是一个常量,并不会随着n的变化而变化,每次递归的空间复杂度就是$O(1)$。 +此时可以分析这段递归的空间复杂度,从代码中可以看出每次递归所需要的空间大小都是一样的,所以每次递归中需要的空间是一个常量,并不会随着n的变化而变化,每次递归的空间复杂度就是 $O(1)$ 。 在看递归的深度是多少呢?如图所示: From 216f9db871f5c08a8b44573c678eab3915aa9f49 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Sat, 9 Mar 2024 17:15:37 +0800 Subject: [PATCH 1021/1533] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0139.单词拆分新增C语言实现 --- ...25\350\257\215\346\213\206\345\210\206.md" | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index d93288ae5a..a3d59ec718 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -498,6 +498,33 @@ function wordBreak(s: string, wordDict: string[]): boolean { }; ``` +### C + +```c +bool wordBreak(char* s, char** wordDict, int wordDictSize) { + int len = strlen(s); + // 初始化 + bool dp[len + 1]; + memset(dp, false, sizeof (dp)); + dp[0] = true; + for (int i = 1; i < len + 1; ++i) { + for(int j = 0; j < wordDictSize; j++){ + int wordLen = strlen(wordDict[j]); + // 分割点是由i和字典单词长度决定 + int k = i - wordLen; + if(k < 0){ + continue; + } + // 这里注意要限制长度,故用strncmp + dp[i] = (dp[k] && !strncmp(s + k, wordDict[j], wordLen)) || dp[i]; + } + } + return dp[len]; +} +``` + + + ### Rust: ```rust @@ -521,4 +548,3 @@ impl Solution { - From dd80ba7af9435c17df28c34496811c465cbfef19 Mon Sep 17 00:00:00 2001 From: Jamie He Date: Sun, 10 Mar 2024 02:43:38 +0000 Subject: [PATCH 1022/1533] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comment 错了 --- .../0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index bbbd5c6379..aaa758e63e 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -296,7 +296,7 @@ class Solution: def integerBreak(self, n): if n == 2: # 当n等于2时,只有一种拆分方式:1+1=2,乘积为1 return 1 - if n == 3: # 当n等于3时,只有一种拆分方式:1+1+1=3,乘积为1 + if n == 3: # 当n等于3时,只有一种拆分方式:2+1=3,乘积为2 return 2 if n == 4: # 当n等于4时,有两种拆分方式:2+2=4和1+1+1+1=4,乘积都为4 return 4 From 448f9ff77d76515cab7837d89c8425e518b45a51 Mon Sep 17 00:00:00 2001 From: cn-hideyoshi <54949420+cn-hideyoshi@users.noreply.github.com> Date: Sun, 10 Mar 2024 13:32:12 +0800 Subject: [PATCH 1023/1533] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=20=E6=B7=BB=E5=8A=A0php?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 203.移除链表元素.md 添加php解法 --- ...76\350\241\250\345\205\203\347\264\240.md" | 74 ++++++++++++++----- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index d6d7e6c2ef..3b04b1b266 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -497,27 +497,67 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? { ```php /** - * Definition for singly-linked list. - * type ListNode struct { - * Val int - * Next *ListNode + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } * } */ - // 虚拟头+双指针 -func removeElements(head *ListNode, val int) *ListNode { - dummyHead := &ListNode{} - dummyHead.Next = head - pred := dummyHead - cur := head - for cur != nil { - if cur.Val == val { - pred.Next = cur.Next - } else { - pred = cur + +//版本一(在原链表上直接删除): +class Solution { + + /** + * @param ListNode $head + * @param Integer $val + * @return ListNode + */ + function removeElements($head, $val) + { + if ($head == null) { + return null; } - cur = cur.Next + + $now = $head; + while ($now->next != null) { + if ($now->next->val == $val) { + $now->next = $now->next->next; + } else { + $now = $now->next; + } + } + if ($head->val == $val) { + return $head->next; + } + return $head; + } +} + +//版本二(虚拟头结点方式): +class Solution { + + /** + * @param ListNode $head + * @param Integer $val + * @return ListNode + */ + function removeElements($head, $val) + { + $dummyHead = new ListNode(0, $head); + $now = $dummyHead; + while ($now->next != null){ + if ($now->next->val == $val) { + $now->next = $now->next->next; + } else { + $now = $now->next; + } + } + return $dummyHead->next; } - return dummyHead.Next } ``` From bc6189e9e9898c72180f1787f7614e0c95d0181f Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Sun, 10 Mar 2024 17:50:38 +0800 Subject: [PATCH 1024/1533] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0198.打家劫舍新增C语言实现 --- ...23\345\256\266\345\212\253\350\210\215.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index a7bc4c998e..480222ef12 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -315,6 +315,31 @@ function rob(nums: number[]): number { }; ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int rob(int* nums, int numsSize) { + if(numsSize == 0){ + return 0; + } + if(numsSize == 1){ + return nums[0]; + } + // dp初始化 + int dp[numsSize]; + dp[0] = nums[0]; + dp[1] = max(nums[0], nums[1]); + for(int i = 2; i < numsSize; i++){ + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); + } + return dp[numsSize - 1]; +} +``` + + + ### Rust: ```rust @@ -339,3 +364,4 @@ impl Solution { + From 2cee392d8e4ad3064a8d437017bb4e8895ad7477 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Sun, 10 Mar 2024 17:56:42 +0800 Subject: [PATCH 1025/1533] =?UTF-8?q?Update=200213.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0213.打家劫舍||新增C语言实现 --- ...\345\256\266\345\212\253\350\210\215II.md" | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index 385c58675e..ba996f2bf4 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -308,6 +308,34 @@ function robRange(nums: number[], start: number, end: number): number { } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +// 198.打家劫舍的逻辑 +int robRange(int* nums, int start, int end, int numsSize) { + if (end == start) return nums[start]; + int dp[numsSize]; + dp[start] = nums[start]; + dp[start + 1] = max(nums[start], nums[start + 1]); + for (int i = start + 2; i <= end; i++) { + dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[end]; +} + +int rob(int* nums, int numsSize) { + if (numsSize == 0) return 0; + if (numsSize == 1) return nums[0]; + int result1 = robRange(nums, 0, numsSize - 2, numsSize); // 情况二 + int result2 = robRange(nums, 1, numsSize - 1, numsSize); // 情况三 + return max(result1, result2); +} +``` + + + ### Rust: ```rust @@ -343,4 +371,3 @@ impl Solution { - From 0dc2180cee3c015a91ab0906858f544efadd7d24 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Sun, 10 Mar 2024 21:44:04 +0800 Subject: [PATCH 1026/1533] =?UTF-8?q?Update=200337.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8DIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0337.打家劫舍|||新增C语言实现 --- ...345\256\266\345\212\253\350\210\215III.md" | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index f616ec7417..61b9f99c6f 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -490,6 +490,33 @@ function robNode(node: TreeNode | null): MaxValueArr { } ``` +### C + +```c +int *robTree(struct TreeNode *node) { + int* amounts = (int*) malloc(sizeof(int) * 2); + memset(amounts, 0, sizeof(int) * 2); + if(node == NULL){ + return amounts; + } + int * left = robTree(node->left); + int * right = robTree(node->right); + // 偷当前节点 + amounts[1] = node->val + left[0] + right[0]; + // 不偷当前节点 + amounts[0] = max(left[0], left[1]) + max(right[0], right[1]); + return amounts; +} + +int rob(struct TreeNode* root) { + int * dp = robTree(root); + // 0代表不偷当前节点可以获得的最大值,1表示偷当前节点可以获取的最大值 + return max(dp[0], dp[1]); +} +``` + + + ### Rust 动态规划: @@ -523,4 +550,3 @@ impl Solution { - From b6e458bcfa3be2f8b7f282f5918f9bc26512e9fd Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Sun, 10 Mar 2024 21:47:48 +0800 Subject: [PATCH 1027/1533] =?UTF-8?q?Update=200121.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0121.买卖股票的最佳时机新增C语言实现 --- ...00\344\275\263\346\227\266\346\234\272.md" | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index cbdf40e85c..fb548cbc91 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -531,6 +531,52 @@ public class Solution } ``` +### C: + +> 贪心 + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int maxProfit(int* prices, int pricesSize) { + int low = INT_MIN; + int result = 0; + for(int i = 0; i < pricesSize; i++){ + low = min(low, prices[i]); + result = max(result, prices[i] - low); + } + return result; +} +``` + +> 动态规划 + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int maxProfit(int* prices, int pricesSize){ + if(pricesSize == 0){ + return 0; + } + // dp初始化 + int ** dp = malloc(sizeof (int *) * pricesSize); + for(int i = 0; i < pricesSize; i++){ + dp[i] = malloc(sizeof (int ) * 2); + } + // 下标0表示持有股票的情况下的最大现金,下标1表示不持有股票的情况下获得的最大现金 + dp[0][0] = -prices[0]; + dp[0][1] = 0; + for(int i = 1; i < pricesSize; i++){ + dp[i][0] = max(dp[i - 1][0], - prices[i]); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]); + } + return dp[pricesSize - 1][1]; +} +``` + + + ### Rust: > 贪心 @@ -568,4 +614,3 @@ impl Solution { - From 410dff4d908a4e992a2a3dc1d583706d684c04c2 Mon Sep 17 00:00:00 2001 From: XD <1296586920@qq.com> Date: Tue, 12 Mar 2024 13:18:06 +0800 Subject: [PATCH 1028/1533] =?UTF-8?q?Update=200104.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=E4=B8=AD?= =?UTF-8?q?=E5=85=B3=E4=BA=8Esolution=E5=A4=A7=E5=B0=8F=E5=86=99=E9=97=AE?= =?UTF-8?q?=E9=A2=98.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了solution大小写问题,即solution改为Solution,力扣官网提交是需要大写的 --- ...00\345\244\247\346\267\261\345\272\246.md" | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 1f55f197e5..12d5640555 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -77,7 +77,7 @@ return depth; 所以整体c++代码如下: ```CPP -class solution { +class Solution { public: int getdepth(TreeNode* node) { if (node == NULL) return 0; @@ -94,7 +94,7 @@ public: 代码精简之后c++代码如下: ```CPP -class solution { +class Solution { public: int maxDepth(TreeNode* root) { if (root == null) return 0; @@ -110,7 +110,7 @@ public: 本题当然也可以使用前序,代码如下:(**充分表现出求深度回溯的过程**) ```CPP -class solution { +class Solution { public: int result; void getdepth(TreeNode* node, int depth) { @@ -144,7 +144,7 @@ public: 注意以上代码是为了把细节体现出来,简化一下代码如下: ```CPP -class solution { +class Solution { public: int result; void getdepth(TreeNode* node, int depth) { @@ -183,7 +183,7 @@ public: c++代码如下: ```CPP -class solution { +class Solution { public: int maxDepth(TreeNode* root) { if (root == NULL) return 0; @@ -232,7 +232,7 @@ public: c++代码: ```CPP -class solution { +class Solution { public: int maxDepth(Node* root) { if (root == 0) return 0; @@ -249,7 +249,7 @@ public: 依然是层序遍历,代码如下: ```CPP -class solution { +class Solution { public: int maxDepth(Node* root) { queue que; @@ -278,7 +278,7 @@ public: 104.二叉树的最大深度 ```java -class solution { +class Solution { /** * 递归法 */ @@ -319,7 +319,7 @@ class Solution { ``` ```java -class solution { +class Solution { /** * 迭代法,使用层序遍历 */ @@ -369,7 +369,7 @@ class Solution { ``` ```java -class solution { +class Solution { /** * 迭代法,使用层序遍历 */ @@ -402,7 +402,7 @@ class solution { 递归法: ```python -class solution: +class Solution: def maxdepth(self, root: treenode) -> int: return self.getdepth(root) @@ -417,7 +417,7 @@ class solution: 递归法:精简代码 ```python -class solution: +class Solution: def maxdepth(self, root: treenode) -> int: if not root: return 0 From cb58e8afa198a4b9abf57e0e3d76383d850b2d43 Mon Sep 17 00:00:00 2001 From: sss1h <49825610+sss1h@users.noreply.github.com> Date: Tue, 12 Mar 2024 20:20:00 +0800 Subject: [PATCH 1029/1533] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更正代码错误 --- ...225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index f508b523e3..2fcfd283b1 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -82,7 +82,7 @@ public: // ListNode *tmp = slow->next; C++释放内存的逻辑 // slow->next = tmp->next; - // delete nth; + // delete tmp; return dummyHead->next; } From 57e56c508b567c453945b3398dc84c83002d6ec0 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Tue, 12 Mar 2024 21:39:58 +0800 Subject: [PATCH 1030/1533] =?UTF-8?q?Update=200714.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=88=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0714.买卖股票的最佳时机含手续费新增C语言实现 --- ...01\350\247\204\345\210\222\357\274\211.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 7e8e3d7c61..88ba9271c8 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -247,7 +247,29 @@ function maxProfit(prices: number[], fee: number): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +// dp[i][0] 表示第i天持有股票所省最多现金。 +// dp[i][1] 表示第i天不持有股票所得最多现金 +int maxProfit(int* prices, int pricesSize, int fee) { + int dp[pricesSize][2]; + dp[0][0] = -prices[0]; + dp[0][1] = 0; + for (int i = 1; i < pricesSize; ++i) { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee); + } + return dp[pricesSize - 1][1]; +} +``` + + + ### Rust: + **贪心** ```Rust @@ -304,3 +326,4 @@ impl Solution { + From 1b9ae455df706c170e6af3c73543a15f8fdbef79 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Tue, 12 Mar 2024 21:43:13 +0800 Subject: [PATCH 1031/1533] =?UTF-8?q?Update=200122.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?II=EF=BC=88=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0122.买卖股票的最佳时机II新增C语言实现 --- ...01\350\247\204\345\210\222\357\274\211.md" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 6e08b57c1d..24c7f16823 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -365,6 +365,49 @@ public class Solution } ``` +### C: + +> 动态规划 + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int maxProfit(int* prices, int pricesSize){ + int **dp = malloc(sizeof (int *) * pricesSize); + for (int i = 0; i < pricesSize; ++i) { + dp[i] = malloc(sizeof (int ) * 2); + } + // 0表示持有该股票所得最大,1表示不持有所得最大 + dp[0][0] = -prices[0]; + dp[0][1] = 0; + for (int i = 1; i < pricesSize; ++i) { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]); + } + return dp[pricesSize - 1][1]; +} +``` + +> 贪心 + +```c +int maxProfit(int* prices, int pricesSize) { + if(pricesSize == 0){ + return 0; + } + int result = 0; + for(int i = 1; i < pricesSize; i++){ + // 如果今天股票价格大于昨天,代表有利润 + if(prices[i] > prices[i - 1]){ + result += prices[i] - prices[i - 1]; + } + } + return result; +} +``` + + + ### Rust: > 贪心 @@ -416,3 +459,4 @@ impl Solution { + From d40d61e265e33deddc2d84daacd52eb85afb5496 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Tue, 12 Mar 2024 21:44:50 +0800 Subject: [PATCH 1032/1533] =?UTF-8?q?Update=200123.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?III.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0123.买卖股票的最佳时机III新增C语言实现 --- ...344\275\263\346\227\266\346\234\272III.md" | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 72dd90426c..18f19c5192 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -413,6 +413,34 @@ function maxProfit(prices: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int maxProfit(int* prices, int pricesSize) { + int buy1 = prices[0], buy2 = prices[0]; + int profit1 = 0, profit2 = 0; + for (int i = 0; i < pricesSize; ++i) { + // 寻找最低点买入 + buy1 = min(buy1, prices[i]); + // 找到第一次交易的最大盈利,并不断维护这一最大值 + profit1 = max(profit1, prices[i] - buy1); + + // 寻找第二次交易的最低投资点,并且考虑前一次交易的成本 + // 当前价格 - 第一次操作的盈利=新的投入成本( + // 为了让盈利最大,要寻找最小的成本) + buy2 = min(buy2, prices[i] - profit1); + // 第二次卖出后的盈利:当前价格减去成本,不断维护这一最大的总利润 + profit2 = max(profit2, prices[i] - buy2); + } + return profit2; +} +``` + + + ### Rust: > 版本一 @@ -465,4 +493,3 @@ impl Solution { - From d91656d036e536637138ebe7c51c8a37eda01bc2 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Tue, 12 Mar 2024 21:46:06 +0800 Subject: [PATCH 1033/1533] =?UTF-8?q?Update=200188.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?IV.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0188.买卖股票的最佳时机IV新增C语言实现 --- ...\344\275\263\346\227\266\346\234\272IV.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index e4c5c48400..2521749fc9 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -474,6 +474,34 @@ function maxProfit(k: number, prices: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int maxProfit(int k, int* prices, int pricesSize) { + if(pricesSize == 0){ + return 0; + } + + int dp[pricesSize][2 * k + 1]; + memset(dp, 0, sizeof(int) * pricesSize * (2 * k + 1)); + for (int j = 1; j < 2 * k; j += 2) { + dp[0][j] = -prices[0]; + } + + for (int i = 1;i < pricesSize; i++) {//枚举股票 + for (int j = 0; j < 2 * k - 1; j += 2) { //更新每一次买入卖出 + dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]); + dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]); + } + } + return dp[pricesSize - 1][2 * k]; +} +``` + + + ### Rust: ```rust @@ -529,3 +557,4 @@ impl Solution { + From ec899d684b31e7fee9ba80836ec9683084d5dacb Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Tue, 12 Mar 2024 21:47:38 +0800 Subject: [PATCH 1034/1533] =?UTF-8?q?Update=200309.=E6=9C=80=E4=BD=B3?= =?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E5=86=B7=E5=86=BB=E6=9C=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0309.最佳买卖股票时机含冷冻期新增C语言实现 --- ...53\345\206\267\345\206\273\346\234\237.md" | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 0eb66fb543..9dc35bdf52 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -457,6 +457,40 @@ function maxProfit(prices: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +/** + * 状态一:持有股票状态(今天买入股票, + * 或者是之前就买入了股票然后没有操作,一直持有) + * 不持有股票状态,这里就有两种卖出股票状态 + * 状态二:保持卖出股票的状态(两天前就卖出了股票,度过一天冷冻期。 + * 或者是前一天就是卖出股票状态,一直没操作) + * 状态三:今天卖出股票 + * 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天! + + */ +int maxProfit(int* prices, int pricesSize) { + if(pricesSize == 0){ + return 0; + } + int dp[pricesSize][4]; + memset(dp, 0, sizeof (int ) * pricesSize * 4); + dp[0][0] = -prices[0]; + for (int i = 1; i < pricesSize; ++i) { + dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1] - prices[i], dp[i - 1][3] - prices[i])); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); + dp[i][2] = dp[i - 1][0] + prices[i]; + dp[i][3] = dp[i - 1][2]; + } + return max(dp[pricesSize - 1][1], max(dp[pricesSize - 1][2], dp[pricesSize - 1][3])); +} +``` + + + ### Rust: ```rust @@ -486,4 +520,3 @@ impl Solution { - From 5f07ae3b033644dc6cbcf96c23eeef136ed4a5b0 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Wed, 13 Mar 2024 22:00:54 +0800 Subject: [PATCH 1035/1533] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0300.最长上升子序列的新增C语言实现 --- ...07\345\255\220\345\272\217\345\210\227.md" | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 64f75291f5..6d82eae1e9 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -288,6 +288,36 @@ function lengthOfLIS(nums: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int lengthOfLIS(int* nums, int numsSize) { + if(numsSize <= 1){ + return numsSize; + } + int dp[numsSize]; + for(int i = 0; i < numsSize; i++){ + dp[i]=1; + } + int result = 1; + for (int i = 1; i < numsSize; ++i) { + for (int j = 0; j < i; ++j) { + if(nums[i] > nums[j]){ + dp[i] = max(dp[i], dp[j] + 1); + } + if(dp[i] > result){ + result = dp[i]; + } + } + } + return result; +} +``` + + + ### Rust: ```rust @@ -311,4 +341,3 @@ pub fn length_of_lis(nums: Vec) -> i32 { - From bf457f49bb1961bc0013283b8b9678363a9a7207 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Wed, 13 Mar 2024 22:04:23 +0800 Subject: [PATCH 1036/1533] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0674.最长连续递增序列新增C语言实现 --- ...22\345\242\236\345\272\217\345\210\227.md" | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 485e321c99..ece629447c 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -425,6 +425,57 @@ function findLengthOfLCIS(nums: number[]): number { }; ``` +### C: + +> 动态规划: + +```c +int findLengthOfLCIS(int* nums, int numsSize) { + if(numsSize == 0){ + return 0; + } + int dp[numsSize]; + for(int i = 0; i < numsSize; i++){ + dp[i] = 1; + } + int result = 1; + for (int i = 1; i < numsSize; ++i) { + if(nums[i] > nums[i - 1]){ + dp[i] = dp[i - 1] + 1; + } + if(dp[i] > result){ + result = dp[i]; + } + } + return result; +} +``` + + + +> 贪心: + +```c +int findLengthOfLCIS(int* nums, int numsSize) { + int result = 1; + int count = 1; + if(numsSize == 0){ + return result; + } + for (int i = 1; i < numsSize; ++i) { + if(nums[i] > nums[i - 1]){ + count++; + } else{ + count = 1; + } + if(count > result){ + result = count; + } + } + return result; +} +``` + @@ -432,4 +483,3 @@ function findLengthOfLCIS(nums: number[]): number { - From 00a5515bad938f6c3ff32e3f88c5639346c0f947 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Wed, 13 Mar 2024 22:05:38 +0800 Subject: [PATCH 1037/1533] =?UTF-8?q?Update=200718.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0178.最长重复子数组新增C语言实现 --- ...15\345\255\220\346\225\260\347\273\204.md" | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 272cf2b2a4..e00b3dedf3 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -560,10 +560,30 @@ impl Solution { } ``` +### C: + +```c +int findLength(int* nums1, int nums1Size, int* nums2, int nums2Size) { + int dp[nums1Size + 1][nums2Size + 1]; + memset(dp, 0, sizeof(dp)); + int result = 0; + for (int i = 1; i <= nums1Size; ++i) { + for (int j = 1; j <= nums2Size; ++j) { + if(nums1[i - 1] == nums2[j - 1]){ + dp[i][j] = dp[i - 1][j - 1] + 1; + } + if(dp[i][j] > result){ + result = dp[i][j]; + } + } + } + return result; +} +``` +

- From fac689939f7c6a31f11198a4ecccfa3d0f2bee6a Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Wed, 13 Mar 2024 22:08:15 +0800 Subject: [PATCH 1038/1533] =?UTF-8?q?Update=201143.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1143.最长公共子序列新增C语言实现 --- ...61\345\255\220\345\272\217\345\210\227.md" | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index f33391c3e6..12bd90f8f4 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -376,10 +376,32 @@ impl Solution { } ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int longestCommonSubsequence(char* text1, char* text2) { + int text1Len = strlen(text1); + int text2Len = strlen(text2); + int dp[text1Len + 1][text2Len + 1]; + memset(dp, 0, sizeof (dp)); + for (int i = 1; i <= text1Len; ++i) { + for (int j = 1; j <= text2Len; ++j) { + if(text1[i - 1] == text2[j - 1]){ + dp[i][j] = dp[i - 1][j - 1] + 1; + } else{ + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + return dp[text1Len][text2Len]; +} +``` +

- From a79bf97d72b1ede1c9c6f6874f705b056677a4b8 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 14 Mar 2024 12:09:57 +0800 Subject: [PATCH 1039/1533] Update --- ...7.\350\247\243\346\225\260\347\213\254.md" | 2 +- ...\350\267\203\346\270\270\346\210\217II.md" | 2 +- ...66\350\277\237\346\227\266\351\227\264.md" | 1230 +++++++++++++++++ ...34\347\232\204\350\210\252\347\217\255.md" | 44 + ...17\202\344\274\232dijkstra\345\240\206.md" | 651 +++++++++ ...74\232dijkstra\346\234\264\347\264\240.md" | 733 ++++++++++ ...ama53.\345\257\273\345\256\235-Kruskal.md" | 400 ++++++ .../kama53.\345\257\273\345\256\235-prim.md" | 55 +- problems/qita/shejimoshi.md | 2 + 9 files changed, 3096 insertions(+), 23 deletions(-) create mode 100644 "problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" create mode 100644 "problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" create mode 100644 "problems/kama47.\345\217\202\344\274\232dijkstra\345\240\206.md" create mode 100644 "problems/kama47.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" create mode 100644 "problems/kama53.\345\257\273\345\256\235-Kruskal.md" rename "problems/kama53.\345\257\273\345\256\235.md" => "problems/kama53.\345\257\273\345\256\235-prim.md" (93%) diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index d96e59dfeb..b5f54b1f8f 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -87,7 +87,7 @@ bool backtracking(vector>& board) ![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131822254.png) -在树形图中可以看出我们需要的是一个二维的递归(也就是两个for循环嵌套着递归) +在树形图中可以看出我们需要的是一个二维的递归 (一行一列) **一个for循环遍历棋盘的行,一个for循环遍历棋盘的列,一行一列确定下来之后,递归遍历这个位置放9个数字的可能性!** diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index e006caa2ca..01a9320dc3 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -4,7 +4,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心里准备! +> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心理准备! # 45.跳跃游戏 II diff --git "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" new file mode 100644 index 0000000000..d94406091b --- /dev/null +++ "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" @@ -0,0 +1,1230 @@ + +# 743.网络延迟时间 + +https://leetcode.cn/problems/network-delay-time/description/ + + +有 n 个网络节点,标记为 1 到 n。 + +给你一个列表 times,表示信号经过 有向 边的传递时间。 times[i] = (ui, vi, wi),其中 ui 是源节点,vi 是目标节点, wi 是一个信号从源节点传递到目标节点的时间。 + +现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1 。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240229104105.png) + +提示: + +* 1 <= k <= n <= 100 +* 1 <= times.length <= 6000 +* times[i].length == 3 +* 1 <= ui, vi <= n +* ui != vi +* 0 <= wi <= 100 +* 所有 (ui, vi) 对都 互不相同(即,不含重复边) + +# dijkstra 精讲 + +本题就是求最短路,最短路是图论中的经典问题即:给出一个有向图,一个起点,一个终点,问起点到终点的最短路径。 + +接下来,我们来详细讲解最短路算法中的 dijkstra 算法。 + +dijkstra算法:在有权图(权值非负数)中求从起点到其他节点的最短路径算法。 + +需要注意两点: + +* dijkstra 算法可以同时求 起点到所有节点的最短路径 +* 权值不能为负数 + +(这两点后面我们会讲到) + +如本题示例中的图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240125162647.png) + +起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 + +最短路径的权值为12。 + +其实 dijkstra 算法 和 我们之前讲解的prim算法思路非常接近,如果大家认真学过[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w),那么理解 Dijkstra 算法会相对容易很多。(这也是我要先讲prim再讲dijkstra的原因) + +dijkstra 算法 同样是贪心的思路,不断寻找距离 源点最近的没有访问过的节点。 + +这里我也给出 **dijkstra三部曲**: + +1. 第一步,选源点到哪个节点近且该节点未被访问过 +2. 第二步,该最近节点被标记访问过 +3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + +大家此时已经会发现,这和prim算法 怎么这么像呢。 + +我在[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w)讲解中也给出了三部曲。 prim 和 dijkstra 确实很像,思路也是类似的,这一点我在后面还会详细来讲。 + +在dijkstra算法中,同样有一个数组很重要,起名为:minDist。 + +**minDist数组 用来记录 每一个节点距离源点的最小距离**。 + +理解这一点很重要,也是理解 dijkstra 算法的核心所在。 + +大家现在看着可能有点懵,不知道什么意思。 + +没关系,先让大家有一个印象,对理解后面讲解有帮助。 + +我们先来画图看一下 dijkstra 的工作过程,以本题示例为例: (以下为朴素版dijkstra的思路) + +(**示例中节点编号是从1开始,所以为了让大家看的不晕,minDist数组下标我也从 1 开始计数,下标0 就不使用了,这样 下标和节点标号就可以对应上了,避免大家搞混**) + +## 朴素版dijkstra + +### 模拟过程 + +----------- + +0、初始化 + +minDist数组数值初始化为int最大值。 + +这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) + +(图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) + +源点(节点1) 到自己的距离为0,所以 minDist[1] = 0 + +此时所有节点都没有被访问过,所以 visited数组都为0 + +--------------- + +以下为dijkstra 三部曲 + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离源点最近,距离为0,且未被访问。 + +2、该最近节点被标记访问过 + +标记源点访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) + + +更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 + +* 源点到节点2的最短距离为1,小于原minDist[2]的数值max,更新minDist[2] = 1 +* 源点到节点3的最短距离为4,小于原minDist[3]的数值max,更新minDist[4] = 4 + +可能有录友问:为啥和 minDist[2] 比较? + +再强调一下 minDist[2] 的含义,它表示源点到节点2的最短距离,那么目前我们得到了 源点到节点2的最短距离为1,小于默认值max,所以更新。 minDist[3]的更新同理 + + +------------- + +1、选源点到哪个节点近且该节点未被访问过 + +未访问过的节点中,源点到节点2距离最近,选节点2 + +2、该最近节点被标记访问过 + +节点2被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) + +更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 + +**为什么更新这些节点呢? 怎么不更新其他节点呢**? + +因为 源点(节点1)通过 已经计算过的节点(节点2) 可以链接到的节点 有 节点3,节点4和节点6. + + +更新 minDist数组: + +* 源点到节点6的最短距离为5,小于原minDist[6]的数值max,更新minDist[6] = 5 +* 源点到节点3的最短距离为3,小于原minDist[3]的数值4,更新minDist[3] = 3 +* 源点到节点4的最短距离为6,小于原minDist[4]的数值max,更新minDist[4] = 6 + + + +------------------- + +1、选源点到哪个节点近且该节点未被访问过 + +未访问过的节点中,源点距离哪些节点最近,怎么算的? + +其实就是看 minDist数组里的数值,minDist 记录了 源点到所有节点的最近距离,结合visited数组筛选出未访问的节点就好。 + +从 上面的图,或者 从minDist数组中,我们都能看出 未访问过的节点中,源点(节点1)到节点3距离最近。 + + +2、该最近节点被标记访问过 + +节点3被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) + +由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: + +更新 minDist数组: + +* 源点到节点4的最短距离为5,小于原minDist[4]的数值6,更新minDist[4] = 5 + +------------------ + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,有节点4 和 节点6,距离源点距离都是 5 (minDist[4] = 5,minDist[6] = 5) ,选哪个节点都可以。 + +2、该最近节点被标记访问过 + +节点4被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) + +由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: + +* 源点到节点5的最短距离为8,小于原minDist[5]的数值max,更新minDist[5] = 8 + +-------------- + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,是节点6,距离源点距离是 5 (minDist[6] = 5) + + +2、该最近节点被标记访问过 + +节点6 被标记访问过 + + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110250.png) + +由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: + +* 源点到节点7的最短距离为14,小于原minDist[7]的数值max,更新minDist[7] = 14 + + + +------------------- + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,是节点5,距离源点距离是 8 (minDist[5] = 8) + +2、该最近节点被标记访问过 + +节点5 被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110651.png) + +由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: + +* 源点到节点7的最短距离为12,小于原minDist[7]的数值14,更新minDist[7] = 12 + +----------------- + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,是节点7(终点),距离源点距离是 12 (minDist[7] = 12) + +2、该最近节点被标记访问过 + +节点7 被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110920.png) + +节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 + +-------------------- + +最后我们要求起点(节点1) 到终点 (节点7)的距离。 + +再来回顾一下minDist数组的含义:记录 每一个节点距离源点的最小距离。 + +那么起到(节点1)到终点(节点7)的最短距离就是 minDist[7] ,按上面举例讲解来说,minDist[7] = 12,节点1 到节点7的最短路径为 12。 + +路径如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) + +在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 + +### 代码实现 + +本题代码如下,里面的 三部曲 我都做了注释,大家按照我上面的讲解 来看如下代码: + +```CPP +class Solution { +public: + int networkDelayTime(vector>& times, int n, int k) { + + // 注意题目中给的二维数组并不是领接矩阵 + // 需要邻接矩阵来存图 + // 因为本题处理方式是节点标号从1开始,所以数组的大小都是 n+1 + vector> grid(n + 1, vector(n + 1, INT_MAX)); + for(int i = 0; i < times.size(); i++){ + int p1 = times[i][0]; + int p2 = times[i][1]; + grid[p1][p2] = times[i][2]; + } + + // 存储从源点到每个节点的最短距离 + std::vector minDist(n + 1, INT_MAX); + + // 记录顶点是否被访问过 + std::vector visited(n + 1, false); + + minDist[k] = 0; // 起始点到自身的距离为0 + for (int i = 1; i <= n; i++) { + + int minVal = INT_MAX; + int cur = 1; + + // 遍历每个节点,选择未被访问的节点集合中哪个节点到源点的距离最小 + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] <= minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; // 标记该顶点已被访问 + + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } + } + + + } + // 源点到最远的节点的时间,也就是寻找 源点到所有节点最短路径的最大值 + int result = 0; + for (int i = 1; i <= n; i++) { + if (minDist[i] == INT_MAX) return -1;// 没有路径 + result = max(minDist[i], result); + } + return result; + + } +}; +``` + +* 时间复杂度:O(n^2) +* 空间复杂度:O(n^2) + +### debug方法 + +写这种题目难免会有各种各样的问题,我们如何发现自己的代码是否有问题呢? + +最好的方式就是打日志,本题的话,就是将 minDist 数组打印出来,就可以很明显发现 哪里出问题了。 + +每次选择节点后,minDist数组的变化是否符合预期 ,是否和我上面讲的逻辑是对应的。 + +例如本题,如果想debug的话,打印日志可以这样写: + + +```CPP +class Solution { +public: + int networkDelayTime(vector>& times, int n, int k) { + + // 注意题目中给的二维数组并不是领接矩阵 + // 需要邻接矩阵来存图 + // 因为本题处理方式是节点标号从1开始,所以数组的大小都是 n+1 + vector> grid(n + 1, vector(n + 1, INT_MAX)); + for(int i = 0; i < times.size(); i++){ + int p1 = times[i][0]; + int p2 = times[i][1]; + grid[p1][p2] = times[i][2]; + } + + // 存储从源点到每个节点的最短距离 + std::vector minDist(n + 1, INT_MAX); + + // 记录顶点是否被访问过 + std::vector visited(n + 1, false); + + minDist[k] = 0; // 起始点到自身的距离为0 + for (int i = 1; i <= n; i++) { + + int minVal = INT_MAX; + int cur = 1; + + // 遍历每个节点,选择未被访问的节点集合中哪个节点到源点的距离最小 + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] <= minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; // 标记该顶点已被访问 + + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } + } + // 打印日志: + cout << "select:" << cur << endl; + for (int v = 1; v <= n; v++) cout << v << ":" << minDist[v] << " "; + cout << endl << endl;; + + + + } + // 源点到最远的节点的时间,也就是寻找 源点到所有节点最短路径的最大值 + int result = 0; + for (int i = 1; i <= n; i++) { + if (minDist[i] == INT_MAX) return -1;// 没有路径 + result = max(minDist[i], result); + } + return result; + + } +}; + + +``` + +打印后的结果: + +``` +select:2 +1:1 2:0 3:1 4:2147483647 + +select:3 +1:1 2:0 3:1 4:2 + +select:1 +1:1 2:0 3:1 4:2 + +select:4 +1:1 2:0 3:1 4:2 +``` + +打印日志可以和上面我讲解的过程进行对比,每一步的结果是完全对应的。 + +所以如果大家如果代码有问题,打日志来debug是最好的方法 + +### 出现负数 + +如果图中边的权值为负数,dijkstra 还合适吗? + +看一下这个图: (有负权值) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104334.png) + +节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 + +那我们来看dijkstra 求解的路径是什么样的,继续dijkstra 三部曲来模拟 :(dijkstra模拟过程上面已经详细讲过,以下只模拟重要过程,例如如何初始化就省略讲解了) + +----------- + +初始化: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104801.png) + +--------------- + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离源点最近,距离为0,且未被访问。 + +2、该最近节点被标记访问过 + +标记源点访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110217.png) + +更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 + +* 源点到节点2的最短距离为100,小于原minDist[2]的数值max,更新minDist[2] = 100 +* 源点到节点3的最短距离为1,小于原minDist[3]的数值max,更新minDist[4] = 1 + +------------------- + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点3最近,距离为1,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点3访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110330.png) + +由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: + +* 源点到节点4的最短距离为2,小于原minDist[4]的数值max,更新minDist[4] = 2 + +-------------- + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点4最近,距离为2,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点4访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110346.png) + +由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: + +* 源点到节点5的最短距离为3,小于原minDist[5]的数值max,更新minDist[5] = 5 + +------------ + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点5最近,距离为3,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点5访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110405.png) + +节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 + +------------ + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点2最近,距离为100,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点2访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110711.png) + +-------------- + +至此dijkstra的模拟过程就结束了,根据最后的minDist数组,我们求 节点1 到 节点5 的最短路径的权值总和为 3,路径: 节点1 -> 节点3 -> 节点4 -> 节点5 + +通过以上的过程模拟,我们可以发现 之所以 没有走有负权值的最短路径 是因为 在 访问 节点 2 的时候,节点 3 已经访问过了,就不会再更新了。 + +那有录友可能会想: 我可以改代码逻辑啊,访问过的节点,也让它继续访问不就好了? + +那么访问过的节点还能继续访问会不会有死循环的出现呢?控制逻辑不让其死循环?那特殊情况自己能都想清楚吗?(可以试试,实践出真知) + +对于负权值的出现,大家可以针对某一个场景 不断去修改 dijkstra 的代码,**但最终会发现只是 拆了东墙补西墙**,对dijkstra的补充逻辑只能满足某特定场景最短路求解。 + +对于求解带有负权值的最短路问题,可以使用 Floyd 算法 ,我在后序会详细讲解。 + +## dijkstra与prim算法的区别 + +> 这里再次提示,需要先看我的 [prim算法精讲](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w) ,否则可能不知道我下面讲的是什么。 + +大家可以发现 dijkstra的代码看上去 怎么和 prim算法这么像呢。 + +其实代码大体不差,唯一区别在 三部曲中的 第三步: 更新minDist数组 + +因为**prim是求 非访问节点到最小生成树的最小距离,而 dijkstra是求 非访问节点到源点的最小距离**。 + +prim 更新 minDist数组的写法: + + +```CPP +for (int j = 1; j <= v; j++) { + if (!isInTree[j] && grid[cur][j] < minDist[j]) { + minDist[j] = grid[cur][j]; + } +} +``` + +因为 minDist表示 节点到最小生成树的最小距离,所以 新节点cur的加入,只需要 使用 grid[cur][j] ,grid[cur][j] 就表示 cur 加入生成树后,生成树到 节点j 的距离。 + +dijkstra 更新 minDist数组的写法: + +```CPP +for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } +} +``` + +因为 minDist表示 节点到源点的最小距离,所以 新节点 cur 的加入,需要使用 源点到cur的距离 (minDist[cur]) + cur 到 节点 v 的距离 (grid[cur][v]),才是 源点到节点v的距离。 + +此时大家可能不禁要想 prim算法 可以有负权值吗? + +当然可以! + +录友们可以自己思考思考一下,这是为什么? + +这里我提示一下:prim算法只需要将节点以最小权值和链接在一起,不涉及到单一路径。 + + + +## 总结 + +本篇,我们深入讲解的dijkstra算法,详细模拟其工作的流程。 + +这里我给出了 **dijkstra 三部曲 来 帮助大家理解 该算法**,不至于 每次写 dijkstra 都是黑盒操作,没有框架没有章法。 + +在给出的代码中,我也按照三部曲的逻辑来给大家注释,只要理解这三部曲,即使 过段时间 对 dijkstra 算法有些遗忘,依然可以写出一个框架出来,然后再去调试细节。 + +对于图论算法,一般代码都比较长,很难写出代码直接可以提交通过,都需要一个debug的过程,所以 **学习如何debug 非常重要**! + +这也是我为什么 在本文中 单独用来讲解 debug方法。 + +本题求的是最短路径和是多少,**同时我们也要掌握 如何把最短路径打印出来**。 + +我还写了大篇幅来讲解 负权值的情况, 只有画图带大家一步一步去 看 出现负权值 dijkstra的求解过程,才能帮助大家理解,问题出在哪里。 + +如果我直接讲:是**因为访问过的节点 不能再访问,导致错过真正的最短路**,我相信大家都不知道我在说啥。 + +最后我还讲解了 dijkstra 和 prim 算法的 相同 与 不同之处, 我在图论的讲解安排中 先讲 prim算法 再讲 dijkstra 是有目的的, **理解这两个算法的相同与不同之处 有助于大家学习的更深入**。 + +而不是 学了 dijkstra 就只看 dijkstra, 算法之间 都是有联系的,多去思考 算法之间的相互联系,会帮助大家思考的更深入,掌握的更彻底。 + +本篇写了这么长,我也只讲解了 朴素版dijkstra,**关于 堆优化dijkstra,我会在下一篇再来给大家详细讲解**。 + + + +## 堆优化版本dijkstra + +> 本篇我们来讲解 堆优化版dijkstra,看本篇之前,一定要先看 我讲解的 朴素版dijkstra,否则本篇会有部分内容看不懂。 + +在上一篇中,我们讲解了朴素版的dijkstra,该解法的时间复杂度为 O(n^2),可以看出时间复杂度 只和 n (节点数量)有关系。 + +如果n很大的话,我们可以换一个角度来优先性能。 + +在 讲解 最小生成树的时候,我们 讲了两个算法,[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w)(从点的角度来求最小生成树)、[Kruskal算法](https://mp.weixin.qq.com/s/rUVaBjCES_4eSjngceT5bw)(从边的角度来求最小生成树) + +这么在n 很大的时候,也有另一个思考维度,即:从边的数量出发。 + +当 n 很大,边 的数量 也很多的时候(稠密图),那么 上述解法没问题。 + +但 n 很大,边 的数量 很小的时候(稀疏图),是不是可以换成从边的角度来求最短路呢? + +毕竟边的数量少。 + +有的录友可能会想,n (节点数量)很大,边不就多吗? 怎么会边的数量少呢? + +别忘了,谁也没有规定 节点之间一定要有边连接着,例如有一万个节点,只有一条边,这也是一张图。 + +了解背景之后,再来看 解法思路。 + +### 图的存储 + +首先是 图的存储。 + +关于图的存储 主流有两种方式: 邻接矩阵和邻接表 + +#### 邻接矩阵 + +邻接矩阵 使用 二维数组来表示图结构。 邻接矩阵是从节点的角度来表示图,有多少节点就申请多大的二维数组。 + +例如: grid[2][5] = 6,表示 节点 2 链接 节点5 为有向图,节点2 指向 节点5,边的权值为6 (套在题意里,可能是距离为6 或者 消耗为6 等等) + +如果想表示无向图,即:grid[2][5] = 6,grid[5][2] = 6,表示节点2 与 节点5 相互连通,权值为6。 + + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) + +在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 + +这种表达方式(邻接矩阵) 在 边少,节点多的情况下,会导致申请过大的二维数组,造成空间浪费。 + +而且在寻找节点链接情况的时候,需要遍历整个矩阵,即 n * n 的时间复杂度,同样造成时间浪费。 + +邻接矩阵的优点: + +* 表达方式简单,易于理解 +* 检查任意两个顶点间是否存在边的操作非常快 +* 适合稠密图,在边数接近顶点数平方的图中,邻接矩阵是一种空间效率较高的表示方法。 + +缺点: + +* 遇到稀疏图,会导致申请过大的二维数组造成空间浪费 且遍历 边 的时候需要遍历整个n * n矩阵,造成时间浪费 + +#### 邻接表 + +邻接表 使用 数组 + 链表的方式来表示。 邻接表是从边的数量来表示图,有多少边 才会申请对应大小的链表。 + +邻接表的构造如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) + +这里表达的图是: + +* 节点1 指向 节点3 和 节点5 +* 节点2 指向 节点4、节点3、节点5 +* 节点3 指向 节点4,节点4指向节点1。 + +有多少边 邻接表才会申请多少个对应的链表节点。 + +从图中可以直观看出 使用 数组 + 链表 来表达 边的链接情况 。 + +邻接表的优点: + +* 对于稀疏图的存储,只需要存储边,空间利用率高 +* 遍历节点链接情况相对容易 + +缺点: + +* 检查任意两个节点间是否存在边,效率相对低,需要 O(V)时间,V表示某节点链接其他节点的数量。 +* 实现相对复杂,不易理解 + +#### 本题图的存储 + +接下来我们继续按照稀疏图的角度来分析本题。 + +在第一个版本的实现思路中,我们提到了三部曲: + +1. 第一步,选源点到哪个节点近且该节点未被访问过 +2. 第二步,该最近节点被标记访问过 +3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + +在第一个版本的代码中,这三部曲是套在一个 for 循环里,为什么? + +因为我们是从节点的角度来解决问题。 + +三部曲中第一步(选源点到哪个节点近且该节点未被访问过),这个操作本身需要for循环遍历 minDist 来寻找最近的节点。 + +同时我们需要 遍历所有 未访问过的节点,所以 我们从 节点角度出发,代码会有两层for循环,代码是这样的: (注意代码中的注释,标记两层for循环的用处) + +```CPP + +for (int i = 1; i <= n; i++) { // 遍历所有节点,第一层for循环 + + int minVal = INT_MAX; + int cur = 1; + + // 1、选距离源点最近且未访问过的节点 , 第二层for循环 + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] < minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; // 2、标记该节点已被访问 + + // 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } + } + +} +``` + +那么当从 边 的角度出发, 在处理 三部曲里的第一步(选源点到哪个节点近且该节点未被访问过)的时候 ,我们可以不用去遍历所有节点了。 + +而且 直接把 边(带权值)加入到 小顶堆(利用堆来自动排序),那么每次我们从 堆顶里 取出 边 自然就是 距离源点最近的节点所在的边。 + +这样我们就不需要两层for循环来寻找最近的节点了。 + +了解了大体思路,我们再来看代码实现。 + +首先是 如何使用 邻接表来表述图结构,这是摆在很多录友面前的第一个难题。 + +邻接表用 数组+链表 来表示,代码如下:(C++中 vector 为数组,list 为链表, 定义了 n+1 这么大的数组空间) + +```CPP +vector> grid(n + 1); +``` + +不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) + +图中邻接表表示: + +* 节点1 指向 节点3 和 节点5 +* 节点2 指向 节点4、节点3、节点5 +* 节点3 指向 节点4 +* 节点4 指向 节点1 + +大家发现图中的边没有权值,而本题中 我们的边是有权值的,权值怎么表示?在哪里表示? + +所以 在`vector> grid(n + 1);` 中 就不能使用int了,而是需要一个键值对 来存两个数字,一个数表示节点,一个数表示 指向该节点的这条边的权值。 + +那么 代码可以改成这样: (pair 为键值对,可以存放两个int) + +```CPP +vector>> grid(n + 1); +``` + +举例来给大家展示 该代码表达的数据 如下: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103904.png) + +* 节点1 指向 节点3 权值为 1 +* 节点1 指向 节点5 权值为 2 +* 节点2 指向 节点4 权值为 7 +* 节点2 指向 节点3 权值为 6 +* 节点2 指向 节点5 权值为 3 +* 节点3 指向 节点4 权值为 3 +* 节点5 指向 节点1 权值为 10 + +这样 我们就把图中权值表示出来了。 + +但是在代码中 使用 `pair` 很容易让我们搞混了,第一个int 表示什么,第二个int表示什么,导致代码可读性很差,或者说别人看你的代码看不懂。 + +那么 可以 定一个类 来取代 `pair` + +类(或者说是结构体)定义如下: + +```CPP +struct Edge { + int to; // 邻接顶点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; +``` + +这个类里有两个成员变量,有对应的命名,这样不容易搞混 两个int的含义。 + +所以 本题中邻接表的定义如下: + +```CPP +struct Edge { + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + +vector> grid(n + 1); // 邻接表 + +``` + +(我们在下面的讲解中会直接使用这个邻接表的代码表示方式) + +### 堆优化细节 + +其实思路依然是 dijkstra 三部曲: + +1. 第一步,选源点到哪个节点近且该节点未被访问过 +2. 第二步,该最近节点被标记访问过 +3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + +只不过之前是 通过遍历节点来遍历边,通过两层for循环来寻找距离源点最近节点。 这次我们直接遍历边,且通过堆来对边进行排序,达到直接选择距离源点最近节点。 + +先来看一下针对这三部曲,如果用 堆来优化。 + +那么三部曲中的第一步(选源点到哪个节点近且该节点未被访问过),我们如何选? + +我们要选择距离源点近的节点(即:该边的权值最小),所以 我们需要一个 小顶堆 来帮我们对边的权值排序,每次从小顶堆堆顶 取边就是权值最小的边。 + +C++定义小顶堆,可以用优先级队列实现,代码如下: + +```CPP +// 小顶堆 +class mycomparison { +public: + bool operator()(const pair& lhs, const pair& rhs) { + return lhs.second > rhs.second; + } +}; +// 优先队列中存放 pair<节点编号,源点到该节点的权值> +priority_queue, vector>, mycomparison> pq; +``` + +(`pair`中 第二个int 为什么要存 源点到该节点的权值,因为 这个小顶堆需要按照权值来排序) + + +有了小顶堆自动对边的权值排序,那我们只需要直接从 堆里取堆顶元素(小顶堆中,最小的权值在上面),就可以取到离源点最近的节点了 (未访问过的节点,不会加到堆里进行排序) + +所以三部曲中的第一步,我们不用 for循环去遍历,直接取堆顶元素: + +```CPP +// pair<节点编号,源点到该节点的权值> +pair cur = pq.top(); pq.pop(); + +``` + +第二步(该最近节点被标记访问过) 这个就是将 节点做访问标记,和 朴素dijkstra 一样 ,代码如下: + +```CPP +// 2. 第二步,该最近节点被标记访问过 +visited[cur.first] = true; + +``` + +(`cur.first` 是指取 `pair` 里的第一个int,即节点编号 ) + +第三步(更新非访问节点到源点的距离),这里的思路 也是 和朴素dijkstra一样的。 + +但很多录友对这里是最懵的,主要是因为两点: + +* 没有理解透彻 dijkstra 的思路 +* 没有理解 邻接表的表达方式 + +我们来回顾一下 朴素dijkstra 在这一步的代码和思路(如果没看过我讲解的朴素版dijkstra,这里会看不懂) + +```CPP + +// 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) +for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } +} +``` + +其中 for循环是用来做什么的? 是为了 找到 节点cur 链接指向了哪些节点,因为使用邻接矩阵的表达方式 所以把所有节点遍历一遍。 + +而在邻接表中,我们可以以相对高效的方式知道一个节点链接指向哪些节点。 + +再回顾一下邻接表的构造(数组 + 链表): + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) + +假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) + +所以在邻接表中,我们要获取 节点cur 链接指向哪些节点,就是遍历 grid[cur节点编号] 这个链表。 + +这个遍历方式,C++代码如下: + +```CPP +for (Edge edge : grid[cur.first]) +``` + +(如果不知道 Edge 是什么,看上面「图的存储」中邻接表的讲解) + +`cur.first` 就是cur节点编号, 参考上面pair的定义: pair<节点编号,源点到该节点的权值> + +接下来就是更新 非访问节点到源点的距离,代码实现和 朴素dijkstra 是一样的,代码如下: + +```CPP +// 3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) +for (Edge edge : grid[cur.first]) { // 遍历 cur指向的节点,cur指向的节点为 edge + // cur指向的节点edge.to,这条边的权值为 edge.val + if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist + minDist[edge.to] = minDist[cur.first] + edge.val; + pq.push(pair(edge.to, minDist[edge.to])); + } +} +``` + +但为什么思路一样,有的录友能写出朴素dijkstra,但堆优化这里的逻辑就是写不出来呢? + +**主要就是因为对邻接表的表达方式不熟悉**! + +以上代码中,cur 链接指向的节点编号 为 edge.to, 这条边的权值为 edge.val ,如果对这里模糊的就再回顾一下 Edge的定义: + +```CPP +struct Edge { + int to; // 邻接顶点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; +``` + +确定该节点没有被访问过,`!visited[edge.to]` , 目前 源点到cur.first的最短距离(minDist) + cur.first 到 edge.to 的距离 (edge.val) 是否 小于 minDist已经记录的 源点到 edge.to 的距离 (minDist[edge.to]) + +如果是的话,就开始更新操作。 + +即: + +```CPP +if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist + minDist[edge.to] = minDist[cur.first] + edge.val; + pq.push(pair(edge.to, minDist[edge.to])); // 由于cur节点的加入,而新链接的边,加入到优先级队里中 +} + +``` + +同时,由于cur节点的加入,源点又有可以新链接到的边,将这些边加入到优先级队里中。 + + +以上代码思路 和 朴素版dijkstra 是一样一样的,主要区别是两点: + +* 邻接表的表示方式不同 +* 使用优先级队列(小顶堆)来对新链接的边排序 + +### 代码实现 + +堆优化dijkstra完整代码如下: + +```CPP +class Solution { +public: + // 小顶堆 + class mycomparison { + public: + bool operator()(const pair& lhs, const pair& rhs) { + return lhs.second > rhs.second; + } + }; + // 定义一个结构体来表示带权重的边 + struct Edge { + int to; // 邻接顶点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 + }; + int networkDelayTime(vector>& times, int n, int k) { + + + std::vector> grid(n + 1); + for(int i = 0; i < times.size(); i++){ + int p1 = times[i][0]; + int p2 = times[i][1]; + // p1 指向 p2,权值为 times[i][2] + grid[p1].push_back(Edge(p2, times[i][2])); + } + + // 存储从源点到每个节点的最短距离 + std::vector minDist(n + 1, INT_MAX); + + // 记录顶点是否被访问过 + std::vector visited(n + 1, false); + + // 优先队列中存放 pair<节点,源点到该节点的距离> + priority_queue, vector>, mycomparison> pq; + + pq.push(pair(k, 0)); + minDist[k] = 0; // 这个不要忘了 + + while (!pq.empty()) { + // <节点, 源点到该节点的距离> + // 1. 第一步,选源点到哪个节点近且该节点未被访问过 (通过优先级队列来实现) + pair cur = pq.top(); pq.pop(); + + if (visited[cur.first]) continue; + + // 2. 第二步,该最近节点被标记访问过 + visited[cur.first] = true; + + + // 3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (Edge edge : grid[cur.first]) { // 遍历 cur指向的节点,cur指向的节点为 edge + // cur指向的节点edge.to,这条边的权值为 edge.val + if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist + minDist[edge.to] = minDist[cur.first] + edge.val; + pq.push(pair(edge.to, minDist[edge.to])); + } + } + + } + + // 源点到最远的节点的时间,也就是寻找 源点到所有节点最短路径的最大值 + int result = 0; + for (int i = 1; i <= n; i++) { + if (minDist[i] == INT_MAX) return -1;// 没有路径 + result = max(minDist[i], result); + } + return result; + + } +}; + +``` + +* 时间复杂度:O(ElogE) E 为边的数量 +* 空间复杂度:O(N + E) N 为节点的数量 + +堆优化的时间复杂度 只和边的数量有关 和节点数无关,在 优先级队列中 放的也是边。 + +以上代码中,`while (!pq.empty())` 里套了 `for (Edge edge : grid[cur.first])` + +`for` 里 遍历的是 当前节点 cur 所连接边。 + +那 当前节点cur 所连接的边 也是不固定的, 这就让大家分不清,这时间复杂度究竟是多少? + +其实 `for (Edge edge : grid[cur.first])` 里最终的数据走向 是 给队列里添加边。 + +那么跳出局部代码,整个队列 一定是 所有边添加了一次,同时也弹出了一次。 + +所以边添加一次时间复杂度是 O(E), `while (!pq.empty())` 里每次都要弹出一个边来进行操作,在优先级队列(小顶堆)中 弹出一个元素的时间复杂度是 O(logE) ,这是堆排序的时间复杂度。 + +(当然小顶堆里 是 添加元素的时候 排序,还是 取数元素的时候排序,这个无所谓,时间复杂度都是O(E),总是是一定要排序的,而小顶堆里也不会滞留元素,有多少元素添加 一定就有多少元素弹出) + +所以 该算法整体时间复杂度为 O(ElogE) + +网上的不少分析 会把 n (节点的数量)算进来,这个分析是有问题的,举一个极端例子,在n 为 10000,且是有一条边的 图里,以上代码,大家感觉执行了多少次? + +`while (!pq.empty())` 中的 pq 存的是边,其实只执行了一次。 + +所以该算法时间复杂度 和 节点没有关系。 + +至于空间复杂度,邻接表是 数组 + 链表 数组的空间 是 N ,有E条边 就申请对应多少个链表节点,所以是 复杂度是 N + E + +## 拓展 + +当然也有录友可能想 堆优化dijkstra 中 我为什么一定要用邻接表呢,我就用邻接矩阵 行不行 ? + +也行的。 + +但 正是因为稀疏图,所以我们使用堆优化的思路, 如果我们还用 邻接矩阵 去表达这个图的话,就是 一个高效的算法 使用了低效的数据结构,那么 整体算法效率 依然是低的。 + +如果还不清楚为什么要使用 邻接表,可以再看看上面 我在 「图的存储」标题下的讲解。 + +这里我也给出 邻接矩阵版本的堆优化dijkstra代码: + +```CPP +class Solution { +public: + // 小顶堆(按照中的v 来从小到大排序) + class mycomparison { + public: + bool operator()(const pair& lhs, const pair& rhs) { + return lhs.second > rhs.second; + } + }; + int networkDelayTime(vector>& times, int n, int k) { + + // 注意题目中给的二维数组并不是邻接矩阵 + // 需要邻接矩阵来存图 + // 因为本题处理方式是节点标号从1开始,所以数组的大小都是 n+1 + vector> grid(n + 1, vector(n + 1, INT_MAX)); + for(int i = 0; i < times.size(); i++){ + int p1 = times[i][0]; + int p2 = times[i][1]; + grid[p1][p2] = times[i][2]; + } + + // 存储从源点到每个节点的最短距离 + std::vector minDist(n + 1, INT_MAX); + + // 记录顶点是否被访问过 + std::vector visited(n + 1, false); + + // 优先队列中存放 [节点,源点到该节点的距离] + priority_queue, vector>, mycomparison> pq; + + pq.push(pair(k, 0)); + minDist[k] = 0; // 这个不要忘了 + + while (!pq.empty()) { + // <节点, 源点到该节点的距离> + // 1、选距离源点最近且未访问过的节点 + pair cur = pq.top(); pq.pop(); + + if (visited[cur.first]) continue; + + // 2、标记该节点已被访问 + visited[cur.first] = true; + + // 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) + // 遍历 cur 可以链接的节点,更新 minDist[j] + for (int j = 1; j <= n; j++) { + if (!visited[j] && grid[cur.first][j] != INT_MAX && (minDist[cur.first] + grid[cur.first][j] < minDist[j])) { + minDist[j] = minDist[cur.first] + grid[cur.first][j]; + pq.push(pair(j, minDist[j])); + } + } + } + + // 源点到最远的节点的时间,也就是寻找 源点到所有节点最短路径的最大值 + int result = 0; + for (int i = 1; i <= n; i++) { + if (minDist[i] == INT_MAX) return -1;// 没有路径 + result = max(minDist[i], result); + } + + + return result; + + } +}; + +``` + +* 时间复杂度:O(E * (N + logE)) E为边的数量,N为节点数量 +* 空间复杂度:O(log(N^2)) + +`while (!pq.empty())` 时间复杂度为 E ,while 里面 每次取元素 时间复杂度 为 logE,和 一个for循环 时间复杂度 为 N 。 + +所以整体是 E * (N + logE) + + +## 总结 + +在学习一种优化思路的时候,首先就要知道为什么要优化,遇到了什么问题。 + +正如我在开篇就给大家交代清楚 堆优化方式的背景。 + +堆优化的整体思路和 朴素版是大体一样的,区别是 堆优化从边的角度触发,且利用堆来排序。 + +很多录友别说写堆优化 就是看 堆优化的代码也看的很懵。 + +主要是因为两点: + +* 不熟悉邻接表的表达方式 +* 对dijkstra的实现思路还是不熟 + +这是我为什么 本篇花了大力气来讲解 图的存储,就是为了让大家彻底理解邻接表以及邻接表的代码写法。 + +至于 dijkstra的实现思路 ,朴素版 和 堆优化版本 都是 按照 dijkstra 三部曲来的。 + +理解了三部曲,dijkstra 的思路就是清晰的。 + +针对邻接表版本代码 我做了详细的 时间复杂度分析,也让录友们清楚,相对于 朴素版,时间都优化到哪了。 + +最后 我也给出了 邻接矩阵的版本代码,分析了这一版本的必要性以及时间复杂度。 + +至此通过 两篇dijkstra的文章,终于把 dijkstra 讲完了,如果大家对我讲解里所涉及的内容都吃透的话,详细对 dijkstra 算法也就理解到位了。 + + +# Bellman_ford + +```CPP +class Solution { +public: + + int networkDelayTime(vector>& times, int n, int k) { + vector minDist(n + 1 , INT_MAX/2); + minDist[k] = 0; + vector minDist_copy(n); // 用来记录每一次遍历的结果 + for (int i = 1; i <= n + 1; i++) { + minDist_copy = minDist; // 获取上一次计算的结果 + for (auto &f : times) { + int from = f[0]; + int to = f[1]; + int price = f[2]; + if (minDist[to] > minDist_copy[from] + price) minDist[to] = minDist_copy[from] + price; + } + + } + int result = 0; + for (int i = 1;i <= n; i++) { + if (minDist[i] == INT_MAX/2) return -1;// 没有路径 + result = max(minDist[i], result); + } + + return result; + + } +}; + +``` + diff --git "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" new file mode 100644 index 0000000000..c7b6236b78 --- /dev/null +++ "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" @@ -0,0 +1,44 @@ + + +```CPP +class Solution { +public: + int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { + vector minDist(n , INT_MAX/2); + minDist[src] = 0; + vector minDist_copy(n); // 用来记录每一次遍历的结果 + for (int i = 1; i <= k + 1; i++) { + minDist_copy = minDist; // 获取上一次计算的结果 + for (auto &f : flights) { + int from = f[0]; + int to = f[1]; + int price = f[2]; + if (minDist[to] > minDist_copy[from] + price) minDist[to] = minDist_copy[from] + price; + } + + } + int result = minDist[dst] == INT_MAX/2 ? -1 : minDist[dst]; + return result; + } +}; +``` + +```CPP +class Solution { +public: + int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { + vector minDist(n , INT_MAX/2); + minDist[src] = 0; + for (int i = 1; i <= k + 1; i++) { + for (auto &f : flights) { + int from = f[0]; + int to = f[1]; + int price = f[2]; + if (minDist[to] > minDist[from] + price) minDist[to] = minDist[from] + price; + } + } + int result = minDist[dst] == INT_MAX/2 ? -1 : minDist[dst]; + return result; + } +}; +``` diff --git "a/problems/kama47.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kama47.\345\217\202\344\274\232dijkstra\345\240\206.md" new file mode 100644 index 0000000000..0cb5930182 --- /dev/null +++ "b/problems/kama47.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -0,0 +1,651 @@ + +# dijkstra(堆优化版)精讲 + +[题目链接](https://kamacoder.com/problempage.php?pid=1047) + +【题目描述】 + +小明是一位科学家,他需要参加一场重要的国际科学大会,以展示自己的最新研究成果。 + +小明的起点是第一个车站,终点是最后一个车站。然而,途中的各个车站之间的道路状况、交通拥堵程度以及可能的自然因素(如天气变化)等不同,这些因素都会影响每条路径的通行时间。 + +小明希望能选择一条花费时间最少的路线,以确保他能够尽快到达目的地。 + +【输入描述】 + +第一行包含两个正整数,第一个正整数 N 表示一共有 N 个公共汽车站,第二个正整数 M 表示有 M 条公路。 + +接下来为 M 行,每行包括三个整数,S、E 和 V,代表了从 S 车站可以单向直达 E 车站,并且需要花费 V 单位的时间。 + +【输出描述】 + +输出一个整数,代表小明在途中和其他科学家和科研团队交流所花费的最少时间。 + +输入示例 + +``` +7 9 +1 2 1 +1 3 4 +2 3 2 +2 4 5 +3 4 2 +4 5 3 +2 6 4 +5 7 4 +6 7 9 +``` + +输出示例:12 + +【提示信息】 + +能够到达的情况: + +如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101345.png) + +不能到达的情况: + +如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101401.png) + +数据范围: + +1 <= N <= 500; +1 <= M <= 5000; + + +## 思路 + +> 本篇我们来讲解 堆优化版dijkstra,看本篇之前,一定要先看 我讲解的 朴素版dijkstra,否则本篇会有部分内容看不懂。 + +在上一篇中,我们讲解了朴素版的dijkstra,该解法的时间复杂度为 O(n^2),可以看出时间复杂度 只和 n (节点数量)有关系。 + +如果n很大的话,我们可以换一个角度来优先性能。 + +在 讲解 最小生成树的时候,我们 讲了两个算法,[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w)(从点的角度来求最小生成树)、[Kruskal算法](https://mp.weixin.qq.com/s/rUVaBjCES_4eSjngceT5bw)(从边的角度来求最小生成树) + +这么在n 很大的时候,也有另一个思考维度,即:从边的数量出发。 + +当 n 很大,边 的数量 也很多的时候(稠密图),那么 上述解法没问题。 + +但 n 很大,边 的数量 很小的时候(稀疏图),是不是可以换成从边的角度来求最短路呢? + +毕竟边的数量少。 + +有的录友可能会想,n (节点数量)很大,边不就多吗? 怎么会边的数量少呢? + +别忘了,谁也没有规定 节点之间一定要有边连接着,例如有一万个节点,只有一条边,这也是一张图。 + +了解背景之后,再来看 解法思路。 + +### 图的存储 + +首先是 图的存储。 + +关于图的存储 主流有两种方式: 邻接矩阵和邻接表 + +#### 邻接矩阵 + +邻接矩阵 使用 二维数组来表示图结构。 邻接矩阵是从节点的角度来表示图,有多少节点就申请多大的二维数组。 + +例如: grid[2][5] = 6,表示 节点 2 链接 节点5 为有向图,节点2 指向 节点5,边的权值为6 (套在题意里,可能是距离为6 或者 消耗为6 等等) + +如果想表示无向图,即:grid[2][5] = 6,grid[5][2] = 6,表示节点2 与 节点5 相互连通,权值为6。 + + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) + +在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 + +这种表达方式(邻接矩阵) 在 边少,节点多的情况下,会导致申请过大的二维数组,造成空间浪费。 + +而且在寻找节点链接情况的时候,需要遍历整个矩阵,即 n * n 的时间复杂度,同样造成时间浪费。 + +邻接矩阵的优点: + +* 表达方式简单,易于理解 +* 检查任意两个顶点间是否存在边的操作非常快 +* 适合稠密图,在边数接近顶点数平方的图中,邻接矩阵是一种空间效率较高的表示方法。 + +缺点: + +* 遇到稀疏图,会导致申请过大的二维数组造成空间浪费 且遍历 边 的时候需要遍历整个n * n矩阵,造成时间浪费 + +#### 邻接表 + +邻接表 使用 数组 + 链表的方式来表示。 邻接表是从边的数量来表示图,有多少边 才会申请对应大小的链表。 + +邻接表的构造如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) + +这里表达的图是: + +* 节点1 指向 节点3 和 节点5 +* 节点2 指向 节点4、节点3、节点5 +* 节点3 指向 节点4,节点4指向节点1。 + +有多少边 邻接表才会申请多少个对应的链表节点。 + +从图中可以直观看出 使用 数组 + 链表 来表达 边的链接情况 。 + +邻接表的优点: + +* 对于稀疏图的存储,只需要存储边,空间利用率高 +* 遍历节点链接情况相对容易 + +缺点: + +* 检查任意两个节点间是否存在边,效率相对低,需要 O(V)时间,V表示某节点链接其他节点的数量。 +* 实现相对复杂,不易理解 + +#### 本题图的存储 + +接下来我们继续按照稀疏图的角度来分析本题。 + +在第一个版本的实现思路中,我们提到了三部曲: + +1. 第一步,选源点到哪个节点近且该节点未被访问过 +2. 第二步,该最近节点被标记访问过 +3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + +在第一个版本的代码中,这三部曲是套在一个 for 循环里,为什么? + +因为我们是从节点的角度来解决问题。 + +三部曲中第一步(选源点到哪个节点近且该节点未被访问过),这个操作本身需要for循环遍历 minDist 来寻找最近的节点。 + +同时我们需要 遍历所有 未访问过的节点,所以 我们从 节点角度出发,代码会有两层for循环,代码是这样的: (注意代码中的注释,标记两层for循环的用处) + +```CPP + +for (int i = 1; i <= n; i++) { // 遍历所有节点,第一层for循环 + + int minVal = INT_MAX; + int cur = 1; + + // 1、选距离源点最近且未访问过的节点 , 第二层for循环 + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] < minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; // 2、标记该节点已被访问 + + // 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } + } + +} +``` + +那么当从 边 的角度出发, 在处理 三部曲里的第一步(选源点到哪个节点近且该节点未被访问过)的时候 ,我们可以不用去遍历所有节点了。 + +而且 直接把 边(带权值)加入到 小顶堆(利用堆来自动排序),那么每次我们从 堆顶里 取出 边 自然就是 距离源点最近的节点所在的边。 + +这样我们就不需要两层for循环来寻找最近的节点了。 + +了解了大体思路,我们再来看代码实现。 + +首先是 如何使用 邻接表来表述图结构,这是摆在很多录友面前的第一个难题。 + +邻接表用 数组+链表 来表示,代码如下:(C++中 vector 为数组,list 为链表, 定义了 n+1 这么大的数组空间) + +```CPP +vector> grid(n + 1); +``` + +不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) + +图中邻接表表示: + +* 节点1 指向 节点3 和 节点5 +* 节点2 指向 节点4、节点3、节点5 +* 节点3 指向 节点4 +* 节点4 指向 节点1 + +大家发现图中的边没有权值,而本题中 我们的边是有权值的,权值怎么表示?在哪里表示? + +所以 在`vector> grid(n + 1);` 中 就不能使用int了,而是需要一个键值对 来存两个数字,一个数表示节点,一个数表示 指向该节点的这条边的权值。 + +那么 代码可以改成这样: (pair 为键值对,可以存放两个int) + +```CPP +vector>> grid(n + 1); +``` + +举例来给大家展示 该代码表达的数据 如下: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103904.png) + +* 节点1 指向 节点3 权值为 1 +* 节点1 指向 节点5 权值为 2 +* 节点2 指向 节点4 权值为 7 +* 节点2 指向 节点3 权值为 6 +* 节点2 指向 节点5 权值为 3 +* 节点3 指向 节点4 权值为 3 +* 节点5 指向 节点1 权值为 10 + +这样 我们就把图中权值表示出来了。 + +但是在代码中 使用 `pair` 很容易让我们搞混了,第一个int 表示什么,第二个int表示什么,导致代码可读性很差,或者说别人看你的代码看不懂。 + +那么 可以 定一个类 来取代 `pair` + +类(或者说是结构体)定义如下: + +```CPP +struct Edge { + int to; // 邻接顶点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; +``` + +这个类里有两个成员变量,有对应的命名,这样不容易搞混 两个int的含义。 + +所以 本题中邻接表的定义如下: + +```CPP +struct Edge { + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + +vector> grid(n + 1); // 邻接表 + +``` + +(我们在下面的讲解中会直接使用这个邻接表的代码表示方式) + +### 堆优化细节 + +其实思路依然是 dijkstra 三部曲: + +1. 第一步,选源点到哪个节点近且该节点未被访问过 +2. 第二步,该最近节点被标记访问过 +3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + +只不过之前是 通过遍历节点来遍历边,通过两层for循环来寻找距离源点最近节点。 这次我们直接遍历边,且通过堆来对边进行排序,达到直接选择距离源点最近节点。 + +先来看一下针对这三部曲,如果用 堆来优化。 + +那么三部曲中的第一步(选源点到哪个节点近且该节点未被访问过),我们如何选? + +我们要选择距离源点近的节点(即:该边的权值最小),所以 我们需要一个 小顶堆 来帮我们对边的权值排序,每次从小顶堆堆顶 取边就是权值最小的边。 + +C++定义小顶堆,可以用优先级队列实现,代码如下: + +```CPP +// 小顶堆 +class mycomparison { +public: + bool operator()(const pair& lhs, const pair& rhs) { + return lhs.second > rhs.second; + } +}; +// 优先队列中存放 pair<节点编号,源点到该节点的权值> +priority_queue, vector>, mycomparison> pq; +``` + +(`pair`中 第二个int 为什么要存 源点到该节点的权值,因为 这个小顶堆需要按照权值来排序) + + +有了小顶堆自动对边的权值排序,那我们只需要直接从 堆里取堆顶元素(小顶堆中,最小的权值在上面),就可以取到离源点最近的节点了 (未访问过的节点,不会加到堆里进行排序) + +所以三部曲中的第一步,我们不用 for循环去遍历,直接取堆顶元素: + +```CPP +// pair<节点编号,源点到该节点的权值> +pair cur = pq.top(); pq.pop(); + +``` + +第二步(该最近节点被标记访问过) 这个就是将 节点做访问标记,和 朴素dijkstra 一样 ,代码如下: + +```CPP +// 2. 第二步,该最近节点被标记访问过 +visited[cur.first] = true; + +``` + +(`cur.first` 是指取 `pair` 里的第一个int,即节点编号 ) + +第三步(更新非访问节点到源点的距离),这里的思路 也是 和朴素dijkstra一样的。 + +但很多录友对这里是最懵的,主要是因为两点: + +* 没有理解透彻 dijkstra 的思路 +* 没有理解 邻接表的表达方式 + +我们来回顾一下 朴素dijkstra 在这一步的代码和思路(如果没看过我讲解的朴素版dijkstra,这里会看不懂) + +```CPP + +// 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) +for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } +} +``` + +其中 for循环是用来做什么的? 是为了 找到 节点cur 链接指向了哪些节点,因为使用邻接矩阵的表达方式 所以把所有节点遍历一遍。 + +而在邻接表中,我们可以以相对高效的方式知道一个节点链接指向哪些节点。 + +再回顾一下邻接表的构造(数组 + 链表): + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) + +假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) + +所以在邻接表中,我们要获取 节点cur 链接指向哪些节点,就是遍历 grid[cur节点编号] 这个链表。 + +这个遍历方式,C++代码如下: + +```CPP +for (Edge edge : grid[cur.first]) +``` + +(如果不知道 Edge 是什么,看上面「图的存储」中邻接表的讲解) + +`cur.first` 就是cur节点编号, 参考上面pair的定义: pair<节点编号,源点到该节点的权值> + +接下来就是更新 非访问节点到源点的距离,代码实现和 朴素dijkstra 是一样的,代码如下: + +```CPP +// 3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) +for (Edge edge : grid[cur.first]) { // 遍历 cur指向的节点,cur指向的节点为 edge + // cur指向的节点edge.to,这条边的权值为 edge.val + if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist + minDist[edge.to] = minDist[cur.first] + edge.val; + pq.push(pair(edge.to, minDist[edge.to])); + } +} +``` + +但为什么思路一样,有的录友能写出朴素dijkstra,但堆优化这里的逻辑就是写不出来呢? + +**主要就是因为对邻接表的表达方式不熟悉**! + +以上代码中,cur 链接指向的节点编号 为 edge.to, 这条边的权值为 edge.val ,如果对这里模糊的就再回顾一下 Edge的定义: + +```CPP +struct Edge { + int to; // 邻接顶点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; +``` + +确定该节点没有被访问过,`!visited[edge.to]` , 目前 源点到cur.first的最短距离(minDist) + cur.first 到 edge.to 的距离 (edge.val) 是否 小于 minDist已经记录的 源点到 edge.to 的距离 (minDist[edge.to]) + +如果是的话,就开始更新操作。 + +即: + +```CPP +if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist + minDist[edge.to] = minDist[cur.first] + edge.val; + pq.push(pair(edge.to, minDist[edge.to])); // 由于cur节点的加入,而新链接的边,加入到优先级队里中 +} + +``` + +同时,由于cur节点的加入,源点又有可以新链接到的边,将这些边加入到优先级队里中。 + + +以上代码思路 和 朴素版dijkstra 是一样一样的,主要区别是两点: + +* 邻接表的表示方式不同 +* 使用优先级队列(小顶堆)来对新链接的边排序 + +### 代码实现 + +堆优化dijkstra完整代码如下: + +```CPP +#include +#include +#include +#include +#include +using namespace std; +// 小顶堆 +class mycomparison { +public: + bool operator()(const pair& lhs, const pair& rhs) { + return lhs.second > rhs.second; + } +}; +// 定义一个结构体来表示带权重的边 +struct Edge { + int to; // 邻接顶点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1); + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid[p1].push_back(Edge(p2, val)); + + } + + int start = 1; // 起点 + int end = n; // 终点 + + // 存储从源点到每个节点的最短距离 + std::vector minDist(n + 1, INT_MAX); + + // 记录顶点是否被访问过 + std::vector visited(n + 1, false); + + // 优先队列中存放 pair<节点,源点到该节点的权值> + priority_queue, vector>, mycomparison> pq; + + + // 初始化队列,源点到源点的距离为0,所以初始为0 + pq.push(pair(start, 0)); + + minDist[start] = 0; // 起始点到自身的距离为0 + + while (!pq.empty()) { + // 1. 第一步,选源点到哪个节点近且该节点未被访问过 (通过优先级队列来实现) + // <节点, 源点到该节点的距离> + pair cur = pq.top(); pq.pop(); + + if (visited[cur.first]) continue; + + // 2. 第二步,该最近节点被标记访问过 + visited[cur.first] = true; + + // 3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (Edge edge : grid[cur.first]) { // 遍历 cur指向的节点,cur指向的节点为 edge + // cur指向的节点edge.to,这条边的权值为 edge.val + if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist + minDist[edge.to] = minDist[cur.first] + edge.val; + pq.push(pair(edge.to, minDist[edge.to])); + } + } + + } + + if (minDist[end] == INT_MAX) cout << -1 << endl; // 不能到达终点 + else cout << minDist[end] << endl; // 到达终点最短路径 +} + +``` + +* 时间复杂度:O(ElogE) E 为边的数量 +* 空间复杂度:O(N + E) N 为节点的数量 + +堆优化的时间复杂度 只和边的数量有关 和节点数无关,在 优先级队列中 放的也是边。 + +以上代码中,`while (!pq.empty())` 里套了 `for (Edge edge : grid[cur.first])` + +`for` 里 遍历的是 当前节点 cur 所连接边。 + +那 当前节点cur 所连接的边 也是不固定的, 这就让大家分不清,这时间复杂度究竟是多少? + +其实 `for (Edge edge : grid[cur.first])` 里最终的数据走向 是 给队列里添加边。 + +那么跳出局部代码,整个队列 一定是 所有边添加了一次,同时也弹出了一次。 + +所以边添加一次时间复杂度是 O(E), `while (!pq.empty())` 里每次都要弹出一个边来进行操作,在优先级队列(小顶堆)中 弹出一个元素的时间复杂度是 O(logE) ,这是堆排序的时间复杂度。 + +(当然小顶堆里 是 添加元素的时候 排序,还是 取数元素的时候排序,这个无所谓,时间复杂度都是O(E),总是是一定要排序的,而小顶堆里也不会滞留元素,有多少元素添加 一定就有多少元素弹出) + +所以 该算法整体时间复杂度为 O(ElogE) + +网上的不少分析 会把 n (节点的数量)算进来,这个分析是有问题的,举一个极端例子,在n 为 10000,且是有一条边的 图里,以上代码,大家感觉执行了多少次? + +`while (!pq.empty())` 中的 pq 存的是边,其实只执行了一次。 + +所以该算法时间复杂度 和 节点没有关系。 + +至于空间复杂度,邻接表是 数组 + 链表 数组的空间 是 N ,有E条边 就申请对应多少个链表节点,所以是 复杂度是 N + E + +## 拓展 + +当然也有录友可能想 堆优化dijkstra 中 我为什么一定要用邻接表呢,我就用邻接矩阵 行不行 ? + +也行的。 + +但 正是因为稀疏图,所以我们使用堆优化的思路, 如果我们还用 邻接矩阵 去表达这个图的话,就是 一个高效的算法 使用了低效的数据结构,那么 整体算法效率 依然是低的。 + +如果还不清楚为什么要使用 邻接表,可以再看看上面 我在 「图的存储」标题下的讲解。 + +这里我也给出 邻接矩阵版本的堆优化dijkstra代码: + +```CPP +#include +#include +#include +#include +using namespace std; +// 小顶堆 +class mycomparison { +public: + bool operator()(const pair& lhs, const pair& rhs) { + return lhs.second > rhs.second; + } +}; + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1, vector(n + 1, INT_MAX)); + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid[p1][p2] = val; + } + + int start = 1; // 起点 + int end = n; // 终点 + + // 存储从源点到每个节点的最短距离 + std::vector minDist(n + 1, INT_MAX); + + // 记录顶点是否被访问过 + std::vector visited(n + 1, false); + + // 优先队列中存放 pair<节点,源点到该节点的距离> + priority_queue, vector>, mycomparison> pq; + + + // 初始化队列,源点到源点的距离为0,所以初始为0 + pq.push(pair(start, 0)); + + minDist[start] = 0; // 起始点到自身的距离为0 + + while (!pq.empty()) { + // <节点, 源点到该节点的距离> + // 1、选距离源点最近且未访问过的节点 + pair cur = pq.top(); pq.pop(); + + if (visited[cur.first]) continue; + + visited[cur.first] = true; // 2、标记该节点已被访问 + + // 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (int j = 1; j <= n; j++) { + if (!visited[j] && grid[cur.first][j] != INT_MAX && (minDist[cur.first] + grid[cur.first][j] < minDist[j])) { + minDist[j] = minDist[cur.first] + grid[cur.first][j]; + pq.push(pair(j, minDist[j])); + } + } + } + + if (minDist[end] == INT_MAX) cout << -1 << endl; // 不能到达终点 + else cout << minDist[end] << endl; // 到达终点最短路径 + +} + +``` + +* 时间复杂度:O(E * (N + logE)) E为边的数量,N为节点数量 +* 空间复杂度:O(log(N^2)) + +`while (!pq.empty())` 时间复杂度为 E ,while 里面 每次取元素 时间复杂度 为 logE,和 一个for循环 时间复杂度 为 N 。 + +所以整体是 E * (N + logE) + + +## 总结 + +在学习一种优化思路的时候,首先就要知道为什么要优化,遇到了什么问题。 + +正如我在开篇就给大家交代清楚 堆优化方式的背景。 + +堆优化的整体思路和 朴素版是大体一样的,区别是 堆优化从边的角度触发,且利用堆来排序。 + +很多录友别说写堆优化 就是看 堆优化的代码也看的很懵。 + +主要是因为两点: + +* 不熟悉邻接表的表达方式 +* 对dijkstra的实现思路还是不熟 + +这是我为什么 本篇花了大力气来讲解 图的存储,就是为了让大家彻底理解邻接表以及邻接表的代码写法。 + +至于 dijkstra的实现思路 ,朴素版 和 堆优化版本 都是 按照 dijkstra 三部曲来的。 + +理解了三部曲,dijkstra 的思路就是清晰的。 + +针对邻接表版本代码 我做了详细的 时间复杂度分析,也让录友们清楚,相对于 朴素版,时间都优化到哪了。 + +最后 我也给出了 邻接矩阵的版本代码,分析了这一版本的必要性以及时间复杂度。 + +至此通过 两篇dijkstra的文章,终于把 dijkstra 讲完了,如果大家对我讲解里所涉及的内容都吃透的话,详细对 dijkstra 算法也就理解到位了。 + + + diff --git "a/problems/kama47.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kama47.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" new file mode 100644 index 0000000000..957d471afd --- /dev/null +++ "b/problems/kama47.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -0,0 +1,733 @@ + +# dijkstra(朴素版)精讲 + +[题目链接](https://kamacoder.com/problempage.php?pid=1047) + +【题目描述】 + +小明是一位科学家,他需要参加一场重要的国际科学大会,以展示自己的最新研究成果。 + +小明的起点是第一个车站,终点是最后一个车站。然而,途中的各个车站之间的道路状况、交通拥堵程度以及可能的自然因素(如天气变化)等不同,这些因素都会影响每条路径的通行时间。 + +小明希望能选择一条花费时间最少的路线,以确保他能够尽快到达目的地。 + +【输入描述】 + +第一行包含两个正整数,第一个正整数 N 表示一共有 N 个公共汽车站,第二个正整数 M 表示有 M 条公路。 + +接下来为 M 行,每行包括三个整数,S、E 和 V,代表了从 S 车站可以单向直达 E 车站,并且需要花费 V 单位的时间。 + +【输出描述】 + +输出一个整数,代表小明在途中和其他科学家和科研团队交流所花费的最少时间。 + +输入示例 + +``` +7 9 +1 2 1 +1 3 4 +2 3 2 +2 4 5 +3 4 2 +4 5 3 +2 6 4 +5 7 4 +6 7 9 +``` + +输出示例:12 + +【提示信息】 + +能够到达的情况: + +如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101345.png) + +不能到达的情况: + +如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101401.png) + +数据范围: + +1 <= N <= 500; +1 <= M <= 5000; + +## 思路 + +本题就是求最短路,最短路是图论中的经典问题即:给出一个有向图,一个起点,一个终点,问起点到终点的最短路径。 + +接下来,我们来详细讲解最短路算法中的 dijkstra 算法。 + +dijkstra算法:在有权图(权值非负数)中求从起点到其他节点的最短路径算法。 + +需要注意两点: + +* dijkstra 算法可以同时求 起点到所有节点的最短路径 +* 权值不能为负数 + +(这两点后面我们会讲到) + +如本题示例中的图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240125162647.png) + +起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 + +最短路径的权值为12。 + +其实 dijkstra 算法 和 我们之前讲解的prim算法思路非常接近,如果大家认真学过[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w),那么理解 Dijkstra 算法会相对容易很多。(这也是我要先讲prim再讲dijkstra的原因) + +dijkstra 算法 同样是贪心的思路,不断寻找距离 源点最近的没有访问过的节点。 + +这里我也给出 **dijkstra三部曲**: + +1. 第一步,选源点到哪个节点近且该节点未被访问过 +2. 第二步,该最近节点被标记访问过 +3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + +大家此时已经会发现,这和prim算法 怎么这么像呢。 + +我在[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w)讲解中也给出了三部曲。 prim 和 dijkstra 确实很像,思路也是类似的,这一点我在后面还会详细来讲。 + +在dijkstra算法中,同样有一个数组很重要,起名为:minDist。 + +**minDist数组 用来记录 每一个节点距离源点的最小距离**。 + +理解这一点很重要,也是理解 dijkstra 算法的核心所在。 + +大家现在看着可能有点懵,不知道什么意思。 + +没关系,先让大家有一个印象,对理解后面讲解有帮助。 + +我们先来画图看一下 dijkstra 的工作过程,以本题示例为例: (以下为朴素版dijkstra的思路) + +(**示例中节点编号是从1开始,所以为了让大家看的不晕,minDist数组下标我也从 1 开始计数,下标0 就不使用了,这样 下标和节点标号就可以对应上了,避免大家搞混**) + +## 朴素版dijkstra + +### 模拟过程 + +----------- + +0、初始化 + +minDist数组数值初始化为int最大值。 + +这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) + +(图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) + +源点(节点1) 到自己的距离为0,所以 minDist[1] = 0 + +此时所有节点都没有被访问过,所以 visited数组都为0 + +--------------- + +以下为dijkstra 三部曲 + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离源点最近,距离为0,且未被访问。 + +2、该最近节点被标记访问过 + +标记源点访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) + + +更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 + +* 源点到节点2的最短距离为1,小于原minDist[2]的数值max,更新minDist[2] = 1 +* 源点到节点3的最短距离为4,小于原minDist[3]的数值max,更新minDist[4] = 4 + +可能有录友问:为啥和 minDist[2] 比较? + +再强调一下 minDist[2] 的含义,它表示源点到节点2的最短距离,那么目前我们得到了 源点到节点2的最短距离为1,小于默认值max,所以更新。 minDist[3]的更新同理 + + +------------- + +1、选源点到哪个节点近且该节点未被访问过 + +未访问过的节点中,源点到节点2距离最近,选节点2 + +2、该最近节点被标记访问过 + +节点2被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) + +更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 + +**为什么更新这些节点呢? 怎么不更新其他节点呢**? + +因为 源点(节点1)通过 已经计算过的节点(节点2) 可以链接到的节点 有 节点3,节点4和节点6. + + +更新 minDist数组: + +* 源点到节点6的最短距离为5,小于原minDist[6]的数值max,更新minDist[6] = 5 +* 源点到节点3的最短距离为3,小于原minDist[3]的数值4,更新minDist[3] = 3 +* 源点到节点4的最短距离为6,小于原minDist[4]的数值max,更新minDist[4] = 6 + + + +------------------- + +1、选源点到哪个节点近且该节点未被访问过 + +未访问过的节点中,源点距离哪些节点最近,怎么算的? + +其实就是看 minDist数组里的数值,minDist 记录了 源点到所有节点的最近距离,结合visited数组筛选出未访问的节点就好。 + +从 上面的图,或者 从minDist数组中,我们都能看出 未访问过的节点中,源点(节点1)到节点3距离最近。 + + +2、该最近节点被标记访问过 + +节点3被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) + +由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: + +更新 minDist数组: + +* 源点到节点4的最短距离为5,小于原minDist[4]的数值6,更新minDist[4] = 5 + +------------------ + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,有节点4 和 节点6,距离源点距离都是 5 (minDist[4] = 5,minDist[6] = 5) ,选哪个节点都可以。 + +2、该最近节点被标记访问过 + +节点4被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) + +由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: + +* 源点到节点5的最短距离为8,小于原minDist[5]的数值max,更新minDist[5] = 8 + +-------------- + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,是节点6,距离源点距离是 5 (minDist[6] = 5) + + +2、该最近节点被标记访问过 + +节点6 被标记访问过 + + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110250.png) + +由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: + +* 源点到节点7的最短距离为14,小于原minDist[7]的数值max,更新minDist[7] = 14 + + + +------------------- + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,是节点5,距离源点距离是 8 (minDist[5] = 8) + +2、该最近节点被标记访问过 + +节点5 被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110651.png) + +由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: + +* 源点到节点7的最短距离为12,小于原minDist[7]的数值14,更新minDist[7] = 12 + +----------------- + +1、选源点到哪个节点近且该节点未被访问过 + +距离源点最近且没有被访问过的节点,是节点7(终点),距离源点距离是 12 (minDist[7] = 12) + +2、该最近节点被标记访问过 + +节点7 被标记访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110920.png) + +节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 + +-------------------- + +最后我们要求起点(节点1) 到终点 (节点7)的距离。 + +再来回顾一下minDist数组的含义:记录 每一个节点距离源点的最小距离。 + +那么起到(节点1)到终点(节点7)的最短距离就是 minDist[7] ,按上面举例讲解来说,minDist[7] = 12,节点1 到节点7的最短路径为 12。 + +路径如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) + +在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 + +### 代码实现 + +本题代码如下,里面的 三部曲 我都做了注释,大家按照我上面的讲解 来看如下代码: + +```CPP +#include +#include +#include +using namespace std; +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1, vector(n + 1, INT_MAX)); + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + grid[p1][p2] = val; + } + + int start = 1; + int end = n; + + // 存储从源点到每个节点的最短距离 + std::vector minDist(n + 1, INT_MAX); + + // 记录顶点是否被访问过 + std::vector visited(n + 1, false); + + minDist[start] = 0; // 起始点到自身的距离为0 + + for (int i = 1; i <= n; i++) { // 遍历所有节点 + + int minVal = INT_MAX; + int cur = 1; + + // 1、选距离源点最近且未访问过的节点 + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] < minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; // 2、标记该节点已被访问 + + // 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } + } + + } + + if (minDist[end] == INT_MAX) cout << -1 << endl; // 不能到达终点 + else cout << minDist[end] << endl; // 到达终点最短路径 + +} +``` + +* 时间复杂度:O(n^2) +* 空间复杂度:O(n^2) + +### debug方法 + +写这种题目难免会有各种各样的问题,我们如何发现自己的代码是否有问题呢? + +最好的方式就是打日志,本题的话,就是将 minDist 数组打印出来,就可以很明显发现 哪里出问题了。 + +每次选择节点后,minDist数组的变化是否符合预期 ,是否和我上面讲的逻辑是对应的。 + +例如本题,如果想debug的话,打印日志可以这样写: + + +```CPP +#include +#include +#include +using namespace std; +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1, vector(n + 1, INT_MAX)); + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + grid[p1][p2] = val; + } + + int start = 1; + int end = n; + + std::vector minDist(n + 1, INT_MAX); + + std::vector visited(n + 1, false); + + minDist[start] = 0; + for (int i = 1; i <= n; i++) { + + int minVal = INT_MAX; + int cur = 1; + + + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] < minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; + + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } + } + + // 打印日志: + cout << "select:" << cur << endl; + for (int v = 1; v <= n; v++) cout << v << ":" << minDist[v] << " "; + cout << endl << endl;; + + } + if (minDist[end] == INT_MAX) cout << -1 << endl; + else cout << minDist[end] << endl; + +} + +``` + +打印后的结果: + +``` +select:1 +1:0 2:1 3:4 4:2147483647 5:2147483647 6:2147483647 7:2147483647 + +select:2 +1:0 2:1 3:3 4:6 5:2147483647 6:5 7:2147483647 + +select:3 +1:0 2:1 3:3 4:5 5:2147483647 6:5 7:2147483647 + +select:4 +1:0 2:1 3:3 4:5 5:8 6:5 7:2147483647 + +select:6 +1:0 2:1 3:3 4:5 5:8 6:5 7:14 + +select:5 +1:0 2:1 3:3 4:5 5:8 6:5 7:12 + +select:7 +1:0 2:1 3:3 4:5 5:8 6:5 7:12 +``` + +打印日志可以和上面我讲解的过程进行对比,每一步的结果是完全对应的。 + +所以如果大家如果代码有问题,打日志来debug是最好的方法 + +### 如何求路径 + +如果题目要求把最短路的路径打印出来,应该怎么办呢? + +这里还是有一些“坑”的,本题打印路径和 prim 打印路径是一样的,我在 [prim算法精讲](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w) 【拓展】中 已经详细讲解了。 + +在这里就不再赘述。 + +打印路径只需要添加 几行代码, 打印路径的代码我都加上的日志,如下: + +```CPP +#include +#include +#include +using namespace std; +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1, vector(n + 1, INT_MAX)); + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + grid[p1][p2] = val; + } + + int start = 1; + int end = n; + + std::vector minDist(n + 1, INT_MAX); + + std::vector visited(n + 1, false); + + minDist[start] = 0; + + //加上初始化 + vector parent(n + 1, -1); + + for (int i = 1; i <= n; i++) { + + int minVal = INT_MAX; + int cur = 1; + + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] < minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; + + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + parent[v] = cur; // 记录边 + } + } + + } + + // 输出最短情况 + for (int i = 1; i <= n; i++) { + cout << parent[i] << "->" << i << endl; + } +} +``` + +打印结果: + +``` +-1->1 +1->2 +2->3 +3->4 +4->5 +2->6 +5->7 +``` + +对应如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) + +### 出现负数 + +如果图中边的权值为负数,dijkstra 还合适吗? + +看一下这个图: (有负权值) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104334.png) + +节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 + +那我们来看dijkstra 求解的路径是什么样的,继续dijkstra 三部曲来模拟 :(dijkstra模拟过程上面已经详细讲过,以下只模拟重要过程,例如如何初始化就省略讲解了) + +----------- + +初始化: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104801.png) + +--------------- + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离源点最近,距离为0,且未被访问。 + +2、该最近节点被标记访问过 + +标记源点访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110217.png) + +更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 + +* 源点到节点2的最短距离为100,小于原minDist[2]的数值max,更新minDist[2] = 100 +* 源点到节点3的最短距离为1,小于原minDist[3]的数值max,更新minDist[4] = 1 + +------------------- + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点3最近,距离为1,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点3访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110330.png) + +由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: + +* 源点到节点4的最短距离为2,小于原minDist[4]的数值max,更新minDist[4] = 2 + +-------------- + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点4最近,距离为2,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点4访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110346.png) + +由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: + +* 源点到节点5的最短距离为3,小于原minDist[5]的数值max,更新minDist[5] = 5 + +------------ + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点5最近,距离为3,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点5访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110405.png) + +节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 + +------------ + +1、选源点到哪个节点近且该节点未被访问过 + +源点距离节点2最近,距离为100,且未被访问。 + +2、该最近节点被标记访问过 + +标记节点2访问过 + +3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110711.png) + +-------------- + +至此dijkstra的模拟过程就结束了,根据最后的minDist数组,我们求 节点1 到 节点5 的最短路径的权值总和为 3,路径: 节点1 -> 节点3 -> 节点4 -> 节点5 + +通过以上的过程模拟,我们可以发现 之所以 没有走有负权值的最短路径 是因为 在 访问 节点 2 的时候,节点 3 已经访问过了,就不会再更新了。 + +那有录友可能会想: 我可以改代码逻辑啊,访问过的节点,也让它继续访问不就好了? + +那么访问过的节点还能继续访问会不会有死循环的出现呢?控制逻辑不让其死循环?那特殊情况自己能都想清楚吗?(可以试试,实践出真知) + +对于负权值的出现,大家可以针对某一个场景 不断去修改 dijkstra 的代码,**但最终会发现只是 拆了东墙补西墙**,对dijkstra的补充逻辑只能满足某特定场景最短路求解。 + +对于求解带有负权值的最短路问题,可以使用 Bellman-Ford 算法 ,我在后序会详细讲解。 + +## dijkstra与prim算法的区别 + +> 这里再次提示,需要先看我的 [prim算法精讲](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w) ,否则可能不知道我下面讲的是什么。 + +大家可以发现 dijkstra的代码看上去 怎么和 prim算法这么像呢。 + +其实代码大体不差,唯一区别在 三部曲中的 第三步: 更新minDist数组 + +因为**prim是求 非访问节点到最小生成树的最小距离,而 dijkstra是求 非访问节点到源点的最小距离**。 + +prim 更新 minDist数组的写法: + + +```CPP +for (int j = 1; j <= v; j++) { + if (!isInTree[j] && grid[cur][j] < minDist[j]) { + minDist[j] = grid[cur][j]; + } +} +``` + +因为 minDist表示 节点到最小生成树的最小距离,所以 新节点cur的加入,只需要 使用 grid[cur][j] ,grid[cur][j] 就表示 cur 加入生成树后,生成树到 节点j 的距离。 + +dijkstra 更新 minDist数组的写法: + +```CPP +for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != INT_MAX && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } +} +``` + +因为 minDist表示 节点到源点的最小距离,所以 新节点 cur 的加入,需要使用 源点到cur的距离 (minDist[cur]) + cur 到 节点 v 的距离 (grid[cur][v]),才是 源点到节点v的距离。 + +此时大家可能不禁要想 prim算法 可以有负权值吗? + +当然可以! + +录友们可以自己思考思考一下,这是为什么? + +这里我提示一下:prim算法只需要将节点以最小权值和链接在一起,不涉及到单一路径。 + + + +## 总结 + +本篇,我们深入讲解的dijkstra算法,详细模拟其工作的流程。 + +这里我给出了 **dijkstra 三部曲 来 帮助大家理解 该算法**,不至于 每次写 dijkstra 都是黑盒操作,没有框架没有章法。 + +在给出的代码中,我也按照三部曲的逻辑来给大家注释,只要理解这三部曲,即使 过段时间 对 dijkstra 算法有些遗忘,依然可以写出一个框架出来,然后再去调试细节。 + +对于图论算法,一般代码都比较长,很难写出代码直接可以提交通过,都需要一个debug的过程,所以 **学习如何debug 非常重要**! + +这也是我为什么 在本文中 单独用来讲解 debug方法。 + +本题求的是最短路径和是多少,**同时我们也要掌握 如何把最短路径打印出来**。 + +我还写了大篇幅来讲解 负权值的情况, 只有画图带大家一步一步去 看 出现负权值 dijkstra的求解过程,才能帮助大家理解,问题出在哪里。 + +如果我直接讲:是**因为访问过的节点 不能再访问,导致错过真正的最短路**,我相信大家都不知道我在说啥。 + +最后我还讲解了 dijkstra 和 prim 算法的 相同 与 不同之处, 我在图论的讲解安排中 先讲 prim算法 再讲 dijkstra 是有目的的, **理解这两个算法的相同与不同之处 有助于大家学习的更深入**。 + +而不是 学了 dijkstra 就只看 dijkstra, 算法之间 都是有联系的,多去思考 算法之间的相互联系,会帮助大家思考的更深入,掌握的更彻底。 + +本篇写了这么长,我也只讲解了 朴素版dijkstra,**关于 堆优化dijkstra,我会在下一篇再来给大家详细讲解**。 + +加油 + + + diff --git "a/problems/kama53.\345\257\273\345\256\235-Kruskal.md" "b/problems/kama53.\345\257\273\345\256\235-Kruskal.md" new file mode 100644 index 0000000000..1e6cbbb909 --- /dev/null +++ "b/problems/kama53.\345\257\273\345\256\235-Kruskal.md" @@ -0,0 +1,400 @@ + +# 寻宝 + +[卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) + +题目描述: + +在世界的某个区域,有一些分散的神秘岛屿,每个岛屿上都有一种珍稀的资源或者宝藏。国王打算在这些岛屿上建公路,方便运输。 + +不同岛屿之间,路途距离不同,国王希望你可以规划建公路的方案,如何可以以最短的总公路距离将 所有岛屿联通起来。 + +给定一张地图,其中包括了所有的岛屿,以及它们之间的距离。以最小化公路建设长度,确保可以链接到所有岛屿。 + +输入描述: + +第一行包含两个整数V 和 E,V代表顶点数,E代表边数 。顶点编号是从1到V。例如:V=2,一个有两个顶点,分别是1和2。 + +接下来共有 E 行,每行三个整数 v1,v2 和 val,v1 和 v2 为边的起点和终点,val代表边的权值。 + +输出描述: + +输出联通所有岛屿的最小路径总距离 + +输入示例: + +``` +7 11 +1 2 1 +1 3 1 +1 5 2 +2 6 1 +2 4 2 +2 3 2 +3 4 1 +4 5 1 +5 6 2 +5 7 1 +6 7 1 +``` + +输出示例: + +6 + +## 解题思路 + +在上一篇 我们讲解了 prim算法求解 最小生成树,本篇我们来讲解另一个算法:Kruskal,同样可以求最小生成树。 + +**prim 算法是维护节点的集合,而 Kruskal 是维护边的集合**。 + +上来就这么说,大家应该看不太懂,这里是先让大家有这么个印象,带着这个印象在看下文,理解的会更到位一些。 + +kruscal的思路: + +* 边的权值排序,因为要优先选最小的边加入到生成树里 +* 遍历排序后的边 + * 如果边首尾的两个节点在同一个集合,说明如果连上这条边图中会出现环 + * 如果边首尾的两个节点不在同一个集合,加入到最小生成树,并把两个节点加入同一个集合 + +下面我们画图举例说明kruscal的工作过程。 + +依然以示例中,如下这个图来举例。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111113514.png) + +将图中的边按照权值有小到大排序,这样从贪心的角度来说,优先选 权值小的边加入到 最小生成树中。 + +排序后的边顺序为[(1,2) (4,5) (1,3) (2,6) (3,4) (6,7) (5,7) (1,5) (3,2) (2,4) (5,6)] + +> (1,2) 表示节点1 与 节点2 之间的边。权值相同的边,先后顺序无所谓。 + +**开始从头遍历排序后的边**。 + +-------- + +选边(1,2),节点1 和 节点2 不在同一个集合,所以生成树可以添加边(1,2),并将 节点1,节点2 放在同一个集合。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111114204.png) + +-------- + +选边(4,5),节点4 和 节点 5 不在同一个集合,生成树可以添加边(4,5) ,并将节点4,节点5 放到同一个集合。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111120458.png) + +**大家判断两个节点是否在同一个集合,就看图中两个节点是否有绿色的粗线连着就行** + +------ + +(这里在强调一下,以下选边是按照上面排序好的边的数组来选择的) + +选边(1,3),节点1 和 节点3 不在同一个集合,生成树添加边(1,3),并将节点1,节点3 放到同一个集合。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112105834.png) + +--------- + +选边(2,6),节点2 和 节点6 不在同一个集合,生成树添加边(2,6),并将节点2,节点6 放到同一个集合。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110214.png) + +-------- + +选边(3,4),节点3 和 节点4 不在同一个集合,生成树添加边(3,4),并将节点3,节点4 放到同一个集合。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110450.png) + +---------- + +选边(6,7),节点6 和 节点7 不在同一个集合,生成树添加边(6,7),并将 节点6,节点7 放到同一个集合。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110637.png) + +----------- + +选边(5,7),节点5 和 节点7 在同一个集合,不做计算。 + +选边(1,5),两个节点在同一个集合,不做计算。 + +后面遍历 边(3,2),(2,4),(5,6) 同理,都因两个节点已经在同一集合,不做计算。 + + +------- + +此时 我们就已经生成了一个最小生成树,即: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110637.png) + +在上面的讲解中,看图的话 大家知道如何判断 两个节点 是否在同一个集合(是否有绿色的线连在一起),以及如何把两个节点加入集合(就在图中把两个节点连上) + +**但在代码中,如果将两个节点加入同一个集合,又如何判断两个节点是否在同一个集合呢**? + +这里就涉及到我们之前讲解的[并查集](https://www.programmercarl.com/%E5%9B%BE%E8%AE%BA%E5%B9%B6%E6%9F%A5%E9%9B%86%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html)。 + +我们在并查集开篇的时候就讲了,并查集主要就两个功能: + +* 将两个元素添加到一个集合中 +* 判断两个元素在不在同一个集合 + +大家发现这正好符合 Kruskal算法的需求,这也是为什么 **我要先讲并查集,再讲 Kruskal**。 + +关于 并查集,我已经在[并查集精讲](https://www.programmercarl.com/%E5%9B%BE%E8%AE%BA%E5%B9%B6%E6%9F%A5%E9%9B%86%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html) 详细讲解过了,所以这里不再赘述,我们直接用。 + +本题代码如下,已经详细注释: + +```CPP + +#include +#include +#include + +using namespace std; + +// l,r为 边两边的节点,val为边的数值 +struct Edge { + int l, r, val; +}; + +// 节点数量 +int n = 10001; +// 并查集标记节点关系的数组 +vector father(n, -1); // 节点编号是从1开始的,n要大一些 + +// 并查集初始化 +void init() { + for (int i = 0; i < n; ++i) { + father[i] = i; + } +} + +// 并查集的查找操作 +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 +} + +// 并查集的加入集合 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} + +int main() { + + int v, e; + int v1, v2, val; + vector edges; + int result_val = 0; + cin >> v >> e; + while (e--) { + cin >> v1 >> v2 >> val; + edges.push_back({v1, v2, val}); + } + + // 执行Kruskal算法 + // 按边的权值对边进行从小到大排序 + sort(edges.begin(), edges.end(), [](const Edge& a, const Edge& b) { + return a.val < b.val; + }); + + // 并查集初始化 + init(); + + // 从头开始遍历边 + for (Edge edge : edges) { + // 并查集,搜出两个节点的祖先 + int x = find(edge.l); + int y = find(edge.r); + + // 如果祖先不同,则不在同一个集合 + if (x != y) { + result_val += edge.val; // 这条边可以作为生成树的边 + join(x, y); // 两个节点加入到同一个集合 + } + } + cout << result_val << endl; + return 0; +} + +``` + +时间复杂度:nlogn (快排) + logn (并查集) ,所以最后依然是 nlogn 。n为边的数量。 + +关于并查集时间复杂度,可以看我在 [并查集理论基础](https://programmercarl.com/%E5%9B%BE%E8%AE%BA%E5%B9%B6%E6%9F%A5%E9%9B%86%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html) 的讲解。 + +## 拓展一 + +如果题目要求将最小生成树的边输出的话,应该怎么办呢? + +Kruskal 算法 输出边的话,相对prim 要容易很多,因为 Kruskal 本来就是直接操作边,边的结构自然清晰,不用像 prim一样 需要再节点练成线输出边 (因为prim是对节点操作,而 Kruskal是对边操作,这是本质区别) + +本题中,边的结构为: + +```CPP +struct Edge { + int l, r, val; +}; +``` + +那么我们只需要找到 在哪里把生成树的边保存下来就可以了。 + +当判断两个节点不在同一个集合的时候,这两个节点的边就加入到最小生成树, 所以添加边的操作在这里: + +```CPP +vector result; // 存储最小生成树的边 +// 如果祖先不同,则不在同一个集合 +if (x != y) { + result.push_back(edge); // 记录最小生成树的边 + result_val += edge.val; // 这条边可以作为生成树的边 + join(x, y); // 两个节点加入到同一个集合 +} +``` + +整体代码如下,为了突出重点,我仅仅将 打印最小生成树的部分代码注释了,大家更容易看到哪些改动。 + +```CPP +#include +#include +#include + +using namespace std; + +struct Edge { + int l, r, val; +}; + + +int n = 10001; + +vector father(n, -1); + +void init() { + for (int i = 0; i < n; ++i) { + father[i] = i; + } +} + +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); +} + +void join(int u, int v) { + u = find(u); + v = find(v); + if (u == v) return ; + father[v] = u; +} + +int main() { + + int v, e; + int v1, v2, val; + vector edges; + int result_val = 0; + cin >> v >> e; + while (e--) { + cin >> v1 >> v2 >> val; + edges.push_back({v1, v2, val}); + } + + sort(edges.begin(), edges.end(), [](const Edge& a, const Edge& b) { + return a.val < b.val; + }); + + vector result; // 存储最小生成树的边 + + init(); + + for (Edge edge : edges) { + + int x = find(edge.l); + int y = find(edge.r); + + + if (x != y) { + result.push_back(edge); // 保存最小生成树的边 + result_val += edge.val; + join(x, y); + } + } + + // 打印最小生成树的边 + for (Edge edge : result) { + cout << edge.l << " - " << edge.r << " : " << edge.val << endl; + } + + return 0; +} + + +``` + +按照题目中的示例,打印边的输出为: + +``` +1 - 2 : 1 +1 - 3 : 1 +2 - 6 : 1 +3 - 4 : 1 +4 - 5 : 1 +5 - 7 : 1 +``` + +大家可能发现 怎么和我们 模拟画的图不一样,差别在于 代码生成的最小生成树中 节点5 和 节点7相连的。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240116163014.png) + + +其实造成这个差别 是对边排序的时候 权值相同的边先后顺序的问题导致的,无论相同权值边的顺序是什么样的,最后都能得出最小生成树。 + + +## 拓展二 + + +此时我们已经讲完了 Kruskal 和 prim 两个解法来求最小生成树。 + +什么情况用哪个算法更合适呢。 + +Kruskal 与 prim 的关键区别在于,prim维护的是节点的集合,而 Kruskal 维护的是边的集合。 +如果 一个图中,节点多,但边相对较少,那么使用Kruskal 更优。 + +有录友可能疑惑,一个图里怎么可能节点多,边却少呢? + +节点未必一定要连着边那, 例如 这个图,大家能明显感受到边没有那么多对吧,但节点数量 和 上述我们讲的例子是一样的。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240116152211.png) + +为什么边少的话,使用 Kruskal 更优呢? + +因为 Kruskal 是对边进行排序的后 进行操作是否加入到最小生成树。 + +边如果少,那么遍历操作的次数就少。 + +在节点数量固定的情况下,图中的边越少,Kruskal 需要遍历的边也就越少。 + +而 prim 算法是对节点进行操作的,节点数量越少,prim算法效率就越少。 + +所以在 稀疏图中,用Kruskal更优。 在稠密图中,用prim算法更优。 + +> 边数量较少为稀疏图,接近或等于完全图(所有节点皆相连)为稠密图 + + +Prim 算法 时间复杂度为 O(n^2),其中 n 为节点数量,它的运行效率和图中边树无关,适用稠密图。 + +Kruskal算法 时间复杂度 为 nlogn,其中n 为边的数量,适用稀疏图。 + +## 总结 + +如果学过了并查集,其实 kruskal 比 prim更好理解一些。 + +本篇,我们依然通过模拟 Kruskal 算法的过程,来带大家一步步了解其工作过程。 + +在 拓展一 中讲解了 如何输出最小生成树的边。 + +在拓展二 中讲解了 prim 和 Kruskal的区别。 + +录友们可以细细体会。 + + diff --git "a/problems/kama53.\345\257\273\345\256\235.md" "b/problems/kama53.\345\257\273\345\256\235-prim.md" similarity index 93% rename from "problems/kama53.\345\257\273\345\256\235.md" rename to "problems/kama53.\345\257\273\345\256\235-prim.md" index 3a4b8b274c..9959954aef 100644 --- "a/problems/kama53.\345\257\273\345\256\235.md" +++ "b/problems/kama53.\345\257\273\345\256\235-prim.md" @@ -5,9 +5,11 @@ 题目描述: -在世界的某个区域,有一些分散的神秘岛屿,每个岛屿上都有一种珍稀的资源或者宝藏。你是一名探险者,决定前往这些岛屿,但为了节省时间和资源,你希望规划一条最短的路径,以便在探索这些岛屿时尽量减少旅行的距离。 +在世界的某个区域,有一些分散的神秘岛屿,每个岛屿上都有一种珍稀的资源或者宝藏。国王打算在这些岛屿上建公路,方便运输。 -给定一张地图,其中包括了所有的岛屿,以及它们之间的距离。每个岛屿都需要被至少访问一次,你的目标是规划一条最短路径,以最小化探索路径的总距离,同时确保访问了所有岛屿。 +不同岛屿之间,路途距离不同,国王希望你可以规划建公路的方案,如何可以以最短的总公路距离将 所有岛屿联通起来。 + +给定一张地图,其中包括了所有的岛屿,以及它们之间的距离。以最小化公路建设长度,确保可以链接到所有岛屿。 输入描述: @@ -133,7 +135,7 @@ minDist 数组 里的数值初始化为 最大数,因为本题 节点距离不 1、prim三部曲,第一步:选距离生成树最近节点 -选取一个距离 最小生成树(节点1) 最近的非生成树里的节点,节点2,3,5 距离 最小生成树(节点1) 最近,选节点 2(其实选 节点3或者节点5都可以,距离一样的)加入最小生成树。 +选取一个距离 最小生成树(节点1) 最近的非生成树里的节点,节点2,3,5 距离 最小生成树(节点1) 最近,选节点 2(其实选 节点3或者节点2都可以,距离一样的)加入最小生成树。 2、prim三部曲,第二步:最近节点加入生成树 @@ -272,6 +274,8 @@ minDist数组已经更新了 所有非生成树的节点距离 最小生成树 ```CPP #include #include +#include + using namespace std; int main() { int v, e; @@ -296,28 +300,28 @@ int main() { for (int i = 1; i < v; i++) { // 1、prim三部曲,第一步:选距离生成树最近节点 - int cur = -1; // 选中哪个节点 加入最小生成树 + int cur = -1; // 选中哪个节点 加入最小生成树 + int minVal = INT_MAX; for (int j = 1; j <= v; j++) { // 1 - v,顶点编号,这里下标从1开始 - // 选取最小生成树节点的条件: + // 选取最小生成树节点的条件: // (1)不在最小生成树里 - // (2)距离最小生成树最近的节点 - // (3)只要不在最小生成树里,先默认选一个节点 ,在比较 哪一个是最小的 - // 理解条件3 很重要,才能理解这段代码:(cur == -1 || minDist[j] < minDist[cur]) - if (!isInTree[j] && (cur == -1 || minDist[j] < minDist[cur])) { + // (2)距离最小生成树最近的节点 + if (!isInTree[j] && minDist[j] < minVal) { + minVal = minDist[j]; cur = j; } } - // 2、prim三部曲,第二步:最近节点(cur)加入生成树 + // 2、prim三部曲,第二步:最近节点(cur)加入生成树 isInTree[cur] = true; // 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) // cur节点加入之后, 最小生成树加入了新的节点,那么所有节点到 最小生成树的距离(即minDist数组)需要更新一下 - // 由于cur节点是新加入到最小生成树,那么只需要关心与 cur 相连的 非生成树节点 的距离 是否比 原来 非生成树节点到生成树节点的距离更小了呢 - for (int j = 1; j <= v; j++) { - // 更新的条件: - // (1)节点是 非生成树里的节点 - // (2)与cur相连的某节点的权值 比 该某节点距离最小生成树的距离小 - // 很多录友看到自己 就想不明白什么意思,其实就是 cur 是新加入 最小生成树的节点,那么 所有非生成树的节点距离生成树节点的最近距离 由于 cur的新加入,需要更新一下数据了 + // 由于cur节点是新加入到最小生成树,那么只需要关心与 cur 相连的 非生成树节点 的距离 是否比 原来 非生成树节点到生成树节点的距离更小了呢 + for (int j = 1; j <= v; j++) { + // 更新的条件: + // (1)节点是 非生成树里的节点 + // (2)与cur相连的某节点的权值 比 该某节点距离最小生成树的距离小 + // 很多录友看到自己 就想不明白什么意思,其实就是 cur 是新加入 最小生成树的节点,那么 所有非生成树的节点距离生成树节点的最近距离 由于 cur的新加入,需要更新一下数据了 if (!isInTree[j] && grid[cur][j] < minDist[j]) { minDist[j] = grid[cur][j]; } @@ -325,14 +329,17 @@ int main() { } // 统计结果 int result = 0; - for (int i = 2; i <= v; i++) { // 不计第一个顶点,因为统计的是边的权值,v个节点有 v-1条边 + for (int i = 2; i <= v; i++) { // 不计第一个顶点,因为统计的是边的权值,v个节点有 v-1条边 result += minDist[i]; } cout << result << endl; } + ``` +时间复杂度为 O(n^2),其中 n 为节点数量。 + ## 拓展 上面讲解的是记录了最小生成树 所有边的权值,如果让打印出来 最小生成树的每条边呢? 或者说 要把这个最小生成树画出来呢? @@ -406,6 +413,8 @@ for (int j = 1; j <= v; j++) { ```CPP #include #include +#include + using namespace std; int main() { int v, e; @@ -426,11 +435,14 @@ int main() { for (int i = 1; i < v; i++) { int cur = -1; - for (int j = 1; j <= v; j++) { - if (!isInTree[j] && (cur == -1 || minDist[j] < minDist[cur])) { + int minVal = INT_MAX; + for (int j = 1; j <= v; j++) { + if (!isInTree[j] && minDist[j] < minVal) { + minVal = minDist[j]; cur = j; } } + isInTree[cur] = true; for (int j = 1; j <= v; j++) { if (!isInTree[j] && grid[cur][j] < minDist[j]) { @@ -440,11 +452,12 @@ int main() { } } } - // 输出 最小生成树边的链接情况 + // 输出 最小生成树边的链接情况 for (int i = 1; i <= v; i++) { - cout << i "->" parent[i] << endl; + cout << i << "->" << parent[i] << endl; } } + ``` 按照本题示例,代码输入如下: diff --git a/problems/qita/shejimoshi.md b/problems/qita/shejimoshi.md index cf980e45f6..d5342c6132 100644 --- a/problems/qita/shejimoshi.md +++ b/problems/qita/shejimoshi.md @@ -1,4 +1,6 @@ +# 23种设计模式精讲 | 配套练习题 | 卡码网 + 关于设计模式的学习,大家应该还是看书或者看博客,但却没有一个边学边练的学习环境。 学完了一种设计模式 是不是应该去练一练? From 8ba68a4d6fe0ed3497967591b3b45e25b228a5da Mon Sep 17 00:00:00 2001 From: kkkkevx <108632304+kkkkevx@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:27:41 -0700 Subject: [PATCH 1040/1533] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strSt?= =?UTF-8?q?r.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 8d0cc52559..e195ef9b98 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -564,6 +564,38 @@ public: ## 其他语言版本 ### Java: +```Java +class Solution { + /** + 牺牲空间,换取最直白的暴力法 + 时间复杂度 O(n * m) + 空间 O(n + m) + */ + public int strStr(String haystack, String needle) { + // 获取 haystack 和 needle 的长度 + int n = haystack.length(), m = needle.length(); + // 将字符串转换为字符数组,方便索引操作 + char[] s = haystack.toCharArray(), p = needle.toCharArray(); + + // 遍历 haystack 字符串 + for (int i = 0; i < n - m + 1; i++) { + // 初始化匹配的指针 + int a = i, b = 0; + // 循环检查 needle 是否在当前位置开始匹配 + while (b < m && s[a] == p[b]) { + // 如果当前字符匹配,则移动指针 + a++; + b++; + } + // 如果 b 等于 m,说明 needle 已经完全匹配,返回当前位置 i + if (b == m) return i; + } + + // 如果遍历完毕仍未找到匹配的子串,则返回 -1 + return -1; + } +} +``` ```Java class Solution { From 6e550159d50f2719d561f0bcd98b08cc7f093394 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 15 Mar 2024 11:47:33 +1100 Subject: [PATCH 1041/1533] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加javascript递归版本实现 --- ...70\255\347\232\204\350\212\202\347\202\271.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index b2a830a746..aa39de5fb3 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -285,6 +285,21 @@ var swapPairs = function (head) { }; ``` +```javascript +// 递归版本 +var swapPairs = function (head) { + if (head == null || head.next == null) { + return head; + } + + let after = head.next; + head.next = swapPairs(after.next); + after.next = head; + + return after; +}; +``` + ### TypeScript: ```typescript From 20216736e5daf5828bf45d1d04e238057074b7fa Mon Sep 17 00:00:00 2001 From: alfiechen Date: Fri, 15 Mar 2024 23:26:45 +0800 Subject: [PATCH 1042/1533] =?UTF-8?q?Update=200035.=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE=20-=20=E6=B7=BB=E5=8A=A0Py?= =?UTF-8?q?thon3=20=E7=AC=AC=E4=BA=8C=E7=A8=AE=E4=BA=8C=E5=88=86=E6=B3=95?= =?UTF-8?q?=20-=20=E5=B7=A6=E9=97=AD=E5=8F=B3=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\344\275\215\347\275\256.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 80b7e40e4a..c969d73034 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -332,6 +332,7 @@ impl Solution { ### Python ```python +# 第一种二分法: [left, right]左闭右闭区间 class Solution: def searchInsert(self, nums: List[int], target: int) -> int: left, right = 0, len(nums) - 1 @@ -348,6 +349,26 @@ class Solution: return right + 1 ``` +```python +# 第二种二分法: [left, right)左闭右开区间 +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left = 0 + right = len(nums) + + while (left < right): + middle = (left + right) // 2 + + if nums[middle] > target: + right = middle + elif nums[middle] < target: + left = middle + 1 + else: + return middle + + return right +``` + ### JavaScript ```js From e4ebc6b08f73f06b00b97f59cd0ec9eaa679d1da Mon Sep 17 00:00:00 2001 From: baiye959 <3383522774@qq.com> Date: Sat, 16 Mar 2024 17:21:28 +0800 Subject: [PATCH 1043/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0kama54=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E5=AD=97=20Java=20=E7=89=88=E6=9C=AC=20?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" index f03e575d1d..7dc22c0a85 100644 --- "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -142,7 +142,7 @@ for (int i = 0; i < a.size(); i++) { ### C: ### Java: - +解法一 ```java import java.util.Scanner; @@ -160,6 +160,42 @@ class Main { } } ``` +解法二 +```java +// 为了还原题目本意,先把原数组复制到扩展长度后的新数组,然后不再使用原数组、原地对新数组进行操作。 +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String s = sc.next(); + int len = s.length(); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) >= 0 && s.charAt(i) <= '9') { + len += 5; + } + } + + char[] ret = new char[len]; + for (int i = 0; i < s.length(); i++) { + ret[i] = s.charAt(i); + } + for (int i = s.length() - 1, j = len - 1; i >= 0; i--) { + if ('0' <= ret[i] && ret[i] <= '9') { + ret[j--] = 'r'; + ret[j--] = 'e'; + ret[j--] = 'b'; + ret[j--] = 'm'; + ret[j--] = 'u'; + ret[j--] = 'n'; + } else { + ret[j--] = ret[i]; + } + } + System.out.println(ret); + } +} +``` ### Go: ````go From 77a4de1f41b5078aa4dcfbdf169b5f9474b40198 Mon Sep 17 00:00:00 2001 From: Yanshi XU Date: Sat, 16 Mar 2024 18:00:50 +0800 Subject: [PATCH 1044/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8A=9B?= =?UTF-8?q?=E6=89=A3=E9=A2=98=E7=9B=AE=E7=9A=84=E9=93=BE=E6=8E=A5:=2020201?= =?UTF-8?q?003=E4=BA=8C=E5=8F=89=E6=A0=91=E5=91=A8=E6=9C=AB=E6=80=BB?= =?UTF-8?q?=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了力扣题目的链接 --- ...40\221\345\221\250\346\234\253\346\200\273\347\273\223.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 57416775fb..4b73e663b4 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -14,8 +14,8 @@ 而本题的迭代法中我们使用了队列,需要注意的是这不是层序遍历,而且仅仅通过一个容器来成对的存放我们要比较的元素,认识到这一点之后就发现:用队列,用栈,甚至用数组,都是可以的。 那么做完本题之后,在看如下两个题目。 -* 100.相同的树 -* 572.另一个树的子树 +* [100.相同的树](https://leetcode.cn/problems/same-tree/description/) +* [572.另一个树的子树](https://leetcode.cn/problems/subtree-of-another-tree/) **[二叉树:我对称么?](https://programmercarl.com/0101.对称二叉树.html)中的递归法和迭代法只需要稍作修改其中一个树的遍历顺序,便可刷了100.相同的树。** From cff7999479528160b104ae8f7709456a1f193ecc Mon Sep 17 00:00:00 2001 From: Yanshi XU Date: Sat, 16 Mar 2024 18:02:00 +0800 Subject: [PATCH 1045/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97:=2020201003=E4=BA=8C=E5=8F=89=E6=A0=91=E5=91=A8?= =?UTF-8?q?=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- ...\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 4b73e663b4..ea5082244c 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -13,7 +13,7 @@ 而本题的迭代法中我们使用了队列,需要注意的是这不是层序遍历,而且仅仅通过一个容器来成对的存放我们要比较的元素,认识到这一点之后就发现:用队列,用栈,甚至用数组,都是可以的。 -那么做完本题之后,在看如下两个题目。 +那么做完本题之后,再看如下两个题目。 * [100.相同的树](https://leetcode.cn/problems/same-tree/description/) * [572.另一个树的子树](https://leetcode.cn/problems/subtree-of-another-tree/) From 29ba4f47e31b289b8cc8b7dc3902ba2f6d7396d4 Mon Sep 17 00:00:00 2001 From: paigeman <53284808+paigeman@users.noreply.github.com> Date: Tue, 19 Mar 2024 11:28:21 +0800 Subject: [PATCH 1046/1533] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index b2a830a746..67bf70f3da 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -81,7 +81,7 @@ public: 上面的代码我第一次提交执行用时8ms,打败6.5%的用户,差点吓到我了。 -心想应该没有更好的方法了吧,也就$O(n)$的时间复杂度,重复提交几次,这样了: +心想应该没有更好的方法了吧,也就 $O(n)$ 的时间复杂度,重复提交几次,这样了: ![24.两两交换链表中的节点](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.png) From 311b2f4fde6b4f980029bf2f4d71b7854d69d6d2 Mon Sep 17 00:00:00 2001 From: gemaxis Date: Tue, 19 Mar 2024 12:58:18 +0800 Subject: [PATCH 1047/1533] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\255\347\232\204\350\212\202\347\202\271.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index b2a830a746..3d330f1731 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -181,6 +181,23 @@ class Solution { } ``` +```java +// 将步骤 2,3 交换顺序,这样不用定义 temp 节点 +public ListNode swapPairs(ListNode head) { + ListNode dummy = new ListNode(0, head); + ListNode cur = dummy; + while (cur.next != null && cur.next.next != null) { + ListNode node1 = cur.next;// 第 1 个节点 + ListNode node2 = cur.next.next;// 第 2 个节点 + cur.next = node2; // 步骤 1 + node1.next = node2.next;// 步骤 3 + node2.next = node1;// 步骤 2 + cur = cur.next.next; + } + return dummy.next; +} +``` + ### Python: ```python From dbac875d31de722ff7c90bac778364956511b821 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 19 Mar 2024 16:25:36 +0800 Subject: [PATCH 1048/1533] =?UTF-8?q?27.=20=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0Go=E7=89=88=E6=9C=AC=E6=9A=B4=E5=8A=9B=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.md" | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 2a2005d736..967a759653 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -276,6 +276,24 @@ class Solution: ### Go: +```go +// 暴力法 +// 时间复杂度 O(n^2) +// 空间复杂度 O(1) +func removeElement(nums []int, val int) int { + size := len(nums) + for i := 0; i < size; i ++ { + if nums[i] == val { + for j := i + 1; j < size; j ++ { + nums[j - 1] = nums[j] + } + i -- + size -- + } + } + return size +} +``` ```go // 快慢指针法 // 时间复杂度 O(n) @@ -318,7 +336,6 @@ func removeElement(nums []int, val int) int { right-- } } - fmt.Println(nums) return left } ``` From 7117871beef8e1754107123a27047bd03354cd67 Mon Sep 17 00:00:00 2001 From: Yubin Li Date: Tue, 19 Mar 2024 20:40:46 +0800 Subject: [PATCH 1049/1533] =?UTF-8?q?Correct=20typo=20in=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 4411b5609f..457050d508 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -129,7 +129,7 @@ class Solution { return resList; } - //DFS--递归方式 + //BFS--递归方式 public void checkFun01(TreeNode node, Integer deep) { if (node == null) return; deep++; From 12bc134af8987659efeb76a973ffb3206ca9d59d Mon Sep 17 00:00:00 2001 From: Shuai Date: Sat, 23 Mar 2024 15:50:42 +0800 Subject: [PATCH 1050/1533] test --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 81558cc12c..3b562f5a1e 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -311,7 +311,7 @@ class Solution: for i in range(startIndex, len(candidates)): if total + candidates[i] > target: - continue + break total += candidates[i] path.append(candidates[i]) self.backtracking(candidates, target, total, i, path, result) @@ -664,4 +664,3 @@ public class Solution - From 0e1cc6d2771113ef0caeed9a26ec0e7e590e91e7 Mon Sep 17 00:00:00 2001 From: Meliodas417 <76930518+Meliodas417@users.noreply.github.com> Date: Sun, 24 Mar 2024 11:07:42 -0500 Subject: [PATCH 1051/1533] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2?= =?UTF-8?q?=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 除法逻辑不完整,假如是5/2=2没问题,但如果是-5/2,会向下取整,就变成-3了,可以单独定义一个函数。 底下最先弹出的数字实际上是最后进入的数字,也就是运算符右边的操作数,而第二个弹出的数字是第一个进入的数字,即运算符左边的操作数。stack.append(operation(op2, op1)) --- ...76\345\274\217\346\261\202\345\200\274.md" | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 663a68ea5c..fd4d072616 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -169,8 +169,12 @@ class Solution { ```python from operator import add, sub, mul -class Solution: - op_map = {'+': add, '-': sub, '*': mul, '/': lambda x, y: int(x / y)} +def div(x, y): + # 使用整数除法的向零取整方式 + return int(x / y) if x * y > 0 else -(abs(x) // abs(y)) + +class Solution(object): + op_map = {'+': add, '-': sub, '*': mul, '/': div} def evalRPN(self, tokens: List[str]) -> int: stack = [] @@ -186,18 +190,31 @@ class Solution: 另一种可行,但因为使用eval相对较慢的方法: ```python -class Solution: - def evalRPN(self, tokens: List[str]) -> int: +from operator import add, sub, mul + +def div(x, y): + # 使用整数除法的向零取整方式 + return int(x / y) if x * y > 0 else -(abs(x) // abs(y)) + +class Solution(object): + op_map = {'+': add, '-': sub, '*': mul, '/': div} + + def evalRPN(self, tokens): + """ + :type tokens: List[str] + :rtype: int + """ stack = [] - for item in tokens: - if item not in {"+", "-", "*", "/"}: - stack.append(item) + for token in tokens: + if token in self.op_map: + op1 = stack.pop() + op2 = stack.pop() + operation = self.op_map[token] + stack.append(operation(op2, op1)) else: - first_num, second_num = stack.pop(), stack.pop() - stack.append( - int(eval(f'{second_num} {item} {first_num}')) # 第一个出来的在运算符后面 - ) - return int(stack.pop()) # 如果一开始只有一个数,那么会是字符串形式的 + stack.append(int(token)) + return stack.pop() + ``` From 8e216326b6858568acc3379db71817218a8e80c4 Mon Sep 17 00:00:00 2001 From: paigeman <53284808+paigeman@users.noreply.github.com> Date: Mon, 25 Mar 2024 09:27:19 +0800 Subject: [PATCH 1052/1533] =?UTF-8?q?Update=20=E5=8F=8C=E6=8C=87=E9=92=88?= =?UTF-8?q?=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" index 6621e0396b..95832a787b 100644 --- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" +++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" @@ -90,7 +90,7 @@ for (int i = 0; i < array.size(); i++) { ## 总结 -本文中一共介绍了leetcode上九道使用双指针解决问题的经典题目,除了链表一些题目一定要使用双指针,其他题目都是使用双指针来提高效率,一般是将O(n^2)的时间复杂度,降为$O(n)$。 +本文中一共介绍了leetcode上九道使用双指针解决问题的经典题目,除了链表一些题目一定要使用双指针,其他题目都是使用双指针来提高效率,一般是将O(n^2)的时间复杂度,降为 $O(n)$ 。 建议大家可以把文中涉及到的题目在好好做一做,琢磨琢磨,基本对双指针法就不在话下了。 From f9ef9e9f20d97b8a1097905ca48ecad24fe8bdaf Mon Sep 17 00:00:00 2001 From: departever <2233492563@qq.com> Date: Mon, 25 Mar 2024 15:36:53 +0800 Subject: [PATCH 1053/1533] =?UTF-8?q?=E5=B0=86java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=B5=8E=E9=87=91=E4=BF=A1=E4=B8=AD=E7=9A=84=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E7=9A=84=E6=80=BB=E5=AD=97=E4=BF=AE=E6=94=B9=E4=B8=AD=E5=AD=97?= =?UTF-8?q?=E4=BE=BF=E4=BA=8E=E7=90=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index ff5aafed62..ca877f0a8b 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -133,7 +133,7 @@ class Solution { record[c - 'a'] -= 1; } - // 如果数组中存在负数,说明ransomNote字符串总存在magazine中没有的字符 + // 如果数组中存在负数,说明ransomNote字符串中存在magazine中没有的字符 for(int i : record){ if(i < 0){ return false; @@ -469,4 +469,3 @@ bool canConstruct(char* ransomNote, char* magazine) { - From 10af2bdde48a77285aaeae879fadf3b59ce123de Mon Sep 17 00:00:00 2001 From: alain Date: Mon, 25 Mar 2024 16:24:29 +0800 Subject: [PATCH 1054/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=B3=95=20Rust=20=E7=A4=BA=E4=BE=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\210\206\346\237\245\346\211\276.md" | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 31e89ae344..8ff6a6fcdb 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -501,19 +501,19 @@ func search(nums: [Int], target: Int) -> Int { ### **Rust:** -(版本一)左闭右开区间 +(版本一)左闭右闭区间 ```rust use std::cmp::Ordering; impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { - let (mut left, mut right) = (0, nums.len()); - while left < right { + let (mut left, mut right) = (0_i32, nums.len() as i32 - 1); + while left <= right { let mid = (right + left) / 2; - match nums[mid].cmp(&target) { + match nums[mid as usize].cmp(&target) { Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, - Ordering::Equal => return mid as i32, + Ordering::Greater => right = mid - 1, + Ordering::Equal => return mid, } } -1 @@ -521,19 +521,19 @@ impl Solution { } ``` -//(版本二)左闭右闭区间 +//(版本二)左闭右开区间 ```rust use std::cmp::Ordering; impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { - let (mut left, mut right) = (0, nums.len()); - while left <= right { + let (mut left, mut right) = (0_i32, nums.len() as i32); + while left < right { let mid = (right + left) / 2; - match nums[mid].cmp(&target) { + match nums[mid as usize].cmp(&target) { Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid - 1, - Ordering::Equal => return mid as i32, + Ordering::Greater => right = mid, + Ordering::Equal => return mid, } } -1 From 8452d5f06743e037eac7dab0898e7ed4e5a2f2af Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Tue, 26 Mar 2024 11:23:25 +0800 Subject: [PATCH 1055/1533] add a new python version --- ...204\347\232\204\345\271\263\346\226\271.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 5bdbcbc7e0..bb311ca2cd 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -178,6 +178,24 @@ class Solution: return sorted(x*x for x in nums) ``` +```Python +(版本四) 双指针+ 反转列表 +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + #根据list的先进排序在先原则 + #将nums的平方按从大到小的顺序添加进新的list + #最后反转list + new_list = [] + left, right = 0 , len(nums) -1 + while left <= right: + if abs(nums[left]) <= abs(nums[right]): + new_list.append(nums[right] ** 2) + right -= 1 + else: + new_list.append(nums[left] ** 2) + left += 1 + return new_list[::-1] + ### Go: ```Go From 92850e8a47b1c7ecba1aef99fdbc38bf2d9a52e9 Mon Sep 17 00:00:00 2001 From: WilsonKuo <78621847+WilsonKuo@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:55:16 +0800 Subject: [PATCH 1056/1533] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Typo : std::men::replace -> std::mem::replace --- ...\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index d6d7e6c2ef..5baaef079b 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -545,7 +545,7 @@ impl Solution { let mut dummyHead = Box::new(ListNode::new(0)); dummyHead.next = head; let mut cur = dummyHead.as_mut(); - // 使用take()替换std::men::replace(&mut node.next, None)达到相同的效果,并且更普遍易读 + // 使用take()替换std::mem::replace(&mut node.next, None)达到相同的效果,并且更普遍易读 while let Some(nxt) = cur.next.take() { if nxt.val == val { cur.next = nxt.next; From 5ec766c81b2758341dd039f5204fd4f73c29164e Mon Sep 17 00:00:00 2001 From: paigeman <53284808+paigeman@users.noreply.github.com> Date: Tue, 26 Mar 2024 20:22:40 +0800 Subject: [PATCH 1057/1533] =?UTF-8?q?Update=20=E6=A0=88=E4=B8=8E=E9=98=9F?= =?UTF-8?q?=E5=88=97=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index 06a7827050..1a1fdbd21a 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -142,7 +142,7 @@ cd a/b/c/../../ 本题就要**使用优先级队列来对部分频率进行排序。** 注意这里是对部分数据进行排序而不需要对所有数据排序! -所以排序的过程的时间复杂度是$O(\log k)$,整个算法的时间复杂度是$O(n\log k)$。 +所以排序的过程的时间复杂度是 $O(\log k)$ ,整个算法的时间复杂度是 $O(n\log k)$ 。 ## 总结 From e56494b0828ae4d1cee61b48f03da00d0a13bc3c Mon Sep 17 00:00:00 2001 From: alain Date: Wed, 27 Mar 2024 15:28:42 +0800 Subject: [PATCH 1058/1533] =?UTF-8?q?fix=200035=5F=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE=20Rust=20=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...222\345\205\245\344\275\215\347\275\256.md" | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 80b7e40e4a..34415524c6 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int { ```rust impl Solution { - pub fn search_insert(nums: Vec, target: i32) -> i32 { - let mut left = 0; - let mut right = nums.len(); - while left < right { + pub fn search_insert(nums: Vec, target: i32) -> i32 { + use std::cmp::Ordering::{Equal, Greater, Less}; + let (mut left, mut right) = (0, nums.len() as i32 - 1); + while left <= right { let mid = (left + right) / 2; - match nums[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Equal => return ((left + right) / 2) as i32, - Ordering::Greater => right = mid, + match nums[mid as usize].cmp(&target) { + Less => left = mid + 1, + Equal => return mid, + Greater => right = mid - 1, } } - ((left + right) / 2) as i32 + right + 1 } } ``` From 304e85eaae8b5fd658e552abda950c17cab9f5c6 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 28 Mar 2024 17:12:11 +0800 Subject: [PATCH 1059/1533] Update --- README.md | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b25b102dfe..212a8b2a4e 100644 --- a/README.md +++ b/README.md @@ -507,24 +507,11 @@ # 关于作者 -大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 +大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术底层技术研发。 -加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。 - -如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦** +# PDF下载 +添加如下企业微信,会自动发送给大家PDF版本,顺便可以选择是否加入刷题群。
- -# 公众号 - -更多精彩文章持续更新,微信搜索:「代码随想录」第一时间围观,关注后回复:666,可以获得我的所有算法专题原创PDF。 - -**来看看就知道了,你会发现相见恨晚!** - - - -
- - From a0cfe9e7c883db1fea7d168e8aac4f14c504ebc8 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 28 Mar 2024 17:14:50 +0800 Subject: [PATCH 1060/1533] Update --- ...73\351\231\244\345\205\203\347\264\240.md" | 29 ------------------- 1 file changed, 29 deletions(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index dbde3d198c..3aadb67e83 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -119,35 +119,6 @@ public: * 时间复杂度:O(n) * 空间复杂度:O(1) -```CPP -/** -* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素 -* 时间复杂度:O(n) -* 空间复杂度:O(1) -*/ -class Solution { -public: - int removeElement(vector& nums, int val) { - int leftIndex = 0; - int rightIndex = nums.size() - 1; - while (leftIndex <= rightIndex) { - // 找左边等于val的元素 - while (leftIndex <= rightIndex && nums[leftIndex] != val){ - ++leftIndex; - } - // 找右边不等于val的元素 - while (leftIndex <= rightIndex && nums[rightIndex] == val) { - -- rightIndex; - } - // 将右边不等于val的元素覆盖左边等于val的元素 - if (leftIndex < rightIndex) { - nums[leftIndex++] = nums[rightIndex--]; - } - } - return leftIndex; // leftIndex一定指向了最终数组末尾的下一个元素 - } -}; -``` ## 相关题目推荐 From e3c4c20e5e08e7e8e10108315f517414297c30e0 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 28 Mar 2024 20:33:27 +0800 Subject: [PATCH 1061/1533] Update --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 212a8b2a4e..7c26d2c924 100644 --- a/README.md +++ b/README.md @@ -511,7 +511,9 @@ # PDF下载 -添加如下企业微信,会自动发送给大家PDF版本,顺便可以选择是否加入刷题群。 +添加如下企业微信,会自动发送给大家PDF版本,顺便可以选择是否加入刷题群。 + +添加微信记得备注,如果是已工作,备注:姓名-城市-岗位。如果学生,备注:姓名-学校-年级。**备注没有自我介绍不通过哦**
From 20db66e8fb5dbc39d7772184e19a98eb412f930e Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Fri, 29 Mar 2024 11:11:39 +0800 Subject: [PATCH 1062/1533] =?UTF-8?q?update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=20python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 520f17a7ec..7a37ad075a 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -467,6 +467,13 @@ class Solution: # 将列表转换成字符串 return " ".join(words) ``` +(版本三) 拆分字符串 + 反转列表 +```python +class Solution: + def reverseWords(self, s): + words = s.split() #type(words) --- list + words = words[::-1] # 反转单词 + return ' '.join(words) #列表转换成字符串 ### Go: From e5c4d56d01713495d0287b7a0a381e326f1b34f7 Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Fri, 29 Mar 2024 11:26:09 +0800 Subject: [PATCH 1063/1533] =?UTF-8?q?update=20kama55.=E5=8F=B3=E6=97=8B?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20python=20=E5=88=87=E7=89=87?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...263\346\227\213\345\255\227\347\254\246\344\270\262.md" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 7137186042..0188918e00 100644 --- "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -222,6 +222,13 @@ s = s[len(s)-k:] + s[:len(s)-k] print(s) ``` +```Python 切片法 +k = int(input()) +s = input() + +print(s[-k:] + s[:-k]) +``` + ### Go: ```go package main From ee99a72d18c6134c314337f7ac66f9232b9d4a61 Mon Sep 17 00:00:00 2001 From: heystone999 <52831724+heystone999@users.noreply.github.com> Date: Sat, 30 Mar 2024 15:51:43 -0400 Subject: [PATCH 1064/1533] =?UTF-8?q?feat(problem):=20=E6=9B=B4=E6=96=B001?= =?UTF-8?q?08.=E5=B0=86=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?= =?UTF-8?q?=E3=80=82=E6=B7=BB=E5=8A=A0=E4=BA=86Python=E9=80=92=E5=BD=92?= =?UTF-8?q?=E7=9A=84=E7=B2=BE=E7=AE=80=E7=89=88=EF=BC=8C=E8=87=AA=E8=BA=AB?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\217\211\346\220\234\347\264\242\346\240\221.md" | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 9fa684cfdf..dd2245db2f 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -334,6 +334,18 @@ class Solution: return root ``` +递归 精简(自身调用) +```python +class Solution: + def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: + if not nums: + return + mid = len(nums) // 2 + root = TreeNode(nums[mid]) + root.left = self.sortedArrayToBST(nums[:mid]) + root.right = self.sortedArrayToBST(nums[mid + 1 :]) + return root +``` 迭代法 ```python From 96e9fcb3adc8676fd6ea0671d0ddbb7d9291eaeb Mon Sep 17 00:00:00 2001 From: jiyongchao Date: Sun, 31 Mar 2024 11:19:54 +0800 Subject: [PATCH 1065/1533] =?UTF-8?q?Update=200017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\204\345\255\227\346\257\215\347\273\204\345\220\210.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index cbd99f8d93..0ad26bfda3 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -260,7 +260,7 @@ class Solution { } - //每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuilder + //每次迭代获取一个字符串,所以会涉及大量的字符串拼接,所以这里选择更为高效的 StringBuilder StringBuilder temp = new StringBuilder(); //比如digits如果为"23",num 为0,则str表示2对应的 abc @@ -274,7 +274,7 @@ class Solution { String str = numString[digits.charAt(num) - '0']; for (int i = 0; i < str.length(); i++) { temp.append(str.charAt(i)); - //c + //递归,处理下一层 backTracking(digits, numString, num + 1); //剔除末尾的继续尝试 temp.deleteCharAt(temp.length() - 1); From 9437047fae6056b05e731f16eb3d7625b9c5bed5 Mon Sep 17 00:00:00 2001 From: LingFenglong <2808021998@qq.com> Date: Sun, 31 Mar 2024 21:55:19 +0800 Subject: [PATCH 1066/1533] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix 300. 最长递增子序列 Java版本添加参数数组长度判断,现在可以AC --- ...270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 6d82eae1e9..199ae59e4b 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -129,6 +129,7 @@ public: ```Java class Solution { public int lengthOfLIS(int[] nums) { + if (nums.length <= 1) return nums.length; int[] dp = new int[nums.length]; int res = 1; Arrays.fill(dp, 1); @@ -137,8 +138,8 @@ class Solution { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } - res = Math.max(res, dp[i]); } + res = Math.max(res, dp[i]); } return res; } From 0c2cc182304c826ef49e25dceffe4e6dc1187762 Mon Sep 17 00:00:00 2001 From: Junhao Qu <869142649qq@gmail.com> Date: Mon, 1 Apr 2024 22:25:54 -0700 Subject: [PATCH 1067/1533] fix java version for linked list 19 --- ...225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index c7a2cfcb3f..167b44ba8b 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -106,7 +106,7 @@ public ListNode removeNthFromEnd(ListNode head, int n){ ListNode slowIndex = dummyNode; // 只要快慢指针相差 n 个结点即可 - for (int i = 0; i < n ; i++){ + for (int i = 0; i <= n ; i++){ fastIndex = fastIndex.next; } From 3193d16f53372a6bd9d294538a83ef3e5d92a87f Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Tue, 2 Apr 2024 14:53:01 +0800 Subject: [PATCH 1068/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00279.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E5=B9=B3=E6=96=B9=E6=95=B0=20Python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\271\263\346\226\271\346\225\260.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index f7c06dbd87..1b29626f7a 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -271,7 +271,27 @@ class Solution: # 返回结果 return dp[n] +``` +```python +class Solution(object): + def numSquares(self, n): + # 先把可以选的数准备好,更好理解 + nums, num = [], 1 + while num ** 2 <= n: + nums.append(num ** 2) + num += 1 + # dp数组初始化 + dp = [float('inf')] * (n + 1) + dp[0] = 0 + # 遍历准备好的完全平方数 + for i in range(len(nums)): + # 遍历背包容量 + for j in range(nums[i], n+1): + dp[j] = min(dp[j], dp[j-nums[i]]+1) + # 返回结果 + return dp[-1] + ``` ### Go: From c25c409c50096583962da6257e4edecc521d5fd7 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 2 Apr 2024 16:55:47 +0800 Subject: [PATCH 1069/1533] Update --- ...7.\350\257\276\347\250\213\350\241\250.md" | 51 +++ ...66\350\277\237\346\227\266\351\227\264.md" | 17 +- ...34\347\232\204\350\210\252\347\217\255.md" | 136 ++++++- ...21\347\232\204\345\237\216\345\270\202.md" | 46 +++ ...7\347\211\251\350\277\220\350\276\223I.md" | 371 ++++++++++++++++++ ...\347\211\251\350\277\220\350\276\223II.md" | 150 +++++++ ...347\211\251\350\277\220\350\276\223III.md" | 125 ++++++ ...17\345\221\230\347\256\200\345\216\206.md" | 8 +- 8 files changed, 891 insertions(+), 13 deletions(-) create mode 100644 "problems/0207.\350\257\276\347\250\213\350\241\250.md" create mode 100644 "problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" create mode 100644 "problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" create mode 100644 "problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" create mode 100644 "problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" diff --git "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" new file mode 100644 index 0000000000..18c4684081 --- /dev/null +++ "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" @@ -0,0 +1,51 @@ + +拓扑排序指的是一种 解决问题的大体思路, 而具体算法,可能是 广搜 可能是深搜。 + +大家可能发现 各式各样的解法,纠结哪个是拓扑排序? + +只要能在把 有向无环图 进行线性排序 的算法 都可以叫做 拓扑排序。 + +引用与任务调度,课程安排等等。 + +为什么 + + +----- + +「拓扑排序」是专门应用于有向图的算法; + +把一个 有向无环图 转成 线性的排序 就叫 拓扑排序。 + +拓扑排序(Kahn 算法,其实就是广度优先遍历的思路) + +这道题的做法同样适用于第 210 题。 + +------------------ + +``` +vector inDegree(numCourses); +unordered_map> map; +for (int i = 0; i < prerequisites.size(); i++) { + inDegree[prerequisites[i][0]]++;//当前课程入度值+1 + map[prerequisites[i][1]].push_back(prerequisites[i][0]);//添加依赖他的后续课 +} +queue Qu; +for (int i = 0; i < numCourses; i++) { + if (inDegree[i] == 0) Qu.push(i);//所有入度为0的课入列 +} +int count = 0; +while (Qu.size()) { + int selected = Qu.front(); //当前选的课 + Qu.pop();//出列 + count++;//选课数+1 + vector toEnQueue = map[selected];//获取这门课对应的后续课 + if (toEnQueue.size()) { //确实有后续课 + for (int i = 0; i < toEnQueue.size(); i++) { + inDegree[toEnQueue[i]]--; //依赖它的后续课的入度-1 + if (inDegree[toEnQueue[i]] == 0) Qu.push(toEnQueue[i]); //如果因此减为0,入列 + } + } +} +if (count == numCourses) return true; +return false; +``` diff --git "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" index d94406091b..e631951a9e 100644 --- "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" +++ "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" @@ -1194,8 +1194,7 @@ public: 至此通过 两篇dijkstra的文章,终于把 dijkstra 讲完了,如果大家对我讲解里所涉及的内容都吃透的话,详细对 dijkstra 算法也就理解到位了。 - -# Bellman_ford +这里在给出本题的Bellman_ford解法,关于 Bellman_ford ,后面我会专门来讲解的,Bellman_ford 有其独特的应用场景 ```CPP class Solution { @@ -1204,27 +1203,25 @@ public: int networkDelayTime(vector>& times, int n, int k) { vector minDist(n + 1 , INT_MAX/2); minDist[k] = 0; - vector minDist_copy(n); // 用来记录每一次遍历的结果 + //vector minDist_copy(n); // 用来记录每一次遍历的结果 for (int i = 1; i <= n + 1; i++) { - minDist_copy = minDist; // 获取上一次计算的结果 + //minDist_copy = minDist; // 获取上一次计算的结果 for (auto &f : times) { int from = f[0]; int to = f[1]; - int price = f[2]; - if (minDist[to] > minDist_copy[from] + price) minDist[to] = minDist_copy[from] + price; - } + int price = f[2]; + if (minDist[to] > minDist[from] + price) minDist[to] = minDist[from] + price; + } } - int result = 0; + int result = 0; for (int i = 1;i <= n; i++) { if (minDist[i] == INT_MAX/2) return -1;// 没有路径 result = max(minDist[i], result); } - return result; } }; - ``` diff --git "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" index c7b6236b78..1ece7116f2 100644 --- "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" +++ "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" @@ -1,4 +1,21 @@ +# 787. K 站中转内最便宜的航班 + +有 n 个城市通过一些航班连接。给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 pricei 抵达 toi。 + +现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ,并返回该价格。 如果不存在这样的路线,则输出 -1。 + + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319103900.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319103919.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319104026.png) + + +## 思路 + + ```CPP class Solution { @@ -13,7 +30,8 @@ public: int from = f[0]; int to = f[1]; int price = f[2]; - if (minDist[to] > minDist_copy[from] + price) minDist[to] = minDist_copy[from] + price; + minDist[to] = min(minDist_copy[from] + price, minDist[to]); + // if (minDist[to] > minDist_copy[from] + price) minDist[to] = minDist_copy[from] + price; } } @@ -23,6 +41,8 @@ public: }; ``` +下面是典型的错误写法 + ```CPP class Solution { public: @@ -42,3 +62,117 @@ public: } }; ``` + + +----------- + +SPFA + + +class Solution { +struct Edge { + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + +public: + int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { + vector minDist(n , INT_MAX/2); + vector> grid(n + 1); // 邻接表 + for (auto &f : flights) { + int from = f[0]; + int to = f[1]; + int price = f[2]; + grid[from].push_back(Edge(to, price)); + + } + minDist[src] = 0; + vector minDist_copy(n); // 用来记录每一次遍历的结果 + k++; + queue que; + que.push(src); + std::vector visited(n + 1, false); // 可加,可不加,加了效率高一些,防止重复访问 + int que_size; + while (k-- && !que.empty()) { + + minDist_copy = minDist; // 获取上一次计算的结果 + que_size = que.size(); + while (que_size--) { // 这个while循环的设计实在是妙啊 + int node = que.front(); que.pop(); + for (Edge edge : grid[node]) { + int from = node; + int to = edge.to; + int price = edge.val; + if (minDist[to] > minDist_copy[from] + price) { + minDist[to] = minDist_copy[from] + price; + que.push(to); + } + } + + } + } + int result = minDist[dst] == INT_MAX/2 ? -1 : minDist[dst]; + return result; + } +}; + + +----------------- + +队列加上 visited 不能重复访问 + +class Solution { +struct Edge { + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + +public: + int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { + vector minDist(n , INT_MAX/2); + vector> grid(n + 1); // 邻接表 + for (auto &f : flights) { + int from = f[0]; + int to = f[1]; + int price = f[2]; + grid[from].push_back(Edge(to, price)); + + } + minDist[src] = 0; + vector minDist_copy(n); // 用来记录每一次遍历的结果 + k++; + queue que; + que.push(src); + int que_size; + while (k-- && !que.empty()) { + // 注意这个数组放的位置 + vector visited(n + 1, false); // 可加,可不加,加了效率高一些,防止队列里重复访问,其数值已经算过了 + minDist_copy = minDist; // 获取上一次计算的结果 + que_size = que.size(); + while (que_size--) { + int node = que.front(); que.pop(); + for (Edge edge : grid[node]) { + int from = node; + int to = edge.to; + int price = edge.val; + if (minDist[to] > minDist_copy[from] + price) { + minDist[to] = minDist_copy[from] + price; + if(visited[to]) continue; // 不用重复放入队列,但需要重复计算,所以放在这里位置 + visited[to] = true; + que.push(to); + } + } + + } + } + int result = minDist[dst] == INT_MAX/2 ? -1 : minDist[dst]; + return result; + } +}; + + + diff --git "a/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" "b/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" new file mode 100644 index 0000000000..2156ee041d --- /dev/null +++ "b/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" @@ -0,0 +1,46 @@ + +floyd + + +class Solution { +public: + int findTheCity(int n, vector>& edges, int distanceThreshold) { + vector> grid(n, vector(n, 10005)); // 因为边的最大距离是10^4 + + // 节点到自己的距离为0 + for (int i = 0; i < n; i++) grid[i][i] = 0; + // 构造邻接矩阵 + for (const vector& e : edges) { + int from = e[0]; + int to = e[1]; + int val = e[2]; + grid[from][to] = val; + grid[to][from] = val; // 注意这里是双向图 + } + + // 开始 floyd + // 思考 为什么 p 要放在最外面一层 + for (int p = 0; p < n; p++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + grid[i][j] = min(grid[i][j], grid[i][p] + grid[p][j]); + } + } + } + + int result = 0; + int count = n + 10; // 记录所有城市在范围内连接的最小城市数量 + for (int i = 0; i < n; i++) { + int curCount = 0; // 统计一个城市在范围内可以连接几个城市 + for (int j = 0; j < n; j++) { + if (i != j && grid[i][j] <= distanceThreshold) curCount++; + // cout << "i:" << i << ", j:" << j << ", val: " << grid[i][j] << endl; + } + if (curCount <= count) { // 注意这里是 <= + count = curCount; + result = i; + } + } + return result; + } +}; diff --git "a/problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" new file mode 100644 index 0000000000..836ac34562 --- /dev/null +++ "b/problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -0,0 +1,371 @@ + +# 94. 城市间货物运输 I + +[题目链接](https://kamacoder.com/problempage.php?pid=1152) + +题目描述 + +某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 + + +网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。权值为正表示扣除了政府补贴后运输货物仍需支付的费用;权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 + + +请找出从城市 1 到城市 n 的所有可能路径中,综合政府补贴后的最低运输成本。如果最低运输成本是一个负数,它表示在遵循最优路径的情况下,运输过程中反而能够实现盈利。 + + +城市 1 到城市 n 之间可能会出现没有路径的情况,同时保证道路网络中不存在任何负权回路。 + +输入描述 + +第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 + +接下来为 m 行,每行包括三个整数,s、t 和 v,表示 s 号城市运输货物到达 t 号城市,道路权值为 v(单向图)。 + +输出描述 + +如果能够从城市 1 到连通到城市 n, 请输出一个整数,表示运输成本。如果该整数是负数,则表示实现了盈利。如果从城市 1 没有路径可达城市 n,请输出 "unconnected"。 + +输入示例: + +``` +6 7 +5 6 -2 +1 2 1 +5 3 1 +2 5 2 +2 4 -3 +4 6 4 +1 3 5 +``` + +## 思路 + +本题依然是最短路问题,求 从 节点1 到节点n 的最小费用。 但本题不同之处在于 边的权值是有负数的。 + +从 节点1 到节点n 的最小费用也可以是负数,费用如果是负数 则表示 运输的过程中 政府补贴大于运输成本。 + +在求单源最短路的方法中,使用dijkstra 的话,则要求图中边的权值都为正数。 + +我们在 [kama47.参会dijkstra朴素](./kama47.参会dijkstra朴素.md) 中专门有讲解,为什么有边为负数 使用dijkstra就不行了。 + +本题是经典的带负权值的单源最短路问题,此时就轮到Bellman_ford登场了,接下来我们来详细介绍Bellman_ford 算法 如何解决这类问题。 + +> 该算法是由 R.Bellman 和L.Ford 在20世纪50年代末期发明的算法,故称为Bellman_ford算法。 + +**Bellman_ford算法的核心思想是 对所有边进行松弛n-1次操作(n为节点数量),从而求得目标最短路**。 + + +看到这里,估计大家都比较晕了,为什么是 n-1 次,那“松弛”这两个字究竟是个啥意思? + +我们先来说什么是 “松弛”。 + +《算法四》里面把这个操作叫做 “放松”, 英文版里叫做 “relax the edge” + +所以大家翻译过来,就是 “放松” 或者 “松弛” 。 + +但《算法四》没有具体去讲这个 “放松” 究竟是个啥? 网上的题解也没有讲题解里的 “松弛这条边,松弛所有边”等等 里面的 “松弛” 究竟是什么意思? + +这里我给大家举一个例子,每条边有起点、终点和边的权值。例如一条边,节点A 到 节点B 权值为value,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240327102620.png) + +minDist[B] 表示 到达B节点 最小权值,minDist[B] 有哪些状态可以推出来? + +状态一: minDist[A] + value 可以推出 minDist[B] +状态二: minDist[B]本身就有权值 (可能是其他边链接的节点B 例如节点C,以至于 dp[B]记录了其他边到dp[B]的权值) + +那么minDist[B] 应为如何取舍。 + +本题我们要求最小权值,那么 这两个状态我们就取最小的 + +``` +if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value + +``` + +也就是说,如果 通过 A 到 B 这条边可以获得更短的到达B节点的路径,即如果 `minDist[B] > minDist[A] + value`,那么我们就更新 `minDist[B] = minDist[A] + value` ,**这个过程就叫做 “松弛**” 。 + +以上讲了这么多,其实都是围绕以下这句代码展开: + +``` +if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value + +``` + +**这句代码就是 Bellman_ford算法的核心操作**。 + +以上代码也可以这么写:`minDist[B] = min(minDist[A] + value, minDist[B]) ` + +如果大家看过代码随想录的动态规划章节,会发现 无论是背包问题还是子序列问题,这段代码(递推公式)出现频率非常高的。 + +其实 Bellman_ford算法 也是采用了动态规划的思想,即:将一个问题分解成多个决策阶段,通过状态之间的递归关系最后计算出全局最优解。 + +(如果理解不了动态规划的思想也无所谓,理解我上面讲的松弛操作就好) + +**那么为什么是 n - 1次 松弛呢**? + +这里要给大家模拟一遍 Bellman_ford 的算法才行,接下来我们来看看对所有边松弛 n -1 次的操作是什么样的。 + +我们依然使用**minDist数组来表达 起点到各个节点的最短距离**,例如minDist[3] = 5 表示起点到达节点3 的最小距离为5 + +### 模拟过程 + +初始化过程。 + +起点为节点1, 起点到起点的距离为0,所以 minDist[1] 初始化为0 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240328104119.png) + +其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 + + +对所有边 进行第一次松弛: (什么是松弛,在上面我已经详细讲过) + +以示例给出的所有边为例: + +``` +5 6 -2 +1 2 1 +5 3 1 +2 5 2 +2 4 -3 +4 6 4 +1 3 5 +``` + +接下来我们来松弛一遍所有的边。 + +边:节点5 -> 节点6,权值为-2 ,minDist[5] 还是默认数值max,所以不能基于 节点5 去更新节点6,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113537.png) + +(在复习一下,minDist[5] 表示起点到节点5的最短距离) + + +边:节点1 -> 节点2,权值为1 ,minDist[2] > minDist[1] + 1 ,更新 minDist[2] = minDist[1] + 1 = 0 + 1 = 1 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113703.png) + +边:节点5 -> 节点3,权值为1 ,minDist[5] 还是默认数值max,所以不能基于节点5去更新节点3 如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113827.png) + + +边:节点2 -> 节点5,权值为2 ,minDist[5] > minDist[2] + 2 (经过上面的计算minDist[2]已经不是默认值,而是 1),更新 minDist[5] = minDist[2] + 2 = 1 + 2 = 3 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113927.png) + + +边:节点2 -> 节点4,权值为-3 ,minDist[4] > minDist[2] + 2,更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114036.png) + +边:节点4 -> 节点6,权值为4 ,minDist[6] > minDist[4] + 4,更新 minDist[6] = minDist[4] + 4 = -2 + 4 = 2 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114120.png) + +边:节点1 -> 节点3,权值为5 ,minDist[3] > minDist[1] + 5,更新 minDist[3] = minDist[1] + 5 = 0 + 5 = 5 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114324.png) + +-------- + +以上是对所有边进行一次松弛之后的结果。 + +那么需要对所有边松弛几次才能得到 起点(节点1) 到终点(节点6)的最短距离呢? + +**对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**。 + +上面的距离中,我们得到里 起点达到 与起点一条边相邻的节点2 和 节点3 的最短距离,分别是 minDist[2] 和 minDist[3] + +这里有录友疑惑了 minDist[3] = 5,分明不是 起点到达 节点3 的最短距离,节点1 -> 节点2 -> 节点5 -> 节点3 这条路线 距离才是4。 + +注意我上面讲的是 **对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**,这里 说的是 一条边相连的节点。 + +与起点(节点1)一条边相邻的节点,到达节点2 最短距离是 1,到达节点3 最短距离是5。 + +而 节点1 -> 节点2 -> 节点5 -> 节点3 这条路线 是 与起点 三条边相连的路线了。 + +所以对所有边松弛一次 能得到 与起点 一条边相连的节点最短距离。 + +那对所有边松弛两次 可以得到与起点 两条边相连的节点的最短距离。 + +那对所有边松弛三次 可以得到与起点 三条边相连的节点的最短距离,这个时候,我们就能得到到达节点3真正的最短距离,也就是 节点1 -> 节点2 -> 节点5 -> 节点3 这条路线。 + +那么再回归刚刚的问题,**需要对所有边松弛几次才能得到 起点(节点1) 到终点(节点6)的最短距离呢**? + +节点数量为n,那么起点到终点,最多是 n-1 条边相连。 + +那么无论图是什么样的,边是什么样的顺序,我们对所有边松弛 n-1 次 就一定能得到 起点到达 终点的最短距离。 + +其实也同时计算出了,起点 到达 所有节点的最短距离,因为所有节点与起点连接的变数最多也就是 n-1条边。 + +截止到这里,Bellman_ford 的核心算法思路,大家就了解的差不多了。 + +共有两个关键点。 + +* “松弛”究竟是个啥 +* 为什么要对所有边松弛 n - 1 次 (n为节点个数) + +那么Bellman_ford的解题解题过程其实就是对所有边松弛 n-1 次,然后得出得到终点的最短路径。 + + + +### 代码 + +理解上面讲解的内容,代码就更容易写了,本题代码如下:(详细注释) + +```CPP +#include +#include +#include +#include +using namespace std; + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid; + + // 将所有边保存起来 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid.push_back({p1, p2, val}); + + } + int start = 1; // 起点 + int end = n; // 终点 + + vector minDist(n + 1 , INT_MAX); + minDist[start] = 0; + for (int i = 1; i < n; i++) { // 对所有边 松弛 n-1 次 + for (vector &side : grid) { // 每一次松弛,都是对所有边进行松弛 + int from = side[0]; // 边的出发点 + int to = side[1]; // 边的到达点 + int price = side[2]; // 边的权值 + // 松弛操作 + // minDist[from] != INT_MAX 防止从未计算过的节点出发 + if (minDist[from] != INT_MAX && minDist[to] > minDist[from] + price) { + minDist[to] = minDist[from] + price; + } + } + } + if (minDist[end] == INT_MAX) cout << "unconnected" << endl; // 不能到达终点 + else cout << minDist[end] << endl; // 到达终点最短路径 + +} +``` + +### 拓展 + +有录友可能会想,那我 松弛 n 次,松弛 n + 1次,松弛 2 * n 次会怎么样? + +其实没啥影响,结果不会变的,因为 题目中说了 “同时保证道路网络中不存在任何负权回路” 也就是图中没有 负权回路(在有向图中出现有向环 且环的总权值为负数)。 + +那么我们只要松弛 n - 1次 就一定能得到结果,没必要在松弛更多次了。 + +这里有疑惑的录友,可以加上打印 minDist数组 的日志,尝试一下,看看松弛 n 次会怎么样。 +你会发现 松弛 大于 n - 1次,minDist数组 就不会变化了。 + +这里我给出打印日志的代码: + +```CPP +#include +#include +#include +#include +using namespace std; + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid; + + // 将所有边保存起来 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid.push_back({p1, p2, val}); + + } + int start = 1; // 起点 + int end = n; // 终点 + + vector minDist(n + 1 , INT_MAX); + minDist[start] = 0; + for (int i = 1; i < n; i++) { // 对所有边 松弛 n-1 次 + for (vector &side : grid) { // 每一次松弛,都是对所有边进行松弛 + int from = side[0]; // 边的出发点 + int to = side[1]; // 边的到达点 + int price = side[2]; // 边的权值 + // 松弛操作 + // minDist[from] != INT_MAX 防止从未计算过的节点出发 + if (minDist[from] != INT_MAX && minDist[to] > minDist[from] + price) { + minDist[to] = minDist[from] + price; + } + } + cout << "对所有边松弛 " << i << "次" << endl; + for (int k = 1; k <= n; k++) { + cout << minDist[k] << " "; + } + cout << endl; + } + if (minDist[end] == INT_MAX) cout << "unconnected" << endl; // 不能到达终点 + else cout << minDist[end] << endl; // 到达终点最短路径 + +} + +``` + +通过打日志,大家发现,怎么对所有边进行第二次松弛以后结果就 不再变化了,那根本就不用松弛 n - 1啊? + +这是本题的样例的特殊性, 松弛 n-1次 是保证对任何图 都能最后求得到终点的最小距离。 + +如果还想不明白 我再举一个例子,用以下测试用例再跑一下。 + + +``` +6 5 +5 6 1 +4 5 1 +3 4 1 +2 3 1 +1 2 1 +``` + +打印结果: + +``` +对所有边松弛 1次 +0 1 2147483647 2147483647 2147483647 2147483647 +对所有边松弛 2次 +0 1 2 2147483647 2147483647 2147483647 +对所有边松弛 3次 +0 1 2 3 2147483647 2147483647 +对所有边松弛 4次 +0 1 2 3 4 2147483647 +对所有边松弛 5次 +0 1 2 3 4 5 +``` + +你会发现到 n-1 次 打印出最后的最短路结果。 + +关于上面的讲解,大家已经要多写代码去实验,验证自己的想法。 + +至于 负权回路 ,我在下一篇会专门讲解这种情况,大家有个印象就好。 + + +## 总结 + +Bellman_ford 是可以计算 负权值的单源最短路算法。 + +其算法核心思路是对 所有边进行 n-1 次 松弛。 + +弄清楚 什么是 松弛? 为什么要 n-1 次? 对理解Bellman_ford 非常重要。 + diff --git "a/problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" new file mode 100644 index 0000000000..7a76d3615d --- /dev/null +++ "b/problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -0,0 +1,150 @@ + +# 95. 城市间货物运输 II + +[题目链接](https://kamacoder.com/problempage.php?pid=1153) + +【题目描述】 + +某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 + +网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。权值为正表示扣除了政府补贴后运输货物仍需支付的费用; + +权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 + +然而,在评估从城市 1 到城市 n 的所有可能路径中综合政府补贴后的最低运输成本时,存在一种情况:图中可能出现负权回路。 + +负权回路是指一系列道路的总权值为负,这样的回路使得通过反复经过回路中的道路,理论上可以无限地减少总成本或无限地增加总收益。 + +为了避免货物运输商采用负权回路这种情况无限的赚取政府补贴,算法还需检测这种特殊情况。 + +请找出从城市 1 到城市 n 的所有可能路径中,综合政府补贴后的最低运输成本。同时能够检测并适当处理负权回路的存在。 + +城市 1 到城市 n 之间可能会出现没有路径的情况 + +【输入描述】 + +第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 + +接下来为 m 行,每行包括三个整数,s、t 和 v,表示 s 号城市运输货物到达 t 号城市,道路权值为 v。 + +【输出描述】 + +如果没有发现负权回路,则输出一个整数,表示从城市 1 到城市 n 的最低运输成本(包括政府补贴)。 + +如果该整数是负数,则表示实现了盈利。如果发现了负权回路的存在,则输出 "circle"。如果从城市 1 无法到达城市 n,则输出 "unconnected"。 + + +输入示例 + +``` +4 4 +1 2 -1 +2 3 1 +3 1 -1 +3 4 1 +``` + +输出示例 + +``` +circle +``` + +## 思路 + +本题是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 延伸题目。 + +本题是要我们判断 负权回路,也就是图中出现环且环上的边总权值为负数。 + +如果在这样的图中求最短路的话, 就会在这个环里无限循环 (也是负数+负数 只会越来越小),无法求出最短路径。 + +所以对于 在有负权值的图中求最短路,都需要先看看这个图里有没有负权回路。 + +接下来我们来看 如何使用 bellman_ford 算法来判断 负权回路。 + +在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 中 我们讲了 bellman_ford 算法的核心就是一句话:对 所有边 进行 n-1 次松弛。 同时文中的 【拓展】部分, 我们也讲了 松弛n次以上 会怎么样? + +在没有负权回路的图中,松弛 n 次以上 ,结果不会有变化。 + +但本题有 负权回路,如果松弛 n 次,结果就会有变化了,因为 有负权回路 就是可以无限最短路径(一直绕圈,就可以一直得到无限小的最短距离)。 + +那么每松弛一次,都会更新最短路径,所以结果会一直有变化。 + +(如果对于 bellman_ford 不了解的录友,建议详细看这里:[kama94.城市间货物运输I](./kama94.城市间货物运输I.md)) + +以上为理论分析,接下来我们再画图举例。 + +我们拿题目中示例来画一个图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402103135.png) + +图中 节点1 到 节点4 的最短路径是多少(题目中的最低运输成本) (注意边可以为负数的) + +节点1 -> 节点2 -> 节点3 -> 节点4,这样的路径总成本为 -1 + 1 + 1 = 1 + +而图中有负权回路: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402103712.png) + +那么我们在负权回路中多绕一圈,我们的最短路径 是不是就更小了 (也就是更低的运输成本) + +节点1 -> 节点2 -> 节点3 -> 节点1 -> 节点2 -> 节点3 -> 节点4,这样的路径总成本 (-1) + 1 + (-1) + (-1) + 1 + (-1) + 1 = -1 + +如果在负权回路多绕两圈,三圈,无穷圈,那么我们的总成本就会无限小, 如果要求最小成本的话,你会发现本题就无解了。 + +在 bellman_ford 算法中,松弛 n-1 次所有的边 就可以求得 起点到任何节点的最短路径,松弛 n 次以上,minDist数组(记录起到到其他节点的最短距离)中的结果也不会有改变 (如果对 bellman_ford 算法 不了解,也不知道 minDist 是什么,建议详看上篇讲解[kama94.城市间货物运输I](./kama94.城市间货物运输I.md)) + +而本题有负权回路的情况下,一直都会有更短的最短路,所以 松弛 第n次,minDist数组 也会发生改变。 + +那么解决本题的 核心思路,就是在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 的基础上,再多松弛一次,看minDist数组 是否发生变化。 + +代码和 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 基本是一样的,如下:(关键地方已注释) + +```CPP +#include +#include +#include +#include +using namespace std; + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid; + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid.push_back({p1, p2, val}); + + } + int start = 1; // 起点 + int end = n; // 终点 + + vector minDist(n + 1 , INT_MAX); + minDist[start] = 0; + bool flag = false; + for (int i = 1; i <= n; i++) { // 这里我们松弛n次,最后一次判断负权回路 + for (vector &side : grid) { + int from = side[0]; + int to = side[1]; + int price = side[2]; + if (i < n) { + if (minDist[from] != INT_MAX && minDist[to] > minDist[from] + price) minDist[to] = minDist[from] + price; + } else { // 多加一次松弛判断负权回路 + if (minDist[from] != INT_MAX && minDist[to] > minDist[from] + price) flag = true; + + } + } + + } + + if (flag) cout << "circle" << endl; + else if (minDist[end] == INT_MAX) { + cout << "unconnected" << endl; + } else { + cout << minDist[end] << endl; + } +} +``` diff --git "a/problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" new file mode 100644 index 0000000000..1fdba49a37 --- /dev/null +++ "b/problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -0,0 +1,125 @@ + +# 96. 城市间货物运输 III + +[题目链接](https://kamacoder.com/problempage.php?pid=1154) + +【题目描述】 + +某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 + +网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。 + +权值为正表示扣除了政府补贴后运输货物仍需支付的费用; + +权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 + +请计算在最多经过 k 个城市的条件下,从城市 src 到城市 dst 的最低运输成本。 + +【输入描述】 + +第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 + +接下来为 m 行,每行包括三个整数,s、t 和 v,表示 s 号城市运输货物到达 t 号城市,道路权值为 v。 + +最后一行包含三个正整数,src、dst、和 k,src 和 dst 为城市编号,从 src 到 dst 经过的城市数量限制。 + +【输出描述】 + +输出一个整数,表示从城市 src 到城市 dst 的最低运输成本,如果无法在给定经过城市数量限制下找到从 src 到 dst 的路径,则输出 "unreachable",表示不存在符合条件的运输方案。 + +输入示例: + +``` +6 7 +1 2 1 +2 4 -3 +2 5 2 +1 3 5 +3 5 1 +4 6 4 +5 6 -2 +2 6 1 +``` + +输出示例: + +``` +0 +``` + +## 思路 + +本题为单源有限最短路问题,同样是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 延伸题目。 + +在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 中我们讲了:**对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**。 + +节点数量为n,那么起点到终点,最多是 n-1 条边相连。 那么对所有边松弛 n-1 次 就一定能得到 起点到达 终点的最短距离。 + +(如果对以上讲解看不懂,建议详看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) ) + +本题是最多经过 k 个城市, 那么是 k + 1条边相连的节点。 这里可能有录友想不懂为什么是k + 1,来看这个图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402115614.png) + +图中,节点2 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 + +所以本题就是求,起点最多经过k + 1 条边到达终点的最短距离。 + + +对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离,那么对所有边松弛 k + 1次,就是求 起点到达 与起点k + 1条边相连的节点的 最短距离。 + +如果 终点数值没有被计算覆盖,那就是无法到达。 + +**注意**: 本题是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 的拓展题,如果对 bellman_ford 没有深入了解,强烈建议先看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 再做本题。 + +理解以上内容,其实本题代码就很容易了,bellman_ford 标准写法是松弛 n-1 次,本题就松弛 k + 1次就好。 + +如果大家理解后,建议先自己写写代码,提交一下看看,因为 这里还有一个坑,如果不自己去试试,体会就不够深刻了。 + + + +代码如下: (关键地方详细注释) + +```CPP +#include +#include +#include +#include +using namespace std; + +int main() { + int src, dst,k ,p1, p2, val ,m , n; + + cin >> n >> m; + + vector> grid; + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid.push_back({p1, p2, val}); + } + + cin >> src >> dst >> k; + + vector minDist(n + 1 , INT_MAX); + minDist[src] = 0; + vector minDist_copy(n + 1); // 用来记录每一次遍历的结果 + for (int i = 1; i <= k + 1; i++) { + minDist_copy = minDist; // 获取上一次计算的结果 + for (vector &side : grid) { + int from = side[0]; + int to = side[1]; + int price = side[2]; + //cout << f[0] << " " << f[1] << " " << f[2] << endl; + if (minDist_copy[from] != INT_MAX && minDist[to] > minDist_copy[from] + price) minDist[to] = minDist_copy[from] + price; + } + + } + + if (minDist[dst] == INT_MAX) cout << "unreachable" << endl; // 不能到达终点 + else cout << minDist[dst] << endl; // 到达终点最短路径 + +} + +``` diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" index fc06c713a0..7fbfa1fd2b 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" @@ -99,7 +99,7 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 面试只有短短的30分钟或者一个小时,如何把自己掌握的技术更好的展现给面试官呢,博客、github都是很好的选择,如果把这些放在简历上,面试官一定会看的,这都是加分项。 -## 简历模板 +## 领取方式 最后福利,把我的简历模板贡献出来!如下图所示。 @@ -107,7 +107,11 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 这里是简历模板中Markdown的代码:[https://github.com/youngyangyang04/Markdown-Resume-Template](https://github.com/youngyangyang04/Markdown-Resume-Template) ,可以fork到自己Github仓库上,按照这个模板来修改自己的简历。 -**Word版本的简历,大家可以在公众号「代码随想录」后台回复:简历模板,就可以获取!** +**Word版本的简历,添加如下企业微信,通过之后就会发你word版本**。 + +
+ +如果已经有我的企业微信,直接回复:简历模板,就可以了。 ## 总结 From fb03de4c4ed82d239102718565c4734e01a211ac Mon Sep 17 00:00:00 2001 From: carlvine500 Date: Tue, 2 Apr 2024 17:46:11 +0800 Subject: [PATCH 1070/1533] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0236 java简洁迭代解法 --- ...54\345\205\261\347\245\226\345\205\210.md" | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 049f70c7ae..7e0a12fa01 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -247,7 +247,7 @@ public: ### Java - +递归 ```Java class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { @@ -271,6 +271,47 @@ class Solution { } } +``` +迭代 +```Java +class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + int max = Integer.MAX_VALUE; + Stack st = new Stack<>(); + TreeNode cur = root, pre = null; + while (cur != null || !st.isEmpty()) { + while (cur != null) { + st.push(cur); + cur = cur.left; + } + cur = st.pop(); + if (cur.right == null || cur.right == pre) { + // p/q是 中/左 或者 中/右 , 返回中 + if (cur == p || cur == q) { + if ((cur.left != null && cur.left.val == max) || (cur.right != null && cur.right.val == max)) { + return cur; + } + cur.val = max; + } + // p/q是 左/右 , 返回中 + if (cur.left != null && cur.left.val == max && cur.right != null && cur.right.val == max) { + return cur; + } + // MAX_VALUE 往上传递 + if ((cur.left != null && cur.left.val == max) || (cur.right != null && cur.right.val == max)) { + cur.val = max; + } + pre = cur; + cur = null; + } else { + st.push(cur); + cur = cur.right; + } + } + return null; + } +} + ``` ### Python From 1810d98f8149fa219ea9289bd014c3c3c52857e7 Mon Sep 17 00:00:00 2001 From: carlvine500 Date: Tue, 2 Apr 2024 17:59:18 +0800 Subject: [PATCH 1071/1533] =?UTF-8?q?Update=200037.=E8=A7=A3=E6=95=B0?= =?UTF-8?q?=E7=8B=AC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0037.解数独 java bitmap解法 --- ...7.\350\247\243\346\225\260\347\213\254.md" | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index b5f54b1f8f..4dc15006aa 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -224,7 +224,7 @@ public: ### Java - +解法一: ```java class Solution { public void solveSudoku(char[][] board) { @@ -291,7 +291,73 @@ class Solution { } } ``` +解法二(bitmap标记) +``` +class Solution{ + int[] rowBit = new int[9]; + int[] colBit = new int[9]; + int[] square9Bit = new int[9]; + + public void solveSudoku(char[][] board) { + // 1 10 11 + for (int y = 0; y < board.length; y++) { + for (int x = 0; x < board[y].length; x++) { + int numBit = 1 << (board[y][x] - '1'); + rowBit[y] ^= numBit; + colBit[x] ^= numBit; + square9Bit[(y / 3) * 3 + x / 3] ^= numBit; + } + } + backtrack(board, 0); + } + + public boolean backtrack(char[][] board, int n) { + if (n >= 81) { + return true; + } + + // 快速算出行列编号 n/9 n%9 + int row = n / 9; + int col = n % 9; + + if (board[row][col] != '.') { + return backtrack(board, n + 1); + } + + for (char c = '1'; c <= '9'; c++) { + int numBit = 1 << (c - '1'); + if (!isValid(numBit, row, col)) continue; + { + board[row][col] = c; // 当前的数字放入到数组之中, + rowBit[row] ^= numBit; // 第一行rowBit[0],第一个元素eg: 1 , 0^1=1,第一个元素:4, 100^1=101,... + colBit[col] ^= numBit; + square9Bit[(row / 3) * 3 + col / 3] ^= numBit; + } + if (backtrack(board, n + 1)) return true; + { + board[row][col] = '.'; // 不满足条件,回退成'.' + rowBit[row] &= ~numBit; // 第一行rowBit[0],第一个元素eg: 1 , 101&=~1==>101&111111110==>100 + colBit[col] &= ~numBit; + square9Bit[(row / 3) * 3 + col / 3] &= ~numBit; + } + } + return false; + } + + boolean isValid(int numBit, int row, int col) { + // 左右 + if ((rowBit[row] & numBit) > 0) return false; + // 上下 + if ((colBit[col] & numBit) > 0) return false; + // 9宫格: 快速算出第n个九宫格,编号[0,8] , 编号=(row / 3) * 3 + col / 3 + if ((square9Bit[(row / 3) * 3 + col / 3] & numBit) > 0) return false; + return true; + } + +} + +``` ### Python ```python From 0422b137a2b04e74f757b2564a5e5bed3e921d2f Mon Sep 17 00:00:00 2001 From: ShimenTian <64670708+ShimenTian@users.noreply.github.com> Date: Tue, 2 Apr 2024 22:35:58 +0800 Subject: [PATCH 1072/1533] =?UTF-8?q?Update=20=E5=85=B3=E4=BA=8E=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=EF=BC=8C=E4=BD=A0=E4=B8=8D?= =?UTF-8?q?=E7=9F=A5=E9=81=93=E7=9A=84=E9=83=BD=E5=9C=A8=E8=BF=99=E9=87=8C?= =?UTF-8?q?=EF=BC=81.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\275\345\234\250\350\277\231\351\207\214\357\274\201.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index 9d00a0f2a0..61f0a7ef6a 100644 --- "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -19,7 +19,7 @@ **时间复杂度是一个函数,它定性描述该算法的运行时间**。 -我们在软件开发中,时间复杂度就是用来方便开发者估算出程序运行的答题时间。 +我们在软件开发中,时间复杂度就是用来方便开发者估算出程序运行的大体时间。 那么该如何估计程序运行时间呢,通常会估算算法的操作单元数量来代表程序消耗的时间,这里默认CPU的每个单元运行消耗的时间都是相同的。 @@ -42,7 +42,7 @@ 我们主要关心的还是一般情况下的数据形式。 -**面试中说道算法的时间复杂度是多少指的都是一般情况**。但是如果面试官和我们深入探讨一个算法的实现以及性能的时候,就要时刻想着数据用例的不一样,时间复杂度也是不同的,这一点是一定要注意的。 +**面试中说的算法的时间复杂度是多少指的都是一般情况**。但是如果面试官和我们深入探讨一个算法的实现以及性能的时候,就要时刻想着数据用例的不一样,时间复杂度也是不同的,这一点是一定要注意的。 ## 不同数据规模的差异 @@ -61,7 +61,7 @@ 例如上图中20就是那个点,n只要大于20 常数项系数已经不起决定性作用了。 -**所以我们说的时间复杂度都是省略常数项系数的,是因为一般情况下都是默认数据规模足够的大,基于这样的事实,给出的算法时间复杂的的一个排行如下所示**: +**所以我们说的时间复杂度都是省略常数项系数的,是因为一般情况下都是默认数据规模足够的大,基于这样的事实,给出的算法时间复杂度的一个排行如下所示**: O(1)常数阶 < O(logn)对数阶 < O(n)线性阶 < O(nlogn)线性对数阶 < O(n^2)平方阶 < O(n^3)立方阶 < O(2^n)指数阶 From 6dd9d117ffc60cb6854f26814b05aa5be2033e84 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Tue, 2 Apr 2024 23:58:58 +0800 Subject: [PATCH 1073/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00139.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8B=86=E5=88=86=20Python=20DP=E5=89=AA=E6=9E=9D?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\350\257\215\346\213\206\345\210\206.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index a3d59ec718..5ad0fcb005 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -394,7 +394,28 @@ class Solution: dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j]) return dp[len(s)] ``` +DP(剪枝) +```python +class Solution(object): + def wordBreak(self, s, wordDict): + + # 先对单词按长度排序 + wordDict.sort(key=lambda x: len(x)) + n = len(s) + dp = [False] * (n + 1) + dp[0] = True + # 遍历背包 + for i in range(1, n + 1): + # 遍历单词 + for word in wordDict: + # 简单的 “剪枝” + if len(word) > i: + break + dp[i] = dp[i] or (dp[i - len(word)] and s[i - len(word): i] == word) + return dp[-1] + +``` ### Go: From fda18c06b7221b1c7bddd6b4115de5549e4fd5a4 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Wed, 3 Apr 2024 22:23:48 +0800 Subject: [PATCH 1074/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C?= =?UTF-8?q?=E5=8C=85=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=A4=9A=E9=87=8D=E8=83=8C=E5=8C=85=20Python=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\351\207\215\350\203\214\345\214\205.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index 5d6440e3bf..29f157fbcc 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -204,6 +204,29 @@ class multi_pack{ ``` ### Python: +```python + +C, N = input().split(" ") +C, N = int(C), int(N) + +# value数组需要判断一下非空不然过不了 +weights = [int(x) for x in input().split(" ")] +values = [int(x) for x in input().split(" ") if x] +nums = [int(x) for x in input().split(" ")] + +dp = [0] * (C + 1) +# 遍历背包容量 +for i in range(N): + for j in range(C, weights[i] - 1, -1): + for k in range(1, nums[i] + 1): + # 遍历 k,如果已经大于背包容量直接跳出循环 + if k * weights[i] > j: + break + dp[j] = max(dp[j], dp[j - weights[i] * k] + values[i] * k) +print(dp[-1]) + +``` + ### Go: From 616258da2083e1c9943364acbc98cfdc83d0e0fc Mon Sep 17 00:00:00 2001 From: Xiong Gu Date: Thu, 4 Apr 2024 14:57:42 -0400 Subject: [PATCH 1075/1533] add dfs function operation in the main traversal function --- ...2\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 4621d4a739..e22e1bf343 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -189,7 +189,7 @@ class Solution: res.append(node.val) dfs(node.left) dfs(node.right) - + dfs(root) return res ``` @@ -207,6 +207,7 @@ class Solution: res.append(node.val) dfs(node.right) + dfs(root) return res ``` ```python @@ -224,7 +225,7 @@ class Solution: dfs(node.left) dfs(node.right) res.append(node.val) - + dfs(root) return res ``` From 2d0b16c92d6aaebf49b56d996a4ea5dd7651e8ac Mon Sep 17 00:00:00 2001 From: Xiong Gu Date: Thu, 4 Apr 2024 15:00:23 -0400 Subject: [PATCH 1076/1533] formatting the former commit --- ...32\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index e22e1bf343..b18067e8b7 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -189,6 +189,7 @@ class Solution: res.append(node.val) dfs(node.left) dfs(node.right) + dfs(root) return res @@ -206,7 +207,7 @@ class Solution: dfs(node.left) res.append(node.val) dfs(node.right) - + dfs(root) return res ``` @@ -225,6 +226,7 @@ class Solution: dfs(node.left) dfs(node.right) res.append(node.val) + dfs(root) return res ``` From 1c4b9a252fd004a9ec3771b6dbfc707818e4f1d4 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Sat, 6 Apr 2024 10:38:12 +0800 Subject: [PATCH 1077/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00583=20=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=20Python=E8=A7=A3=E6=B3=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\351\231\244\346\223\215\344\275\234.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 7dbb8ef542..4816c37cc2 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -234,6 +234,25 @@ class Solution: return dp[-1][-1] ``` +> 版本 2 + +```python +class Solution(object): + def minDistance(self, word1, word2): + m, n = len(word1), len(word2) + + # dp 求解两字符串最长公共子序列 + dp = [[0] * (n+1) for _ in range(m+1)] + for i in range(1, m+1): + for j in range(1, n+1): + if word1[i-1] == word2[j-1]: + dp[i][j] = dp[i-1][j-1] + 1 + else: + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + + # 删去最长公共子序列以外元素 + return m + n - 2 * dp[-1][-1] +``` ### Go: ```go From 7df5157aab58935f0dd9784e4968bd73f1facbdf Mon Sep 17 00:00:00 2001 From: Jasen Chao <68803975+JasenChao@users.noreply.github.com> Date: Sat, 6 Apr 2024 15:09:55 +0800 Subject: [PATCH 1078/1533] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix typos --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 02edad4d8b..f030ae77e3 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -101,7 +101,7 @@ public: int sum = 0; for (int i = 0; i < nums.size(); i++) sum += nums[i]; if (S > sum) return 0; // 此时没有方案 - if ((S + sum) % 2) return 0; // 此时没有方案,两个int相加的时候要各位小心数值溢出的问题 + if ((S + sum) % 2) return 0; // 此时没有方案,两个int相加的时候要格外小心数值溢出的问题 int bagSize = (S + sum) / 2; // 转变为组合总和问题,bagsize就是要求的和 // 以下为回溯法代码 From 74d38fc6965445e35946d06300d60ff2c5d0a130 Mon Sep 17 00:00:00 2001 From: Jingqi Sun <71240100+xiaoqingma0@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:06:39 +0800 Subject: [PATCH 1079/1533] =?UTF-8?q?Update=20=E5=93=88=E5=B8=8C=E8=A1=A8?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改typo,优化句子表达。 --- ...\250\347\220\206\350\256\272\345\237\272\347\241\200.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index e426c65791..816f673e91 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -12,11 +12,11 @@ ## 哈希表 -首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 +首先什么是哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 > 哈希表是根据关键码的值而直接进行访问的数据结构。 -这么这官方的解释可能有点懵,其实直白来讲其实数组就是一张哈希表。 +这么官方的解释可能有点懵,其实直白来讲其实数组就是一张哈希表。 哈希表中关键码就是数组的索引下标,然后通过下标直接访问数组中的元素,如下图所示: @@ -113,7 +113,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 其他语言例如:java里的HashMap ,TreeMap 都是一样的原理。可以灵活贯通。 -虽然std::set、std::multiset 的底层实现是红黑树,不是哈希表,std::set、std::multiset 使用红黑树来索引和存储,不过给我们的使用方式,还是哈希法的使用方式,即key和value。所以使用这些数据结构来解决映射问题的方法,我们依然称之为哈希法。 map也是一样的道理。 +虽然std::set和std::multiset 的底层实现基于红黑树而非哈希表,它们通过红黑树来索引和存储数据。不过给我们的使用方式,还是哈希法的使用方式,即依靠键(key)来访问值(value)。所以使用这些数据结构来解决映射问题的方法,我们依然称之为哈希法。std::map也是一样的道理。 这里在说一下,一些C++的经典书籍上 例如STL源码剖析,说到了hash_set hash_map,这个与unordered_set,unordered_map又有什么关系呢? From e2b92184a88e4d5287e982cd97d4a425cf88acbc Mon Sep 17 00:00:00 2001 From: kankan-web <2728360489@qq.com> Date: Sun, 7 Apr 2024 21:25:29 +0800 Subject: [PATCH 1080/1533] =?UTF-8?q?Update=200770.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\214\205\347\211\210\346\234\254.md" | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 622b1117e7..fd5c24fb2b 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -211,10 +211,33 @@ func main() { ``` ### JavaScript: - +```javaScript +var climbStairs = function (n) { + let dp = new Array(n + 1).fill(0); + dp[0] = 1; + // 排列题,注意循环顺序,背包在外物品在内 + for (let j = 1; j <= n; j++) {//遍历背包 + for (let i = 1; i <= 2; i++) {//遍历物品 + if (j - i >= 0) dp[j] = dp[j] + dp[j - i]; + } + } + return dp[n]; +} +``` ### TypeScript: - +```typescript +var climbStairs = function (n: number): number { + let dp: number[] = new Array(n + 1).fill(0); + dp[0] = 1; + for (let j = 1; j <= n; j++) {//遍历背包 + for (let i = 1; i <= 2; i++) {//遍历物品 + if (j - i >= 0) dp[j] = dp[j] + dp[j - i]; + } + } + return dp[n]; +} +``` ### Rust: From c5a6c3e4104bb7a17a55edc44db6cbf7c55e642e Mon Sep 17 00:00:00 2001 From: markwang Date: Mon, 8 Apr 2024 17:12:03 +0800 Subject: [PATCH 1081/1533] =?UTF-8?q?977.=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9Go=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\273\204\347\232\204\345\271\263\346\226\271.md" | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 5bdbcbc7e0..a39902b341 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -181,6 +181,17 @@ class Solution: ### Go: ```Go +// 排序法 +func sortedSquares(nums []int) []int { + for i, val := range nums { + nums[i] *= val + } + sort.Ints(nums) + return nums +} +``` +```Go +// 双指针法 func sortedSquares(nums []int) []int { n := len(nums) i, j, k := 0, n-1, n-1 From 2dce5252c01dc934795bf93723d2229a05f33405 Mon Sep 17 00:00:00 2001 From: "beChild.pu" <1351402134@qq.com> Date: Tue, 9 Apr 2024 09:47:06 +0800 Subject: [PATCH 1082/1533] =?UTF-8?q?Update:=20=E4=BF=AE=E5=A4=8D=E5=8E=9F?= =?UTF-8?q?Java=E7=89=88=E6=9C=AC=E6=8A=A5=E7=A9=BA=E6=8C=87=E9=92=88?= =?UTF-8?q?=E5=BC=82=E5=B8=B8(NullPointerException)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index c7a2cfcb3f..9e180f753e 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -98,27 +98,33 @@ public: ### Java: ```java -public ListNode removeNthFromEnd(ListNode head, int n){ - ListNode dummyNode = new ListNode(0); - dummyNode.next = head; - - ListNode fastIndex = dummyNode; - ListNode slowIndex = dummyNode; +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + //新建一个虚拟头节点指向head + ListNode dummyNode = new ListNode(0); + dummyNode.next = head; + //快慢指针指向虚拟头节点 + ListNode fastIndex = dummyNode; + ListNode slowIndex = dummyNode; + + // 只要快慢指针相差 n 个结点即可 + for (int i = 0; i <= n; i++) { + fastIndex = fastIndex.next; + } - // 只要快慢指针相差 n 个结点即可 - for (int i = 0; i < n ; i++){ - fastIndex = fastIndex.next; - } + while (fastIndex != null) { + fastIndex = fastIndex.next; + slowIndex = slowIndex.next; + } - while (fastIndex != null){ - fastIndex = fastIndex.next; - slowIndex = slowIndex.next; + // 此时 slowIndex 的位置就是待删除元素的前一个位置。 + // 具体情况可自己画一个链表长度为 3 的图来模拟代码来理解 + // 检查 slowIndex.next 是否为 null,以避免空指针异常 + if (slowIndex.next != null) { + slowIndex.next = slowIndex.next.next; + } + return dummyNode.next; } - - //此时 slowIndex 的位置就是待删除元素的前一个位置。 - //具体情况可自己画一个链表长度为 3 的图来模拟代码来理解 - slowIndex.next = slowIndex.next.next; - return dummyNode.next; } ``` From a8de2463bca5f60de2474629e2fb96f356b5979a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 9 Apr 2024 15:31:53 +0800 Subject: [PATCH 1083/1533] Delete .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 6a3e68da15..0000000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -**/.DS_Store \ No newline at end of file From 65bf739ef28c16d9c7e13047af658c1850a46287 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Tue, 9 Apr 2024 16:32:23 +0800 Subject: [PATCH 1084/1533] =?UTF-8?q?fix:=20=E7=BA=A0=E6=AD=A3=20target=20?= =?UTF-8?q?=E7=9A=84=E6=8B=BC=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\216\344\270\200\344\270\252\344\275\215\347\275\256.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index 22936fef13..9afb9941a9 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -233,7 +233,7 @@ class Solution { if (index == -1) { // nums 中不存在 target,直接返回 {-1, -1} return new int[] {-1, -1}; // 匿名数组 } - // nums 中存在 targe,则左右滑动指针,来找到符合题意的区间 + // nums 中存在 target,则左右滑动指针,来找到符合题意的区间 int left = index; int right = index; // 向左滑动,找左边界 @@ -450,7 +450,7 @@ class Solution: return -1 index = binarySearch(nums, target) if index == -1:return [-1, -1] # nums 中不存在 target,直接返回 {-1, -1} - # nums 中存在 targe,则左右滑动指针,来找到符合题意的区间 + # nums 中存在 target,则左右滑动指针,来找到符合题意的区间 left, right = index, index # 向左滑动,找左边界 while left -1 >=0 and nums[left - 1] == target: left -=1 From baab80473377952f85095a4ec4aeee2febc2101c Mon Sep 17 00:00:00 2001 From: skylarkkk <116788771+skylarkkk@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:00:59 +0800 Subject: [PATCH 1085/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 4621d4a739..3fc1d24105 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -189,7 +189,7 @@ class Solution: res.append(node.val) dfs(node.left) dfs(node.right) - + dfs(root) return res ``` @@ -206,7 +206,7 @@ class Solution: dfs(node.left) res.append(node.val) dfs(node.right) - + dfs(root) return res ``` ```python @@ -224,7 +224,7 @@ class Solution: dfs(node.left) dfs(node.right) res.append(node.val) - + dfs(root) return res ``` From 6b8b526dbfd646b46a997d4c001c248e11b2c10f Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Thu, 11 Apr 2024 17:20:55 +0800 Subject: [PATCH 1086/1533] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=200209?= =?UTF-8?q?.=E9=95=BF=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90?= =?UTF-8?q?=E6=95=B0=E7=BB=84.md=20=E7=9A=84=20Typescript=20=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\346\225\260\347\273\204.md" | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 5934d5e364..d2dfe1ee9e 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -270,22 +270,21 @@ var minSubArrayLen = function(target, nums) { ```typescript function minSubArrayLen(target: number, nums: number[]): number { - let left: number = 0, right: number = 0; - let res: number = nums.length + 1; - let sum: number = 0; - while (right < nums.length) { - sum += nums[right]; - if (sum >= target) { - // 不断移动左指针,直到不能再缩小为止 - while (sum - nums[left] >= target) { - sum -= nums[left++]; - } - res = Math.min(res, right - left + 1); - } - right++; + let left: number = 0, + res: number = Infinity, + subLen: number = 0, + sum: number = 0; + for (let right: number = 0; right < nums.length; right++) { + sum += nums[right]; + while (sum >= target) { + subLen = right - left + 1; + res = Math.min(res, subLen); + sum -= nums[left]; + left++; } - return res === nums.length + 1 ? 0 : res; -}; + } + return res === Infinity ? 0 : res; +} ``` ### Swift: From d811a8ee78f6ec724f17e98780a0dccba85498ed Mon Sep 17 00:00:00 2001 From: wbingb Date: Thu, 11 Apr 2024 22:37:08 +0800 Subject: [PATCH 1087/1533] Summary: Added an iterative implementation of the find function in the union-find template to prevent stack overflow problems caused by recursive calls. --- .obsidian/app.json | 1 + .obsidian/appearance.json | 3 + .obsidian/core-plugins-migration.json | 30 ++++ .obsidian/core-plugins.json | 20 +++ .obsidian/workspace.json | 147 ++++++++++++++++++ ...30\345\234\250\350\267\257\345\276\204.md" | 24 ++- 6 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 .obsidian/app.json create mode 100644 .obsidian/appearance.json create mode 100644 .obsidian/core-plugins-migration.json create mode 100644 .obsidian/core-plugins.json create mode 100644 .obsidian/workspace.json diff --git a/.obsidian/app.json b/.obsidian/app.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/.obsidian/app.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json new file mode 100644 index 0000000000..c8c365d89b --- /dev/null +++ b/.obsidian/appearance.json @@ -0,0 +1,3 @@ +{ + "accentColor": "" +} \ No newline at end of file diff --git a/.obsidian/core-plugins-migration.json b/.obsidian/core-plugins-migration.json new file mode 100644 index 0000000000..436f43cf56 --- /dev/null +++ b/.obsidian/core-plugins-migration.json @@ -0,0 +1,30 @@ +{ + "file-explorer": true, + "global-search": true, + "switcher": true, + "graph": true, + "backlink": true, + "canvas": true, + "outgoing-link": true, + "tag-pane": true, + "properties": false, + "page-preview": true, + "daily-notes": true, + "templates": true, + "note-composer": true, + "command-palette": true, + "slash-command": false, + "editor-status": true, + "bookmarks": true, + "markdown-importer": false, + "zk-prefixer": false, + "random-note": false, + "outline": true, + "word-count": true, + "slides": false, + "audio-recorder": false, + "workspaces": false, + "file-recovery": true, + "publish": false, + "sync": false +} \ No newline at end of file diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json new file mode 100644 index 0000000000..9405bfdc22 --- /dev/null +++ b/.obsidian/core-plugins.json @@ -0,0 +1,20 @@ +[ + "file-explorer", + "global-search", + "switcher", + "graph", + "backlink", + "canvas", + "outgoing-link", + "tag-pane", + "page-preview", + "daily-notes", + "templates", + "note-composer", + "command-palette", + "editor-status", + "bookmarks", + "outline", + "word-count", + "file-recovery" +] \ No newline at end of file diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json new file mode 100644 index 0000000000..144fe03fdc --- /dev/null +++ b/.obsidian/workspace.json @@ -0,0 +1,147 @@ +{ + "main": { + "id": "14608cf8b641f651", + "type": "split", + "children": [ + { + "id": "7b559c19d2418ebd", + "type": "tabs", + "children": [ + { + "id": "a006865600bc4e20", + "type": "leaf", + "state": { + "type": "empty", + "state": {} + } + } + ] + } + ], + "direction": "vertical" + }, + "left": { + "id": "340e6a79c670a47b", + "type": "split", + "children": [ + { + "id": "c5f81ca398c18cda", + "type": "tabs", + "children": [ + { + "id": "51f9f8f44ecceb89", + "type": "leaf", + "state": { + "type": "file-explorer", + "state": { + "sortOrder": "alphabetical" + } + } + }, + { + "id": "0cff567244d7cff5", + "type": "leaf", + "state": { + "type": "search", + "state": { + "query": "1971", + "matchingCase": false, + "explainSearch": false, + "collapseAll": false, + "extraContext": false, + "sortOrder": "alphabetical" + } + } + }, + { + "id": "209574c920774a4d", + "type": "leaf", + "state": { + "type": "bookmarks", + "state": {} + } + } + ], + "currentTab": 1 + } + ], + "direction": "horizontal", + "width": 300 + }, + "right": { + "id": "93ce8f4c11893983", + "type": "split", + "children": [ + { + "id": "da8cbb56e5ee7c52", + "type": "tabs", + "children": [ + { + "id": "dfccbc1ebc0bb831", + "type": "leaf", + "state": { + "type": "backlink", + "state": { + "collapseAll": false, + "extraContext": false, + "sortOrder": "alphabetical", + "showSearch": false, + "searchQuery": "", + "backlinkCollapsed": false, + "unlinkedCollapsed": true + } + } + }, + { + "id": "9d2201419a111fe4", + "type": "leaf", + "state": { + "type": "outgoing-link", + "state": { + "linksCollapsed": false, + "unlinkedCollapsed": true + } + } + }, + { + "id": "dc86ccf1b01bc02c", + "type": "leaf", + "state": { + "type": "tag", + "state": { + "sortOrder": "frequency", + "useHierarchy": true + } + } + }, + { + "id": "ec512545d2a7f2e5", + "type": "leaf", + "state": { + "type": "outline", + "state": {} + } + } + ] + } + ], + "direction": "horizontal", + "width": 300, + "collapsed": true + }, + "left-ribbon": { + "hiddenItems": { + "switcher:打开快速切换": false, + "graph:查看关系图谱": false, + "canvas:新建白板": false, + "daily-notes:打开/创建今天的日记": false, + "templates:插入模板": false, + "command-palette:打开命令面板": false + } + }, + "active": "a006865600bc4e20", + "lastOpenFiles": [ + "problems/1971.寻找图中是否存在路径.md", + "README.md" + ] +} \ No newline at end of file diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 89a2a1fe87..4123c03e25 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -47,11 +47,22 @@ void init() { father[i] = i; } } -// 并查集里寻根的过程 +// 并查集里寻根的过程,这里递归调用当题目数据过多,递归调用可能会发生栈溢出 + int find(int u) { return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 } +// 使用迭代的方法可以避免栈溢出问题 +int find(int x) { + while (x != parent[x]) { + // 路径压缩,直接将x链接到其祖先节点,减少树的高度 + parent[x] = parent[parent[x]]; + x = parent[x]; + } +return x; +} + // 判断 u 和 v是否找到同一个根 bool isSame(int u, int v) { u = find(u); @@ -84,6 +95,8 @@ void join(int u, int v) { 此时我们就可以直接套用并查集模板。 +本题在join函数调用find函数时如果是递归调用会发生栈溢出提示,建议使用迭代方法 + 使用 join(int u, int v)将每条边加入到并查集。 最后 isSame(int u, int v) 判断是否是同一个根 就可以了。 @@ -102,8 +115,13 @@ private: } } // 并查集里寻根的过程 - int find(int u) { - return u == father[u] ? u : father[u] = find(father[u]); + int find(int x) { + while (x != parent[x]) { + // 路径压缩,直接将x链接到其祖先节点,减少树的高度 + parent[x] = parent[parent[x]]; + x = parent[x]; + } + return x; } // 判断 u 和 v是否找到同一个根 From f835589049f3beb25925d615d5fa25d7998243fc Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 12 Apr 2024 10:37:17 +0800 Subject: [PATCH 1088/1533] Update --- README.md | 4 +- ...75\347\232\204\350\267\257\345\276\204.md" | 10 +- ...30\345\234\250\350\267\257\345\276\204.md" | 11 +- ...17\202\344\274\232dijkstra\345\240\206.md" | 0 ...74\232dijkstra\346\234\264\347\264\240.md" | 0 ...a0053.\345\257\273\345\256\235-Kruskal.md" | 2 +- ...kama0053.\345\257\273\345\256\235-prim.md" | 2 +- ...77\346\215\242\346\225\260\345\255\227.md" | 0 ...13\345\255\227\347\254\246\344\270\262.md" | 0 ...\211\251\350\277\220\350\276\223I-SPFA.md" | 286 +++++++++++++ ...7\347\211\251\350\277\220\350\276\223I.md" | 14 +- ...\347\211\251\350\277\220\350\276\223II.md" | 3 + ...347\211\251\350\277\220\350\276\223III.md" | 392 ++++++++++++++++++ ...16\351\200\233\345\205\254\345\233\255.md" | 57 +++ ...347\211\251\350\277\220\350\276\223III.md" | 125 ------ problems/qita/xunlianying.md | 77 +++- ...50\350\277\231\351\207\214\357\274\201.md" | 165 -------- 17 files changed, 818 insertions(+), 330 deletions(-) rename "problems/kama47.\345\217\202\344\274\232dijkstra\345\240\206.md" => "problems/kama0047.\345\217\202\344\274\232dijkstra\345\240\206.md" (100%) rename "problems/kama47.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" => "problems/kama0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" (100%) rename "problems/kama53.\345\257\273\345\256\235-Kruskal.md" => "problems/kama0053.\345\257\273\345\256\235-Kruskal.md" (99%) rename "problems/kama53.\345\257\273\345\256\235-prim.md" => "problems/kama0053.\345\257\273\345\256\235-prim.md" (99%) rename "problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" => "problems/kama0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" (100%) rename "problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" => "problems/kama0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" (100%) create mode 100644 "problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" rename "problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" => "problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" (95%) rename "problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" => "problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" (97%) create mode 100644 "problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" create mode 100644 "problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" delete mode 100644 "problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" delete mode 100644 "problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" diff --git a/README.md b/README.md index 7c26d2c924..a30e0423fe 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ 👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master) > 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) -> 2. **正式出版** :[《代码随想录》](https://programmercarl.com/other/publish.html) 。 -> 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 +> 2. **正式出版** :[《代码随想录》](https://programmercarl.com/qita/publish.html) 。 +> 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/qita/algo_pdf.html) 。 > 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 > 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 > 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index 05b55b5b16..4b46d65909 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -52,7 +52,7 @@ graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节 至于 单一路径,和路径集合可以放在全局变量,那么代码是这样的: -```c++ +```CPP vector> result; // 收集符合条件的路径 vector path; // 0节点到终点的路径 // x:目前遍历的节点 @@ -71,7 +71,7 @@ void dfs (vector>& graph, int x) 所以 但 x 等于 graph.size() - 1 的时候就找到一条有效路径。 代码如下: -```c++ +```CPP // 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1 if (x == graph.size() - 1) { // 找到符合条件的一条路径 result.push_back(path); // 收集有效路径 @@ -104,13 +104,13 @@ path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 进入下一层递归 -```C++ +```CPP dfs(graph, graph[x][i]); // 进入下一层递归 ``` 最后就是回溯的过程,撤销本次添加节点的操作。 该过程整体代码: -```C++ +```CPP for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 dfs(graph, graph[x][i]); // 进入下一层递归 @@ -120,7 +120,7 @@ for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节 本题整体代码如下: -```c++ +```CPP class Solution { private: vector> result; // 收集符合条件的路径 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 89a2a1fe87..5463ec7288 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -16,16 +16,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220705101442.png) -提示: - -- 1 <= n <= 2 \* 10^5 -- 0 <= edges.length <= 2 \* 10^5 -- edges[i].length == 2 -- 0 <= ui, vi <= n - 1 -- ui != vi -- 0 <= start, end <= n - 1 -- 不存在双向边 -- 不存在指向顶点自身的边 + ## 思路 diff --git "a/problems/kama47.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kama0047.\345\217\202\344\274\232dijkstra\345\240\206.md" similarity index 100% rename from "problems/kama47.\345\217\202\344\274\232dijkstra\345\240\206.md" rename to "problems/kama0047.\345\217\202\344\274\232dijkstra\345\240\206.md" diff --git "a/problems/kama47.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kama0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" similarity index 100% rename from "problems/kama47.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" rename to "problems/kama0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" diff --git "a/problems/kama53.\345\257\273\345\256\235-Kruskal.md" "b/problems/kama0053.\345\257\273\345\256\235-Kruskal.md" similarity index 99% rename from "problems/kama53.\345\257\273\345\256\235-Kruskal.md" rename to "problems/kama0053.\345\257\273\345\256\235-Kruskal.md" index 1e6cbbb909..1d0c9c83e8 100644 --- "a/problems/kama53.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kama0053.\345\257\273\345\256\235-Kruskal.md" @@ -1,5 +1,5 @@ -# 寻宝 +# kruskal算法精讲 [卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) diff --git "a/problems/kama53.\345\257\273\345\256\235-prim.md" "b/problems/kama0053.\345\257\273\345\256\235-prim.md" similarity index 99% rename from "problems/kama53.\345\257\273\345\256\235-prim.md" rename to "problems/kama0053.\345\257\273\345\256\235-prim.md" index 9959954aef..4d3d9bd829 100644 --- "a/problems/kama53.\345\257\273\345\256\235-prim.md" +++ "b/problems/kama0053.\345\257\273\345\256\235-prim.md" @@ -1,5 +1,5 @@ -# 寻宝 +# prim算法精讲 [卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) diff --git "a/problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kama0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" similarity index 100% rename from "problems/kama54.\346\233\277\346\215\242\346\225\260\345\255\227.md" rename to "problems/kama0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" diff --git "a/problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kama0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" similarity index 100% rename from "problems/kama55.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" rename to "problems/kama0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" diff --git "a/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" new file mode 100644 index 0000000000..634a33a04e --- /dev/null +++ "b/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -0,0 +1,286 @@ + +# Bellman_ford 队列优化算法(又名SPFA) + +[卡码网: 94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) + +题目描述 + +某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 + + +网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。权值为正表示扣除了政府补贴后运输货物仍需支付的费用;权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 + + +请找出从城市 1 到城市 n 的所有可能路径中,综合政府补贴后的最低运输成本。如果最低运输成本是一个负数,它表示在遵循最优路径的情况下,运输过程中反而能够实现盈利。 + + +城市 1 到城市 n 之间可能会出现没有路径的情况,同时保证道路网络中不存在任何负权回路。 + +输入描述 + +第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 + +接下来为 m 行,每行包括三个整数,s、t 和 v,表示 s 号城市运输货物到达 t 号城市,道路权值为 v(单向图)。 + +输出描述 + +如果能够从城市 1 到连通到城市 n, 请输出一个整数,表示运输成本。如果该整数是负数,则表示实现了盈利。如果从城市 1 没有路径可达城市 n,请输出 "unconnected"。 + +输入示例: + +``` +6 7 +5 6 -2 +1 2 1 +5 3 1 +2 5 2 +2 4 -3 +4 6 4 +1 3 5 +``` + +## 思路 + +本题我们来系统讲解 Bellman_ford 队列优化算法 ,也叫SPFA算法(Shortest Path Faster Algorithm)。 + +> SPFA的称呼来自 1994年西南交通大学段凡丁的论文,其实Bellman_ford 提出后不久 (20世纪50年代末期) 就有队列优化的版本,国际上不承认这个算法是是国内提出的。 所以国际上一般称呼 算法为 Bellman_ford 队列优化算法(Queue improved Bellman-Ford) + +大家知道以上来历,知道 SPFA 和 Bellman_ford 队列优化算法 指的都是一个算法就好。 + +如果大家还不够了解 Bellman_ford 算法,强烈建议按照《代码随想录》的顺序学习,否则可能看不懂下面的讲解。 + +大家可以发现 Bellman_ford 算法每次松弛 都是对所有边进行松弛。 + +但真正有效的松弛,是基于已经计算过的节点在做的松弛。 + +给大家举一个例子: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240328104119.png) + +本图中,对所有边进行松弛,真正有效的松弛,只有松弛 边(节点1->节点2) 和 边(节点1->节点5) 。 + +而松弛 边(节点4->节点6) ,边(节点5->节点3)等等 都是无效的操作,因为 节点4 和 节点 5 都是没有被计算过的节点。 + + +所以 Bellman_ford 算法 每次都是对所有边进行松弛,其实是多做了一些无用功。 + +**只需要对 上一次松弛的时候更新过的节点作为出发节点所连接的边 进行松弛就够了**。 + +基于以上思路,如何记录 上次松弛的时候更新过的节点呢? + +用队列来记录。 + +接下来来举例这个队列是如何工作的。 + +以示例给出的所有边为例: + +``` +5 6 -2 +1 2 1 +5 3 1 +2 5 2 +2 4 -3 +4 6 4 +1 3 5 +``` + +我们依然使用**minDist数组来表达 起点到各个节点的最短距离**,例如minDist[3] = 5 表示起点到达节点3 的最小距离为5 + +初始化,起点为节点1, 起点到起点的最短距离为0,所以minDist[1] 为 0。 将节点1 加入队列 (下次松弛送节点1开始) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115555.png) + +------------ + +从队列里取出节点1,松弛节点1 作为出发点链接的边(节点1 -> 节点2)和边(节点1 -> 节点3) + +边:节点1 -> 节点2,权值为1 ,minDist[2] > minDist[1] + 1 ,更新 minDist[2] = minDist[1] + 1 = 0 + 1 = 1 。 + +边:节点1 -> 节点3,权值为5 ,minDist[3] > minDist[1] + 5,更新 minDist[3] = minDist[1] + 5 = 0 + 5 = 5。 + +将节点2,节点3 加入队列,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115544.png) + + +----------------- + + +从队列里取出节点2,松弛节点2 作为出发点链接的边(节点2 -> 节点4)和边(节点2 -> 节点5) + +边:节点2 -> 节点4,权值为1 ,minDist[4] > minDist[2] + (-3) ,更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 。 + +边:节点2 -> 节点5,权值为2 ,minDist[5] > minDist[2] + 2 ,更新 minDist[5] = minDist[2] + 2 = 1 + 2 = 3 。 + + +将节点4,节点5 加入队列,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115527.png) + + +-------------------- + + +从队列里出去节点3,松弛节点3 作为出发点链接的边。 + +因为没有从节点3作为出发点的边,所以这里就从队列里取出节点3就好,不用做其他操作,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115515.png) + + +------------ + +从队列中取出节点4,松弛节点4作为出发点链接的边(节点4 -> 节点6) + +边:节点4 -> 节点6,权值为4 ,minDist[6] > minDist[4] + 4,更新 minDist[6] = minDist[4] + 4 = -2 + 4 = 2 。 + +讲节点6加入队列 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115451.png) + + +--------------- + +从队列中取出节点5,松弛节点5作为出发点链接的边(节点5 -> 节点3),边(节点5 -> 节点6) + +边:节点5 -> 节点3,权值为1 ,minDist[3] > minDist[5] + 1 ,更新 minDist[3] = minDist[5] + 1 = 3 + 1 = 4 + +边:节点5 -> 节点6,权值为-2 ,minDist[6] > minDist[5] + (-2) ,更新 minDist[6] = minDist[5] + (-2) = 3 - 2 = 1 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115436.png) + + +因为节点3,和 节点6 都曾经加入过队列,不用重复加入,避免重复计算。 + + +-------------- + +从队列中取出节点6,松弛节点6 作为出发点链接的边。 + +节点6作为终点,没有可以出发的边。 + +所以直接从队列中取出,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115424.png) + + +---------- + +这样我们就完成了基于队列优化的bellman_ford的算法模拟过程。 + +大家可以发现 基于队列优化的算法,要比bellman_ford 算法 减少很多无用的松弛情况,特别是对于边树众多的大图 优化效果明显。 + +了解了大体流程,我们再看代码应该怎么写。 + +在上面模拟过程中,我们每次都要知道 一个节点作为出发点 链接了哪些节点。 + +如果想方便这道这些数据,就需要使用邻接表来存储这个图,如果对于邻接表不了解的话,可以看 [kama0047.参会dijkstra堆](./kama0047.参会dijkstra堆.md) 中 图的存储 部分。 + + +代码如下: + +```CPP +#include +#include +#include +#include +#include +using namespace std; + +struct Edge { //邻接表 + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1); // 邻接表 + + // 将所有边保存起来 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid[p1].push_back(Edge(p2, val)); + } + int start = 1; // 起点 + int end = n; // 终点 + + vector minDist(n + 1 , INT_MAX); + minDist[start] = 0; + + queue que; + que.push(start); + int que_size; + while (!que.empty()) { + // 注意这个数组放的位置 + vector visited(n + 1, false); // 可加,可不加,加了效率高一些,防止队列里重复访问,其数值已经算过了 + que_size = que.size(); + + int node = que.front(); que.pop(); + + for (Edge edge : grid[node]) { + int from = node; + int to = edge.to; + int price = edge.val; + if (minDist[to] > minDist[from] + price) { // 开始松弛 + minDist[to] = minDist[from] + price; + if(visited[to]) continue; // 节点不用重复放入队列,但节点需要重复计算,所以放在这里位置 + visited[to] = true; + que.push(to); + } + } + + } + + if (minDist[end] == INT_MAX) cout << "unconnected" << endl; // 不能到达终点 + else cout << minDist[end] << endl; // 到达终点最短路径 +} +``` + +代码中有一点需要注意,即 `if(visited[to]) continue;` 这段代码放的位置。 + + +一些录友可能写成这样: + +```CPP +if (minDist[to] > minDist[from] + price) { // 开始松弛 + if(visited[to]) continue; + minDist[to] = minDist[from] + price; + visited[to] = true; + que.push(to); +} +``` + +这是不对了,我们仅仅是控制节点不用重复加入队列,但对于边的松弛,节点数值的更新,是要重复计算的,要不然如何 不断更新最短路径呢? + +所以 `if(visited[to]) continue;` 应该放在这里: + +```CPP +if (minDist[to] > minDist[from] + price) { // 开始松弛 + minDist[to] = minDist[from] + price; + if(visited[to]) continue; // 仅仅控制节点不要重复加入队列 + visited[to] = true; + que.push(to); +} +``` + + + + +## 拓展 + +关于 加visited 方式节点重复方便,可能也有录友认为,加上 visited 也是防止 如果图中出现了环的话,会导致的 队列里一直不为空。 + + + + + diff --git "a/problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" similarity index 95% rename from "problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" rename to "problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 836ac34562..50c59ea7c0 100644 --- "a/problems/kama94.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -1,7 +1,7 @@ # 94. 城市间货物运输 I -[题目链接](https://kamacoder.com/problempage.php?pid=1152) +[卡码网: 94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) 题目描述 @@ -159,7 +159,7 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113927.png) -边:节点2 -> 节点4,权值为-3 ,minDist[4] > minDist[2] + 2,更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 ,如图: +边:节点2 -> 节点4,权值为-3 ,minDist[4] > minDist[2] + (-3),更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 ,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114036.png) @@ -261,6 +261,16 @@ int main() { } ``` +* 时间复杂度: O(N * E) , N为节点数量,E为图中边的数量 +* 空间复杂度: O(N) ,即 minDist 数组所开辟的空间 + +关于空间复杂度,可能有录友疑惑,代码中数组grid不也开辟空间了吗? 为什么只算minDist数组的空间呢? + +grid数组是用来存图的,这是题目描述中必须要使用的空间,而不是我们算法所使用的空间。 + +我们在讲空间复杂度的时候,一般都是说,我们这个算法的空间复杂度。 + + ### 拓展 有录友可能会想,那我 松弛 n 次,松弛 n + 1次,松弛 2 * n 次会怎么样? diff --git "a/problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" similarity index 97% rename from "problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" rename to "problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 7a76d3615d..e9387c7312 100644 --- "a/problems/kama95.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -148,3 +148,6 @@ int main() { } } ``` + +* 时间复杂度: O(N * E) , N为节点数量,E为图中边的数量 +* 空间复杂度: O(N) ,即 minDist 数组所开辟的空间 diff --git "a/problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" new file mode 100644 index 0000000000..10fb03803a --- /dev/null +++ "b/problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -0,0 +1,392 @@ + +# 96. 城市间货物运输 III + +[题目链接](https://kamacoder.com/problempage.php?pid=1154) + +【题目描述】 + +某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 + +网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。 + +权值为正表示扣除了政府补贴后运输货物仍需支付的费用; + +权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 + +请计算在最多经过 k 个城市的条件下,从城市 src 到城市 dst 的最低运输成本。 + +【输入描述】 + +第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 + +接下来为 m 行,每行包括三个整数,s、t 和 v,表示 s 号城市运输货物到达 t 号城市,道路权值为 v。 + +最后一行包含三个正整数,src、dst、和 k,src 和 dst 为城市编号,从 src 到 dst 经过的城市数量限制。 + +【输出描述】 + +输出一个整数,表示从城市 src 到城市 dst 的最低运输成本,如果无法在给定经过城市数量限制下找到从 src 到 dst 的路径,则输出 "unreachable",表示不存在符合条件的运输方案。 + +输入示例: + +``` +6 7 +1 2 1 +2 4 -3 +2 5 2 +1 3 5 +3 5 1 +4 6 4 +5 6 -2 +2 6 1 +``` + +输出示例: + +``` +0 +``` + +## 思路 + +本题为单源有限最短路问题,同样是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 延伸题目。 + +注意题目中描述是 **最多经过 k 个城市的条件下,而不是一定经过k个城市,也可以经过的城市数量比k小,但要最短的路径**。 + +在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 中我们讲了:**对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**。 + +节点数量为n,起点到终点,最多是 n-1 条边相连。 那么对所有边松弛 n-1 次 就一定能得到 起点到达 终点的最短距离。 + +(如果对以上讲解看不懂,建议详看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) ) + +本题是最多经过 k 个城市, 那么是 k + 1条边相连的节点。 这里可能有录友想不懂为什么是k + 1,来看这个图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402115614.png) + +图中,节点2 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 + +所以本题就是求,起点最多经过k + 1 条边到达终点的最短距离。 + +对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离,那么对所有边松弛 k + 1次,就是求 起点到达 与起点k + 1条边相连的节点的 最短距离。 + +**注意**: 本题是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 的拓展题,如果对 bellman_ford 没有深入了解,强烈建议先看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 再做本题。 + +理解以上内容,其实本题代码就很容易了,bellman_ford 标准写法是松弛 n-1 次,本题就松弛 k + 1次就好。 + +此时我们可以写出如下代码: + +```CPP +// 版本一 +#include +#include +#include +#include +using namespace std; + +int main() { + int src, dst,k ,p1, p2, val ,m , n; + + cin >> n >> m; + + vector> grid; + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid.push_back({p1, p2, val}); + } + + cin >> src >> dst >> k; + + vector minDist(n + 1 , INT_MAX); + minDist[src] = 0; + for (int i = 1; i <= k + 1; i++) { // 对所有边松弛 k + 1次 + for (vector &side : grid) { + int from = side[0]; + int to = side[1]; + int price = side[2]; + if (minDist[from] != INT_MAX && minDist[to] > minDist[from] + price) minDist[to] = minDist[from] + price; + } + + } + + if (minDist[dst] == INT_MAX) cout << "unreachable" << endl; // 不能到达终点 + else cout << minDist[dst] << endl; // 到达终点最短路径 + +} + +``` + +以上代码 标准 bellman_ford 写法,松弛 k + 1次,看上去没什么问题。 + +但大家提交后,居然没通过! + +这是为什么呢? + +接下来我们拿这组数据来举例: + +``` +4 4 +1 2 -1 +2 3 1 +3 1 -1 +3 4 1 +1 4 3 +``` + +(**注意上面的示例是有负权回路的,只有带负权回路的图才能说明问题**) + +> 负权回路是指一条道路的总权值为负,这样的回路使得通过反复经过回路中的道路,理论上可以无限地减少总成本或无限地增加总收益。 + +正常来说,这组数据输出应该是 1,但以上代码输出的是 -2。 + + +在讲解原因的时候,强烈建议大家,先把 minDist数组打印出来,看看minDist数组是不是按照自己的想法变化的,这样更容易理解我接下来的讲解内容。 (**一定要动手,实践出真实,脑洞模拟不靠谱**) + +打印的代码可以是这样: + +```CPP +#include +#include +#include +#include +using namespace std; + +int main() { + int src, dst,k ,p1, p2, val ,m , n; + + cin >> n >> m; + + vector> grid; + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid.push_back({p1, p2, val}); + } + + cin >> src >> dst >> k; + + vector minDist(n + 1 , INT_MAX); + minDist[src] = 0; + for (int i = 1; i <= k + 1; i++) { // 对所有边松弛 k + 1次 + for (vector &side : grid) { + int from = side[0]; + int to = side[1]; + int price = side[2]; + if (minDist[from] != INT_MAX && minDist[to] > minDist[from] + price) minDist[to] = minDist[from] + price; + } + // 打印 minDist 数组 + for (int j = 1; j <= n; j++) cout << minDist[j] << " "; + cout << endl; + + } + + if (minDist[dst] == INT_MAX) cout << "unreachable" << endl; // 不能到达终点 + else cout << minDist[dst] << endl; // 到达终点最短路径 + +} + +``` + +接下来,我按照上面的示例带大家 画图举例 对所有边松弛一次 的效果图。 + +起点为节点1, 起点到起点的距离为0,所以 minDist[1] 初始化为0 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111940.png) + +其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 + +当我们开始对所有边开始第一次松弛: + +边:节点1 -> 节点2,权值为-1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = minDist[1] + (-1) = 0 - 1 = -1 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111914.png) + + +边:节点2 -> 节点3,权值为1 ,minDist[3] > minDist[2] + 1 ,更新 minDist[3] = minDist[2] + 1 = -1 + 1 = 0 ,如图: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111903.png) + + +边:节点3 -> 节点1,权值为-1 ,minDist[1] > minDist[3] + (-1),更新 minDist[1] = 0 + (-1) = -1 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111849.png) + + +边:节点3 -> 节点4,权值为1 ,minDist[4] > minDist[3] + 1,更新 minDist[4] = 0 + (-1) = -1 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111837.png) + + +以上是对所有边进行的第一次松弛,最后 minDist数组为 :-1 -1 0 1 ,(从下标1算起) + +后面几次松弛我就不挨个画图了,过程大同小异,我直接给出minDist数组的变化: + +所有边进行的第二次松弛,minDist数组为 : -2 -2 -1 0 +所有边进行的第三次松弛,minDist数组为 : -3 -3 -2 -1 +所有边进行的第四次松弛,minDist数组为 : -4 -4 -3 -2 (本示例中k为3,所以松弛4次) + +最后计算的结果minDist[4] = -2,即 起点到 节点4,最多经过 3 个节点的最短距离是 -2,但 正确的结果应该是 1,即路径:节点1 -> 节点2 -> 节点3 -> 节点4。 + +理论上来说,**对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**。 + +对所有边松弛两次,相当于计算 起点到达 与起点两条边相连的节点的最短距离。 + +对所有边松弛三次,以此类推。 + +但在对所有边松弛第一次的过程中,大家会发现,不仅仅 与起点一条边相连的节点更新了,所有节点都更新了。 + +而且对所有边的后面几次松弛,同样是更新了所有的节点,说明 至多经过k 个节点 这个限制 根本没有限制住,每个节点的数值都被更新了。 + +这是为什么? + +在上面画图距离中,对所有边进行第一次松弛,在计算 边(节点2 -> 节点3) 的时候,更新了 节点3。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111903.png) + +理论上来说节点3 应该在对所有边第二次松弛的时候才更新。 这因为当时是基于已经计算好的 节点2(minDist[2])来做计算了。 + +minDist[2]在计算边:(节点1 -> 节点2)的时候刚刚被赋值为 -1。 + +这样就造成了一个情况,即:计算minDist数组的时候,基于了本次松弛的 minDist数值,而不是上一次 松弛时候minDist的数值。 +所以在每次计算 minDist 时候,要基于 对所有边上一次松弛的 minDist 数值才行,所以我们要记录上一次松弛的minDist。 + +代码修改如下: (关键地方已经注释) + +```CPP +// 版本二 +#include +#include +#include +#include +using namespace std; + +int main() { + int src, dst,k ,p1, p2, val ,m , n; + + cin >> n >> m; + + vector> grid; + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + grid.push_back({p1, p2, val}); + } + + cin >> src >> dst >> k; + + vector minDist(n + 1 , INT_MAX); + minDist[src] = 0; + vector minDist_copy(n + 1); // 用来记录上一次遍历的结果 + for (int i = 1; i <= k + 1; i++) { + minDist_copy = minDist; // 获取上一次计算的结果 + for (vector &side : grid) { + int from = side[0]; + int to = side[1]; + int price = side[2]; + // 注意使用 minDist_copy 来计算 minDist + if (minDist_copy[from] != INT_MAX && minDist[to] > minDist_copy[from] + price) { + minDist[to] = minDist_copy[from] + price; + } + } + } + if (minDist[dst] == INT_MAX) cout << "unreachable" << endl; // 不能到达终点 + else cout << minDist[dst] << endl; // 到达终点最短路径 + +} + +``` + +* 时间复杂度: O(K * E) , K为至多经过K个节点,E为图中边的数量 +* 空间复杂度: O(N) ,即 minDist 数组所开辟的空间 + +## 拓展一(边的顺序的影响) + +其实边的顺序会影响我们每一次拓展的结果。 + +我来给大家举个例子。 + +我上面讲解中,给出的示例是这样的: +``` +4 4 +1 2 -1 +2 3 1 +3 1 -1 +3 4 1 +1 4 3 +``` + +我将示例中边的顺序改一下,给成: + +``` +4 4 +3 1 -1 +3 4 1 +2 3 1 +1 2 -1 +1 4 3 +``` + +所构成是图是一样的,都是如下的这个图,但给出的边的顺序是不一样的。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410154340.png) + +再用版本一的代码是运行一下,发现结果输出是 1, 是对的。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410154940.png) + +分明刚刚输出的结果是 -2,是错误的,怎么 一样的图,这次输出的结果就对了呢? + +其实这是和示例中给出的边的顺序是有关的, + +我们按照我修改后的示例再来模拟 对所有边的第一次拓展情况。 + +初始化: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410155545.png) + +边:节点3 -> 节点1,权值为-1 ,节点3还没有被计算过,节点1 不更新。 + +边:节点3 -> 节点4,权值为1 ,节点3还没有被计算过,节点4 不更新。 + +边:节点2 -> 节点3,权值为 1 ,节点2还没有被计算过,节点3 不更新。 + +边:节点1 -> 节点2,权值为 -1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = 0 + (-1) = -1 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410160046.png) + +以上是对所有边 松弛一次的状态。 + +可以发现 同样的图,边的顺序不一样,使用版本一的代码 每次松弛更新的节点也是不一样的。 + +而边的顺序是随机的,是题目给我们的,所以本题我们才需要 记录上一次松弛的minDist,来保障 每一次对所有边松弛的结果。 + + +## 拓展二(本题本质) + +那么前面讲解过的 [94.城市间货物运输I](./kama94.城市间货物运输I.md) 和 [95.城市间货物运输II](./kama95.城市间货物运输II.md) 也是bellman_ford经典算法,也没使用 minDist_copy,怎么就没问题呢? + +> 如果没看过我上面这两篇讲解的话,建议详细学习上面两篇,在看我下面讲的区别,否则容易看不懂。 + +[94.城市间货物运输I](./kama94.城市间货物运输I.md), 是没有 负权回路的,那么 多松弛多少次,对结果都没有影响。 + +求 节点1 到 节点n 的最短路径,松弛n-1 次就够了,松弛 大于 n-1次,结果也不会变。 + +那么在对所有边进行第一次松弛的时候,如果基于 最近计算的 minDist 来计算 minDist (相当于多做松弛了),也是对最终结果没影响。 + +[95.城市间货物运输II](./kama95.城市间货物运输II.md) 是判断是否有 负权回路,一旦有负权回路, 对所有边松弛 n -1 次以后,在做松弛 minDist 数值一定会变,根据这一点是判断是否有负权回路。 + +所以 在对所有边进行第一次松弛的时候,如果基于 最近计算的 minDist 来计算 minDist (相当于多做松弛了),对最后判断是否有负权回路同样没有影响。 + +你可以理解 minDist的数组其实是不准确了,但它只要变化了就可以让我们来判断 是否有 负权回路。 + + +那么本题 为什么计算minDist 一定要基于上次 的 minDist 数值。 + +其关键在于本题的两个因素: + +* 本题可以有负权回路,说明只要多做松弛,结果是会变的。 +* 本题要求最多经过k个节点,对松弛次数是有限制的。 + +如果本题中 没有负权回路的测试用例, 那版本一的代码就可以过了,也就不用我费这么大口舌去讲解的这个坑了。 + diff --git "a/problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" new file mode 100644 index 0000000000..1000eed75d --- /dev/null +++ "b/problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -0,0 +1,57 @@ + +# Floyd 算法精讲 + +[卡码网:97. 小明逛公园](https://kamacoder.com/problempage.php?pid=1155) + +【题目描述】 + +小明喜欢去公园散步,公园内布置了许多的景点,相互之间通过小路连接,小明希望在观看景点的同时,能够节省体力,走最短的路径。 + + +给定一个公园景点图,图中有 N 个景点(编号为 1 到 N),以及 M 条双向道路连接着这些景点。每条道路上行走的距离都是已知的。 + + +小明有 Q 个观景计划,每个计划都有一个起点 start 和一个终点 end,表示他想从景点 start 前往景点 end。由于小明希望节省体力,他想知道每个观景计划中从起点到终点的最短路径长度。 请你帮助小明计算出每个观景计划的最短路径长度。 + +【输入描述】 + +第一行包含两个整数 N, M, 分别表示景点的数量和道路的数量。 + +接下来的 M 行,每行包含三个整数 u, v, w,表示景点 u 和景点 v 之间有一条长度为 w 的双向道路。 + +接下里的一行包含一个整数 Q,表示观景计划的数量。 + +接下来的 Q 行,每行包含两个整数 start, end,表示一个观景计划的起点和终点。 + +【输出描述】 + +对于每个观景计划,输出一行表示从起点到终点的最短路径长度。如果两个景点之间不存在路径,则输出 -1。 + +【输入示例】 + +7 3 +1 2 4 +2 5 6 +3 6 8 +2 +1 2 +2 3 + +【输出示例】 + +4 +-1 + +【提示信息】 + +从 1 到 2 的路径长度为 4,2 到 3 之间并没有道路。 + +1 <= N, M, Q <= 1000. + +## 思路 + +本题是经典的多源最短路问题。 + +我们之前讲解过的算法,dijkstra, + + diff --git "a/problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" deleted file mode 100644 index 1fdba49a37..0000000000 --- "a/problems/kama96.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ /dev/null @@ -1,125 +0,0 @@ - -# 96. 城市间货物运输 III - -[题目链接](https://kamacoder.com/problempage.php?pid=1154) - -【题目描述】 - -某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 - -网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。 - -权值为正表示扣除了政府补贴后运输货物仍需支付的费用; - -权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 - -请计算在最多经过 k 个城市的条件下,从城市 src 到城市 dst 的最低运输成本。 - -【输入描述】 - -第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 - -接下来为 m 行,每行包括三个整数,s、t 和 v,表示 s 号城市运输货物到达 t 号城市,道路权值为 v。 - -最后一行包含三个正整数,src、dst、和 k,src 和 dst 为城市编号,从 src 到 dst 经过的城市数量限制。 - -【输出描述】 - -输出一个整数,表示从城市 src 到城市 dst 的最低运输成本,如果无法在给定经过城市数量限制下找到从 src 到 dst 的路径,则输出 "unreachable",表示不存在符合条件的运输方案。 - -输入示例: - -``` -6 7 -1 2 1 -2 4 -3 -2 5 2 -1 3 5 -3 5 1 -4 6 4 -5 6 -2 -2 6 1 -``` - -输出示例: - -``` -0 -``` - -## 思路 - -本题为单源有限最短路问题,同样是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 延伸题目。 - -在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 中我们讲了:**对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**。 - -节点数量为n,那么起点到终点,最多是 n-1 条边相连。 那么对所有边松弛 n-1 次 就一定能得到 起点到达 终点的最短距离。 - -(如果对以上讲解看不懂,建议详看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) ) - -本题是最多经过 k 个城市, 那么是 k + 1条边相连的节点。 这里可能有录友想不懂为什么是k + 1,来看这个图: - -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402115614.png) - -图中,节点2 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 - -所以本题就是求,起点最多经过k + 1 条边到达终点的最短距离。 - - -对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离,那么对所有边松弛 k + 1次,就是求 起点到达 与起点k + 1条边相连的节点的 最短距离。 - -如果 终点数值没有被计算覆盖,那就是无法到达。 - -**注意**: 本题是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 的拓展题,如果对 bellman_ford 没有深入了解,强烈建议先看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 再做本题。 - -理解以上内容,其实本题代码就很容易了,bellman_ford 标准写法是松弛 n-1 次,本题就松弛 k + 1次就好。 - -如果大家理解后,建议先自己写写代码,提交一下看看,因为 这里还有一个坑,如果不自己去试试,体会就不够深刻了。 - - - -代码如下: (关键地方详细注释) - -```CPP -#include -#include -#include -#include -using namespace std; - -int main() { - int src, dst,k ,p1, p2, val ,m , n; - - cin >> n >> m; - - vector> grid; - - for(int i = 0; i < m; i++){ - cin >> p1 >> p2 >> val; - // p1 指向 p2,权值为 val - grid.push_back({p1, p2, val}); - } - - cin >> src >> dst >> k; - - vector minDist(n + 1 , INT_MAX); - minDist[src] = 0; - vector minDist_copy(n + 1); // 用来记录每一次遍历的结果 - for (int i = 1; i <= k + 1; i++) { - minDist_copy = minDist; // 获取上一次计算的结果 - for (vector &side : grid) { - int from = side[0]; - int to = side[1]; - int price = side[2]; - //cout << f[0] << " " << f[1] << " " << f[2] << endl; - if (minDist_copy[from] != INT_MAX && minDist[to] > minDist_copy[from] + price) minDist[to] = minDist_copy[from] + price; - } - - } - - if (minDist[dst] == INT_MAX) cout << "unreachable" << endl; // 不能到达终点 - else cout << minDist[dst] << endl; // 到达终点最短路径 - -} - -``` diff --git a/problems/qita/xunlianying.md b/problems/qita/xunlianying.md index 0d8c1c83a6..f13290f302 100644 --- a/problems/qita/xunlianying.md +++ b/problems/qita/xunlianying.md @@ -1,7 +1,9 @@ # 代码随想录算法训练营 -> 训练营17期将在6月28日开营,目前可以报名,提前拉群,在群里等着开营就好! +::: tip 通知 +训练营35期,将于 4月3日开营,目前可以报名,报名后提前拉群,在群里等着开营就好。 +::: 大家可以百度搜索:代码随想录算法训练营, 看看往期录友们在训练营里打卡总结的博客。 @@ -9,14 +11,39 @@ 这是训练营里录友坚持到最后一天的打卡,大家可以看看他们的博客是每天都有记录的: -* [训练营结束,深感坚持是最难的(Java-犯困-东南研二)](https://blog.csdn.net/weixin_57956443/article/details/128995318) +* [这种方式,有效逼我坚持下来(C++-小飞-嘉院大三)(精华)](https://blog.csdn.net/weixin_60353640/article/details/133797799) +* [完成比完美重要(Java-小姜-已工作/南京)(精华)](https://xie.infoq.cn/article/3d07b4040ceab0f546d66e3e1) +* [已经刷了500题的基础,参加训练营依然收获满满(Java-怪懒懒-求职)(精华)](https://blog.csdn.net/2301_78266314/article/details/132144046) + +* [算法超级弱,最后坚持下来了(Java-信任呢-上大研二)(精华)](https://blog.csdn.net/xinrenne/article/details/133267089) +* [第一次比较完整的刷题训练经历,群里氛围超级好(JAVA-雷贯三十三重天-北航研二)(精华)](https://blog.csdn.net/qq_44120129/article/details/133230372) +* [我全程坚持下来,还是很有成就感的(python-wj-待业)(精华)](https://blog.csdn.net/u013441272/article/details/133229421) + +* [一点基础都没有,坚持下来了(C++ 润 大二)(精华)](https://blog.csdn.net/m0_74583479/article/details/132776719) +* [这个钱花的很值得(C++-GMZ-研一)(精华)](https://blog.csdn.net/weixin_43303286/article/details/132796571) +* [看着名单里录友都在坚持,自己也要坚持(C++-凯-湖工大研三)(精华)](https://blog.csdn.net/weixin_62453859/article/details/132788830) + +* [一刷心得(Java-小何同学-广财大二)(精华)](https://juejin.cn/post/7272250890597531684) +* [花钱买服务、买环境、买时间(Java-古今-大工研二)(精华)](https://blog.csdn.net/dannky_Z/article/details/132532049) +* [一刷心得(java-唔哩凹-大三)(精华)](https://blog.csdn.net/iwtup/article/details/132545456) + +* [训练营结束有点不舍,坚持最久的一件事(C++-徐一-中科院研二)(精华)](https://blog.csdn.net/weixin_46108098/article/details/132158352) +* [同学推荐,报名训练营,坚持下来了(c++-刘浩-沈自所-研二)(精华)](https://blog.csdn.net/qq1156148707/article/details/132155446) + +* [每日的刷题训练真的艰难,但坚持下来了(C++-五-已工作福建)(精华)](https://blog.csdn.net/weixin_44952586/article/details/131909720) +* [加入训练营,就是因为这个气氛,只靠自己很难坚持(cpp-Lord HouSton-cqu研二)(精华)](https://blog.csdn.net/HSL13594379250/article/details/131889934) +* [很幸运,我坚持下来了,感觉收货满满(java-李-UCAS研0)(精华)](https://blog.csdn.net/ResNet156/article/details/131920163) +* [谈谈自己的收获,养成了写博客的习惯(java-翌-研二)(精华)](https://blog.csdn.net/weixin_47460244/article/details/131912316) + +* [养成了刷题的习惯(C++-热心市民C先生-南理工研一)(精华)](https://blog.csdn.net/qqq1521902442/article/details/131614999) +* [工作也坚持下来(Python-Hongying-已工作杭州)(精华)](https://blog.csdn.net/weixin_42286468/article/details/131628069) +* [入营不亏(C++-小叶子-云财研二)(精华)](https://blog.csdn.net/dream_aleaf/article/details/131613667) + * [训练营一刷总结(Java-HQH-研二)](https://blog.csdn.net/weixin_43821876/article/details/128991822) * [训练营总结,一群人才能走的更远(Java-Lixy-已工作南京)](https://blog.csdn.net/weixin_45368277/article/details/128997823) * [训练营总结,中途🐑了,也坚持下来(C++-Jane-科大研二)](https://blog.csdn.net/Jane_10358/article/details/128977424) * [这两个月有很多不可控因素,但依然坚持下来(java-hha-南工大二)](https://blog.csdn.net/qerwtrt4t/article/details/128975401) * [训练营总结,最后坚持下来(C++ - 阿舟 - 已工作武汉)](https://blog.csdn.net/m0_74360161/article/details/129000723) -* [训练营总结,一刷知识点回顾(Java-魏-待就业)](https://blog.csdn.net/weixin_48111139/article/details/128973746) -* [在训练营中,零基础刷一遍的感受(C++-东风-东北大学研二)](https://blog.csdn.net/nightcood/article/details/128947111) 博客链接:[https://blog.csdn.net/m0_61724447/article/details/128443084](https://blog.csdn.net/m0_61724447/article/details/128443084) @@ -69,7 +96,7 @@ ----------- -### 训练营的目的是什么? +## 训练营的目的是什么? 对于刷题,学算法,[《代码随想录》](https://programmercarl.com/other/publish.html)(programmercarl.com)已经把刷题顺序给大家列好了,大家跟着刷就行。 @@ -97,7 +124,7 @@ 我亲自给大家规划节奏,大家一起按照我的节奏来,规定时间内,一刷一定能把代码随想录所有内容吃透,然后大家自己去二刷,三刷就好了,师傅领进门修行在个人。 -### 训练营提供一些什么呢? +## 训练营提供一些什么呢? 1.具体内容 @@ -180,7 +207,7 @@ 毕竟内容是开源的,质量如何 大家自己去看就好。 -### 训练营的学习方式 +## 训练营的学习方式 组织方式:一个学习微信群(180人左右),大家进群之后,等群公告就好,我会通知开始时间和每日刷题计划。 @@ -194,11 +221,11 @@ 所需语言:**所有语种都可以**,毕竟代码随想录几乎支持所有主流语言,**也会针对大家所用的语言做针对性答疑**。 -### 开营时间 +## 开营时间 -**训练营开始常态化报名,即一直可以报名,当人满180人的时候,就开始新的一期**。 最新的一期可以看文章评论区。 +**训练营开始常态化报名,即一直可以报名,当人满180人的时候,就开始新的一期**。 最新的一期可以看文章评论区,或者文章开头。 -### 训练营的价格 +## 训练营的价格 大家应该最关心的是价格了,**定价依然是268元**,注意这是两个月训练营的费用,而且是全程规划,指导,监督和答疑。 @@ -208,7 +235,7 @@ 后面一定会涨价的,**如果你确实需要有人带,有监督,给规划,有答疑,能花两个月时间更下来的话,还是早报早学习**。 -### 我适合报名吗? +## 我适合报名吗? 符合一下特点的录友可以报名: @@ -233,7 +260,7 @@ **训练营不限编程语言**,任何语言都可以报名,都会答疑。 -### 常见疑问 +## 常见疑问 **海外录友有时差可以报名吗**? @@ -265,22 +292,29 @@ 至于三个月的训练营,是可以考虑的,不过安排时间还要待定。 -### 报名方式 +## 报名方式 + +* 正常支付,价格:268 (支付成功后,支付记录发给客服 +* 知识星球录友支付,价格:238 (支付成功后,[代码随想录知识星球](https://programmercarl.com/other/kstar.html)截图 和 支付记录发给客服 +* 往期算法训练营录友再次报名,价格:130 (支付成功后,往期训练营群或者支付记录 和 本次支付记录发给客服 -扫码支付268元。 (如果是[代码随想录知识星球](https://programmercarl.com/other/kstar.html)成员录友,只需要支付238元,提交客服的时候需提供知识星球截图,**注意一定要是代码随想录知识星球**) +(**注意一定要是[代码随想录知识星球](https://programmercarl.com/other/kstar.html)成员才会有优惠**) + +支付宝支付如下:
-付款后,将付款截图发给客服,客服会在24h内统一回复,**所以大家发给客服信息不要急,当天一定会回复的**。 +[微信支付点击这里](https://www.programmercarl.com/other/weixinzhifu.html) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230603175235.png) +付款后,将付款截图发给客服,客服会在24h内统一回复,**所以大家发给客服信息不要急,当天一定会回复的**。 -客服的联系方式就在大家的微信聊天窗口,不用担心突出聊天窗口错过消息,客服回复之后 会有微信提示的。 +
关于训练营的任何问题,可以在客服这里咨询! -### 最后 + +## 最后 训练营其实算是代码随想录的一个补充,其内容都是免费开放的,有学习能力的录友自己学习就好。 @@ -294,5 +328,10 @@ 关于训练营的任何疑问都可以扫码联系客服 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230603175235.png) +
+ + + + + diff --git "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" deleted file mode 100644 index a1e553d0bc..0000000000 --- "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ /dev/null @@ -1,165 +0,0 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -# 时间复杂度 - -## 究竟什么是时间复杂度 - -**时间复杂度是一个函数,它定性描述该算法的运行时间**。 - -我们在软件开发中,时间复杂度就是用来方便开发者估算出程序运行的大体时间。 - -那么该如何估计程序运行时间呢,通常会估算算法的操作单元数量来代表程序消耗的时间,这里默认CPU的每个单元运行消耗的时间都是相同的。 - -假设算法的问题规模为n,那么操作单元数量便用函数f(n)来表示,随着数据规模n的增大,算法执行时间的增长率和f(n)的增长率相同,这称作为算法的渐近时间复杂度,简称时间复杂度,记为 O(f(n))。 - -## 什么是大O - -这里的大O是指什么呢,说到时间复杂度,**大家都知道O(n),O(n^2),却说不清什么是大O**。 - -算法导论给出的解释:**大O用来表示上界的**,当用它作为算法的最坏情况运行时间的上界,就是对任意数据输入的运行时间的上界。 - -同样算法导论给出了例子:拿插入排序来说,插入排序的时间复杂度我们都说是O(n^2) 。 - -输入数据的形式对程序运算时间是有很大影响的,在数据本来有序的情况下时间复杂度是O(n),但如果数据是逆序的话,插入排序的时间复杂度就是O(n^2),也就对于所有输入情况来说,最坏是O(n^2) 的时间复杂度,所以称插入排序的时间复杂度为O(n^2)。 - -同样的同理再看一下快速排序,都知道快速排序是O(nlog n),但是当数据已经有序情况下,快速排序的时间复杂度是O(n^2) 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是O(n^2)**。 - -**但是我们依然说快速排序是O(nlog n)的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示: -![时间复杂度4,一般情况下的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728185745611.png) - -我们主要关心的还是一般情况下的数据形式。 - -**面试中说道算法的时间复杂度是多少指的都是一般情况**。但是如果面试官和我们深入探讨一个算法的实现以及性能的时候,就要时刻想着数据用例的不一样,时间复杂度也是不同的,这一点是一定要注意的。 - - -## 不同数据规模的差异 - -如下图中可以看出不同算法的时间复杂度在不同数据输入规模下的差异。 - -![时间复杂度,不同数据规模的差异](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447384.png) - -在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。 - -就像上图中 O(5n^2) 和 O(100n) 在n为20之前 很明显 O(5n^2)是更优的,所花费的时间也是最少的。 - -那为什么在计算时间复杂度的时候要忽略常数项系数呢,也就说O(100n) 就是O(n)的时间复杂度,O(5n^2) 就是O(n^2)的时间复杂度,而且要默认O(n) 优于O(n^2) 呢 ? - -这里就又涉及到大O的定义,**因为大O就是数据量级突破一个点且数据量级非常大的情况下所表现出的时间复杂度,这个数据量也就是常数项系数已经不起决定性作用的数据量**。 - -例如上图中20就是那个点,n只要大于20 常数项系数已经不起决定性作用了。 - -**所以我们说的时间复杂度都是省略常数项系数的,是因为一般情况下都是默认数据规模足够的大,基于这样的事实,给出的算法时间复杂的的一个排行如下所示**: - -O(1) 常数阶 < O(\log n) 对数阶 < O(n) 线性阶 < O(n^2) 平方阶 < O(n^3) 立方阶 < O(2^n)指数阶 - -但是也要注意大常数,如果这个常数非常大,例如10^7 ,10^9 ,那么常数就是不得不考虑的因素了。 - -## 复杂表达式的化简 - -有时候我们去计算时间复杂度的时候发现不是一个简单的O(n) 或者O(n^2), 而是一个复杂的表达式,例如: - -``` -O(2*n^2 + 10*n + 1000) -``` - -那这里如何描述这个算法的时间复杂度呢,一种方法就是简化法。 - -去掉运行时间中的加法常数项 (因为常数项并不会因为n的增大而增加计算机的操作次数)。 - -``` -O(2*n^2 + 10*n) -``` - -去掉常数系数(上文中已经详细讲过为什么可以去掉常数项的原因)。 - -``` -O(n^2 + n) -``` - -只保留最高项,去掉数量级小一级的n (因为n^2 的数据规模远大于n),最终简化为: - -``` -O(n^2) -``` - -如果这一步理解有困难,那也可以做提取n的操作,变成O(n(n+1)),省略加法常数项后也就变成了: - -``` -O(n^2) -``` - -所以最后我们说:这个算法的算法时间复杂度是O(n^2) 。 - - -也可以用另一种简化的思路,其实当n大于40的时候, 这个复杂度会恒小于O(3 × n^2), -O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),3 × n^2省略掉常数项系数,最终时间复杂度也是O(n^2)。 - -## O(log n)中的log是以什么为底? - -平时说这个算法的时间复杂度是logn的,那么一定是log 以2为底n的对数么? - -其实不然,也可以是以10为底n的对数,也可以是以20为底n的对数,**但我们统一说 logn,也就是忽略底数的描述**。 - -为什么可以这么做呢?如下图所示: - -![时间复杂度1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447349.png) - - -假如有两个算法的时间复杂度,分别是log以2为底n的对数和log以10为底n的对数,那么这里如果还记得高中数学的话,应该不能理解`以2为底n的对数 = 以2为底10的对数 * 以10为底n的对数`。 - -而以2为底10的对数是一个常数,在上文已经讲述了我们计算时间复杂度是忽略常数项系数的。 - -抽象一下就是在时间复杂度的计算过程中,log以i为底n的对数等于log 以j为底n的对数,所以忽略了i,直接说是logn。 - -这样就应该不难理解为什么忽略底数了。 - -## 举一个例子 - -通过这道面试题目,来分析一下时间复杂度。题目描述:找出n个字符串中相同的两个字符串(假设这里只有两个相同的字符串)。 - -如果是暴力枚举的话,时间复杂度是多少呢,是O(n^2)么? - -这里一些同学会忽略了字符串比较的时间消耗,这里并不像int 型数字做比较那么简单,除了n^2次的遍历次数外,字符串比较依然要消耗m次操作(m也就是字母串的长度),所以时间复杂度是O(m × n × n)。 - -接下来再想一下其他解题思路。 - -先对n个字符串按字典序来排序,排序后n个字符串就是有序的,意味着两个相同的字符串就是挨在一起,然后再遍历一遍n个字符串,这样就找到两个相同的字符串了。 - -那看看这种算法的时间复杂度,快速排序时间复杂度为O(nlog n),依然要考虑字符串的长度是m,那么快速排序每次的比较都要有m次的字符比较的操作,就是O(m × n × log n)。 - -之后还要遍历一遍这n个字符串找出两个相同的字符串,别忘了遍历的时候依然要比较字符串,所以总共的时间复杂度是 O(m × n × log n + n × m)。 - -我们对O(m × n × log n + n × m)进行简化操作,把m × n提取出来变成O(m × n × (log n + 1)),再省略常数项最后的时间复杂度是O(m × n × log n)。 - -最后很明显O(m × n × log n) 要优于O(m × n × n)! - -所以先把字符串集合排序再遍历一遍找到两个相同字符串的方法要比直接暴力枚举的方式更快。 - -这就是我们通过分析两种算法的时间复杂度得来的结论。 - -**当然这不是这道题目的最优解,我仅仅是用这道题目来讲解一下时间复杂度**。 - -## 总结 - -本篇讲解了什么是时间复杂度,复杂度是用来干什么的,以及数据规模对时间复杂度的影响。 - -还讲解了被大多数同学忽略的大O的定义以及log究竟是以谁为底的问题。 - -再分析了如何简化复杂的时间复杂度,最后举一个具体的例子,把本篇的内容串起来。 - -相信看完本篇,大家对时间复杂度的认识会深刻很多! - -如果感觉「代码随想录」很不错,赶快推荐给身边的朋友同学们吧,他们发现和「代码随想录」相见恨晚! - - - - -

- - - - From ff75974e30459b23a44d18a9180e8c9def80de46 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Fri, 12 Apr 2024 16:33:29 +0800 Subject: [PATCH 1089/1533] =?UTF-8?q?fix:=20update=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=E5=92=8C=E6=95=B0=E7=BB=84=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" | 2 +- ...\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index 7550ce0254..d6a4e5401f 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -16,7 +16,7 @@ **数组是存放在连续内存空间上的相同类型数据的集合。** -数组可以方便的通过下标索引的方式获取到下标下对应的数据。 +数组可以方便的通过下标索引的方式获取到下标对应的数据。 举一个字符数组的例子,如图所示: diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index d104c883f7..1eb5b3f5a6 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -16,7 +16,7 @@ **数组是存放在连续内存空间上的相同类型数据的集合。** -数组可以方便的通过下标索引的方式获取到下标下对应的数据。 +数组可以方便的通过下标索引的方式获取到下标对应的数据。 举一个字符数组的例子,如图所示: From 3c9d6c408ac7286f19ddc096623b4454b3e8765c Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Fri, 12 Apr 2024 16:40:11 +0800 Subject: [PATCH 1090/1533] =?UTF-8?q?fix:=20update=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=E5=92=8C=E6=95=B0=E7=BB=84=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" | 2 +- ...\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index d6a4e5401f..646580368b 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -27,7 +27,7 @@ * **数组下标都是从0开始的。** * **数组内存空间的地址是连续的** -正是**因为数组的在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** +正是**因为数组在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** 例如删除下标为3的元素,需要对下标为3的元素后面的所有元素都要做移动操作,如图所示: diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index 1eb5b3f5a6..3492f4cc8c 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -29,7 +29,7 @@ * **数组下标都是从0开始的。** * **数组内存空间的地址是连续的** -正是**因为数组的在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** +正是**因为数组在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** 例如删除下标为3的元素,需要对下标为3的元素后面的所有元素都要做移动操作,如图所示: From 23c24d357e73eed31a46b58bf9bbc55047bfd2b7 Mon Sep 17 00:00:00 2001 From: WanpengXu Date: Fri, 12 Apr 2024 21:17:30 +0800 Subject: [PATCH 1091/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=200015.?= =?UTF-8?q?=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8C.md=20=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=B8=A5=E9=87=8D=E8=A1=A8=E8=BF=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\225\260\344\271\213\345\222\214.md" | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index bf165788f8..edd22ec103 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -50,36 +50,49 @@ ```CPP class Solution { public: + // 在一个数组中找到3个数形成的三元组,它们的和为0,不能重复使用(三数下标互不相同),且三元组不能重复。 + // 理论解法:a+b+c(存储)==0(检索) <=> c(存储)==0-(a+b)(检索) + // 实际解法:a+b+c(存储)==0(检索) <=> b(存储)==0-(a+c)(检索) vector> threeSum(vector& nums) { - vector> result; + // 本解法的内层循环一边存储一边检索,所以被存储的应该是b,而不是c + vector> res; sort(nums.begin(), nums.end()); - // 找出a + b + c = 0 - // a = nums[i], b = nums[j], c = -(a + b) + + // 如果只有正数,不可能形成和为0的三元组 + if (nums[0] > 0) + return res; + for (int i = 0; i < nums.size(); i++) { - // 排序之后如果第一个元素已经大于零,那么不可能凑成三元组 - if (nums[i] > 0) { - break; - } - if (i > 0 && nums[i] == nums[i - 1]) { //三元组元素a去重 + // [a, a, ...] 如果本轮a和上轮a相同,那么找到的b,c也是相同的,所以去重a + if (i > 0 && nums[i] == nums[i - 1]) continue; - } - unordered_set set; - for (int j = i + 1; j < nums.size(); j++) { - if (j > i + 2 - && nums[j] == nums[j-1] - && nums[j-1] == nums[j-2]) { // 三元组元素b去重 + + // 这个st的作用是存储b + unordered_set st; + + for (int k = i + 1; k < nums.size(); k++) { + // [(-2x), ..., (x), (x), x, x, x, ...] + // eg. [0, 0, 0] + // eg. [-4, 2, 2] + // eg. [(-4), -1, 0, 0, 1, (2), (2), {2}, {2}, 3, 3] + // 去重b=c时的b和c,即第三个x到最后一个x需要被跳过 + if (k > i + 2 && nums[k] == nums[k - 1] && nums[k - 1] == nums[k - 2]) continue; + + // a+b+c=0 <=> b=0-(a+c) + int target = 0 - (nums[i] + nums[k]); + if (st.find(target) != st.end()) { + res.push_back({nums[i], target, nums[k]}); // nums[k]成为c + // 内层循环中,a固定,如果find到了和上轮一样的b,那么c也就和上轮一样,所以去重b + st.erase(target); } - int c = 0 - (nums[i] + nums[j]); - if (set.find(c) != set.end()) { - result.push_back({nums[i], nums[j], c}); - set.erase(c);// 三元组元素c去重 - } else { - set.insert(nums[j]); + else { + st.insert(nums[k]); // nums[k]成为b } } } - return result; + + return res; } }; ``` From c71a9b00eb103a13e78c8ba3d807c758cde5a127 Mon Sep 17 00:00:00 2001 From: WanpengXu Date: Fri, 12 Apr 2024 22:14:08 +0800 Subject: [PATCH 1092/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=200015.?= =?UTF-8?q?=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8C.md=20=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=B8=A5=E9=87=8D=E8=A1=A8=E8=BF=B0=E9=94=99=E8=AF=AF=EF=BC=88?= =?UTF-8?q?update:=20=E7=BB=9F=E4=B8=80=E5=8F=98=E9=87=8F=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E5=9B=9E=E9=80=80=E5=8E=BB=E9=87=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\225\260\344\271\213\345\222\214.md" | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index edd22ec103..0d47bf4815 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -55,20 +55,20 @@ public: // 实际解法:a+b+c(存储)==0(检索) <=> b(存储)==0-(a+c)(检索) vector> threeSum(vector& nums) { // 本解法的内层循环一边存储一边检索,所以被存储的应该是b,而不是c - vector> res; + vector> result; sort(nums.begin(), nums.end()); - - // 如果只有正数,不可能形成和为0的三元组 - if (nums[0] > 0) - return res; for (int i = 0; i < nums.size(); i++) { + // 如果a是正数,a 0) + break; + // [a, a, ...] 如果本轮a和上轮a相同,那么找到的b,c也是相同的,所以去重a if (i > 0 && nums[i] == nums[i - 1]) continue; - // 这个st的作用是存储b - unordered_set st; + // 这个set的作用是存储b + unordered_set set; for (int k = i + 1; k < nums.size(); k++) { // [(-2x), ..., (x), (x), x, x, x, ...] @@ -81,18 +81,18 @@ public: // a+b+c=0 <=> b=0-(a+c) int target = 0 - (nums[i] + nums[k]); - if (st.find(target) != st.end()) { - res.push_back({nums[i], target, nums[k]}); // nums[k]成为c + if (set.find(target) != set.end()) { + result.push_back({nums[i], target, nums[k]}); // nums[k]成为c // 内层循环中,a固定,如果find到了和上轮一样的b,那么c也就和上轮一样,所以去重b - st.erase(target); + set.erase(target); } else { - st.insert(nums[k]); // nums[k]成为b + set.insert(nums[k]); // nums[k]成为b } } } - return res; + return result; } }; ``` From bdc06a4960a6ce17c4a279e9f0b8220a2a2b059f Mon Sep 17 00:00:00 2001 From: 502y <53784463+502y@users.noreply.github.com> Date: Sat, 13 Apr 2024 09:51:06 +0800 Subject: [PATCH 1093/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=20Dart=E7=9B=B8=E5=90=91?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index cb342586d9..5f681c5f8a 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -476,6 +476,32 @@ public class Solution { } ``` +###Dart: +```dart +int removeElement(List nums, int val) { + //相向双指针法 + var left = 0; + var right = nums.length - 1; + while (left <= right) { + //寻找左侧的val,将其被右侧非val覆盖 + if (nums[left] == val) { + while (nums[right] == val&&left<=right) { + right--; + if (right < 0) { + return 0; + } + } + nums[left] = nums[right--]; + } else { + left++; + } + } + //覆盖后可以将0至left部分视为所需部分 + return left; +} + +``` +

From a68d7c99370b6e2dd20c6485c1c408b8b0bf5327 Mon Sep 17 00:00:00 2001 From: Nicolas Leigh Date: Thu, 18 Apr 2024 13:21:39 +0800 Subject: [PATCH 1094/1533] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a break statement to break the for-loop in the Typescript solution. --- .../0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 80218cb535..d40cade805 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -349,6 +349,7 @@ function twoSum(nums: number[], target: number): number[] { index = helperMap.get(target - nums[i]); if (index !== undefined) { resArr = [i, index]; + break; } helperMap.set(nums[i], i); } From 62cfba1210951f64a726567170705ecbf1866886 Mon Sep 17 00:00:00 2001 From: Shuxiao Wang Date: Fri, 19 Apr 2024 09:52:36 +0200 Subject: [PATCH 1095/1533] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20`0063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md`=20=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BC=A9=E8=BF=9B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 8c208ea865..2419b6a8fd 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -145,7 +145,7 @@ public: int uniquePathsWithObstacles(vector>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); - if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0 + if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0 return 0; vector> dp(m, vector(n, 0)); for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1; From 9fdaeb5b9075ed5195e828d9426c1dd98a7212ec Mon Sep 17 00:00:00 2001 From: Vox Dai Date: Sun, 21 Apr 2024 09:16:13 +0800 Subject: [PATCH 1096/1533] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md=20/?= =?UTF-8?q?=20Fix=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 部分Markdown代码块没有标注语言导致代码没有高亮 --- ...\204\346\234\200\345\260\217\346\267\261\345\272\246.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 6d1632d593..9619b75b40 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -64,7 +64,7 @@ 代码如下: -``` +```CPP int getDepth(TreeNode* node) ``` @@ -74,14 +74,14 @@ int getDepth(TreeNode* node) 代码如下: -``` +```CPP if (node == NULL) return 0; ``` 3. 确定单层递归的逻辑 这块和求最大深度可就不一样了,一些同学可能会写如下代码: -``` +```CPP int leftDepth = getDepth(node->left); int rightDepth = getDepth(node->right); int result = 1 + min(leftDepth, rightDepth); From 19b76720a2277f7ca8a72ffdf7e1a7c810bfe951 Mon Sep 17 00:00:00 2001 From: Vox Dai Date: Sun, 21 Apr 2024 09:37:42 +0800 Subject: [PATCH 1097/1533] =?UTF-8?q?Update=20'''0222.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA?= =?UTF-8?q?=E6=95=B0.md'''=20Fix=20Typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复部分代码块无高亮 --- ...\204\350\212\202\347\202\271\344\270\252\346\225\260.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index d93d2a3381..69403607c3 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -54,7 +54,7 @@ 1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。 代码如下: -``` +```CPP int getNodesNum(TreeNode* cur) { ``` @@ -62,7 +62,7 @@ int getNodesNum(TreeNode* cur) { 代码如下: -``` +```CPP if (cur == NULL) return 0; ``` @@ -70,7 +70,7 @@ if (cur == NULL) return 0; 代码如下: -``` +```CPP int leftNum = getNodesNum(cur->left); // 左 int rightNum = getNodesNum(cur->right); // 右 int treeNum = leftNum + rightNum + 1; // 中 From 183704ff5b90ffb7420ffd23d9d62a0800af3a1d Mon Sep 17 00:00:00 2001 From: Vox Dai Date: Sun, 21 Apr 2024 10:19:11 +0800 Subject: [PATCH 1098/1533] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md=20/?= =?UTF-8?q?=20Fix=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复部分代码块无高亮 --- ...04\346\211\200\346\234\211\350\267\257\345\276\204.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 4c6c92c5b4..fb2b0d144f 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -40,7 +40,7 @@ 要传入根节点,记录每一条路径的path,和存放结果集的result,这里递归不需要返回值,代码如下: -``` +```CPP void traversal(TreeNode* cur, vector& path, vector& result) ``` @@ -48,7 +48,7 @@ void traversal(TreeNode* cur, vector& path, vector& result) 在写递归的时候都习惯了这么写: -``` +```CPP if (cur == NULL) { 终止处理逻辑 } @@ -59,7 +59,7 @@ if (cur == NULL) { **那么什么时候算是找到了叶子节点?** 是当 cur不为空,其左右孩子都为空的时候,就找到叶子节点。 所以本题的终止条件是: -``` +```CPP if (cur->left == NULL && cur->right == NULL) { 终止处理逻辑 } @@ -102,7 +102,7 @@ if (cur->left == NULL && cur->right == NULL) { // 遇到叶子节点 所以递归前要加上判断语句,下面要递归的节点是否为空,如下 -``` +```CPP if (cur->left) { traversal(cur->left, path, result); } From 1f94655e9a3f2a91075b43477ef2cbe648891083 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Tue, 23 Apr 2024 15:26:56 +0800 Subject: [PATCH 1099/1533] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=960019.?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0?= =?UTF-8?q?=E7=AC=ACN=E4=B8=AA=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 9e180f753e..4a973edb73 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -22,10 +22,12 @@ 输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] + 示例 2: 输入:head = [1], n = 1 输出:[] + 示例 3: 输入:head = [1,2], n = 1 @@ -193,16 +195,18 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode { * @param {number} n * @return {ListNode} */ -var removeNthFromEnd = function(head, n) { - let ret = new ListNode(0, head), - slow = fast = ret; - while(n--) fast = fast.next; - while (fast.next !== null) { - fast = fast.next; - slow = slow.next - }; - slow.next = slow.next.next; - return ret.next; +var removeNthFromEnd = function (head, n) { + // 创建哨兵节点,简化解题逻辑 + let dummyHead = new ListNode(0, head); + let fast = dummyHead; + let slow = dummyHead; + while (n--) fast = fast.next; + while (fast.next !== null) { + slow = slow.next; + fast = fast.next; + } + slow.next = slow.next.next; + return dummyHead.next; }; ``` ### TypeScript: From 956e8530f1323ce404788844d8ac7272bdf6cc74 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 24 Apr 2024 16:08:28 +0800 Subject: [PATCH 1100/1533] Update --- ...50\345\271\263\346\226\271\346\225\260.md" | 6 +- ...16\351\200\233\345\205\254\345\233\255.md" | 57 --- ...17\202\344\274\232dijkstra\345\240\206.md" | 8 +- ...74\232dijkstra\346\234\264\347\264\240.md" | 2 +- .../0053.\345\257\273\345\256\235-Kruskal.md" | 0 .../0053.\345\257\273\345\256\235-prim.md" | 0 ...77\346\215\242\346\225\260\345\255\227.md" | 0 ...13\345\255\227\347\254\246\344\270\262.md" | 0 ...\211\251\350\277\220\350\276\223I-SPFA.md" | 138 +++++-- ...7\347\211\251\350\277\220\350\276\223I.md" | 3 + ...\347\211\251\350\277\220\350\276\223II.md" | 89 ++++- ...347\211\251\350\277\220\350\276\223III.md" | 211 +++++++++- ...16\351\200\233\345\205\254\345\233\255.md" | 367 ++++++++++++++++++ 13 files changed, 768 insertions(+), 113 deletions(-) delete mode 100644 "problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" rename "problems/kama0047.\345\217\202\344\274\232dijkstra\345\240\206.md" => "problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" (98%) rename "problems/kama0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" => "problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" (99%) rename "problems/kama0053.\345\257\273\345\256\235-Kruskal.md" => "problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" (100%) rename "problems/kama0053.\345\257\273\345\256\235-prim.md" => "problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" (100%) rename "problems/kama0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" => "problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" (100%) rename "problems/kama0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" => "problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" (100%) rename "problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" => "problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" (57%) rename "problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" => "problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" (98%) rename "problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" => "problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" (72%) rename "problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" => "problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" (68%) create mode 100644 "problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index f7c06dbd87..b71b69f473 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -219,7 +219,7 @@ class Solution { ### Python: -先遍历物品, 再遍历背包 +先遍历背包, 再遍历物品 ```python class Solution: def numSquares(self, n: int) -> int: @@ -234,7 +234,7 @@ class Solution: return dp[n] ``` -先遍历背包, 再遍历物品 +先遍历物品, 再遍历背包 ```python class Solution: def numSquares(self, n: int) -> int: @@ -389,7 +389,7 @@ function numSquares(n: number): number { }; ``` -## C +### C ```c #define min(a, b) ((a) > (b) ? (b) : (a)) diff --git "a/problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" deleted file mode 100644 index 1000eed75d..0000000000 --- "a/problems/kama0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ /dev/null @@ -1,57 +0,0 @@ - -# Floyd 算法精讲 - -[卡码网:97. 小明逛公园](https://kamacoder.com/problempage.php?pid=1155) - -【题目描述】 - -小明喜欢去公园散步,公园内布置了许多的景点,相互之间通过小路连接,小明希望在观看景点的同时,能够节省体力,走最短的路径。 - - -给定一个公园景点图,图中有 N 个景点(编号为 1 到 N),以及 M 条双向道路连接着这些景点。每条道路上行走的距离都是已知的。 - - -小明有 Q 个观景计划,每个计划都有一个起点 start 和一个终点 end,表示他想从景点 start 前往景点 end。由于小明希望节省体力,他想知道每个观景计划中从起点到终点的最短路径长度。 请你帮助小明计算出每个观景计划的最短路径长度。 - -【输入描述】 - -第一行包含两个整数 N, M, 分别表示景点的数量和道路的数量。 - -接下来的 M 行,每行包含三个整数 u, v, w,表示景点 u 和景点 v 之间有一条长度为 w 的双向道路。 - -接下里的一行包含一个整数 Q,表示观景计划的数量。 - -接下来的 Q 行,每行包含两个整数 start, end,表示一个观景计划的起点和终点。 - -【输出描述】 - -对于每个观景计划,输出一行表示从起点到终点的最短路径长度。如果两个景点之间不存在路径,则输出 -1。 - -【输入示例】 - -7 3 -1 2 4 -2 5 6 -3 6 8 -2 -1 2 -2 3 - -【输出示例】 - -4 --1 - -【提示信息】 - -从 1 到 2 的路径长度为 4,2 到 3 之间并没有道路。 - -1 <= N, M, Q <= 1000. - -## 思路 - -本题是经典的多源最短路问题。 - -我们之前讲解过的算法,dijkstra, - - diff --git "a/problems/kama0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" similarity index 98% rename from "problems/kama0047.\345\217\202\344\274\232dijkstra\345\240\206.md" rename to "problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index 0cb5930182..b93138a78a 100644 --- "a/problems/kama0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -19,7 +19,7 @@ 【输出描述】 -输出一个整数,代表小明在途中和其他科学家和科研团队交流所花费的最少时间。 +输出一个整数,代表小明从起点到终点所花费的最小时间。 输入示例 @@ -519,7 +519,7 @@ int main() { 所以边添加一次时间复杂度是 O(E), `while (!pq.empty())` 里每次都要弹出一个边来进行操作,在优先级队列(小顶堆)中 弹出一个元素的时间复杂度是 O(logE) ,这是堆排序的时间复杂度。 -(当然小顶堆里 是 添加元素的时候 排序,还是 取数元素的时候排序,这个无所谓,时间复杂度都是O(E),总是是一定要排序的,而小顶堆里也不会滞留元素,有多少元素添加 一定就有多少元素弹出) +(当然小顶堆里 是 添加元素的时候 排序,还是 取数元素的时候排序,这个无所谓,时间复杂度都是O(E),总之是一定要排序的,而小顶堆里也不会滞留元素,有多少元素添加 一定就有多少元素弹出) 所以 该算法整体时间复杂度为 O(ElogE) @@ -537,7 +537,7 @@ int main() { 也行的。 -但 正是因为稀疏图,所以我们使用堆优化的思路, 如果我们还用 邻接矩阵 去表达这个图的话,就是 一个高效的算法 使用了低效的数据结构,那么 整体算法效率 依然是低的。 +但 正是因为稀疏图,所以我们使用堆优化的思路, 如果我们还用 邻接矩阵 去表达这个图的话,就是 **一个高效的算法 使用了低效的数据结构,那么 整体算法效率 依然是低的**。 如果还不清楚为什么要使用 邻接表,可以再看看上面 我在 「图的存储」标题下的讲解。 @@ -626,7 +626,7 @@ int main() { 正如我在开篇就给大家交代清楚 堆优化方式的背景。 -堆优化的整体思路和 朴素版是大体一样的,区别是 堆优化从边的角度触发,且利用堆来排序。 +堆优化的整体思路和 朴素版是大体一样的,区别是 堆优化从边的角度出发且利用堆来排序。 很多录友别说写堆优化 就是看 堆优化的代码也看的很懵。 diff --git "a/problems/kama0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" similarity index 99% rename from "problems/kama0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" rename to "problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 957d471afd..295d3aec15 100644 --- "a/problems/kama0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -19,7 +19,7 @@ 【输出描述】 -输出一个整数,代表小明在途中和其他科学家和科研团队交流所花费的最少时间。 +输出一个整数,代表小明从起点到终点所花费的最小时间。 输入示例 diff --git "a/problems/kama0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" similarity index 100% rename from "problems/kama0053.\345\257\273\345\256\235-Kruskal.md" rename to "problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" diff --git "a/problems/kama0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" similarity index 100% rename from "problems/kama0053.\345\257\273\345\256\235-prim.md" rename to "problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" diff --git "a/problems/kama0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" similarity index 100% rename from "problems/kama0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" rename to "problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" diff --git "a/problems/kama0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" similarity index 100% rename from "problems/kama0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" rename to "problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" diff --git "a/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" similarity index 57% rename from "problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" rename to "problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 634a33a04e..f41d6fd121 100644 --- "a/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -1,7 +1,7 @@ # Bellman_ford 队列优化算法(又名SPFA) -[卡码网: 94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) +[卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) 题目描述 @@ -16,6 +16,8 @@ 城市 1 到城市 n 之间可能会出现没有路径的情况,同时保证道路网络中不存在任何负权回路。 +> 负权回路是指一系列道路的总权值为负,这样的回路使得通过反复经过回路中的道路,理论上可以无限地减少总成本或无限地增加总收益。 + 输入描述 第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 @@ -68,7 +70,7 @@ 基于以上思路,如何记录 上次松弛的时候更新过的节点呢? -用队列来记录。 +用队列来记录。(其实用栈也行,对元素顺序没有要求) 接下来来举例这个队列是如何工作的。 @@ -115,7 +117,7 @@ 将节点4,节点5 加入队列,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115527.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110348.png) -------------------- @@ -125,7 +127,7 @@ 因为没有从节点3作为出发点的边,所以这里就从队列里取出节点3就好,不用做其他操作,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115515.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110420.png) ------------ @@ -138,7 +140,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115451.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110445.png) --------------- @@ -151,10 +153,13 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115436.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110509.png) + + +因为节点3,和 节点6 都曾经加入过队列,不用重复加入,避免重复计算。 -因为节点3,和 节点6 都曾经加入过队列,不用重复加入,避免重复计算。 +在代码中我们可以用一个数组 visited 来记录入过队列的元素,加入过队列的元素,不再重复入队列。 -------------- @@ -172,16 +177,16 @@ 这样我们就完成了基于队列优化的bellman_ford的算法模拟过程。 -大家可以发现 基于队列优化的算法,要比bellman_ford 算法 减少很多无用的松弛情况,特别是对于边树众多的大图 优化效果明显。 +大家可以发现 基于队列优化的算法,要比bellman_ford 算法 减少很多无用的松弛情况,特别是对于边数众多的大图 优化效果明显。 了解了大体流程,我们再看代码应该怎么写。 在上面模拟过程中,我们每次都要知道 一个节点作为出发点 链接了哪些节点。 -如果想方便这道这些数据,就需要使用邻接表来存储这个图,如果对于邻接表不了解的话,可以看 [kama0047.参会dijkstra堆](./kama0047.参会dijkstra堆.md) 中 图的存储 部分。 +如果想方便知道这些数据,就需要使用邻接表来存储这个图,如果对于邻接表不了解的话,可以看 [kama0047.参会dijkstra堆](./kama0047.参会dijkstra堆.md) 中 图的存储 部分。 -代码如下: +整体代码如下: ```CPP #include @@ -218,24 +223,19 @@ int main() { minDist[start] = 0; queue que; - que.push(start); - int que_size; + que.push(start); // 队列里放入起点 + while (!que.empty()) { - // 注意这个数组放的位置 - vector visited(n + 1, false); // 可加,可不加,加了效率高一些,防止队列里重复访问,其数值已经算过了 - que_size = que.size(); int node = que.front(); que.pop(); for (Edge edge : grid[node]) { int from = node; int to = edge.to; - int price = edge.val; - if (minDist[to] > minDist[from] + price) { // 开始松弛 - minDist[to] = minDist[from] + price; - if(visited[to]) continue; // 节点不用重复放入队列,但节点需要重复计算,所以放在这里位置 - visited[to] = true; - que.push(to); + int value = edge.val; + if (minDist[to] > minDist[from] + value) { // 开始松弛 + minDist[to] = minDist[from] + value; + que.push(to); } } @@ -244,41 +244,103 @@ int main() { if (minDist[end] == INT_MAX) cout << "unconnected" << endl; // 不能到达终点 else cout << minDist[end] << endl; // 到达终点最短路径 } + ``` -代码中有一点需要注意,即 `if(visited[to]) continue;` 这段代码放的位置。 +## 效率分析 + +队列优化版Bellman_ford 的时间复杂度 并不稳定,效率高低依赖于图的结构。 + +例如 如果是一个双向图,且每一个节点和所有其他节点都相连的话,那么该算法的时间复杂度就接近于 Bellman_ford 的 O(N * E) N 为节点数量,E为边的数量。 + +在这种图中,每一个节点都会重复加入队列 n - 1次,因为 这种图中 每个节点 都有 n-1 条指向该节点的边,每条边指向该节点,就需要加入一次队列。(如果这里看不懂,可以在重温一下代码逻辑) + +至于为什么 双向图且每一个节点和所有其他节点都相连的话,每个节点 都有 n-1 条指向该节点的边, 我再来举个例子,如图: + +[](https://code-thinking-1253855093.file.myqcloud.com/pics/20240416104138.png) + +图中 每个节点都与其他所有节点相连,节点数n 为 4,每个节点都有3条指向该节点的边,即入度为3。 +n为其他数值的时候,也是一样的。 -一些录友可能写成这样: +当然这种图是比较极端的情况,也是最稠密的图。 + +所以如果图越稠密,则 SPFA的效率越接近与 Bellman_ford。 + +反之,图越稀疏,SPFA的效率就越高。 + +一般来说,SPFA 的时间复杂度为 O(K * N) K 为不定值,因为 节点需要计入几次队列取决于 图的稠密度。 + +如果图是一条线形图且单向的话,每个节点的入度为1,那么只需要加入一次队列,这样时间复杂度就是 O(N)。 + +所以 SPFA 在最坏的情况下是 O(N * E),但 一般情况下 时间复杂度为 O(K * N)。 + +尽管如此,**以上分析都是 理论上的时间复杂度分析**。 + +并没有计算 出队列 和 入队列的时间消耗。 因为这个在不同语言上 时间消耗也是不一定的。 + +以C++为例,以下两端代码理论上,时间复杂度都是 O(n) : ```CPP -if (minDist[to] > minDist[from] + price) { // 开始松弛 - if(visited[to]) continue; - minDist[to] = minDist[from] + price; - visited[to] = true; - que.push(to); +for (long long i = 0; i < n; i++) { + k++; } -``` - -这是不对了,我们仅仅是控制节点不用重复加入队列,但对于边的松弛,节点数值的更新,是要重复计算的,要不然如何 不断更新最短路径呢? -所以 `if(visited[to]) continue;` 应该放在这里: +``` ```CPP -if (minDist[to] > minDist[from] + price) { // 开始松弛 - minDist[to] = minDist[from] + price; - if(visited[to]) continue; // 仅仅控制节点不要重复加入队列 - visited[to] = true; - que.push(to); +for (long long i = 0; i < n; i++) { + que.push(i); + que.front(); + que.pop(); } + ``` +在 MacBook Pro (13-inch, M1, 2020) 机器上分别测试这两段代码的时间消耗情况: + +* n = 10^4,第一段代码的时间消耗:1ms,第二段代码的时间消耗: 4 ms +* n = 10^5,第一段代码的时间消耗:1ms,第二段代码的时间消耗: 13 ms +* n = 10^6,第一段代码的时间消耗:4ms,第二段代码的时间消耗: 59 ms +* n = 10^7,第一段代码的时间消耗: 24ms,第二段代码的时间消耗: 463 ms +* n = 10^8,第一段代码的时间消耗: 135ms,第二段代码的时间消耗: 4268 ms + +在这里就可以看出 出队列和入队列 其实也是十分耗时的。 + +SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹,但实际上,也要看图的稠密程度,如果 图很大且非常稠密的情况下,虽然 SPFA的时间复杂度接近Bellman_ford,但实际时间消耗 可能是 SPFA耗时更多。 +针对这种情况,我在后面题目讲解中,会特别加入稠密图的测试用例来给大家讲解。 ## 拓展 -关于 加visited 方式节点重复方便,可能也有录友认为,加上 visited 也是防止 如果图中出现了环的话,会导致的 队列里一直不为空。 +这里可能有录友疑惑,`while (!que.empty())` 队里里 会不会造成死循环? 例如 图中有环,这样一直有元素加入到队列里? + +其实有环的情况,要看它是 正权回路 还是 负全回路。 + +题目描述中,已经说了,本题没有 负权回路 。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412111849.png) + +正权回路 就是有环,但环的总权值为正数。 + +在有环且只有正权回路的情况下,即使元素重复加入队列,最后,也会因为 所有边都松弛后,节点数值(minDist数组)不在发生变化了 而终止。 + +(而且有重复元素加入队列是正常的,多条路径到达同一个节点,节点必要要选择一个最短的路径,而这个节点就会重复加入队列进行判断,选一个最短的) + +在[0094.城市间货物运输I](./0094.城市间货物运输I.md) 中我们讲过对所有边 最多松弛 n -1 次,就一定可以求出所有起点到所有节点的最小距离即 minDist数组。 + +即使再松弛n次以上, 所有起点到所有节点的最小距离(minDist数组) 不会再变了。 (这里如果不理解,建议认真看[0094.城市间货物运输I](./0094.城市间货物运输I.md)讲解) + +所以本题我们使用队列优化,有元素重复加入队列,也会因为最后 minDist数组 不会在发生变化而终止。 + +节点再加入队列,需要有松弛的行为, 而 每个节点已经都计算出来 起点到该节点的最短路径,那么就不会有 执行这个判断条件`if (minDist[to] > minDist[from] + value)`,从而不会有新的节点加入到队列。 + +但如果本题有 负权回路,那情况就不一样了,我在下一题目讲解中,会重点讲解 负权回路 带来的变化。 + + diff --git "a/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" similarity index 98% rename from "problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" rename to "problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 50c59ea7c0..0ce00bbf1d 100644 --- "a/problems/kama0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -16,6 +16,8 @@ 城市 1 到城市 n 之间可能会出现没有路径的情况,同时保证道路网络中不存在任何负权回路。 +> 负权回路是指一系列道路的总权值为负,这样的回路使得通过反复经过回路中的道路,理论上可以无限地减少总成本或无限地增加总收益。 + 输入描述 第一行包含两个正整数,第一个正整数 n 表示该国一共有 n 个城市,第二个整数 m 表示这些城市中共有 m 条道路。 @@ -55,6 +57,7 @@ **Bellman_ford算法的核心思想是 对所有边进行松弛n-1次操作(n为节点数量),从而求得目标最短路**。 +## 什么叫做松弛 看到这里,估计大家都比较晕了,为什么是 n-1 次,那“松弛”这两个字究竟是个啥意思? diff --git "a/problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" similarity index 72% rename from "problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" rename to "problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index e9387c7312..1dd14f5816 100644 --- "a/problems/kama0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -11,7 +11,7 @@ 权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 -然而,在评估从城市 1 到城市 n 的所有可能路径中综合政府补贴后的最低运输成本时,存在一种情况:图中可能出现负权回路。 +然而,在评估从城市 1 到城市 n 的所有可能路径中综合政府补贴后的最低运输成本时,存在一种情况:**图中可能出现负权回路**。 负权回路是指一系列道路的总权值为负,这样的回路使得通过反复经过回路中的道路,理论上可以无限地减少总成本或无限地增加总收益。 @@ -151,3 +151,90 @@ int main() { * 时间复杂度: O(N * E) , N为节点数量,E为图中边的数量 * 空间复杂度: O(N) ,即 minDist 数组所开辟的空间 + +## 拓展 + +本题可不可 使用 队列优化版的bellman_ford(SPFA)呢? + +上面的解法中,我们对所有边松弛了n-1次后,在松弛一次,如果出现minDist出现变化就判断有负权回路。 + +如果使用 SPFA 那么节点都是进队列的,那么节点进入队列几次后 足够判断该图是否有负权回路呢? + +在 [0094.城市间货物运输I-SPFA](./0094.城市间货物运输I-SPFA) 中,我们讲过 在极端情况下,即:所有节点都与其他节点相连,每个节点的入度为 n-1 (n为节点数量),所以每个节点最多加入 n-1 次队列。 + +那么如果节点加入队列的次数 超过了 n-1次 ,那么该图就一定有负权回路。 + +所以本题也是可以使用 SPFA 来做的。 代码如下: + +```CPP +#include +#include +#include +#include +#include +using namespace std; + +struct Edge { //邻接表 + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1); // 邻接表 + + // 将所有边保存起来 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid[p1].push_back(Edge(p2, val)); + } + int start = 1; // 起点 + int end = n; // 终点 + + vector minDist(n + 1 , INT_MAX); + minDist[start] = 0; + + queue que; + que.push(start); // 队列里放入起点 + + vector count(n+1, 0); // 记录节点加入队列几次 + count[start]++; + + bool flag = false; + while (!que.empty()) { + + int node = que.front(); que.pop(); + + for (Edge edge : grid[node]) { + int from = node; + int to = edge.to; + int value = edge.val; + if (minDist[to] > minDist[from] + value) { // 开始松弛 + minDist[to] = minDist[from] + value; + que.push(to); + count[to]++; + if (count[to] == n) {// 如果加入队列次数超过 n-1次 就说明该图与负权回路 + flag = true; + while (!que.empty()) que.pop(); + break; + } + } + } + } + + if (flag) cout << "circle" << endl; + else if (minDist[end] == INT_MAX) { + cout << "unconnected" << endl; + } else { + cout << minDist[end] << endl; + } + +} + +``` diff --git "a/problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" similarity index 68% rename from "problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" rename to "problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index 10fb03803a..c9d97a9d8e 100644 --- "a/problems/kama0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -65,7 +65,7 @@ 图中,节点2 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 -所以本题就是求,起点最多经过k + 1 条边到达终点的最短距离。 +所以本题就是求:起点最多经过k + 1 条边到达终点的最短距离。 对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离,那么对所有边松弛 k + 1次,就是求 起点到达 与起点k + 1条边相连的节点的 最短距离。 @@ -339,7 +339,7 @@ int main() { 其实这是和示例中给出的边的顺序是有关的, -我们按照我修改后的示例再来模拟 对所有边的第一次拓展情况。 +我们按照修改后的示例再来模拟 对所有边的第一次拓展情况。 初始化: @@ -366,20 +366,17 @@ int main() { 那么前面讲解过的 [94.城市间货物运输I](./kama94.城市间货物运输I.md) 和 [95.城市间货物运输II](./kama95.城市间货物运输II.md) 也是bellman_ford经典算法,也没使用 minDist_copy,怎么就没问题呢? -> 如果没看过我上面这两篇讲解的话,建议详细学习上面两篇,在看我下面讲的区别,否则容易看不懂。 +> 如果没看过我上面这两篇讲解的话,建议详细学习上面两篇,再看我下面讲的区别,否则容易看不懂。 [94.城市间货物运输I](./kama94.城市间货物运输I.md), 是没有 负权回路的,那么 多松弛多少次,对结果都没有影响。 求 节点1 到 节点n 的最短路径,松弛n-1 次就够了,松弛 大于 n-1次,结果也不会变。 -那么在对所有边进行第一次松弛的时候,如果基于 最近计算的 minDist 来计算 minDist (相当于多做松弛了),也是对最终结果没影响。 +那么在对所有边进行第一次松弛的时候,如果基于 本次计算的 minDist 来计算 minDist (相当于多做松弛了),也是对最终结果没影响。 -[95.城市间货物运输II](./kama95.城市间货物运输II.md) 是判断是否有 负权回路,一旦有负权回路, 对所有边松弛 n -1 次以后,在做松弛 minDist 数值一定会变,根据这一点是判断是否有负权回路。 - -所以 在对所有边进行第一次松弛的时候,如果基于 最近计算的 minDist 来计算 minDist (相当于多做松弛了),对最后判断是否有负权回路同样没有影响。 - -你可以理解 minDist的数组其实是不准确了,但它只要变化了就可以让我们来判断 是否有 负权回路。 +[95.城市间货物运输II](./kama95.城市间货物运输II.md) 是判断是否有 负权回路,一旦有负权回路, 对所有边松弛 n-1 次以后,在做松弛 minDist 数值一定会变,根据这一点来判断是否有负权回路。 +所以,[95.城市间货物运输II](./kama95.城市间货物运输II.md) 只需要判断minDist数值变化了就行,而 minDist 的数值对不对,并不是我们关心的。 那么本题 为什么计算minDist 一定要基于上次 的 minDist 数值。 @@ -390,3 +387,199 @@ int main() { 如果本题中 没有负权回路的测试用例, 那版本一的代码就可以过了,也就不用我费这么大口舌去讲解的这个坑了。 +## 拓展三(SPFA) + +本题也可以用 SPFA来做,关于 SPFA ,已经在这里 [0094.城市间货物运输I-SPFA](./0094.城市间货物运输I-SPFA.md) 有详细讲解。 + +使用SPFA算法解决本题的时候,关键在于 如何控制松弛k次。 + +其实实现不难,但有点技巧,可以用一个变量 que_size 记录每一轮松弛入队列的所有节点数量。 + +下一轮松弛的时候,就把队列里 que_size 个节点都弹出来,就是上一轮松弛入队列的节点。 + +代码如下(详细注释) + +```CPP +#include +#include +#include +#include +#include +using namespace std; + +struct Edge { //邻接表 + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1); // 邻接表 + + // 将所有边保存起来 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid[p1].push_back(Edge(p2, val)); + } + int start, end, k; + cin >> start >> end >> k; + + k++; + + vector minDist(n + 1 , INT_MAX); + vector minDist_copy(n + 1); // 用来记录每一次遍历的结果 + + minDist[start] = 0; + + queue que; + que.push(start); // 队列里放入起点 + + int que_size; + while (k-- && !que.empty()) { + + minDist_copy = minDist; // 获取上一次计算的结果 + que_size = que.size(); // 记录上次入队列的节点个数 + while (que_size--) { // 上一轮松弛入队列的节点,这次对应的边都要做松弛 + int node = que.front(); que.pop(); + for (Edge edge : grid[node]) { + int from = node; + int to = edge.to; + int price = edge.val; + if (minDist[to] > minDist_copy[from] + price) { + minDist[to] = minDist_copy[from] + price; + que.push(to); + } + } + + } + } + if (minDist[end] == INT_MAX) cout << "unreachable" << endl; + else cout << minDist[end] << endl; + +} + +``` + +时间复杂度: O(K * H) H 为不确定数,取决于 图的稠密度,但H 一定是小于等于 E 的 + +关于 SPFA的是时间复杂度分析,我在[0094.城市间货物运输I-SPFA](./0094.城市间货物运输I-SPFA.md) 有详细讲解 + +但大家会发现,以上代码大家提交后,怎么耗时这么多? + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418113308.png) + +理论上,SPFA的时间复杂度不是要比 bellman_ford 更优吗? + +怎么耗时多了这么多呢? + +以上代码有一个可以改进的点,每一轮松弛中,重复节点可以不用入队列。 + +因为重复节点入队列,下次从队列里取节点的时候,该节点要取很多次,而且都是重复计算。 + +所以代码可以优化成这样: + +```CPP +#include +#include +#include +#include +#include +using namespace std; + +struct Edge { //邻接表 + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1); // 邻接表 + + // 将所有边保存起来 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid[p1].push_back(Edge(p2, val)); + } + int start, end, k; + cin >> start >> end >> k; + + k++; + + vector minDist(n + 1 , INT_MAX); + vector minDist_copy(n + 1); // 用来记录每一次遍历的结果 + + minDist[start] = 0; + + queue que; + que.push(start); // 队列里放入起点 + + int que_size; + while (k-- && !que.empty()) { + + vector visited(n + 1, false); // 每一轮松弛中,控制节点不用重复入队列 + minDist_copy = minDist; + que_size = que.size(); + while (que_size--) { + int node = que.front(); que.pop(); + for (Edge edge : grid[node]) { + int from = node; + int to = edge.to; + int price = edge.val; + if (minDist[to] > minDist_copy[from] + price) { + minDist[to] = minDist_copy[from] + price; + if(visited[to]) continue; // 不用重复放入队列,但需要重复松弛,所以放在这里位置 + visited[to] = true; + que.push(to); + } + } + + } + } + if (minDist[end] == INT_MAX) cout << "unreachable" << endl; + else cout << minDist[end] << endl; +} +``` + +以上代码提交后,耗时情况: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418113952.png) + +大家发现 依然远比 bellman_ford 的代码版本 耗时高。 + +这又是为什么呢? + +可以发现耗时主要是在 第8组数据上: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418114511.png) + +其实第八组数据是我特别制作的一个 稠密大图,该图有250个节点和10000条边, 在这种情况下, SPFA 的时间复杂度 是接近与 bellman_ford的。 + +但因为 SPFA 节点的进出队列操作,耗时很大,所以相同的时间复杂度的情况下,SPFA 实际上更耗时了。 + +这一点我在 [0094.城市间货物运输I-SPFA](./0094.城市间货物运输I-SPFA.md) 有分析,感兴趣的录友再回头去看看。 + +## 总结 + +本题是单源有限最短路问题,也是 bellman_ford的一个拓展问题,如果理解bellman_ford 其实思路比较容易理解,但有很多细节。 + +例如 为什么要用 minDist_copy 来记录上一轮 松弛的结果。 这也是本篇我为什么花了这么大篇幅讲解的关键所在。 + +接下来,还给大家多了三个拓展: + +* 边的顺序的影响 +* 本题的本质 +* SPFA的解法 + +学透了以上三个拓展,相信大家会对bellman_ford有更深入的理解。 diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" new file mode 100644 index 0000000000..b2afc482fd --- /dev/null +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -0,0 +1,367 @@ + +# Floyd 算法精讲 + +[卡码网:97. 小明逛公园](https://kamacoder.com/problempage.php?pid=1155) + +【题目描述】 + +小明喜欢去公园散步,公园内布置了许多的景点,相互之间通过小路连接,小明希望在观看景点的同时,能够节省体力,走最短的路径。 + + +给定一个公园景点图,图中有 N 个景点(编号为 1 到 N),以及 M 条双向道路连接着这些景点。每条道路上行走的距离都是已知的。 + + +小明有 Q 个观景计划,每个计划都有一个起点 start 和一个终点 end,表示他想从景点 start 前往景点 end。由于小明希望节省体力,他想知道每个观景计划中从起点到终点的最短路径长度。 请你帮助小明计算出每个观景计划的最短路径长度。 + +【输入描述】 + +第一行包含两个整数 N, M, 分别表示景点的数量和道路的数量。 + +接下来的 M 行,每行包含三个整数 u, v, w,表示景点 u 和景点 v 之间有一条长度为 w 的双向道路。 + +接下里的一行包含一个整数 Q,表示观景计划的数量。 + +接下来的 Q 行,每行包含两个整数 start, end,表示一个观景计划的起点和终点。 + +【输出描述】 + +对于每个观景计划,输出一行表示从起点到终点的最短路径长度。如果两个景点之间不存在路径,则输出 -1。 + +【输入示例】 + +7 3 +1 2 4 +2 5 6 +3 6 8 +2 +1 2 +2 3 + +【输出示例】 + +4 +-1 + +【提示信息】 + +从 1 到 2 的路径长度为 4,2 到 3 之间并没有道路。 + +1 <= N, M, Q <= 1000. + +## 思路 + +本题是经典的多源最短路问题。 + +在这之前我们讲解过,dijkstra朴素版、dijkstra堆优化、Bellman算法、Bellman队列优化(SPFA) 都是单源最短路,即只能有一个起点。 + +而本题是多源最短路,即 求多个起点到多个终点的多条最短路径。 + +通过本题,我们来系统讲解一个新的最短路算法-Floyd 算法。 + +Floyd 算法对边的权值正负没有要求,都可以处理。 + +Floyd算法核心思想是动态规划。 + +例如我们再求节点1 到 节点9 的最短距离,用二维数组来表示即:grid[1][9],如果最短距离是10 ,那就是 grid[1][9] = 10。 + +那 节点1 到 节点9 的最短距离 是不是可以由 节点1 到节点5的最短距离 + 节点5到节点9的最短距离组成呢? + +即 grid[1][9] = grid[1][5] + grid[5][9] + +节点1 到节点5的最短距离 是不是可以有 节点1 到 节点3的最短距离 + 节点3 到 节点5 的最短距离组成呢? + +即 grid[1][5] = grid[1][3] + grid[3][5] + +以此类推,节点1 到 节点3的最短距离 可以由更小的区间组成。 + +那么这样我们是不是就找到了,子问题推导求出整体最优方案的递归关系呢。 + +而节点1 到 节点9 的最短距离 可以由 节点1 到节点5的最短距离 + 节点5到节点9的最短距离组成, 也可以有 节点1 到节点7的最短距离 + 节点7 到节点9的最短距离的距离组成。 + +那么选哪个呢? + +是不是 要选一个最小的,毕竟是求最短路。 + +此时我们已经接近明确递归公式了。 + +之前在讲解动态规划的时候,给出过动规五部曲: + +* 确定dp数组(dp table)以及下标的含义 +* 确定递推公式 +* dp数组如何初始化 +* 确定遍历顺序 +* 举例推导dp数组 + +那么接下来我们还是用这五部来给大家讲解 Floyd。 + +1、确定dp数组(dp table)以及下标的含义 + +这里我们用 grid数组来存图,那就把dp数组命名为 grid。 + +grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点的最短距离为m。 + +可能有录友会想: 节点i 到 节点j 的最短距离为m,这句话可以理解,但 以[1...k]集合为中间节点 理解不辽。 + +节点i 到 节点j 的最短路径中 一定是经过很多节点,那么这个集合用[1...k] 来表示。 + +k不能单独指某个节点,因为谁说 节点i 到节点j的最短路径中 一定只有一个节点呢,所以k 一定要表示一个集合,即[1...k] ,表示节点1 到 节点k 一共k个节点的集合。 + + +2、确定递推公式 + +在上面的分析中我们已经初步感受到了递推的关系。 + +我们分两种情况: + +1. 节点i 到 节点j 的最短路径经过节点k +2. 节点i 到 节点j 的最短路径不经过节点k + +对于第一种情况,`grid[i][j][k] = grid[i][k][k - 1] + grid[k][j][k - 1]` + +节点i 到 节点k 的最短距离 是不经过节点k,中间节点集合为[1...k-1],所以 表示为`grid[i][k][k - 1]` + +节点k 到 节点j 的最短距离 也是不经过节点k,中间节点集合为[1...k-1],所以表示为 `grid[k][j][k - 1]` + +第二种情况,`grid[i][j][k] = grid[i][j][k - 1]` + +如果节点i 到 节点j的最短距离 不经过节点k,那么 中间节点集合[1...k-1],表示为 `grid[i][j][k - 1]` + +因为我们是求最短路,对于这两种情况自然是取最小值。 + +即: `grid[i][j][k] = min(grid[i][k][k - 1] + grid[k][j][k - 1], grid[i][j][k - 1])` + + +3、dp数组如何初始化 + +grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点的最短距离为m。 + +刚开始初始化k 是不确定的。 + +例如题目中只是输入边(节点2 -> 节点6,权值为3),那么grid[2][6][k] = 3,k需要填什么呢? + +把k 填成1,那如何上来就知道 节点2 经过节点1 到达节点6的最短距离是3 呢。 + +所以 只能 把k 赋值为 0,本题 节点0 是无意义的,节点是从1 到 n。 + +这样我们在下一轮计算的时候,就可以根据 grid[i][j][0] 来计算 grid[i][j][1],此时的 grid[i][j][1] 就是 节点i 经过节点1 到达 节点j 的最小距离了。 + + + + +**初始化这里要画图,对后面的遍历顺序理解很重要** + +所以初始化: + +```CPP +vector>> grid(n + 1, vector>(n + 1, vector(n + 1, 10005))); // C++定义了一个三位数组,10005是因为边的最大距离是10^4 + +for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + grid[p1][p2][0] = val; + grid[p2][p1][0] = val; // 注意这里是双向图 +} + +``` + +grid数组中其他元素数值应该初始化多少呢? + +本题求的是最小值,所以输入数据没有涉及到的节点的情况都应该初始为一个最大数。 + +这样才不会影响,每次计算去最小值的时候,初始值对计算结果的影响。 + +所以grid数组的定义可以是: + +```CPP +// C++写法,定义了一个三位数组,10005是因为边的最大距离是10^4 +vector>> grid(n + 1, vector>(n + 1, vector(n + 1, 10005))); + +``` + +4、确定遍历顺序 + +从递推公式:`grid[i][j][k] = min(grid[i][k][k - 1] + grid[k][j][k - 1], grid[i][j][k - 1])` 可以看出,我们需要三个for循环,分别遍历i,j 和k + +而 k 依赖于 k - 1, i 和j 的到 并不依赖与 i - 1 或者 j - 1 等等。 + +那么这三个for的嵌套顺序应该是什么样的呢? + +我们来看初始化,我们是把 k =0 的 i 和j 对应的数值都初始化了,这样才能去计算 k = 1 的时候 i 和 j 对应的数值。 + +这就好比是一个三维坐标,i 和j 是平层,而k 是 垂直向上 的。 + +遍历的顺序是从底向上 一层一层去遍历。 + +所以遍历k 的for循环一定是在最外面,这样才能 水平方向一层一层去遍历。如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120109.png) + +至于遍历 i 和 j 的话,for 循环的先后顺序无所谓。 + +代码如下: + +```CPP +for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + grid[i][j][k] = min(grid[i][j][k-1], grid[i][k][k-1] + grid[k][j][k-1]); + } + } +} +``` + +有录友可能想,难道 遍历k 放在最里层就不行吗? + +k 放在最里层,代码是这样: + +```CPP +for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + for (int k = 1; k <= n; k++) { + grid[i][j][k] = min(grid[i][j][k-1], grid[i][k][k-1] + grid[k][j][k-1]); + } + } +} +``` + +此时就遍历了 j 与 k 形成一个平面,i 则是纵面,那遍历 就是这样的: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) + + +而我们初始化,是 k 为0,然后 i 和 j 形成的平面做初始化,如果以 k 和 j 形成的平面去遍历,就造成了 递推公式 用不上上一轮计算的结果,从而导致结果不对(初始化的结果只能用上一部分,因为初始化是 i 与j 形成的平面)。 + +我再给大家举一个测试用例 + +``` +5 4 +1 2 10 +1 3 1 +3 4 1 +4 2 1 +1 +1 2 +``` + +就是图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120942.png) + +就节点1 到 节点 2 的最短距离,运行结果是 10 ,但正确的结果很明显是3。 + +为什么呢? + +因为 k 放在最里面,先就把 节点1 和 节点 2 的最短距离就确定了,后面再也不会计算节点 1 和 节点 2的距离,同时也不会基于 初始化或者之前计算过的结果来计算,即不会考虑 节点1 到 节点3, 节点3 到节点 4,节点4到节点2 的距离。 + + +而遍历k 的for循环如果放在中间呢,同样是 j 与k 行程一个平面,i 是纵面,遍历的也是这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) + + +同样不能完全用上初始化 和 上一层计算的结果。 + +很多录友对于 floyd算法的遍历顺序搞不懂,其实 是没有从三维的角度去思考,同时我把三维立体图给大家画出来,遍历顺序标出来,大家就很容易想明白,为什么 k 放在最外层 才能用上 初始化和上一轮计算的结果了。 + + + + +```CPP +#include +#include +#include +using namespace std; + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector>> grid(n + 1, vector>(n + 1, vector(n + 1, 10005))); // 因为边的最大距离是10^4 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + grid[p1][p2][0] = val; + grid[p2][p1][0] = val; // 注意这里是双向图 + + } + // 开始 floyd + for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + grid[i][j][k] = min(grid[i][j][k-1], grid[i][k][k-1] + grid[k][j][k-1]); + } + } + } + // 输出结果 + int z, start, end; + cin >> z; + while (z--) { + cin >> start >> end; + if (grid[start][end][n] == 10005) cout << -1 << endl; + else cout << grid[start][end][n] << endl; + } +} + +``` + + + +# 拓展 负权回路 + +本题可以有负数,但不能出现负权回路 + +--------- + +floyd n^3 + +同样多源汇最短路算法 Floyd 也是基于动态规划 + +Floyd 算法可以用来解决多源最短路径问题,它会计算图中每两个点之间的最短路径。 + + Floyd 算法对边权的正负没有限制要求(可处理正负权边的图),且能利用 Floyd 算法可能够对图中负环进行判定 + +LeetCode-1334. 阈值距离内邻居最少的城市 + +https://leetcode.cn/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/description/ + +----------- + + +```CPP +#include +#include +#include +using namespace std; + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n, vector(n, 10005)); // 因为边的最大距离是10^4 + + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + grid[p1][p2] = val; + grid[p2][p1] = val; // 注意这里是双向图 + + } + // 开始 floyd + for (int p = 0; p < n; p++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + grid[i][j] = min(grid[i][j], grid[i][p] + grid[p][j]); + } + } + } + // 输出结果 + int z, start, end; + cin >> z; + while (z--) { + cin >> start >> end; + if (grid[start][end] == 10005) cout << -1 << endl; + else cout << grid[start][end] << endl; + } +} + + +``` + + From baf021f2f54727a94236d94b1463f82d722cd7c6 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 24 Apr 2024 16:26:49 +0800 Subject: [PATCH 1101/1533] Update --- ...3\344\274\240\346\265\267\346\212\245.jpg" | Bin 286579 -> 154265 bytes ...11\346\225\260\344\271\213\345\222\214.md" | 2 -- 2 files changed, 2 deletions(-) diff --git "a/pics/\347\275\221\347\253\231\346\230\237\347\220\203\345\256\243\344\274\240\346\265\267\346\212\245.jpg" "b/pics/\347\275\221\347\253\231\346\230\237\347\220\203\345\256\243\344\274\240\346\265\267\346\212\245.jpg" index b0325d8733f1e94f803e9e44e0d1b4e5a0cd570b..547d570418908e1ea34b0cee9034a05fd38071c1 100644 GIT binary patch literal 154265 zcmbTd1yCHp*FCy;AS48L2<{qO0|eK_o!}PS9YTPF;O_3Q5M*(8cXxMpS>V6<>G!>= z_v*bjRQKHJ+jVCyv%7UppWCnVuj_yhG7{1f02mkm00#O8cwGXB0TAI45E0-J5fKoP zkPzRXd_X}#Mn=JUkB0gI7Yh#$7Yhf6fS8JufRKU+2j?T>M+#~hI(j;MQYKa=T2?Ar zI@-TMV33fIP~M9MV9{aV&|zM?0i@99M1c9n z0r=Me0}BUzUZgk3C~u(?z#jmxFmQ0N@NftS@bJ*tK2SdZ9vuOLj79I(iOHE^Z!PKCv(25|UEVGAgQS>Kd9_ z+QufPX66=_R!+_?u5Rugo`FAtfQN>0hi&C4$+EGjOks;&Xo*3~yO zc6N35^!D`+3{Fl>&&+*GIYQsXAgLTB^sSw*?~;)N%f&!||H$nBU1Hz=e`WTc#QvMtA^;T*2Kw^g&;i1LN4E2dw)+r+e0f6uyudi} z+y;Ghku?w_V`-o9Vsm|igl-ES$VU^N@ZkwGJ{=Iu)rOc#9xoQ5dR-tP(m8M!DJJSW zs9THMQ%v^LMS?s@U1GhG0~z~0A;$%KKs@|pW|fc4zOoH!he&8=fbit5@X6LSXuNFC z>0FS3eHkI)psjIz7KN{Tj;Au^64K9{|Mrj9W>T9#45{Rci~7L;{&mzVfSKOJCOfhe zZEx)16+lC$+8jZW-wC_qS2;$y2YGQ3?P4k_=dF^dJvgbb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GKsP!LQtbb>E;e+x>ujXR%@@;v^9*a-e=Jb{)52EqFO%y(0uvr{|ChDw|2Hoc z23E(k|F$~*ugCk3zdQyS_HX)AV1MS%`2TQmA4KQGPT;g5hn%W zz`0NVHwXvY`QDPMZ3$;T&K|ByY;GSB)K3QQf@FM&$Cg<35Aw4wyFDAK*Q5|dLNkcg zW)a-@_6`sado#};QEefq{rogkwaXA%au`+kz35W#2vOm z4UAi8u(KuEAc4vnOPfu$(1Gj`fYd=Krl`L9Ns*b5B%9C3Rx4g)P9X>_m%|vF(?ZFe zVW&`=%No&N&kqm2coym8#rpU!%<_-p|8M&<7tcK(HG3tV%aO?HS{PZQL1nGGkbVJi zlvr?T%#Jk(1C`W*)GaMppP6WZ0KZD1&_7gb5u}Ztc?4rYe;h(-D?iWss%4B5Ah_|C zK~;zl4j>BymA>GSDb3pB5+{-Kv7G9c5Ss&%123|(A}{Z?%3b&t4b(Z3Eq+ zjC`^L&PK8UjjtCYypK`UuL$Ss{~(xbTxo+Xi3A#Q&Um$S8}#21Hrl_m)W2BW|K4BynfPB= 用哈希表解决了[两数之和](https://programmercarl.com/0001.两数之和.html),那么三数之和呢? - # 第15题. 三数之和 [力扣题目链接](https://leetcode.cn/problems/3sum/) From ffbeed09713707790b81a1239ffae7188b885bae Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 24 Apr 2024 16:55:33 +0800 Subject: [PATCH 1102/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 ++-------- ...44\346\225\260\344\271\213\345\222\214.md" | 6 ++-- ...36\346\226\207\345\255\220\344\270\262.md" | 6 ++-- ...11\346\225\260\344\271\213\345\222\214.md" | 6 ++-- ...27\346\257\215\347\273\204\345\220\210.md" | 6 ++-- ...33\346\225\260\344\271\213\345\222\214.md" | 6 ++-- ...4N\344\270\252\350\212\202\347\202\271.md" | 4 +-- ...10\347\232\204\346\213\254\345\217\267.md" | 4 +-- ...55\347\232\204\350\212\202\347\202\271.md" | 4 +-- ...73\351\231\244\345\205\203\347\264\240.md" | 4 +-- .../0028.\345\256\236\347\216\260strStr.md" | 4 +-- ...00\344\270\252\346\216\222\345\210\227.md" | 6 ++-- ...00\344\270\252\344\275\215\347\275\256.md" | 6 ++-- ...22\345\205\245\344\275\215\347\275\256.md" | 8 ++--- ...7.\350\247\243\346\225\260\347\213\254.md" | 8 ++--- ...04\345\220\210\346\200\273\345\222\214.md" | 8 ++--- ...\345\220\210\346\200\273\345\222\214II.md" | 6 ++-- ...2.\346\216\245\351\233\250\346\260\264.md" | 6 ++-- ...\350\267\203\346\270\270\346\210\217II.md" | 6 ++-- ...6.\345\205\250\346\216\222\345\210\227.md" | 6 ++-- ...\345\205\250\346\216\222\345\210\227II.md" | 8 ++--- "problems/0051.N\347\232\207\345\220\216.md" | 6 ++-- .../0052.N\347\232\207\345\220\216II.md" | 6 ++-- ...47\345\255\220\345\272\217\345\222\214.md" | 4 +-- ...01\350\247\204\345\210\222\357\274\211.md" | 6 ++-- ...72\346\227\213\347\237\251\351\230\265.md" | 4 +-- ...63\350\267\203\346\270\270\346\210\217.md" | 4 +-- ...10\345\271\266\345\214\272\351\227\264.md" | 4 +-- ...\346\227\213\347\237\251\351\230\265II.md" | 4 +-- ...15\345\220\214\350\267\257\345\276\204.md" | 8 ++--- ...\345\220\214\350\267\257\345\276\204II.md" | 8 ++--- ...0.\347\210\254\346\245\274\346\242\257.md" | 6 ++-- ...14\345\214\205\347\211\210\346\234\254.md" | 4 +-- ...26\350\276\221\350\267\235\347\246\273.md" | 4 +-- "problems/0077.\347\273\204\345\220\210.md" | 8 ++--- ...04\345\220\210\344\274\230\345\214\226.md" | 6 ++-- "problems/0078.\345\255\220\351\233\206.md" | 6 ++-- ...47\347\232\204\347\237\251\345\275\242.md" | 6 ++-- "problems/0090.\345\255\220\351\233\206II.md" | 6 ++-- ...\345\216\237IP\345\234\260\345\235\200.md" | 6 ++-- ...11\346\220\234\347\264\242\346\240\221.md" | 6 ++-- ...11\346\220\234\347\264\242\346\240\221.md" | 6 ++-- ...70\345\220\214\347\232\204\346\240\221.md" | 6 ++-- ...60\344\272\214\345\217\211\346\240\221.md" | 6 ++-- ...02\345\272\217\351\201\215\345\216\206.md" | 8 ++--- ...00\345\244\247\346\267\261\345\272\246.md" | 6 ++-- ...40\344\272\214\345\217\211\346\240\221.md" | 8 ++--- ...11\346\220\234\347\264\242\346\240\221.md" | 6 ++-- ...41\344\272\214\345\217\211\346\240\221.md" | 8 ++--- ...00\345\260\217\346\267\261\345\272\246.md" | 4 +-- ...57\345\276\204\346\200\273\345\222\214.md" | 8 ++--- ...04\345\255\220\345\272\217\345\210\227.md" | 6 ++-- ...02\347\202\271\346\214\207\351\222\210.md" | 6 ++-- ...00\344\275\263\346\227\266\346\234\272.md" | 4 +-- ...\344\275\263\346\227\266\346\234\272II.md" | 6 ++-- ...01\350\247\204\345\210\222\357\274\211.md" | 6 ++-- ...344\275\263\346\227\266\346\234\272III.md" | 4 +-- ...25\350\257\215\346\216\245\351\276\231.md" | 4 +-- ...60\345\255\227\344\271\213\345\222\214.md" | 6 ++-- ...25\347\232\204\345\214\272\345\237\237.md" | 6 ++-- ...62\345\233\236\346\226\207\344\270\262.md" | 6 ++-- ...\345\233\236\346\226\207\344\270\262II.md" | 6 ++-- ...4.\345\212\240\346\262\271\347\253\231.md" | 6 ++-- ...06\345\217\221\347\263\226\346\236\234.md" | 6 ++-- ...25\350\257\215\346\213\206\345\210\206.md" | 4 +-- ...57\345\275\242\351\223\276\350\241\250.md" | 6 ++-- ...\345\275\242\351\223\276\350\241\250II.md" | 6 ++-- ...15\346\216\222\351\223\276\350\241\250.md" | 4 +-- ...76\345\274\217\346\261\202\345\200\274.md" | 4 +-- ...14\347\232\204\345\215\225\350\257\215.md" | 4 +-- ...70\344\272\244\351\223\276\350\241\250.md" | 4 +-- ...\344\275\263\346\227\266\346\234\272IV.md" | 6 ++-- ...13\350\275\254\346\225\260\347\273\204.md" | 6 ++-- ...23\345\256\266\345\212\253\350\210\215.md" | 6 ++-- ...7.\345\271\277\346\220\234\347\211\210.md" | 8 ++--- ...7.\346\267\261\346\220\234\347\211\210.md" | 4 +-- ...2.\345\277\253\344\271\220\346\225\260.md" | 6 ++-- ...76\350\241\250\345\205\203\347\264\240.md" | 8 ++--- ...04\345\255\227\347\254\246\344\270\262.md" | 6 ++-- ...73\350\275\254\351\223\276\350\241\250.md" | 4 +-- ...7.\350\257\276\347\250\213\350\241\250.md" | 10 +++++- ...04\345\255\220\346\225\260\347\273\204.md" | 4 +-- ...\345\256\266\345\212\253\350\210\215II.md" | 4 +-- ...345\220\210\346\200\273\345\222\214III.md" | 6 ++-- ...02\347\202\271\344\270\252\346\225\260.md" | 6 ++-- ...27\345\256\236\347\216\260\346\240\210.md" | 6 ++-- ...54\344\272\214\345\217\211\346\240\221.md" | 6 ++-- ...36\347\216\260\351\230\237\345\210\227.md" | 4 +-- ...36\346\226\207\351\223\276\350\241\250.md" | 6 ++-- ...54\345\205\261\347\245\226\345\205\210.md" | 6 ++-- ...54\345\205\261\347\245\226\345\205\210.md" | 6 ++-- ...43\346\234\200\345\244\247\345\200\274.md" | 4 +-- ...15\345\274\202\344\275\215\350\257\215.md" | 6 ++-- ...00\346\234\211\350\267\257\345\276\204.md" | 4 +-- ...50\345\271\263\346\226\271\346\225\260.md" | 4 +-- ...3.\347\247\273\345\212\250\351\233\266.md" | 6 ++-- ...07\345\255\220\345\272\217\345\210\227.md" | 4 +-- ...53\345\206\267\345\206\273\346\234\237.md" | 4 +-- ...66\351\222\261\345\205\221\346\215\242.md" | 4 +-- ...11\346\216\222\350\241\214\347\250\213.md" | 6 ++-- ...345\256\266\345\212\253\350\210\215III.md" | 4 +-- ...64\346\225\260\346\213\206\345\210\206.md" | 6 ++-- ...54\345\255\227\347\254\246\344\270\262.md" | 4 +-- ...30\351\242\221\345\205\203\347\264\240.md" | 6 ++-- ...04\347\232\204\344\272\244\351\233\206.md" | 4 +-- ...06\345\212\250\345\272\217\345\210\227.md" | 4 +-- ...10\346\200\273\345\222\214\342\205\243.md" | 4 +-- ...3.\350\265\216\351\207\221\344\277\241.md" | 6 ++-- ...55\345\255\220\345\272\217\345\210\227.md" | 6 ++-- ...66\345\255\220\344\271\213\345\222\214.md" | 6 ++-- ...15\345\273\272\351\230\237\345\210\227.md" | 6 ++-- ...11\345\222\214\345\255\220\351\233\206.md" | 6 ++-- ...64\346\265\201\351\227\256\351\242\230.md" | 4 +-- ...15\345\217\240\345\214\272\351\227\264.md" | 6 ++-- ...55\347\232\204\350\212\202\347\202\271.md" | 6 ++-- ...25\347\210\206\346\260\224\347\220\203.md" | 4 +-- ...\346\225\260\347\233\270\345\212\240II.md" | 6 ++-- ...06\345\217\221\351\245\274\345\271\262.md" | 4 +-- ...20\345\255\227\347\254\246\344\270\262.md" | 4 +-- ...77\347\232\204\345\221\250\351\225\277.md" | 6 ++-- ...4.\344\270\200\345\222\214\351\233\266.md" | 6 ++-- ...36\345\255\220\345\272\217\345\210\227.md" | 5 ++- ...4.\347\233\256\346\240\207\345\222\214.md" | 6 ++-- ...4\345\244\247\345\205\203\347\264\240I.md" | 6 ++-- ...55\347\232\204\344\274\227\346\225\260.md" | 6 ++-- ...\345\244\247\345\205\203\347\264\240II.md" | 6 ++-- ...42\351\202\243\345\245\221\346\225\260.md" | 6 ++-- ...13\350\247\222\347\232\204\345\200\274.md" | 4 +-- ...07\345\255\220\345\272\217\345\210\227.md" | 8 ++--- ...\351\222\261\345\205\221\346\215\242II.md" | 6 ++-- ...17\347\273\235\345\257\271\345\267\256.md" | 6 ++-- ...72\347\264\257\345\212\240\346\240\221.md" | 6 ++-- ...\345\255\227\347\254\246\344\270\262II.md" | 4 +-- ...40\351\231\244\346\223\215\344\275\234.md" | 4 +-- ...66\344\272\214\345\217\211\346\240\221.md" | 6 ++-- ...36\346\226\207\345\255\220\344\270\262.md" | 4 +-- ...a2\345\217\202\350\256\256\351\231\242.md" | 6 ++-- ...47\344\272\214\345\217\211\346\240\221.md" | 6 ++-- ...24\345\233\236\345\216\237\347\202\271.md" | 6 ++-- ...11\346\220\234\347\264\242\346\240\221.md" | 8 ++--- ...27\347\232\204\344\270\252\346\225\260.md" | 6 ++-- ...22\345\242\236\345\272\217\345\210\227.md" | 4 +-- ...27\344\275\231\350\277\236\346\216\245.md" | 6 ++-- ...\344\275\231\350\277\236\346\216\245II.md" | 6 ++-- ...00\345\244\247\351\235\242\347\247\257.md" | 6 ++-- ...55\347\232\204\346\220\234\347\264\242.md" | 6 ++-- ...22\345\205\245\346\223\215\344\275\234.md" | 4 +-- ...14\345\210\206\346\237\245\346\211\276.md" | 6 ++-- ...76\350\256\241\351\223\276\350\241\250.md" | 4 +-- ...53\346\211\213\347\273\255\350\264\271.md" | 6 ++-- ...01\350\247\204\345\210\222\357\274\211.md" | 6 ++-- ...15\345\255\220\346\225\260\347\273\204.md" | 4 +-- ...55\345\277\203\347\264\242\345\274\225.md" | 4 +-- ...36\347\232\204\346\225\260\345\255\227.md" | 4 +-- ...17\346\227\245\346\270\251\345\272\246.md" | 8 ++--- ...71\347\210\254\346\245\274\346\242\257.md" | 6 ++-- ...27\346\257\215\345\214\272\351\227\264.md" | 4 +-- ...34\347\232\204\350\210\252\347\217\255.md" | 10 +++++- ...75\347\232\204\350\267\257\345\276\204.md" | 4 +-- ...47\344\272\272\345\267\245\345\262\233.md" | 4 +-- ...31\345\222\214\346\210\277\351\227\264.md" | 4 +-- ...04\345\255\227\347\254\246\344\270\262.md" | 6 ++-- ...54\346\260\264\346\211\276\351\233\266.md" | 6 ++-- ...\345\272\217\346\225\260\347\273\204II.md" | 6 ++-- ...77\346\214\211\351\224\256\345\205\245.md" | 4 +-- ...61\350\204\211\346\225\260\347\273\204.md" | 6 ++-- ...47\344\272\214\345\217\211\346\240\221.md" | 6 ++-- ...04\347\232\204\345\271\263\346\226\271.md" | 6 ++-- ...70\347\224\250\345\255\227\347\254\246.md" | 6 ++-- ...04\346\225\260\347\273\204\345\222\214.md" | 4 +-- ...60\347\232\204\346\225\260\351\207\217.md" | 6 ++-- ...70\344\272\244\347\232\204\347\272\277.md" | 6 ++-- ...73\351\207\215\345\244\215\351\241\271.md" | 6 ++-- ...\347\232\204\351\207\215\351\207\217II.md" | 6 ++-- ...61\345\255\220\345\272\217\345\210\227.md" | 4 +-- ...72\347\216\260\346\254\241\346\225\260.md" | 6 ++-- ...41\345\255\227\347\254\246\344\270\262.md" | 4 +-- ...77\347\232\204\346\225\260\347\233\256.md" | 8 ++--- ...21\347\232\204\345\237\216\345\270\202.md" | 9 +++++ ...60\347\233\256\346\216\222\345\272\217.md" | 6 ++-- ...27\347\232\204\346\225\260\345\255\227.md" | 4 +-- ...21\345\217\230\345\271\263\350\241\241.md" | 6 ++-- ...55\345\277\203\350\212\202\347\202\271.md" | 4 +-- ...30\345\234\250\350\267\257\345\276\204.md" | 4 +-- ...57\345\244\232\345\244\247\357\274\237.md" | 8 ++--- problems/toolgithub.sh | 34 +++++++++++++------ ...11\346\255\245\351\223\272\345\236\253.md" | 4 +-- ...46\347\235\200\345\233\236\346\272\257.md" | 4 +-- ...21\346\200\273\347\273\223\347\257\207.md" | 4 +-- ...06\350\256\272\345\237\272\347\241\200.md" | 8 ++--- ...00\350\277\255\344\273\243\346\263\225.md" | 6 ++-- ...55\344\273\243\351\201\215\345\216\206.md" | 4 +-- ...22\345\275\222\351\201\215\345\216\206.md" | 6 ++-- ...77\346\215\242\347\251\272\346\240\274.md" | 6 ++-- ...54\345\255\227\347\254\246\344\270\262.md" | 4 +-- ...30\346\200\273\347\273\223\347\257\207.md" | 6 ++-- ...22\346\200\273\347\273\223\347\257\207.md" | 4 +-- ...06\350\256\272\345\237\272\347\241\200.md" | 6 ++-- ...07\351\222\210\346\200\273\347\273\223.md" | 4 +-- ...14\350\241\250\346\200\273\347\273\223.md" | 6 ++-- ...06\350\256\272\345\237\272\347\241\200.md" | 6 ++-- ...36\346\272\257\346\200\273\347\273\223.md" | 8 ++--- ...00\347\247\215\345\206\231\346\263\225.md" | 6 ++-- ...06\350\256\272\345\237\272\347\241\200.md" | 6 ++-- ...06\350\256\272\345\237\272\347\241\200.md" | 9 +++++ ...06\350\256\272\345\237\272\347\241\200.md" | 4 +-- ...06\350\256\272\345\237\272\347\241\200.md" | 4 +-- ...46\344\270\262\346\200\273\347\273\223.md" | 4 +-- ...04\346\200\273\347\273\223\347\257\207.md" | 4 +-- ...06\350\256\272\345\237\272\347\241\200.md" | 4 +-- ...37\345\210\227\346\200\273\347\273\223.md" | 6 ++-- ...06\350\256\272\345\237\272\347\241\200.md" | 6 ++-- ...06\350\256\262\350\247\243\357\274\211.md" | 6 ++-- ...27\346\263\225\346\250\241\346\235\277.md" | 4 +-- ...05\346\200\273\347\273\223\347\257\207.md" | 6 ++-- ...47\241\20001\350\203\214\345\214\205-1.md" | 6 ++-- ...47\241\20001\350\203\214\345\214\205-2.md" | 6 ++-- ...32\351\207\215\350\203\214\345\214\205.md" | 6 ++-- ...14\345\205\250\350\203\214\345\214\205.md" | 6 ++-- ...25\346\200\273\347\273\223\347\257\207.md" | 6 ++-- ...06\350\256\272\345\237\272\347\241\200.md" | 4 +-- ...50\346\200\273\347\273\223\347\257\207.md" | 6 ++-- ...06\350\256\272\345\237\272\347\241\200.md" | 6 ++-- ...76\350\241\250\347\233\270\344\272\244.md" | 4 +-- 224 files changed, 658 insertions(+), 624 deletions(-) diff --git a/README.md b/README.md index a30e0423fe..9e88dfb781 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ @@ -78,18 +78,6 @@ * [Java语言基础课](https://kamacoder.com/course.php?course_id=2) * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) -* 项目 - * [基于跳表的轻量级KV存储引擎](https://github.com/youngyangyang04/Skiplist-CPP) - * [Nosql数据库注入攻击系统](https://github.com/youngyangyang04/NoSQLAttack) - -* 编程素养 - * [看了这么多代码,谈一谈代码风格!](./problems/前序/代码风格.md) - * [力扣上的代码想在本地编译运行?](./problems/前序/力扣上的代码想在本地编译运行?.md) - * [什么是核心代码模式,什么又是ACM模式?](./problems/前序/什么是核心代码模式,什么又是ACM模式?.md) - * [刷题要不要用库函数](./problems/前序/刷力扣用不用库函数.md) - * [ACM模式如何构造二叉树](./problems/前序/ACM模式如何构建二叉树.md) - * [解密互联网大厂研发流程](./problems/前序/互联网大厂研发流程.md) - * 工具 * [一站式vim配置](https://github.com/youngyangyang04/PowerVim) * [保姆级Git入门教程,万字详解](https://mp.weixin.qq.com/s/Q_O0ey4C9tryPZaZeJocbA) @@ -106,7 +94,6 @@ * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) * [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md) * [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md) - * [本周小结!(算法性能分析系列一)](./problems/周总结/20201210复杂度分析周末总结.md) * [关于空间复杂度,可能有几个疑问?](./problems/前序/关于空间复杂度,可能有几个疑问?.md) * [递归算法的时间与空间复杂度分析!](./problems/前序/递归算法的时间与空间复杂度分析.md) * [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md) @@ -499,7 +486,7 @@ # 贡献者 -[点此这里](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)查看LeetCode-Master的所有贡献者。感谢他们补充了LeetCode-Master的其他语言版本,让更多的读者收益于此项目。 +[点此这里](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)查看LeetCode-Master的所有贡献者。感谢他们补充了LeetCode-Master的其他语言版本,让更多的读者受益于此项目。 # Star 趋势 diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 80218cb535..b3db2438c0 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1. 两数之和 @@ -537,8 +537,8 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){ return NULL; } ``` +

- diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index b13f9ac356..a13daf1e2f 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -677,8 +677,8 @@ public class Solution { } ``` +

- diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 90b29cf703..ae21838540 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -936,8 +936,8 @@ object Solution { } } ``` +

- diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index cbd99f8d93..e25d15d5ad 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 17.电话号码的字母组合 @@ -765,8 +765,8 @@ public class Solution } ``` +

- diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 17715b2e6f..89bc2a8bf5 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 一样的道理,能解决四数之和 @@ -697,8 +697,8 @@ def four_sum(nums, target) return result end ``` +

- diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 9e180f753e..c3d1994536 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index 17fbe2be2d..d310f4153c 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index b2a830a746..6de04dc6ca 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 24. 两两交换链表中的节点 diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index cb342586d9..f052d45e94 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 27. 移除元素 diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 8d0cc52559..86dd5e3d21 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 在一个串中查找是否出现过另一个串,这是KMP的看家本领。 diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" index 3cfb673a29..48af8d0da1 100644 --- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" +++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -268,8 +268,8 @@ var nextPermutation = function(nums) { ``` +

- diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index 9afb9941a9..16adcdf19c 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 34. 在排序数组中查找元素的第一个和最后一个位置 @@ -854,8 +854,8 @@ int* searchRange(int* nums, int numsSize, int target, int* returnSize){ } ``` +

- diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 80b7e40e4a..76b5f4a249 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -527,8 +527,8 @@ int searchInsert(int* nums, int numsSize, int target){ } ``` +

- diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index b5f54b1f8f..7611d98235 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ > 如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) @@ -810,8 +810,8 @@ public class Solution } ``` +

- diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 81558cc12c..4c86bc2fa7 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -660,8 +660,8 @@ public class Solution ``` +

- diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 994b04b82f..22cf726d8c 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 这篇可以说是全网把组合问题如何去重,讲的最清晰的了! @@ -806,8 +806,8 @@ public class Solution } } ``` +

- diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 6d92d2b380..0484f830f6 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 6f4d3b16d9..3eeec2689c 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心理准备! @@ -541,8 +541,8 @@ public class Solution } ``` +

- diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index 15e6ae162a..638a2a7c45 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 46.全排列 @@ -518,8 +518,8 @@ public class Solution } ``` +

- diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index 7f2c363889..56006a7744 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -554,8 +554,8 @@ public class Solution } ``` +

- diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 1e1085401d..6ced679cd4 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 51. N皇后 @@ -920,8 +920,8 @@ public class Solution } ``` +

- diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index 29c2b58818..271484a4ee 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -306,8 +306,8 @@ class Solution { } } ``` +

- diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 74ff2ca40d..551c39bfac 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 53. 最大子序和 diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 70ad7a8482..38a3a11818 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 53. 最大子序和 @@ -243,8 +243,8 @@ function maxSubArray(nums: number[]): number { ``` +

- diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 85e6a9364b..4d54ccd6d7 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index 086fd64f5e..01fd951384 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 55. 跳跃游戏 diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index f9d6f65468..134a9028a6 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 56. 合并区间 diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 7f73bc488d..c59ec0333f 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 32c64a12c9..b451704b4c 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 62.不同路径 @@ -594,8 +594,8 @@ public class Solution +

- diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 8c208ea865..e2b17d88a9 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 63. 不同路径 II @@ -759,8 +759,8 @@ public class Solution } ``` +

- diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 67bbdd7b81..1d0b192fe4 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 70. 爬楼梯 @@ -519,8 +519,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 622b1117e7..0810557dca 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 70. 爬楼梯(进阶版) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 777b851cca..b8de8bdcc7 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 72. 编辑距离 diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 103fb627f5..9bbb4455db 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 第77题. 组合 @@ -845,8 +845,8 @@ public class Solution } } ``` +

- diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index 9577d65f3c..0fa568afe9 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -411,8 +411,8 @@ object Solution { } ``` +

- diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 06547e3df5..1415f2d27b 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 78.子集 @@ -466,8 +466,8 @@ public class Solution { ``` +

- diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index b836705ab5..1c4d7f591f 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 84.柱状图中最大的矩形 @@ -741,8 +741,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 6d618978a8..03bbd1dca3 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 90.子集II @@ -659,8 +659,8 @@ public class Solution } ``` +

- diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index c662957a10..73d5e3c3b2 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -848,8 +848,8 @@ public class Solution ``` +

- diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 15b99083e0..e0e773100e 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 96.不同的二叉搜索树 diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 88e1628243..023eeea50a 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 98.验证二叉搜索树 @@ -806,8 +806,8 @@ public bool IsValidBST(TreeNode root) } ``` +

- diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" index 56a6c8840f..7268b9f083 100644 --- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" +++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -339,8 +339,8 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { +

- diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 8442f0ab9e..063b542945 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 101. 对称二叉树 @@ -945,8 +945,8 @@ public bool IsSymmetric(TreeNode root) } ``` +

- diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 4411b5609f..6cdb741454 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 二叉树层序遍历登场! @@ -3226,8 +3226,8 @@ impl Solution { **致敬叶师傅!** +

- diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 1f55f197e5..49bdeb168f 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -1159,8 +1159,8 @@ public int MaxDepth(TreeNode root) ``` +

- diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index 0e0ab1d74f..3518343fc4 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -1242,8 +1242,8 @@ public TreeNode BuildTree(int[] inorder, int[] postorder) } ``` +

- diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 9fa684cfdf..666595e0bb 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 构造二叉搜索树,一不小心就平衡了 @@ -549,8 +549,8 @@ public TreeNode Traversal(int[] nums, int left, int right) ``` +

- diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index 40fdcd143d..dd05bdd6af 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -934,8 +934,8 @@ public int GetHeight(TreeNode root) } ``` +

- diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 6d1632d593..35640ef691 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 和求最大深度一个套路? diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index d45be3bd82..2beb8a7fa3 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 112. 路径总和 @@ -1579,8 +1579,8 @@ public class Solution { // @lc code=end ``` +

- diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 96ab2583f1..8682b88d13 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 115.不同的子序列 @@ -375,8 +375,8 @@ impl Solution { ``` +

- diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" index 60ea9210a2..ca36ac6f3e 100644 --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 116. 填充每个节点的下一个右侧节点指针 @@ -438,8 +438,8 @@ public class Solution ``` +

- diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index 60fbc5cc8e..e9aea0e6c6 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 121. 买卖股票的最佳时机 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index 69706e369a..e255723d2a 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 122.买卖股票的最佳时机 II @@ -422,8 +422,8 @@ public class Solution } ``` +

- diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 24c7f16823..f0dff50506 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 122.买卖股票的最佳时机II @@ -455,8 +455,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 18f19c5192..d06b4f8022 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 123.买卖股票的最佳时机III diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 6f8933101e..00d7d4cfc4 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 127. 单词接龙 diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" index ebb36071cf..90dfd0618f 100644 --- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" +++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -382,8 +382,8 @@ int sumNumbers(struct TreeNode* root){ } ``` +

- diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index 1ddaaa7f83..8ef8d5b280 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 130. 被围绕的区域 @@ -792,8 +792,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index ca342d4bc6..822d4399f1 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 切割问题其实是一种组合问题! @@ -952,8 +952,8 @@ public class Solution ``` +

- diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" index eb91a1899f..85e047f2b7 100644 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -372,8 +372,8 @@ var minCut = function(s) { ``` +

- diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index c093023d5f..c88b43b199 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 134. 加油站 @@ -654,8 +654,8 @@ public class Solution } ``` +

- diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index 210f4995dc..6805857e8c 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 135. 分发糖果 @@ -401,8 +401,8 @@ public class Solution ``` +

- diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index a3d59ec718..f19626d09d 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" index b1f42ba979..ac6565763f 100644 --- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" +++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 141. 环形链表 @@ -159,8 +159,8 @@ function hasCycle(head: ListNode | null): boolean { +

- diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index a643fd7091..d97b160b91 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" index 0b3be9a0f1..8707543190 100644 --- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" +++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 143.重排链表 diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 663a68ea5c..7cadc46592 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 这不仅仅是一道好题,也展现出计算机的思考方式 diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 520f17a7ec..34b099a22d 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" "b/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" index bf62ab30b5..d4422bd8ba 100644 --- "a/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" +++ "b/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

同:[链表:链表相交](https://programmercarl.com/面试题02.07.链表相交.html)

diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 2521749fc9..def6927756 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 188.买卖股票的最佳时机IV @@ -553,8 +553,8 @@ impl Solution { ``` +

- diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" index d60612e92b..b47ee4b91f 100644 --- "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" +++ "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 189. 旋转数组 @@ -201,8 +201,8 @@ function reverseByRange(nums: number[], left: number, right: number): void { +

- diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 480222ef12..032204bbce 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 198.打家劫舍 @@ -360,8 +360,8 @@ impl Solution { ``` +

- diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 85471f73b5..00e4efd894 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 200. 岛屿数量 @@ -408,9 +408,9 @@ impl Solution { } } ``` +``` +

-``` - diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 83d295bd5b..4657920334 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 200. 岛屿数量 diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 719672a281..409a7471d4 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -533,8 +533,8 @@ public class Solution { } } ``` +

- diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index d6d7e6c2ef..18ae160bfd 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -664,8 +664,8 @@ public class Solution } ``` +

- diff --git "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" index e07ab746d9..e416d9ceda 100644 --- "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 205. 同构字符串 @@ -179,8 +179,8 @@ function isIsomorphic(s: string, t: string): boolean { +

- diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index 5a57939af2..430bebe59b 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。 diff --git "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" index 18c4684081..6a8eb23b84 100644 --- "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" +++ "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" @@ -1,3 +1,8 @@ +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

拓扑排序指的是一种 解决问题的大体思路, 而具体算法,可能是 广搜 可能是深搜。 @@ -20,7 +25,6 @@ 这道题的做法同样适用于第 210 题。 ------------------- ``` vector inDegree(numCourses); @@ -49,3 +53,7 @@ while (Qu.size()) { if (count == numCourses) return true; return false; ``` +

+ + + diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 5934d5e364..0a742f958b 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 209.长度最小的子数组 diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index ba996f2bf4..05ebd1ad09 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 213.打家劫舍II diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index ac28f9fcfa..861b6c6634 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index d93d2a3381..745a27e879 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 222.完全二叉树的节点个数 @@ -893,8 +893,8 @@ public int CountNodes(TreeNode root) } ``` +

- diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 6900e66869..c7dc52f1cc 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -1249,8 +1249,8 @@ impl MyStack { } ``` +

- diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 8691953a3e..824968f0f3 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 226.翻转二叉树 @@ -1028,8 +1028,8 @@ public TreeNode InvertTree(TreeNode root) { } ``` +

- diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 41933ca4ba..e8a3d2ec33 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 工作上一定没人这么搞,但是考察对栈、队列理解程度的好题 diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" index fef942fc49..1356b7da3b 100644 --- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" +++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 234.回文链表 @@ -428,8 +428,8 @@ function reverseList(head: ListNode | null): ListNode | null { +

- diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 2a11f9f4b7..597c2dffd3 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 235. 二叉搜索树的最近公共祖先 @@ -547,8 +547,8 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) ``` +

- diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 049f70c7ae..1ad9850be1 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 本来是打算将二叉树和二叉搜索树的公共祖先问题一起讲,后来发现篇幅过长了,只能先说一说二叉树的公共祖先问题。 @@ -445,8 +445,8 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) } ``` +

- diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 19ac12619e..23bf615b4a 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 6eed90a73a..ac03ddbb40 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 数组就是简单的哈希表,但是数组的大小可不是无限开辟的 @@ -390,8 +390,8 @@ object Solution { * [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/) +

- diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 4c6c92c5b4..f64e52d9f4 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 以为只用了递归,其实还用了回溯 diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index b71b69f473..0b732b349b 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 279.完全平方数 diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index fc708844bd..4d8fd9a155 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 283. 移动零:动态规划:一样的套路,再求一次完全平方数 @@ -172,8 +172,8 @@ void moveZeroes(int* nums, int numsSize){ +

- diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 6d82eae1e9..19c129dfca 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 300.最长递增子序列 diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 9dc35bdf52..5a38111ad5 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 309.最佳买卖股票时机含冷冻期 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 156b5ff36d..0ed5cf688e 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" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 322. 零钱兑换 diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 6c8a481435..144672a9aa 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 这也可以用回溯法? 其实深搜和回溯也是相辅相成的,毕竟都用递归。 @@ -942,8 +942,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 61b9f99c6f..7aae5cbf2a 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 337.打家劫舍 III diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index aaa758e63e..205b220144 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 343. 整数拆分 @@ -516,8 +516,8 @@ public class Solution } ``` +

- diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 44184c53bc..793c9af362 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index b340e1855b..34d9f82c1b 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 前K个大数问题,老生常谈,不得不谈 @@ -603,8 +603,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 9902fff880..e17e940f5f 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 5c2241c805..9e6714ce9d 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 376. 摆动序列 diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index 6f81bffef9..e96f8dc646 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 377. 组合总和 Ⅳ diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index ff5aafed62..768771c333 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -465,8 +465,8 @@ bool canConstruct(char* ransomNote, char* magazine) { } ``` +

- diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index caca8cb86c..92246e4f9b 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 392.判断子序列 @@ -404,8 +404,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 3d0f5a8abf..1ba71dc94f 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 404.左叶子之和 @@ -669,8 +669,8 @@ public int SumOfLeftLeaves(TreeNode root) } ``` +

- diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index b0b02c1454..b7e94543e6 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 406.根据身高重建队列 @@ -421,8 +421,8 @@ public class Solution ``` +

- diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 71e01ae392..4d2e6bf656 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 416. 分割等和子集 @@ -755,8 +755,8 @@ public class Solution } } ``` +

- diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 8fe0f1b426..b8448e936d 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index b668e86046..209616024e 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 435. 无重叠区间 @@ -494,8 +494,8 @@ public class Solution ``` +

- diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 60dae7b977..f6057f44a5 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 二叉搜索树删除节点就涉及到结构调整了 @@ -801,8 +801,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index cd57f83b26..318c3035ce 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 452. 用最少数量的箭引爆气球 diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index a0bf84da83..db9a9e430c 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 需要哈希的地方都能找到map的身影 @@ -481,8 +481,8 @@ int fourSumCount(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums return count; } ``` +

- diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 6ae206dba2..91ed40b422 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 455.分发饼干 diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 311e3a695e..1028bf1ea2 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> KMP算法还能干这个 diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 2e954e30ad..5261d6c2ae 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -432,8 +432,8 @@ function islandPerimeter(grid: number[][]): number { } ``` +

- diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index af50fa5cbc..47b34a0f31 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 474.一和零 @@ -595,8 +595,8 @@ public class Solution } ``` +

- diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 1aa69a3669..8f642a5fc9 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 和子集问题有点像,但又处处是陷阱 @@ -206,7 +206,6 @@ public: ### Java ```Java -//using set, aligned with the unimproved method class Solution { List> result = new ArrayList<>(); List path = new ArrayList<>(); diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 02edad4d8b..cacf6dfa18 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -650,8 +650,8 @@ public class Solution } ``` +

- diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index d97a3e8482..54182d3018 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 496.下一个更大元素 I @@ -450,8 +450,8 @@ impl Solution { +

- diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 20627d1ad6..93b3fb5400 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 二叉树上应该怎么求,二叉搜索树上又应该怎么求? @@ -1051,8 +1051,8 @@ public class Solution ``` +

- diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 6df83fb208..62066d8521 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 503.下一个更大元素II @@ -295,8 +295,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 71c022bd34..21b07802f1 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 509. 斐波那契数 @@ -475,8 +475,8 @@ public class Solution +

- diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index d897bba13a..d69ceb6f51 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 513.找树左下角的值 diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index 44bdec1f19..af36b94d29 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 516.最长回文子序列 @@ -298,8 +298,8 @@ impl Solution { ``` +

- diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 59fdf6cd25..255912d62b 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -395,8 +395,8 @@ public class Solution } ``` +

- diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 82b3f5d4e9..7fe64ad21d 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 利用二叉搜索树的特性搞起! @@ -669,8 +669,8 @@ public class Solution } ``` +

- diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index 7fcb5efd32..b95b585485 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 538.把二叉搜索树转换为累加树 @@ -548,8 +548,8 @@ public class Solution ``` +

- diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index 80e662f9dd..3e304fabc0 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 7dbb8ef542..7bb7ceffc8 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 583. 两个字符串的删除操作 diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 3478a2af5d..530350ac69 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 617.合并二叉树 @@ -803,8 +803,8 @@ public TreeNode MergeTrees(TreeNode root1, TreeNode root2) } ``` +

- diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 4887ff8311..2011bee3e5 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 647. 回文子串 diff --git "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" index db6b43df8e..1540a60173 100644 --- "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" +++ "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -283,8 +283,8 @@ function predictPartyVictory(senate: string): string { +

- diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index f54558a68c..fed9b2b991 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 654.最大二叉树 @@ -598,8 +598,8 @@ public TreeNode ConstructMaximumBinaryTree(int[] nums) } ``` +

- diff --git "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" index ef58739122..eccfef3a21 100644 --- "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" +++ "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 657. 机器人能否返回原点 @@ -181,8 +181,8 @@ var judgeCircle = function (moves) { ``` +

- diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 6824c7e29a..aef8465943 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -586,8 +586,8 @@ public TreeNode TrimBST(TreeNode root, int low, int high) ``` +

- diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" index 0277f24989..0366ee8063 100644 --- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" +++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 673.最长递增子序列的个数 @@ -360,8 +360,8 @@ var findNumberOfLIS = function(nums) { ``` +

- diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 0ca8a4c913..cebb552bf8 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 674. 最长连续递增序列 diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index f5e84223b1..6d5f2bc4fe 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 684.冗余连接 @@ -378,8 +378,8 @@ var findRedundantConnection = function(edges) { +

- diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index c07dda3a78..3f489d82b7 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 685.冗余连接II @@ -619,8 +619,8 @@ var findRedundantDirectedConnection = function(edges) { +

- diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 87b1b5bbbb..11b638d4d6 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 695. 岛屿的最大面积 @@ -708,8 +708,8 @@ impl Solution { } } ``` +

- diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 9efb1e0519..5c1b9c43fd 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 700.二叉搜索树中的搜索 @@ -487,8 +487,8 @@ public TreeNode SearchBST(TreeNode root, int val) } ``` +

- diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 5cb0de9911..6b9e58347d 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 701.二叉搜索树中的插入操作 diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 31e89ae344..43ede76948 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 704. 二分查找 @@ -828,8 +828,8 @@ class Solution { } ``` +

- diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index fecdbc3cad..47771d2889 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 听说这道题目把链表常见的五个操作都覆盖了? diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" index db39864908..88b03d9d76 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 714. 买卖股票的最佳时机含手续费 @@ -360,8 +360,8 @@ object Solution { } ``` +

- diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 88ba9271c8..73714147e0 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 714.买卖股票的最佳时机含手续费 @@ -322,8 +322,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index e00b3dedf3..6c8e7101a1 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 718. 最长重复子数组 diff --git "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" index 9ed8535e0d..a66a445083 100644 --- "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" +++ "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 724.寻找数组的中心下标 diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index 4a8a6e087d..3d46d5ad04 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 738.单调递增的数字 diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index fdb11c63e3..45af52868f 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ @@ -482,8 +482,8 @@ impl Solution { ``` +

- diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 6320ed89a7..753a104d97 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -537,8 +537,8 @@ public class Solution +

- diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 8b0ca7b802..5218692735 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 763.划分字母区间 diff --git "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" index 1ece7116f2..9c0a8e7f27 100644 --- "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" +++ "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" @@ -1,3 +1,8 @@ +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 787. K 站中转内最便宜的航班 @@ -119,7 +124,6 @@ public: }; ------------------ 队列加上 visited 不能重复访问 @@ -176,3 +180,7 @@ public: +

+ + + diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index 4b46d65909..40e1bbe73e 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 797.所有可能的路径 diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 7930c7a18a..e452e4e395 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 827.最大人工岛 diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index b4785d1b02..b78693b446 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" index c7f5220288..c32cdd339b 100644 --- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 844.比较含退格的字符串 @@ -587,8 +587,8 @@ impl Solution { } ``` +

- diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index db70112d8f..804ff13c12 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 860.柠檬水找零 @@ -439,8 +439,8 @@ public class Solution ``` +

- diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" index 72be8fa732..1ac6800c08 100644 --- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" +++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -378,8 +378,8 @@ function sortArrayByParityII(nums: number[]): number[] { }; ``` +

- diff --git "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" index 11edecd0d1..f4a8fa8e52 100644 --- "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" +++ "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 925.长按键入 diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" index 48c29eb479..9f63f441c2 100644 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 941.有效的山脉数组 @@ -197,8 +197,8 @@ public class Solution { ``` +

- diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index 9743ca2b80..d59496c87d 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 5bdbcbc7e0..f89ad44926 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 双指针风骚起来,也是无敌 @@ -507,8 +507,8 @@ public class Solution { } } ``` +

- diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index 8d81e3f8a0..f938c2b734 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -581,8 +581,8 @@ def common_chars(words) end ``` +

- diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 498015d0cd..fa27d3b7c3 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1005.K次取反后最大化的数组和 diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 3488777ad2..59610c6801 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1020. 飞地的数量 @@ -695,8 +695,8 @@ impl Solution { * 1254. 统计封闭岛屿的数目 +

- diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index e0625a2ba3..8ee52c5d48 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1035.不相交的线 @@ -273,8 +273,8 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { ``` +

- diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index ffe1353039..4aa0e95478 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 匹配问题都是栈的强项 @@ -495,8 +495,8 @@ def remove_duplicates(s) end ``` +

- diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 4c3c01a037..4f2cc9e350 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1049.最后一块石头的重量II @@ -497,8 +497,8 @@ public class Solution } ``` +

- diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 12bd90f8f4..7fa7bb68e1 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1143.最长公共子序列 diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index 2ccd30c376..5c5f92c339 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1207.独一无二的出现次数 @@ -224,8 +224,8 @@ func uniqueOccurrences(arr []int) bool { +

- diff --git "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" index 2a7b092290..a32ca98ffc 100644 --- "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1221. 分割平衡字符串 diff --git "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" index dc8fda41f1..3d7b9fe96c 100644 --- "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" +++ "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1254. 统计封闭岛屿的数目 @@ -134,9 +134,9 @@ var closedIsland = function(grid) { ``` + +

- - diff --git "a/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" "b/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" index 2156ee041d..d8d8861f47 100644 --- "a/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" +++ "b/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" @@ -1,3 +1,8 @@ +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

floyd @@ -44,3 +49,7 @@ public: return result; } }; +

+ + + diff --git "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" index c2455cf043..9cfb674328 100644 --- "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" +++ "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

@@ -216,8 +216,8 @@ var sortByBits = function(arr) { ``` +

- diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index c706ba216e..94c1eb77c9 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" index 57e56b8fe3..120cafffd3 100644 --- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" +++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1382.将二叉搜索树变平衡 @@ -217,8 +217,8 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null +

- diff --git "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" index 9bcc7ef9ca..e3db794706 100644 --- "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" +++ "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1791.找出星型图的中心节点 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 5463ec7288..24e81992f0 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 1971. 寻找图中是否存在路径 diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 8be48f38c2..a5dab942c8 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 程序提交之后为什么会超时?O(n)的算法会超时,n究竟是多大? @@ -223,8 +223,8 @@ int main() { +

- diff --git a/problems/toolgithub.sh b/problems/toolgithub.sh index ebe9c7df7d..9e005f0c0e 100644 --- a/problems/toolgithub.sh +++ b/problems/toolgithub.sh @@ -7,21 +7,35 @@ ######################################################################### #!/bin/bash +#

+# +# +# +#

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ +# +#

+# +# +# + for i in *.md do if [[ $i != 'README.md' ]] then # 移除开头 - # sed -i '' '/align/d;/\"\"><\/a>/d;/<\/p>/d;/<\/a>/d;/20210924105952.png/d;/_blank/d' $i - # # 移除结尾 - # sed -i '' '/--------------/d;/训练营/d;' $i - # # 添加开头 - # # 记得从后向前添加 - # ex -sc '1i|

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

' -cx $i - # ex -sc '1i|' -cx $i - # ex -sc '1i| ' -cx $i - # ex -sc '1i|' -cx $i - # ex -sc '1i|

' -cx $i + sed -i '' '/align/d;/\"\"><\/a>/d;/<\/p>/d;/<\/a>/d;/20210924105952.png/d;/_blank/d' $i + # 移除结尾 + sed -i '' '/训练营/d;/网站星球宣传海报/d' $i + + + # 添加开头 + # 记得从后向前添加 + ex -sc '1i|

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

' -cx $i + ex -sc '1i|' -cx $i + ex -sc '1i| ' -cx $i + ex -sc '1i|' -cx $i + ex -sc '1i|

' -cx $i # echo '## 其他语言版本' >> $i # echo '\n' >> $i # echo 'Java:' >> $i diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" index 442a505573..c2f5efd96d 100644 --- "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" +++ "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 动态规划之编辑距离总结篇 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" index 67570bc8d6..42d78ae39f 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 二叉树:以为使用了递归,其实还隐藏着回溯 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" index 739184bbf7..8db40d6560 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 二叉树:总结篇!(需要掌握的二叉树技能都在这里了) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 50d592a24c..c665827791 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 二叉树理论基础篇 @@ -313,8 +313,8 @@ public class TreeNode public TreeNode(int x) { val = x; } } ``` +

- diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index ee4899b1b1..13c507375b 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 统一写法是一种什么感觉 @@ -835,8 +835,8 @@ public IList PostorderTraversal(TreeNode root) } ``` +

- diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 35a01a7fbe..8549bac154 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 听说还可以用非递归的方式 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 4621d4a739..11a3d716e4 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 一看就会,一写就废! @@ -631,8 +631,8 @@ public void Traversal(TreeNode cur, IList res) } ``` +

- diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index 040be29983..f5803cb452 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 替换数字 @@ -170,8 +170,8 @@ for (int i = 0; i < a.size(); i++) { +

- diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 138cf3a88d..e32f4ce15d 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 右旋字符串 diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index 4df21fb761..7927deb7d7 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# Leetcode股票问题总结篇! @@ -473,8 +473,8 @@ public: +

- diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index e28bfd0472..3dda376855 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 动态规划最强总结篇! diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" index bff26d1d34..c9420d24eb 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 动态规划理论基础 @@ -131,8 +131,8 @@ 今天我们开始新的征程了,你准备好了么? +

- diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" index 6621e0396b..02a8f98629 100644 --- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" +++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 又是一波总结 diff --git "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" index 465ef9d1da..cde23ad138 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 哈希表总结篇如约而至 @@ -125,8 +125,8 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 +

- diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index e426c65791..de18447017 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index 5d4c945075..8d9b78c4b9 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ > 20张树形结构图、14道精选回溯题目,21篇回溯法精讲文章,由浅入深,一气呵成,这是全网最强回溯算法总结! @@ -447,8 +447,8 @@ N皇后问题分析: **回溯算法系列正式结束,新的系列终将开始,录友们准备开启新的征程!** +

- diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 2be79805af..96dfeffec3 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 回溯算法去重问题的另一种写法 @@ -708,8 +708,8 @@ impl Solution { } ``` +

- diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index f22c67b137..862fb101a1 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 回溯算法理论基础 @@ -175,8 +175,8 @@ void backtracking(参数) { +

- diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 6a1456e950..6a9cc1aca2 100644 --- "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,3 +1,8 @@ +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 并查集理论基础 @@ -456,3 +461,7 @@ void join(int u, int v) { 敬请期待 并查集题目精讲系列。 +

+ + + diff --git "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index b631f4f5c5..51a82944ea 100644 --- "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 广度优先搜索理论基础 diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 7f847f8fae..9e8d90a3db 100644 --- "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 深度优先搜索理论基础 diff --git "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" index df4db78729..7da9791400 100644 --- "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" +++ "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 字符串:总结篇 diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index 7550ce0254..2a2681abed 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 数组总结篇 diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index d104c883f7..05451b3cd3 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index 06a7827050..090beff6b5 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 栈与队列总结篇 @@ -159,8 +159,8 @@ cd a/b/c/../../ 好了,栈与队列我们就总结到这里了,接下来Carl就要带大家开启新的篇章了,大家加油! +

- diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index ad748e489e..21c61a4c8b 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

> 来看看栈和队列不为人知的一面 @@ -92,8 +92,8 @@ std::queue> third; // 定义以list为底层容器的队列 +

- diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 5f9b46b373..70a9a97a19 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ diff --git "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" index 21b7f93b1c..0d32cebb2d 100644 --- "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" +++ "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 算法模板 ## 算法模板 diff --git "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" index 9be93096fd..651a92a804 100644 --- "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 听说背包问题很难? 这篇总结篇来拯救你了 @@ -102,8 +102,8 @@ +

- diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 413d984c08..fa11fb94bc 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 动态规划:01背包理论基础 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 5630bc99d1..b5838c5df8 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 动态规划:01背包理论基础(滚动数组) @@ -503,8 +503,8 @@ fn test_wei_bag_problem2() { } ``` +

- diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index 5d6440e3bf..da1ee02ff5 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 动态规划:关于多重背包,你该了解这些! @@ -213,8 +213,8 @@ class multi_pack{ +

- diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index efc56c50ac..3a50ee7bbf 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 动态规划:完全背包理论基础 @@ -505,8 +505,8 @@ fn test_complete_pack() { } ``` +

- diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" index d49cdc5fb1..14d82151c9 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 贪心算法总结篇 @@ -150,8 +150,8 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 +

- diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index cac2929251..f042c0acf3 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 关于贪心算法,你该了解这些! diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index b2b1b7795f..7da0d2de3c 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 链表总结篇 @@ -97,8 +97,8 @@ +

- diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 88e41d7d3b..d131380728 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,9 +1,9 @@ -

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index e2905d49be..b0f2e8eaea 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -1,8 +1,8 @@

- + -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 面试题 02.07. 链表相交 From 07c0dedbb269ed680a5b2d4e870d26bff1ffce47 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 24 Apr 2024 17:23:45 +0800 Subject: [PATCH 1103/1533] Update --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e88dfb781..5389c89a9b 100644 --- a/README.md +++ b/README.md @@ -177,8 +177,9 @@ ## 二叉树 + 题目分类大纲如下: -二叉树大纲 +二叉树大纲 1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md) 2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md) @@ -220,7 +221,7 @@ 题目分类大纲如下: -回溯算法大纲 +回溯算法大纲 1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md) 2. [回溯算法:77.组合](./problems/0077.组合.md) From 00a4cdc161413dccf044b3b573a3d87cacf276d0 Mon Sep 17 00:00:00 2001 From: zhengprince Date: Thu, 25 Apr 2024 15:21:13 +0800 Subject: [PATCH 1104/1533] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错字:中->种 --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index cacf6dfa18..a3d79d2bb5 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -173,9 +173,9 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 * 已经有一个1(nums[i]) 的话,有 dp[4]种方法 凑成 容量为5的背包。 * 已经有一个2(nums[i]) 的话,有 dp[3]种方法 凑成 容量为5的背包。 -* 已经有一个3(nums[i]) 的话,有 dp[2]中方法 凑成 容量为5的背包 -* 已经有一个4(nums[i]) 的话,有 dp[1]中方法 凑成 容量为5的背包 -* 已经有一个5 (nums[i])的话,有 dp[0]中方法 凑成 容量为5的背包 +* 已经有一个3(nums[i]) 的话,有 dp[2]种方法 凑成 容量为5的背包 +* 已经有一个4(nums[i]) 的话,有 dp[1]种方法 凑成 容量为5的背包 +* 已经有一个5 (nums[i])的话,有 dp[0]种方法 凑成 容量为5的背包 那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。 From f755ecc5fcce894cdbcc9c7eadfaf7bb123a3b1b Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Thu, 25 Apr 2024 17:34:06 +0800 Subject: [PATCH 1105/1533] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D?= =?UTF-8?q?=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\275\222\351\201\215\345\216\206.md" | 111 ++++++++++++------ 1 file changed, 75 insertions(+), 36 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 4621d4a739..ac42f69fed 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -48,7 +48,7 @@ void traversal(TreeNode* cur, vector& vec) if (cur == NULL) return; ``` -3. **确定单层递归的逻辑**:前序遍历是中左右的循序,所以在单层递归的逻辑,是要先取中节点的数值,代码如下: +3. **确定单层递归的逻辑**:前序遍历是中左右的顺序,所以在单层递归的逻辑,是要先取中节点的数值,代码如下: ```cpp vec.push_back(cur->val); // 中 @@ -287,52 +287,91 @@ func postorderTraversal(root *TreeNode) (res []int) { 前序遍历: ```Javascript var preorderTraversal = function(root) { - let res=[]; - const dfs=function(root){ - if(root===null)return ; - //先序遍历所以从父节点开始 - res.push(root.val); - //递归左子树 - dfs(root.left); - //递归右子树 - dfs(root.right); - } - //只使用一个参数 使用闭包进行存储结果 - dfs(root); - return res; +// 第一种 +// let res=[]; +// const dfs=function(root){ +// if(root===null)return ; +// //先序遍历所以从父节点开始 +// res.push(root.val); +// //递归左子树 +// dfs(root.left); +// //递归右子树 +// dfs(root.right); +// } +// //只使用一个参数 使用闭包进行存储结果 +// dfs(root); +// return res; +// 第二种 + return root + ? [ + // 前序遍历:中左右 + root.val, + // 递归左子树 + ...preorderTraversal(root.left), + // 递归右子树 + ...preorderTraversal(root.right), + ] + : []; }; ``` 中序遍历 ```javascript var inorderTraversal = function(root) { - let res=[]; - const dfs=function(root){ - if(root===null){ - return ; - } - dfs(root.left); - res.push(root.val); - dfs(root.right); - } - dfs(root); - return res; +// 第一种 + + // let res=[]; + // const dfs=function(root){ + // if(root===null){ + // return ; + // } + // dfs(root.left); + // res.push(root.val); + // dfs(root.right); + // } + // dfs(root); + // return res; + +// 第二种 + return root + ? [ + // 中序遍历:左中右 + // 递归左子树 + ...inorderTraversal(root.left), + root.val, + // 递归右子树 + ...inorderTraversal(root.right), + ] + : []; }; ``` 后序遍历 ```javascript var postorderTraversal = function(root) { - let res=[]; - const dfs=function(root){ - if(root===null){ - return ; - } - dfs(root.left); - dfs(root.right); - res.push(root.val); - } - dfs(root); - return res; + // 第一种 + // let res=[]; + // const dfs=function(root){ + // if(root===null){ + // return ; + // } + // dfs(root.left); + // dfs(root.right); + // res.push(root.val); + // } + // dfs(root); + // return res; + + // 第二种 + // 后续遍历:左右中 + return root + ? [ + // 递归左子树 + ...postorderTraversal(root.left), + // 递归右子树 + ...postorderTraversal(root.right), + root.val, + ] + : []; }; ``` From 5955c1de5dc8811b30c29c49121cb8ad42ff32c4 Mon Sep 17 00:00:00 2001 From: yawebNW <38253154+yawebNW@users.noreply.github.com> Date: Fri, 26 Apr 2024 11:48:33 +0800 Subject: [PATCH 1106/1533] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md=20=E6=9B=B4=E6=96=B0jav?= =?UTF-8?q?a=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit java最后一个方法新增去重逻辑,原代码遇到循环时会无限递归 --- ...\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 144672a9aa..bc453c51ee 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -379,6 +379,8 @@ class Solution { String targetLocation; //遍历从当前位置出发的机票 for (int i = 0; i < targetLocations.size(); i++) { + //去重,否则在最后一个测试用例中遇到循环时会无限递归 + if(i > 0 && targetLocations.get(i).equals(targetLocations.get(i - 1))) continue; targetLocation = targetLocations.get(i); //删除终点列表中当前的终点 targetLocations.remove(i); From c3ca80645400a5a87767020d66af6c981db5336d Mon Sep 17 00:00:00 2001 From: francisshzhu Date: Fri, 26 Apr 2024 12:50:59 +0800 Subject: [PATCH 1107/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90332.=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md=20=E4=B8=ADpyt?= =?UTF-8?q?hon=E8=A7=A3=E6=B3=95=E7=9A=84markdown=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" | 1 - 1 file changed, 1 deletion(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 144672a9aa..bbe3a001e7 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -419,7 +419,6 @@ class Solution { ``` ### Python -``` 回溯 使用字典 ```python class Solution: From 1c390b3562b03be7a24933455b76a9187996ff72 Mon Sep 17 00:00:00 2001 From: xqsrpanz Date: Fri, 26 Apr 2024 18:01:03 +0800 Subject: [PATCH 1108/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=20JavaScr?= =?UTF-8?q?ipt=20=E8=A7=A3=E6=B3=95=E4=BA=8C=EF=BC=8C=E5=A0=86=20pop=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=97=A0=E6=B3=95=E5=A4=84=E7=90=86=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E9=95=BF=E5=BA=A6=20<=3D=201=20=E7=9A=84=E8=BE=B9?= =?UTF-8?q?=E7=95=8C=E6=83=85=E5=86=B5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\252\351\253\230\351\242\221\345\205\203\347\264\240.md" | 6 ++++++ 1 file changed, 6 insertions(+) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 34d9f82c1b..8a219c6a41 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -405,6 +405,11 @@ class Heap { // 获取堆顶元素并移除 pop() { + // 边界情况,只有一个元素或没有元素应直接弹出 + if (this.size() <= 1) { + return this.queue.pop() + } + // 堆顶元素 const out = this.queue[0]; @@ -608,3 +613,4 @@ impl Solution { + From bcdf3e4ecdf352ee5f6a9d539bd980c66c3ea93e Mon Sep 17 00:00:00 2001 From: xqsrpanz <129468278+xqsrpanz@users.noreply.github.com> Date: Fri, 26 Apr 2024 19:45:18 +0800 Subject: [PATCH 1109/1533] =?UTF-8?q?Update=200347.=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复了 JavaScript 解法二,堆 pop 方法无法处理数组长度 <= 1 的边界情况的问题 2. 删除了有误导性的条件判断。事实上,被删除的表达式永远为真(searchChild !== undefined)。原作者想表达的意思可能是 this.queue[searchChild] !== undefined,而实际上,这个判断也是不必要的,这种情况会被后续的 this.compare(index, searchChild) > 0 判断排除。但鉴于本项目的教程性质,直接去除可能会导致语义不清,考虑酌情将原处替换为 this.queue[searchChild] !== undefined 或直接删除 --- ...\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 8a219c6a41..cca9b0edce 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -421,7 +421,7 @@ class Heap { let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标 let searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left; - while (searchChild !== undefined && this.compare(index, searchChild) > 0) { // 注意compare参数顺序 + while (this.compare(index, searchChild) > 0) { // 注意compare参数顺序 [this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]]; // 更新下标 From 9356debd174b412729496b9ee254597967d551b2 Mon Sep 17 00:00:00 2001 From: Haoting <1165101405@qq.com> Date: Sat, 27 Apr 2024 17:08:54 +0800 Subject: [PATCH 1110/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00104=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20C=E8=AF=AD=E8=A8=80=E8=BF=AD=E4=BB=A3=E6=B3=95=E2=80=94?= =?UTF-8?q?=E2=80=94=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 0f93cb0fc1..607e195b42 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -829,7 +829,42 @@ int maxDepth(struct TreeNode* root){ return depth; } ``` - +二叉树最大深度迭代——后序遍历实现 +```c +int maxDepth(struct TreeNode *root) +{ + if(root == NULL) + return 0; + struct TreeNode *stack[10000] = {}; + int top = -1; + struct TreeNode *p = root, *r = NULL; // r指向上一个被访问的结点 + int depth = 0, maxDepth = -1; + while(p != NULL || top >= 0) + { + if(p != NULL) + { + stack[++top] = p; + depth++; + p = p->left; + } + else + { + p = stack[top]; + if(p->right != NULL && p->right != r) // 右子树未被访问 + p = p->right; + else + { + if(depth >= maxDepth) maxDepth = depth; + p = stack[top--]; + depth--; + r = p; + p = NULL; + } + } + } + return maxDepth; +} +``` ### Swift: 104.二叉树的最大深度 From 8dcfd4f319c219fca9db3268f3cecff3a3156b59 Mon Sep 17 00:00:00 2001 From: Haoting <1165101405@qq.com> Date: Sat, 27 Apr 2024 17:10:20 +0800 Subject: [PATCH 1111/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00055=E5=8F=B3?= =?UTF-8?q?=E6=97=8B=E5=AD=97=E7=AC=A6=E4=B8=B2=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\255\227\347\254\246\344\270\262.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 7137186042..22be955f47 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -253,6 +253,47 @@ func main(){ ``` +### C: +```C +#include +#include + +void reverse(char *s, int left, int right) +{ + while(left <= right) + { + char c = s[left]; + s[left] = s[right]; + s[right] = c; + left++; + right--; + } +} + +void rightRotate(char *s, int k) +{ + int len = strlen(s); + // 先局部反转再整体反转 + reverse(s, 0, len - k - 1); // 反转前部分 + reverse(s, len - k, len - 1); // 反转后部分:后k位 + reverse(s, 0, len - 1); // 整体反转 +} + +int main() +{ + + int k; + scanf("%d", &k); + char s[10000]; + scanf("%s", s); + + rightRotate(s, k); + printf("%s\n", s); + + return 0; +} +``` + ### JavaScript: From b06601c58c08a8647c64af82034e41f1b06d8618 Mon Sep 17 00:00:00 2001 From: markwang Date: Sun, 28 Apr 2024 15:18:01 +0800 Subject: [PATCH 1112/1533] =?UTF-8?q?54.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=E5=A2=9E=E5=8A=A0Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\346\227\213\347\237\251\351\230\265.md" | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 4d54ccd6d7..175ae14745 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -348,6 +348,82 @@ class Solution(object): return print_list ``` +### Go: + +```go +func spiralOrder(matrix [][]int) []int { + rows := len(matrix) + if rows == 0 { + return []int{} + } + columns := len(matrix[0]) + if columns == 0 { + return []int{} + } + res := make([]int, rows * columns) + startx, starty := 0, 0 // 定义每循环一个圈的起始位置 + loop := min(rows, columns) / 2 + mid := min(rows, columns) / 2 + count := 0 // 用来给矩阵中每一个空格赋值 + offset := 1 // 每一圈循环,需要控制每一条边遍历的长度 + for loop > 0 { + i, j := startx, starty + + // 模拟填充上行从左到右(左闭右开) + for ; j < starty + columns - offset; j++ { + res[count] = matrix[startx][j] + count++ + } + // 模拟填充右列从上到下(左闭右开) + for ; i < startx + rows - offset; i++ { + res[count] = matrix[i][j] + count++ + } + // 模拟填充下行从右到左(左闭右开) + for ; j > starty; j-- { + res[count] = matrix[i][j] + count++ + } + // 模拟填充左列从下到上(左闭右开) + for ; i > startx; i-- { + res[count] = matrix[i][starty] + count++ + } + + // 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1) + startx++ + starty++ + + // offset 控制每一圈里每一条边遍历的长度 + offset += 2 + loop-- + } + + // 如果min(rows, columns)为奇数的话,需要单独给矩阵最中间的位置赋值 + if min(rows, columns) % 2 == 1 { + if rows > columns { + for i := mid; i < mid + rows - columns + 1; i++ { + res[count] = matrix[i][mid] + count++ + } + } else { + for i := mid; i < mid + columns - rows + 1; i++ { + res[count] = matrix[mid][i] + count++ + } + } + } + return res +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} +``` +

From fda179fd7f0789e33f65b6bc5cb1c517a8a83104 Mon Sep 17 00:00:00 2001 From: markwang Date: Sun, 28 Apr 2024 15:21:11 +0800 Subject: [PATCH 1113/1533] =?UTF-8?q?54.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=E5=A2=9E=E5=8A=A0Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 175ae14745..022eed66f8 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -348,7 +348,7 @@ class Solution(object): return print_list ``` -### Go: +### Go ```go func spiralOrder(matrix [][]int) []int { From 8150d654ba7291b60b8d3a344abc015cd5f8c8c3 Mon Sep 17 00:00:00 2001 From: Yunliuyu <96341845+YShelter@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:47:33 +0800 Subject: [PATCH 1114/1533] =?UTF-8?q?Update=200142.=E7=8E=AF=E5=BD=A2?= =?UTF-8?q?=E9=93=BE=E8=A1=A8II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 错别字,再看本篇内容 --- .../0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index d97b160b91..7cda58c396 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -26,7 +26,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。** +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频再看本篇题解,更有助于大家对链表的理解。** ## 思路 From 2e489ea3bbe82a96a88e2c55ae92e19f8564f936 Mon Sep 17 00:00:00 2001 From: Jeff Lin Date: Sun, 28 Apr 2024 15:58:38 -0700 Subject: [PATCH 1115/1533] =?UTF-8?q?Update=200017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更正时间复杂度说明。 --- ...\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index e25d15d5ad..3b26066ae4 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -180,7 +180,7 @@ public: } }; ``` -* 时间复杂度: O(3^m * 4^n),其中 m 是对应四个字母的数字个数,n 是对应三个字母的数字个数 +* 时间复杂度: O(3^m * 4^n),其中 m 是对应三个字母的数字个数,n 是对应四个字母的数字个数 * 空间复杂度: O(3^m * 4^n) 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方) From 5ee2f42edb5765f6e28f2022fbcd257f27914b06 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 16:12:38 +0800 Subject: [PATCH 1116/1533] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 42 ++++++++++--------- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 4411b5609f..d834eef7e1 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -692,27 +692,29 @@ func levelOrderBottom(root *TreeNode) [][]int { #### Javascript: ```javascript -var levelOrderBottom = function(root) { - let res = [], queue = []; - queue.push(root); - while(queue.length && root!==null) { - // 存放当前层级节点数组 - let curLevel = []; - // 计算当前层级节点数量 - let length = queue.length; - while(length--) { - let node = queue.shift(); - // 把当前层节点存入curLevel数组 - curLevel.push(node.val); - // 把下一层级的左右节点存入queue队列 - node.left && queue.push(node.left); - node.right && queue.push(node.right); - } - // 从数组前头插入值,避免最后反转数组,减少运算时间 - res.unshift(curLevel); - } - return res; +var levelOrderBottom = function (root) { + let res = [], + queue = []; + queue.push(root); + while (queue.length && root !== null) { + // 存放当前层级节点数组 + let curLevel = []; + // 计算当前层级节点数量 + let length = queue.length; + while (length--) { + let node = queue.shift(); + // 把当前层节点存入curLevel数组 + curLevel.push(node.val); + // 把下一层级的左右节点存入queue队列 + node.left && queue.push(node.left); + node.right && queue.push(node.right); + } + // 从数组前头插入值,避免最后反转数组,减少运算时间 + res.unshift(curLevel); + } + return res; }; + ``` #### TypeScript: diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 7f847f8fae..15c5af3a64 100644 --- "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -71,7 +71,7 @@ 有递归的地方就有回溯,那么回溯在哪里呢? -就地递归函数的下面,例如如下代码: +就递归函数的下面,例如如下代码: ```cpp void dfs(参数) { From d7863c9dbfb124c93a1e4d589827b8397448553c Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 16:36:42 +0800 Subject: [PATCH 1117/1533] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index d834eef7e1..b9943f3982 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1142,7 +1142,7 @@ impl Solution { ### 思路 -本题就是层序遍历的时候把一层求个总和在取一个均值。 +本题就是层序遍历的时候把一层求个总和再取一个均值。 C++代码: From 4ca945f998d047eb7a0d9ea9308e53f46fd274fa Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 17:08:26 +0800 Subject: [PATCH 1118/1533] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index b9943f3982..0a98ab88a3 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1297,26 +1297,26 @@ func averageOfLevels(root *TreeNode) []float64 { ```javascript var averageOfLevels = function(root) { - //层级平均值 - let res = [], queue = []; - queue.push(root); - - while(queue.length && root!==null) { - //每一层节点个数 - let length = queue.length; - //sum记录每一层的和 - let sum = 0; - for(let i=0; i < length; i++) { - let node = queue.shift(); - sum += node.val; - node.left && queue.push(node.left); - node.right && queue.push(node.right); - } - //每一层的平均值存入数组res - res.push(sum/length); + let res = [], + queue = []; + queue.push(root); + while (queue.length) { + // 每一层节点个数; + let lengthLevel = queue.length, + len = queue.length, + // sum记录每一层的和; + sum = 0; + while (lengthLevel--) { + const node = queue.shift(); + sum += node.val; + // 队列存放下一层节点 + node.left && queue.push(node.left); + node.right && queue.push(node.right); } - - return res; + // 求平均值 + res.push(sum / len); + } + return res; }; ``` @@ -1927,26 +1927,28 @@ func max(x, y int) int { #### Javascript: ```javascript -var largestValues = function(root) { - //使用层序遍历 - let res = [], queue = []; - queue.push(root); - - while(root !== null && queue.length) { - //设置max初始值就是队列的第一个元素 - let max = queue[0].val; - let length = queue.length; - while(length--) { - let node = queue.shift(); - max = max > node.val ? max : node.val; - node.left && queue.push(node.left); - node.right && queue.push(node.right); - } - //把每一层的最大值放到res数组 - res.push(max); - } - +var largestValues = function (root) { + let res = [], + queue = []; + queue.push(root); + if (root === null) { return res; + } + while (queue.length) { + let lengthLevel = queue.length, + // 初始值设为负无穷大 + max = -Infinity; + while (lengthLevel--) { + const node = queue.shift(); + // 在当前层中找到最大值 + max = Math.max(max, node.val); + // 找到下一层的节点 + node.left && queue.push(node.left); + node.right && queue.push(node.right); + } + res.push(max); + } + return res; }; ``` From 2d37cd39e02789d09387b5dc1176fd8f71adf769 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 18:19:48 +0800 Subject: [PATCH 1119/1533] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 0a98ab88a3..1cb916b41c 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -2809,21 +2809,23 @@ func maxDepth(root *TreeNode) int { * @param {TreeNode} root * @return {number} */ -var maxDepth = function(root) { - // 最大的深度就是二叉树的层数 - if (root === null) return 0; - let queue = [root]; - let height = 0; - while (queue.length) { - let n = queue.length; - height++; - for (let i=0; i Date: Tue, 30 Apr 2024 18:16:57 +0800 Subject: [PATCH 1120/1533] =?UTF-8?q?docs:=20=E3=80=900055=E5=8F=B3?= =?UTF-8?q?=E6=97=8B=E5=AD=97=E7=AC=A6=E4=B8=B2=E3=80=91JavaScript?= =?UTF-8?q?=E9=83=A8=E5=88=86=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\255\227\347\254\246\344\270\262.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 7137186042..a9f57192e4 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -254,7 +254,37 @@ func main(){ ### JavaScript: +```javascript +// JS中字符串内不可单独修改 + +// 右旋转 +function reverseLeftWords(s, k) { + const reverse = (sList, start, end) => { + for (let i = start, j = end; i < j; i++, j--) { + [sList[i], sList[j]] = [sList[j], sList[i]]; + } + } + const sList = Array.from(s); + reverse(sList, 0, sList.length - k - 1); + reverse(sList, sList.length - k, sList.length - 1); + reverse(sList, 0, sList.length - 1); + return sList.join(''); +} +// 左旋转 +var reverseLeftWords = function(s, n) { + const reverse = (sList, start, end) => { + for (let i = start, j = end; i < j; i++, j--) { + [sList[i], sList[j]] = [sList[j], sList[i]]; + } + } + const sList = s.split(''); + reverse(sList, 0, n - 1); + reverse(sList, n, sList.length - 1); + reverse(sList, 0, sList.length - 1); + return sList.join(''); +}; +``` ### TypeScript: From 3e5c1705c2cda8cdb6acd9c7581b16d81fd4b77d Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Wed, 1 May 2024 16:08:36 +0800 Subject: [PATCH 1121/1533] =?UTF-8?q?refactor:=200077.=E7=BB=84=E5=90=88.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 103fb627f5..35ed5fad49 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -469,6 +469,32 @@ func dfs(n int, k int, start int) { ``` ### Javascript +未剪枝: + +```js +var combine = function (n, k) { + // 回溯法 + let result = [], + path = []; + let backtracking = (_n, _k, startIndex) => { + // 终止条件 + if (path.length === _k) { + result.push(path.slice()); + return; + } + // 循环本层集合元素 + for (let i = startIndex; i <= _n; i++) { + path.push(i); + // 递归 + backtracking(_n, _k, i + 1); + // 回溯操作 + path.pop(); + } + }; + backtracking(n, k, 1); + return result; +}; +``` 剪枝: From d1d7cdeecd2922b86edccb7cfd135117a5926a60 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Wed, 1 May 2024 19:10:32 +0800 Subject: [PATCH 1122/1533] =?UTF-8?q?refactor:=200077.=E7=BB=84=E5=90=88.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 38 ++++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 35ed5fad49..f394b3d84c 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -499,24 +499,28 @@ var combine = function (n, k) { 剪枝: ```javascript -let result = [] -let path = [] -var combine = function(n, k) { - result = [] - combineHelper(n, k, 1) - return result +var combine = function (n, k) { + // 回溯法 + let result = [], + path = []; + let backtracking = (_n, _k, startIndex) => { + // 终止条件 + if (path.length === _k) { + result.push(path.slice()); + return; + } + // 循环本层集合元素 + for (let i = startIndex; i <= _n - (_k - path.length) + 1; i++) { + path.push(i); + // 递归 + backtracking(_n, _k, i + 1); + // 回溯操作 + path.pop(); + } + }; + backtracking(n, k, 1); + return result; }; -const combineHelper = (n, k, startIndex) => { - if (path.length === k) { - result.push([...path]) - return - } - for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) { - path.push(i) - combineHelper(n, k, i + 1) - path.pop() - } -} ``` ### TypeScript From 5e6a657d90fe9a3ba7c86aa8505898e0f7ed739b Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Wed, 1 May 2024 19:51:01 +0800 Subject: [PATCH 1123/1533] =?UTF-8?q?refactor:=200216.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\220\210\346\200\273\345\222\214III.md" | 93 ++++++++++++++----- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index ac28f9fcfa..d331726e55 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -417,6 +417,7 @@ func dfs(k, n int, start int, sum int) { ``` ### JavaScript +- 未剪枝: ```js /** @@ -424,32 +425,74 @@ func dfs(k, n int, start int, sum int) { * @param {number} n * @return {number[][]} */ -var combinationSum3 = function(k, n) { - let res = []; - let path = []; - let sum = 0; - const dfs = (path,index) => { - // 剪枝操作 - if (sum > n){ - return - } - if (path.length == k) { - if(sum == n){ - res.push([...path]); - return - } - } - for (let i = index; i <= 9 - (k-path.length) + 1;i++) { - path.push(i); - sum = sum + i; - index += 1; - dfs(path,index); - sum -= i - path.pop() - } +var combinationSum3 = function (k, n) { + // 回溯法 + let result = [], + path = []; + const backtracking = (_k, targetSum, sum, startIndex) => { + // 终止条件 + if (path.length === _k) { + if (sum === targetSum) { + result.push(path.slice()); + } + // 如果总和不相等,就直接返回 + return; } - dfs(path,1); - return res + + // 循环当前节点,因为只使用数字1到9,所以最大是9 + for (let i = startIndex; i <= 9; i++) { + path.push(i); + sum += i; + // 回调函数 + backtracking(_k, targetSum, sum, i + 1); + // 回溯 + sum -= i; + path.pop(); + } + }; + backtracking(k, n, 0, 1); + return result; +}; +``` + +- 剪枝: + +```js +/** + * @param {number} k + * @param {number} n + * @return {number[][]} + */ +var combinationSum3 = function (k, n) { + // 回溯法 + let result = [], + path = []; + const backtracking = (_k, targetSum, sum, startIndex) => { + if (sum > targetSum) { + return; + } + // 终止条件 + if (path.length === _k) { + if (sum === targetSum) { + result.push(path.slice()); + } + // 如果总和不相等,就直接返回 + return; + } + + // 循环当前节点,因为只使用数字1到9,所以最大是9 + for (let i = startIndex; i <= 9 - (_k - path.length) + 1; i++) { + path.push(i); + sum += i; + // 回调函数 + backtracking(_k, targetSum, sum, i + 1); + // 回溯 + sum -= i; + path.pop(); + } + }; + backtracking(k, n, 0, 1); + return result; }; ``` From d488652a135250c82fb9c8e984922ba834665e14 Mon Sep 17 00:00:00 2001 From: Yunliuyu <96341845+YShelter@users.noreply.github.com> Date: Thu, 2 May 2024 21:45:01 +0800 Subject: [PATCH 1124/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 又是错别字,在 -> 再 碎碎念:这样子可以吗? --- ...454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index db9a9e430c..83dea97e7e 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -54,7 +54,7 @@ 1. 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。 2. 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。 3. 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。 -4. 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。 +4. 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。 5. 最后返回统计值 count 就可以了 C++代码: @@ -71,7 +71,7 @@ public: } } int count = 0; // 统计a+b+c+d = 0 出现的次数 - // 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。 + // 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。 for (int c : C) { for (int d : D) { if (umap.find(0 - (c + d)) != umap.end()) { From fffec6ec918a3d45950497e14778882b258c73ff Mon Sep 17 00:00:00 2001 From: heystone999 <52831724+heystone999@users.noreply.github.com> Date: Fri, 3 May 2024 17:50:11 -0400 Subject: [PATCH 1125/1533] =?UTF-8?q?style:=20=E4=BB=A3=E7=A0=81highlight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...43\236\345\234\260\347\232\204\346\225\260\351\207\217.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 3488777ad2..5063139fe0 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -395,7 +395,7 @@ class Solution { 深度优先遍历 -```Python3 +```Python class Solution: def __init__(self): self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向 @@ -442,7 +442,7 @@ class Solution: 广度优先遍历 -```Python3 +```Python class Solution: def __init__(self): self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向 From cd03a5b79a5deb90e444d46ba4c22cffd9478440 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 7 May 2024 10:57:05 +0800 Subject: [PATCH 1126/1533] =?UTF-8?q?15.=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=E5=A2=9E=E5=8A=A0Go=E5=93=88=E5=B8=8C=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\225\260\344\271\213\345\222\214.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index ae21838540..f7146907b4 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -403,6 +403,7 @@ class Solution: ``` ### Go: +(版本一) 双指针 ```Go func threeSum(nums []int) [][]int { @@ -442,6 +443,42 @@ func threeSum(nums []int) [][]int { return res } ``` +(版本二) 哈希解法 + +```Go +func threeSum(nums []int) [][]int { + res := make([][]int, 0) + sort.Ints(nums) + // 找出a + b + c = 0 + // a = nums[i], b = nums[j], c = -(a + b) + for i := 0; i < len(nums); i++ { + // 排序之后如果第一个元素已经大于零,那么不可能凑成三元组 + if nums[i] > 0 { + break + } + // 三元组元素a去重 + if i > 0 && nums[i] == nums[i-1] { + continue + } + set := make(map[int]struct{}) + for j := i + 1; j < len(nums); j++ { + // 三元组元素b去重 + if j > i + 2 && nums[j] == nums[j-1] && nums[j-1] == nums[j-2] { + continue + } + c := -nums[i] - nums[j] + if _, ok := set[c]; ok { + res = append(res, []int{nums[i], nums[j], c}) + // 三元组元素c去重 + delete(set, c) + } else { + set[nums[j]] = struct{}{} + } + } + } + return res +} +``` ### JavaScript: From fa6c8d2e98240323085e9970c5016a951f916eff Mon Sep 17 00:00:00 2001 From: ray <109325327+raydemo1@users.noreply.github.com> Date: Wed, 8 May 2024 18:45:21 +0800 Subject: [PATCH 1127/1533] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md=20Pyt?= =?UTF-8?q?hon=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=E9=80=92=E5=BD=92?= =?UTF-8?q?=E6=B3=95=E6=9B=B4=E7=AE=80=E6=B4=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index ab6b07bfcf..cdc7a7837a 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -201,7 +201,7 @@ class Solution: return result ``` ```python -# 递归法 +#递归法 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): @@ -210,18 +210,24 @@ class Solution: # self.right = right class Solution: def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + if not root: + return [] + levels = [] - self.helper(root, 0, levels) + + def traverse(node, level): + if not node: + return + + if len(levels) == level: + levels.append([]) + + levels[level].append(node.val) + traverse(node.left, level + 1) + traverse(node.right, level + 1) + + traverse(root, 0) return levels - - def helper(self, node, level, levels): - if not node: - return - if len(levels) == level: - levels.append([]) - levels[level].append(node.val) - self.helper(node.left, level + 1, levels) - self.helper(node.right, level + 1, levels) ``` From 60ec1b83e2d697fdf5f31118509f362e42d50664 Mon Sep 17 00:00:00 2001 From: coffeeboy <114488340+zp-coffee@users.noreply.github.com> Date: Fri, 10 May 2024 10:20:57 +0800 Subject: [PATCH 1128/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新排版以及修改一个错别字 --- ...32\204\350\277\255\344\273\243\351\201\215\345\216\206.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 8549bac154..5f59c38897 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -117,7 +117,7 @@ public: ### 后序遍历(迭代法) -再来看后序遍历,先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: +再来看后序遍历,先序遍历是中左右,后序遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: ![前序到后序](https://code-thinking-1253855093.file.myqcloud.com/pics/20200808200338924.png) @@ -153,7 +153,7 @@ public: 上面这句话,可能一些同学不太理解,建议自己亲手用迭代法,先写出来前序,再试试能不能写出中序,就能理解了。 -**那么问题又来了,难道 二叉树前后中序遍历的迭代法实现,就不能风格统一么(即前序遍历 改变代码顺序就可以实现中序 和 后序)?** +**那么问题又来了,难道二叉树前后中序遍历的迭代法实现,就不能风格统一么(即前序遍历改变代码顺序就可以实现中序 和 后序)?** 当然可以,这种写法,还不是很好理解,我们将在下一篇文章里重点讲解,敬请期待! From 12fffbeaa86bfc293caf22b4b5fc05d4e929d738 Mon Sep 17 00:00:00 2001 From: wbingb Date: Wed, 15 May 2024 10:39:13 +0800 Subject: [PATCH 1129/1533] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .obsidian/app.json | 1 - .obsidian/appearance.json | 3 - .obsidian/core-plugins-migration.json | 30 ------ .obsidian/core-plugins.json | 20 ---- .obsidian/workspace.json | 147 -------------------------- 5 files changed, 201 deletions(-) delete mode 100644 .obsidian/app.json delete mode 100644 .obsidian/appearance.json delete mode 100644 .obsidian/core-plugins-migration.json delete mode 100644 .obsidian/core-plugins.json delete mode 100644 .obsidian/workspace.json diff --git a/.obsidian/app.json b/.obsidian/app.json deleted file mode 100644 index 9e26dfeeb6..0000000000 --- a/.obsidian/app.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json deleted file mode 100644 index c8c365d89b..0000000000 --- a/.obsidian/appearance.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "accentColor": "" -} \ No newline at end of file diff --git a/.obsidian/core-plugins-migration.json b/.obsidian/core-plugins-migration.json deleted file mode 100644 index 436f43cf56..0000000000 --- a/.obsidian/core-plugins-migration.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "file-explorer": true, - "global-search": true, - "switcher": true, - "graph": true, - "backlink": true, - "canvas": true, - "outgoing-link": true, - "tag-pane": true, - "properties": false, - "page-preview": true, - "daily-notes": true, - "templates": true, - "note-composer": true, - "command-palette": true, - "slash-command": false, - "editor-status": true, - "bookmarks": true, - "markdown-importer": false, - "zk-prefixer": false, - "random-note": false, - "outline": true, - "word-count": true, - "slides": false, - "audio-recorder": false, - "workspaces": false, - "file-recovery": true, - "publish": false, - "sync": false -} \ No newline at end of file diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json deleted file mode 100644 index 9405bfdc22..0000000000 --- a/.obsidian/core-plugins.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - "file-explorer", - "global-search", - "switcher", - "graph", - "backlink", - "canvas", - "outgoing-link", - "tag-pane", - "page-preview", - "daily-notes", - "templates", - "note-composer", - "command-palette", - "editor-status", - "bookmarks", - "outline", - "word-count", - "file-recovery" -] \ No newline at end of file diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json deleted file mode 100644 index 144fe03fdc..0000000000 --- a/.obsidian/workspace.json +++ /dev/null @@ -1,147 +0,0 @@ -{ - "main": { - "id": "14608cf8b641f651", - "type": "split", - "children": [ - { - "id": "7b559c19d2418ebd", - "type": "tabs", - "children": [ - { - "id": "a006865600bc4e20", - "type": "leaf", - "state": { - "type": "empty", - "state": {} - } - } - ] - } - ], - "direction": "vertical" - }, - "left": { - "id": "340e6a79c670a47b", - "type": "split", - "children": [ - { - "id": "c5f81ca398c18cda", - "type": "tabs", - "children": [ - { - "id": "51f9f8f44ecceb89", - "type": "leaf", - "state": { - "type": "file-explorer", - "state": { - "sortOrder": "alphabetical" - } - } - }, - { - "id": "0cff567244d7cff5", - "type": "leaf", - "state": { - "type": "search", - "state": { - "query": "1971", - "matchingCase": false, - "explainSearch": false, - "collapseAll": false, - "extraContext": false, - "sortOrder": "alphabetical" - } - } - }, - { - "id": "209574c920774a4d", - "type": "leaf", - "state": { - "type": "bookmarks", - "state": {} - } - } - ], - "currentTab": 1 - } - ], - "direction": "horizontal", - "width": 300 - }, - "right": { - "id": "93ce8f4c11893983", - "type": "split", - "children": [ - { - "id": "da8cbb56e5ee7c52", - "type": "tabs", - "children": [ - { - "id": "dfccbc1ebc0bb831", - "type": "leaf", - "state": { - "type": "backlink", - "state": { - "collapseAll": false, - "extraContext": false, - "sortOrder": "alphabetical", - "showSearch": false, - "searchQuery": "", - "backlinkCollapsed": false, - "unlinkedCollapsed": true - } - } - }, - { - "id": "9d2201419a111fe4", - "type": "leaf", - "state": { - "type": "outgoing-link", - "state": { - "linksCollapsed": false, - "unlinkedCollapsed": true - } - } - }, - { - "id": "dc86ccf1b01bc02c", - "type": "leaf", - "state": { - "type": "tag", - "state": { - "sortOrder": "frequency", - "useHierarchy": true - } - } - }, - { - "id": "ec512545d2a7f2e5", - "type": "leaf", - "state": { - "type": "outline", - "state": {} - } - } - ] - } - ], - "direction": "horizontal", - "width": 300, - "collapsed": true - }, - "left-ribbon": { - "hiddenItems": { - "switcher:打开快速切换": false, - "graph:查看关系图谱": false, - "canvas:新建白板": false, - "daily-notes:打开/创建今天的日记": false, - "templates:插入模板": false, - "command-palette:打开命令面板": false - } - }, - "active": "a006865600bc4e20", - "lastOpenFiles": [ - "problems/1971.寻找图中是否存在路径.md", - "README.md" - ] -} \ No newline at end of file From 96359371d8668d1648cce37b2cfe3614fd01a49b Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 15 May 2024 10:52:59 +0800 Subject: [PATCH 1130/1533] =?UTF-8?q?459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=A2=9E=E5=8A=A0Go?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E5=8C=B9=E9=85=8D=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\255\220\345\255\227\347\254\246\344\270\262.md" | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 1028bf1ea2..5142579681 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -403,6 +403,18 @@ func repeatedSubstringPattern(s string) bool { } ``` +移动匹配 + +```go +func repeatedSubstringPattern(s string) bool { + if len(s) == 0 { + return false + } + t := s + s + return strings.Contains(t[1:len(t)-1], s) +} +``` + ### JavaScript: > 前缀表统一减一 From 94a4e812655c462115234246d9cf2cb2bcdbbc8b Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 15 May 2024 11:35:26 +0800 Subject: [PATCH 1131/1533] Update --- README.md | 13 +- ...7.\350\257\276\347\250\213\350\241\250.md" | 66 +-- ...\350\257\276\347\250\213\350\241\250II.md" | 39 ++ ...57\344\273\266\346\236\204\345\273\272.md" | 337 +++++++++++++ ...7\347\211\251\350\277\220\350\276\223I.md" | 49 +- ...\347\211\251\350\277\220\350\276\223II.md" | 4 +- ...347\211\251\350\277\220\350\276\223III.md" | 65 ++- ...16\351\200\233\345\205\254\345\233\255.md" | 128 +++-- ...57\350\276\276\350\267\257\345\276\204.md" | 471 ++++++++++++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 0 ...06\350\256\272\345\237\272\347\241\200.md" | 0 ...72\346\200\273\347\273\223\347\257\207.md" | 31 ++ ...06\350\256\272\345\237\272\347\241\200.md" | 0 ...06\350\256\272\345\237\272\347\241\200.md" | 222 +++++++++ ...30\346\200\273\347\273\223\347\257\207.md" | 48 ++ .../ACM\346\250\241\345\274\217.md" | 0 ...05\345\255\230\346\266\210\350\200\227.md" | 0 ...26\350\257\221\350\277\220\350\241\214.md" | 0 ...64\345\244\215\346\235\202\345\272\246.md" | 0 ...64\345\244\215\346\235\202\345\272\246.md" | 0 ...27\346\263\225\350\266\205\346\227\266.md" | 0 ...64\345\244\215\346\235\202\345\272\246.md" | 0 22 files changed, 1367 insertions(+), 106 deletions(-) create mode 100644 "problems/0210.\350\257\276\347\250\213\350\241\250II.md" create mode 100644 "problems/kamacoder/00.\350\275\257\344\273\266\346\236\204\345\273\272.md" create mode 100644 "problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" rename "problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" => "problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" (100%) rename "problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" => "problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" (100%) create mode 100644 "problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" rename "problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" => "problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" (100%) create mode 100644 "problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" create mode 100644 "problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" rename "problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" => "problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" (100%) rename "problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" => "problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" (100%) rename "problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" => "problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214.md" (100%) rename "problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" => "problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" (100%) rename "problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" => "problems/\345\211\215\345\272\217/\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246.md" (100%) rename "problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" => "problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" (100%) rename "problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" => "problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" (100%) diff --git a/README.md b/README.md index 5389c89a9b..e94b428578 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,7 @@ * 编程语言 * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) - * [C++语言基础课](https://kamacoder.com/course.php?course_id=1) - * [Java语言基础课](https://kamacoder.com/course.php?course_id=2) + * [编程语言基础课](https://kamacoder.com/courseshop.php) * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) * 工具 @@ -91,12 +90,12 @@ * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) * 算法性能分析 - * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) - * [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md) - * [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md) - * [关于空间复杂度,可能有几个疑问?](./problems/前序/关于空间复杂度,可能有几个疑问?.md) + * [关于时间复杂度,你不知道的都在这里!](./problems/前序/时间复杂度.md) + * [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/算法超时.md) + * [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/递归算法的时间复杂度.md) + * [关于空间复杂度,可能有几个疑问?](./problems/前序/空间复杂度.md) * [递归算法的时间与空间复杂度分析!](./problems/前序/递归算法的时间与空间复杂度分析.md) - * [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md) + * [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/内存消耗.md) ## 数组 diff --git "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" index 6a8eb23b84..dff0b18eb0 100644 --- "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" +++ "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" @@ -12,10 +12,6 @@ 引用与任务调度,课程安排等等。 -为什么 - - ------ 「拓扑排序」是专门应用于有向图的算法; @@ -26,32 +22,42 @@ 这道题的做法同样适用于第 210 题。 -``` -vector inDegree(numCourses); -unordered_map> map; -for (int i = 0; i < prerequisites.size(); i++) { - inDegree[prerequisites[i][0]]++;//当前课程入度值+1 - map[prerequisites[i][1]].push_back(prerequisites[i][0]);//添加依赖他的后续课 -} -queue Qu; -for (int i = 0; i < numCourses; i++) { - if (inDegree[i] == 0) Qu.push(i);//所有入度为0的课入列 -} -int count = 0; -while (Qu.size()) { - int selected = Qu.front(); //当前选的课 - Qu.pop();//出列 - count++;//选课数+1 - vector toEnQueue = map[selected];//获取这门课对应的后续课 - if (toEnQueue.size()) { //确实有后续课 - for (int i = 0; i < toEnQueue.size(); i++) { - inDegree[toEnQueue[i]]--; //依赖它的后续课的入度-1 - if (inDegree[toEnQueue[i]] == 0) Qu.push(toEnQueue[i]); //如果因此减为0,入列 - } - } -} -if (count == numCourses) return true; -return false; +```CPP +class Solution { +public: + bool canFinish(int numCourses, vector>& prerequisites) { + vector inDegree(numCourses, 0); + unordered_map> umap; + for (int i = 0; i < prerequisites.size(); i++) { + + // prerequisites[i][0] 是 课程入度,prerequisites[i][1] 是课程出度 + // 即: 上课prerequisites[i][0] 之前,必须先上课prerequisites[i][1] + // prerequisites[i][1] -> prerequisites[i][0] + inDegree[prerequisites[i][0]]++;//当前课程入度值+1 + umap[prerequisites[i][1]].push_back(prerequisites[i][0]); // 添加 prerequisites[i][1] 指向的课程 + } + queue que; + for (int i = 0; i < numCourses; i++) { + if (inDegree[i] == 0) que.push(i); // 所有入度为0,即为 开头课程 加入队列 + } + int count = 0; + while (que.size()) { + int cur = que.front(); //当前选的课 + que.pop(); + count++; // 选课数+1 + vector courses = umap[cur]; //获取这门课指向的课程,也就是这么课的后续课 + if (courses.size()) { // 有后续课 + for (int i = 0; i < courses.size(); i++) { + inDegree[courses[i]]--; // 它的后续课的入度-1 + if (inDegree[courses[i]] == 0) que.push(courses[i]); // 如果入度为0,加入队列 + } + } + } + if (count == numCourses) return true; + return false; + + } +}; ```

diff --git "a/problems/0210.\350\257\276\347\250\213\350\241\250II.md" "b/problems/0210.\350\257\276\347\250\213\350\241\250II.md" new file mode 100644 index 0000000000..2d2e242941 --- /dev/null +++ "b/problems/0210.\350\257\276\347\250\213\350\241\250II.md" @@ -0,0 +1,39 @@ + +```CPP +class Solution { +public: + vector findOrder(int numCourses, vector>& prerequisites) { + vector inDegree(numCourses, 0); + vector result; + unordered_map> umap; + for (int i = 0; i < prerequisites.size(); i++) { + + // prerequisites[i][0] 是 课程入度,prerequisites[i][1] 是课程出度 + // 即: 上课prerequisites[i][0] 之前,必须先上课prerequisites[i][1] + // prerequisites[i][1] -> prerequisites[i][0] + inDegree[prerequisites[i][0]]++;//当前课程入度值+1 + umap[prerequisites[i][1]].push_back(prerequisites[i][0]); // 添加 prerequisites[i][1] 指向的课程 + } + queue que; + for (int i = 0; i < numCourses; i++) { + if (inDegree[i] == 0) que.push(i); // 所有入度为0,即为 开头课程 加入队列 + } + int count = 0; + while (que.size()) { + int cur = que.front(); //当前选的课 + que.pop(); + count++; // 选课数+1 + result.push_back(cur); + vector courses = umap[cur]; //获取这门课指向的课程,也就是这么课的后续课 + if (courses.size()) { // 有后续课 + for (int i = 0; i < courses.size(); i++) { + inDegree[courses[i]]--; // 它的后续课的入度-1 + if (inDegree[courses[i]] == 0) que.push(courses[i]); // 如果入度为0,加入队列 + } + } + } + if (count == numCourses) return result; + else return vector(); + } +}; +``` diff --git "a/problems/kamacoder/00.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/00.\350\275\257\344\273\266\346\236\204\345\273\272.md" new file mode 100644 index 0000000000..7229489b6a --- /dev/null +++ "b/problems/kamacoder/00.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -0,0 +1,337 @@ + +# 拓扑排序精讲 + +[卡码网:软件构建](https://kamacoder.com/problempage.php?pid=1191) + +题目描述: + +某个大型软件项目的构建系统拥有 N 个文件,文件编号从 0 到 N - 1,在这些文件中,某些文件依赖于其他文件的内容,这意味着如果文件 A 依赖于文件 B,则必须在处理文件 A 之前处理文件 B (0 <= A, B <= N - 1)。请编写一个算法,用于确定文件处理的顺序。 + +输入描述: + +第一行输入两个正整数 M, N。表示 N 个文件之间拥有 M 条依赖关系。 + +后续 M 行,每行两个正整数 S 和 T,表示 T 文件依赖于 S 文件。 + +输出描述: + +输出共一行,如果能处理成功,则输出文件顺序,用空格隔开。 + +如果不能成功处理(相互依赖),则输出 -1。 + +输入示例: + +``` +5 4 +0 1 +0 2 +1 3 +2 4 +``` + +输出示例: + +0 1 2 3 4 + +提示信息: + +文件依赖关系如下: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510192157.png) + +所以,文件处理的顺序除了示例中的顺序,还存在 + +0 2 4 1 3 + +0 2 1 3 4 + +等等合法的顺序。 + +数据范围: + +* 0 <= N <= 10 ^ 5 +* 1 <= M <= 10 ^ 9 + + +## 拓扑排序的背景 + +本题是拓扑排序的经典题目。 + +一聊到 拓扑排序,一些录友可能会想这是排序,不会想到这是图论算法。 + +其实拓扑排序是经典的图论问题。 + +先说说 拓扑排序的应用场景。 + +大学排课,例如 先上A课,才能上B课,上了B课才能上C课,上了A课才能上D课,等等一系列这样的依赖顺序。 问给规划出一条 完整的上课顺序。 + +拓扑排序在文件处理上也有应用,我们在做项目安装文件包的时候,经常发现 复杂的文件依赖关系, A依赖B,B依赖C,B依赖D,C依赖E 等等。 + +如果给出一条线性的依赖顺序来下载这些文件呢? + +有录友想上面的例子都很简单啊,我一眼能给排序出来。 + +那如果上面的依赖关系是一百对呢,一千对甚至上万个依赖关系,这些依赖关系中可能还有循环依赖,你如何发现循环依赖呢,又如果排出线性顺序呢。 + +所以 拓扑排序就是专门解决这类问题的。 + +概括来说,**给出一个 有向图,把这个有向图转成线性的排序 就叫拓扑排序**。 + +当然拓扑排序也要检测这个有向图 是否有环,即存在循环依赖的情况,因为这种情况是不能做线性排序的。 + +所以**拓扑排序也是图论中判断有向无环图的常用方法**。 + +------------ + + +## 拓扑排序的思路 + +拓扑排序指的是一种 解决问题的大体思路, 而具体算法,可能是广搜也可能是深搜。 + +大家可能发现 各式各样的解法,纠结哪个是拓扑排序? + +其实只要能在把 有向无环图 进行线性排序 的算法 都可以叫做 拓扑排序。 + +实现拓扑排序的算法有两种:卡恩算法(BFS)和DFS + +> 卡恩1962年提出这种解决拓扑排序的思路 + +一般来说我们只需要掌握 BFS (广度优先搜索)就可以了,清晰易懂,如果还想多了解一些,可以再去学一下 DFS 的思路,但 DFS 不是本篇重点。 + +接下来我们来讲解BFS的实现思路。 + +以题目中示例为例如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510110836.png) + +做拓扑排序的话,如果肉眼去找开头的节点,一定能找到 节点0 吧,都知道要从节点0 开始。 + +但为什么我们能找到 节点0呢,因为我们肉眼看着 这个图就是从 节点0出发的。 + +作为出发节点,它有什么特征? + +你看节点0 的入度 为0 出度为2, 也就是 没有边指向它,而它有两条边是指出去的。 + +> 节点的入度表示 有多少条边指向它,节点的出度表示有多少条边 从该节点出发。 + +所以当我们做拓扑排序的时候,应该优先找 入度为 0 的节点,只有入度为0,它才是出发节点。 +**理解以上内容很重要**! + +接下来我给出 拓扑排序的过程,其实就两步: + +1. 找到入度为0 的节点,加入结果集 +2. 将该节点从图中移除 + +循环以上两步,直到 所有节点都在图中被移除了。 + +结果集的顺序,就是我们想要的拓扑排序顺序 (结果集里顺序可能不唯一) + +## 模拟过程 + +用本题的示例来模拟一下这一过程: + + +1、找到入度为0 的节点,加入结果集 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113110.png) + +2、将该节点从图中移除 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113142.png) + +---------------- + +1、找到入度为0 的节点,加入结果集 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113345.png) + +这里大家会发现,节点1 和 节点2 入度都为0, 选哪个呢? + +选哪个都行,所以这也是为什么拓扑排序的结果是不唯一的。 + +2、将该节点从图中移除 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113640.png) + +--------------- + +1、找到入度为0 的节点,加入结果集 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113853.png) + +节点2 和 节点3 入度都为0,选哪个都行,这里选节点2 + +2、将该节点从图中移除 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510114004.png) + +-------------- + +后面的过程一样的,节点3 和 节点4,入度都为0,选哪个都行。 + +最后结果集为: 0 1 2 3 4 。当然结果不唯一的。 + +## 判断有环 + +如果有 有向环怎么办呢?例如这个图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510115115.png) + +这个图,我们只能将入度为0 的节点0 接入结果集。 + +之后,节点1、2、3、4 形成了环,找不到入度为0 的节点了,所以此时结果集里只有一个元素。 +那么如果我们发现结果集元素个数 不等于 图中节点个数,我们就可以认定图中一定有 有向环! +这也是拓扑排序判断有向环的方法。 + +通过以上过程的模拟大家会发现这个拓扑排序好像不难,还有点简单。 + +## 写代码 + +理解思想后,确实不难,但代码写起来也不容易。 + +为了每次可以找到所有节点的入度信息,我们要在初始话的时候,就把每个节点的入度 和 每个节点的依赖关系做统计。 + +代码如下: + +```CPP +cin >> n >> m; +vector inDegree(n, 0); // 记录每个文件的入度 +vector result; // 记录结果 +unordered_map> umap; // 记录文件依赖关系 + +while (m--) { + // s->t,先有s才能有t + cin >> s >> t; + inDegree[t]++; // t的入度加一 + umap[s].push_back(t); // 记录s指向哪些文件 +} + +``` + +找入度为0 的节点,我们需要用一个队列放存放。 + +因为每次寻找入度为0的节点,不一定只有一个节点,可能很多节点入度都为0,所以要将这些入度为0的节点放到队列里,依次去处理。 + +代码如下: + +```CPP + +queue que; +for (int i = 0; i < n; i++) { + // 入度为0的节点,可以作为开头,先加入队列 + if (inDegree[i] == 0) que.push(i); +} +``` + +开始从队列里遍历入度为0 的节点,将其放入结果集。 + +```CPP + +while (que.size()) { + int cur = que.front(); // 当前选中的节点 + que.pop(); + result.push_back(cur); + // 将该节点从图中移除 + +} +``` + +这里面还有一个很重要的过程,如何把这个入度为0的节点从图中移除呢? + +首先我们为什么要把节点从图中移除? + +为的是将 该节点作为出发点所连接的边删掉。 + +删掉的目的是什么呢? + +要把 该节点作为出发点所连接的节点的 入度 减一。 + +如果这里不理解,看上面的模拟过程第一步: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113110.png) + +这事节点1 和 节点2 的入度为 1。 + +将节点0删除后,图为这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113142.png) + +那么 节点0 作为出发点 所连接的节点的入度 就都做了 减一 的操作。 + +此时 节点1 和 节点 2 的入度都为0, 这样才能作为下一轮选取的节点。 + +所以,我们在代码实现的过程中,本质是要将 该节点作为出发点所连接的节点的 入度 减一 就可以了,这样好能根据入度找下一个节点,不用真在图里把这个节点删掉。 + +该过程代码如下: + + +```CPP + +while (que.size()) { + int cur = que.front(); // 当前选中的节点 + que.pop(); + result.push_back(cur); + // 将该节点从图中移除 + vector files = umap[cur]; //获取cur指向的节点 + if (files.size()) { // 如果cur有指向的节点 + for (int i = 0; i < files.size(); i++) { // 遍历cur指向的节点 + inDegree[files[i]] --; // cur指向的节点入度都做减一操作 + // 如果指向的节点减一之后,入度为0,说明是我们要选取的下一个节点,放入队列。 + if(inDegree[files[i]] == 0) que.push(files[i]); + } + } + +} +``` + +最后代码如下: + + +```CPP +#include +#include +#include +#include +using namespace std; +int main() { + int m, n, s, t; + cin >> n >> m; + vector inDegree(n, 0); // 记录每个节点的入度 + + unordered_map> umap;// 记录节点依赖关系 + vector result; // 记录结果 + + while (m--) { + // s->t,先有s才能有t + cin >> s >> t; + inDegree[t]++; // t的入度加一 + umap[s].push_back(t); // 记录s指向哪些节点 + } + queue que; + for (int i = 0; i < n; i++) { + // 入度为0的节点,可以作为开头,先加入队列 + if (inDegree[i] == 0) que.push(i); + //cout << inDegree[i] << endl; + } + // int count = 0; + while (que.size()) { + int cur = que.front(); // 当前选中的节点 + que.pop(); + //count++; + result.push_back(cur); + vector files = umap[cur]; //获取该节点指向的节点 + if (files.size()) { // cur有后续节点 + for (int i = 0; i < files.size(); i++) { + inDegree[files[i]] --; // cur的指向的节点入度-1 + if(inDegree[files[i]] == 0) que.push(files[i]); + } + } + } + // 判断是否有有向环 + if (result.size() == n) { + // 注意输出格式,最后一个元素后面没有空格 + for (int i = 0; i < n - 2; i++) cout << result[i] << " "; + cout << result[n - 1]; + } else cout << -1 << endl; +} +``` diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 0ce00bbf1d..dc9b46f338 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -1,18 +1,21 @@ -# 94. 城市间货物运输 I +# Bellman_ford 算法精讲 -[卡码网: 94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) +[卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) 题目描述 某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 -网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。权值为正表示扣除了政府补贴后运输货物仍需支付的费用;权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 +网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。 +权值为正表示扣除了政府补贴后运输货物仍需支付的费用;权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 -请找出从城市 1 到城市 n 的所有可能路径中,综合政府补贴后的最低运输成本。如果最低运输成本是一个负数,它表示在遵循最优路径的情况下,运输过程中反而能够实现盈利。 +请找出从城市 1 到城市 n 的所有可能路径中,综合政府补贴后的最低运输成本。 + +如果最低运输成本是一个负数,它表示在遵循最优路径的情况下,运输过程中反而能够实现盈利。 城市 1 到城市 n 之间可能会出现没有路径的情况,同时保证道路网络中不存在任何负权回路。 @@ -41,17 +44,19 @@ 1 3 5 ``` +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240509200224.png) + ## 思路 -本题依然是最短路问题,求 从 节点1 到节点n 的最小费用。 但本题不同之处在于 边的权值是有负数的。 +本题依然是单源最短路问题,求 从 节点1 到节点n 的最小费用。 **但本题不同之处在于 边的权值是有负数了**。 从 节点1 到节点n 的最小费用也可以是负数,费用如果是负数 则表示 运输的过程中 政府补贴大于运输成本。 在求单源最短路的方法中,使用dijkstra 的话,则要求图中边的权值都为正数。 -我们在 [kama47.参会dijkstra朴素](./kama47.参会dijkstra朴素.md) 中专门有讲解,为什么有边为负数 使用dijkstra就不行了。 +我们在 [dijkstra朴素版](./0047.参会dijkstra朴素.md) 中专门有讲解:为什么有边为负数 使用dijkstra就不行了。 -本题是经典的带负权值的单源最短路问题,此时就轮到Bellman_ford登场了,接下来我们来详细介绍Bellman_ford 算法 如何解决这类问题。 +**本题是经典的带负权值的单源最短路问题,此时就轮到Bellman_ford登场了**,接下来我们来详细介绍Bellman_ford 算法 如何解决这类问题。 > 该算法是由 R.Bellman 和L.Ford 在20世纪50年代末期发明的算法,故称为Bellman_ford算法。 @@ -67,7 +72,7 @@ 所以大家翻译过来,就是 “放松” 或者 “松弛” 。 -但《算法四》没有具体去讲这个 “放松” 究竟是个啥? 网上的题解也没有讲题解里的 “松弛这条边,松弛所有边”等等 里面的 “松弛” 究竟是什么意思? +但《算法四》没有具体去讲这个 “放松” 究竟是个啥? 网上很多题解也没有讲题解里的 “松弛这条边,松弛所有边”等等 里面的 “松弛” 究竟是什么意思? 这里我给大家举一个例子,每条边有起点、终点和边的权值。例如一条边,节点A 到 节点B 权值为value,如图: @@ -76,13 +81,13 @@ minDist[B] 表示 到达B节点 最小权值,minDist[B] 有哪些状态可以推出来? 状态一: minDist[A] + value 可以推出 minDist[B] -状态二: minDist[B]本身就有权值 (可能是其他边链接的节点B 例如节点C,以至于 dp[B]记录了其他边到dp[B]的权值) +状态二: minDist[B]本身就有权值 (可能是其他边链接的节点B 例如节点C,以至于 minDist[B]记录了其他边到minDist[B]的权值) -那么minDist[B] 应为如何取舍。 +minDist[B] 应为如何取舍。 本题我们要求最小权值,那么 这两个状态我们就取最小的 -``` +```CPP if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value ``` @@ -108,7 +113,7 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value **那么为什么是 n - 1次 松弛呢**? -这里要给大家模拟一遍 Bellman_ford 的算法才行,接下来我们来看看对所有边松弛 n -1 次的操作是什么样的。 +这里要给大家模拟一遍 Bellman_ford 的算法才行,接下来我们来看看对所有边松弛 n - 1 次的操作是什么样的。 我们依然使用**minDist数组来表达 起点到各个节点的最短距离**,例如minDist[3] = 5 表示起点到达节点3 的最小距离为5 @@ -204,19 +209,18 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value 那么无论图是什么样的,边是什么样的顺序,我们对所有边松弛 n-1 次 就一定能得到 起点到达 终点的最短距离。 -其实也同时计算出了,起点 到达 所有节点的最短距离,因为所有节点与起点连接的变数最多也就是 n-1条边。 +其实也同时计算出了,起点 到达 所有节点的最短距离,因为所有节点与起点连接的边数最多也就是 n-1 条边。 截止到这里,Bellman_ford 的核心算法思路,大家就了解的差不多了。 共有两个关键点。 -* “松弛”究竟是个啥 -* 为什么要对所有边松弛 n - 1 次 (n为节点个数) +* “松弛”究竟是个啥? +* 为什么要对所有边松弛 n - 1 次 (n为节点个数) ? 那么Bellman_ford的解题解题过程其实就是对所有边松弛 n-1 次,然后得出得到终点的最短路径。 - ### 代码 理解上面讲解的内容,代码就更容易写了,本题代码如下:(详细注释) @@ -271,7 +275,7 @@ int main() { grid数组是用来存图的,这是题目描述中必须要使用的空间,而不是我们算法所使用的空间。 -我们在讲空间复杂度的时候,一般都是说,我们这个算法的空间复杂度。 +我们在讲空间复杂度的时候,一般都是说,我们这个算法所用的空间复杂度。 ### 拓展 @@ -283,6 +287,7 @@ grid数组是用来存图的,这是题目描述中必须要使用的空间, 那么我们只要松弛 n - 1次 就一定能得到结果,没必要在松弛更多次了。 这里有疑惑的录友,可以加上打印 minDist数组 的日志,尝试一下,看看松弛 n 次会怎么样。 + 你会发现 松弛 大于 n - 1次,minDist数组 就不会变化了。 这里我给出打印日志的代码: @@ -336,9 +341,9 @@ int main() { ``` -通过打日志,大家发现,怎么对所有边进行第二次松弛以后结果就 不再变化了,那根本就不用松弛 n - 1啊? +通过打日志,大家发现,怎么对所有边进行第二次松弛以后结果就 不再变化了,那根本就不用松弛 n - 1 ? -这是本题的样例的特殊性, 松弛 n-1次 是保证对任何图 都能最后求得到终点的最小距离。 +这是本题的样例的特殊性, 松弛 n-1 次 是保证对任何图 都能最后求得到终点的最小距离。 如果还想不明白 我再举一个例子,用以下测试用例再跑一下。 @@ -367,11 +372,11 @@ int main() { 0 1 2 3 4 5 ``` -你会发现到 n-1 次 打印出最后的最短路结果。 +你会发现到 n-1 次 才打印出最后的最短路结果。 -关于上面的讲解,大家已经要多写代码去实验,验证自己的想法。 +关于上面的讲解,大家一定要多写代码去实验,验证自己的想法。 -至于 负权回路 ,我在下一篇会专门讲解这种情况,大家有个印象就好。 +**至于 负权回路 ,我在下一篇会专门讲解这种情况,大家有个印象就好**。 ## 总结 diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 1dd14f5816..3200efb377 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -1,7 +1,7 @@ -# 95. 城市间货物运输 II +# bellman_ford之判断负权回路 -[题目链接](https://kamacoder.com/problempage.php?pid=1153) +[卡码网:95. 城市间货物运输 II](https://kamacoder.com/problempage.php?pid=1153) 【题目描述】 diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index c9d97a9d8e..f7533e024f 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -1,7 +1,7 @@ -# 96. 城市间货物运输 III +# bellman_ford之单源有限最短路 -[题目链接](https://kamacoder.com/problempage.php?pid=1154) +[卡码网:96. 城市间货物运输 III](https://kamacoder.com/problempage.php?pid=1154) 【题目描述】 @@ -560,26 +560,73 @@ int main() { 这又是为什么呢? -可以发现耗时主要是在 第8组数据上: - -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418114511.png) - -其实第八组数据是我特别制作的一个 稠密大图,该图有250个节点和10000条边, 在这种情况下, SPFA 的时间复杂度 是接近与 bellman_ford的。 +对于后台数据,我特别制作的一个稠密大图,该图有250个节点和10000条边, 在这种情况下, SPFA 的时间复杂度 是接近与 bellman_ford的。 但因为 SPFA 节点的进出队列操作,耗时很大,所以相同的时间复杂度的情况下,SPFA 实际上更耗时了。 这一点我在 [0094.城市间货物运输I-SPFA](./0094.城市间货物运输I-SPFA.md) 有分析,感兴趣的录友再回头去看看。 +## 拓展四(能否用dijkstra) + +本题能否使用 dijkstra 算法呢? + +dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非访问过的节点。 + +如果限制最多访问k个节点,那么 dijkstra 未必能在有限次就能到达终点,即使在经过k个节点确实可以到达终点的情况下。 + +这么说大家会感觉有点抽象,我用 [dijkstra朴素版精讲](./0047.参会dijkstra朴素.md) 里的示例在举例说明: (如果没看过我讲的[dijkstra朴素版精讲](./0047.参会dijkstra朴素.md),建议去仔细看一下,否则下面讲解容易看不懂) + + +在以下这个图中,求节点1 到 节点7 最多经过2个节点 的最短路是多少呢? + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508112249.png) + +最短路显然是: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508112416.png) + +最多经过2个节点,也就是3条边相连的路线:节点1 -> 节点2 -> 节点6-> 节点7 + +如果是 dijkstra 求解的话,求解过程是这样的: (下面是dijkstra的模拟过程,我精简了很多,如果看不懂,一定要先看[dijkstra朴素版精讲](./0047.参会dijkstra朴素.md)) + +初始化如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) + +找距离源点最近且没有被访问过的节点,先找节点1 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) + + +距离源点最近且没有被访问过的节点,找节点2: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) + +距离源点最近且没有被访问过的节点,找到节点3: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) + +距离源点最近且没有被访问过的节点,找到节点4: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) + +此时最多经过2个节点的搜索就完毕了,但结果中minDist[7] (即节点7的结果)并没有被更。 + +那么 dijkstra 会告诉我们 节点1 到 节点7 最多经过2个节点的情况下是不可到达的。 + +通过以上模拟过程,大家应该能感受到 dijkstra 贪心的过程,正是因为 贪心,所以 dijkstra 找不到 节点1 -> 节点2 -> 节点6-> 节点7 这条路径。 + ## 总结 本题是单源有限最短路问题,也是 bellman_ford的一个拓展问题,如果理解bellman_ford 其实思路比较容易理解,但有很多细节。 例如 为什么要用 minDist_copy 来记录上一轮 松弛的结果。 这也是本篇我为什么花了这么大篇幅讲解的关键所在。 -接下来,还给大家多了三个拓展: +接下来,还给大家做了四个拓展: * 边的顺序的影响 * 本题的本质 * SPFA的解法 +* 能否用dijkstra -学透了以上三个拓展,相信大家会对bellman_ford有更深入的理解。 +学透了以上四个拓展,相信大家会对bellman_ford有更深入的理解。 diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index b2afc482fd..7e69994961 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -58,7 +58,7 @@ 通过本题,我们来系统讲解一个新的最短路算法-Floyd 算法。 -Floyd 算法对边的权值正负没有要求,都可以处理。 +**Floyd 算法对边的权值正负没有要求,都可以处理**。 Floyd算法核心思想是动态规划。 @@ -76,7 +76,7 @@ Floyd算法核心思想是动态规划。 那么这样我们是不是就找到了,子问题推导求出整体最优方案的递归关系呢。 -而节点1 到 节点9 的最短距离 可以由 节点1 到节点5的最短距离 + 节点5到节点9的最短距离组成, 也可以有 节点1 到节点7的最短距离 + 节点7 到节点9的最短距离的距离组成。 +节点1 到 节点9 的最短距离 可以由 节点1 到节点5的最短距离 + 节点5到节点9的最短距离组成, 也可以有 节点1 到节点7的最短距离 + 节点7 到节点9的最短距离的距离组成。 那么选哪个呢? @@ -100,11 +100,15 @@ Floyd算法核心思想是动态规划。 grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点的最短距离为m。 -可能有录友会想: 节点i 到 节点j 的最短距离为m,这句话可以理解,但 以[1...k]集合为中间节点 理解不辽。 +可能有录友会想,凭什么就这么定义呢? -节点i 到 节点j 的最短路径中 一定是经过很多节点,那么这个集合用[1...k] 来表示。 +节点i 到 节点j 的最短距离为m,这句话可以理解,但 以[1...k]集合为中间节点就理解不辽了。 -k不能单独指某个节点,因为谁说 节点i 到节点j的最短路径中 一定只有一个节点呢,所以k 一定要表示一个集合,即[1...k] ,表示节点1 到 节点k 一共k个节点的集合。 +节点i 到 节点j 的最短路径中 一定是经过很多节点,那么这个集合用[1...k] 来表示。 + +你可以反过来想,节点i 到 节点j 中间一定经过很多节点,那么你能用什么方式来表述中间这么多节点呢? + +所以 这里的k不能单独指某个节点,k 一定要表示一个集合,即[1...k] ,表示节点1 到 节点k 一共k个节点的集合。 2、确定递推公式 @@ -139,18 +143,20 @@ grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点 例如题目中只是输入边(节点2 -> 节点6,权值为3),那么grid[2][6][k] = 3,k需要填什么呢? -把k 填成1,那如何上来就知道 节点2 经过节点1 到达节点6的最短距离是3 呢。 +把k 填成1,那如何上来就知道 节点2 经过节点1 到达节点6的最短距离是多少 呢。 所以 只能 把k 赋值为 0,本题 节点0 是无意义的,节点是从1 到 n。 这样我们在下一轮计算的时候,就可以根据 grid[i][j][0] 来计算 grid[i][j][1],此时的 grid[i][j][1] 就是 节点i 经过节点1 到达 节点j 的最小距离了。 +grid数组是一个三维数组,那么我们初始化的数据在 i 与 j 构成的平层,如图: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240425104247.png) -**初始化这里要画图,对后面的遍历顺序理解很重要** +红色的 底部一层是我们初始化好的数据,注意:从三维角度去看初始化的数据很重要,下面我们在聊遍历顺序的时候还会再讲。 -所以初始化: +所以初始化代码: ```CPP vector>> grid(n + 1, vector>(n + 1, vector(n + 1, 10005))); // C++定义了一个三位数组,10005是因为边的最大距离是10^4 @@ -167,7 +173,7 @@ grid数组中其他元素数值应该初始化多少呢? 本题求的是最小值,所以输入数据没有涉及到的节点的情况都应该初始为一个最大数。 -这样才不会影响,每次计算去最小值的时候,初始值对计算结果的影响。 +这样才不会影响,每次计算去最小值的时候 初始值对计算结果的影响。 所以grid数组的定义可以是: @@ -191,7 +197,7 @@ vector>> grid(n + 1, vector>(n + 1, vector(n 遍历的顺序是从底向上 一层一层去遍历。 -所以遍历k 的for循环一定是在最外面,这样才能 水平方向一层一层去遍历。如图: +所以遍历k 的for循环一定是在最外面,这样才能一层一层去遍历。如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120109.png) @@ -228,7 +234,7 @@ for (int i = 1; i <= n; i++) { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) -而我们初始化,是 k 为0,然后 i 和 j 形成的平面做初始化,如果以 k 和 j 形成的平面去遍历,就造成了 递推公式 用不上上一轮计算的结果,从而导致结果不对(初始化的结果只能用上一部分,因为初始化是 i 与j 形成的平面)。 +而我们初始化的数据 是 k 为0, i 和 j 形成的平面做初始化,如果以 k 和 j 形成的平面去一层一层遍历,就造成了 递推公式 用不上上一轮计算的结果,从而导致结果不对(初始化的部分是 i 与j 形成的平面,在初始部分有讲过)。 我再给大家举一个测试用例 @@ -246,23 +252,54 @@ for (int i = 1; i <= n; i++) { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120942.png) -就节点1 到 节点 2 的最短距离,运行结果是 10 ,但正确的结果很明显是3。 +求节点1 到 节点 2 的最短距离,运行结果是 10 ,但正确的结果很明显是3。 为什么呢? -因为 k 放在最里面,先就把 节点1 和 节点 2 的最短距离就确定了,后面再也不会计算节点 1 和 节点 2的距离,同时也不会基于 初始化或者之前计算过的结果来计算,即不会考虑 节点1 到 节点3, 节点3 到节点 4,节点4到节点2 的距离。 +因为 k 放在最里面,先就把 节点1 和 节点 2 的最短距离就确定了,后面再也不会计算节点 1 和 节点 2的距离,同时也不会基于 初始化或者之前计算过的结果来计算,即:不会考虑 节点1 到 节点3, 节点3 到节点 4,节点4到节点2 的距离。 + + +造成这一原因,是 在三维立体坐标中, 我们初始化的是 i 和 i 在k 为0 所构成的平面,但遍历的时候 是以 j 和 k构成的平面以 i 为垂直方向去层次遍历。 而遍历k 的for循环如果放在中间呢,同样是 j 与k 行程一个平面,i 是纵面,遍历的也是这样: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) - 同样不能完全用上初始化 和 上一层计算的结果。 -很多录友对于 floyd算法的遍历顺序搞不懂,其实 是没有从三维的角度去思考,同时我把三维立体图给大家画出来,遍历顺序标出来,大家就很容易想明白,为什么 k 放在最外层 才能用上 初始化和上一轮计算的结果了。 +根据这个情况再举一个例子: + +``` +5 2 +1 2 1 +2 3 10 +1 +1 3 +``` + +图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240425112636.png) +求 节点1 到节点3 的最短距离,如果k循环放中间,程序的运行结果是 -1,也就是不能到达节点3。 +在计算 grid[i][j][k] 的时候,需要基于 grid[i][k][k-1] 和 grid[k][j][k-1]的数值。 + +也就是 计算 grid[1][3][2] (表示节点1 到 节点3,经过节点2) 的时候,需要基于 grid[1][2][1] 和 grid[2][3][1]的数值,而 我们初始化,只初始化了 k为0 的那一层。 + +造成这一原因 依然是 在三维立体坐标中, 我们初始化的是 i 和 j 在k 为0 所构成的平面,但遍历的时候 是以 j 和 k构成的平面以 i 为垂直方向去层次遍历。 + + +很多录友对于 floyd算法的遍历顺序搞不懂,**其实 是没有从三维的角度去思考**,同时我把三维立体图给大家画出来,遍历顺序标出来,大家就很容易想明白,为什么 k 放在最外层 才能用上 初始化和上一轮计算的结果了。 + +5、举例推导dp数组 + +这里涉及到 三维矩阵,可以一层一层打印出来去分析,例如k=0 的这一层,k = 1的这一层,但一起把三维带数据的图画出来其实不太好画。 + +## 代码如下 + +以上分析完毕,最后代码如下: ```CPP @@ -300,42 +337,43 @@ int main() { } } -``` - +``` +## 空间优化 -# 拓展 负权回路 +这里 我们可以做一下 空间上的优化,从滚动数组的角度来看,我们定义一个 grid[n + 1][ n + 1][2] 这么大的数组就可以,因为k 只是依赖于 k-1的状态,并不需要记录k-2,k-3,k-4 等等这些状态。 -本题可以有负数,但不能出现负权回路 +那么我们只需要记录 grid[i][j][1] 和 grid[i][j][0] 就好,之后就是 grid[i][j][1] 和 grid[i][j][0] 交替滚动。 ---------- +在进一步想,如果本层计算(本层计算即k相同,从三维角度来讲) gird[i][j] 用到了 本层中刚计算好的 grid[i][k] 会有什么问题吗? -floyd n^3 +如果 本层刚计算好的 grid[i][k] 比上一层 (即k-1层)计算的 grid[i][k] 小,说明确实有 i 到 k 的更短路径,那么基于 更小的 grid[i][k] 去计算 gird[i][j] 没有问题。 -同样多源汇最短路算法 Floyd 也是基于动态规划 +如果 本层刚计算好的 grid[i][k] 比上一层 (即k-1层)计算的 grid[i][k] 大, 这不可能,因为这样也不会做更新 grid[i][k]的操作。 -Floyd 算法可以用来解决多源最短路径问题,它会计算图中每两个点之间的最短路径。 +所以本层计算中,使用了本层计算过的 grid[i][k] 和 grid[k][j] 是没问题的。 - Floyd 算法对边权的正负没有限制要求(可处理正负权边的图),且能利用 Floyd 算法可能够对图中负环进行判定 +那么就没必要区分,grid[i][k] 和 grid[k][j] 是 属于 k - 1 层的呢,还是 k 层的。 -LeetCode-1334. 阈值距离内邻居最少的城市 +所以递归公式可以为: -https://leetcode.cn/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/description/ +```CPP +grid[i][j] = min(grid[i][j], grid[i][k] + grid[k][j]); +``` ------------ +基于二维数组的本题代码为: -```CPP +```CPP #include #include -#include using namespace std; int main() { int n, m, p1, p2, val; cin >> n >> m; - vector> grid(n, vector(n, 10005)); // 因为边的最大距离是10^4 + vector> grid(n + 1, vector(n + 1, 10005)); // 因为边的最大距离是10^4 for(int i = 0; i < m; i++){ cin >> p1 >> p2 >> val; @@ -344,10 +382,10 @@ int main() { } // 开始 floyd - for (int p = 0; p < n; p++) { - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - grid[i][j] = min(grid[i][j], grid[i][p] + grid[p][j]); + for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + grid[i][j] = min(grid[i][j], grid[i][k] + grid[k][j]); } } } @@ -355,13 +393,31 @@ int main() { int z, start, end; cin >> z; while (z--) { - cin >> start >> end; + cin >> start >> end; if (grid[start][end] == 10005) cout << -1 << endl; else cout << grid[start][end] << endl; } } - ``` +* 时间复杂度: O(n^3) +* 空间复杂度:O(n^2) + +## 总结 + +本期如果上来只用二维数组来讲的话,其实更容易,但遍历顺序那里用二维数组其实是讲不清楚的,所以我直接用三维数组来讲,目的是将遍历顺序这里讲清楚。 + +理解了遍历顺序才是floyd算法最精髓的地方。 + + +floyd算法的时间复杂度相对较高,适合 稠密图且源点较多的情况。 + +如果是稀疏图,floyd是从节点的角度去计算了,例如 图中节点数量是 1000,就一条边,那 floyd的时间复杂度依然是 O(n^3) 。 + +如果 源点少,其实可以 多次dijsktra 求源点到终点。 + + + + diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" new file mode 100644 index 0000000000..0aa12fb61e --- /dev/null +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -0,0 +1,471 @@ + +# 98. 所有可达路径 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1170) + +【题目描述】 + +给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个函数,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。 + +【输入描述】 + +第一行包含两个整数 N,M,表示图中拥有 N 个节点,M 条边 + +后续 M 行,每行包含两个整数 s 和 t,表示图中的 s 节点与 t 节点中有一条路径 + +【输出描述】 + +输出所有的可达路径,路径中所有节点的后面跟一个空格,每条路径独占一行,存在多条路径,路径输出的顺序可任意。 + +如果不存在任何一条路径,则输出 -1。 + +【输入示例】 + +``` +5 5 +1 3 +3 5 +1 2 +2 4 +4 5 +``` + +【输出示例】 + +``` +1 3 5 +1 2 4 5 +``` + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240514103953.png) + +用例解释: + +有五个节点,其中的从 1 到达 5 的路径有两个,分别是 1 -> 3 -> 5 和 1 -> 2 -> 4 -> 5。 + +因为拥有多条路径,所以输出结果为: + +``` +1 3 5 +1 2 4 5 +``` + +或 + +``` +1 2 4 5 +1 3 5 +``` + +都算正确。 + +数据范围: + +* 图中不存在自环 +* 图中不存在平行边 +* 1 <= N <= 100 +* 1 <= M <= 500 + + +## 插曲 + +这道题目是深度优先搜索,比较好的入门题。 + +如果对深度优先搜索还不够了解,可以先看这里:[深度优先搜索的理论基础](https://programmercarl.com/图论深搜理论基础.html) + +我依然总结了深搜三部曲,如果按照代码随想录刷题的录友,应该刷过 二叉树的递归三部曲,回溯三部曲。 + +**大家可能有疑惑,深搜 和 二叉树和回溯算法 有什么区别呢**? 什么时候用深搜 什么时候用回溯? + +我在讲解[二叉树理论基础](https://programmercarl.com/二叉树理论基础.html)的时候,提到过,**二叉树的前中后序遍历其实就是深搜在二叉树这种数据结构上的应用**。 + +那么回溯算法呢,**其实 回溯算法就是 深搜,只不过 我们给他一个更细分的定义,叫做回溯算法**。 + +那有的录友可能说:那我以后称回溯算法为深搜,是不是没毛病? + +理论上来说,没毛病,但 就像是 二叉树 你不叫它二叉树,叫它数据结构,有问题不? 也没问题对吧。 + +建议是 有细分的场景,还是称其细分场景的名称。 所以回溯算法可以独立出来,但回溯确实就是深搜。 + +## 图的存储 + +在[图论理论基础篇]() + + + +## 深度优先搜索 + +接下来我们使用深搜三部曲来分析题目: + +1. 确认递归函数,参数 + +首先我们dfs函数一定要存一个图,用来遍历的,还要存一个目前我们遍历的节点,定义为x + +至于 单一路径,和路径集合可以放在全局变量,那么代码是这样的: + +```CPP +vector> result; // 收集符合条件的路径 +vector path; // 0节点到终点的路径 +// x:目前遍历的节点 +// graph:存当前的图 +void dfs (vector>& graph, int x) +``` + +2. 确认终止条件 + +什么时候我们就找到一条路径了? + +当目前遍历的节点 为 最后一个节点的时候 就找到了一条 从出发点到终止点的路径。 + +----------- +当前遍历的节点,我们定义为x,最后一点节点 就是 graph.size() - 1(因为题目描述是找出所有从节点 0 到节点 n-1 的路径并输出)。 + +所以 但 x 等于 graph.size() - 1 的时候就找到一条有效路径。 代码如下: + +------- + +```CPP +// 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1 +if (x == graph.size() - 1) { // 找到符合条件的一条路径 + result.push_back(path); // 收集有效路径 + return; +} +``` + +3. 处理目前搜索节点出发的路径 + +接下来是走 当前遍历节点x的下一个节点。 + +首先是要找到 x节点链接了哪些节点呢? 遍历方式是这样的: + +```c++ +for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 +``` + +接下来就是将 选中的x所连接的节点,加入到 单一路径来。 + +```C++ +path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 + +``` + +一些录友可以疑惑这里如果找到x 链接的节点的,例如如果x目前是节点0,那么目前的过程就是这样的: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221204111937.png) + +二维数组中,graph[x][i] 都是x链接的节点,当前遍历的节点就是 `graph[x][i]` 。 + +进入下一层递归 + +```CPP +dfs(graph, graph[x][i]); // 进入下一层递归 +``` + +最后就是回溯的过程,撤销本次添加节点的操作。 该过程整体代码: + +```CPP +for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 + path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 + dfs(graph, graph[x][i]); // 进入下一层递归 + path.pop_back(); // 回溯,撤销本节点 +} +``` + +本题整体代码如下: + +```CPP +class Solution { +private: + vector> result; // 收集符合条件的路径 + vector path; // 0节点到终点的路径 + // x:目前遍历的节点 + // graph:存当前的图 + void dfs (vector>& graph, int x) { + // 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1 + if (x == graph.size() - 1) { // 找到符合条件的一条路径 + result.push_back(path); + return; + } + for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 + path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 + dfs(graph, graph[x][i]); // 进入下一层递归 + path.pop_back(); // 回溯,撤销本节点 + } + } +public: + vector> allPathsSourceTarget(vector>& graph) { + path.push_back(0); // 无论什么路径已经是从0节点出发 + dfs(graph, 0); // 开始遍历 + return result; + } +}; + +``` + +## 总结 + +本题是比较基础的深度优先搜索模板题,这种有向图路径问题,最合适使用深搜,当然本题也可以使用广搜,但广搜相对来说就麻烦了一些,需要记录一下路径。 + +而深搜和广搜都适合解决颜色类的问题,例如岛屿系列,其实都是 遍历+标记,所以使用哪种遍历都是可以的。 + +至于广搜理论基础,我们在下一篇在好好讲解,敬请期待! + +## 其他语言版本 + +### Java + +```Java +// 深度优先遍历 +class Solution { + List> ans; // 用来存放满足条件的路径 + List cnt; // 用来保存 dfs 过程中的节点值 + + public void dfs(int[][] graph, int node) { + if (node == graph.length - 1) { // 如果当前节点是 n - 1,那么就保存这条路径 + ans.add(new ArrayList<>(cnt)); + return; + } + for (int index = 0; index < graph[node].length; index++) { + int nextNode = graph[node][index]; + cnt.add(nextNode); + dfs(graph, nextNode); + cnt.remove(cnt.size() - 1); // 回溯 + } + } + + public List> allPathsSourceTarget(int[][] graph) { + ans = new ArrayList<>(); + cnt = new ArrayList<>(); + cnt.add(0); // 注意,0 号节点要加入 cnt 数组中 + dfs(graph, 0); + return ans; + } +} +``` + +### Python + +```python +class Solution: + def __init__(self): + self.result = [] + self.path = [0] + + def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]: + if not graph: return [] + + self.dfs(graph, 0) + return self.result + + def dfs(self, graph, root: int): + if root == len(graph) - 1: # 成功找到一条路径时 + # ***Python的list是mutable类型*** + # ***回溯中必须使用Deep Copy*** + self.result.append(self.path[:]) + return + + for node in graph[root]: # 遍历节点n的所有后序节点 + self.path.append(node) + self.dfs(graph, node) + self.path.pop() # 回溯 +``` + + +### JavaScript +```javascript +var allPathsSourceTarget = function(graph) { + let res=[],path=[] + + function dfs(graph,start){ + if(start===graph.length-1){ + res.push([...path]) + return; + } + for(let i=0;i>) -> Vec> { + let (mut res, mut path) = (vec![], vec![0]); + Self::dfs(&graph, &mut path, &mut res, 0); + res + } + + pub fn dfs(graph: &Vec>, path: &mut Vec, res: &mut Vec>, node: usize) { + if node == graph.len() - 1 { + res.push(path.clone()); + return; + } + for &v in &graph[node] { + path.push(v); + Self::dfs(graph, path, res, v as usize); + path.pop(); + } + } +} +``` + +

+ + + + + +邻接矩阵 + +```CPP +#include +#include +using namespace std; +vector> result; // 收集符合条件的路径 +vector path; // 1节点到终点的路径 + +void dfs (const vector>& graph, int x, int n) { + + // 要求从节点 1 到节点 n 的路径并输出,所以是 graph.size() + if (x == n) { // 找到符合条件的一条路径 + result.push_back(path); + return; + } + + for (int i = 1; i <= n; i++) { // 遍历节点x链接的所有节点 + if (graph[x][i] == 1) { // 找到 x链接的节点 + path.push_back(i); // 遍历到的节点加入到路径中来 + dfs(graph, i, n); // 进入下一层递归 + path.pop_back(); // 回溯,撤销本节点 + } + } +} + +int main() { + + + int n, m, s, t; + + cin >> n >> m; + + // 节点编号从1到n,所以申请 n+1 这么大的数组 + vector> graph(n + 1, vector(n + 1, 0)); + + while (m--) { + cin >> s >> t; + // 使用临近矩阵 表示无线图,1 表示 s 与 t 是相连的 + graph[s][t] = 1; + } + path.push_back(1); // 无论什么路径已经是从0节点出发 + + dfs(graph, 1, n); // 开始遍历 + + // 输出结果 + if (result.size() == 0) cout << -1 << endl; + for (const vector &pa : result) { + for (int i = 0; i < pa.size(); i++) { + cout << pa[i] << " "; + } + cout << endl; + } +} +``` + + + +邻接表 + +```CPP +#include +#include +#include +using namespace std; + +vector> result; // 收集符合条件的路径 +vector path; // 1节点到终点的路径 + +void dfs (const vector>& graph, int x, int n) { + + // 要求从节点 1 到节点 n 的路径并输出,所以是 graph.size() + if (x == n) { // 找到符合条件的一条路径 + result.push_back(path); + return; + } + for (int i : graph[x]) { // 找到 x链接的节点 + path.push_back(i); // 遍历到的节点加入到路径中来 + dfs(graph, i, n); // 进入下一层递归 + path.pop_back(); // 回溯,撤销本节点 + } +} + +int main() { + + + int n, m, s, t; + + cin >> n >> m; + + // 节点编号从1到n,所以申请 n+1 这么大的数组 + vector> graph(n + 1); // 邻接表 + while (m--) { + cin >> s >> t; + // 使用邻接表 ,表示 s -> t 是相连的 + graph[s].push_back(t); + + } + + path.push_back(1); // 无论什么路径已经是从0节点出发 + + dfs(graph, 1, n); // 开始遍历 + + //输出结果 + if (result.size() == 0) cout << -1 << endl; + for (const vector &pa : result) { + for (int i = 0; i < pa.size(); i++) { + cout << pa[i] << " "; + } + cout << endl; + } +} + +``` diff --git "a/problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" similarity index 100% rename from "problems/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" rename to "problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" diff --git "a/problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" similarity index 100% rename from "problems/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" rename to "problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" new file mode 100644 index 0000000000..bee1001dc8 --- /dev/null +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" @@ -0,0 +1,31 @@ + +# 图论总结篇 + +从深搜广搜 到并查集,从最小生成树到拓扑排序, 最后是最短路算法系列。 + +至此算上本篇,一共30篇文章,图论之旅就在此收官了。 + + +## 深搜与广搜 + +深搜与广搜是图论里基本的搜索方法,大家需要掌握三点: + +* 搜索方式:深搜是可一个方向搜,不到黄河不回头。 广搜是围绕这起点一圈一圈的去搜。 +* 代码模板:需要熟练掌握深搜和广搜的基本写法。 +* 应用场景:图论题目基本上可以即用深搜也可以广搜,无疑是用哪个方便而已 + +深搜注意事项 + +广搜注意事项 + +## 并查集 + +## 最小生成树 + +## 拓扑排序 + +## 最短路算法 + + + +算法4,只讲解了 Dijkstra,SPFA (Bellman-Ford算法基于队列) 和 拓扑排序, diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" similarity index 100% rename from "problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" rename to "problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" diff --git "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" new file mode 100644 index 0000000000..be42a5fc7c --- /dev/null +++ "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -0,0 +1,222 @@ + +# 图论理论基础 + +这一篇我们正式开始图论! + +## 图的基本概念 + +二维坐标中,两点可以连成线,多个点连成的线就构成了图。 + +当然图也可以就一个节点,甚至没有节点(空图) + +### 图的种类 + +整体上一般分为 有向图 和 无向图。 + +有向图是指 图中边是有方向的: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195737.png) + +无向图是指 图中边没有方向: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195451.png) + +加权有向图,就是图中边是有权值的,例如: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195821.png) + +加权无向图也是同理。 + +### 度 + +无向图中有几条边连接该节点,该节点就有几度。 + +例如,该无向图中,节点4的度为5,节点6的度为3。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511115029.png) + +在有向图中,每个节点有出度和入度。 + +出度:从该节点出发的边的个数。 + +入度:指向该节点边的个数。 + +例如,该有向图中,节点3的入度为2,出度为1,节点1的入度为0,出度为2。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511115235.png) + + +## 连通性 + +在图中表示节点的连通情况,我们称之为连通性。 + +### 连通图 + +在无向图中,任何两个节点都是可以到达的,我们称之为连通图 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511102351.png) + +如果有节点不能到达其他节点,则为非连通图,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511102449.png) + +节点1 不能到达节点4。 + +### 强连通图 + +在有向图中,任何两个节点是可以相互到达的,我们称之为 强连通图。 + +这里有录友可能想,这和无向图中的连通图有什么区别,不是一样的吗? + +我们来看这个有向图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511104531.png) + +这个图是强连通图吗? + +初步一看,好像这节点都连着呢,但这不是强连通图,节点1 可以到节点5,但节点5 不能到 节点1 。 + +强连通图是在有向图中**任何两个节点是可以相互到达** + +下面这个有向图才是强连通图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511113101.png) + + +### 连通分量 + +在无向图中的极大连通子图称之为该图的一个连通分量。 + +只看概念大家可能不理解,我来画个图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511111559.png) + +该无向图中 节点1、节点2、节点5 构成的子图就是 该无向图中的一个连通分量,该子图所有节点都是相互可达到的。 + +同理,节点3、节点4、节点6 构成的子图 也是该无向图中的一个连通分量。 + +那么无向图中 节点3 、节点4 构成的子图 是该无向图的联通分量吗? + +不是! + +因为必须是极大联通子图才能是连通分量,所以 必须是节点3、节点4、节点6 构成的子图才是连通分量。 + +在图论中,连通分量是一个很重要的概念,例如岛屿问题(后面章节会有专门讲解)其实就是求连通分量。 + +### 强连通分量 + +在有向图中极大强连通子图称之为该图的强连通分量。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511112951.png) + +节点1、节点2、节点3、节点4、节点5 构成的子图是强连通分量,因为这是强连通图,也是极大图。 + +节点6、节点7、节点8 构成的子图 不是强连通分量,因为这不是强连通图,节点8 不能达到节点6。 + +节点1、节点2、节点5 构成的子图 也不是 强连通分量,因为这不是极大图。 + + +## 图的构造 + +我们如何用代码来表示一个图呢? + +一般使用邻接表、邻接矩阵 或者用类来表示。 + +主流是 邻接表和邻接矩阵。 + +### 邻接矩阵 + +邻接矩阵 使用 二维数组来表示图结构。 邻接矩阵是从节点的角度来表示图,有多少节点就申请多大的二维数组。 + +例如: grid[2][5] = 6,表示 节点 2 连接 节点5 为有向图,节点2 指向 节点5,边的权值为6。 + +如果想表示无向图,即:grid[2][5] = 6,grid[5][2] = 6,表示节点2 与 节点5 相互连通,权值为6。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) + +在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间。 + +图中有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 + +这种表达方式(邻接矩阵) 在 边少,节点多的情况下,会导致申请过大的二维数组,造成空间浪费。 + +而且在寻找节点连接情况的时候,需要遍历整个矩阵,即 n * n 的时间复杂度,同样造成时间浪费。 + +邻接矩阵的优点: + +* 表达方式简单,易于理解 +* 检查任意两个顶点间是否存在边的操作非常快 +* 适合稠密图,在边数接近顶点数平方的图中,邻接矩阵是一种空间效率较高的表示方法。 + +缺点: + +* 遇到稀疏图,会导致申请过大的二维数组造成空间浪费 且遍历 边 的时候需要遍历整个n * n矩阵,造成时间浪费 + +### 邻接表 + +邻接表 使用 数组 + 链表的方式来表示。 邻接表是从边的数量来表示图,有多少边 才会申请对应大小的链表。 + +邻接表的构造如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) + +这里表达的图是: + +* 节点1 指向 节点3 和 节点5 +* 节点2 指向 节点4、节点3、节点5 +* 节点3 指向 节点4 +* 节点4指向节点1 + +有多少边 邻接表才会申请多少个对应的链表节点。 + +从图中可以直观看出 使用 数组 + 链表 来表达 边的连接情况 。 + +邻接表的优点: + +* 对于稀疏图的存储,只需要存储边,空间利用率高 +* 遍历节点连接情况相对容易 + +缺点: + +* 检查任意两个节点间是否存在边,效率相对低,需要 O(V)时间,V表示某节点连接其他节点的数量。 +* 实现相对复杂,不易理解 + + +**以上大家可能理解比较模糊,没关系**,因为大家还没做过图论的题目,对于图的表达没有概念。 + +这里我先不给出具体的实现代码,大家先有个初步印象,在后面算法题实战中,我还会讲到具体代码实现,等带大家做算法题,写了代码之后,自然就理解了。 + +## 图的遍历方式 + +图的遍历方式基本是两大类: + +* 深度优先搜索(dfs) +* 广度优先搜索(bfs) + +在讲解二叉树章节的时候,其实就已经讲过这两种遍历方式。 + +二叉树的递归遍历,是dfs 在二叉树上的遍历方式。 + +二叉树的层序遍历,是bfs 在二叉树上的遍历方式。 + +dfs 和 bfs 一种搜索算法,可以在不同的数据结构上进行搜索,在二叉树章节里是在二叉树这样的数据结构上搜索。 + +而在图论章节,则是在图(邻接表或邻接矩阵)上进行搜索。 + +## 总结 + +以上知识点 大家先有个印象,上面提到的每个知识点,其实都需要大篇幅才能讲明白的。 + +我这里先给大家做一个概括,后面章节会针对每个知识点都会有对应的算法题和针对性的讲解,大家再去深入学习。 + +图论是非常庞大的知识体系,上面的内容还不足以概括图论内容,仅仅是理论基础而已。 + +在图论章节我会带大家深入讲解 深度优先搜索(DFS)、广度优先搜索(BFS)、并查集、拓扑排序、最小生成树系列、最短路算法系列等等。 + +敬请期待! + + diff --git "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" new file mode 100644 index 0000000000..7f4ee6f87d --- /dev/null +++ "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -0,0 +1,48 @@ + +# 最短路算法总结篇 + +至此已经讲解了四大最短路算法,分别是Dijkstra、Bellman_ford、SPFA 和 Floyd。 + +针对这四大最短路算法,我用了七篇长文才彻底讲清楚,分别是: + +* dijkstra朴素版 +* dijkstra堆优化版 +* Bellman_ford +* Bellman_ford 队列优化算法(又名SPFA) +* bellman_ford 算法判断负权回路 +* bellman_ford之单源有限最短路 +* Floyd 算法精讲 + + +最短路算法比较复杂,而且各自有各自的应用场景,我来用一张表把讲过的最短路算法的使用场景都展现出来: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508121355.png) + + +可能有同学感觉:这个表太复杂了,我记也记不住。 + +其实记不住的原因还是对 这几个最短路算法没有深刻的理解。 + +这里我给大家一个大体使用场景的分析: + +如果遇到单源且边为正数,直接Dijkstra。 + +至于 使用朴素版还是 堆优化版 还是取决于图的稠密度, 多少节点多少边算是稠密图,多少算是稀疏图,这个没有量化,如果想量化只能写出两个版本然后做实验去测试,不同的判题机得出的结果还不太一样。 + +一般情况下,可以直接用堆优化版本。 + +如果遇到单源边可为负数,直接 Bellman-Ford,同样 SPFA 还是 Bellman-Ford 取决于图的稠密度。 + +一般情况下,直接用 SPFA。 + +如果有负权回路,优先 Bellman-Ford, 如果是有限节点最短路 也优先 Bellman-Ford,理由是写代码比较方便。 + +如果是遇到多源点求最短路,直接 Floyd。 + +除非 源点特别少,且边都是正数,那可以 多次 Dijkstra 求出最短路径,但这种情况很少,一般出现多个源点了,就是想让你用 Floyd 了。 + + + + + + diff --git "a/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" similarity index 100% rename from "problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" rename to "problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" diff --git "a/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" similarity index 100% rename from "problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" rename to "problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" diff --git "a/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214.md" similarity index 100% rename from "problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" rename to "problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214.md" diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" similarity index 100% rename from "problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" rename to "problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" "b/problems/\345\211\215\345\272\217/\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246.md" similarity index 100% rename from "problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" rename to "problems/\345\211\215\345\272\217/\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246.md" diff --git "a/problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" similarity index 100% rename from "problems/\345\211\215\345\272\217/On\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" rename to "problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" diff --git "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" similarity index 100% rename from "problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" rename to "problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" From 7ab1e2df30503803cb97a14a4a1b37ac781d6cfa Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 17 May 2024 10:18:25 +0800 Subject: [PATCH 1132/1533] Update --- ...57\350\276\276\350\267\257\345\276\204.md" | 369 ++++++++---------- ...60\351\207\217\345\271\277\346\220\234.md" | 186 +++++++++ ...60\351\207\217\346\267\261\346\220\234.md" | 179 +++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 11 - ...06\350\256\272\345\237\272\347\241\200.md" | 11 - ...72\346\200\273\347\273\223\347\257\207.md" | 2 + ...06\350\256\272\345\237\272\347\241\200.md" | 9 - 7 files changed, 520 insertions(+), 247 deletions(-) create mode 100644 "problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" create mode 100644 "problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 0aa12fb61e..0573d0817e 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -3,6 +3,8 @@ [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1170) +[力扣题目讲解以及题目链接(核心代码模式)](https://programmercarl.com/0797.%E6%89%80%E6%9C%89%E5%8F%AF%E8%83%BD%E7%9A%84%E8%B7%AF%E5%BE%84.html#%E6%80%9D%E8%B7%AF) + 【题目描述】 给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个函数,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。 @@ -19,6 +21,8 @@ 如果不存在任何一条路径,则输出 -1。 +注意输出的序列中,最后一个节点后面没有空格! 例如正确的答案是 `1 3 5`,而不是 `1 3 5 `, 5后面没有空格! + 【输入示例】 ``` @@ -81,7 +85,7 @@ 我在讲解[二叉树理论基础](https://programmercarl.com/二叉树理论基础.html)的时候,提到过,**二叉树的前中后序遍历其实就是深搜在二叉树这种数据结构上的应用**。 -那么回溯算法呢,**其实 回溯算法就是 深搜,只不过 我们给他一个更细分的定义,叫做回溯算法**。 +那么回溯算法呢,**其实 回溯算法就是 深搜,只不过针对某一搜索场景 我们给他一个更细分的定义,叫做回溯算法**。 那有的录友可能说:那我以后称回溯算法为深搜,是不是没毛病? @@ -91,271 +95,194 @@ ## 图的存储 -在[图论理论基础篇]() +在[图论理论基础篇](./图论理论基础.md) 中我们讲到了 两种 图的存储方式:邻接表 和 邻接矩阵。 +本题我们将带大家分别实现这两个图的存储方式。 +### 邻接矩阵 -## 深度优先搜索 - -接下来我们使用深搜三部曲来分析题目: +邻接矩阵 使用 二维数组来表示图结构。 邻接矩阵是从节点的角度来表示图,有多少节点就申请多大的二维数组。 -1. 确认递归函数,参数 +本题我们会有n 个节点,因为节点标号是从1开始的,为了节点标号和下标对齐,我们申请 n + 1 * n + 1 这么大的二维数组。 -首先我们dfs函数一定要存一个图,用来遍历的,还要存一个目前我们遍历的节点,定义为x +```CPP +vector> graph(n + 1, vector(n + 1, 0)); +``` -至于 单一路径,和路径集合可以放在全局变量,那么代码是这样的: +输入m个边,构造方式如下: ```CPP -vector> result; // 收集符合条件的路径 -vector path; // 0节点到终点的路径 -// x:目前遍历的节点 -// graph:存当前的图 -void dfs (vector>& graph, int x) +while (m--) { + cin >> s >> t; + // 使用邻接矩阵 ,1 表示 节点s 指向 节点t + graph[s][t] = 1; +} ``` -2. 确认终止条件 +### 邻接表 -什么时候我们就找到一条路径了? +邻接表 使用 数组 + 链表的方式来表示。 邻接表是从边的数量来表示图,有多少边 才会申请对应大小的链表。 + +邻接表的构造相对邻接矩阵难理解一些。 -当目前遍历的节点 为 最后一个节点的时候 就找到了一条 从出发点到终止点的路径。 +我在 [图论理论基础篇](./图论理论基础.md) 举了一个例子: ------------ -当前遍历的节点,我们定义为x,最后一点节点 就是 graph.size() - 1(因为题目描述是找出所有从节点 0 到节点 n-1 的路径并输出)。 -所以 但 x 等于 graph.size() - 1 的时候就找到一条有效路径。 代码如下: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) -------- +这里表达的图是: + +* 节点1 指向 节点3 和 节点5 +* 节点2 指向 节点4、节点3、节点5 +* 节点3 指向 节点4 +* 节点4指向节点1 + +我们需要构造一个数组,数组里的元素是一个链表。 + +C++写法: ```CPP -// 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1 -if (x == graph.size() - 1) { // 找到符合条件的一条路径 - result.push_back(path); // 收集有效路径 - return; +// 节点编号从1到n,所以申请 n+1 这么大的数组 +vector> graph(n + 1); // 邻接表,list为C++里的链表 +``` + +输入m个边,构造方式如下: + +```CPP +while (m--) { + cin >> s >> t; + // 使用邻接表 ,表示 s -> t 是相连的 + graph[s].push_back(t); } ``` -3. 处理目前搜索节点出发的路径 +本题我们使用邻接表 或者 邻接矩阵都可以,因为后台数据并没有对图的大小以及稠密度做很大的区分。 -接下来是走 当前遍历节点x的下一个节点。 +以下我们使用邻接矩阵的方式来讲解,文末我也会给出 使用邻接表的整体代码。 -首先是要找到 x节点链接了哪些节点呢? 遍历方式是这样的: +**注意邻接表 和 邻接矩阵的写法都要掌握**! -```c++ -for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 -``` +## 深度优先搜索 -接下来就是将 选中的x所连接的节点,加入到 单一路径来。 +本题是深度优先搜索的基础题目,关于深搜我在[图论深搜理论基础](./图论深搜理论基础.md) 已经有详细的讲解,图文并茂。 -```C++ -path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 +关于本题我会直接使用深搜三部曲来分析,如果对深搜不够了解,建议先看 [图论深搜理论基础](./图论深搜理论基础.md)。 -``` +深搜三部曲来分析题目: -一些录友可以疑惑这里如果找到x 链接的节点的,例如如果x目前是节点0,那么目前的过程就是这样的: +1. 确认递归函数,参数 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221204111937.png) +首先我们dfs函数一定要存一个图,用来遍历的,需要存一个目前我们遍历的节点,定义为x。 -二维数组中,graph[x][i] 都是x链接的节点,当前遍历的节点就是 `graph[x][i]` 。 +还需要存一个n,表示终点,我们遍历的时候,用来判断当 x==n 时候 标明找到了终点。 -进入下一层递归 +(其实在递归函数的参数 不容易一开始就确定了,一般是在写函数体的时候发现缺什么,参加就补什么) + +至于 单一路径 和 路径集合 可以放在全局变量,那么代码是这样的: ```CPP -dfs(graph, graph[x][i]); // 进入下一层递归 +vector> result; // 收集符合条件的路径 +vector path; // 0节点到终点的路径 +// x:目前遍历的节点 +// graph:存当前的图 +// n:终点 +void dfs (const vector>& graph, int x, int n) { ``` -最后就是回溯的过程,撤销本次添加节点的操作。 该过程整体代码: +2. 确认终止条件 + +什么时候我们就找到一条路径了? + +当目前遍历的节点 为 最后一个节点 n 的时候 就找到了一条 从出发点到终止点的路径。 ```CPP -for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 - path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 - dfs(graph, graph[x][i]); // 进入下一层递归 - path.pop_back(); // 回溯,撤销本节点 +// 当前遍历的节点x 到达节点n +if (x == n) { // 找到符合条件的一条路径 + result.push_back(path); + return; } ``` -本题整体代码如下: +3. 处理目前搜索节点出发的路径 -```CPP -class Solution { -private: - vector> result; // 收集符合条件的路径 - vector path; // 0节点到终点的路径 - // x:目前遍历的节点 - // graph:存当前的图 - void dfs (vector>& graph, int x) { - // 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1 - if (x == graph.size() - 1) { // 找到符合条件的一条路径 - result.push_back(path); - return; - } - for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点 - path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 - dfs(graph, graph[x][i]); // 进入下一层递归 - path.pop_back(); // 回溯,撤销本节点 - } - } -public: - vector> allPathsSourceTarget(vector>& graph) { - path.push_back(0); // 无论什么路径已经是从0节点出发 - dfs(graph, 0); // 开始遍历 - return result; - } -}; +接下来是走 当前遍历节点x的下一个节点。 + +首先是要找到 x节点指向了哪些节点呢? 遍历方式是这样的: +```c++ +for (int i = 1; i <= n; i++) { // 遍历节点x链接的所有节点 + if (graph[x][i] == 1) { // 找到 x指向的节点,就是节点i + } +} ``` -## 总结 +接下来就是将 选中的x所指向的节点,加入到 单一路径来。 -本题是比较基础的深度优先搜索模板题,这种有向图路径问题,最合适使用深搜,当然本题也可以使用广搜,但广搜相对来说就麻烦了一些,需要记录一下路径。 +```C++ +path.push_back(i); // 遍历到的节点加入到路径中来 -而深搜和广搜都适合解决颜色类的问题,例如岛屿系列,其实都是 遍历+标记,所以使用哪种遍历都是可以的。 +``` -至于广搜理论基础,我们在下一篇在好好讲解,敬请期待! -## 其他语言版本 +进入下一层递归 + +```CPP +dfs(graph, i, n); // 进入下一层递归 +``` -### Java +最后就是回溯的过程,撤销本次添加节点的操作。 -```Java -// 深度优先遍历 -class Solution { - List> ans; // 用来存放满足条件的路径 - List cnt; // 用来保存 dfs 过程中的节点值 +为什么要有回溯,我在[图论深搜理论基础](./图论深搜理论基础.md) 也有详细的讲解。 - public void dfs(int[][] graph, int node) { - if (node == graph.length - 1) { // 如果当前节点是 n - 1,那么就保存这条路径 - ans.add(new ArrayList<>(cnt)); - return; - } - for (int index = 0; index < graph[node].length; index++) { - int nextNode = graph[node][index]; - cnt.add(nextNode); - dfs(graph, nextNode); - cnt.remove(cnt.size() - 1); // 回溯 - } - } +该过程整体代码: - public List> allPathsSourceTarget(int[][] graph) { - ans = new ArrayList<>(); - cnt = new ArrayList<>(); - cnt.add(0); // 注意,0 号节点要加入 cnt 数组中 - dfs(graph, 0); - return ans; +```CPP +for (int i = 1; i <= n; i++) { // 遍历节点x链接的所有节点 + if (graph[x][i] == 1) { // 找到 x链接的节点 + path.push_back(i); // 遍历到的节点加入到路径中来 + dfs(graph, i, n); // 进入下一层递归 + path.pop_back(); // 回溯,撤销本节点 } } ``` -### Python - -```python -class Solution: - def __init__(self): - self.result = [] - self.path = [0] - - def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]: - if not graph: return [] - - self.dfs(graph, 0) - return self.result - - def dfs(self, graph, root: int): - if root == len(graph) - 1: # 成功找到一条路径时 - # ***Python的list是mutable类型*** - # ***回溯中必须使用Deep Copy*** - self.result.append(self.path[:]) - return - - for node in graph[root]: # 遍历节点n的所有后序节点 - self.path.append(node) - self.dfs(graph, node) - self.path.pop() # 回溯 -``` +## 打印结果 +ACM格式大家在输出结果的时候,要关注看看格式问题,特别是字符串,有的题目说的是每个元素后面都有空格,有的题目说的是 每个元素间有空格,最后一个元素没有空格。 -### JavaScript -```javascript -var allPathsSourceTarget = function(graph) { - let res=[],path=[] +有的题目呢,压根没说,那只能提交去试一试了。 - function dfs(graph,start){ - if(start===graph.length-1){ - res.push([...path]) - return; - } - for(let i=0;i>) -> Vec> { - let (mut res, mut path) = (vec![], vec![0]); - Self::dfs(&graph, &mut path, &mut res, 0); - res - } +以上代码中,结果都存在了 result数组里(二维数组,每一行是一个结果),最后将其打印出来。(重点看注释) - pub fn dfs(graph: &Vec>, path: &mut Vec, res: &mut Vec>, node: usize) { - if node == graph.len() - 1 { - res.push(path.clone()); - return; - } - for &v in &graph[node] { - path.push(v); - Self::dfs(graph, path, res, v as usize); - path.pop(); - } +```CPP +// 输出结果 +if (result.size() == 0) cout << -1 << endl; +for (const vector &pa : result) { + for (int i = 0; i < pa.size() - 1; i++) { // 这里指打印到倒数第二个 + cout << pa[i] << " "; } + cout << pa[pa.size() - 1] << endl; // 这里再打印倒数第一个,控制最后一个元素后面没有空格 } ``` -

- - - +## 本题代码 +### 邻接矩阵写法 -邻接矩阵 ```CPP #include @@ -365,13 +292,11 @@ vector> result; // 收集符合条件的路径 vector path; // 1节点到终点的路径 void dfs (const vector>& graph, int x, int n) { - - // 要求从节点 1 到节点 n 的路径并输出,所以是 graph.size() + // 当前遍历的节点x 到达节点n if (x == n) { // 找到符合条件的一条路径 result.push_back(path); return; } - for (int i = 1; i <= n; i++) { // 遍历节点x链接的所有节点 if (graph[x][i] == 1) { // 找到 x链接的节点 path.push_back(i); // 遍历到的节点加入到路径中来 @@ -382,10 +307,7 @@ void dfs (const vector>& graph, int x, int n) { } int main() { - - int n, m, s, t; - cin >> n >> m; // 节点编号从1到n,所以申请 n+1 这么大的数组 @@ -393,27 +315,26 @@ int main() { while (m--) { cin >> s >> t; - // 使用临近矩阵 表示无线图,1 表示 s 与 t 是相连的 + // 使用邻接矩阵 表示无线图,1 表示 s 与 t 是相连的 graph[s][t] = 1; } - path.push_back(1); // 无论什么路径已经是从0节点出发 + path.push_back(1); // 无论什么路径已经是从0节点出发 dfs(graph, 1, n); // 开始遍历 // 输出结果 if (result.size() == 0) cout << -1 << endl; for (const vector &pa : result) { - for (int i = 0; i < pa.size(); i++) { + for (int i = 0; i < pa.size() - 1; i++) { cout << pa[i] << " "; } - cout << endl; + cout << pa[pa.size() - 1] << endl; } } -``` - +``` -邻接表 +### 邻接表写法 ```CPP #include @@ -426,12 +347,11 @@ vector path; // 1节点到终点的路径 void dfs (const vector>& graph, int x, int n) { - // 要求从节点 1 到节点 n 的路径并输出,所以是 graph.size() if (x == n) { // 找到符合条件的一条路径 result.push_back(path); return; } - for (int i : graph[x]) { // 找到 x链接的节点 + for (int i : graph[x]) { // 找到 x指向的节点 path.push_back(i); // 遍历到的节点加入到路径中来 dfs(graph, i, n); // 进入下一层递归 path.pop_back(); // 回溯,撤销本节点 @@ -439,10 +359,7 @@ void dfs (const vector>& graph, int x, int n) { } int main() { - - int n, m, s, t; - cin >> n >> m; // 节点编号从1到n,所以申请 n+1 这么大的数组 @@ -455,17 +372,37 @@ int main() { } path.push_back(1); // 无论什么路径已经是从0节点出发 - dfs(graph, 1, n); // 开始遍历 - //输出结果 + // 输出结果 if (result.size() == 0) cout << -1 << endl; for (const vector &pa : result) { - for (int i = 0; i < pa.size(); i++) { + for (int i = 0; i < pa.size() - 1; i++) { cout << pa[i] << " "; } - cout << endl; + cout << pa[pa.size() - 1] << endl; } } ``` + +## 总结 + +本题是一道简单的深搜题目,也可以说是模板题,和 [力扣797. 所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/description/) 思路是一样一样的。 + +很多录友做力扣的时候,轻松就把代码写出来了, 但做面试笔试的时候,遇到这样的题就写不出来了。 + +在力扣上刷题不用考虑图的存储方式,也不用考虑输出的格式。 + +而这些都是 ACM 模式题目的知识点(图的存储方式)和细节(输出的格式) + +所以我才会特别制作ACM题目,同样也重点去讲解图的存储和遍历方式,来帮大家去练习。 + +对于这种有向图路径问题,最合适使用深搜,当然本题也可以使用广搜,但广搜相对来说就麻烦了一些,需要记录一下路径。 + +而深搜和广搜都适合解决颜色类的问题,例如岛屿系列,其实都是 遍历+标记,所以使用哪种遍历都是可以的。 + +至于广搜理论基础,我们在下一篇在好好讲解,敬请期待! + + + diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" new file mode 100644 index 0000000000..dfcf062f90 --- /dev/null +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -0,0 +1,186 @@ + +# 99. 岛屿数量 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171) + +[力扣题目讲解以及题目链接(核心代码模式)](https://programmercarl.com/0200.%E5%B2%9B%E5%B1%BF%E6%95%B0%E9%87%8F.%E5%B9%BF%E6%90%9C%E7%89%88.html) + +题目描述: + +给定一个由 1(陆地)和 0(水)组成的矩阵,你需要计算岛屿的数量。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。 + +输入描述: + +第一行包含两个整数 N, M,表示矩阵的行数和列数。 + +后续 N 行,每行包含 M 个数字,数字为 1 或者 0。 + +输出描述: + +输出一个整数,表示岛屿的数量。如果不存在岛屿,则输出 0。 + +输入示例: + +``` +4 5 +1 1 0 0 0 +1 1 0 0 0 +0 0 1 0 0 +0 0 0 1 1 +``` + +输出示例: + +3 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240516111613.png) + +根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 + +数据范围: + +* 1 <= N, M <= 50 + + +## 思路 + +注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 + +也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: + +![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) + +这道题题目是 DFS,BFS,并查集,基础题目。 + +本题思路,是用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。 + +在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。 + +那么如果把节点陆地所能遍历到的陆地都标记上呢,就可以使用 DFS,BFS或者并查集。 + +### 广度优先搜索 + +不少同学用广搜做这道题目的时候,超时了。 这里有一个广搜中很重要的细节: + +根本原因是**只要 加入队列就代表 走过,就需要标记,而不是从队列拿出来的时候再去标记走过**。 + +很多同学可能感觉这有区别吗? + +如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 + +![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png) + +超时写法 (从队列中取出节点再标记) + +```CPP +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void bfs(vector>& grid, vector>& visited, int x, int y) { + queue> que; + que.push({x, y}); + while(!que.empty()) { + pair cur = que.front(); que.pop(); + int curx = cur.first; + int cury = cur.second; + visited[curx][cury] = true; // 从队列中取出在标记走过 + for (int i = 0; i < 4; i++) { + int nextx = curx + dir[i][0]; + int nexty = cury + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') { + que.push({nextx, nexty}); + } + } + } + +} +``` + + +加入队列 就代表走过,立刻标记,正确写法: + +```CPP +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void bfs(vector>& grid, vector>& visited, int x, int y) { + queue> que; + que.push({x, y}); + visited[x][y] = true; // 只要加入队列,立刻标记 + while(!que.empty()) { + pair cur = que.front(); que.pop(); + int curx = cur.first; + int cury = cur.second; + for (int i = 0; i < 4; i++) { + int nextx = curx + dir[i][0]; + int nexty = cury + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') { + que.push({nextx, nexty}); + visited[nextx][nexty] = true; // 只要加入队列立刻标记 + } + } + } + +} +``` + +以上两个版本其实,其实只有细微区别,就是 `visited[x][y] = true;` 放在的地方,这取决于我们对 代码中队列的定义,队列中的节点就表示已经走过的节点。 **所以只要加入队列,立即标记该节点走过**。 + +本题完整广搜代码: + +```CPP +#include +#include +#include +using namespace std; + +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void bfs(const vector>& grid, vector>& visited, int x, int y) { + queue> que; + que.push({x, y}); + visited[x][y] = true; // 只要加入队列,立刻标记 + while(!que.empty()) { + pair cur = que.front(); que.pop(); + int curx = cur.first; + int cury = cur.second; + for (int i = 0; i < 4; i++) { + int nextx = curx + dir[i][0]; + int nexty = cury + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { + que.push({nextx, nexty}); + visited[nextx][nexty] = true; // 只要加入队列立刻标记 + } + } + } +} + + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + + vector> visited(n, vector(m, false)); + + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + result++; // 遇到没访问过的陆地,+1 + bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + } + } + } + + + cout << result << endl; +} + +``` + diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" new file mode 100644 index 0000000000..310676105f --- /dev/null +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -0,0 +1,179 @@ + +# 99. 岛屿数量 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171) + +[力扣题目讲解以及题目链接(核心代码模式)](https://programmercarl.com/0200.%E5%B2%9B%E5%B1%BF%E6%95%B0%E9%87%8F.%E6%B7%B1%E6%90%9C%E7%89%88.html) + +题目描述: + +给定一个由 1(陆地)和 0(水)组成的矩阵,你需要计算岛屿的数量。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。 + +输入描述: + +第一行包含两个整数 N, M,表示矩阵的行数和列数。 + +后续 N 行,每行包含 M 个数字,数字为 1 或者 0。 + +输出描述: + +输出一个整数,表示岛屿的数量。如果不存在岛屿,则输出 0。 + +输入示例: + +``` +4 5 +1 1 0 0 0 +1 1 0 0 0 +0 0 1 0 0 +0 0 0 1 1 +``` + +输出示例: + +3 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240516111613.png) + +根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 + +数据范围: + +* 1 <= N, M <= 50 + +## 思路 + +注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 + +也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: + +![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) + +这道题题目是 DFS,BFS,并查集,基础题目。 + +本题思路,是用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。 + +在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。 + +那么如何把节点陆地所能遍历到的陆地都标记上呢,就可以使用 DFS,BFS或者并查集。 + +### 深度优先搜索 + +以下代码使用dfs实现,如果对dfs不太了解的话,**建议按照代码随想录的讲解顺序学习**。 + +C++代码如下: + +```CPP +// 版本一 +#include +#include +using namespace std; + +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void dfs(const vector>& grid, vector>& visited, int x, int y) { + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 没有访问过的 同时 是陆地的 + + visited[nextx][nexty] = true; + dfs(grid, visited, nextx, nexty); + } + } +} + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + + vector> visited(n, vector(m, false)); + + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + visited[i][j] = true; + result++; // 遇到没访问过的陆地,+1 + dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + } + } + } + + cout << result << endl; +} +``` + +很多录友可能有疑惑,为什么 以上代码中的dfs函数,没有终止条件呢? 感觉递归没有终止很危险。 + +其实终止条件 就写在了 调用dfs的地方,如果遇到不合法的方向,直接不会去调用dfs。 + +当然也可以这么写: + +```CPP +// 版本二 +#include +#include +using namespace std; +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void dfs(const vector>& grid, vector>& visited, int x, int y) { + if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = true; // 标记访问过 + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + dfs(grid, visited, nextx, nexty); + } +} + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + + vector> visited(n, vector(m, false)); + + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + result++; // 遇到没访问过的陆地,+1 + dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + } + } + } + cout << result << endl; +} +``` + +这里大家应该能看出区别了,无疑就是版本一中 调用dfs 的条件判断 放在了 版本二 的 终止条件位置上。 + +**版本一的写法**是 :下一个节点是否能合法已经判断完了,传进dfs函数的就是合法节点。 + +**版本二的写法**是:不管节点是否合法,上来就dfs,然后在终止条件的地方进行判断,不合法再return。 + +**理论上来讲,版本一的效率更高一些**,因为避免了 没有意义的递归调用,在调用dfs之前,就做合法性判断。 但从写法来说,可能版本二 更利于理解一些。(不过其实都差不太多) + +很多同学看了同一道题目,都是dfs,写法却不一样,**有时候有终止条件,有时候连终止条件都没有,其实这就是根本原因,两种写法而已**。 + + +## 总结 + +其实本题是 dfs,bfs 模板题,但正是因为是模板题,所以大家或者一些题解把重要的细节都很忽略了,我这里把大家没注意的但以后会踩的坑 都给列出来了。 + +本篇我只给出的dfs的写法,大家发现我写的还是比较细的,那么后面我再单独给出本题的bfs写法,虽然是模板题,但依然有很多注意的点,敬请期待! + diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 6a9cc1aca2..3bf45f77f1 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,9 +1,3 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

- # 并查集理论基础 接下来我们来讲一下并查集,首先当然是并查集理论基础。 @@ -460,8 +454,3 @@ void join(int u, int v) { 敬请期待 并查集题目精讲系列。 - -

- - - diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 51a82944ea..c494e0d0e2 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,9 +1,3 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

- # 广度优先搜索理论基础 @@ -156,8 +150,3 @@ def bfs(grid, visited, x, y): ``` - -

- - - diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" index bee1001dc8..92b3581d53 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" @@ -16,6 +16,8 @@ 深搜注意事项 +同样是深搜模板题,会有两种写法, + 广搜注意事项 ## 并查集 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 27464aabe4..2be97057bf 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,3 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

# 深度优先搜索理论基础 @@ -199,7 +194,3 @@ for (选择:本节点所连接的其他节点) { 后面我也会给大家安排具体练习的题目,依旧是代码随想录的风格,循序渐进由浅入深! -

- - - From f5a5d2005e394f0d5de2adf9d88f839796f3267b Mon Sep 17 00:00:00 2001 From: GP Date: Sat, 18 May 2024 13:05:11 +0800 Subject: [PATCH 1133/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E7=89=88=E6=9B=B4=E9=80=9A=E4=BF=97=E6=98=93=E6=87=82=E7=9A=84?= =?UTF-8?q?Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\346\227\213\347\237\251\351\230\265.md" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 4d54ccd6d7..d7cfc7bf27 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -200,6 +200,79 @@ class Solution { } ``` +```java +class Solution { + public List spiralOrder(int[][] matrix) { + List res = new ArrayList<>(); // 存放结果 + if (matrix.length == 0 || matrix[0].length == 0) + return res; + int rows = matrix.length, columns = matrix[0].length; + int startx = 0, starty = 0; // 定义每循环一个圈的起始位置 + int loop = 0; // 循环次数 + int offset = 1; // 每一圈循环,需要控制每一条边遍历的长度 + while (loop < Math.min(rows, columns) / 2) { + int i = startx; + int j = starty; + // 模拟填充上行从左到右(左闭右开) + for (; j < columns - offset; j++) { + res.add(matrix[i][j]); + } + // 模拟填充右列从上到下(左闭右开) + for (; i < rows - offset; i++) { + res.add(matrix[i][j]); + } + // 模拟填充下行从右到左(左闭右开) + for (; j > starty; j--) { + res.add(matrix[i][j]); + } + // 模拟填充左列从下到上(左闭右开) + for (; i > startx; i--) { + res.add(matrix[i][j]); + } + + // 起始位置加1 循环次数加1 并控制每条边遍历的长度 + startx++; + starty++; + offset++; + loop++; + } + + // 如果列或行中的最小值为奇数 则一定有未遍历的部分 + // 可以自行画图理解 + if (Math.min(rows, columns) % 2 == 1) { + // 当行大于列时 未遍历的部分是列 + // (startx, starty)即下一个要遍历位置 从该位置出发 遍历完未遍历的列 + // 遍历次数为rows - columns + 1 + if (rows > columns) { + for (int i = 0; i < rows - columns + 1; i++) { + res.add(matrix[startx++][starty]); + } + } else { + // 此处与上面同理 遍历完未遍历的行 + for (int i = 0; i < columns - rows + 1; i++) { + res.add(matrix[startx][starty++]); + } + } + } + + return res; + } +} +``` + + + + + + + + + + + + + + ### Javascript ``` /** From 00c9165e3ca52cebaa3449cc996a43fdd4184c93 Mon Sep 17 00:00:00 2001 From: GPFE05 <113404586+GPFE05@users.noreply.github.com> Date: Sun, 19 May 2024 11:06:26 +0800 Subject: [PATCH 1134/1533] =?UTF-8?q?Update=200054.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\236\272\346\227\213\347\237\251\351\230\265.md" | 13 ------------- 1 file changed, 13 deletions(-) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index d7cfc7bf27..96d17413c4 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -260,19 +260,6 @@ class Solution { } ``` - - - - - - - - - - - - - ### Javascript ``` /** From 784ed2a68c2eb3a790bda49dc5fbbee797f99fbe Mon Sep 17 00:00:00 2001 From: Henry Zheng <1204831218@qq.com> Date: Mon, 20 May 2024 08:51:59 +0800 Subject: [PATCH 1135/1533] =?UTF-8?q?Update=20#0455=20=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2=20=E6=A0=88=20=E5=A4=A7=E9=A5=BC=E5=B9=B2?= =?UTF-8?q?=E4=BC=98=E5=85=88=20&=20#700=20=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2=20?= =?UTF-8?q?=E6=A0=88-=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\217\221\351\245\274\345\271\262.md" | 20 ++++++++++++++++++- ...55\347\232\204\346\220\234\347\264\242.md" | 20 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 6ae206dba2..b6f5bae54b 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -206,8 +206,26 @@ class Solution: ``` -### Go +栈 大饼干优先 +```python +from collecion import deque +class Solution: + def findContentChildren(self, g: List[int], s: List[int]) -> int: + #思路,饼干和孩子按从大到小排序,依次从栈中取出,若满足条件result += 1 否则将饼干栈顶元素重新返回 + result = 0 + queue_g = deque(sorted(g, reverse = True)) + queue_s = deque(sorted(s, reverse = True)) + while queue_g and queue_s: + child = queue_g.popleft() + cookies = queue_s.popleft() + if child <= cookies: + result += 1 + else: + queue_s.appendleft(cookies) + return result +``` +### Go ```golang //排序后,局部最优 func findContentChildren(g []int, s []int) int { diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 9efb1e0519..58ada3cb0f 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -262,6 +262,26 @@ class Solution: return None ``` +(方法三) 栈-遍历 +```python +class Solution: + def searchBST(self, root: TreeNode, val: int) -> TreeNode: + stack = [root] + while stack: + node = stack.pop() + # 根据TreeNode的定义 + # node携带有三类信息 node.left/node.right/node.val + # 找到val直接返回node 即是找到了该节点为根的子树 + # 此处node.left/node.right/val的前后顺序可打乱 + if node.val == val: + return node + if node.right: + stack.append(node.right) + if node.left: + stack.append(node.left) + return None +``` + ### Go From c8ef172319234d388730cc835dbd9214c48ea82c Mon Sep 17 00:00:00 2001 From: Minxi Yan <77104028+Yan0613@users.noreply.github.com> Date: Mon, 20 May 2024 13:53:15 +0800 Subject: [PATCH 1136/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0IIGo=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E7=AD=94=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改四数相加IIGo版本的解答代码,修改原版代码中的逻辑问题,使其通过leetcode的代码测试。 --- ...\346\225\260\347\233\270\345\212\240II.md" | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index db9a9e430c..b73901f8ee 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -185,22 +185,28 @@ class Solution: ### Go: ```go -func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { - m := make(map[int]int) //key:a+b的数值,value:a+b数值出现的次数 - count := 0 - // 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中 +func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { + m := make(map[int]int) + count := 0 + + // 构建nums1和nums2的和的map for _, v1 := range nums1 { - for _, v2 := range nums2 { - m[v1+v2]++ - } - } - // 遍历nums3和nums4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来 - for _, v3 := range nums3 { - for _, v4 := range nums4 { - count += m[-v3-v4] - } - } - return count + for _, v2 := range nums2 { + m[v1+v2]++ + } + } + + // 遍历nums3和nums4,检查-c-d是否在map中 + for _, v3 := range nums3 { + for _, v4 := range nums4 { + sum := -v3 - v4 + if countVal, ok := m[sum]; ok { + count += countVal + } + } + } + + return count } ``` From 5073196a1083da5322dde47177f4af08ced9b4f1 Mon Sep 17 00:00:00 2001 From: iteng <1475208984@qq.com> Date: Sat, 25 May 2024 17:26:43 +0800 Subject: [PATCH 1137/1533] =?UTF-8?q?=E8=B0=83=E6=8D=A2leftNode=E3=80=81ri?= =?UTF-8?q?ghtNode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 063b542945..31c24fc5aa 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -224,8 +224,8 @@ public: st.push(root->left); st.push(root->right); while (!st.empty()) { - TreeNode* leftNode = st.top(); st.pop(); TreeNode* rightNode = st.top(); st.pop(); + TreeNode* leftNode = st.top(); st.pop(); if (!leftNode && !rightNode) { continue; } @@ -950,3 +950,4 @@ public bool IsSymmetric(TreeNode root) + From 49aee514ef7c4bf647f85810d013ed633eaf4d4c Mon Sep 17 00:00:00 2001 From: Scarlett-HS <88608721+Scarlett-HS@users.noreply.github.com> Date: Sat, 25 May 2024 21:32:01 -0400 Subject: [PATCH 1138/1533] =?UTF-8?q?=E7=BB=99=200474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6=20=E6=B7=BB=E5=8A=A0=E4=BA=86CPP=E7=9A=84=E4=B8=89?= =?UTF-8?q?=E7=BB=B4=E6=95=B0=E7=BB=84=E5=AE=9E=E7=8E=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\344\270\200\345\222\214\351\233\266.md" | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 47b34a0f31..7b46abeece 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -159,7 +159,89 @@ public: * 时间复杂度: O(kmn),k 为strs的长度 * 空间复杂度: O(mn) +C++: +使用三维数组的版本 +```CPP +class Solution { +public: + int findMaxForm(vector& strs, int m, int n) { + int num_of_str = strs.size(); + + vector>> dp(num_of_str, vector>(m + 1,vector(n + 1, 0))); + + /* dp[i][j][k] represents, if choosing items among strs[0] to strs[i] to form a subset, + what is the maximum size of this subset such that there are no more than m 0's and n 1's in this subset. + Each entry of dp[i][j][k] is initialized with 0 + + transition formula: + using x[i] to indicates the number of 0's in strs[i] + using y[i] to indicates the number of 1's in strs[i] + + dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - x[i]][k - y[i]] + 1) + + */ + + + // num_of_zeros records the number of 0's for each str + // num_of_ones records the number of 1's for each str + // find the number of 0's and the number of 1's for each str in strs + vector num_of_zeros; + vector num_of_ones; + for (auto& str : strs){ + int count_of_zero = 0; + int count_of_one = 0; + for (char &c : str){ + if(c == '0') count_of_zero ++; + else count_of_one ++; + } + num_of_zeros.push_back(count_of_zero); + num_of_ones.push_back(count_of_one); + + } + + + // num_of_zeros[0] indicates the number of 0's for str[0] + // num_of_ones[0] indiates the number of 1's for str[1] + + // initialize the 1st plane of dp[i][j][k], i.e., dp[0][j][k] + // if num_of_zeros[0] > m or num_of_ones[0] > n, no need to further initialize dp[0][j][k], + // because they have been intialized to 0 previously + if(num_of_zeros[0] <= m && num_of_ones[0] <= n){ + // for j < num_of_zeros[0] or k < num_of_ones[0], dp[0][j][k] = 0 + for(int j = num_of_zeros[0]; j <= m; j++){ + for(int k = num_of_ones[0]; k <= n; k++){ + dp[0][j][k] = 1; + } + } + } + + /* if j - num_of_zeros[i] >= 0 and k - num_of_ones[i] >= 0: + dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - num_of_zeros[i]][k - num_of_ones[i]] + 1) + else: + dp[i][j][k] = dp[i-1][j][k] + */ + + for (int i = 1; i < num_of_str; i++){ + int count_of_zeros = num_of_zeros[i]; + int count_of_ones = num_of_ones[i]; + for (int j = 0; j <= m; j++){ + for (int k = 0; k <= n; k++){ + if( j < count_of_zeros || k < count_of_ones){ + dp[i][j][k] = dp[i-1][j][k]; + }else{ + dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - count_of_zeros][k - count_of_ones] + 1); + } + } + } + + } + + return dp[num_of_str-1][m][n]; + + } +}; +``` ## 总结 From 4fa3e4d9b0a39254aecf4597b3bb916218405e6d Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sun, 26 May 2024 10:02:54 +0800 Subject: [PATCH 1139/1533] Update --- ...47\344\272\272\345\267\245\345\262\233.md" | 2 +- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 39 +-- ...60\351\207\217\345\271\277\346\220\234.md" | 11 +- ...00\345\244\247\351\235\242\347\247\257.md" | 221 ++++++++++++++ ...04\346\200\273\351\235\242\347\247\257.md" | 180 +++++++++++ ...11\346\262\241\345\255\244\345\262\233.md" | 133 ++++++++ ...64\346\265\201\351\227\256\351\242\230.md" | 279 +++++++++++++++++ ...00\345\244\247\345\262\233\345\261\277.md" | 254 ++++++++++++++++ ...50\345\217\257\350\276\276\346\200\247.md" | 284 ++++++++++++++++++ ...77\347\232\204\345\221\250\351\225\277.md" | 154 ++++++++++ ...72\346\200\273\347\273\223\347\257\207.md" | 29 +- 11 files changed, 1561 insertions(+), 25 deletions(-) create mode 100644 "problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" create mode 100644 "problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" create mode 100644 "problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" create mode 100644 "problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" create mode 100644 "problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" create mode 100644 "problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" create mode 100644 "problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index e452e4e395..d24eaacc50 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -39,7 +39,7 @@ 每改变一个0的方格,都需要重新计算一个地图的最大面积,所以 整体时间复杂度为:n^4。 -如果对深度优先搜索不了解的录友,可以看这里:[深度优先搜索精讲](https://programmercarl.com/图论深搜理论基础.html) +如果对深度优先搜索不了解的录友,可以看这里:[深度优先搜索精讲](https://programmercarl.com/kamacoder/图论深搜理论基础.html) ## 优化思路 diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index f41d6fd121..ec35fa9d98 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -8,11 +8,14 @@ 某国为促进城市间经济交流,决定对货物运输提供补贴。共有 n 个编号为 1 到 n 的城市,通过道路网络连接,网络中的道路仅允许从某个城市单向通行到另一个城市,不能反向通行。 -网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。权值为正表示扣除了政府补贴后运输货物仍需支付的费用;权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 +网络中的道路都有各自的运输成本和政府补贴,道路的权值计算方式为:运输成本 - 政府补贴。 +权值为正表示扣除了政府补贴后运输货物仍需支付的费用;权值为负则表示政府的补贴超过了支出的运输成本,实际表现为运输过程中还能赚取一定的收益。 -请找出从城市 1 到城市 n 的所有可能路径中,综合政府补贴后的最低运输成本。如果最低运输成本是一个负数,它表示在遵循最优路径的情况下,运输过程中反而能够实现盈利。 +请找出从城市 1 到城市 n 的所有可能路径中,综合政府补贴后的最低运输成本。 + +如果最低运输成本是一个负数,它表示在遵循最优路径的情况下,运输过程中反而能够实现盈利。 城市 1 到城市 n 之间可能会出现没有路径的情况,同时保证道路网络中不存在任何负权回路。 @@ -41,11 +44,11 @@ 1 3 5 ``` -## 思路 +## 背景 本题我们来系统讲解 Bellman_ford 队列优化算法 ,也叫SPFA算法(Shortest Path Faster Algorithm)。 -> SPFA的称呼来自 1994年西南交通大学段凡丁的论文,其实Bellman_ford 提出后不久 (20世纪50年代末期) 就有队列优化的版本,国际上不承认这个算法是是国内提出的。 所以国际上一般称呼 算法为 Bellman_ford 队列优化算法(Queue improved Bellman-Ford) +> SPFA的称呼来自 1994年西南交通大学段凡丁的论文,其实Bellman_ford 提出后不久 (20世纪50年代末期) 就有队列优化的版本,国际上不承认这个算法是是国内提出的。 所以国际上一般称呼 该算法为 Bellman_ford 队列优化算法(Queue improved Bellman-Ford) 大家知道以上来历,知道 SPFA 和 Bellman_ford 队列优化算法 指的都是一个算法就好。 @@ -72,6 +75,8 @@ 用队列来记录。(其实用栈也行,对元素顺序没有要求) +## 模拟过程 + 接下来来举例这个队列是如何工作的。 以示例给出的所有边为例: @@ -88,19 +93,19 @@ 我们依然使用**minDist数组来表达 起点到各个节点的最短距离**,例如minDist[3] = 5 表示起点到达节点3 的最小距离为5 -初始化,起点为节点1, 起点到起点的最短距离为0,所以minDist[1] 为 0。 将节点1 加入队列 (下次松弛送节点1开始) +初始化,起点为节点1, 起点到起点的最短距离为0,所以minDist[1] 为 0。 将节点1 加入队列 (下次松弛从节点1开始) ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115555.png) ------------ -从队列里取出节点1,松弛节点1 作为出发点链接的边(节点1 -> 节点2)和边(节点1 -> 节点3) +从队列里取出节点1,松弛节点1 作为出发点连接的边(节点1 -> 节点2)和边(节点1 -> 节点3) 边:节点1 -> 节点2,权值为1 ,minDist[2] > minDist[1] + 1 ,更新 minDist[2] = minDist[1] + 1 = 0 + 1 = 1 。 边:节点1 -> 节点3,权值为5 ,minDist[3] > minDist[1] + 5,更新 minDist[3] = minDist[1] + 5 = 0 + 5 = 5。 -将节点2,节点3 加入队列,如图: +将节点2、节点3 加入队列,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115544.png) @@ -108,7 +113,7 @@ ----------------- -从队列里取出节点2,松弛节点2 作为出发点链接的边(节点2 -> 节点4)和边(节点2 -> 节点5) +从队列里取出节点2,松弛节点2 作为出发点连接的边(节点2 -> 节点4)和边(节点2 -> 节点5) 边:节点2 -> 节点4,权值为1 ,minDist[4] > minDist[2] + (-3) ,更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 。 @@ -123,7 +128,7 @@ -------------------- -从队列里出去节点3,松弛节点3 作为出发点链接的边。 +从队列里出去节点3,松弛节点3 作为出发点连接的边。 因为没有从节点3作为出发点的边,所以这里就从队列里取出节点3就好,不用做其他操作,如图: @@ -132,11 +137,11 @@ ------------ -从队列中取出节点4,松弛节点4作为出发点链接的边(节点4 -> 节点6) +从队列中取出节点4,松弛节点4作为出发点连接的边(节点4 -> 节点6) 边:节点4 -> 节点6,权值为4 ,minDist[6] > minDist[4] + 4,更新 minDist[6] = minDist[4] + 4 = -2 + 4 = 2 。 -讲节点6加入队列 +将节点6加入队列 如图: @@ -145,7 +150,7 @@ --------------- -从队列中取出节点5,松弛节点5作为出发点链接的边(节点5 -> 节点3),边(节点5 -> 节点6) +从队列中取出节点5,松弛节点5作为出发点连接的边(节点5 -> 节点3),边(节点5 -> 节点6) 边:节点5 -> 节点3,权值为1 ,minDist[3] > minDist[5] + 1 ,更新 minDist[3] = minDist[5] + 1 = 3 + 1 = 4 @@ -157,14 +162,14 @@ -因为节点3,和 节点6 都曾经加入过队列,不用重复加入,避免重复计算。 +因为节点3 和 节点6 都曾经加入过队列,不用重复加入,避免重复计算。 在代码中我们可以用一个数组 visited 来记录入过队列的元素,加入过队列的元素,不再重复入队列。 -------------- -从队列中取出节点6,松弛节点6 作为出发点链接的边。 +从队列中取出节点6,松弛节点6 作为出发点连接的边。 节点6作为终点,没有可以出发的边。 @@ -181,7 +186,7 @@ 了解了大体流程,我们再看代码应该怎么写。 -在上面模拟过程中,我们每次都要知道 一个节点作为出发点 链接了哪些节点。 +在上面模拟过程中,我们每次都要知道 一个节点作为出发点连接了哪些节点。 如果想方便知道这些数据,就需要使用邻接表来存储这个图,如果对于邻接表不了解的话,可以看 [kama0047.参会dijkstra堆](./kama0047.参会dijkstra堆.md) 中 图的存储 部分。 @@ -279,7 +284,7 @@ n为其他数值的时候,也是一样的。 并没有计算 出队列 和 入队列的时间消耗。 因为这个在不同语言上 时间消耗也是不一定的。 -以C++为例,以下两端代码理论上,时间复杂度都是 O(n) : +以C++为例,以下两段代码理论上,时间复杂度都是 O(n) : ```CPP for (long long i = 0; i < n; i++) { @@ -316,7 +321,7 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹 这里可能有录友疑惑,`while (!que.empty())` 队里里 会不会造成死循环? 例如 图中有环,这样一直有元素加入到队列里? -其实有环的情况,要看它是 正权回路 还是 负全回路。 +其实有环的情况,要看它是 正权回路 还是 负权回路。 题目描述中,已经说了,本题没有 负权回路 。 diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index dfcf062f90..e7a0a8f3ac 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -54,14 +54,16 @@ 这道题题目是 DFS,BFS,并查集,基础题目。 -本题思路,是用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。 +本题思路:遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。 -在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。 +再遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。 那么如果把节点陆地所能遍历到的陆地都标记上呢,就可以使用 DFS,BFS或者并查集。 ### 广度优先搜索 +如果不熟悉广搜,建议先看 [广搜理论基础](./图论广搜理论基础.md)。 + 不少同学用广搜做这道题目的时候,超时了。 这里有一个广搜中很重要的细节: 根本原因是**只要 加入队列就代表 走过,就需要标记,而不是从队列拿出来的时候再去标记走过**。 @@ -72,7 +74,7 @@ ![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png) -超时写法 (从队列中取出节点再标记) +超时写法 (从队列中取出节点再标记,注意代码注释的地方) ```CPP int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 @@ -98,7 +100,7 @@ void bfs(vector>& grid, vector>& visited, int x, int y ``` -加入队列 就代表走过,立刻标记,正确写法: +加入队列 就代表走过,立刻标记,正确写法: (注意代码注释的地方) ```CPP int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 @@ -155,7 +157,6 @@ void bfs(const vector>& grid, vector>& visited, int x, } } - int main() { int n, m; cin >> n >> m; diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" new file mode 100644 index 0000000000..6f8a9ed86a --- /dev/null +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -0,0 +1,221 @@ + +# 100. 岛屿的最大面积 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1172) + +[力扣题目链接](https://programmercarl.com/0695.%E5%B2%9B%E5%B1%BF%E7%9A%84%E6%9C%80%E5%A4%A7%E9%9D%A2%E7%A7%AF.html#%E6%80%9D%E8%B7%AF) + +题目描述 + +给定一个由 1(陆地)和 0(水)组成的矩阵,计算岛屿的最大面积。岛屿面积的计算方式为组成岛屿的陆地的总数。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。 + +输入描述 + +第一行包含两个整数 N, M,表示矩阵的行数和列数。后续 N 行,每行包含 M 个数字,数字为 1 或者 0,表示岛屿的单元格。 + +输出描述 + +输出一个整数,表示岛屿的最大面积。如果不存在岛屿,则输出 0。 + +输入示例 + +``` +4 5 +1 1 0 0 0 +1 1 0 0 0 +0 0 1 0 0 +0 0 0 1 1 +``` + +输出示例 + +4 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517103410.png) + +样例输入中,岛屿的最大面积为 4。 + +数据范围: + +* 1 <= M, N <= 50。 + + +## 思路 + +注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 + +也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: + +![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) + +这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上“1”的数量,然后取一个最大的。 + +本题思路上比较简单,难点其实都是 dfs 和 bfs的理论基础,关于理论基础我在这里都有详细讲解 : + +* [DFS理论基础](https://programmercarl.com/kamacoder/图论深搜理论基础.html) +* [BFS理论基础](https://programmercarl.com/kamacoder/图论广搜理论基础.html) + +### DFS + +很多同学写dfs其实也是凭感觉来的,有的时候dfs函数中写终止条件才能过,有的时候 dfs函数不写终止添加也能过! + +这里其实涉及到dfs的两种写法。 + +写法一,dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 + +```CPP +// 版本一 +#include +#include +using namespace std; +int count; +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void dfs(vector>& grid, vector>& visited, int x, int y) { + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 没有访问过的 同时 是陆地的 + visited[nextx][nexty] = true; + count++; + dfs(grid, visited, nextx, nexty); + } + } +} + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + vector> visited(n, vector(m, false)); + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + count = 1; // 因为dfs处理下一个节点,所以这里遇到陆地了就先计数,dfs处理接下来的相邻陆地 + visited[i][j] = true; + dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + result = max(result, count); + } + } + } + cout << result << endl; + +} +``` + +写法二,dfs处理当前节点,即在主函数遇到岛屿就计数为0,dfs处理接下来的全部陆地 + +dfs +```CPP +// 版本二 +#include +#include +using namespace std; + +int count; +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void dfs(vector>& grid, vector>& visited, int x, int y) { + if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = true; // 标记访问过 + count++; + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + dfs(grid, visited, nextx, nexty); + } +} + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + vector> visited = vector>(n, vector(m, false)); + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + count = 0; // 因为dfs处理当前节点,所以遇到陆地计数为0,进dfs之后在开始从1计数 + dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + result = max(result, count); + } + } + } + cout << result << endl; +} +``` + +大家通过注释可以发现,两种写法,版本一,在主函数遇到陆地就计数为1,接下来的相邻陆地都在dfs中计算。 + +版本二 在主函数遇到陆地 计数为0,也就是不计数,陆地数量都去dfs里做计算。 + +这也是为什么大家看了很多 dfs的写法 ,发现写法怎么都不一样呢? 其实这就是根本原因。 + + +### BFS + +关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://programmercarl.com/kamacoder/图论广搜理论基础.html) + +本题BFS代码如下: + +```CPP +class Solution { +private: + int count; + int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 + void bfs(vector>& grid, vector>& visited, int x, int y) { + queue que; + que.push(x); + que.push(y); + visited[x][y] = true; // 加入队列就意味节点是陆地可到达的点 + count++; + while(!que.empty()) { + int xx = que.front();que.pop(); + int yy = que.front();que.pop(); + for (int i = 0 ;i < 4; i++) { + int nextx = xx + dir[i][0]; + int nexty = yy + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界 + if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 节点没有被访问过且是陆地 + visited[nextx][nexty] = true; + count++; + que.push(nextx); + que.push(nexty); + } + } + } + } + +public: + int maxAreaOfIsland(vector>& grid) { + int n = grid.size(), m = grid[0].size(); + vector> visited = vector>(n, vector(m, false)); + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + count = 0; + bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + result = max(result, count); + } + } + } + return result; + } +}; + +``` + diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" new file mode 100644 index 0000000000..602fb97738 --- /dev/null +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -0,0 +1,180 @@ + +# 101. 孤岛的总面积 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1173) + +题目描述 + +给定一个由 1(陆地)和 0(水)组成的矩阵,岛屿指的是由水平或垂直方向上相邻的陆地单元格组成的区域,且完全被水域单元格包围。孤岛是那些位于矩阵内部、所有单元格都不接触边缘的岛屿。 + + +现在你需要计算所有孤岛的总面积,岛屿面积的计算方式为组成岛屿的陆地的总数。 + +输入描述 + +第一行包含两个整数 N, M,表示矩阵的行数和列数。之后 N 行,每行包含 M 个数字,数字为 1 或者 0。 + +输出描述 + +输出一个整数,表示所有孤岛的总面积,如果不存在孤岛,则输出 0。 + +输入示例 + +``` +4 5 +1 1 0 0 0 +1 1 0 0 0 +0 0 1 0 0 +0 0 0 1 1 +``` + + +输出示例: + +1 + +提示信息: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517105557.png) + +在矩阵中心部分的岛屿,因为没有任何一个单元格接触到矩阵边缘,所以该岛屿属于孤岛,总面积为 1。 + + +数据范围: + +1 <= M, N <= 50。 + +## 思路 + +本题使用dfs,bfs,并查集都是可以的。 + +本题要求找到不靠边的陆地面积,那么我们只要从周边找到陆地然后 通过 dfs或者bfs 将周边靠陆地且相邻的陆地都变成海洋,然后再去重新遍历地图 统计此时还剩下的陆地就可以了。 + +如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104632.png) + +在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104651.png) + +然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 + +如果对深搜或者广搜不够了解,建议先看这里:[深度优先搜索精讲](https://programmercarl.com/kamacoder/图论深搜理论基础.html),[广度优先搜索精讲](https://programmercarl.com/kamacoder/图论广搜理论基础.html)。 + + +采用深度优先搜索的代码如下: + +```CPP +#include +#include +using namespace std; +int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // 保存四个方向 +int count; // 统计符合题目要求的陆地空格数量 +void dfs(vector>& grid, int x, int y) { + grid[x][y] = 0; + count++; + for (int i = 0; i < 4; i++) { // 向四个方向遍历 + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + // 超过边界 + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; + // 不符合条件,不继续遍历 + if (grid[nextx][nexty] == 0) continue; + + dfs (grid, nextx, nexty); + } + return; +} + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + + // 从左侧边,和右侧边 向中间遍历 + for (int i = 0; i < n; i++) { + if (grid[i][0] == 1) dfs(grid, i, 0); + if (grid[i][m - 1] == 1) dfs(grid, i, m - 1); + } + // 从上边和下边 向中间遍历 + for (int j = 0; j < m; j++) { + if (grid[0][j] == 1) dfs(grid, 0, j); + if (grid[n - 1][j] == 1) dfs(grid, n - 1, j); + } + count = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) dfs(grid, i, j); + } + } + cout << count << endl; +} +``` + +采用广度优先搜索的代码如下: + +```CPP +#include +#include +#include +using namespace std; +int count = 0; +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void bfs(vector>& grid, int x, int y) { + queue> que; + que.push({x, y}); + grid[x][y] = 0; // 只要加入队列,立刻标记 + count++; + while(!que.empty()) { + pair cur = que.front(); que.pop(); + int curx = cur.first; + int cury = cur.second; + for (int i = 0; i < 4; i++) { + int nextx = curx + dir[i][0]; + int nexty = cury + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + if (grid[nextx][nexty] == 1) { + que.push({nextx, nexty}); + count++; + grid[nextx][nexty] = 0; // 只要加入队列立刻标记 + } + } + } +} + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + // 从左侧边,和右侧边 向中间遍历 + for (int i = 0; i < n; i++) { + if (grid[i][0] == 1) bfs(grid, i, 0); + if (grid[i][m - 1] == 1) bfs(grid, i, m - 1); + } + // 从上边和下边 向中间遍历 + for (int j = 0; j < m; j++) { + if (grid[0][j] == 1) bfs(grid, 0, j); + if (grid[n - 1][j] == 1) bfs(grid, n - 1, j); + } + count = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) bfs(grid, i, j); + } + } + + cout << count << endl; +} +``` + diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" new file mode 100644 index 0000000000..4c7491f716 --- /dev/null +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -0,0 +1,133 @@ + +# 102. 沉没孤岛 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1174) + +题目描述: + +给定一个由 1(陆地)和 0(水)组成的矩阵,岛屿指的是由水平或垂直方向上相邻的陆地单元格组成的区域,且完全被水域单元格包围。孤岛是那些位于矩阵内部、所有单元格都不接触边缘的岛屿。 + + +现在你需要将所有孤岛“沉没”,即将孤岛中的所有陆地单元格(1)转变为水域单元格(0)。 + +输入描述: + +第一行包含两个整数 N, M,表示矩阵的行数和列数。 + +之后 N 行,每行包含 M 个数字,数字为 1 或者 0,表示岛屿的单元格。 + +输出描述 + +输出将孤岛“沉没”之后的岛屿矩阵。 + +输入示例: + +``` +4 5 +1 1 0 0 0 +1 1 0 0 0 +0 0 1 0 0 +0 0 0 1 1 +``` + +输出示例: + +``` +1 1 0 0 0 +1 1 0 0 0 +0 0 0 0 0 +0 0 0 1 1 +``` + +提示信息: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517110932.png) + +将孤岛沉没: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517110953.png) + +数据范围: + +1 <= M, N <= 50 + +## 思路 + +这道题目和[0101.孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173)正好反过来了,[0101.孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173)是求 地图中间的空格数,而本题是要把地图中间的 1 都改成 0 。 + +那么两题在思路上也是差不多的。 + +思路依然是从地图周边出发,将周边空格相邻的陆地都做上标记,然后在遍历一遍地图,遇到 陆地 且没做过标记的,那么都是地图中间的 陆地 ,全部改成水域就行。 + +有的录友可能想,我在定义一个 visited 二维数组,单独标记周边的陆地,然后遍历地图的时候同时对 数组board 和 数组visited 进行判断,决定 陆地是否变成水域。 + +这样做其实就有点麻烦了,不用额外定义空间了,标记周边的陆地,可以直接改陆地为其他特殊值作为标记。 + +步骤一:深搜或者广搜将地图周边的 1 (陆地)全部改成 2 (特殊标记) + +步骤二:将水域中间 1 (陆地)全部改成 水域(0) + +步骤三:将之前标记的 2 改为 1 (陆地) + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517113813.png) + +整体C++代码如下,以下使用dfs实现,其实遍历方式dfs,bfs都是可以的。 + +```CPP +#include +#include +using namespace std; +int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // 保存四个方向 +void dfs(vector>& grid, int x, int y) { + grid[x][y] = 2; + for (int i = 0; i < 4; i++) { // 向四个方向遍历 + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + // 超过边界 + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; + // 不符合条件,不继续遍历 + if (grid[nextx][nexty] == 0 || grid[nextx][nexty] == 2) continue; + dfs (grid, nextx, nexty); + } + return; +} + +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + + // 步骤一: + // 从左侧边,和右侧边 向中间遍历 + for (int i = 0; i < n; i++) { + if (grid[i][0] == 1) dfs(grid, i, 0); + if (grid[i][m - 1] == 1) dfs(grid, i, m - 1); + } + + // 从上边和下边 向中间遍历 + for (int j = 0; j < m; j++) { + if (grid[0][j] == 1) dfs(grid, 0, j); + if (grid[n - 1][j] == 1) dfs(grid, n - 1, j); + } + // 步骤二、步骤三 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) grid[i][j] = 0; + if (grid[i][j] == 2) grid[i][j] = 1; + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cout << grid[i][j] << " "; + } + cout << endl; + } +} +``` diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" new file mode 100644 index 0000000000..8f99beae1f --- /dev/null +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -0,0 +1,279 @@ + +# 103. 水流问题 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1175) + +题目描述: + +现有一个 N × M 的矩阵,每个单元格包含一个数值,这个数值代表该位置的相对高度。矩阵的左边界和上边界被认为是第一组边界,而矩阵的右边界和下边界被视为第二组边界。 + + +矩阵模拟了一个地形,当雨水落在上面时,水会根据地形的倾斜向低处流动,但只能从较高或等高的地点流向较低或等高并且相邻(上下左右方向)的地点。我们的目标是确定那些单元格,从这些单元格出发的水可以达到第一组边界和第二组边界。 + +输入描述: + +第一行包含两个整数 N 和 M,分别表示矩阵的行数和列数。 + +后续 N 行,每行包含 M 个整数,表示矩阵中的每个单元格的高度。 + +输出描述: + +输出共有多行,每行输出两个整数,用一个空格隔开,表示可达第一组边界和第二组边界的单元格的坐标,输出顺序任意。 + +输入示例: + +``` +5 5 +1 3 1 2 4 +1 2 1 3 2 +2 4 7 2 1 +4 5 6 1 1 +1 4 1 2 1 +``` + +输出示例: + +``` +0 4 +1 3 +2 2 +3 0 +3 1 +3 2 +4 0 +4 1 +``` + +提示信息: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517115816.png) + +图中的蓝色方块上的雨水既能流向第一组边界,也能流向第二组边界。所以最终答案为所有蓝色方块的坐标。 + + +数据范围: + +1 <= M, N <= 50 + +## 思路 + +一个比较直白的想法,其实就是 遍历每个点,然后看这个点 能不能同时到达第一组边界和第二组边界。 + +至于遍历方式,可以用dfs,也可以用bfs,以下用dfs来举例。 + +那么这种思路的实现代码如下: + +```CPP +#include +#include +using namespace std; +int n, m; +int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; + +// 从 x,y 出发 把可以走的地方都标记上 +void dfs(vector>& grid, vector>& visited, int x, int y) { + if (visited[x][y]) return; + + visited[x][y] = true; + + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue; + if (grid[x][y] < grid[nextx][nexty]) continue; // 高度不合适 + + dfs (grid, visited, nextx, nexty); + } + return; +} +bool isResult(vector>& grid, int x, int y) { + vector> visited(n, vector(m, false)); + + // 深搜,将x,y出发 能到的节点都标记上。 + dfs(grid, visited, x, y); + bool isFirst = false; + bool isSecond = false; + + // 以下就是判断x,y出发,是否到达第一组边界和第二组边界 + // 第一边界的上边 + for (int j = 0; j < m; j++) { + if (visited[0][j]) { + isFirst = true; + break; + } + } + // 第一边界的左边 + for (int i = 0; i < n; i++) { + if (visited[i][0]) { + isFirst = true; + break; + } + } + // 第二边界右边 + for (int j = 0; j < m; j++) { + if (visited[n - 1][j]) { + isSecond = true; + break; + } + } + // 第二边界下边 + for (int i = 0; i < n; i++) { + if (visited[i][m - 1]) { + isSecond = true; + break; + } + } + if (isFirst && isSecond) return true; + return false; +} + + +int main() { + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + // 遍历每一个点,看是否能同时到达第一组边界和第二组边界 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (isResult(grid, i, j)) { + cout << i << " " << j << endl; + } + } + } +} + +``` + +这种思路很直白,但很明显,以上代码超时了。 来看看时间复杂度。 + +遍历每一个节点,是 m * n,遍历每一个节点的时候,都要做深搜,深搜的时间复杂度是: m * n + +那么整体时间复杂度 就是 O(m^2 * n^2) ,这是一个四次方的时间复杂度。 + +## 优化 + +那么我们可以 反过来想,从第一组边界上的节点 逆流而上,将遍历过的节点都标记上。 + +同样从第二组边界的边上节点 逆流而上,将遍历过的节点也标记上。 + +然后**两方都标记过的节点就是既可以流太平洋也可以流大西洋的节点**。 + +从第一组边界边上节点出发,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522120036.png) + + +从第二组边界上节点出发,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522120122.png) + +按照这样的逻辑,就可以写出如下遍历代码:(详细注释) + + +```CPP +#include +#include +using namespace std; +int n, m; +int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; +void dfs(vector>& grid, vector>& visited, int x, int y) { + if (visited[x][y]) return; + + visited[x][y] = true; + + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue; + if (grid[x][y] > grid[nextx][nexty]) continue; // 注意:这里是从低向高遍历 + + dfs (grid, visited, nextx, nexty); + } + return; +} + + + +int main() { + + cin >> n >> m; + vector> grid(n, vector(m, 0)); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + // 标记从第一组边界上的节点出发,可以遍历的节点 + vector> firstBorder(n, vector(m, false)); + + // 标记从第一组边界上的节点出发,可以遍历的节点 + vector> secondBorder(n, vector(m, false)); + + // 从最上和最下行的节点出发,向高处遍历 + for (int i = 0; i < n; i++) { + dfs (grid, firstBorder, i, 0); // 遍历最左列,接触第一组边界 + dfs (grid, secondBorder, i, m - 1); // 遍历最右列,接触第二组边界 + } + + // 从最左和最右列的节点出发,向高处遍历 + for (int j = 0; j < m; j++) { + dfs (grid, firstBorder, 0, j); // 遍历最上行,接触第一组边界 + dfs (grid, secondBorder, n - 1, j); // 遍历最下行,接触第二组边界 + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + // 如果这个节点,从第一组边界和第二组边界出发都遍历过,就是结果 + if (firstBorder[i][j] && secondBorder[i][j]) cout << i << " " << j << endl;; + } + } + + +} + +``` + + +时间复杂度分析, 关于dfs函数搜索的过程 时间复杂度是 O(n * m),这个大家比较容易想。 + +关键看主函数,那么每次dfs的时候,上面还是有for循环的。 + +第一个for循环,时间复杂度是:n * (n * m) 。 + +第二个for循环,时间复杂度是:m * (n * m)。 + +所以本题看起来 时间复杂度好像是 : n * (n * m) + m * (n * m) = (m * n) * (m + n) 。 + +其实这是一个误区,大家再自己看 dfs函数的实现,其实 有visited函数记录 走过的节点,而走过的节点是不会再走第二次的。 + +所以 调用dfs函数,**只要参数传入的是 数组 firstBorder,那么地图中 每一个节点其实就遍历一次,无论你调用多少次**。 + +同理,调用dfs函数,只要 参数传入的是 数组 secondBorder,地图中每个节点也只会遍历一次。 + +所以,以下这段代码的时间复杂度是 2 * n * m。 地图用每个节点就遍历了两次,参数传入 firstBorder 的时候遍历一次,参数传入 secondBorder 的时候遍历一次。 + +```CPP +// 从最上和最下行的节点出发,向高处遍历 +for (int i = 0; i < n; i++) { + dfs (grid, firstBorder, i, 0); // 遍历最左列,接触第一组边界 + dfs (grid, secondBorder, i, m - 1); // 遍历最右列,接触第二组边界 +} + +// 从最左和最右列的节点出发,向高处遍历 +for (int j = 0; j < m; j++) { + dfs (grid, firstBorder, 0, j); // 遍历最上行,接触第一组边界 + dfs (grid, secondBorder, n - 1, j); // 遍历最下行,接触第二组边界 +} +``` + +那么本题整体的时间复杂度其实是: 2 * n * m + n * m ,所以最终时间复杂度为 O(n * m) 。 + +空间复杂度为:O(n * m) 这个就不难理解了。开了几个 n * m 的数组。 + + + + diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" new file mode 100644 index 0000000000..927b20b2d3 --- /dev/null +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -0,0 +1,254 @@ + +# 104.建造最大岛屿 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1176) + +题目描述: + +给定一个由 1(陆地)和 0(水)组成的矩阵,你最多可以将矩阵中的一格水变为一块陆地,在执行了此操作之后,矩阵中最大的岛屿面积是多少。 + +岛屿面积的计算方式为组成岛屿的陆地的总数。岛屿是被水包围,并且通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设矩阵外均被水包围。 + +输入描述: + +第一行包含两个整数 N, M,表示矩阵的行数和列数。之后 N 行,每行包含 M 个数字,数字为 1 或者 0,表示岛屿的单元格。 + +输出描述: + +输出一个整数,表示最大的岛屿面积。如果矩阵中不存在岛屿,则输出 0。 + +输入示例: + +``` +4 5 +1 1 0 0 0 +1 1 0 0 0 +0 0 1 0 0 +0 0 0 1 1 +``` + +输出示例 + +6 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522154055.png) + + +对于上面的案例,有两个位置可将 0 变成 1,使得岛屿的面积最大,即 6。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522154110.png) + + +数据范围: + +1 <= M, N <= 50。 + + +## 思路 + +本题的一个暴力想法,应该是遍历地图尝试 将每一个 0 改成1,然后去搜索地图中的最大的岛屿面积。 + +计算地图的最大面积:遍历地图 + 深搜岛屿,时间复杂度为 n * n。 + +(其实使用深搜还是广搜都是可以的,其目的就是遍历岛屿做一个标记,相当于染色,那么使用哪个遍历方式都行,以下我用深搜来讲解) + +每改变一个0的方格,都需要重新计算一个地图的最大面积,所以 整体时间复杂度为:n^4。 + +## 优化思路 + +其实每次深搜遍历计算最大岛屿面积,我们都做了很多重复的工作。 + +只要用一次深搜把每个岛屿的面积记录下来就好。 + +第一步:一次遍历地图,得出各个岛屿的面积,并做编号记录。可以使用map记录,key为岛屿编号,value为岛屿面积 + +第二步:再遍历地图,遍历0的方格(因为要将0变成1),并统计该1(由0变成的1)周边岛屿面积,将其相邻面积相加在一起,遍历所有 0 之后,就可以得出 选一个0变成1 之后的最大面积。 + +拿如下地图的岛屿情况来举例: (1为陆地) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829104834.png) + +第一步,则遍历题目,并将岛屿到编号和面积上的统计,过程如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105644.png) + + +本过程代码如下: + +```CPP +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void dfs(vector>& grid, vector>& visited, int x, int y, int mark) { + if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = true; // 标记访问过 + grid[x][y] = mark; // 给陆地标记新标签 + count++; + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + dfs(grid, visited, nextx, nexty, mark); + } +} + +int largestIsland(vector>& grid) { + int n = grid.size(), m = grid[0].size(); + vector> visited = vector>(n, vector(m, false)); // 标记访问过的点 + unordered_map gridNum; + int mark = 2; // 记录每个岛屿的编号 + bool isAllGrid = true; // 标记是否整个地图都是陆地 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 0) isAllGrid = false; + if (!visited[i][j] && grid[i][j] == 1) { + count = 0; + dfs(grid, visited, i, j, mark); // 将与其链接的陆地都标记上 true + gridNum[mark] = count; // 记录每一个岛屿的面积 + mark++; // 记录下一个岛屿编号 + } + } + } +} +``` + + +这个过程时间复杂度 n * n 。可能有录友想:分明是两个for循环下面套这一个dfs,时间复杂度怎么回事 n * n呢? + +其实大家可以仔细看一下代码,**n * n这个方格地图中,每个节点我们就遍历一次,并不会重复遍历**。 + +第二步过程如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105249.png) + +也就是遍历每一个0的方格,并统计其相邻岛屿面积,最后取一个最大值。 + +这个过程的时间复杂度也为 n * n。 + +所以整个解法的时间复杂度,为 n * n + n * n 也就是 n^2。 + +当然这里还有一个优化的点,就是 可以不用 visited数组,因为有mark来标记,所以遍历过的grid[i][j]是不等于1的。 + +代码如下: + +```CPP +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void dfs(vector>& grid, int x, int y, int mark) { + if (grid[x][y] != 1 || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水 + grid[x][y] = mark; // 给陆地标记新标签 + count++; + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue; // 越界了,直接跳过 + dfs(grid, nextx, nexty, mark); + } +} + +int main() { + cin >> n >> m; + vector> grid(n, vector(m, 0)); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + unordered_map gridNum; + int mark = 2; // 记录每个岛屿的编号 + bool isAllGrid = true; // 标记是否整个地图都是陆地 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 0) isAllGrid = false; + if (grid[i][j] == 1) { + count = 0; + dfs(grid, i, j, mark); // 将与其链接的陆地都标记上 true + gridNum[mark] = count; // 记录每一个岛屿的面积 + mark++; // 记录下一个岛屿编号 + } + } + } +``` + +不过为了让各个变量各司其事,代码清晰一些,完整代码还是使用visited数组来标记。 + +最后,整体代码如下: + +```CPP +#include +#include +#include +#include +using namespace std; +int n, m; +int count; + +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void dfs(vector>& grid, vector>& visited, int x, int y, int mark) { + if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = true; // 标记访问过 + grid[x][y] = mark; // 给陆地标记新标签 + count++; + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue; // 越界了,直接跳过 + dfs(grid, visited, nextx, nexty, mark); + } +} + +int main() { + cin >> n >> m; + vector> grid(n, vector(m, 0)); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + vector> visited(n, vector(m, false)); // 标记访问过的点 + unordered_map gridNum; + int mark = 2; // 记录每个岛屿的编号 + bool isAllGrid = true; // 标记是否整个地图都是陆地 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 0) isAllGrid = false; + if (!visited[i][j] && grid[i][j] == 1) { + count = 0; + dfs(grid, visited, i, j, mark); // 将与其链接的陆地都标记上 true + gridNum[mark] = count; // 记录每一个岛屿的面积 + mark++; // 记录下一个岛屿编号 + } + } + } + if (isAllGrid) { + cout << n * m << endl; // 如果都是陆地,返回全面积 + return 0; // 结束程序 + } + + // 以下逻辑是根据添加陆地的位置,计算周边岛屿面积之和 + int result = 0; // 记录最后结果 + unordered_set visitedGrid; // 标记访问过的岛屿 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + count = 1; // 记录连接之后的岛屿数量 + visitedGrid.clear(); // 每次使用时,清空 + if (grid[i][j] == 0) { + for (int k = 0; k < 4; k++) { + int neari = i + dir[k][1]; // 计算相邻坐标 + int nearj = j + dir[k][0]; + if (neari < 0 || neari >= n || nearj < 0 || nearj >= m) continue; + if (visitedGrid.count(grid[neari][nearj])) continue; // 添加过的岛屿不要重复添加 + // 把相邻四面的岛屿数量加起来 + count += gridNum[grid[neari][nearj]]; + visitedGrid.insert(grid[neari][nearj]); // 标记该岛屿已经添加过 + } + } + result = max(result, count); + } + } + cout << result << endl; + +} +``` + diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" new file mode 100644 index 0000000000..81f4f6f0ac --- /dev/null +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -0,0 +1,284 @@ + +# 105.有向图的完全可达性 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1177) + +【题目描述】 + +给定一个有向图,包含 N 个节点,节点编号分别为 1,2,...,N。现从 1 号节点开始,如果可以从 1 号节点的边可以到达任何节点,则输出 1,否则输出 -1。 + +【输入描述】 + +第一行包含两个正整数,表示节点数量 N 和边的数量 K。 后续 K 行,每行两个正整数 s 和 t,表示从 s 节点有一条边单向连接到 t 节点。 + +【输出描述】 + +如果可以从 1 号节点的边可以到达任何节点,则输出 1,否则输出 -1。 + +【输入示例】 + +``` +4 4 +1 2 +2 1 +1 3 +3 4 +``` + +【输出示例】 + +1 + +【提示信息】 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522174707.png) + +从 1 号节点可以到达任意节点,输出 1。 + +数据范围: + +* 1 <= N <= 100; +* 1 <= K <= 2000。 + +## 思路 + +本题给我们是一个有向图, 意识到这是有向图很重要! + +接下来我们再画一个图,从图里可以直观看出来,节点6 是 不能到达节点1 的 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522175451.png) + +这就很容易让我们想起岛屿问题,只要发现独立的岛,就是不可到达的。 + +**但本题是有向图**,在有向图中,即使所有节点都是链接的,但依然不可能从0出发遍历所有边。 + +例如上图中,节点1 可以到达节点2,但节点2是不能到达节点1的。 + +所以本题是一个**有向图搜索全路径的问题**。 只能用深搜(DFS)或者广搜(BFS)来搜。 + +**以下dfs分析 大家一定要仔细看,本题有两种dfs的解法,很多题解没有讲清楚**。 看完之后 相信你对dfs会有更深的理解。 + +深搜三部曲: + +1. 确认递归函数,参数 + +需要传入地图,需要知道当前我们拿到的key,以至于去下一个房间。 + +同时还需要一个数组,用来记录我们都走过了哪些房间,这样好知道最后有没有把所有房间都遍历的,可以定义一个一维数组。 + +所以 递归函数参数如下: + +```C++ +// key 当前得到的可以 +// visited 记录访问过的房间 +void dfs(const vector>& graph, int key, vector& visited) { +``` + +2. 确认终止条件 + +遍历的时候,什么时候终止呢? + +这里有一个很重要的逻辑,就是在递归中,**我们是处理当前访问的节点,还是处理下一个要访问的节点**。 + +这决定 终止条件怎么写。 + +首先明确,本题中什么叫做处理,就是 visited数组来记录访问过的节点,该节点默认 数组里元素都是false,把元素标记为true就是处理 本节点了。 + +如果我们是处理当前访问的节点,当前访问的节点如果是 true ,说明是访问过的节点,那就终止本层递归,如果不是true,我们就把它赋值为true,因为这是我们处理本层递归的节点。 + +代码就是这样: + +```C++ +// 写法一:处理当前访问的节点 +void dfs(const vector>& graph, int key, vector& visited) { + if (visited[key]) { + return; + } + visited[key] = true; + list keys = graph[key]; + for (int key : keys) { + // 深度优先搜索遍历 + dfs(graph, key, visited); + } +} +``` + +如果我们是处理下一层访问的节点,而不是当前层。那么就要在 深搜三部曲中第三步:处理目前搜索节点出发的路径的时候对 节点进行处理。 + +这样的话,就不需要终止条件,而是在 搜索下一个节点的时候,直接判断 下一个节点是否是我们要搜的节点。 + +代码就是这样的: + +```C++ +// 写法二:处理下一个要访问的节点 +void dfs(const vector>& graph, int key, vector& visited) { + list keys = rooms[key]; + for (int key : keys) { + if (visited[key] == false) { // 确认下一个是没访问过的节点 + visited[key] = true; + dfs(rooms, key, visited); + } + } +} +``` + +可以看出,**如何看待 我们要访问的节点,直接决定了两种不一样的写法**,很多录友对这一块很模糊,可能做过这道题,但没有思考到这个维度上。 + + +3. 处理目前搜索节点出发的路径 + +其实在上面,深搜三部曲 第二部,就已经讲了,因为终止条件的两种写法, 直接决定了两种不一样的递归写法。 + +这里还有细节: + +看上面两个版本的写法中, 好像没有发现回溯的逻辑。 + +我们都知道,有递归就有回溯,回溯就在递归函数的下面, 那么之前我们做的dfs题目,都需要回溯操作,例如:[0098.所有可达路径](./0098.所有可达路径), **为什么本题就没有回溯呢?** + +代码中可以看到dfs函数下面并没有回溯的操作。 + +此时就要在思考本题的要求了,本题是需要判断 1节点 是否能到所有节点,那么我们就没有必要回溯去撤销操作了,只要遍历过的节点一律都标记上。 + +**那什么时候需要回溯操作呢?** + +当我们需要搜索一条可行路径的时候,就需要回溯操作了,因为没有回溯,就没法“调头”, 如果不理解的话,去看我写的 [0098.所有可达路径](./0098.所有可达路径.md) 的题解。 + + +以上分析完毕,DFS整体实现C++代码如下: + +```CPP +// 写法一:dfs 处理当前访问的节点 +#include +#include +#include +using namespace std; + +void dfs(const vector>& graph, int key, vector& visited) { + if (visited[key]) { + return; + } + visited[key] = true; + list keys = graph[key]; + for (int key : keys) { + // 深度优先搜索遍历 + dfs(graph, key, visited); + } +} + +int main() { + int n, m, s, t; + cin >> n >> m; + + // 节点编号从1到n,所以申请 n+1 这么大的数组 + vector> graph(n + 1); // 邻接表 + while (m--) { + cin >> s >> t; + // 使用邻接表 ,表示 s -> t 是相连的 + graph[s].push_back(t); + } + vector visited(n + 1, false); + dfs(graph, 1, visited); + //检查是否都访问到了 + for (int i = 1; i <= n; i++) { + if (visited[i] == false) { + cout << -1 << endl; + return 0; + } + } + cout << 1 << endl; +} + +``` + +**第二种写法注意有注释的地方是和写法一的区别** + +```c++ +写法二:dfs处理下一个要访问的节点 +#include +#include +#include +using namespace std; + +void dfs(const vector>& graph, int key, vector& visited) { + list keys = rooms[key]; + for (int key : keys) { + if (visited[key] == false) { // 确认下一个是没访问过的节点 + visited[key] = true; + dfs(rooms, key, visited); + } + } +} + +int main() { + int n, m, s, t; + cin >> n >> m; + + vector> graph(n + 1); + while (m--) { + cin >> s >> t; + graph[s].push_back(t); + + } + vector visited(n + 1, false); + + visited[0] = true; // 节点1 预先处理 + dfs(graph, 1, visited); + + for (int i = 1; i <= n; i++) { + if (visited[i] == false) { + cout << -1 << endl; + return 0; + } + } + cout << 1 << endl; +} + +``` + +本题我也给出 BFS C++代码,[BFS理论基础](https://programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E6%B7%B1%E6%90%9C%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html),代码如下: + +```CPP +#include +#include +#include +#include +using namespace std; + +int main() { + int n, m, s, t; + cin >> n >> m; + + vector> graph(n + 1); + while (m--) { + cin >> s >> t; + graph[s].push_back(t); + + } + vector visited(n + 1, false); + visited[1] = true; // 1 号房间开始 + queue que; + que.push(1); // 1 号房间开始 + + // 广度优先搜索的过程 + while (!que.empty()) { + int key = que.front(); que.pop(); + list keys = graph[key]; + for (int key : keys) { + if (!visited[key]) { + que.push(key); + visited[key] = true; + } + } + } + + for (int i = 1; i <= n; i++) { + if (visited[i] == false) { + cout << -1 << endl; + return 0; + } + } + cout << 1 << endl; +} + +``` + diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" new file mode 100644 index 0000000000..4a81922d43 --- /dev/null +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -0,0 +1,154 @@ + +# 106. 岛屿的周长 + +题目描述 + +给定一个由 1(陆地)和 0(水)组成的矩阵,岛屿是被水包围,并且通过水平方向或垂直方向上相邻的陆地连接而成的。 + + +你可以假设矩阵外均被水包围。在矩阵中恰好拥有一个岛屿,假设组成岛屿的陆地边长都为 1,请计算岛屿的周长。岛屿内部没有水域。 + +输入描述 + +第一行包含两个整数 N, M,表示矩阵的行数和列数。之后 N 行,每行包含 M 个数字,数字为 1 或者 0,表示岛屿的单元格。 + +输出描述 + +输出一个整数,表示岛屿的周长。 + +输入示例 + +``` +5 5 +0 0 0 0 0 +0 1 0 1 0 +0 1 1 1 0 +0 1 1 1 0 +0 0 0 0 0 +``` + +输出示例 + +14 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524115244.png) + +岛屿的周长为 14。 + +数据范围: + +1 <= M, N <= 50。 + +## 思路 + +岛屿问题最容易让人想到BFS或者DFS,但本题确实还用不上。 + +为了避免大家惯性思维,所以给大家安排了这道题目。 + +### 解法一: + +遍历每一个空格,遇到岛屿则计算其上下左右的空格情况。 + +如果该陆地上下左右的空格是有水域,则说明是一条边,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524115933.png) + +陆地的右边空格是水域,则说明找到一条边。 + + +如果该陆地上下左右的空格出界了,则说明是一条边,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120105.png) + +该录友的下边空格出界了,则说明找到一条边。 + + +C++代码如下:(详细注释) + +```CPP +#include +#include +using namespace std; +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + int direction[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) { + for (int k = 0; k < 4; k++) { // 上下左右四个方向 + int x = i + direction[k][0]; + int y = j + direction[k][1]; // 计算周边坐标x,y + if (x < 0 // x在边界上 + || x >= grid.size() // x在边界上 + || y < 0 // y在边界上 + || y >= grid[0].size() // y在边界上 + || grid[x][y] == 0) { // x,y位置是水域 + result++; + } + } + } + } + } + cout << result << endl; + +} +``` + +### 解法二: + +计算出总的岛屿数量,总的变数为:岛屿数量 * 4 + +因为有一对相邻两个陆地,边的总数就要减2,如图,有两个陆地相邻,总变数就要减2 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120855.png) + + +那么只需要在计算出相邻岛屿的数量就可以了,相邻岛屿数量为cover。 + +结果 result = 岛屿数量 * 4 - cover * 2; + + +C++代码如下:(详细注释) + +```CPP +#include +#include +using namespace std; +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + int sum = 0; // 陆地数量 + int cover = 0; // 相邻数量 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) { + sum++; // 统计总的陆地数量 + // 统计上边相邻陆地 + if(i - 1 >= 0 && grid[i - 1][j] == 1) cover++; + // 统计左边相邻陆地 + if(j - 1 >= 0 && grid[i][j - 1] == 1) cover++; + // 为什么没统计下边和右边? 因为避免重复计算 + } + } + } + + cout << sum * 4 - cover * 2 << endl; + +} +``` diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" index 92b3581d53..65816bb1ec 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" @@ -5,6 +5,12 @@ 至此算上本篇,一共30篇文章,图论之旅就在此收官了。 +在[0098.所有可达路径](./0098.所有可达路径.md) ,我们接触了两种图的存储方式,邻接表和邻接矩阵,数量掌握两种图的存储方式很重要。 + +这也是大家习惯在核心代码模式下刷题 经常忽略的 知识点。因为在力扣上刷题不需要掌握图的存储方式。 + + + ## 深搜与广搜 @@ -14,11 +20,30 @@ * 代码模板:需要熟练掌握深搜和广搜的基本写法。 * 应用场景:图论题目基本上可以即用深搜也可以广搜,无疑是用哪个方便而已 -深搜注意事项 同样是深搜模板题,会有两种写法, -广搜注意事项 + +在[0099.岛屿的数量深搜.md](./0099.岛屿的数量深搜.md) 和 [0105.有向图的完全可达性](./0105.有向图的完全可达性.md),涉及到dfs的两种写法。 + +我们对dfs函数的定义是 是处理当前节点 还是处理下一个节点 很重要,决定了两种dfs的写法。 + +这也是为什么很多录友看到不同的dfs写法,结果发现提交都能过的原因。 + +而深搜还有细节,有的深搜题目需要回溯,有的就不用回溯, + +需要计算路径的问题,一般需要回溯,如果只是染色问题 就不需要回溯。 + +例如: [0105.有向图的完全可达性](./0105.有向图的完全可达性.md) 深搜就不需要回溯,而 [0098.所有可达路径](./0098.所有可达路径.md) 中的递归就需要回溯,文章中都有详细讲解 + + + +注意:以上说的是不需要回溯,不是没有回溯,只要有递归就会有回溯,只是我们是否需要用到回溯这个过程 才是要考虑的 + + +广搜注意事项,很多录友写广搜超时了。 + +深搜和广搜是图论的基础,也有很多变形,我在图论里用最大岛屿问题,讲了很多 ## 并查集 From 6ef77329949055714d0d263ff762e898ac38233d Mon Sep 17 00:00:00 2001 From: Nicolas Leigh Date: Sun, 26 May 2024 11:07:12 +0800 Subject: [PATCH 1140/1533] =?UTF-8?q?Add=20JavaScript=20solution=20to=20th?= =?UTF-8?q?e=20problem=201020.=E9=A3=9E=E5=9C=B0=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\232\204\346\225\260\351\207\217.md" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 59610c6801..5f1995a5ce 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -605,6 +605,63 @@ func bfs(grid [][]int, i, j int) { } ``` +### JavaScript + +```js +/** + * @param {number[][]} grid + * @return {number} + */ +var numEnclaves = function (grid) { + let row = grid.length; + let col = grid[0].length; + let count = 0; + + // Check the first and last row, if there is a 1, then change all the connected 1s to 0 and don't count them. + for (let j = 0; j < col; j++) { + if (grid[0][j] === 1) { + dfs(0, j, false); + } + if (grid[row - 1][j] === 1) { + dfs(row - 1, j, false); + } + } + + // Check the first and last column, if there is a 1, then change all the connected 1s to 0 and don't count them. + for (let i = 0; i < row; i++) { + if (grid[i][0] === 1) { + dfs(i, 0, false); + } + if (grid[i][col - 1] === 1) { + dfs(i, col - 1, false); + } + } + + // Check the rest of the grid, if there is a 1, then change all the connected 1s to 0 and count them. + for (let i = 1; i < row - 1; i++) { + for (let j = 1; j < col - 1; j++) { + dfs(i, j, true); + } + } + + function dfs(i, j, isCounting) { + let condition = i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] === 0; + + if (condition) return; + if (isCounting) count++; + + grid[i][j] = 0; + + dfs(i - 1, j, isCounting); + dfs(i + 1, j, isCounting); + dfs(i, j - 1, isCounting); + dfs(i, j + 1, isCounting); + } + + return count; +}; +``` + ### Rust dfs: @@ -700,3 +757,4 @@ impl Solution { + From e3e96203b74e036492d6c2984ca8d82344b9f585 Mon Sep 17 00:00:00 2001 From: liao junwu Date: Sun, 26 May 2024 11:31:28 +0800 Subject: [PATCH 1141/1533] [0018 fourSum]add C version add C version for 0018: fourSum problem Signed-off-by: liao junwu --- ...33\346\225\260\344\271\213\345\222\214.md" | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 89bc2a8bf5..9c8bb4fe24 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -151,6 +151,96 @@ if (nums[k] + nums[i] > target && nums[i] >= 0) { ## 其他语言版本 +### C: + +```C +/* qsort */ +static int cmp(const void* arg1, const void* arg2) { + int a = *(int *)arg1; + int b = *(int *)arg2; + return (a > b); +} + +int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes) { + + /* 对nums数组进行排序 */ + qsort(nums, numsSize, sizeof(int), cmp); + + int **res = (int **)malloc(sizeof(int *) * 40000); + int index = 0; + + /* k */ + for (int k = 0; k < numsSize - 3; k++) { /* 第一级 */ + + /* k剪枝 */ + if ((nums[k] > target) && (nums[k] >= 0)) { + break; + } + /* k去重 */ + if ((k > 0) && (nums[k] == nums[k - 1])) { + continue; + } + + /* i */ + for (int i = k + 1; i < numsSize - 2; i++) { /* 第二级 */ + + /* i剪枝 */ + if ((nums[k] + nums[i] > target) && (nums[i] >= 0)) { + break; + } + /* i去重 */ + if ((i > (k + 1)) && (nums[i] == nums[i - 1])) { + continue; + } + + /* left and right */ + int left = i + 1; + int right = numsSize - 1; + + while (left < right) { + + /* 防止大数溢出 */ + long long val = (long long)nums[k] + nums[i] + nums[left] + nums[right]; + if (val > target) { + right--; + } else if (val < target) { + left++; + } else { + int *res_tmp = (int *)malloc(sizeof(int) * 4); + res_tmp[0] = nums[k]; + res_tmp[1] = nums[i]; + res_tmp[2] = nums[left]; + res_tmp[3] = nums[right]; + res[index++] = res_tmp; + + /* right去重 */ + while ((right > left) && (nums[right] == nums[right - 1])) { + right--; + } + /* left去重 */ + while ((left < right) && (nums[left] == nums[left + 1])) { + left++; + } + + /* 更新right与left */ + left++, right--; + } + } + } + } + + /* 返回值处理 */ + *returnSize = index; + + int *column = (int *)malloc(sizeof(int) * index); + for (int i = 0; i < index; i++) { + column[i] = 4; + } + *returnColumnSizes = column; + return res; +} +``` + ### Java: ```Java From 692f63a77e55a2b1141b4eac78634db306eb4ec9 Mon Sep 17 00:00:00 2001 From: alanx15a2 Date: Thu, 30 May 2024 12:00:12 +0800 Subject: [PATCH 1142/1533] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86=20add=20php=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\275\222\351\201\215\345\216\206.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index a1d49e985e..f2a97f4de0 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -671,6 +671,62 @@ public void Traversal(TreeNode cur, IList res) } ``` +### PHP +```php +// 144.前序遍历 +function preorderTraversal($root) { + $output = []; + $this->traversal($root, $output); + return $output; +} + +function traversal($root, array &$output) { + if ($root->val === null) { + return; + } + + $output[] = $root->val; + $this->traversal($root->left, $output); + $this->traversal($root->right, $output); +} +``` +```php +// 94.中序遍历 +function inorderTraversal($root) { + $output = []; + $this->traversal($root, $output); + return $output; +} + +function traversal($root, array &$output) { + if ($root->val === null) { + return; + } + + $this->traversal($root->left, $output); + $output[] = $root->val; + $this->traversal($root->right, $output); +} +``` +```php +// 145.后序遍历 +function postorderTraversal($root) { + $output = []; + $this->traversal($root, $output); + return $output; +} + +function traversal($root, array &$output) { + if ($root->val === null) { + return; + } + + $this->traversal($root->left, $output); + $this->traversal($root->right, $output); + $output[] = $root->val; +} +``` +

From 5fabc5c1e09f0372bfedbc5c92bb2ccbd73cf8da Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 31 May 2024 10:12:41 +0800 Subject: [PATCH 1143/1533] Update --- ...27\344\275\231\350\277\236\346\216\245.md" | 2 +- ...77\347\232\204\345\221\250\351\225\277.md" | 6 +- ...50\347\232\204\350\267\257\345\276\204.md" | 155 +++++++++++ ...27\344\275\231\350\277\236\346\216\245.md" | 128 ++++++++++ ...\344\275\231\350\277\236\346\216\245II.md" | 240 ++++++++++++++++++ ...46\344\270\262\346\216\245\351\276\231.md" | 149 +++++++++++ ...57\344\273\266\346\236\204\345\273\272.md" | 0 ...72\346\200\273\347\273\223\347\257\207.md" | 56 +++- ...06\350\256\272\345\237\272\347\241\200.md" | 3 +- 9 files changed, 720 insertions(+), 19 deletions(-) create mode 100644 "problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" create mode 100644 "problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" create mode 100644 "problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" create mode 100644 "problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" rename "problems/kamacoder/00.\350\275\257\344\273\266\346\236\204\345\273\272.md" => "problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" (100%) diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 6d5f2bc4fe..7808549036 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -75,7 +75,7 @@ void join(int u, int v) { 2. 将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上 3. 判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点 -如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html) +如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/kamacoder/图论并查集理论基础.html) 我们再来看一下这道题目。 diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 4a81922d43..4e285a9eec 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -1,6 +1,8 @@ # 106. 岛屿的周长 +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1178) + 题目描述 给定一个由 1(陆地)和 0(水)组成的矩阵,岛屿是被水包围,并且通过水平方向或垂直方向上相邻的陆地连接而成的。 @@ -108,16 +110,14 @@ int main() { 计算出总的岛屿数量,总的变数为:岛屿数量 * 4 -因为有一对相邻两个陆地,边的总数就要减2,如图,有两个陆地相邻,总变数就要减2 +因为有一对相邻两个陆地,边的总数就要减2,如图红线部分,有两个陆地相邻,总边数就要减2 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120855.png) - 那么只需要在计算出相邻岛屿的数量就可以了,相邻岛屿数量为cover。 结果 result = 岛屿数量 * 4 - cover * 2; - C++代码如下:(详细注释) ```CPP diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" new file mode 100644 index 0000000000..d769518098 --- /dev/null +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -0,0 +1,155 @@ + +# 107. 寻找存在的路径 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1179) + +题目描述 + +给定一个包含 n 个节点的无向图中,节点编号从 1 到 n (含 1 和 n )。 + +你的任务是判断是否有一条从节点 source 出发到节点 destination 的路径存在。 + +输入描述 + +第一行包含两个正整数 N 和 M,N 代表节点的个数,M 代表边的个数。  + +后续 M 行,每行两个正整数 s 和 t,代表从节点 s 与节点 t 之间有一条边。 + +最后一行包含两个正整数,代表起始节点 source 和目标节点 destination。 + +输出描述 + +输出一个整数,代表是否存在从节点 source 到节点 destination 的路径。如果存在,输出 1;否则,输出 0。 + +输入示例 + +``` +5 4 +1 2 +1 3 +2 4 +3 4 +1 4 +``` + +输出示例 + +1 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527104432.png) + +数据范围: + +1 <= M, N <= 100。 + +## 思路 + +本题是并查集基础题目。 如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/kamacoder/图论并查集理论基础.html) + +并查集可以解决什么问题呢? + +主要就是集合问题,**两个节点在不在一个集合,也可以将两个节点添加到一个集合中**。 + +这里整理出我的并查集模板如下: + +```CPP +int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好 +vector father = vector (n, 0); // C++里的一种数组结构 + +// 并查集初始化 +void init() { + for (int i = 0; i < n; ++i) { + father[i] = i; + } +} +// 并查集里寻根的过程 +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 +} + +// 判断 u 和 v是否找到同一个根 +bool isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} + +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} +``` + +以上模板中,只要修改 n 大小就可以。 + +并查集主要有三个功能: + +1. 寻找根节点,函数:find(int u),也就是判断这个节点的祖先节点是哪个 +2. 将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上 +3. 判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点 + +简单介绍并查集之后,我们再来看一下这道题目。 + +为什么说这道题目是并查集基础题目,题目中各个点是双向图链接,那么判断 一个顶点到另一个顶点有没有有效路径其实就是看这两个顶点是否在同一个集合里。 + +如何算是同一个集合呢,有边连在一起,就算是一个集合。 + +此时我们就可以直接套用并查集模板。 + +使用 join(int u, int v)将每条边加入到并查集。 + +最后 isSame(int u, int v) 判断是否是同一个根 就可以了。 + +C++代码如下: + +```CPP +#include +#include +using namespace std; + +int n; // 节点数量 +vector father = vector (101, 0); // 按照节点大小定义数组大小 + +// 并查集初始化 +void init() { + for (int i = 1; i <= n; i++) father[i] = i; +} +// 并查集里寻根的过程 +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); +} + +// 判断 u 和 v是否找到同一个根 +bool isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} + +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} + +int main() { + int m, s, t, source, destination; + cin >> n >> m; + init(); + while (m--) { + cin >> s >> t; + join(s, t); + } + cin >> source >> destination; + if (isSame(source, destination)) cout << 1 << endl; + else cout << 0 << endl; +} +``` + diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" new file mode 100644 index 0000000000..de0a0c6fca --- /dev/null +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -0,0 +1,128 @@ + +# 108. 冗余连接 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1181) + +题目描述 + +树可以看成是一个图(拥有 n 个节点和 n - 1 条边的连通无环无向图)。 + +现给定一个拥有 n 个节点(节点编号从 1 到 n)和 n 条边的连通无向图,请找出一条可以删除的边,删除后图可以变成一棵树。 + +输入描述 + +第一行包含一个整数 N,表示图的节点个数和边的个数。 + +后续 N 行,每行包含两个整数 s 和 t,表示图中 s 和 t 之间有一条边。 + +输出描述 + +输出一条可以删除的边。如果有多个答案,请删除标准输入中最后出现的那条边。 + +输入示例 + +``` +3 +1 2 +2 3 +1 3 +``` + +输出示例 + +1 3 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527110320.png) + +图中的 1 2,2 3,1 3 等三条边在删除后都能使原图变为一棵合法的树。但是 1 3 由于是标准输出里最后出现的那条边,所以输出结果为 1 3 + +数据范围: + +1 <= N <= 1000. + +## 思路 + +这道题目也是并查集基础题目。 + +这里我依然降调一下,并查集可以解决什么问题:两个节点是否在一个集合,也可以将两个节点添加到一个集合中。 + +如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html) + +我们再来看一下这道题目。 + +题目说是无向图,返回一条可以删去的边,使得结果图是一个有着N个节点的树(即:只有一个根节点)。 + +如果有多个答案,则返回二维数组中最后出现的边。 + +那么我们就可以从前向后遍历每一条边(因为优先让前面的边连上),边的两个节点如果不在同一个集合,就加入集合(即:同一个根节点)。 + +如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104720.png) + +节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 + +如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了。 + +如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104330.png) + +已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 + +这个思路清晰之后,代码就很好写了。 + +并查集C++代码如下: + +```CPP +#include +#include +using namespace std; +int n; // 节点数量 +vector father(1001, 0); // 按照节点大小范围定义数组 + +// 并查集初始化 +void init() { + for (int i = 0; i <= n; ++i) { + father[i] = i; + } +} +// 并查集里寻根的过程 +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); +} +// 判断 u 和 v是否找到同一个根 +bool isSame(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); // 寻找u的根 + v = find(v); // 寻找v的根 + if (u == v) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u; +} + +int main() { + int s, t; + cin >> n; + init(); + for (int i = 0; i < n; i++) { + cin >> s >> t; + if (isSame(s, t)) { + cout << s << " " << t << endl; + return 0; + } else { + join(s, t); + } + } +} +``` + +可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。 + + diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" new file mode 100644 index 0000000000..4f3bbf0a05 --- /dev/null +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -0,0 +1,240 @@ + +# 109. 冗余连接II + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182) + +题目描述 + +有向树指满足以下条件的有向图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。有向树拥有 n 个节点和 n - 1 条边。 + + +输入一个有向图,该图由一个有着 n 个节点(节点编号 从 1 到 n),n 条边,请返回一条可以删除的边,使得删除该条边之后该有向图可以被当作一颗有向树。 + +输入描述 + +第一行输入一个整数 N,表示有向图中节点和边的个数。 + +后续 N 行,每行输入两个整数 s 和 t,代表 s 节点有一条连接 t 节点的单向边 + +输出描述 + +输出一条可以删除的边,若有多条边可以删除,请输出标准输入中最后出现的一条边。 + +输入示例 + +``` +3 +1 2 +1 3 +2 3 +``` + +输出示例 + +2 3 + +提示信息 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527112633.png) + +在删除 2 3 后有向图可以变为一棵合法的有向树,所以输出 2 3 + +数据范围: + +1 <= N <= 1000. + +## 思路 + +本题与 [108.冗余连接](./0108.冗余连接.md) 类似,但本题是一个有向图,有向图相对要复杂一些。 + +本题的本质是 :有一个有向图,是由一颗有向树 + 一条有向边组成的 (所以此时这个图就不能称之为有向树),现在让我们找到那条边 把这条边删了,让这个图恢复为有向树。 + +还有“**若有多条边可以删除,请输出标准输入中最后出现的一条边**”,这说明在两条边都可以删除的情况下,要删顺序靠后的边! + +我们来想一下 有向树的性质,如果是有向树的话,只有根节点入度为0,其他节点入度都为1(因为该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点)。 + +所以情况一:如果我们找到入度为2的点,那么删一条指向该节点的边就行了。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527115807.png) + +找到了节点3 的入度为2,删 1 -> 3 或者 2 -> 3 。选择删顺序靠后便可。 + +但 入度为2 还有一种情况,情况二,只能删特定的一条边,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527151456.png) + +节点3 的入度为 2,但在删除边的时候,只能删 这条边(节点1 -> 节点3),如果删这条边(节点4 -> 节点3),那么删后本图也不是有向树了(因为找不到根节点)。 + +综上,如果发现入度为2的节点,我们需要判断 删除哪一条边,删除后本图能成为有向树。如果是删哪个都可以,优先删顺序靠后的边。 + + +情况三: 如果没有入度为2的点,说明 图中有环了(注意是有向环)。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527120531.png) + +对于情况二,删掉构成环的边就可以了。 + + +## 写代码 + +把每条边记录下来,并统计节点入度: + +```cpp + int s, t; + vector> edges; + cin >> n; + vector inDegree(n + 1, 0); // 记录节点入度 + for (int i = 0; i < n; i++) { + cin >> s >> t; + inDegree[t]++; + edges.push_back({s, t}); + } + + +``` + +前两种入度为2的情况,一定是删除指向入度为2的节点的两条边其中的一条,如果删了一条,判断这个图是一个树,那么这条边就是答案。 + +同时注意要从后向前遍历,因为如果两条边删哪一条都可以成为树,就删最后那一条。 + +代码如下: + +```cpp +vector vec; // 记录入度为2的边(如果有的话就两条边) +// 找入度为2的节点所对应的边,注意要倒序,因为优先删除最后出现的一条边 +for (int i = n - 1; i >= 0; i--) { + if (inDegree[edges[i][1]] == 2) { + vec.push_back(i); + } +} +if (vec.size() > 0) { + // 放在vec里的边已经按照倒叙放的,所以这里就优先删vec[0]这条边 + if (isTreeAfterRemoveEdge(edges, vec[0])) { + cout << edges[vec[0]][0] << " " << edges[vec[0]][1]; + } else { + cout << edges[vec[1]][0] << " " << edges[vec[1]][1]; + } + return 0; +} +``` + +再来看情况三,明确没有入度为2的情况,那么一定有向环,找到构成环的边就是要删除的边。 + +可以定义一个函数,代码如下: + +```cpp +// 在有向图里找到删除的那条边,使其变成树 +void getRemoveEdge(const vector>& edges) +``` + +大家应该知道了,我们要解决本题要实现两个最为关键的函数: + +* `isTreeAfterRemoveEdge()` 判断删一个边之后是不是有向树 +* `getRemoveEdge()` 确定图中一定有了有向环,那么要找到需要删除的那条边 + +此时就用到**并查集**了。 + +如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/kamacoder/图论并查集理论基础.html) + +`isTreeAfterRemoveEdge()` 判断删一个边之后是不是有向树: 将所有边的两端节点分别加入并查集,遇到要 要删除的边则跳过,如果遇到即将加入并查集的边的两端节点 本来就在并查集了,说明构成了环。 + +如果顺利将所有边的两端节点(除了要删除的边)加入了并查集,则说明 删除该条边 还是一个有向树 + +`getRemoveEdge()`确定图中一定有了有向环,那么要找到需要删除的那条边: 将所有边的两端节点分别加入并查集,如果遇到即将加入并查集的边的两端节点 本来就在并查集了,说明构成了环。 + +本题C++代码如下:(详细注释了) + + +```cpp +#include +#include +using namespace std; +int n; +vector father (1001, 0); +// 并查集初始化 +void init() { + for (int i = 1; i <= n; ++i) { + father[i] = i; + } +} +// 并查集里寻根的过程 +int find(int u) { + return u == father[u] ? u : father[u] = find(father[u]); +} +// 将v->u 这条边加入并查集 +void join(int u, int v) { + u = find(u); + v = find(v); + if (u == v) return ; + father[v] = u; +} +// 判断 u 和 v是否找到同一个根 +bool same(int u, int v) { + u = find(u); + v = find(v); + return u == v; +} + +// 在有向图里找到删除的那条边,使其变成树 +void getRemoveEdge(const vector>& edges) { + init(); // 初始化并查集 + for (int i = 0; i < n; i++) { // 遍历所有的边 + if (same(edges[i][0], edges[i][1])) { // 构成有向环了,就是要删除的边 + cout << edges[i][0] << " " << edges[i][1]; + return; + } else { + join(edges[i][0], edges[i][1]); + } + } +} + +// 删一条边之后判断是不是树 +bool isTreeAfterRemoveEdge(const vector>& edges, int deleteEdge) { + init(); // 初始化并查集 + for (int i = 0; i < n; i++) { + if (i == deleteEdge) continue; + if (same(edges[i][0], edges[i][1])) { // 构成有向环了,一定不是树 + return false; + } + join(edges[i][0], edges[i][1]); + } + return true; +} + +int main() { + int s, t; + vector> edges; + cin >> n; + vector inDegree(n + 1, 0); // 记录节点入度 + for (int i = 0; i < n; i++) { + cin >> s >> t; + inDegree[t]++; + edges.push_back({s, t}); + } + + vector vec; // 记录入度为2的边(如果有的话就两条边) + // 找入度为2的节点所对应的边,注意要倒序,因为优先删除最后出现的一条边 + for (int i = n - 1; i >= 0; i--) { + if (inDegree[edges[i][1]] == 2) { + vec.push_back(i); + } + } + if (vec.size() > 0) { + // 放在vec里的边已经按照倒叙放的,所以这里就优先删vec[0]这条边 + if (isTreeAfterRemoveEdge(edges, vec[0])) { + cout << edges[vec[0]][0] << " " << edges[vec[0]][1]; + } else { + cout << edges[vec[1]][0] << " " << edges[vec[1]][1]; + } + return 0; + } + + // 处理情况三 + // 明确没有入度为2的情况,那么一定有有向环,找到构成环的边返回就可以了 + getRemoveEdge(edges); +} +``` diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" new file mode 100644 index 0000000000..81c1a33dca --- /dev/null +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -0,0 +1,149 @@ + +# 110. 字符串接龙 + +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182) + +题目描述 + +字典 strList 中从字符串 beginStr 和 endStr 的转换序列是一个按下述规格形成的序列: + +1. 序列中第一个字符串是 beginStr。 + +2. 序列中最后一个字符串是 endStr。 + +3. 每次转换只能改变一个字符。 + +4. 转换过程中的中间字符串必须是字典 strList 中的字符串。 + +给你两个字符串 beginStr 和 endStr 和一个字典 strList,找到从 beginStr 到 endStr 的最短转换序列中的字符串数目。如果不存在这样的转换序列,返回 0。 + +输入描述 + +第一行包含一个整数 N,表示字典 strList 中的字符串数量。 第二行包含两个字符串,用空格隔开,分别代表 beginStr 和 endStr。 后续 N 行,每行一个字符串,代表 strList 中的字符串。 + +输出描述 + +输出一个整数,代表从 beginStr 转换到 endStr 需要的最短转换序列中的字符串数量。如果不存在这样的转换序列,则输出 0。 + +输入示例 + +``` +6 +abc def +efc +dbc +ebc +dec +dfc +yhn +``` + +输出示例 + +4 + +提示信息 + +从 startStr 到 endStr,在 strList 中最短的路径为 abc -> dbc -> dec -> def,所以输出结果为 4 + +数据范围: + +2 <= N <= 500 + +

+ +

+ + +## 思路 + +以示例1为例,从这个图中可以看出 abc 到 def的路线 不止一条,但最短的一条路径上是4个节点。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240529121038.png) + +本题只需要求出最短路径的长度就可以了,不用找出具体路径。 + +所以这道题要解决两个问题: + +* 图中的线是如何连在一起的 +* 起点和终点的最短路径长度 + +首先题目中并没有给出点与点之间的连线,而是要我们自己去连,条件是字符只能差一个。 + +所以判断点与点之间的关系,需要判断是不是差一个字符,**如果差一个字符,那就是有链接**。 + +然后就是求起点和终点的最短路径长度,**这里无向图求最短路,广搜最为合适,广搜只要搜到了终点,那么一定是最短的路径**。因为广搜就是以起点中心向四周扩散的搜索。 + +**本题如果用深搜,会比较麻烦,要在到达终点的不同路径中选则一条最短路**。 而广搜只要达到终点,一定是最短路。 + +另外需要有一个注意点: + +* 本题是一个无向图,需要用标记位,标记着节点是否走过,否则就会死循环! +* 使用set来检查字符串是否出现在字符串集合里更快一些 + +C++代码如下:(详细注释) + +```CPP +#include +#include +#include +#include +#include +#include +using namespace std; +int main() { + string beginStr, endStr, str; + int n; + cin >> n; + unordered_set strSet; + cin >> beginStr >> endStr; + for (int i = 0; i < n; i++) { + cin >> str; + strSet.insert(str); + } + + // 记录strSet里的字符串是否被访问过,同时记录路径长度 + unordered_map visitMap; // <记录的字符串,路径长度> + + // 初始化队列 + queue que; + que.push(beginStr); + + // 初始化visitMap + visitMap.insert(pair(beginStr, 1)); + + while(!que.empty()) { + string word = que.front(); + que.pop(); + int path = visitMap[word]; // 这个字符串在路径中的长度 + + // 开始在这个str中,挨个字符去替换 + for (int i = 0; i < word.size(); i++) { + string newWord = word; // 用一个新字符串替换str,因为每次要置换一个字符 + + // 遍历26的字母 + for (int j = 0 ; j < 26; j++) { + newWord[i] = j + 'a'; + if (newWord == endStr) { // 发现替换字母后,字符串与终点字符串相同 + cout << path + 1 << endl; // 找到了路径 + return 0; + } + // 字符串集合里出现了newWord,并且newWord没有被访问过 + if (strSet.find(newWord) != strSet.end() + && visitMap.find(newWord) == visitMap.end()) { + // 添加访问信息,并将新字符串放到队列中 + visitMap.insert(pair(newWord, path + 1)); + que.push(newWord); + } + } + } + } + + // 没找到输出0 + cout << 0 << endl; + +} +``` + +当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。 + diff --git "a/problems/kamacoder/00.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" similarity index 100% rename from "problems/kamacoder/00.\350\275\257\344\273\266\346\236\204\345\273\272.md" rename to "problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" index 65816bb1ec..aa360bab1d 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" @@ -3,50 +3,78 @@ 从深搜广搜 到并查集,从最小生成树到拓扑排序, 最后是最短路算法系列。 -至此算上本篇,一共30篇文章,图论之旅就在此收官了。 +至此算上本篇,一共32篇文章,图论之旅就在此收官了。 -在[0098.所有可达路径](./0098.所有可达路径.md) ,我们接触了两种图的存储方式,邻接表和邻接矩阵,数量掌握两种图的存储方式很重要。 - -这也是大家习惯在核心代码模式下刷题 经常忽略的 知识点。因为在力扣上刷题不需要掌握图的存储方式。 +在[0098.所有可达路径](./0098.所有可达路径.md) ,我们接触了两种图的存储方式,邻接表和邻接矩阵,掌握两种图的存储方式很重要。 +图的存储方式也是大家习惯在核心代码模式下刷题 经常忽略的 知识点。因为在力扣上刷题不需要掌握图的存储方式。 +## 深搜与广搜 +在二叉树章节中,其实我们讲过了 深搜和广搜在二叉树上的搜索过程。 -## 深搜与广搜 +在图论章节中,深搜与广搜就是在图这个数据结构上的搜索过程。 深搜与广搜是图论里基本的搜索方法,大家需要掌握三点: * 搜索方式:深搜是可一个方向搜,不到黄河不回头。 广搜是围绕这起点一圈一圈的去搜。 * 代码模板:需要熟练掌握深搜和广搜的基本写法。 -* 应用场景:图论题目基本上可以即用深搜也可以广搜,无疑是用哪个方便而已 - +* 应用场景:图论题目基本上可以即用深搜也可用广搜,无疑是用哪个方便而已 -同样是深搜模板题,会有两种写法, +### 注意事项 +需要注意的是,同样是深搜模板题,会有两种写法。 在[0099.岛屿的数量深搜.md](./0099.岛屿的数量深搜.md) 和 [0105.有向图的完全可达性](./0105.有向图的完全可达性.md),涉及到dfs的两种写法。 -我们对dfs函数的定义是 是处理当前节点 还是处理下一个节点 很重要,决定了两种dfs的写法。 +**我们对dfs函数的定义是 是处理当前节点 还是处理下一个节点 很重要**,决定了两种dfs的写法。 这也是为什么很多录友看到不同的dfs写法,结果发现提交都能过的原因。 -而深搜还有细节,有的深搜题目需要回溯,有的就不用回溯, +而深搜还有细节,有的深搜题目需要用到回溯的过程,有的就不用回溯的过程, -需要计算路径的问题,一般需要回溯,如果只是染色问题 就不需要回溯。 +一般是需要计算路径的问题 需要回溯,如果只是染色问题(岛屿问题系列) 就不需要回溯。 例如: [0105.有向图的完全可达性](./0105.有向图的完全可达性.md) 深搜就不需要回溯,而 [0098.所有可达路径](./0098.所有可达路径.md) 中的递归就需要回溯,文章中都有详细讲解 +注意:以上说的是不需要回溯,不是没有回溯,只要有递归就会有回溯,只是我们是否需要用到回溯这个过程,这是需要考虑的。 +很多录友写出来的广搜可能超时了, 例如题目:[0099.岛屿的数量广搜](./0099.岛屿的数量广搜.md) -注意:以上说的是不需要回溯,不是没有回溯,只要有递归就会有回溯,只是我们是否需要用到回溯这个过程 才是要考虑的 +根本原因是**只要 加入队列就代表 走过,就需要标记,而不是从队列拿出来的时候再去标记走过**。 +具体原因,我在[0099.岛屿的数量广搜](./0099.岛屿的数量广搜.md) 中详细讲了。 -广搜注意事项,很多录友写广搜超时了。 +在深搜与广搜的讲解中,为了防止惯性思维,我特别加入了题目 [0106.岛屿的周长](./0106.岛屿的周长.md),提醒大家,看到类似的题目,也不要上来就想着深搜和广搜。 -深搜和广搜是图论的基础,也有很多变形,我在图论里用最大岛屿问题,讲了很多 +还有一些图的问题,在题目描述中,是没有图的,需要我们自己构建一个图,例如 [0110.字符串接龙](./0110.字符串接龙.md),题目中连线都没有,需要我们自己去思考 什么样的两个字符串可以连成线。 ## 并查集 +并查集相对来说是比较复杂的数据结构,其实他的代码不长,但想彻底学透并查集,需要从多个维度入手, + +我在理论基础篇的时候 讲解如下重点: + +* 为什么要用并查集,怎么不用个二维数据,或者set、map之类的。 +* 并查集能解决那些问题,哪些场景会用到并查集 +* 并查集原理以及代码实现 +* 并查集写法的常见误区 +* 带大家去模拟一遍并查集的过程 +* 路径压缩的过程 +* 时间复杂度分析 + +上面这几个维度 大家都去思考了,并查集基本就学明白了。 + +其实理论基础篇就算是给大家出了一道裸的并查集题目了,所以在后面的题目安排中,会稍稍的拔高一些,重点在于并查集的应用上。 + + + +[0108.冗余连接](./0108.冗余连接.md), [0109.冗余连接II](./0109.冗余连接II.md) + +后面的两道题目,[0108.冗余连接](./0108.冗余连接.md) 和 + + + ## 最小生成树 ## 拓扑排序 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" index be42a5fc7c..18d63e2d7b 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -124,7 +124,8 @@ 一般使用邻接表、邻接矩阵 或者用类来表示。 -主流是 邻接表和邻接矩阵。 +主要是 朴素存储、邻接表和邻接矩阵。 + ### 邻接矩阵 From c3753198fefae41ebc5832d2539b86a50bf504ba Mon Sep 17 00:00:00 2001 From: XZY <1214807740@qq.com> Date: Sun, 2 Jun 2024 16:18:34 +0800 Subject: [PATCH 1144/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200055.=E5=8F=B3?= =?UTF-8?q?=E6=97=8B=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20JavaScript=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\255\227\347\254\246\344\270\262.md" | 65 ++++++++++++------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 4dea19a832..6f176a573a 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -298,33 +298,50 @@ int main() ```javascript // JS中字符串内不可单独修改 -// 右旋转 -function reverseLeftWords(s, k) { - const reverse = (sList, start, end) => { - for (let i = start, j = end; i < j; i++, j--) { - [sList[i], sList[j]] = [sList[j], sList[i]]; - } - } - const sList = Array.from(s); - reverse(sList, 0, sList.length - k - 1); - reverse(sList, sList.length - k, sList.length - 1); - reverse(sList, 0, sList.length - 1); - return sList.join(''); +const readline = require('readline') + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) + +const inputs = []; // 存储输入 + +rl.on('line', function(data) { + inputs.push(data); + +}).on('close', function() { + const res = deal(inputs); + // 打印结果 + console.log(res); +}) + +// 对传入的数据进行处理 +function deal(inputs) { + let [k, s] = inputs; + const len = s.length - 1; + k = parseInt(k); + str = s.split(''); + + str = reverseStr(str, 0, len - k) + str = reverseStr(str, len - k + 1, len) + str = reverseStr(str, 0, len) + + return str.join(''); } -// 左旋转 -var reverseLeftWords = function(s, n) { - const reverse = (sList, start, end) => { - for (let i = start, j = end; i < j; i++, j--) { - [sList[i], sList[j]] = [sList[j], sList[i]]; +// 根据提供的范围进行翻转 +function reverseStr(s, start, end) { + + while (start < end) { + [s[start], s[end]] = [s[end], s[start]] + + start++; + end--; } - } - const sList = s.split(''); - reverse(sList, 0, n - 1); - reverse(sList, n, sList.length - 1); - reverse(sList, 0, sList.length - 1); - return sList.join(''); -}; + + return s; +} ``` ### TypeScript: From 081e27db222d0b3775b84bed6e3629d5de8b96b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E7=BE=BD?= <90547641+xCk27x@users.noreply.github.com> Date: Mon, 3 Jun 2024 01:23:58 +0800 Subject: [PATCH 1145/1533] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增TypeScript的一維dp解法 --- ...\345\220\214\350\267\257\345\276\204II.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 6819c19f5e..78507a8483 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -550,6 +550,27 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { }; ``` +// 版本二: dp改為使用一維陣列,從終點開始遍歷 +```typescript +function uniquePathsWithObstacles2(obstacleGrid: number[][]): number { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + + const dp: number[] = new Array(n).fill(0); + dp[n - 1] = 1; + + // 由下而上,右而左進行遍歷 + for (let i = m - 1; i >= 0; i--) { + for (let j = n - 1; j >= 0; j--) { + if (obstacleGrid[i][j] === 1) dp[j] = 0; + else dp[j] = dp[j] + (dp[j + 1] || 0); + } + } + + return dp[0]; +}; +``` + ### Rust ```Rust From 89f9573f63158c46b02d15d2249bc1caa2c6da49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E7=BE=BD?= <90547641+xCk27x@users.noreply.github.com> Date: Mon, 3 Jun 2024 01:29:33 +0800 Subject: [PATCH 1146/1533] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正typescript方法2的函數名稱 --- .../0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 78507a8483..daf3d8c5a1 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -552,7 +552,7 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { // 版本二: dp改為使用一維陣列,從終點開始遍歷 ```typescript -function uniquePathsWithObstacles2(obstacleGrid: number[][]): number { +function uniquePathsWithObstacles(obstacleGrid: number[][]): number { const m = obstacleGrid.length; const n = obstacleGrid[0].length; From f47eaf1e465d42608cd91ac545a0e4766bae07fe Mon Sep 17 00:00:00 2001 From: markwang Date: Mon, 3 Jun 2024 10:51:44 +0800 Subject: [PATCH 1147/1533] =?UTF-8?q?1047.=E5=88=A0=E9=99=A4=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89=E7=9B=B8?= =?UTF-8?q?=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=E5=A2=9E=E5=8A=A0Go?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\207\215\345\244\215\351\241\271.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 4aa0e95478..7232008a2c 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -241,6 +241,33 @@ class Solution: ### Go: +使用栈 +```go +func removeDuplicates(s string) string { + stack := make([]rune, 0) + for _, val := range s { + if len(stack) == 0 || val != stack[len(stack)-1] { + stack = append(stack, val) + } else { + stack = stack[:len(stack)-1] + } + } + var res []rune + for len(stack) != 0 { // 将栈中元素放到result字符串汇总 + res = append(res, stack[len(stack)-1]) + stack = stack[:len(stack)-1] + } + // 此时字符串需要反转一下 + l, r := 0, len(res)-1 + for l < r { + res[l], res[r] = res[r], res[l] + l++ + r-- + } + return string(res) +} +``` +拿字符串直接作为栈,省去了栈还要转为字符串的操作 ```go func removeDuplicates(s string) string { var stack []byte From b5a5340d1a964254325dc4ed64507c3460a9d3b9 Mon Sep 17 00:00:00 2001 From: mengyi Date: Wed, 5 Jun 2024 19:08:31 -0400 Subject: [PATCH 1148/1533] add curly braces and comments to advoid confusion --- ...14\345\210\206\346\237\245\346\211\276.md" | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 5604cd56f3..d86146d63a 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -174,13 +174,17 @@ class Solution { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + ((right - left) >> 1); - if (nums[mid] == target) + if (nums[mid] == target) { return mid; - else if (nums[mid] < target) + } + else if (nums[mid] < target) { left = mid + 1; - else if (nums[mid] > target) + } + else { // nums[mid] > target right = mid - 1; + } } + // 未找到目标值 return -1; } } @@ -194,13 +198,17 @@ class Solution { int left = 0, right = nums.length; while (left < right) { int mid = left + ((right - left) >> 1); - if (nums[mid] == target) + if (nums[mid] == target) { return mid; - else if (nums[mid] < target) + } + else if (nums[mid] < target) { left = mid + 1; - else if (nums[mid] > target) + } + else { // nums[mid] > target right = mid; + } } + // 未找到目标值 return -1; } } From f96b0cf776e3ea6c06bf1e2471a5822437f973c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BA=B7=E5=AE=87?= <746294093@qq.com> Date: Thu, 6 Jun 2024 14:24:52 +0800 Subject: [PATCH 1149/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00053.=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E5=AD=97.md=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 45a0aa5461..ec5fcc63c6 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -369,9 +369,43 @@ main(); ### Scala: - ### PHP: +```php += 0) { + if (is_numeric($s[$oldLen])) { + $s[$newLen--] = 'r'; + $s[$newLen--] = 'e'; + $s[$newLen--] = 'b'; + $s[$newLen--] = 'm'; + $s[$newLen--] = 'u'; + $s[$newLen--] = 'n'; + } else { + $s[$newLen--] = $s[$oldLen]; + } + $oldLen--; +} + +echo $s; +?> +``` + + + ### Rust: @@ -381,4 +415,3 @@ main(); - From 3cba050e372565e3d982940c000f33e476e4924c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BA=B7=E5=AE=87?= <746294093@qq.com> Date: Thu, 6 Jun 2024 16:22:11 +0800 Subject: [PATCH 1150/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00055.=E5=8F=B3?= =?UTF-8?q?=E6=97=8B=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\255\227\347\254\246\344\270\262.md" | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 4dea19a832..1444bcae89 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -23,7 +23,7 @@ ``` 2 abcdefg -``` +``` 样例输出: @@ -336,6 +336,32 @@ var reverseLeftWords = function(s, n) { ### PHP: +```php + +``` + ### Scala: @@ -349,3 +375,4 @@ var reverseLeftWords = function(s, n) { + From 104d4e336d3b5461f6685c75ff0d096168a95515 Mon Sep 17 00:00:00 2001 From: Joey Date: Sun, 9 Jun 2024 20:31:48 +1000 Subject: [PATCH 1151/1533] =?UTF-8?q?Update=2020201010=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 391a463102..5f5f688a13 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -55,7 +55,7 @@ **构造二叉树有三个注意的点:** -* 分割时候,坚持区间不变量原则,左闭右开,或者左闭又闭。 +* 分割时候,坚持区间不变量原则,左闭右开,或者左闭右闭。 * 分割的时候,注意后序 或者 前序已经有一个节点作为中间节点了,不能继续使用了。 * 如何使用切割后的后序数组来切合中序数组?利用中序数组大小一定是和后序数组的大小相同这一特点来进行切割。 From ddc8e2de604813772abe857bd7e7906ef2837fa0 Mon Sep 17 00:00:00 2001 From: mengyi Date: Sun, 9 Jun 2024 21:30:32 -0400 Subject: [PATCH 1152/1533] update project 203 java version solution --- ...76\350\241\250\345\205\203\347\264\240.md" | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index efcc6414fe..64124fbb3c 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -224,9 +224,10 @@ struct ListNode* removeElements(struct ListNode* head, int val){ ### Java: +用原来的链表操作: ```java /** - * 添加虚节点方式 + * 方法1 * 时间复杂度 O(n) * 空间复杂度 O(1) * @param head @@ -234,25 +235,22 @@ struct ListNode* removeElements(struct ListNode* head, int val){ * @return */ public ListNode removeElements(ListNode head, int val) { - if (head == null) { - return head; + while(head!=null && head.val==val) { + head = head.next; } - // 因为删除可能涉及到头节点,所以设置dummy节点,统一操作 - ListNode dummy = new ListNode(-1, head); - ListNode pre = dummy; - ListNode cur = head; - while (cur != null) { - if (cur.val == val) { - pre.next = cur.next; + ListNode curr = head; + while(curr!=null && curr.next !=null) { + if(curr.next.val == val){ + curr.next = curr.next.next; } else { - pre = cur; + curr = curr.next; } - cur = cur.next; } - return dummy.next; + return head; } + /** - * 不添加虚拟节点方式 + * 方法1 * 时间复杂度 O(n) * 空间复杂度 O(1) * @param head @@ -280,8 +278,13 @@ public ListNode removeElements(ListNode head, int val) { } return head; } + +``` + +设置一个虚拟头结点: + +```java /** - * 不添加虚拟节点and pre Node方式 * 时间复杂度 O(n) * 空间复杂度 O(1) * @param head @@ -289,18 +292,21 @@ public ListNode removeElements(ListNode head, int val) { * @return */ public ListNode removeElements(ListNode head, int val) { - while(head!=null && head.val==val){ - head = head.next; - } - ListNode curr = head; - while(curr!=null){ - while(curr.next!=null && curr.next.val == val){ - curr.next = curr.next.next; + // 设置一个虚拟的头结点 + ListNode dummy = new ListNode(); + dummy.next = head; + + ListNode cur = dummy; + while (cur.next != null) { + if (cur.next.val == val) { + cur.next = cur.next.next; + } else { + cur = cur.next; } - curr = curr.next; } - return head; + return dummy.next; } + ``` ### Python: From 4257be7d3175ff459157145082215b33790f45b1 Mon Sep 17 00:00:00 2001 From: mengyi Date: Sun, 9 Jun 2024 21:44:59 -0400 Subject: [PATCH 1153/1533] update problem 707 java version solution --- ...76\350\256\241\351\223\276\350\241\250.md" | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 47771d2889..a247b17839 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -328,14 +328,29 @@ class MyLinkedList { return currentNode.val; } - //在链表最前面插入一个节点,等价于在第0个元素前添加 public void addAtHead(int val) { - addAtIndex(0, val); + ListNode newNode = new ListNode(val); + newNode.next = head.next; + head.next = newNode; + size++; + + // 在链表最前面插入一个节点,等价于在第0个元素前添加 + // addAtIndex(0, val); } - //在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加 + public void addAtTail(int val) { - addAtIndex(size, val); + ListNode newNode = new ListNode(val); + ListNode cur = head; + while (cur.next != null) { + cur = cur.next; + } + + cur.next = newNode; + size++; + + // 在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加 + // addAtIndex(size, val); } // 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。 @@ -407,7 +422,7 @@ class MyLinkedList { public int get(int index) { //判断index是否有效 - if(index<0 || index>=size){ + if(index>=size){ return -1; } ListNode cur = this.head; @@ -441,10 +456,7 @@ class MyLinkedList { if(index>size){ return; } - //index小于0 - if(index<0){ - index = 0; - } + size++; //找到前驱 ListNode pre = this.head; @@ -462,7 +474,7 @@ class MyLinkedList { public void deleteAtIndex(int index) { //判断索引是否有效 - if(index<0 || index>=size){ + if(index>=size){ return; } //删除操作 From 3b9fa3f074b1d39cbfb7000b8a19124a8794f3cb Mon Sep 17 00:00:00 2001 From: mengyi Date: Mon, 10 Jun 2024 17:56:59 -0400 Subject: [PATCH 1154/1533] update running map solution in Java --- .DS_Store | Bin 0 -> 6148 bytes ...44\346\225\260\344\271\213\345\222\214.md" | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f89600e9deb6f2c531cd456675779b0e188cf6e0 GIT binary patch literal 6148 zcmeHKyG{c^3>-s>lW0;>?l0sIR#EtZ`~X6rGQyC-DdB+wn*x~JCecVr~FDINk;1x$4@br_v zJ?uAm?`5L0Qa}nw0VyB_q`mri^f33h5x+WE_ literal 0 HcmV?d00001 diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 580fa3e2c9..044eac1424 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -152,6 +152,24 @@ public int[] twoSum(int[] nums, int target) { return res; } ``` + +```java +//使用哈希表方法2 +public int[] twoSum(int[] nums, int target) { + Map indexMap = new HashMap<>(); + + for(int i = 0; i < nums.length; i++){ + int balance = target - nums[i]; // 记录当前的目标值的余数 + if(indexMap.containsKey(balance)){ // 查找当前的map中是否有满足要求的值 + return new int []{i, indexMap.get(balance)}; // 如果有,返回目标值 + } else{ + indexMap.put(nums[i], i); // 如果没有,把访问过的元素和下标加入map中 + } + } + return null; +} +``` + ```java //使用双指针 public int[] twoSum(int[] nums, int target) { From 96f4622dcf91e4888a8ab86f4022fe11ecba67d7 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 13 Jun 2024 10:29:09 +0800 Subject: [PATCH 1155/1533] =?UTF-8?q?=E5=9B=BE=E8=AE=BA=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 3 - ...17\202\344\274\232dijkstra\345\240\206.md" | 30 +- ...74\232dijkstra\346\234\264\347\264\240.md" | 36 +- .../0053.\345\257\273\345\256\235-Kruskal.md" | 32 +- .../0053.\345\257\273\345\256\235-prim.md" | 28 +- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 26 ++ ...7\347\211\251\350\277\220\350\276\223I.md" | 26 ++ ...\347\211\251\350\277\220\350\276\223II.md" | 27 ++ ...347\211\251\350\277\220\350\276\223III.md" | 27 ++ ...16\351\200\233\345\205\254\345\233\255.md" | 23 ++ ...57\350\276\276\350\267\257\345\276\204.md" | 35 +- ...60\351\207\217\345\271\277\346\220\234.md" | 29 +- ...60\351\207\217\346\267\261\346\220\234.md" | 28 +- ...00\345\244\247\351\235\242\347\247\257.md" | 31 +- ...04\346\200\273\351\235\242\347\247\257.md" | 31 +- ...11\346\262\241\345\255\244\345\262\233.md" | 27 ++ ...64\346\265\201\351\227\256\351\242\230.md" | 27 ++ ...00\345\244\247\345\262\233\345\261\277.md" | 27 ++ ...50\345\217\257\350\276\276\346\200\247.md" | 27 ++ ...77\347\232\204\345\221\250\351\225\277.md" | 27 ++ ...50\347\232\204\350\267\257\345\276\204.md" | 27 ++ ...27\344\275\231\350\277\236\346\216\245.md" | 29 +- ...\344\275\231\350\277\236\346\216\245II.md" | 27 ++ ...46\344\270\262\346\216\245\351\276\231.md" | 27 ++ ...57\344\273\266\346\236\204\345\273\272.md" | 55 ++- ...47\346\225\260\345\207\217\346\263\225.md" | 89 +++++ ...43\346\234\200\345\244\247\345\200\274.md" | 127 ++++++ ...60\347\273\204\346\236\204\351\200\240.md" | 52 +++ ...76\345\215\216\345\270\226\345\255\220.md" | 38 ++ ...04\346\234\200\345\244\247\345\222\214.md" | 66 ++++ ...7\232\204\346\224\273\345\207\273astar.md" | 361 ++++++++++++++++++ ...06\350\256\272\345\237\272\347\241\200.md" | 10 +- ...06\350\256\272\345\237\272\347\241\200.md" | 56 +-- ...72\346\200\273\347\273\223\347\257\207.md" | 69 +++- ...06\350\256\272\345\237\272\347\241\200.md" | 10 +- ...30\346\200\273\347\273\223\347\257\207.md" | 15 +- ...06\350\256\272\345\237\272\347\241\200.md" | 3 +- 37 files changed, 1496 insertions(+), 112 deletions(-) create mode 100644 "problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" create mode 100644 "problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" create mode 100644 "problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" create mode 100644 "problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" create mode 100644 "problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" create mode 100644 "problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index a34490ea1b..1d9c524b13 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -253,9 +253,6 @@ for (pair& target : targets[result[result.size() - 1]]) 如果最终代码,发现照着回溯法模板画的话好像也能画出来,但难就难如何知道可以使用回溯,以及如果套进去,所以我再写了这么长的一篇来详细讲解。 -就酱,很多录友表示和「代码随想录」相见恨晚,那么帮Carl宣传一波吧,让更多同学知道这里! - - ## 其他语言版本 diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index b93138a78a..a0778d2db8 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -1,7 +1,7 @@ # dijkstra(堆优化版)精讲 -[题目链接](https://kamacoder.com/problempage.php?pid=1047) +[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047) 【题目描述】 @@ -66,7 +66,7 @@ 如果n很大的话,我们可以换一个角度来优先性能。 -在 讲解 最小生成树的时候,我们 讲了两个算法,[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w)(从点的角度来求最小生成树)、[Kruskal算法](https://mp.weixin.qq.com/s/rUVaBjCES_4eSjngceT5bw)(从边的角度来求最小生成树) +在 讲解 最小生成树的时候,我们 讲了两个算法,[prim算法](./0053.寻宝-prim.md)(从点的角度来求最小生成树)、[Kruskal算法](./0053.寻宝-Kruskal.md)(从边的角度来求最小生成树) 这么在n 很大的时候,也有另一个思考维度,即:从边的数量出发。 @@ -649,3 +649,29 @@ int main() { +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 295d3aec15..10474f1318 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -1,7 +1,7 @@ # dijkstra(朴素版)精讲 -[题目链接](https://kamacoder.com/problempage.php?pid=1047) +[卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047) 【题目描述】 @@ -80,7 +80,7 @@ dijkstra算法:在有权图(权值非负数)中求从起点到其他节点 最短路径的权值为12。 -其实 dijkstra 算法 和 我们之前讲解的prim算法思路非常接近,如果大家认真学过[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w),那么理解 Dijkstra 算法会相对容易很多。(这也是我要先讲prim再讲dijkstra的原因) +其实 dijkstra 算法 和 我们之前讲解的prim算法思路非常接近,如果大家认真学过[prim算法](./0053.寻宝-prim.md),那么理解 Dijkstra 算法会相对容易很多。(这也是我要先讲prim再讲dijkstra的原因) dijkstra 算法 同样是贪心的思路,不断寻找距离 源点最近的没有访问过的节点。 @@ -92,7 +92,7 @@ dijkstra 算法 同样是贪心的思路,不断寻找距离 源点最近的没 大家此时已经会发现,这和prim算法 怎么这么像呢。 -我在[prim算法](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w)讲解中也给出了三部曲。 prim 和 dijkstra 确实很像,思路也是类似的,这一点我在后面还会详细来讲。 +我在[prim算法](./0053.寻宝-prim.md)讲解中也给出了三部曲。 prim 和 dijkstra 确实很像,思路也是类似的,这一点我在后面还会详细来讲。 在dijkstra算法中,同样有一个数组很重要,起名为:minDist。 @@ -462,7 +462,7 @@ select:7 如果题目要求把最短路的路径打印出来,应该怎么办呢? -这里还是有一些“坑”的,本题打印路径和 prim 打印路径是一样的,我在 [prim算法精讲](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w) 【拓展】中 已经详细讲解了。 +这里还是有一些“坑”的,本题打印路径和 prim 打印路径是一样的,我在 [prim算法精讲](./0053.寻宝-prim.md) 【拓展】中 已经详细讲解了。 在这里就不再赘述。 @@ -660,7 +660,7 @@ int main() { ## dijkstra与prim算法的区别 -> 这里再次提示,需要先看我的 [prim算法精讲](https://mp.weixin.qq.com/s/yX936hHC6Z10K36Vm1Wl9w) ,否则可能不知道我下面讲的是什么。 +> 这里再次提示,需要先看我的 [prim算法精讲](./0053.寻宝-prim.md) ,否则可能不知道我下面讲的是什么。 大家可以发现 dijkstra的代码看上去 怎么和 prim算法这么像呢。 @@ -731,3 +731,29 @@ for (int v = 1; v <= n; v++) { +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 1d0c9c83e8..527f0e70b8 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -130,7 +130,7 @@ kruscal的思路: **但在代码中,如果将两个节点加入同一个集合,又如何判断两个节点是否在同一个集合呢**? -这里就涉及到我们之前讲解的[并查集](https://www.programmercarl.com/%E5%9B%BE%E8%AE%BA%E5%B9%B6%E6%9F%A5%E9%9B%86%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html)。 +这里就涉及到我们之前讲解的[并查集](./图论并查集理论基础.md)。 我们在并查集开篇的时候就讲了,并查集主要就两个功能: @@ -139,7 +139,7 @@ kruscal的思路: 大家发现这正好符合 Kruskal算法的需求,这也是为什么 **我要先讲并查集,再讲 Kruskal**。 -关于 并查集,我已经在[并查集精讲](https://www.programmercarl.com/%E5%9B%BE%E8%AE%BA%E5%B9%B6%E6%9F%A5%E9%9B%86%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html) 详细讲解过了,所以这里不再赘述,我们直接用。 +关于 并查集,我已经在[并查集精讲](./图论并查集理论基础.md) 详细讲解过了,所以这里不再赘述,我们直接用。 本题代码如下,已经详细注释: @@ -374,7 +374,7 @@ Kruskal 与 prim 的关键区别在于,prim维护的是节点的集合,而 K 在节点数量固定的情况下,图中的边越少,Kruskal 需要遍历的边也就越少。 -而 prim 算法是对节点进行操作的,节点数量越少,prim算法效率就越少。 +而 prim 算法是对节点进行操作的,节点数量越少,prim算法效率就越优。 所以在 稀疏图中,用Kruskal更优。 在稠密图中,用prim算法更优。 @@ -398,3 +398,29 @@ Kruskal算法 时间复杂度 为 nlogn,其中n 为边的数量,适用稀疏 录友们可以细细体会。 +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index 4d3d9bd829..f1b3b721bf 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -507,10 +507,36 @@ int main() { 最后我们拓展了如何求职 最小生成树 的每一条边,其实 添加的代码很简单,主要是理解 为什么使用 parent数组 来记录边 以及 在哪里 更新parent数组。 -同时,因为使用一维数组,数组的下标和数组 如何赋值很重要,不要搞反,导师结果被覆盖。 +同时,因为使用一维数组,数组的下标和数组 如何赋值很重要,不要搞反,导致结果被覆盖。 好了,以上为总结,录友们学习愉快。 +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index ec35fa9d98..eb3204b612 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -347,6 +347,32 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹 +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index dc9b46f338..7cadc37888 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -387,3 +387,29 @@ Bellman_ford 是可以计算 负权值的单源最短路算法。 弄清楚 什么是 松弛? 为什么要 n-1 次? 对理解Bellman_ford 非常重要。 +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 3200efb377..ea7065792f 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -238,3 +238,30 @@ int main() { } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index f7533e024f..2b9f44fcb7 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -630,3 +630,30 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 * 能否用dijkstra 学透了以上四个拓展,相信大家会对bellman_ford有更深入的理解。 + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index 7e69994961..f4b53acfaa 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -418,6 +418,29 @@ floyd算法的时间复杂度相对较高,适合 稠密图且源点较多的 如果 源点少,其实可以 多次dijsktra 求源点到终点。 +## 其他语言版本 +### Java +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 0573d0817e..ca4f117c19 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -3,8 +3,6 @@ [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1170) -[力扣题目讲解以及题目链接(核心代码模式)](https://programmercarl.com/0797.%E6%89%80%E6%9C%89%E5%8F%AF%E8%83%BD%E7%9A%84%E8%B7%AF%E5%BE%84.html#%E6%80%9D%E8%B7%AF) - 【题目描述】 给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个函数,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。 @@ -406,3 +404,36 @@ int main() { +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + + + + + + + + diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index e7a0a8f3ac..993c01f225 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -3,8 +3,6 @@ [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171) -[力扣题目讲解以及题目链接(核心代码模式)](https://programmercarl.com/0200.%E5%B2%9B%E5%B1%BF%E6%95%B0%E9%87%8F.%E5%B9%BF%E6%90%9C%E7%89%88.html) - 题目描述: 给定一个由 1(陆地)和 0(水)组成的矩阵,你需要计算岛屿的数量。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。 @@ -185,3 +183,30 @@ int main() { ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 310676105f..7fac4cb8f9 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -3,7 +3,6 @@ [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171) -[力扣题目讲解以及题目链接(核心代码模式)](https://programmercarl.com/0200.%E5%B2%9B%E5%B1%BF%E6%95%B0%E9%87%8F.%E6%B7%B1%E6%90%9C%E7%89%88.html) 题目描述: @@ -177,3 +176,30 @@ int main() { 本篇我只给出的dfs的写法,大家发现我写的还是比较细的,那么后面我再单独给出本题的bfs写法,虽然是模板题,但依然有很多注意的点,敬请期待! + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 6f8a9ed86a..2e2ed4f349 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -3,8 +3,6 @@ [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1172) -[力扣题目链接](https://programmercarl.com/0695.%E5%B2%9B%E5%B1%BF%E7%9A%84%E6%9C%80%E5%A4%A7%E9%9D%A2%E7%A7%AF.html#%E6%80%9D%E8%B7%AF) - 题目描述 给定一个由 1(陆地)和 0(水)组成的矩阵,计算岛屿的最大面积。岛屿面积的计算方式为组成岛屿的陆地的总数。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。 @@ -167,7 +165,7 @@ int main() { ### BFS -关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://programmercarl.com/kamacoder/图论广搜理论基础.html) +关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](./图论广搜理论基础.md) 本题BFS代码如下: @@ -219,3 +217,30 @@ public: ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 602fb97738..5be256117f 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -1,7 +1,7 @@ # 101. 孤岛的总面积 -[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1173) +[卡码网:101. 孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173) 题目描述 @@ -60,7 +60,7 @@ 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 -如果对深搜或者广搜不够了解,建议先看这里:[深度优先搜索精讲](https://programmercarl.com/kamacoder/图论深搜理论基础.html),[广度优先搜索精讲](https://programmercarl.com/kamacoder/图论广搜理论基础.html)。 +如果对深搜或者广搜不够了解,建议先看这里:[深度优先搜索精讲](./图论深搜理论基础.md),[广度优先搜索精讲](./图论广搜理论基础.md)。 采用深度优先搜索的代码如下: @@ -178,3 +178,30 @@ int main() { } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 4c7491f716..43666325f2 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -131,3 +131,30 @@ int main() { } } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 8f99beae1f..0f461edb3c 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -276,4 +276,31 @@ for (int j = 0; j < m; j++) { +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + + diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 927b20b2d3..a1be8fb12a 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -252,3 +252,30 @@ int main() { } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 81f4f6f0ac..9903b6d7b4 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -282,3 +282,30 @@ int main() { ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 4e285a9eec..2233c2db96 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -152,3 +152,30 @@ int main() { } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index d769518098..30b8e58701 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -153,3 +153,30 @@ int main() { } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index de0a0c6fca..4e8ea4cfcf 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -48,7 +48,7 @@ 这里我依然降调一下,并查集可以解决什么问题:两个节点是否在一个集合,也可以将两个节点添加到一个集合中。 -如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/图论并查集理论基础.html) +如果还不了解并查集,可以看这里:[并查集理论基础](./图论并查集理论基础.md) 我们再来看一下这道题目。 @@ -126,3 +126,30 @@ int main() { 可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。 + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 4f3bbf0a05..09b6118b64 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -238,3 +238,30 @@ int main() { getRemoveEdge(edges); } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 81c1a33dca..cac33c2304 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -147,3 +147,30 @@ int main() { 当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。 + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index 7229489b6a..1cd9a983b1 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -1,7 +1,7 @@ # 拓扑排序精讲 -[卡码网:软件构建](https://kamacoder.com/problempage.php?pid=1191) +[卡码网:117. 软件构建](https://kamacoder.com/problempage.php?pid=1191) 题目描述: @@ -128,7 +128,7 @@ ## 模拟过程 -用本题的示例来模拟一下这一过程: +用本题的示例来模拟这一过程: 1、找到入度为0 的节点,加入结果集 @@ -180,7 +180,9 @@ 这个图,我们只能将入度为0 的节点0 接入结果集。 之后,节点1、2、3、4 形成了环,找不到入度为0 的节点了,所以此时结果集里只有一个元素。 + 那么如果我们发现结果集元素个数 不等于 图中节点个数,我们就可以认定图中一定有 有向环! + 这也是拓扑排序判断有向环的方法。 通过以上过程的模拟大家会发现这个拓扑排序好像不难,还有点简单。 @@ -296,42 +298,69 @@ using namespace std; int main() { int m, n, s, t; cin >> n >> m; - vector inDegree(n, 0); // 记录每个节点的入度 + vector inDegree(n, 0); // 记录每个文件的入度 - unordered_map> umap;// 记录节点依赖关系 + unordered_map> umap;// 记录文件依赖关系 vector result; // 记录结果 while (m--) { // s->t,先有s才能有t cin >> s >> t; inDegree[t]++; // t的入度加一 - umap[s].push_back(t); // 记录s指向哪些节点 + umap[s].push_back(t); // 记录s指向哪些文件 } queue que; for (int i = 0; i < n; i++) { - // 入度为0的节点,可以作为开头,先加入队列 + // 入度为0的文件,可以作为开头,先加入队列 if (inDegree[i] == 0) que.push(i); //cout << inDegree[i] << endl; } // int count = 0; while (que.size()) { - int cur = que.front(); // 当前选中的节点 + int cur = que.front(); // 当前选中的文件 que.pop(); //count++; result.push_back(cur); - vector files = umap[cur]; //获取该节点指向的节点 - if (files.size()) { // cur有后续节点 + vector files = umap[cur]; //获取该文件指向的文件 + if (files.size()) { // cur有后续文件 for (int i = 0; i < files.size(); i++) { - inDegree[files[i]] --; // cur的指向的节点入度-1 + inDegree[files[i]] --; // cur的指向的文件入度-1 if(inDegree[files[i]] == 0) que.push(files[i]); } } } - // 判断是否有有向环 if (result.size() == n) { - // 注意输出格式,最后一个元素后面没有空格 - for (int i = 0; i < n - 2; i++) cout << result[i] << " "; + for (int i = 0; i < n - 1; i++) cout << result[i] << " "; cout << result[n - 1]; } else cout << -1 << endl; + + } ``` + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + diff --git "a/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" "b/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" new file mode 100644 index 0000000000..a36f2cbbe7 --- /dev/null +++ "b/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" @@ -0,0 +1,89 @@ + +# 大数减法 + +本题测试数据超过int 和 longlong了,所以考察的使用 string 来模拟 两个大数的 加减操作。 + +当然如果使用python或者Java 使用库函数都可以水过。 + +使用字符串来模拟过程,需要处理以下几个问题: + +* 负号处理:要考虑正负数的处理,如果大数相减的结果是负数,需要在结果前加上负号。 +* 大数比较:在进行减法之前,需要确定哪个数大,以便知道结果是否需要添加负号。 +* 位数借位:处理大数相减时的借位问题,这类似于手动减法。 + +```CPP +#include +#include +#include +using namespace std; + +// 比较两个字符串表示的数字,返回1表示a > b,0表示a == b,-1表示a < b +int compareStrings(const string& a, const string& b) { + if (a.length() > b.length()) return 1; + if (a.length() < b.length()) return -1; + return a.compare(b); +} + +// 去除字符串左侧的前导零 +string removeLeadingZeros(const string& num) { + size_t start = 0; + while (start < num.size() && num[start] == '0') { + start++; + } + return start == num.size() ? "0" : num.substr(start); +} + +// 大数相减,假设a >= b +string subtractStrings(const string& a, const string& b) { + string result; + int len1 = a.length(), len2 = b.length(); + int carry = 0; + + for (int i = 0; i < len1; i++) { + int digitA = a[len1 - 1 - i] - '0'; + int digitB = i < len2 ? b[len2 - 1 - i] - '0' : 0; + + int digit = digitA - digitB - carry; + if (digit < 0) { + digit += 10; + carry = 1; + } else { + carry = 0; + } + + result.push_back(digit + '0'); + } + + // 去除结果中的前导零 + reverse(result.begin(), result.end()); + return removeLeadingZeros(result); +} + +string subtractLargeNumbers(const string& num1, const string& num2) { + string a = num1, b = num2; + + // 比较两个数的大小 + int cmp = compareStrings(a, b); + + if (cmp == 0) { + return "0"; // 如果两个数相等,结果为0 + } else if (cmp < 0) { + // 如果a < b,交换它们并在结果前加上负号 + swap(a, b); + return "-" + subtractStrings(a, b); + } else { + return subtractStrings(a, b); + } +} + +int main() { + string num1, num2; + cin >> num1 >> num2; + + string result = subtractLargeNumbers(num1, num2); + cout << result << endl; + + return 0; +} + +``` diff --git "a/problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" new file mode 100644 index 0000000000..7820d01e59 --- /dev/null +++ "b/problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -0,0 +1,127 @@ + +# 滑动窗口最大值 + +本题是 [代码随想录:滑动窗口最大值](https://www.programmercarl.com/0239.%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.html) 的升级版。 + +在[代码随想录:滑动窗口最大值](https://www.programmercarl.com/0239.%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.html) 中详细讲解了如何求解 滑动窗口的最大值。 + +那么求滑动窗口的最小值原理也是一样的, 大家稍加思考,把优先级队列里的 大于 改成小于 就行了。 + +求最大值的优先级队列(从大到小) +``` +while (!que.empty() && value > que.back()) { +``` + +求最小值的优先级队列(从小到大) +``` +while (!que.empty() && value > que.back()) { +``` + +这样在滑动窗口里 最大值最小值都求出来了,遍历一遍找出 差值最大的就好。 + +至于输入,需要一波字符串处理,比较考察基本功。 + +CPP代码如下: + +```CPP +#include +#include +#include +#include +#include +using namespace std; +class MyBigQueue { //单调队列(从大到小) +public: + deque que; // 使用deque来实现单调队列 + // 每次弹出的时候,比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。 + // 同时pop之前判断队列当前是否为空。 + void pop(int value) { + if (!que.empty() && value == que.front()) { + que.pop_front(); + } + } + // 如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。 + // 这样就保持了队列里的数值是单调从大到小的了。 + void push(int value) { + while (!que.empty() && value > que.back()) { + que.pop_back(); + } + que.push_back(value); + + } + // 查询当前队列里的最大值 直接返回队列前端也就是front就可以了。 + int front() { + return que.front(); + } +}; + +class MySmallQueue { //单调队列(从小到大) +public: + deque que; + + void pop(int value) { + if (!que.empty() && value == que.front()) { + que.pop_front(); + } + } + + // 和上面队列的区别是这里换成了小于, + void push(int value) { + while (!que.empty() && value < que.back()) { + que.pop_back(); + } + que.push_back(value); + + } + + int front() { + return que.front(); + } +}; + +int main() { + string input; + + getline(cin, input); + + vector nums; + int k; + + // 找到并截取nums的部分 + int numsStart = input.find('['); + int numsEnd = input.find(']'); + string numsStr = input.substr(numsStart + 1, numsEnd - numsStart - 1); + // cout << numsStr << endl; + + // 用字符串流处理nums字符串,提取数字 + stringstream ss(numsStr); + string temp; + while (getline(ss, temp, ',')) { + nums.push_back(stoi(temp)); + } + + // 找到并提取k的值 + int kStart = input.find("k = ") + 4; + k = stoi(input.substr(kStart)); + + MyBigQueue queB; // 获取区间最大值 + MySmallQueue queS; // 获取区间最小值 + // vector result; + for (int i = 0; i < k; i++) { // 先将前k的元素放进队列 + queB.push(nums[i]); + queS.push(nums[i]); + } + + int result = queB.front() - queS.front(); + for (int i = k; i < nums.size(); i++) { + queB.pop(nums[i - k]); // 滑动窗口移除最前面元素 + queB.push(nums[i]); // 滑动窗口前加入最后面的元素 + + queS.pop(nums[i - k]); + queS.push(nums[i]); + + result = max (result, queB.front() - queS.front()); + } + cout << result << endl; +} +``` diff --git "a/problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" "b/problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" new file mode 100644 index 0000000000..ef66dec8bc --- /dev/null +++ "b/problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" @@ -0,0 +1,52 @@ + +121. 小红的数组构造 + +本题大家不要想着真去模拟数组的情况,那样就想复杂了。 + +数组只能是:1k、2k、3k ... (n-1)k、nk,这样 总和就是最小的。 + +注意最后的和可能超过int,所以用 long long。 + +代码如下: + +```CPP +#include +using namespace std; +int main () { + long long result = 0; + int n, k; + cin >> n >> k; + for (int i = 1; i <= n; i++) { + result += i * k; + } + cout << result << endl; +} +``` + +优化思路: + + +由于要计算1到n的整数之和,可以利用等差数列求和公式来优化计算。 + +和公式:1 + 2 + 3 + ... + n = n * (n + 1) / 2 + +因此,总和 result = k * (n * (n + 1) / 2) + +```CPP + +#include +using namespace std; + +int main() { + long long result = 0; + int n, k; + cin >> n >> k; + + // 使用等差数列求和公式进行计算 + result = k * (n * (n + 1LL) / 2); + + cout << result << endl; + return 0; +} + +``` diff --git "a/problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" "b/problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" new file mode 100644 index 0000000000..2855c89efa --- /dev/null +++ "b/problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" @@ -0,0 +1,38 @@ + + +# 122.精华帖子 + + +开辟一个数组,默认都是0,把精华帖标记为1. + +使用前缀和,快速计算出,k 范围内 有多少个精华帖。 + +前缀和要特别注意区间问题,即 vec[i+k] - vec[i] 求得区间和是 (i, i + k] 这个区间,注意这是一个左开右闭的区间。 + +所以前缀和 很容易漏掉 vec[0] 这个数值的计算 + +```CPP +#include +#include +using namespace std; +int main() { + int n, m, k, l, r; + cin >> n >> m >> k; + vector vec(n); + while (m--) { + cin >> l >> r; + for (int i = l; i < r; i++) vec[i] = 1; + } + int result = 0; + for (int i = 0; i < k; i++) result += vec[i]; // 提前预处理result,包含vec[0]的区间,否则前缀和容易漏掉这个区间 + + for (int i = 1; i < n; i++) { + vec[i] += vec[i - 1]; + } + + for (int i = 0; i < n - k; i++) { + result = max (result, vec[i + k] - vec[i]); + } + cout << result << endl; +} +``` diff --git "a/problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" "b/problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" new file mode 100644 index 0000000000..32833b2fb2 --- /dev/null +++ "b/problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" @@ -0,0 +1,66 @@ + +# 123.连续子数组最大和 + +这道题目可以说是 [代码随想录,动态规划:最大子序和](https://www.programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html) 的升级版。 + +题目求的是 可以替换一个数字 之后 的 连续子数组最大和。 + +如果替换的是数组下标 i 的元素。 + +那么可以用 [代码随想录,动态规划:最大子序和](https://www.programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html) 的方法,先求出 [0 - i) 区间的 最大子序和 dp1 和 (i, n)的最大子序和dp2 。 + +然后在遍历一遍i, 计算 dp1 + dp2 + vec[i] 的最大值就可以。 + +正序遍历,求出 [0 - i) 区间的 最大子序,dp[ i - 1] 表示 是 包括下标i - 1(以vec[i - 1]为结尾)的最大连续子序列和为dp[i - 1]。 + +所以 在计算区间 (i, n)即 dp2 的时候,我们要倒叙。 因为我们求的是以 包括下标i + 1 为起始位置的最大连续子序列和为dp[i + 1]。 + +这样 dp1 + dp2 + vec[i] 才是一个完整区间。 + +这里就体现出对 dp数组定义的把控,本题如果对 dp数组含义理解不清,其实是不容易做出来的。 + +代码: + +```CPP +#include +#include +#include +using namespace std; +int main() { + int t, n, x; + cin >> t; + while (t--) { + cin >> n >> x; + vector vec(n); + for (int i = 0; i < n; i++) cin >> vec[i]; + vector dp1(n); + dp1[0] = vec[0]; + int res = vec[0]; + // 从前向后统计最大子序和 + for (int i = 1; i < n; i++) { + dp1[i] = max(dp1[i - 1] + vec[i], vec[i]); // 状态转移公式 + res = max(res, dp1[i]); + } + + res = max(res, vec[n - 1]); + // 从后向前统计最大子序和 + vector dp2(n); + dp2[n - 1] = vec[n - 1]; + for (int i = n - 2; i >= 0; i--) { + dp2[i] = max(dp2[i + 1] + vec[i], vec[i]); + + } + + for (int i = 0 ; i < n ; i++) { + int dp1res = 0; + if (i > 0) dp1res = max(dp1[i-1], 0); + int dp2res = 0; + if (i < n - 1 ) dp2res = max(dp2[i+1], 0); + + res = max(res, dp1res + dp2res + x); + } + cout << res << endl; + } + +} +``` diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" new file mode 100644 index 0000000000..fe510ae25c --- /dev/null +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -0,0 +1,361 @@ + +# A * 算法精讲 (A star算法) + +[卡码网:126. 骑士的攻击](https://kamacoder.com/problempage.php?pid=1203) + +题目描述 + +在象棋中,马和象的移动规则分别是“马走日”和“象走田”。现给定骑士的起始坐标和目标坐标,要求根据骑士的移动规则,计算从起点到达目标点所需的最短步数。 + +棋盘大小 1000 x 1000(棋盘的 x 和 y 坐标均在 [1, 1000] 区间内,包含边界) + +输入描述 + +第一行包含一个整数 n,表示测试用例的数量。 + +接下来的 n 行,每行包含四个整数 a1, a2, b1, b2,分别表示骑士的起始位置 (a1, a2) 和目标位置 (b1, b2)。 + +输出描述 + +输出共 n 行,每行输出一个整数,表示骑士从起点到目标点的最短路径长度。 + +输入示例 + +``` +6 +5 2 5 4 +1 1 2 2 +1 1 8 8 +1 1 8 7 +2 1 3 3 +4 6 4 6 +``` + +输出示例 + +``` +2 +4 +6 +5 +1 +0 +``` + +## 思路 + +我们看到这道题目的第一个想法就是广搜,这也是最经典的广搜类型题目。 + +这里我直接给出广搜的C++代码: + +```CPP +#include +#include +#include +using namespace std; +int moves[1001][1001]; +int dir[8][2]={-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2}; +void bfs(int a1,int a2, int b1, int b2) +{ + queue q; + q.push(a1); + q.push(a2); + while(!q.empty()) + { + int m=q.front(); q.pop(); + int n=q.front(); q.pop(); + if(m == b1 && n == b2) + break; + for(int i=0;i<8;i++) + { + int mm=m + dir[i][0]; + int nn=n + dir[i][1]; + if(mm < 1 || mm > 1000 || nn < 1 || nn > 1000) + continue; + if(!moves[mm][nn]) + { + moves[mm][nn]=moves[m][n]+1; + q.push(mm); + q.push(nn); + } + } + } +} + +int main() +{ + int n, a1, a2, b1, b2; + cin >> n; + while (n--) { + cin >> a1 >> a2 >> b1 >> b2; + memset(moves,0,sizeof(moves)); + bfs(a1, a2, b1, b2); + cout << moves[b1][b2] << endl; + } + return 0; +} + +``` + +提交后,大家会发现,超时了。 + +因为本题地图足够大,且 n 也有可能很大,导致有非常多的查询。 + +我们来看一下广搜的搜索过程,如图,红色是起点,绿色是终点,黄色是要遍历的点,最后从 起点 找到 达到终点的最短路径是棕色。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611143712.png) + +可以看出 广搜中,做了很多无用的遍历, 黄色的格子是广搜遍历到的点。 + +这里我们能不能让便利方向,向这终点的方向去遍历呢? + +这样我们就可以避免很多无用遍历。 + + +## Astar + +Astar 是一种 广搜的改良版。 有的是 Astar是 dijkstra 的改良版。 + +其实只是场景不同而已 我们在搜索最短路的时候, 如果是无权图(边的权值都是1) 那就用广搜,代码简洁,时间效率和 dijkstra 差不多 (具体要取决于图的稠密) + +如果是有权图(边有不同的权值),优先考虑 dijkstra。 + +而 Astar 关键在于 启发式函数, 也就是 影响 广搜或者 dijkstra 从 容器(队列)里取元素的优先顺序。 + +以下,我用BFS版本的A * 来进行讲解。 + +在BFS中,我们想搜索,从起点到终点的最短路径,要一层一层去遍历。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611143712.png) + +如果 使用A * 的话,其搜索过程是这样的,如图,图中着色的都是我们要遍历的点。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611195223.png) + + +(上面两图中 最短路长度都是8,只是走的方式不同而已) + +大家可以发现 **BFS 是没有目的性的 一圈一圈去搜索, 而 A * 是有方向性的去搜索**。 + +看出 A * 可以节省很多没有必要的遍历步骤。 + +为了让大家可以明显看到区别,我将 BFS 和 A * 制作成可视化动图,大家可以自己看看动图,效果更好。 + +地址:https://kamacoder.com/tools/knight.html + +那么 A * 为什么可以有方向性的去搜索,它的如何知道方向呢? + +**其关键在于 启发式函数**。 + +那么启发式函数落实到代码处,如果指引搜索的方向? + +在本篇开篇中给出了BFS代码,指引 搜索的方向的关键代码在这里: + +```CPP +int m=q.front();q.pop(); +int n=q.front();q.pop(); +``` + +从队列里取出什么元素,接下来就是从哪里开始搜索。 + +**所以 启发式函数 要影响的就是队列里元素的排序**! + +这是影响BFS搜索方向的关键。 + +对队列里节点进行排序,就需要给每一个节点权值,如何计算权值呢? + +每个节点的权值为F,给出公式为:F = G + H + +G:起点达到目前遍历节点的距离 + +F:目前遍历的节点到达终点的距离 + +起点达到目前遍历节点的距离 + 目前遍历的节点到达终点的距离 就是起点到达终点的距离。 + +本题的图是无权网格状,在计算两点距离通常有如下三种计算方式: + +1. 曼哈顿距离,计算方式: d = abs(x1-x2)+abs(y1-y2) +2. 欧氏距离(欧拉距离) ,计算方式:d = sqrt( (x1-x2)^2 + (y1-y2)^2 ) +3. 切比雪夫距离,计算方式:d = max(abs(x1 - x2), abs(y1 - y2)) + +x1, x2 为起点坐标,y1, y2 为终点坐标 ,abs 为求绝对值,sqrt 为求开根号, + +选择哪一种距离计算方式 也会导致 A * 算法的结果不同。 + +本题,采用欧拉距离才能最大程度体现 点与点之间的距离。 + +所以 使用欧拉距离计算 和 广搜搜出来的最短路的节点数是一样的。 (路径可能不同,但路径上的节点数是相同的) + +我在制作动画演示的过程中,分别给出了曼哈顿、欧拉以及契比雪夫 三种计算方式下,A * 算法的寻路过程,大家可以自己看看看其区别。 + +动画地址:https://kamacoder.com/tools/knight.html + +计算出来 F 之后,按照 F 的 大小,来选去出队列的节点。 + +可以使用 优先级队列 帮我们排好序,每次出队列,就是F最大的节点。 + +实现代码如下:(启发式函数 采用 欧拉距离计算方式) + +```CPP +#include +#include +#include +using namespace std; +int moves[1001][1001]; +int dir[8][2]={-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2}; +int b1, b2; +// F = G + H +// G = 从起点到该节点路径消耗 +// H = 该节点到终点的预估消耗 + +struct Knight{ + int x,y; + int g,h,f; + bool operator < (const Knight & k) const{ // 重载运算符, 从小到大排序 + return k.f < f; + } +}; + +priority_queue que; + +int Heuristic(const Knight& k) { // 欧拉距离 + return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2); // 统一不开根号,这样可以提高精度 +} +void astar(const Knight& k) +{ + Knight cur, next; + que.push(k); + while(!que.empty()) + { + cur=que.top(); que.pop(); + if(cur.x == b1 && cur.y == b2) + break; + for(int i = 0; i < 8; i++) + { + next.x = cur.x + dir[i][0]; + next.y = cur.y + dir[i][1]; + if(next.x < 1 || next.x > 1000 || next.y < 1 || next.y > 1000) + continue; + if(!moves[next.x][next.y]) + { + moves[next.x][next.y] = moves[cur.x][cur.y] + 1; + + // 开始计算F + next.g = cur.g + 5; // 统一不开根号,这样可以提高精度,马走日,1 * 1 + 2 * 2 = 5 + next.h = Heuristic(next); + next.f = next.g + next.h; + que.push(next); + } + } + } +} + +int main() +{ + int n, a1, a2; + cin >> n; + while (n--) { + cin >> a1 >> a2 >> b1 >> b2; + memset(moves,0,sizeof(moves)); + Knight start; + start.x = a1; + start.y = a2; + start.g = 0; + start.h = Heuristic(start); + start.f = start.g + start.h; + astar(start); + while(!que.empty()) que.pop(); // 队列清空 + cout << moves[b1][b2] << endl; + } + return 0; +} + +``` + +## 复杂度分析 + +A * 算法的时间复杂度 其实是不好去量化的,因为他取决于 启发式函数怎么写。 + +最坏情况下,A * 退化成广搜,算法的时间复杂度 是 O(n * 2),n 为节点数量。 + +最佳情况,是从起点直接到终点,时间复杂度为 O(dlogd),d 为起点到终点的深度。 + +因为在搜索的过程中也需要堆排序,所以是 O(dlogd)。 + +实际上 A * 的时间复杂度是介于 最优 和最坏 情况之间, 可以 非常粗略的认为 A * 算法的时间复杂度是 O(nlogn) ,n 为节点数量。 + +A * 算法的空间复杂度 O(b ^ d) ,d 为起点到终点的深度,b 是 图中节点间的连接数量,本题因为是无权网格图,所以 节点间连接数量为 4。 + + +## 拓展 + +如果本题大家使用 曼哈顿距离 或者 切比雪夫距离 计算的话,可以提交试一试,有的最短路结果是并不是最短的。 + +原因也是 曼哈顿 和 切比雪夫这两种计算方式在 本题的网格地图中,都没有体现出点到点的真正距离! + +可能有些录友找到类似的题目,例如 [poj 2243](http://poj.org/problem?id=2243),使用 曼哈顿距离 提交也过了, 那是因为题目中的地图太小了,仅仅是一张 8 * 8的地图,根本看不出来 不同启发式函数写法的区别。 + +A * 算法 并不是一个明确的最短路算法,**A * 算法搜的路径如何,完全取决于 启发式函数怎么写**。 + +**A * 算法并不能保证一定是最短路**,因为在设计 启发式函数的时候,要考虑 时间效率与准确度之间的一个权衡。 + +虽然本题中,A * 算法得到是最短路,也是因为本题 启发式函数 和 地图结构都是最简单的。 + +例如在游戏中,在地图很大、不同路径权值不同、有障碍 且多个游戏单位在地图中寻路的情况,如果要计算准确最短路,耗时很大,会给玩家一种卡顿的感觉。 + +而真实玩家在玩游戏的时候,并不要求一定是最短路,次短路也是可以的 (玩家不一定能感受出来,及时感受出来也不是很在意),只要奔着目标走过去 大体就可以接受。 + +所以 在游戏开发设计中,**保证运行效率的情况下,A * 算法中的启发式函数 设计往往不是最短路,而是接近最短路的 次短路设计**。 + +大家如果玩 LOL,或者 王者荣耀 可以回忆一下:如果 从很远的地方点击 让英雄直接跑过去 是 跑的路径是不靠谱的,所以玩家们才会在 距离英雄尽可能近的位置去点击 让英雄跑过去。 + +## A * 的缺点 + +大家看上述 A * 代码的时候,可以看到 我们想 队列里添加了很多节点,但真正从队列里取出来的 仅仅是 靠启发式函数判断 距离终点最近的节点。 + +相对了 普通BFS,A * 算法只从 队列里取出 距离终点最近的节点。 + +那么问题来了,A * 在一次路径搜索中,大量不需要访问的节点都在队列里,会造成空间的过度消耗。 + +IDA * 算法 对这一空间增长问题进行了优化,关于 IDA * 算法,本篇不再做讲解,感兴趣的录友可以自行找资料学习。 + +另外还有一种场景 是 A * 解决不了的。 + +如果题目中,给出 多个可能的目标,然后在这多个目标中 选择最近的目标,这种 A * 就不擅长了, A * 只擅长给出明确的目标 然后找到最短路径。 + +如果是多个目标找最近目标(特别是潜在目标数量很多的时候),可以考虑 Dijkstra ,BFS 或者 Floyd。 + + +## 其他语言版本 + +### Java + +### Python + +### Go + +### Rust + +### Javascript + +### TypeScript + +### PhP + +### Swift + +### Scala + +### C# + +### Dart + +### C + + + + + + + + diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 3bf45f77f1..e463b95600 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -29,7 +29,7 @@ 但如果我们要判断两个元素是否在同一个集合里的时候 我们又能怎么办? 只能把而二维数组都遍历一遍。 -而且每当想添加一个元素到某集合的时候,依然需要把把二维数组组都遍历一遍,才知道要放在哪个集合里。 +而且每当想添加一个元素到某集合的时候,依然需要把把二维数组都遍历一遍,才知道要放在哪个集合里。 这仅仅是一个粗略的思路,如果沿着这个思路去实现代码,非常复杂,因为管理集合还需要很多逻辑。 @@ -208,7 +208,7 @@ bool isSame(int u, int v) { // 将v->u 这条边加入并查集 void join(int u, int v) { - if (isSame) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + if (isSame(u, v)) return ; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 father[v] = u; } ``` @@ -219,7 +219,7 @@ void join(int u, int v) { 举一个例子: -``` +```CPP join(1, 2); join(3, 2); ``` @@ -271,7 +271,7 @@ join(3, 2); 不少录友在接触并查集模板之后,用起来很娴熟,因为模板确实相对固定,但是对并查集内部数据组织方式以及如何判断是否是同一个集合的原理很模糊。 -通过以上讲解之后,我在带大家一步一步去画一下,并查集内部数据连接方式。 +通过以上讲解之后,我再带大家一步一步去画一下,并查集内部数据连接方式。 1、`join(1, 8);` @@ -301,7 +301,7 @@ join(3, 2); 即如下代码在寻找根的过程中,会有路径压缩,减少 下次查询的路径长度。 -``` +```CPP // 并查集里寻根的过程 int find(int u) { return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index c494e0d0e2..d791d2c0ff 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,7 +1,6 @@ # 广度优先搜索理论基础 - -在[深度优先搜索](https://programmercarl.com/图论深搜理论基础.html)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 +在[深度优先搜索](./图论深搜理论基础.md)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 广搜(bfs)是一圈一圈的搜索过程,和深搜(dfs)是一条路跑到黑然后再回溯。 @@ -88,28 +87,12 @@ void bfs(vector>& grid, vector>& visited, int x, int y } ``` -以上模板代码,就是可以直接拿来做 [200.岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/) 这道题目,唯一区别是 针对地图 grid 中有数字1的地方去做一个遍历。 - -即: - -``` -if (!visited[nextx][nexty]) { // 如果节点没被访问过 -``` - -改为 - -``` -if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') { // 如果节点没被访问过且节点是可遍历的 - -``` -就可以通过 [200.岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/) 这道题目,大家可以去体验一下。 - - - ## 总结 -当然广搜还有很多细节需要注意的地方,后面我会针对广搜的题目还做针对性的讲解,因为在理论篇讲太多细节,可能会让刚学广搜的录友们越看越懵,所以细节方面针对具体题目在做讲解。 +当然广搜还有很多细节需要注意的地方,后面我会针对广搜的题目还做针对性的讲解。 + +**因为在理论篇讲太多细节,可能会让刚学广搜的录友们越看越懵**,所以细节方面针对具体题目在做讲解。 本篇我们重点讲解了广搜的使用场景,广搜的过程以及广搜的代码框架。 @@ -119,34 +102,3 @@ if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') { // 如果节点没被 相信看完本篇,大家会对广搜有一个基础性的认识,后面再来做对应的题目就会得心应手一些。 -## 其他语言版本 - -### Python -```python -from collections import deque - -dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 创建方向元素 - -def bfs(grid, visited, x, y): - - queue = deque() # 初始化队列 - queue.append((x, y)) # 放入第一个元素/起点 - visited[x][y] = True # 标记为访问过的节点 - - while queue: # 遍历队列里的元素 - - curx, cury = queue.popleft() # 取出第一个元素 - - for dx, dy in dir: # 遍历四个方向 - - nextx, nexty = curx + dx, cury + dy - - if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]): # 越界了,直接跳过 - continue - - if not visited[nextx][nexty]: # 如果节点没被访问过 - queue.append((nextx, nexty)) # 加入队列 - visited[nextx][nexty] = True # 标记为访问过的节点 - -``` - diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" index aa360bab1d..d89d6411e9 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" @@ -3,7 +3,7 @@ 从深搜广搜 到并查集,从最小生成树到拓扑排序, 最后是最短路算法系列。 -至此算上本篇,一共32篇文章,图论之旅就在此收官了。 +至此算上本篇,一共30篇文章,图论之旅就在此收官了。 在[0098.所有可达路径](./0098.所有可达路径.md) ,我们接触了两种图的存储方式,邻接表和邻接矩阵,掌握两种图的存储方式很重要。 @@ -67,20 +67,79 @@ 其实理论基础篇就算是给大家出了一道裸的并查集题目了,所以在后面的题目安排中,会稍稍的拔高一些,重点在于并查集的应用上。 +例如 并查集可以判断这个图是否是树,因为树的话,只有一个根,符合并查集判断集合的逻辑,题目:[0108.冗余连接](./0108.冗余连接.md)。 +在[0109.冗余连接II](./0109.冗余连接II.md) 中 对有向树的判断难度更大一些,需要考虑的情况比较多。 -[0108.冗余连接](./0108.冗余连接.md), [0109.冗余连接II](./0109.冗余连接II.md) -后面的两道题目,[0108.冗余连接](./0108.冗余连接.md) 和 +## 最小生成树 +最小生成树是所有节点的最小连通子图, 即:以最小的成本(边的权值)将图中所有节点链接到一起。 +最小生成树算法,有prim 和 kruskal。 + +**prim 算法是维护节点的集合,而 Kruskal 是维护边的集合**。 + +在 稀疏图中,用Kruskal更优。 在稠密图中,用prim算法更优。 + +> 边数量较少为稀疏图,接近或等于完全图(所有节点皆相连)为稠密图 + +Prim 算法 时间复杂度为 O(n^2),其中 n 为节点数量,它的运行效率和图中边树无关,适用稠密图。 + +Kruskal算法 时间复杂度 为 O(nlogn),其中n 为边的数量,适用稀疏图。 + +关于 prim算法,我自创了三部曲,来帮助大家理解: + +1. 第一步,选距离生成树最近节点 +2. 第二步,最近节点加入生成树 +3. 第三步,更新非生成树节点到生成树的距离(即更新minDist数组) + +大家只要理解这三部曲, prim算法 至少是可以写出一个框架出来,然后在慢慢补充细节,这样不至于 自己在写prim的时候 两眼一抹黑 完全凭感觉去写。 + +**minDist数组 是prim算法的灵魂,它帮助 prim算法完成最重要的一步,就是如何找到 距离最小生成树最近的点**。 + +kruscal的主要思路: + +* 边的权值排序,因为要优先选最小的边加入到生成树里 +* 遍历排序后的边 + * 如果边首尾的两个节点在同一个集合,说明如果连上这条边图中会出现环 + * 如果边首尾的两个节点不在同一个集合,加入到最小生成树,并把两个节点加入同一个集合 + +而判断节点是否在一个集合 以及将两个节点放入同一个集合,正是并查集的擅长所在。 + +所以 Kruskal 是需要用到并查集的。 + +这也是我在代码随想录图论编排上 为什么要先 讲解 并查集 在讲解 最小生成树。 -## 最小生成树 ## 拓扑排序 +拓扑排序 是在图上的一种排序。 + +概括来说,**给出一个 有向图,把这个有向图转成线性的排序 就叫拓扑排序**。 + +同样,拓扑排序也可以检测这个有向图 是否有环,即存在循环依赖的情况。 + +拓扑排序的一些应用场景,例如:大学排课,文件下载依赖 等等。 + +只要记住如下两步拓扑排序的过程,代码就容易写了: + +1. 找到入度为0 的节点,加入结果集 +2. 将该节点从图中移除 + ## 最短路算法 +最短路算法是图论中,比较复杂的算法,而且不同的最短路算法都有不同的应用场景。 + +我在 [最短路算法总结篇](./最短路问题总结篇.md) 里已经做了一个高度的概括。 + +大家要时常温故而知新,才能透彻理解各个最短路算法。 + + +## 总结 + +到最后,图论终于剧终了,相信这是市面上大家能看到最全最细致的图论讲解教程。 +图论也是我 《代码随想录》所有章节里 所费精力最大的一个章节。 -算法4,只讲解了 Dijkstra,SPFA (Bellman-Ford算法基于队列) 和 拓扑排序, +只为了不负录友们的期待。 大家加油💪🏻 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 2be97057bf..efe833a7e1 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -62,7 +62,7 @@ 正是因为dfs搜索可一个方向,并需要回溯,所以用递归的方式来实现是最方便的。 -很多录友对回溯很陌生,建议先看看代码随想录,[回溯算法章节](https://programmercarl.com/回溯算法理论基础.html)。 +很多录友对回溯很陌生,建议先看看代码随想录,[回溯算法章节](../回溯算法理论基础.md)。 有递归的地方就有回溯,那么回溯在哪里呢? @@ -78,11 +78,11 @@ void dfs(参数) { 可以看到回溯操作就在递归函数的下面,递归和回溯是相辅相成的。 -在讲解[二叉树章节](https://programmercarl.com/二叉树理论基础.html)的时候,二叉树的递归法其实就是dfs,而二叉树的迭代法,就是bfs(广度优先搜索) +在讲解[二叉树章节](../二叉树理论基础.md)的时候,二叉树的递归法其实就是dfs,而二叉树的迭代法,就是bfs(广度优先搜索) 所以**dfs,bfs其实是基础搜索算法,也广泛应用与其他数据结构与算法中**。 -我们在回顾一下[回溯法](https://programmercarl.com/回溯算法理论基础.html)的代码框架: +我们在回顾一下[回溯法](../回溯算法理论基础.md)的代码框架: ```cpp void backtracking(参数) { @@ -123,9 +123,9 @@ void dfs(参数) { ## 深搜三部曲 -在 [二叉树递归讲解](https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html)中,给出了递归三部曲。 +在 [二叉树递归讲解](../二叉树的递归遍历.md)中,给出了递归三部曲。 -[回溯算法](https://programmercarl.com/回溯算法理论基础.html)讲解中,给出了 回溯三部曲。 +[回溯算法](../回溯算法理论基础.md)讲解中,给出了 回溯三部曲。 其实深搜也是一样的,深搜三部曲如下: diff --git "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index 7f4ee6f87d..54f9153900 100644 --- "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -12,12 +12,15 @@ * bellman_ford 算法判断负权回路 * bellman_ford之单源有限最短路 * Floyd 算法精讲 +* 启发式搜索:A * 算法 最短路算法比较复杂,而且各自有各自的应用场景,我来用一张表把讲过的最短路算法的使用场景都展现出来: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508121355.png) +(因为A * 属于启发式搜索,和上面最短路算法并不是一类,不适合一起对比,所以没有放在一起) + 可能有同学感觉:这个表太复杂了,我记也记不住。 @@ -25,23 +28,25 @@ 这里我给大家一个大体使用场景的分析: -如果遇到单源且边为正数,直接Dijkstra。 +**如果遇到单源且边为正数,直接Dijkstra**。 -至于 使用朴素版还是 堆优化版 还是取决于图的稠密度, 多少节点多少边算是稠密图,多少算是稀疏图,这个没有量化,如果想量化只能写出两个版本然后做实验去测试,不同的判题机得出的结果还不太一样。 +至于 **使用朴素版还是 堆优化版 还是取决于图的稠密度**, 多少节点多少边算是稠密图,多少算是稀疏图,这个没有量化,如果想量化只能写出两个版本然后做实验去测试,不同的判题机得出的结果还不太一样。 一般情况下,可以直接用堆优化版本。 -如果遇到单源边可为负数,直接 Bellman-Ford,同样 SPFA 还是 Bellman-Ford 取决于图的稠密度。 +**如果遇到单源边可为负数,直接 Bellman-Ford**,同样 SPFA 还是 Bellman-Ford 取决于图的稠密度。 一般情况下,直接用 SPFA。 -如果有负权回路,优先 Bellman-Ford, 如果是有限节点最短路 也优先 Bellman-Ford,理由是写代码比较方便。 +**如果有负权回路,优先 Bellman-Ford**, 如果是有限节点最短路 也优先 Bellman-Ford,理由是写代码比较方便。 -如果是遇到多源点求最短路,直接 Floyd。 +**如果是遇到多源点求最短路,直接 Floyd**。 除非 源点特别少,且边都是正数,那可以 多次 Dijkstra 求出最短路径,但这种情况很少,一般出现多个源点了,就是想让你用 Floyd 了。 +对于A * ,由于其高效性,所以在实际工程应用中使用最为广泛 ,由于其 结果的不唯一性,也就是可能是次短路的特性,一般不适合作为算法题。 +游戏开发、地图导航、数据包路由等都广泛使用 A * 算法。 diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index 76e618c29d..e6d25c15e6 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -42,8 +42,7 @@ 那么二维数组直接上图,大家应该就知道怎么回事了 -![算法通关数组2](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%842.png) - +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240606105522.png) **那么二维数组在内存的空间地址是连续的么?** From 0f6ea64f68857ea7c080b820f31b88f622cab276 Mon Sep 17 00:00:00 2001 From: Jack Lin Date: Sat, 15 Jun 2024 15:11:24 +0800 Subject: [PATCH 1156/1533] Update 0242: Add C solution --- ...15\345\274\202\344\275\215\350\257\215.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index ac03ddbb40..61488f03fd 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -383,6 +383,31 @@ object Solution { } ``` +### C + +```c +bool isAnagram(char* s, char* t) { + int len1 = strlen(s), len2 = strlen(t); + if (len1 != len2) { + return false; + } + + int map1[26] = {0}, map2[26] = {0}; + for (int i = 0; i < len1; i++) { + map1[s[i] - 'a'] += 1; + map2[t[i] - 'a'] += 1; + } + + for (int i = 0; i < 26; i++) { + if (map1[i] != map2[i]) { + return false; + } + } + + return true; +} +``` + ## 相关题目 * [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html) From 1d206949dd3649f997868536b66b0d268e365965 Mon Sep 17 00:00:00 2001 From: lesleyzhao Date: Sun, 16 Jun 2024 13:27:11 +0800 Subject: [PATCH 1157/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0417=E4=B8=ADCarl?= =?UTF-8?q?=E9=A2=98=E8=A7=A3C++=E4=BB=A3=E7=A0=81=E9=87=8C=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\213\346\260\264\346\265\201\351\227\256\351\242\230.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index b8448e936d..5218b571aa 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -177,14 +177,14 @@ public: // 记录从大西洋出发,可以遍历的节点 vector> atlantic = vector>(n, vector(m, false)); - - // 从最上最下行的节点出发,向高处遍历 + + // 从最左最右列的节点出发,向高处遍历 for (int i = 0; i < n; i++) { dfs (heights, pacific, i, 0); // 遍历最左列,接触太平洋 dfs (heights, atlantic, i, m - 1); // 遍历最右列,接触大西 } - // 从最左最右列的节点出发,向高处遍历 + // 从最上最下行的节点出发,向高处遍历 for (int j = 0; j < m; j++) { dfs (heights, pacific, 0, j); // 遍历最上行,接触太平洋 dfs (heights, atlantic, n - 1, j); // 遍历最下行,接触大西洋 From 51f7960963061ad15bde0ff90067913d62b960ca Mon Sep 17 00:00:00 2001 From: lesleyzhao Date: Sun, 16 Jun 2024 13:50:09 +0800 Subject: [PATCH 1158/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0417Java=20DFS?= =?UTF-8?q?=E8=B7=9FCarl=20C++=E4=BB=A3=E7=A0=81=E6=80=9D=E8=B7=AF?= =?UTF-8?q?=E6=9B=B4=E4=B8=80=E8=87=B4=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 5218b571aa..5156ce2289 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -297,6 +297,73 @@ class Solution { } ``` +```Java +class Solution { + + // 和Carl题解更加符合的Java DFS + private int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; + + /** + * @param heights 题目给定的二维数组 + * @param m 当前位置的行号 + * @param n 当前位置的列号 + * @param visited 记录这个位置可以到哪条河 + */ + + public void dfs(int[][] heights, boolean[][] visited, int m, int n){ + if(visited[m][n]) return; + visited[m][n] = true; + + for(int[] dir: directions){ + int nextm = m + dir[0]; + int nextn = n + dir[1]; + //出了2D array的边界,continue + if(nextm < 0||nextm == heights.length||nextn <0||nextn== heights[0].length) continue; + //下一个位置比当下位置还要低,跳过,继续找下一个更高的位置 + if(heights[m][n] > heights[nextm][nextn]) continue; + dfs(heights, visited, nextm, nextn); + } + } + + + public List> pacificAtlantic(int[][] heights) { + int m = heights.length; + int n = heights[0].length; + + // 记录从太平洋边出发,可以遍历的节点 + boolean[][] pacific = new boolean[m][n]; + // 记录从大西洋出发,可以遍历的节点 + boolean[][] atlantic = new boolean[m][n]; + + // 从最左最右列的节点出发,向高处遍历 + for(int i = 0; i> result = new ArrayList<>(); + for(int a = 0; a pair = new ArrayList<>(); + pair.add(a); + pair.add(b); + result.add(pair); + } + } + } + return result; + } +} +``` + 广度优先遍历: ```Java From 9aca3b5ca46a3bd39bbf9e15568b5bc390d55466 Mon Sep 17 00:00:00 2001 From: jycathy Date: Sun, 16 Jun 2024 17:32:38 +0800 Subject: [PATCH 1159/1533] fix prob.1207 --- ...272\347\216\260\346\254\241\346\225\260.md" | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index 5c5f92c339..cd89522e4e 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -40,9 +40,9 @@ 回归本题,**本题强调了-1000 <= arr[i] <= 1000**,那么就可以用数组来做哈希,arr[i]作为哈希表(数组)的下标,那么arr[i]可以是负数,怎么办?负数不能做数组下标。 -**此时可以定义一个2000大小的数组,例如int count[2002];**,统计的时候,将arr[i]统一加1000,这样就可以统计arr[i]的出现频率了。 +**此时可以定义一个2001大小的数组,例如int count[2001];**,统计的时候,将arr[i]统一加1000,这样就可以统计arr[i]的出现频率了。 -题目中要求的是是否有相同的频率出现,那么需要再定义一个哈希表(数组)用来记录频率是否重复出现过,bool fre[1002]; 定义布尔类型的就可以了,**因为题目中强调1 <= arr.length <= 1000,所以哈希表大小为1000就可以了**。 +题目中要求的是是否有相同的频率出现,那么需要再定义一个哈希表(数组)用来记录频率是否重复出现过,bool fre[1001]; 定义布尔类型的就可以了,**因为题目中强调1 <= arr.length <= 1000,所以哈希表大小为1000就可以了**。 如图所示: @@ -55,11 +55,11 @@ C++代码如下: class Solution { public: bool uniqueOccurrences(vector& arr) { - int count[2002] = {0}; // 统计数字出现的频率 + int count[2001] = {0}; // 统计数字出现的频率 for (int i = 0; i < arr.size(); i++) { count[arr[i] + 1000]++; } - bool fre[1002] = {false}; // 看相同频率是否重复出现 + bool fre[1001] = {false}; // 看相同频率是否重复出现 for (int i = 0; i <= 2000; i++) { if (count[i]) { if (fre[count[i]] == false) fre[count[i]] = true; @@ -78,7 +78,7 @@ public: ```java class Solution { public boolean uniqueOccurrences(int[] arr) { - int[] count = new int[2002]; + int[] count = new int[2001]; for (int i = 0; i < arr.length; i++) { count[arr[i] + 1000]++; // 防止负数作为下标 } @@ -103,10 +103,10 @@ class Solution { # 方法 1: 数组在哈西法的应用 class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: - count = [0] * 2002 + count = [0] * 2001 for i in range(len(arr)): count[arr[i] + 1000] += 1 # 防止负数作为下标 - freq = [False] * 1002 # 标记相同频率是否重复出现 + freq = [False] * 1001 # 标记相同频率是否重复出现 for i in range(2001): if count[i] > 0: if freq[count[i]] == False: @@ -139,12 +139,12 @@ class Solution: ``` javascript // 方法一:使用数组记录元素出现次数 var uniqueOccurrences = function(arr) { - const count = new Array(2002).fill(0);// -1000 <= arr[i] <= 1000 + const count = new Array(2001).fill(0);// -1000 <= arr[i] <= 1000 for(let i = 0; i < arr.length; i++){ count[arr[i] + 1000]++;// 防止负数作为下标 } // 标记相同频率是否重复出现 - const fre = new Array(1002).fill(false);// 1 <= arr.length <= 1000 + const fre = new Array(1001).fill(false);// 1 <= arr.length <= 1000 for(let i = 0; i <= 2000; i++){ if(count[i] > 0){//有i出现过 if(fre[count[i]] === false) fre[count[i]] = true;//之前未出现过,标记为出现 From 2514a7abbbb7d8290324b767212466260586805d Mon Sep 17 00:00:00 2001 From: MrYoungg <151980452+MrYoungg@users.noreply.github.com> Date: Mon, 17 Jun 2024 00:02:15 +0800 Subject: [PATCH 1160/1533] =?UTF-8?q?Update=200225.=E7=94=A8=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\345\256\236\347\216\260\346\240\210.md" | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index c7dc52f1cc..8b9abd35a8 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -72,6 +72,7 @@ class MyStack { public: queue que1; queue que2; // 辅助队列,用来备份 + /** Initialize your data structure here. */ MyStack() { @@ -100,9 +101,28 @@ public: return result; } - /** Get the top element. */ - int top() { - return que1.back(); + /** Get the top element. + ** Can not use back() direactly. + */ + int top(){ + int size = que1.size(); + size--; + while (size--){ + // 将que1 导入que2,但要留下最后一个元素 + que2.push(que1.front()); + que1.pop(); + } + + int result = que1.front(); // 留下的最后一个元素就是要回返的值 + que2.push(que1.front()); // 获取值后将最后一个元素也加入que2中,保持原本的结构不变 + que1.pop(); + + que1 = que2; // 再将que2赋值给que1 + while (!que2.empty()){ + // 清空que2 + que2.pop(); + } + return result; } /** Returns whether the stack is empty. */ @@ -126,14 +146,17 @@ C++优化代码 class MyStack { public: queue que; + /** Initialize your data structure here. */ MyStack() { } + /** Push element x onto stack. */ void push(int x) { que.push(x); } + /** Removes the element on top of the stack and returns that element. */ int pop() { int size = que.size(); @@ -147,9 +170,21 @@ public: return result; } - /** Get the top element. */ - int top() { - return que.back(); + /** Get the top element. + ** Can not use back() direactly. + */ + int top(){ + int size = que.size(); + size--; + while (size--){ + // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部 + que.push(que.front()); + que.pop(); + } + int result = que.front(); // 此时获得的元素就是栈顶的元素了 + que.push(que.front()); // 将获取完的元素也重新添加到队列尾部,保证数据结构没有变化 + que.pop(); + return result; } /** Returns whether the stack is empty. */ From c4a3fe083fb5f40e232922413195f0cf8856a10f Mon Sep 17 00:00:00 2001 From: MrYoungg <151980452+MrYoungg@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:18:19 +0800 Subject: [PATCH 1161/1533] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2?= =?UTF-8?q?=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index d05f67bc48..48e99c5baa 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -82,7 +82,7 @@ 如动画所示: ![150.逆波兰表达式求值](https://code-thinking.cdn.bcebos.com/gifs/150.逆波兰表达式求值.gif) -相信看完动画大家应该知道,这和[1047. 删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)是差不错的,只不过本题不要相邻元素做消除了,而是做运算! +相信看完动画大家应该知道,这和[1047. 删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)是差不多的,只不过本题不要相邻元素做消除了,而是做运算! C++代码如下: From 4e6052698dccfe08529b39729f77c0da27f06345 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 17 Jun 2024 11:53:52 +0800 Subject: [PATCH 1162/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=9B=BE=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 51 +++++++----- ...17\202\344\274\232dijkstra\345\240\206.md" | 2 + ...74\232dijkstra\346\234\264\347\264\240.md" | 2 + .../0053.\345\257\273\345\256\235-Kruskal.md" | 2 + .../0053.\345\257\273\345\256\235-prim.md" | 2 + ...77\346\215\242\346\225\260\345\255\227.md" | 13 --- ...13\345\255\227\347\254\246\344\270\262.md" | 11 --- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 2 + ...7\347\211\251\350\277\220\350\276\223I.md" | 2 + ...\347\211\251\350\277\220\350\276\223II.md" | 2 + ...347\211\251\350\277\220\350\276\223III.md" | 2 + ...16\351\200\233\345\205\254\345\233\255.md" | 2 + ...57\350\276\276\350\267\257\345\276\204.md" | 2 + ...60\351\207\217\345\271\277\346\220\234.md" | 2 + ...60\351\207\217\346\267\261\346\220\234.md" | 2 + ...00\345\244\247\351\235\242\347\247\257.md" | 2 + ...04\346\200\273\351\235\242\347\247\257.md" | 2 + ...11\346\262\241\345\255\244\345\262\233.md" | 2 + ...64\346\265\201\351\227\256\351\242\230.md" | 2 + ...00\345\244\247\345\262\233\345\261\277.md" | 2 + ...50\345\217\257\350\276\276\346\200\247.md" | 4 +- ...77\347\232\204\345\221\250\351\225\277.md" | 2 + ...50\347\232\204\350\267\257\345\276\204.md" | 2 + ...27\344\275\231\350\277\236\346\216\245.md" | 2 + ...\344\275\231\350\277\236\346\216\245II.md" | 2 + ...46\344\270\262\346\216\245\351\276\231.md" | 2 + ...57\344\273\266\346\236\204\345\273\272.md" | 2 + ...47\346\225\260\345\207\217\346\263\225.md" | 2 + ...13\347\263\225\345\210\207\345\211\262.md" | 49 ++++++++++++ ...46\344\270\262\345\217\230\346\215\242.md" | 79 +++++++++++++++++++ ...21\344\270\212\346\237\223\350\211\262.md" | 5 ++ ...22\345\210\227\350\257\242\351\227\256.md" | 29 +++++++ ...16\350\265\260\345\205\254\350\267\257.md" | 30 +++++++ 33 files changed, 275 insertions(+), 44 deletions(-) create mode 100644 "problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" create mode 100644 "problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" create mode 100644 "problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" create mode 100644 "problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" create mode 100644 "problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" diff --git a/README.md b/README.md index e94b428578..4c570c050d 100644 --- a/README.md +++ b/README.md @@ -372,25 +372,38 @@ 通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor -### 深搜广搜 - -* [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md) -* [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md) -* [图论:广度优先搜索理论基础](./problems/图论广搜理论基础.md) -* [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md) -* [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md) -* [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md) -* [图论:1020.飞地的数量](./problems/1020.飞地的数量.md) -* [图论:130.被围绕的区域](./problems/0130.被围绕的区域.md) -* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md) -* [图论:827.最大人工岛](./problems/0827.最大人工岛.md) -* [图论:127. 单词接龙](./problems/0127.单词接龙.md) -* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md) -* [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md) -* [图论:并查集理论基础](./problems/图论并查集理论基础.md) -* [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md) -* [图论:684.冗余连接](./problems/0684.冗余连接.md) -* [图论:685.冗余连接II](./problems/0685.冗余连接II.md) +1. [图论:理论基础](./problems/kamacoder/图论理论基础.md) +2. [图论:深度优先搜索理论基础](./problems/kamacoder/图论深搜理论基础.md) +3. [图论:所有可达路径](./problems/kamacoder/0098.所有可达路径.md) +4. [图论:广度优先搜索理论基础](./problems/kamacoder/图论广搜理论基础.md) +5. [图论:岛屿数量.深搜版](./problems/kamacoder/0099.岛屿的数量深搜.md) +6. [图论:岛屿数量.广搜版](./problems/kamacoder/0099.岛屿的数量广搜.md) +7. [图论:岛屿的最大面积](./problems/kamacoder/0100.岛屿的最大面积.md) +8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md) +9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md) +10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md) +11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) +12. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) +13. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) +14. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) +15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md) +16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md) +17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md) +18. [图论:冗余连接II](./problems/kamacoder/0109.冗余连接II.md) +19. [图论:最小生成树之prim](./problems/kamacoder/0053.寻宝-prim.md) +20. [图论:最小生成树之kruskal](./problems/kamacoder/0053.寻宝-Kruskal.md) +21. [图论:拓扑排序](./problems/kamacoder/0117.软件构建.md) +22. [图论:dijkstra(朴素版)](./problems/kamacoder/0047.参会dijkstra朴素.md) +23. [图论:dijkstra(堆优化版)](./problems/kamacoder/0047.参会dijkstra堆.md) +24. [图论:Bellman_ford 算法](./problems/kamacoder/0094.城市间货物运输I.md) +25. [图论:Bellman_ford 队列优化算法(又名SPFA)](./problems/kamacoder/0094.城市间货物运输I-SPFA.md) +26. [图论:Bellman_ford之判断负权回路](./problems/kamacoder/0095.城市间货物运输II.md) +27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0095.城市间货物运输II.md) +28. [图论:Floyd 算法](./problems/kamacoder/0097.小明逛公园.md) +29. [图论:A * 算法](./problems/kamacoder/0126.骑士的攻击astar.md) +30. [图论:最短路算法总结篇](./problems/kamacoder/最短路问题总结篇.md) +31. [图论:图论总结篇](./problems/kamacoder/图论总结篇.md) + (持续更新中....) diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index a0778d2db8..6050758240 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # dijkstra(堆优化版)精讲 [卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047) diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 10474f1318..5b214a3454 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # dijkstra(朴素版)精讲 [卡码网:47. 参加科学大会](https://kamacoder.com/problempage.php?pid=1047) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 527f0e70b8..a97409aa3a 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # kruskal算法精讲 [卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index f1b3b721bf..782bd143dd 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # prim算法精讲 [卡码网:53. 寻宝](https://kamacoder.com/problempage.php?pid=1053) diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 45a0aa5461..32d777483e 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -1,11 +1,4 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - # 替换数字 [卡码网题目链接](https://kamacoder.com/problempage.php?pid=1064) @@ -376,9 +369,3 @@ main(); ### Rust: - -

- - - - diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 4dea19a832..28ee5e308e 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -1,9 +1,4 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

# 右旋字符串 @@ -343,9 +338,3 @@ var reverseLeftWords = function(s, n) { ### Rust: - - -

- - - diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index eb3204b612..2480b12c0a 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # Bellman_ford 队列优化算法(又名SPFA) [卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 7cadc37888..f72bfc00a4 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # Bellman_ford 算法精讲 [卡码网:94. 城市间货物运输 I](https://kamacoder.com/problempage.php?pid=1152) diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index ea7065792f..4eab01a8ec 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # bellman_ford之判断负权回路 [卡码网:95. 城市间货物运输 II](https://kamacoder.com/problempage.php?pid=1153) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index 2b9f44fcb7..96f9a9c81e 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # bellman_ford之单源有限最短路 [卡码网:96. 城市间货物运输 III](https://kamacoder.com/problempage.php?pid=1154) diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index f4b53acfaa..5465c356f9 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # Floyd 算法精讲 [卡码网:97. 小明逛公园](https://kamacoder.com/problempage.php?pid=1155) diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index ca4f117c19..c4cfc8f9a4 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 98. 所有可达路径 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1170) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 993c01f225..fc7f38e913 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 99. 岛屿数量 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 7fac4cb8f9..37f7086a2b 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 99. 岛屿数量 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1171) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 2e2ed4f349..19292be7a4 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 100. 岛屿的最大面积 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1172) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 5be256117f..b300b58ca4 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 101. 孤岛的总面积 [卡码网:101. 孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173) diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 43666325f2..5491a8e3d8 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 102. 沉没孤岛 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1174) diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 0f461edb3c..259321b685 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 103. 水流问题 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1175) diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index a1be8fb12a..b68e44424b 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 104.建造最大岛屿 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1176) diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 9903b6d7b4..dfaa3be8ca 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 105.有向图的完全可达性 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1177) @@ -192,7 +194,7 @@ int main() { **第二种写法注意有注释的地方是和写法一的区别** -```c++ +```CPP 写法二:dfs处理下一个要访问的节点 #include #include diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 2233c2db96..340630398f 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 106. 岛屿的周长 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1178) diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 30b8e58701..906609c99a 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 107. 寻找存在的路径 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1179) diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 4e8ea4cfcf..7088413967 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 108. 冗余连接 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1181) diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 09b6118b64..9c2f403941 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 109. 冗余连接II [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index cac33c2304..84b1dd6551 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 110. 字符串接龙 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182) diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index 1cd9a983b1..5d1edf97cd 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 拓扑排序精讲 [卡码网:117. 软件构建](https://kamacoder.com/problempage.php?pid=1191) diff --git "a/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" "b/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" index a36f2cbbe7..84d55249fd 100644 --- "a/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" +++ "b/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" @@ -1,4 +1,6 @@ +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+ # 大数减法 本题测试数据超过int 和 longlong了,所以考察的使用 string 来模拟 两个大数的 加减操作。 diff --git "a/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" "b/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" new file mode 100644 index 0000000000..84bbbd6f94 --- /dev/null +++ "b/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" @@ -0,0 +1,49 @@ + +前缀和 + +```CPP + +#include +#include +#include + +using namespace std; +int main () { + int n, m; + cin >> n >> m; + int sum = 0; + vector> vec(n, vector(m, 0)) ; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> vec[i][j]; + sum += vec[i][j]; + } + } + // 统计横向 + vector horizontal(n, 0); + for (int i = 0; i < n; i++) { + for (int j = 0 ; j < m; j++) { + horizontal[i] += vec[i][j]; + } + } + // 统计纵向 + vector vertical(m , 0); + for (int j = 0; j < m; j++) { + for (int i = 0 ; i < n; i++) { + vertical[j] += vec[i][j]; + } + } + int result = INT_MAX; + int horizontalCut = 0; + for (int i = 0 ; i < n; i++) { + horizontalCut += horizontal[i]; + result = min(result, abs(sum - horizontalCut - horizontalCut)); + } + int verticalCut = 0; + for (int j = 0; j < m; j++) { + verticalCut += vertical[j]; + result = min(result, abs(sum - verticalCut - verticalCut)); + } + cout << result << endl; +} +``` diff --git "a/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" "b/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" new file mode 100644 index 0000000000..cd889995ec --- /dev/null +++ "b/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" @@ -0,0 +1,79 @@ + +# 130.小美的字符串变换 + +本题是[岛屿数量](./0099.岛屿的数量广搜.md)的进阶版,主要思路和代码都是一样的,统计一个图里岛屿的数量,也是染色问题。 + +1、 先枚举各个可能出现的矩阵 +2、 针对矩阵经行广搜染色(深搜,并查集一样可以) +3、 统计岛屿数量最小的数量。 + +```CPP +#include +#include +#include +#include +using namespace std; + +// 广搜代码同 卡码网:99. 岛屿数量 +int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 +void bfs(const vector>& grid, vector>& visited, int x, int y, char a) { + queue> que; + que.push({x, y}); + visited[x][y] = true; // 只要加入队列,立刻标记 + while(!que.empty()) { + pair cur = que.front(); que.pop(); + int curx = cur.first; + int cury = cur.second; + for (int i = 0; i < 4; i++) { + int nextx = curx + dir[i][0]; + int nexty = cury + dir[i][1]; + if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 + if (!visited[nextx][nexty] && grid[nextx][nexty] == a) { + que.push({nextx, nexty}); + visited[nextx][nexty] = true; // 只要加入队列立刻标记 + } + } + } +} + + +int main() { + int n; + string s; + cin >> n; + int result = INT_MAX; + cin >> s; + for (int k = 1; k < n; k++) { + if (n % k != 0) continue; + // 计算出 矩阵的 行 和 列 + int x = n / k; + int y = k; + //cout << x << " " << y << endl; + vector> vec(x, vector(y, 0)); + // 填装矩阵 + int sCount = 0; + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { + vec[i][j] = s[sCount++]; + } + } + + // 开始广搜染色 + vector> visited(x, vector(y, false)); + int count = 0; + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { + + if (!visited[i][j]) { + count++; // 遇到没访问过的陆地,+1 + bfs(vec, visited, i, j, vec[i][j]); // 将与其链接的陆地都标记上 true + } + } + } + // 取岛屿数量最少的 + result = min (result, count); + + } + cout << result << endl; +} +``` diff --git "a/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" "b/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" new file mode 100644 index 0000000000..1f5212b0e5 --- /dev/null +++ "b/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" @@ -0,0 +1,5 @@ +# 131. 小美的树上染色 + +贪心的思路 : https://blog.csdn.net/weixin_43739821/article/details/136299012 + +dp思路:https://www.cnblogs.com/ganyq/p/18111114 diff --git "a/problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" "b/problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" new file mode 100644 index 0000000000..0e58e39a90 --- /dev/null +++ "b/problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" @@ -0,0 +1,29 @@ + +小美的排列询问 + +注意 x 和y 不分先后 + +```CPP + +#include +#include +using namespace std; +int main() { + int n, x, y; + cin >> n; + vector vec(n, 0); + for (int i =0; i < n; i++) { + cin >> vec[i]; + } + cin >> x >> y; + for (int i = 0; i < n - 1; i++) { + if (x == vec[i] && y == vec[i + 1]) || (y == vec[i] && x == vec[i + 1]) ) { + cout << "Yes" << endl; + return 0; + } + } + cout << "No" << endl; + +} + +``` diff --git "a/problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" "b/problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" new file mode 100644 index 0000000000..7d63b7510b --- /dev/null +++ "b/problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" @@ -0,0 +1,30 @@ + + +两个注意点 + +1. x 可以比 y 大,题目没规定 x 和y 的大小顺序 +2. 累计相加的数可能超过int + + +```CPP +#include +#include +using namespace std; +int main () { + int n; + cin >> n; + vector vec(2* n + 1, 0); + for (int i = 1; i <= n; i++) { + cin >> vec[i]; + vec[n + i] = vec[i]; + } + int x, y; + cin >> x >> y; + int xx = min(x ,y); // 注意点1:x 可以比 y 大 + int yy = max(x, y); + long long a = 0, b = 0; // 注意点2:相加的数可能超过int + for (int i = xx; i < yy; i++) a += vec[i]; + for (int i = yy; i < xx + n; i++ ) b += vec[i]; + cout << min(a, b) << endl; +} +``` From 1cf800b89f3785d2cfd7b0dc040da4f294e0682d Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 18 Jun 2024 17:10:19 +0800 Subject: [PATCH 1163/1533] =?UTF-8?q?102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?Go=E4=BD=BF=E7=94=A8=E5=88=87=E7=89=87=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 17832e44ff..3385289894 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -265,7 +265,7 @@ func levelOrder(root *TreeNode) [][]int { ```go /** -102. 二叉树的层序遍历 +102. 二叉树的层序遍历 使用container包 */ func levelOrder(root *TreeNode) [][]int { res := [][]int{} @@ -296,6 +296,35 @@ func levelOrder(root *TreeNode) [][]int { return res } +/** + 102. 二叉树的层序遍历 使用切片 +*/ +func levelOrder(root *TreeNode) [][]int { + res := make([][]int, 0) + if root == nil { + return res + } + queue := make([]*TreeNode, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + level := make([]int, 0) + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + level = append(level, node.Val) + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + res = append(res, level) + } + return res +} + /** 102. 二叉树的层序遍历:使用切片模拟队列,易理解 */ From b2f5b319876dc1852d320c13135e307f91bac5f1 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 20 Jun 2024 10:23:22 +0800 Subject: [PATCH 1164/1533] =?UTF-8?q?102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?Go=E4=BD=BF=E7=94=A8=E5=88=87=E7=89=87=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 265 +++++++++++++++++- 1 file changed, 264 insertions(+), 1 deletion(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 3385289894..cad324148d 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -724,6 +724,41 @@ func levelOrderBottom(root *TreeNode) [][]int { } ``` +```GO +// 使用切片作为队列 +func levelOrderBottom(root *TreeNode) [][]int { + res := make([][]int, 0) + if root == nil { + return res + } + queue := make([]*TreeNode, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + level := make([]int, 0) + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + level = append(level, node.Val) + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + res = append(res, level) + } + l, r := 0, len(res)-1 + for l < r { + res[l], res[r] = res[r], res[l] + l++ + r-- + } + return res +} +``` + #### Javascript: ```javascript @@ -1037,6 +1072,35 @@ func rightSideView(root *TreeNode) []int { } ``` +```GO +// 使用切片作为队列 +func rightSideView(root *TreeNode) []int { + res := make([]int, 0) + if root == nil { + return res + } + queue := make([]*TreeNode, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + if i == size-1 { + res = append(res, node.Val) + } + } + } + return res +} +``` + #### Javascript: ```javascript @@ -1328,6 +1392,35 @@ func averageOfLevels(root *TreeNode) []float64 { } ``` +```GO +// 使用切片作为队列 +func averageOfLevels(root *TreeNode) []float64 { + res := make([]float64, 0) + if root == nil { + return res + } + queue := make([]*TreeNode, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + sum := 0 + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + sum += node.Val + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + res = append(res, float64(sum)/float64(size)) + } + return res +} +``` + #### Javascript: ```javascript @@ -1660,6 +1753,32 @@ func levelOrder(root *Node) [][]int { } ``` +```GO +// 使用切片作为队列 +func levelOrder(root *Node) [][]int { + res := make([][]int, 0) + if root == nil { + return res + } + queue := make([]*Node, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + level := make([]int, 0) + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + level = append(level, node.Val) + if len(node.Children) > 0 { + queue = append(queue, node.Children...) + } + } + res = append(res, level) + } + return res +} +``` + #### JavaScript: ```JavaScript @@ -1959,6 +2078,37 @@ func max(x, y int) int { } ``` +```GO +// 使用切片作为队列 +func largestValues(root *TreeNode) []int { + res := make([]int, 0) + if root == nil { + return res + } + queue := make([]*TreeNode, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + maxValue := math.MinInt64 + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + if node.Val > maxValue { + maxValue = node.Val + } + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + res = append(res, maxValue) + } + return res +} +``` + #### Javascript: ```javascript @@ -2272,6 +2422,34 @@ func connect(root *Node) *Node { ``` +```GO +// 使用切片作为队列 +func connect(root *Node) *Node { + if root == nil { + return root + } + queue := make([]*Node, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + for i := 0; i < size; i++ { + node := queue[i] + if i != size - 1 { + queue[i].Next = queue[i+1] + } + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + queue = queue[size:] + } + return root +} +``` + #### JavaScript: ```javascript @@ -2560,6 +2738,34 @@ func connect(root *Node) *Node { } ``` +```GO +// 使用切片作为队列 +func connect(root *Node) *Node { + if root == nil { + return root + } + queue := make([]*Node, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + for i := 0; i < size; i++ { + node := queue[i] + if i != size - 1 { + queue[i].Next = queue[i+1] + } + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + queue = queue[size:] + } + return root +} +``` + #### JavaScript: ```javascript @@ -2829,6 +3035,33 @@ func maxDepth(root *TreeNode) int { } ``` +```go +// 使用切片作为队列 +func maxDepth(root *TreeNode) int { + if root == nil { + return 0 + } + depth := 0 + queue := make([]*TreeNode, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + } + depth++ + } + return depth +} +``` + #### JavaScript: ```javascript @@ -3102,7 +3335,37 @@ func minDepth(root *TreeNode) int { ans++//记录层数 } - return ans+1 + return ans +} +``` + +```go +// 使用切片作为队列 +func minDepth(root *TreeNode) int { + if root == nil { + return 0 + } + depth := 0 + queue := make([]*TreeNode, 0) + queue = append(queue, root) + for len(queue) > 0 { + size := len(queue) + depth++ + for i := 0; i < size; i++ { + node := queue[0] + queue = queue[1:] + if node.Left != nil { + queue = append(queue, node.Left) + } + if node.Right != nil { + queue = append(queue, node.Right) + } + if node.Left == nil && node.Right == nil { + return depth + } + } + } + return depth } ``` From 0e068f95f2ab7e4aedc25fcafe03c8389096a698 Mon Sep 17 00:00:00 2001 From: wutao Date: Thu, 20 Jun 2024 16:44:42 -0400 Subject: [PATCH 1165/1533] =?UTF-8?q?java=E5=8D=95=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=AD=EF=BC=8C=E5=9B=A0=E4=B8=BA=E6=9C=89?= =?UTF-8?q?=E8=99=9A=E6=8B=9F=E5=A4=B4=E8=8A=82=E7=82=B9=EF=BC=8C=E6=89=80?= =?UTF-8?q?=E4=BB=A5=E4=B8=8D=E7=94=A8=E5=AF=B9Index=3D0=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=E8=BF=9B=E8=A1=8C=E7=89=B9=E6=AE=8A=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 47771d2889..fe8a53004f 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -365,10 +365,7 @@ class MyLinkedList { return; } size--; - if (index == 0) { - head = head.next; - return; - } + //因为有虚拟头节点,所以不用对Index=0的情况进行特殊处理 ListNode pred = head; for (int i = 0; i < index ; i++) { pred = pred.next; From 26a5b0cc214cb89c22aaf549869d391e33949057 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 24 Jun 2024 17:13:15 +0800 Subject: [PATCH 1166/1533] update --- ...17\202\344\274\232dijkstra\345\240\206.md" | 98 +++++++++++++ ...74\232dijkstra\346\234\264\347\264\240.md" | 67 +++++++++ ...57\350\276\276\350\267\257\345\276\204.md" | 13 ++ ...46\344\270\262\346\216\245\351\276\231.md" | 2 +- ...66\350\241\214\345\210\227\345\274\217.md" | 27 ++++ .../0112.\346\214\221\346\210\230boss.md" | 26 ++++ ...04\345\271\263\345\235\207\346\225\260.md" | 29 ++++ ...04\350\243\205\346\211\213\346\234\272.md" | 67 +++++++++ ...7\232\204\346\224\273\345\207\273astar.md" | 2 +- ...22\345\210\227\350\257\242\351\227\256.md" | 4 +- ...16\350\265\260\345\205\254\350\267\257.md" | 7 +- ...13\347\263\225\345\210\207\345\211\262.md" | 4 +- ...46\344\270\262\345\217\230\346\215\242.md" | 1 - ...21\344\270\212\346\237\223\350\211\262.md" | 133 +++++++++++++++++- .../\345\244\271\345\220\203\346\227\227.md" | 47 +++++++ 15 files changed, 518 insertions(+), 9 deletions(-) create mode 100644 "problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" create mode 100644 "problems/kamacoder/0112.\346\214\221\346\210\230boss.md" create mode 100644 "problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" create mode 100644 "problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" rename "problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" => "problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" (87%) rename "problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" => "problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" (69%) create mode 100644 "problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index 6050758240..0452615a61 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -655,6 +655,104 @@ int main() { ### Java +```Java + +import java.util.*; + +class Edge { + int to; // 邻接顶点 + int val; // 边的权重 + + Edge(int to, int val) { + this.to = to; + this.val = val; + } +} + +class MyComparison implements Comparator> { + @Override + public int compare(Pair lhs, Pair rhs) { + return Integer.compare(lhs.second, rhs.second); + } +} + +class Pair { + public final U first; + public final V second; + + public Pair(U first, V second) { + this.first = first; + this.second = second; + } +} + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + List> grid = new ArrayList<>(n + 1); + for (int i = 0; i <= n; i++) { + grid.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int p1 = scanner.nextInt(); + int p2 = scanner.nextInt(); + int val = scanner.nextInt(); + grid.get(p1).add(new Edge(p2, val)); + } + + int start = 1; // 起点 + int end = n; // 终点 + + // 存储从源点到每个节点的最短距离 + int[] minDist = new int[n + 1]; + Arrays.fill(minDist, Integer.MAX_VALUE); + + // 记录顶点是否被访问过 + boolean[] visited = new boolean[n + 1]; + + // 优先队列中存放 Pair<节点,源点到该节点的权值> + PriorityQueue> pq = new PriorityQueue<>(new MyComparison()); + + // 初始化队列,源点到源点的距离为0,所以初始为0 + pq.add(new Pair<>(start, 0)); + + minDist[start] = 0; // 起始点到自身的距离为0 + + while (!pq.isEmpty()) { + // 1. 第一步,选源点到哪个节点近且该节点未被访问过(通过优先级队列来实现) + // <节点, 源点到该节点的距离> + Pair cur = pq.poll(); + + if (visited[cur.first]) continue; + + // 2. 第二步,该最近节点被标记访问过 + visited[cur.first] = true; + + // 3. 第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (Edge edge : grid.get(cur.first)) { // 遍历 cur指向的节点,cur指向的节点为 edge + // cur指向的节点edge.to,这条边的权值为 edge.val + if (!visited[edge.to] && minDist[cur.first] + edge.val < minDist[edge.to]) { // 更新minDist + minDist[edge.to] = minDist[cur.first] + edge.val; + pq.add(new Pair<>(edge.to, minDist[edge.to])); + } + } + } + + if (minDist[end] == Integer.MAX_VALUE) { + System.out.println(-1); // 不能到达终点 + } else { + System.out.println(minDist[end]); // 到达终点最短路径 + } + } +} + +``` + + ### Python ### Go diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 5b214a3454..7328882a1c 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -737,6 +737,73 @@ for (int v = 1; v <= n; v++) { ### Java +```Java +import java.util.Arrays; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + int[][] grid = new int[n + 1][n + 1]; + for (int i = 0; i <= n; i++) { + Arrays.fill(grid[i], Integer.MAX_VALUE); + } + + for (int i = 0; i < m; i++) { + int p1 = scanner.nextInt(); + int p2 = scanner.nextInt(); + int val = scanner.nextInt(); + grid[p1][p2] = val; + } + + int start = 1; + int end = n; + + // 存储从源点到每个节点的最短距离 + int[] minDist = new int[n + 1]; + Arrays.fill(minDist, Integer.MAX_VALUE); + + // 记录顶点是否被访问过 + boolean[] visited = new boolean[n + 1]; + + minDist[start] = 0; // 起始点到自身的距离为0 + + for (int i = 1; i <= n; i++) { // 遍历所有节点 + + int minVal = Integer.MAX_VALUE; + int cur = 1; + + // 1、选距离源点最近且未访问过的节点 + for (int v = 1; v <= n; ++v) { + if (!visited[v] && minDist[v] < minVal) { + minVal = minDist[v]; + cur = v; + } + } + + visited[cur] = true; // 2、标记该节点已被访问 + + // 3、第三步,更新非访问节点到源点的距离(即更新minDist数组) + for (int v = 1; v <= n; v++) { + if (!visited[v] && grid[cur][v] != Integer.MAX_VALUE && minDist[cur] + grid[cur][v] < minDist[v]) { + minDist[v] = minDist[cur] + grid[cur][v]; + } + } + } + + if (minDist[end] == Integer.MAX_VALUE) { + System.out.println(-1); // 不能到达终点 + } else { + System.out.println(minDist[end]); // 到达终点最短路径 + } + } +} + +``` + ### Python ### Go diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index c4cfc8f9a4..159cdb9f04 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -75,6 +75,19 @@ ## 插曲 +------------- + +本题和力扣 [797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/description/) 是一样的,录友了解深度优先搜索之后,这道题目就是模板题,是送分题。 + +力扣是核心代码模式,把图的存储方式给大家定义好了,只需要写出深搜的核心代码就可以。 + +如果笔试的时候出一道原题 (笔试都是ACM模式,部分面试也是ACM模式),不少熟练刷力扣的录友都难住了,因为不知道图应该怎么存,也不知道自己存的图如何去遍历。 + +所以这也是为什么我要让大家练习 ACM模式 + +-------- + + 这道题目是深度优先搜索,比较好的入门题。 如果对深度优先搜索还不够了解,可以先看这里:[深度优先搜索的理论基础](https://programmercarl.com/图论深搜理论基础.html) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 84b1dd6551..5a60dbb7df 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -3,7 +3,7 @@ # 110. 字符串接龙 -[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1182) +[卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1183) 题目描述 diff --git "a/problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" "b/problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" new file mode 100644 index 0000000000..efb304ebd1 --- /dev/null +++ "b/problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" @@ -0,0 +1,27 @@ + +# 111. 构造二阶行列式 + +暴力模拟就好,每个数不超过 20, 暴力枚举其实也没多大。 + +```CPP +#include +using namespace std; +int main() { + int n; + cin >> n; + for (int x = 1; x <= 20; x++) { + for (int y = 1; y <= 20; y++) { + for (int i = 1; i <= 20; i++) { + for (int j = 1; j <= 20; j++) { + if ((x * j - y * i) == n) { + cout << x << " " << y << endl; + cout << i << " " << j << endl; + return 0; + } + } + } + } + } + cout << -1 << endl; +} +``` diff --git "a/problems/kamacoder/0112.\346\214\221\346\210\230boss.md" "b/problems/kamacoder/0112.\346\214\221\346\210\230boss.md" new file mode 100644 index 0000000000..781fb6e802 --- /dev/null +++ "b/problems/kamacoder/0112.\346\214\221\346\210\230boss.md" @@ -0,0 +1,26 @@ + +# 112. 挑战boss + +本题题意有点绕,注意看一下 题目描述中的【提示信息】,但是在笔试中,是不给这样的提示信息的。 + +简单模拟: + +```CPP +#include +using namespace std; +int main() { + int n, a, b, k = 0; + cin >> n >> a >> b; + string s; + cin >> s; + int result = 0; + for (int i = 0; i < s.size(); i++) { + int cur = a + k * b; + result += cur; + ++k; + if (s[i] == 'x') k = 0; + } + cout << result << endl; + return 0; +} +``` diff --git "a/problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" "b/problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" new file mode 100644 index 0000000000..1188d9159d --- /dev/null +++ "b/problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" @@ -0,0 +1,29 @@ + +# 114. 小欧的平均数 + +这道题非常的脑筋急转弯, 读题都要理解半天。 + +初步读题,感觉好像是求 如何最小加减,得到三个数的平均数。 + +但题意不是这样的。 + +小欧的说的三个数平衡,只是三个数里 任何两个数 相加都能被2整除, 那么 也就是说,这三个数 要么都是 奇数,要么都是偶数,才能达到小欧所说的平衡。 + +所以题目要求的,就是,三个数,最小加减1 几次 可以让三个数都变成奇数,或者都变成偶数。 + +所以最终的结果 不是1 就是0,没有其他的。 + +录友可能想,题目出的这么绕干啥? 没办法,企业的笔试题就是这样的。 + +```CPP +#include +#include +using namespace std; +int main() { + int x, y, z; + cin >> x >> y >> z; + int count = (x % 2 == 0) + (y % 2 == 0) + (z % 2 == 0); + cout << min(3 - count, count); +} +``` + diff --git "a/problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" "b/problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" new file mode 100644 index 0000000000..8cae4a78a5 --- /dev/null +++ "b/problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" @@ -0,0 +1,67 @@ + +# 115. 组装手机 + +这道题是比较难得哈希表题目。 把代码随想录哈希表章节理解透彻,做本题没问题。 + +思路是 + +1. 用哈希表记录 外壳售价 和 手机零件售价 出现的次数 +2. 记录总和出现的次数 +3. 遍历总和,减去 外壳售价,看 手机零件售价出现了几次 +4. 最后累加,取最大值 + +有一个需要注意的点: 数字可以重复,在计算个数的时候,如果计算重复的数字 + +例如 如果输入是 + +``` +4 +1 1 1 1 +1 1 1 1 +``` +那么输出应该是 4, 外壳售价 和 手机零件售价 是可以重复的。 + +代码如下: + +```CPP +#include +#include +#include +#include +using namespace std; +int main() { + int n; + cin >> n; + vector aVec(n, 0); + vector bVec(n, 0); + unordered_map aUmap; + unordered_map bUmap; + for (int i = 0; i < n; i++) { + cin >> aVec[i]; + aUmap[aVec[i]]++; + } + for (int i = 0; i < n; i++) { + cin >> bVec[i]; + bUmap[bVec[i]]++; + } + unordered_set uset; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++){ + uset.insert(aVec[i] + bVec[j]); + } + } + int result = 0; + for (int sum : uset) { + //cout << p.first << endl; + int count = 0; + for (pair p : aUmap) { + //cout << p.first - aVec[i] << endl; + if (sum - p.first > 0 && bUmap[sum - p.first] != 0) { + count += min(bUmap[sum - p.first], p.second); + } + } + result = max(result, count); + } + cout << result << endl; +} +``` diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index fe510ae25c..1ee44aace3 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -192,7 +192,7 @@ x1, x2 为起点坐标,y1, y2 为终点坐标 ,abs 为求绝对值,sqrt 计算出来 F 之后,按照 F 的 大小,来选去出队列的节点。 -可以使用 优先级队列 帮我们排好序,每次出队列,就是F最大的节点。 +可以使用 优先级队列 帮我们排好序,每次出队列,就是F最小的节点。 实现代码如下:(启发式函数 采用 欧拉距离计算方式) diff --git "a/problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" "b/problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" similarity index 87% rename from "problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" rename to "problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" index 0e58e39a90..3be7f75bf8 100644 --- "a/problems/kamacoder/\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" +++ "b/problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" @@ -1,7 +1,7 @@ -小美的排列询问 +# 小美的排列询问 -注意 x 和y 不分先后 +模拟题,注意 x 和y 不分先后 ```CPP diff --git "a/problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" "b/problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" similarity index 69% rename from "problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" rename to "problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" index 7d63b7510b..dea68b9090 100644 --- "a/problems/kamacoder/\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" +++ "b/problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" @@ -1,6 +1,11 @@ +# 小美走公路 -两个注意点 +在处理环形情况的时候,很多录友容易算懵了,不是多算一个数,就是少算一个数。 + +这里这样的题目,最好的方式是将 两个环展开,首尾相连,这样我们就可以通过 直线的思维去解题了 + +两个注意点: 1. x 可以比 y 大,题目没规定 x 和y 的大小顺序 2. 累计相加的数可能超过int diff --git "a/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" "b/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" index 84bbbd6f94..ae77478f13 100644 --- "a/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" +++ "b/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" @@ -1,5 +1,7 @@ -前缀和 +# 小美的蛋糕切割 + +二维前缀和,不了解前缀和的录友 可以自行查一下,是一个很容易理解的算法思路 ```CPP diff --git "a/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" "b/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" index cd889995ec..9f9d789930 100644 --- "a/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" +++ "b/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" @@ -36,7 +36,6 @@ void bfs(const vector>& grid, vector>& visited, int x, } } - int main() { int n; string s; diff --git "a/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" "b/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" index 1f5212b0e5..36b3c38cc2 100644 --- "a/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" +++ "b/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" @@ -1,5 +1,134 @@ # 131. 小美的树上染色 -贪心的思路 : https://blog.csdn.net/weixin_43739821/article/details/136299012 +本题为树形dp 稍有难度,主要在于 递推公式上。 + +dp数组的定义: + +dp[cur][1] :当前节点染色,那么当前节点为根节点及其左右子节点中,可以染色的最大数量 + +dp[cur][0] :当前节点不染色,那么当前节点为根节点及其左右子节点中,可以染色的最大数量 + +关于 dp转移方程 + +1、 情况一: + +如果当前节点不染色,那就去 子节点 染色 或者 不染色的最大值。 + +`dp[cur][0] += max(dp[child][0], dp[child][1]);` + + +2、情况二: + +那么当前节点染色的话,这种情况就不好想了。 + +首先这不是二叉树,每一个节点都有可能 会有n个子节点。 + +所以我们要分别讨论,每一个子节点的情况 对父节点的影响。 + +那么父节点 针对每种情况,就要去 最大值, 也就是 `dp[cur][1] = max(dp[cur][1], 每个自孩子的情况)` + +如图,假如节点1 是我们要计算的父节点,节点2是我们这次要计算的子节点。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240617204601.png) + +选中一个节点2 作为我们这次计算的子节点,父节点染色的话,子节点必染色。 + +接下来就是计算 父节点1和该子节点2染色的话, 以子节点2 为根的 染色节点的最大数量 。 + +是:节点2不染色 且 以节点2为根节点的最大 染色数量 + 2, + 2 是因为 节点 1 和 节点2 要颜色了,染色节点增加两个。 + +代码:`dp[child][0] + 2` + +细心的录友会发现,那我们只计算了 红色框里面的,那么框外 最大的染色数量是多少呢? + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240617205709.png) + + +先看 作为子节点的节点2 为根节点的最大染色数量是多少? 取一个最值,即 节点2染色 或者 不染色取最大值。 + +代码:`max(dp[child][0], dp[child][1])` + +那么红框以外的 染色最大节点数量 就是 `dp[cur][0] - max(dp[child][0], dp[child][1])` + +(cur是节点1,child是节点2) + +红框以外的染色最大数量 + 父节点1和该子节点2染色的话 以子节点2 为根的 染色节点的最大数量 就是 节点1 染色的最大节点数量。 + +代码: + +`dp[cur][1] = max(dp[cur][1], dp[cur][0] - max(dp[child][0], dp[child][1]) + dp[child][0] + 2);` + +整体代码如下: + +```CPP + +#include +#include +#include +#include +#include + +using namespace std; + +int maxN = 10005; +vector> dp (maxN, vector(2, 0)); +vector> grid(maxN); // 邻接表 +vector value(maxN); // 存储每个节点的权值 + + +// 在树上进行动态规划的函数 +void dpOnTheTree(int cur) { + + for (int child : grid[cur]) { + // 后序遍历,从下向上计算 + dpOnTheTree(child); + // 情况一 + dp[cur][0] += max(dp[child][0], dp[child][1]); + + } + + // 计算dp[1] - 当前节点染色 + for (int child : grid[cur]) { + long mul = value[cur] * value[child]; // 当前节点和相邻节点权值的乘积 + long sqrtNum = (long) sqrt(mul); + + if (sqrtNum * sqrtNum == mul) { // 如果乘积是完全平方数 + // 情况二 + // dp[cur][0] 表示所有子节点 染色或者不染色的 最大染色数量 + // max(dp[child][0], dp[child][1]) 需要染色节点的孩子节点的最大染色数量 + // dp[cur][0] - max(dp[child][0], dp[child][1]) 除了要染色的节点及其子节点,其他孩子的最大染色数量 + // 最后 + dp[child][0] + 2 , 就是本节点染色的最大染色节点数量 + dp[cur][1] = max(dp[cur][1], dp[cur][0] - max(dp[child][0], dp[child][1]) + dp[child][0] + 2); + } + } + +} + +int main() { + + int n; + cin >> n; // 输入节点数量 + + // 读取节点权值 + for (int i = 1; i <= n; ++i) { + cin >> value[i]; + } + + // 构建树的邻接表 + for (int i = 1; i < n; ++i) { + int x, y; + cin >> x >> y; + grid[x].push_back(y); + } + + // 从根节点(节点1)开始进行动态规划 + dpOnTheTree(1); + + // 输出最大染色节点数量 + cout << max(dp[1][0], dp[1][1]) << endl; + + return 0; +} + +``` -dp思路:https://www.cnblogs.com/ganyq/p/18111114 diff --git "a/problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" "b/problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" new file mode 100644 index 0000000000..a53568dd9c --- /dev/null +++ "b/problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" @@ -0,0 +1,47 @@ + +#include + +using namespace std; + +int main() { + int t = 0; + + cin >> t; + + while(t--) { + vector grid(3, ""); + int a = 0; + int b = 0; + for(int i = 0; i < 3; i++) { + cin >> grid[i]; + if(grid[i] == "o*o") { + a++; + } else if(grid[i] == "*o*") { + b++; + } + } + // 判断列 + for(int i = 0; i < 3; i++) { + string line(1, grid[0][i]); + line += grid[1][i]; + line += grid[2][i]; + + if(line == "o*o") { + a++; + } else if(line == "*o*") { + b++; + } + } + if((a && b) || (!a && !b)) { + cout << "draw" << endl; + } + if(a && !b) { + cout << "yukan" << endl; + } + if(!a && b) { + cout << "kou" << endl; + } + } + + return 0; +} From 5cb1a4c202018e2cd77d081e72f65dd801a8ed9f Mon Sep 17 00:00:00 2001 From: lizhuo Date: Mon, 24 Jun 2024 18:43:39 +0800 Subject: [PATCH 1167/1533] =?UTF-8?q?doc=EF=BC=9A=E3=80=8A=E5=A6=82?= =?UTF-8?q?=E4=BD=95=E5=9C=A8Github=E4=B8=8A=E6=8F=90=E4=BA=A4PR=E3=80=8B?= =?UTF-8?q?=E6=96=87=E5=AD=97=E7=BC=BA=E5=B0=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/qita/join.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/qita/join.md b/problems/qita/join.md index 62dec674d5..c7e17588bf 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -188,7 +188,7 @@ python代码 大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。
-一天晚分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] +一天晚上分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] 那么在处理冲突的时候 保留谁的代码,删点谁的代码呢? From 2b6c2b509822ac66726f4b839349e499f009dd08 Mon Sep 17 00:00:00 2001 From: SusanAIFF <50587627+SusanAIFF@users.noreply.github.com> Date: Wed, 26 Jun 2024 15:43:39 +0800 Subject: [PATCH 1168/1533] =?UTF-8?q?Update=200101.=E5=AD=A4=E5=B2=9B?= =?UTF-8?q?=E7=9A=84=E6=80=BB=E9=9D=A2=E7=A7=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新了101.孤岛总面积 - python 广搜实现版本 --- ...04\346\200\273\351\235\242\347\247\257.md" | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index b300b58ca4..25de67ecd2 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -186,7 +186,56 @@ int main() { ### Java ### Python - +```python +from collections import deque + +# 处理输入 +n, m = list(map(int, input().strip().split())) +g = [] +for _ in range(n): + row = list(map(int, input().strip().split())) + g.append(row) + +# 定义四个方向、孤岛面积(遍历完边缘后会被重置) +directions = [[0,1], [1,0], [-1,0], [0,-1]] +count = 0 + +# 广搜 +def bfs(r, c): + global count + q = deque() + q.append((r, c)) + g[r][c] = 0 + count += 1 + + while q: + r, c = q.popleft() + for di in directions: + next_r = r + di[0] + next_c = c + di[1] + if next_c < 0 or next_c >= m or next_r < 0 or next_r >= n: + continue + if g[next_r][next_c] == 1: + q.append((next_r, next_c)) + g[next_r][next_c] = 0 + count += 1 + + +for i in range(n): + if g[i][0] == 1: bfs(i, 0) + if g[i][m-1] == 1: bfs(i, m-1) + +for i in range(m): + if g[0][i] == 1: bfs(0, i) + if g[n-1][i] == 1: bfs(n-1, i) + +count = 0 +for i in range(n): + for j in range(m): + if g[i][j] == 1: bfs(i, j) + +print(count) +``` ### Go ### Rust From c4eb2cb824f8415e28519911abd0780cdae899eb Mon Sep 17 00:00:00 2001 From: Erudito Soul <20226472@stu.neu.edu.cn> Date: Wed, 26 Jun 2024 21:17:00 +0800 Subject: [PATCH 1169/1533] =?UTF-8?q?Update=200098.=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E8=BE=BE=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 提供了Java和Python版本代码 --- ...57\350\276\276\350\267\257\345\276\204.md" | 179 +++++++++++++++++- 1 file changed, 178 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index c4cfc8f9a4..3a1ae6851d 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -409,9 +409,186 @@ int main() { ## 其他语言版本 ### Java +#### 邻接矩阵写法 +```java +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Main { + static List> result = new ArrayList<>(); // 收集符合条件的路径 + static List path = new ArrayList<>(); // 1节点到终点的路径 + + public static void dfs(int[][] graph, int x, int n) { + // 当前遍历的节点x 到达节点n + if (x == n) { // 找到符合条件的一条路径 + result.add(new ArrayList<>(path)); + return; + } + for (int i = 1; i <= n; i++) { // 遍历节点x链接的所有节点 + if (graph[x][i] == 1) { // 找到 x链接的节点 + path.add(i); // 遍历到的节点加入到路径中来 + dfs(graph, i, n); // 进入下一层递归 + path.remove(path.size() - 1); // 回溯,撤销本节点 + } + } + } -### Python + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + // 节点编号从1到n,所以申请 n+1 这么大的数组 + int[][] graph = new int[n + 1][n + 1]; + + for (int i = 0; i < m; i++) { + int s = scanner.nextInt(); + int t = scanner.nextInt(); + // 使用邻接矩阵表示无向图,1 表示 s 与 t 是相连的 + graph[s][t] = 1; + } + + path.add(1); // 无论什么路径已经是从1节点出发 + dfs(graph, 1, n); // 开始遍历 + + // 输出结果 + if (result.isEmpty()) System.out.println(-1); + for (List pa : result) { + for (int i = 0; i < pa.size() - 1; i++) { + System.out.print(pa.get(i) + " "); + } + System.out.println(pa.get(pa.size() - 1)); + } + } +} +``` + +#### 邻接表写法 +```java +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; + +public class Main { + static List> result = new ArrayList<>(); // 收集符合条件的路径 + static List path = new ArrayList<>(); // 1节点到终点的路径 + + public static void dfs(List> graph, int x, int n) { + if (x == n) { // 找到符合条件的一条路径 + result.add(new ArrayList<>(path)); + return; + } + for (int i : graph.get(x)) { // 找到 x指向的节点 + path.add(i); // 遍历到的节点加入到路径中来 + dfs(graph, i, n); // 进入下一层递归 + path.remove(path.size() - 1); // 回溯,撤销本节点 + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + // 节点编号从1到n,所以申请 n+1 这么大的数组 + List> graph = new ArrayList<>(n + 1); + for (int i = 0; i <= n; i++) { + graph.add(new LinkedList<>()); + } + + while (m-- > 0) { + int s = scanner.nextInt(); + int t = scanner.nextInt(); + // 使用邻接表表示 s -> t 是相连的 + graph.get(s).add(t); + } + path.add(1); // 无论什么路径已经是从1节点出发 + dfs(graph, 1, n); // 开始遍历 + + // 输出结果 + if (result.isEmpty()) System.out.println(-1); + for (List pa : result) { + for (int i = 0; i < pa.size() - 1; i++) { + System.out.print(pa.get(i) + " "); + } + System.out.println(pa.get(pa.size() - 1)); + } + } +} +``` +### Python +#### 邻接矩阵写法 +``` python +def dfs(graph, x, n, path, result): + if x == n: + result.append(path.copy()) + return + for i in range(1, n + 1): + if graph[x][i] == 1: + path.append(i) + dfs(graph, i, n, path, result) + path.pop() + +def main(): + n, m = map(int, input().split()) + graph = [[0] * (n + 1) for _ in range(n + 1)] + + for _ in range(m): + s, t = map(int, input().split()) + graph[s][t] = 1 + + result = [] + dfs(graph, 1, n, [1], result) + + if not result: + print(-1) + else: + for path in result: + print(' '.join(map(str, path))) + +if __name__ == "__main__": + main() +``` + +#### 邻接表写法 +``` python +from collections import defaultdict + +result = [] # 收集符合条件的路径 +path = [] # 1节点到终点的路径 + +def dfs(graph, x, n): + if x == n: # 找到符合条件的一条路径 + result.append(path.copy()) + return + for i in graph[x]: # 找到 x指向的节点 + path.append(i) # 遍历到的节点加入到路径中来 + dfs(graph, i, n) # 进入下一层递归 + path.pop() # 回溯,撤销本节点 + +def main(): + n, m = map(int, input().split()) + + graph = defaultdict(list) # 邻接表 + for _ in range(m): + s, t = map(int, input().split()) + graph[s].append(t) + + path.append(1) # 无论什么路径已经是从1节点出发 + dfs(graph, 1, n) # 开始遍历 + + # 输出结果 + if not result: + print(-1) + for pa in result: + print(' '.join(map(str, pa))) + +if __name__ == "__main__": + main() +``` ### Go ### Rust From 718d3c065a351547f1c95a1941372b40e36e07e8 Mon Sep 17 00:00:00 2001 From: SteveL <66228787+stevenleon99@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:33:01 -0400 Subject: [PATCH 1170/1533] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add 剪枝 to the second for loop --- ...0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 9c8bb4fe24..8e34713d37 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -262,6 +262,11 @@ class Solution { for (int j = i + 1; j < nums.length; j++) { + // nums[i]+nums[j] > target 直接返回, 剪枝操作 + if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) { + return result; + } + if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重 continue; } From 686c3a5580fa79b5c22a2b783301f82b41769e8d Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sun, 30 Jun 2024 15:46:16 +0800 Subject: [PATCH 1171/1533] Update --- ...8.\345\214\272\351\227\264\345\222\214.md" | 191 ++++++++++++++++++ ...50\345\217\257\350\276\276\346\200\247.md" | 2 +- ...7\232\204\346\224\273\345\207\273astar.md" | 5 + ...2.\345\244\271\345\220\203\346\227\227.md" | 47 +++++ ...17\347\272\242\344\271\260\350\215\257.md" | 48 +++++ ...00\345\260\217\346\255\245\346\225\260.md" | 77 +++++++ .../\345\244\271\345\220\203\346\227\227.md" | 47 ----- ...62\345\244\204\347\220\206\345\231\250.md" | 50 +++++ ...02\347\202\271\345\210\227\350\241\250.md" | 47 +++++ 9 files changed, 466 insertions(+), 48 deletions(-) create mode 100644 "problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" create mode 100644 "problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" create mode 100644 "problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" create mode 100644 "problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" delete mode 100644 "problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" create mode 100644 "problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" create mode 100644 "problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" new file mode 100644 index 0000000000..a4bb54e940 --- /dev/null +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -0,0 +1,191 @@ + +# 58. 区间和 + +[题目链接](https://kamacoder.com/problempage.php?pid=1070) + +题目描述 + +给定一个整数数组 Array,请计算该数组在每个指定区间内元素的总和。 + +输入描述 + +第一行输入为整数数组 Array 的长度 n,接下来 n 行,每行一个整数,表示数组的元素。随后的输入为需要计算总和的区间,直至文件结束。 + +输出描述 + +输出每个指定区间内元素的总和。 + +输入示例 + +``` +5 +1 +2 +3 +4 +5 +0 1 +1 3 +``` + +输出示例 + +``` +3 +9 +``` + +数据范围: + +0 < n <= 100000 + +## 思路 + +本题我们来讲解 数组 上常用的解题技巧:前缀和 + +首先来看本题,我们最直观的想法是什么? + +那就是给一个区间,然后 把这个区间的和都累加一遍不就得了,是一道简单不能再简单的题目。 + +代码如下: + +```CPP +#include +#include +using namespace std; +int main() { + int n, a, b; + cin >> n; + vector vec(n); + for (int i = 0; i < n; i++) cin >> vec[i]; + while (cin >> a >> b) { + int sum = 0; + // 累加区间 a 到 b 的和 + for (int i = a; i <= b; i++) sum += vec[i]; + cout << sum << endl; + } +} +``` + +代码一提交,发现超时了..... + +我在制作本题的时候,特别制作了大数据量查询,卡的就是这种暴力解法。 + +来举一个极端的例子,如果我查询m次,每次查询的范围都是从0 到 n - 1 + +那么该算法的时间复杂度是 O(n * m) m 是查询的次数 + +如果查询次数非常大的话,这个时间复杂度也是非常大的。 + +接下来我们来引入前缀和,看看前缀和如何解决这个问题。 + +前缀和的思想是重复利用计算过的子数组之和,从而降低区间查询需要累加计算的次数。 + +**前缀和 在涉及计算区间和的问题时非常有用**! + +前缀和的思路其实很简单,我给大家举个例子很容易就懂了。 + +例如,我们要统计 vec[i] 这个数组上的区间和。 + +我们先做累加,即 p[i] 表示 下标 0 到 i 的 vec[i] 累加 之和。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627110604.png) + + +如果,我们想统计,在vec数组上 下标 2 到下标 5 之间的累加和,那是不是就用 p[5] - p[1] 就可以了。 + +为什么呢? + +p[1] = vec[0] + vec[1]; + +p[5] = vec[0] + vec[1] + vec[2] + vec[3] + vec[4] + vec[5]; + +p[5] - p[1] = vec[2] + vec[3] + vec[4] + vec[5]; + +这不就是我们要求的 下标 2 到下标 5 之间的累加和吗。 + +如图所示: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627111319.png) + +p[5] - p[1] 就是 红色部分的区间和。 + +而 p 数组是我们之前就计算好的累加和,所以后面每次求区间和的之后 我们只需要 O(1)的操作。 + + + +```CPP +#include +#include +using namespace std; +int main() { + int n, a, b; + cin >> n; + vector vec(n); + vector p(n); + int presum = 0; + for (int i = 0; i < n; i++) { + cin >> vec[i]; + presum += vec[i]; + p[i] = presum; + } + + while (cin >> a >> b) { + int sum; + if (a == 0) sum = p[b]; + else sum = p[b] - p[a - 1]; + cout << sum << endl; + } +} + +``` + +```CPP +#include +#include +using namespace std; +int main() { + int n, a, b; + cin >> n; + vector vec(n); + vector p(n); + int presum = 0; + for (int i = 0; i < n; i++) { + scanf("%d", &vec[i]); + presum += vec[i]; + p[i] = presum; + } + + while (~scanf("%d%d", &a, &b)) { + int sum; + if (a == 0) sum = p[b]; + else sum = p[b] - p[a - 1]; + printf("%d\n", sum); + } +} + +``` + +```CPP + +#include +using namespace std; + + +int main(){ + int n, a, b; + cin >> n; + vector vec(n + 1); + vector p(n + 1, 0); + for(int i = 1; i <= n; i++) { + scanf("%d", &vec[i]); + p[i] = p[i - 1] + vec[i]; + } + while(~scanf("%d%d", &a, &b)){ + printf("%d\n", p[b + 1] - p[a]); + } + return 0; +} +``` diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index dfaa3be8ca..03048825c5 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -24,7 +24,7 @@ 1 2 2 1 1 3 -3 4 +2 4 ``` 【输出示例】 diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 1ee44aace3..26f4529bc7 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -7,6 +7,10 @@ 在象棋中,马和象的移动规则分别是“马走日”和“象走田”。现给定骑士的起始坐标和目标坐标,要求根据骑士的移动规则,计算从起点到达目标点所需的最短步数。 +骑士移动规则如图,红色是起始位置,黄色是骑士可以走的地方。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240626104833.png) + 棋盘大小 1000 x 1000(棋盘的 x 和 y 坐标均在 [1, 1000] 区间内,包含边界) 输入描述 @@ -42,6 +46,7 @@ 0 ``` + ## 思路 我们看到这道题目的第一个想法就是广搜,这也是最经典的广搜类型题目。 diff --git "a/problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" "b/problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" new file mode 100644 index 0000000000..2ec50bfb12 --- /dev/null +++ "b/problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" @@ -0,0 +1,47 @@ + +# 132. 夹吃棋 + +[题目链接](https://kamacoder.com/problempage.php?pid=1209) + +这道题是模拟题,但很多录友可能想复杂了。 + +行方向,白棋吃,只有这样的布局 `o*o`,黑棋吃,只有这样的布局 `*o*` + +列方向也是同理的。 + +想到这一点,本题的代码就容易写了, C++代码如下: + +```CPP +#include +#include +using namespace std; +int main() { + int n; + cin >> n; + while (n--) { + int black = 0, white = 0; + vector grid(3, ""); + // 判断行 + for (int i = 0; i < 3; i++) { + cin >> grid[i]; + if (grid[i] == "o*o") white++; + if (grid[i] == "*o*") black++; + } + // 判断列 + for (int i = 0; i < 3; i++) { + string s; + s += grid[0][i]; + s += grid[1][i]; + s += grid[2][i]; + if (s == "o*o") white++; + if (s == "*o*") black++; + } + // 如果一个棋盘的局面没有一方被夹吃或者黑白双方都被对面夹吃,则认为是平局 + if ((!white && !black) || (white && black)) cout << "draw" << endl; + // 白棋赢 + else if (white && !black) cout << "yukan" << endl; + // 黑棋赢 + else cout << "kou" << endl; + } +} +``` diff --git "a/problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" "b/problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" new file mode 100644 index 0000000000..2b144fa5c5 --- /dev/null +++ "b/problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" @@ -0,0 +1,48 @@ + +# 133. 小红买药 + +[题目链接](https://kamacoder.com/problempage.php?pid=1210) + +本题是一道直观的模拟题,但也并不简单,很多情况容易漏了,笔试现场可能要多错几次 才能把情况都想到。 + +主要是三个情况: + +* 小红没症状,药有副作用,统计加一,同时要给小红标记上症状 +* 小红有症状,药治不了,同时也没副症状 ,这时也要统计加一 +* 小红有症状,药可以治,给小红取消症状标记 + + +```CPP +#include +#include +using namespace std; +int main() { + int n, m, q, u; + cin >> n; + string s; + cin >> s; + cin >> m; + vector a(m + 1); // 因为后面u是从1开始的 + vector b(m + 1); + for (int i = 1; i <= m; i++) { + cin >> a[i] >> b[i]; + } + cin >> q; + while (q--) { + cin >> u; + int num = 0; + for (int i = 0; i < n; i++) { + // s 没症状,但b给了副作用,统计num的同时,要给s标记上症状 + if (s[i] == '0' && b[u][i] == '1') { + num ++; + s[i] = '1'; + } + // s 有症状,但 a治不了,b也没副症状 + else if (s[i] == '1' && a[u][i] == '0' && a[u][i] == '0') num++; + // s 有症状,a 可以治 + else if (s[i] == '1' && a[u][i] == '1') s[i] = '0'; + } + cout << num << endl; + } +} +``` diff --git "a/problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" "b/problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" new file mode 100644 index 0000000000..ca681df412 --- /dev/null +++ "b/problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" @@ -0,0 +1,77 @@ + +# 134. 皇后移动的最小步数 + +[题目链接](https://kamacoder.com/problempage.php?pid=1211) + +本题和 [代码随想录-不同路径](https://www.programmercarl.com/0062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84.html) 有一些类似。 + +关键是弄清楚递推公式 + +一共分三个情况, + +情况一,向右移动: + +然后从 (i, j) 再向右走 到 (i, k)。 无论k 多大,步数只加1 : + +`dp[i][k] = dp[i][j] + 1` + +那么 `dp[i][k]` 也有可能 从其他方向得到,例如 从上到下, 或者斜上方到达 dp[i][k] + +本题我们要求最小步数,所以取最小值:`dp[i][k] = min(dp[i][k], dp[i][j] + 1);` + +情况二,向下移动: + +从 (i, j) 再向下走 到 (k, j)。 无论k 多大,步数只加1 : + +`dp[k][j] = dp[i][j] + 1;` + +同理 `dp[i][k]` 也有可能 从其他方向得到,取最小值:`dp[k][j] = min(dp[k][j], dp[i][j] + 1);` + +情况三,右下方移动: + +从 (i, j) 再向右下方移动 到 (i + k, j + k)。 无论k 多大,步数只加1 : + +`dp[i + k][j + k] = dp[i][j] + 1` + +同理 `dp[i + k][j + k]` 也有可能 从其他方向得到,取最小值:`dp[i + k][j + k] = min(dp[i + k][j + k], dp[i][j] + 1);` + + +```CPP +#include +#include +using namespace std; +const int INF = 4e6; // 最多步数也就是 2000 * 2000 +int main() { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + vector> dp(n, vector(m, INF)); + dp[0][0] = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == '*') continue; + // 向右移动k个格子 + for (int k = j + 1; k < m && grid[i][k] == '.'; k++) { + dp[i][k] = min(dp[i][k], dp[i][j] + 1); + } + // 向下移动 k个格子 + for (int k = i + 1; k < n && grid[k][j] == '.'; k++) { + dp[k][j] = min(dp[k][j], dp[i][j] + 1); + } + // 向右下移动k个格子 + for (int k = 1; i + k < n && j + k < m && grid[i + k][j + k] == '.'; k++) { + dp[i + k][j + k] = min(dp[i + k][j + k], dp[i][j] + 1); + } + } + } + if (dp[n - 1][m - 1] == INF) cout << -1 << endl; + else cout << dp[n - 1][m - 1] << endl; +} +``` + + diff --git "a/problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" "b/problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" deleted file mode 100644 index a53568dd9c..0000000000 --- "a/problems/kamacoder/\345\244\271\345\220\203\346\227\227.md" +++ /dev/null @@ -1,47 +0,0 @@ - -#include - -using namespace std; - -int main() { - int t = 0; - - cin >> t; - - while(t--) { - vector grid(3, ""); - int a = 0; - int b = 0; - for(int i = 0; i < 3; i++) { - cin >> grid[i]; - if(grid[i] == "o*o") { - a++; - } else if(grid[i] == "*o*") { - b++; - } - } - // 判断列 - for(int i = 0; i < 3; i++) { - string line(1, grid[0][i]); - line += grid[1][i]; - line += grid[2][i]; - - if(line == "o*o") { - a++; - } else if(line == "*o*") { - b++; - } - } - if((a && b) || (!a && !b)) { - cout << "draw" << endl; - } - if(a && !b) { - cout << "yukan" << endl; - } - if(!a && b) { - cout << "kou" << endl; - } - } - - return 0; -} diff --git "a/problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" "b/problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" new file mode 100644 index 0000000000..bdd8222ed5 --- /dev/null +++ "b/problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" @@ -0,0 +1,50 @@ + + +```CPP +#include +using namespace std; + int main() { + int index = 0; + long long optNum; + string s; + string cmd; + while(cin >> cmd){ + //cout << s << endl; + if(cmd == "insert"){ + string buff; + cin >> buff; + s.insert(index, buff); + index += buff.size(); + } + else if(cmd == "move"){ + cin >> optNum; + if(optNum > 0 && index + optNum <= s.size()) index += optNum; + if(optNum < 0 && index >= -optNum) index += optNum; + } + else if(cmd == "delete"){ + cin >> optNum; + if(index >= optNum && optNum != 0){ + s.erase(index - optNum, optNum); + index -= optNum; + } + } + else if(cmd == "copy"){ + if(index > 0) { + string tmp = s.substr(0, index); + s.insert(index, tmp); + } + } + else if(cmd == "end"){ + for(int i = 0; i < index; i++) { + cout << s[i]; + } + cout << '|'; + for(int i = index; i < s.size(); i++){ + cout << s[i]; + } + break; + } + } + return 0; + } +``` diff --git "a/problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" "b/problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" new file mode 100644 index 0000000000..791b6b6819 --- /dev/null +++ "b/problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" @@ -0,0 +1,47 @@ + + + +```CPP +#include +#include +#include +#include +using namespace std; +int main() { + unordered_set uset; + int n, a; + cin >> n; + while (n--) { + cin >> a; + uset.insert(a); + } + int m, x, vlan_id; + long long tb; + vector vecTB; + cin >> m; + while(m--) { + cin >> tb; + cin >> x; + vector vecVlan_id(x); + for (int i = 0; i < x; i++) { + cin >> vecVlan_id[i]; + } + for (int i = 0; i < x; i++) { + if (uset.find(vecVlan_id[i]) != uset.end()) { + vecTB.push_back(tb); + break; + } + } + + } + + cout << vecTB.size() << endl; + if (vecTB.size() != 0) { + sort(vecTB.begin(), vecTB.end()); + for (int i = 0; i < vecTB.size() ; i++) cout << vecTB[i] << " "; + } + + +} + +``` From 2b93089f56765f3f48872e657a6c742e689c901f Mon Sep 17 00:00:00 2001 From: KaiserTT <116904940+KaiserTT@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:06:09 +0800 Subject: [PATCH 1172/1533] add 0117 Java and Python --- ...57\344\273\266\346\236\204\345\273\272.md" | 101 +++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index 5d1edf97cd..05cb735815 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -167,7 +167,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510114004.png) --------------- +-------------- 后面的过程一样的,节点3 和 节点4,入度都为0,选哪个都行。 @@ -344,8 +344,107 @@ int main() { ### Java +```java +import java.util.*; + + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + List> umap = new ArrayList<>(); // 记录文件依赖关系 + int[] inDegree = new int[n]; // 记录每个文件的入度 + + for (int i = 0; i < n; i++) + umap.add(new ArrayList<>()); + + for (int i = 0; i < m; i++) { + int s = scanner.nextInt(); + int t = scanner.nextInt(); + umap.get(s).add(t); // 记录s指向哪些文件 + inDegree[t]++; // t的入度加一 + } + + Queue queue = new LinkedList<>(); + for (int i = 0; i < n; i++) { + if (inDegree[i] == 0) { + // 入度为0的文件,可以作为开头,先加入队列 + queue.add(i); + } + } + + List result = new ArrayList<>(); + + // 拓扑排序 + while (!queue.isEmpty()) { + int cur = queue.poll(); // 当前选中的文件 + result.add(cur); + for (int file : umap.get(cur)) { + inDegree[file]--; // cur的指向的文件入度-1 + if (inDegree[file] == 0) { + queue.add(file); + } + } + } + + if (result.size() == n) { + for (int i = 0; i < result.size(); i++) { + System.out.print(result.get(i)); + if (i < result.size() - 1) { + System.out.print(" "); + } + } + } else { + System.out.println(-1); + } + } +} +``` + + + ### Python +```python +from collections import deque, defaultdict + +def topological_sort(n, edges): + inDegree = [0] * n # inDegree 记录每个文件的入度 + umap = defaultdict(list) # 记录文件依赖关系 + + # 构建图和入度表 + for s, t in edges: + inDegree[t] += 1 + umap[s].append(t) + + # 初始化队列,加入所有入度为0的节点 + queue = deque([i for i in range(n) if inDegree[i] == 0]) + result = [] + + while queue: + cur = queue.popleft() # 当前选中的文件 + result.append(cur) + for file in umap[cur]: # 获取该文件指向的文件 + inDegree[file] -= 1 # cur的指向的文件入度-1 + if inDegree[file] == 0: + queue.append(file) + + if len(result) == n: + print(" ".join(map(str, result))) + else: + print(-1) + + +if __name__ == "__main__": + n, m = map(int, input().split()) + edges = [tuple(map(int, input().split())) for _ in range(m)] + topological_sort(n, edges) +``` + + + ### Go ### Rust From db2fca022ecde6d1af4f8f0a30dc2607ee7c27b5 Mon Sep 17 00:00:00 2001 From: kr <963689810@qq.com> Date: Mon, 1 Jul 2024 15:12:42 +0800 Subject: [PATCH 1173/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00078.=E5=AD=90?= =?UTF-8?q?=E9=9B=86=20Rust=E7=AC=AC=E4=BA=8C=E7=A7=8D=E6=80=9D=E8=B7=AF?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0078.\345\255\220\351\233\206.md" | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 1415f2d27b..723d99a18b 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -287,6 +287,7 @@ function subsets(nums: number[]): number[][] { ### Rust +思路一:使用本题的标准解法,递归回溯。 ```Rust impl Solution { fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, start_index: usize) { @@ -308,6 +309,30 @@ impl Solution { } } ``` +思路二:使用二进制枚举,n个元素的子集问题一共是$2^n$种情况。如果我们使用一个二进制数字,每一位根据0和1来决定是选取该元素与否,那么一共也是$2^n$的情况,正好可以一一对应,所以我们可以不使用递归,直接利用循环枚举完成子集问题。 +这种方法的优点在于效率高,不需要递归调用,并且代码容易编写。缺点则是过滤某些非法情况时会比递归方法难写一点,不过在子集问题中不存在这个问题。 +```Rust +impl Solution { + pub fn subsets(nums: Vec) -> Vec> { + let n = nums.len(); + // 预分配2^n空间 + let mut result = Vec::with_capacity(1 << n); + // 二进制枚举,2^n种情况 + for i in 0..(1 << n) { + let mut subset = Vec::new(); + for j in 0..n { + // 枚举该二进制数字的每一位 + // 如果该位是1,对应位置上的元素加入子集,否则跳过 + if i & (1 << j) != 0 { + subset.push(nums[j]); + } + } + result.push(subset); + } + result + } +} +``` ### C From 45ad945e63e90b45ef063d6c7dcc3cbc7ad60b2f Mon Sep 17 00:00:00 2001 From: SusanAIFF Date: Tue, 2 Jul 2024 18:02:39 +0800 Subject: [PATCH 1174/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86prim?= =?UTF-8?q?=E5=AF=BB=E5=AE=9D=E7=9A=84python=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新了prim寻宝的python写法 --- .../0053.\345\257\273\345\256\235-prim.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index 782bd143dd..18256db81c 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -521,6 +521,40 @@ int main() { ### Java ### Python +```python +# 接收输入 +v, e = list(map(int, input().strip().split())) +# 按照常规的邻接矩阵存储图信息,不可达的初始化为10001 +graph = [[10001] * (v+1) for _ in range(v+1)] +for _ in range(e): + x, y, w = list(map(int, input().strip().split())) + graph[x][y] = w + graph[y][x] = w + +# 定义加入生成树的标记数组和未加入生成树的最近距离 +visited = [False] * (v + 1) +minDist = [10001] * (v + 1) + +# 循环 n - 1 次,建立 n - 1 条边 +# 从节点视角来看:每次选中一个节点加入树,更新剩余的节点到树的最短距离, +# 这一步其实蕴含了确定下一条选取的边,计入总路程 ans 的计算 +for _ in range(1, v + 1): + min_val = 10002 + cur = -1 + for j in range(1, v + 1): + if visited[j] == False and minDist[j] < min_val: + cur = j + min_val = minDist[j] + visited[cur] = True + for j in range(1, v + 1): + if visited[j] == False and minDist[j] > graph[cur][j]: + minDist[j] = graph[cur][j] + +ans = 0 +for i in range(2, v + 1): + ans += minDist[i] +print(ans) +``` ### Go From 2d62adc079f920e0bea4abce8f209cc7507c633e Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 4 Jul 2024 15:50:30 +0800 Subject: [PATCH 1175/1533] =?UTF-8?q?110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E5=A2=9E=E5=8A=A0Go=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\344\272\214\345\217\211\346\240\221.md" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index dd05bdd6af..f8071333f0 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -623,6 +623,8 @@ class Solution: ``` ### Go: +递归法 + ```Go func isBalanced(root *TreeNode) bool { h := getHeight(root) @@ -653,6 +655,64 @@ func max(a, b int) int { } ``` +迭代法 + +```Go +func isBalanced(root *TreeNode) bool { + st := make([]*TreeNode, 0) + if root == nil { + return true + } + st = append(st, root) + for len(st) > 0 { + node := st[len(st)-1] + st = st[:len(st)-1] + if math.Abs(float64(getDepth(node.Left)) - float64(getDepth(node.Right))) > 1 { + return false + } + if node.Right != nil { + st = append(st, node.Right) + } + if node.Left != nil { + st = append(st, node.Left) + } + } + return true +} + +func getDepth(cur *TreeNode) int { + st := make([]*TreeNode, 0) + if cur != nil { + st = append(st, cur) + } + depth := 0 + result := 0 + for len(st) > 0 { + node := st[len(st)-1] + if node != nil { + st = st[:len(st)-1] + st = append(st, node, nil) + depth++ + if node.Right != nil { + st = append(st, node.Right) + } + if node.Left != nil { + st = append(st, node.Left) + } + } else { + st = st[:len(st)-1] + node = st[len(st)-1] + st = st[:len(st)-1] + depth-- + } + if result < depth { + result = depth + } + } + return result +} +``` + ### JavaScript: 递归法: From 72c1db1c8a3ab41ec12791d153bc27d7c269d2b7 Mon Sep 17 00:00:00 2001 From: ForsakenDelusion <144082461+ForsakenDelusion@users.noreply.github.com> Date: Sat, 6 Jul 2024 11:15:55 +0800 Subject: [PATCH 1176/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00707.=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8=20=E6=96=B0=E5=A2=9ECPP=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E5=8F=8C=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\351\223\276\350\241\250.md" | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index a247b17839..1dbcdc60e9 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -165,6 +165,140 @@ private: ## 其他语言版本 +### C++双链表法: + +```CPP +//采用循环虚拟结点的双链表实现 +class MyLinkedList { +public: + // 定义双向链表节点结构体 + struct DList { + int elem; // 节点存储的元素 + DList *next; // 指向下一个节点的指针 + DList *prev; // 指向上一个节点的指针 + // 构造函数,创建一个值为elem的新节点 + DList(int elem) : elem(elem), next(nullptr), prev(nullptr) {}; + }; + + // 构造函数,初始化链表 + MyLinkedList() { + sentinelNode = new DList(0); // 创建哨兵节点,不存储有效数据 + sentinelNode->next = sentinelNode; // 哨兵节点的下一个节点指向自身,形成循环 + sentinelNode->prev = sentinelNode; // 哨兵节点的上一个节点指向自身,形成循环 + size = 0; // 初始化链表大小为0 + } + + // 获取链表中第index个节点的值 + int get(int index) { + if (index > (size - 1) || index < 0) { // 检查索引是否超出范围 + return -1; // 如果超出范围,返回-1 + } + int num; + int mid = size >> 1; // 计算链表中部位置 + DList *curNode = sentinelNode; // 从哨兵节点开始 + if (index < mid) { // 如果索引小于中部位置,从前往后遍历 + for (int i = 0; i < index + 1; i++) { + curNode = curNode->next; // 移动到目标节点 + } + } else { // 如果索引大于等于中部位置,从后往前遍历 + for (int i = 0; i < size - index; i++) { + curNode = curNode->prev; // 移动到目标节点 + } + } + num = curNode->elem; // 获取目标节点的值 + return num; // 返回节点的值 + } + + // 在链表头部添加节点 + void addAtHead(int val) { + DList *newNode = new DList(val); // 创建新节点 + DList *next = sentinelNode->next; // 获取当前头节点的下一个节点 + newNode->prev = sentinelNode; // 新节点的上一个节点指向哨兵节点 + newNode->next = next; // 新节点的下一个节点指向原来的头节点 + size++; // 链表大小加1 + sentinelNode->next = newNode; // 哨兵节点的下一个节点指向新节点 + next->prev = newNode; // 原来的头节点的上一个节点指向新节点 + } + + // 在链表尾部添加节点 + void addAtTail(int val) { + DList *newNode = new DList(val); // 创建新节点 + DList *prev = sentinelNode->prev; // 获取当前尾节点的上一个节点 + newNode->next = sentinelNode; // 新节点的下一个节点指向哨兵节点 + newNode->prev = prev; // 新节点的上一个节点指向原来的尾节点 + size++; // 链表大小加1 + sentinelNode->prev = newNode; // 哨兵节点的上一个节点指向新节点 + prev->next = newNode; // 原来的尾节点的下一个节点指向新节点 + } + + // 在链表中的第index个节点之前添加值为val的节点 + void addAtIndex(int index, int val) { + if (index > size) { // 检查索引是否超出范围 + return; // 如果超出范围,直接返回 + } + if (index <= 0) { // 如果索引为0或负数,在头部添加节点 + addAtHead(val); + return; + } + int num; + int mid = size >> 1; // 计算链表中部位置 + DList *curNode = sentinelNode; // 从哨兵节点开始 + if (index < mid) { // 如果索引小于中部位置,从前往后遍历 + for (int i = 0; i < index; i++) { + curNode = curNode->next; // 移动到目标位置的前一个节点 + } + DList *temp = curNode->next; // 获取目标位置的节点 + DList *newNode = new DList(val); // 创建新节点 + curNode->next = newNode; // 在目标位置前添加新节点 + temp->prev = newNode; // 目标位置的节点的前一个节点指向新节点 + newNode->next = temp; // 新节点的下一个节点指向目标位置的结点 + newNode->prev = curNode; // 新节点的上一个节点指向当前节点 + } else { // 如果索引大于等于中部位置,从后往前遍历 + for (int i = 0; i < size - index; i++) { + curNode = curNode->prev; // 移动到目标位置的后一个节点 + } + DList *temp = curNode->prev; // 获取目标位置的节点 + DList *newNode = new DList(val); // 创建新节点 + curNode->prev = newNode; // 在目标位置后添加新节点 + temp->next = newNode; // 目标位置的节点的下一个节点指向新节点 + newNode->prev = temp; // 新节点的上一个节点指向目标位置的节点 + newNode->next = curNode; // 新节点的下一个节点指向当前节点 + } + size++; // 链表大小加1 + } + + // 删除链表中的第index个节点 + void deleteAtIndex(int index) { + if (index > (size - 1) || index < 0) { // 检查索引是否超出范围 + return; // 如果超出范围,直接返回 + } + int num; + int mid = size >> 1; // 计算链表中部位置 + DList *curNode = sentinelNode; // 从哨兵节点开始 + if (index < mid) { // 如果索引小于中部位置,从前往后遍历 + for (int i = 0; i < index; i++) { + curNode = curNode->next; // 移动到目标位置的前一个节点 + } + DList *next = curNode->next->next; // 获取目标位置的下一个节点 + curNode->next = next; // 删除目标位置的节点 + next->prev = curNode; // 目标位置的下一个节点的前一个节点指向当前节点 + } else { // 如果索引大于等于中部位置,从后往前遍历 + for (int i = 0; i < size - index - 1; i++) { + curNode = curNode->prev; // 移动到目标位置的后一个节点 + } + DList *prev = curNode->prev->prev; // 获取目标位置的下一个节点 + curNode->prev = prev; // 删除目标位置的节点 + prev->next = curNode; // 目标位置的下一个节点的下一个节点指向当前节点 + } + size--; // 链表大小减1 + } + +private: + int size; // 链表的大小 + DList *sentinelNode; // 哨兵节点的指针 +}; +``` + ### C: ```C From 290e15e69e41d06f07c44ea70f01f009034c81cf Mon Sep 17 00:00:00 2001 From: yunskj Date: Tue, 9 Jul 2024 21:25:11 +0800 Subject: [PATCH 1177/1533] =?UTF-8?q?Update=200977.=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Java=E6=8E=92=E5=BA=8F=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 977.有序数组的平方Java排序法 --- ...7\273\204\347\232\204\345\271\263\346\226\271.md" | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index b10620d032..7119dda585 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -100,6 +100,18 @@ public: ## 其他语言版本 ### Java: +排序法 +```Java +class Solution { + public int[] sortedSquares(int[] nums) { + for (int i = 0; i < nums.length; i++) { + nums[i] = nums[i] * nums[i]; + } + Arrays.sort(nums); + return nums; + } +} +``` ```Java class Solution { From 6fd1df5bd7c6e665d6caf56368bf0289c2c27cdc Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 10 Jul 2024 10:19:39 +0800 Subject: [PATCH 1178/1533] =?UTF-8?q?404.=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=E5=A2=9E=E5=8A=A0Go=E9=80=92=E5=BD=92?= =?UTF-8?q?=E7=B2=BE=E7=AE=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\266\345\255\220\344\271\213\345\222\214.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 1ba71dc94f..66aff68f2e 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -337,6 +337,21 @@ func sumOfLeftLeaves(root *TreeNode) int { } ``` +**递归精简版** + +```go +func sumOfLeftLeaves(root *TreeNode) int { + if root == nil { + return 0 + } + leftValue := 0 + if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil { + leftValue = root.Left.Val + } + return leftValue + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right) +} +``` + **迭代法(前序遍历)** ```go From 354359409cd03574336c3e5f1ea4764159ccf2ba Mon Sep 17 00:00:00 2001 From: MrYoungg <151980452+MrYoungg@users.noreply.github.com> Date: Wed, 10 Jul 2024 11:21:38 +0800 Subject: [PATCH 1179/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...237\272\347\241\20001\350\203\214\345\214\205-1.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index fa11fb94bc..a92faf3d75 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -118,7 +118,7 @@ dp[0][j],即:i为0,存放编号0的物品的时候,各个容量的背包 代码初始化如下: -``` +```CPP for (int j = 0 ; j < weight[0]; j++) { // 当然这一步,如果把dp数组预先初始化为0了,这一步就可以省略,但很多同学应该没有想清楚这一点。 dp[0][j] = 0; } @@ -147,7 +147,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 最后初始化代码如下: -``` +```CPP // 初始化 dp vector> dp(weight.size(), vector(bagweight + 1, 0)); for (int j = weight[0]; j <= bagweight; j++) { @@ -171,7 +171,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 那么我先给出先遍历物品,然后遍历背包重量的代码。 -``` +```CPP // weight数组的大小 就是物品个数 for(int i = 1; i < weight.size(); i++) { // 遍历物品 for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 @@ -186,7 +186,7 @@ for(int i = 1; i < weight.size(); i++) { // 遍历物品 例如这样: -``` +```CPP // weight数组的大小 就是物品个数 for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 for(int i = 1; i < weight.size(); i++) { // 遍历物品 @@ -232,7 +232,7 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。 -```cpp +```CPP void test_2_wei_bag_problem1() { vector weight = {1, 3, 4}; vector value = {15, 20, 30}; From 5a19fc16d20b18e18377257b16231123971dd3b4 Mon Sep 17 00:00:00 2001 From: XZY <1214807740@qq.com> Date: Wed, 10 Jul 2024 16:07:47 +0800 Subject: [PATCH 1180/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E5=8D=95=E8=AF=8D=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" | 2 +- .../0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" | 2 +- ...\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md" | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index f256d15c45..9ee7bef38a 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -41,7 +41,7 @@ 首先通过本题大家要明确什么是子序列,“子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序”。 本题也是代码随想录中子序列问题的第一题,如果没接触过这种题目的话,本题还是很难的,甚至想暴力去搜索也不知道怎么搜。 -子序列问题是动态规划解决的经典问题,当前下标i的递增子序列长度,其实和i之前的下表j的子序列长度有关系,那又是什么样的关系呢。 +子序列问题是动态规划解决的经典问题,当前下标i的递增子序列长度,其实和i之前的下标j的子序列长度有关系,那又是什么样的关系呢。 接下来,我们依然用动规五部曲来详细分析一波: diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 2011bee3e5..89e4ad1166 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -55,7 +55,7 @@ dp[i] 和 dp[i-1] ,dp[i + 1] 看上去都没啥关系。 我们在判断字符串S是否是回文,那么如果我们知道 s[1],s[2],s[3] 这个子串是回文的,那么只需要比较 s[0]和s[4]这两个元素是否相同,如果相同的话,这个字符串s 就是回文串。 -那么此时我们是不是能找到一种递归关系,也就是判断一个子字符串(字符串的下表范围[i,j])是否回文,依赖于,子字符串(下表范围[i + 1, j - 1])) 是否是回文。 +那么此时我们是不是能找到一种递归关系,也就是判断一个子字符串(字符串下标范围[i,j])是否回文,依赖于,子字符串(下标范围[i + 1, j - 1])) 是否是回文。 所以为了明确这种递归关系,我们的dp数组是要定义成一位二维dp数组。 diff --git "a/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md" "b/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md" index 6099747a92..edb62bc8b5 100644 --- "a/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md" +++ "b/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md" @@ -25,7 +25,7 @@ index_{left}=(\sum_{i=0}^{i=k}2^i)+2*m-1-1=2^{k+1}+2m-3 $$ - - 故左孩子的下表为$index_{left}=index_{father}\times2+1$,同理可得到右子孩子的索引关系。也可以直接在左子孩子的基础上`+1`。 + - 故左孩子的下标为$index_{left}=index_{father}\times2+1$,同理可得到右子孩子的索引关系。也可以直接在左子孩子的基础上`+1`。 From 423ed9899e4a7400eaade4abaa270ff1548d9079 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 11 Jul 2024 09:05:30 +0800 Subject: [PATCH 1181/1533] =?UTF-8?q?112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C=E5=A2=9E=E5=8A=A0Go=E9=80=92=E5=BD=92=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 2beb8a7fa3..6709a2fbd2 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -727,6 +727,48 @@ class Solution: ```go //递归法 +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func hasPathSum(root *TreeNode, targetSum int) bool { + if root == nil { + return false + } + return traversal(root, targetSum - root.Val) +} + +func traversal(cur *TreeNode, count int) bool { + if cur.Left == nil && cur.Right == nil && count == 0 { + return true + } + if cur.Left == nil && cur.Right == nil { + return false + } + if cur.Left != nil { + count -= cur.Left.Val + if traversal(cur.Left, count) { + return true + } + count += cur.Left.Val + } + if cur.Right != nil { + count -= cur.Right.Val + if traversal(cur.Right, count) { + return true + } + count += cur.Right.Val + } + return false +} +``` + +```go +//递归法精简 /** * Definition for a binary tree node. * type TreeNode struct { From 9b9adf6100bb2d8b3e2703b5f573af05a8890d15 Mon Sep 17 00:00:00 2001 From: eat to 160 pounds <2915390277@qq.com> Date: Sat, 13 Jul 2024 15:10:24 +0800 Subject: [PATCH 1182/1533] =?UTF-8?q?feat(0309):=20=E5=8F=A6=E4=B8=80?= =?UTF-8?q?=E7=A7=8D=E7=8A=B6=E6=80=81=E5=AE=9A=E4=B9=89=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\345\206\267\345\206\273\346\234\237.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 5a38111ad5..95689a4805 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -359,6 +359,26 @@ func max(a, b int) int { ### Javascript: +> 不同的状态定义 感觉更容易理解些 +```javascript +function maxProfit(prices) { + // 第i天状态 持股 卖出 非冷冻期(不持股) 处于冷冻期 + const dp = new Array(prices.length).fill(0).map(() => [0, 0, 0, 0]); + dp[0][0] = -prices[0]; + for (let i = 1; i < prices.length; i++) { + // 持股 + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]); + // 卖出 + dp[i][1] = dp[i - 1][0] + prices[i]; + // 非冷冻期(不持股) + dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1]); + // 冷冻期(上一天卖出) + dp[i][3] = dp[i - 1][1]; + } + return Math.max(...dp.pop()); +}; +``` + ```javascript const maxProfit = (prices) => { if(prices.length < 2) { From afbcb1d98bd5a0e40f9f65d1ced60e62c286fbf2 Mon Sep 17 00:00:00 2001 From: XZY <1214807740@qq.com> Date: Mon, 15 Jul 2024 12:14:43 +0800 Subject: [PATCH 1183/1533] =?UTF-8?q?Update=20121.=20=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\344\275\263\346\227\266\346\234\272.md" | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index e9aea0e6c6..df586ff99f 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -466,7 +466,7 @@ function maxProfit(prices: number[]): number { }; ``` -> 动态规划 +> 动态规划:版本一 ```typescript function maxProfit(prices: number[]): number { @@ -487,6 +487,26 @@ function maxProfit(prices: number[]): number { }; ``` +> 动态规划:版本二 + +```typescript +// dp[i][0] 表示第i天持有股票所得最多现金 +// dp[i][1] 表示第i天不持有股票所得最多现金 +function maxProfit(prices: number[]): number { + const dp:number[][] = Array(2).fill(0).map(item => Array(2)); + dp[0][0] = -prices[0]; + dp[0][1] = 0; + + for (let i = 1; i < prices.length; i++) { + dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], -prices[i]); + dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 1) % 2][0] + prices[i]); + } + + // 返回不持有股票的最大现金 + return dp[(prices.length-1) % 2][1]; +}; +``` + ### C#: > 贪心法 From 06a089e780e1be6206e58ed052b6615ee0cb7dc2 Mon Sep 17 00:00:00 2001 From: Jack Lin Date: Mon, 15 Jul 2024 15:25:31 +0800 Subject: [PATCH 1184/1533] =?UTF-8?q?0406.=E6=A0=B9=E6=8D=AE=E8=BA=AB?= =?UTF-8?q?=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97.md:=20Fix=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index b7e94543e6..d92a7f3f58 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -87,7 +87,7 @@ 回归本题,整个插入过程如下: 排序完的people: -[[7,0], [7,1], [6,1], [5,0], [5,2],[4,4]] +[[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]] 插入的过程: * 插入[7,0]:[[7,0]] From 2c322293830aca64b1e329fb6595503acf845610 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 15 Jul 2024 16:49:16 +0800 Subject: [PATCH 1185/1533] Update --- README.md | 1 + ...55\344\271\260\345\234\237\345\234\260.md" | 386 ++++++++++++++++++ ...17\202\344\274\232dijkstra\345\240\206.md" | 152 +++++++ ...74\232dijkstra\346\234\264\347\264\240.md" | 57 +++ .../0053.\345\257\273\345\256\235-Kruskal.md" | 137 +++++++ .../0053.\345\257\273\345\256\235-prim.md" | 132 ++++++ ...8.\345\214\272\351\227\264\345\222\214.md" | 104 ++++- ...\347\211\251\350\277\220\350\276\223II.md" | 2 +- ...347\211\251\350\277\220\350\276\223III.md" | 2 +- ...57\350\276\276\350\267\257\345\276\204.md" | 2 +- ...50\345\217\257\350\276\276\346\200\247.md" | 10 +- ...02\347\202\271\345\210\227\350\241\250.md" | 147 +++++++ ...62\345\244\204\347\220\206\345\231\250.md" | 148 +++++++ ...10\346\201\257\344\274\240\350\276\223.md" | 192 +++++++++ ...9.\345\217\257\347\210\261\344\270\262.md" | 101 +++++ ...40\351\231\244\346\200\273\345\222\214.md" | 30 ++ ...347\224\250ACM\346\250\241\345\274\217.md" | 93 +++++ ...06\350\256\272\345\237\272\347\241\200.md" | 2 + ...75\344\272\214\345\217\211\346\240\221.md" | 104 +++++ ...62\345\244\204\347\220\206\345\231\250.md" | 50 --- .../\345\256\214\347\276\216\346\225\260.md" | 29 ++ ...72\351\227\264\347\277\273\350\275\254.md" | 109 +++++ ...14\345\200\274\350\267\257\345\276\204.md" | 81 ++++ ...02\347\202\271\345\210\227\350\241\250.md" | 47 --- 24 files changed, 1996 insertions(+), 122 deletions(-) create mode 100644 "problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" create mode 100644 "problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" create mode 100644 "problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" create mode 100644 "problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" create mode 100644 "problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" create mode 100644 "problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" create mode 100644 "problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" create mode 100644 "problems/kamacoder/\345\245\275\344\272\214\345\217\211\346\240\221.md" delete mode 100644 "problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" create mode 100644 "problems/kamacoder/\345\256\214\347\276\216\346\225\260.md" create mode 100644 "problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" create mode 100644 "problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" delete mode 100644 "problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" diff --git a/README.md b/README.md index 4c570c050d..19d1bebbef 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ 3. [数组:27.移除元素](./problems/0027.移除元素.md) 4. [数组:977.有序数组的平方](./problems/0977.有序数组的平方.md) 5. [数组:209.长度最小的子数组](./problems/0209.长度最小的子数组.md) +6. [数组:区间和](./problems/kamacoder/0058.区间和.md) 6. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md) 7. [数组:总结篇](./problems/数组总结篇.md) diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" new file mode 100644 index 0000000000..73e9e8c5f4 --- /dev/null +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -0,0 +1,386 @@ + +# 44. 开发商购买土地 + +【题目描述】 + +在一个城市区域内,被划分成了n * m个连续的区块,每个区块都拥有不同的权值,代表着其土地价值。目前,有两家开发公司,A 公司和 B 公司,希望购买这个城市区域的土地。 + +现在,需要将这个城市区域的所有区块分配给 A 公司和 B 公司。 + +然而,由于城市规划的限制,只允许将区域按横向或纵向划分成两个子区域,而且每个子区域都必须包含一个或多个区块。 + +为了确保公平竞争,你需要找到一种分配方式,使得 A 公司和 B 公司各自的子区域内的土地总价值之差最小。 + +注意:区块不可再分。 + +【输入描述】 + +第一行输入两个正整数,代表 n 和 m。 + +接下来的 n 行,每行输出 m 个正整数。 + +输出描述 + +请输出一个整数,代表两个子区域内土地总价值之间的最小差距。 + +【输入示例】 + +3 3 +1 2 3 +2 1 3 +1 2 3 + +【输出示例】 + +0 + +【提示信息】 + +如果将区域按照如下方式划分: + +1 2 | 3 +2 1 | 3 +1 2 | 3 + +两个子区域内土地总价值之间的最小差距可以达到 0。 + +【数据范围】: + +* 1 <= n, m <= 100; +* n 和 m 不同时为 1。 + +## 思路 + +看到本题,大家如果想暴力求解,应该是 n^3 的时间复杂度, + +一个 for 枚举分割线, 嵌套 两个for 去累加区间里的和。 + +如果本题要求 任何两个行(或者列)之间的数值总和,大家在[0058.区间和](./0058.区间和.md) 的基础上 应该知道怎么求。 + +就是前缀和的思路,先统计好,前n行的和 q[n],如果要求矩阵 a 行到 b行 之间的总和,那么就 q[b] - q[a - 1]就好。 + +至于为什么是 a - 1,大家去看 [0058.区间和](./0058.区间和.md) 的分析,使用 前缀和 要注意 区间左右边的开闭情况。 + +本题也可以使用 前缀和的思路来求解,先将 行方向,和 列方向的和求出来,这样可以方便知道 划分的两个区间的和。 + +代码如下: + + +```CPP +#include +#include +#include + +using namespace std; +int main () { + int n, m; + cin >> n >> m; + int sum = 0; + vector> vec(n, vector(m, 0)) ; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> vec[i][j]; + sum += vec[i][j]; + } + } + // 统计横向 + vector horizontal(n, 0); + for (int i = 0; i < n; i++) { + for (int j = 0 ; j < m; j++) { + horizontal[i] += vec[i][j]; + } + } + // 统计纵向 + vector vertical(m , 0); + for (int j = 0; j < m; j++) { + for (int i = 0 ; i < n; i++) { + vertical[j] += vec[i][j]; + } + } + int result = INT_MAX; + int horizontalCut = 0; + for (int i = 0 ; i < n; i++) { + horizontalCut += horizontal[i]; + result = min(result, abs(sum - horizontalCut - horizontalCut)); + } + int verticalCut = 0; + for (int j = 0; j < m; j++) { + verticalCut += vertical[j]; + result = min(result, abs(sum - verticalCut - verticalCut)); + } + cout << result << endl; +} + +``` + +时间复杂度: O(n^2) + +其实本题可以在暴力求解的基础上,优化一下,就不用前缀和了,在行向遍历的时候,遇到行末尾就统一一下, 在列向遍历的时候,遇到列末尾就统计一下。 + +时间复杂度也是 O(n^2) + +代码如下: + +```CPP +#include +#include +#include + +using namespace std; +int main () { + int n, m; + cin >> n >> m; + int sum = 0; + vector> vec(n, vector(m, 0)) ; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> vec[i][j]; + sum += vec[i][j]; + } + } + + int result = INT_MAX; + int count = 0; // 统计遍历过的行 + for (int i = 0; i < n; i++) { + for (int j = 0 ; j < m; j++) { + count += vec[i][j]; + // 遍历到行末尾时候开始统计 + if (j == m - 1) result = min (result, abs(sum - count - count)); + + } + } + + count = 0; // 统计遍历过的列 + for (int j = 0; j < m; j++) { + for (int i = 0 ; i < n; i++) { + count += vec[i][j]; + // 遍历到列末尾的时候开始统计 + if (i == n - 1) result = min (result, abs(sum - count - count)); + } + } + cout << result << endl; +} + +``` + + + +## 其他语言版本 + +### Java + +前缀和 + +```Java +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int sum = 0; + int[][] vec = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + vec[i][j] = scanner.nextInt(); + sum += vec[i][j]; + } + } + + // 统计横向 + int[] horizontal = new int[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + horizontal[i] += vec[i][j]; + } + } + + // 统计纵向 + int[] vertical = new int[m]; + for (int j = 0; j < m; j++) { + for (int i = 0; i < n; i++) { + vertical[j] += vec[i][j]; + } + } + + int result = Integer.MAX_VALUE; + int horizontalCut = 0; + for (int i = 0; i < n; i++) { + horizontalCut += horizontal[i]; + result = Math.min(result, Math.abs(sum - 2 * horizontalCut)); + } + + int verticalCut = 0; + for (int j = 0; j < m; j++) { + verticalCut += vertical[j]; + result = Math.min(result, Math.abs(sum - 2 * verticalCut)); + } + + System.out.println(result); + scanner.close(); + } +} + +``` + +优化暴力 + +```Java +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int sum = 0; + int[][] vec = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + vec[i][j] = scanner.nextInt(); + sum += vec[i][j]; + } + } + + int result = Integer.MAX_VALUE; + int count = 0; // 统计遍历过的行 + + // 行切分 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + count += vec[i][j]; + // 遍历到行末尾时候开始统计 + if (j == m - 1) { + result = Math.min(result, Math.abs(sum - 2 * count)); + } + } + } + + count = 0; + // 列切分 + for (int j = 0; j < m; j++) { + for (int i = 0; i < n; i++) { + count += vec[i][j]; + // 遍历到列末尾时候开始统计 + if (i == n - 1) { + result = Math.min(result, Math.abs(sum - 2 * count)); + } + } + } + + System.out.println(result); + scanner.close(); + } +} + +``` + +### python + +前缀和 +```python +def main(): + import sys + input = sys.stdin.read + data = input().split() + + idx = 0 + n = int(data[idx]) + idx += 1 + m = int(data[idx]) + idx += 1 + sum = 0 + vec = [] + for i in range(n): + row = [] + for j in range(m): + num = int(data[idx]) + idx += 1 + row.append(num) + sum += num + vec.append(row) + + # 统计横向 + horizontal = [0] * n + for i in range(n): + for j in range(m): + horizontal[i] += vec[i][j] + + # 统计纵向 + vertical = [0] * m + for j in range(m): + for i in range(n): + vertical[j] += vec[i][j] + + result = float('inf') + horizontalCut = 0 + for i in range(n): + horizontalCut += horizontal[i] + result = min(result, abs(sum - 2 * horizontalCut)) + + verticalCut = 0 + for j in range(m): + verticalCut += vertical[j] + result = min(result, abs(sum - 2 * verticalCut)) + + print(result) + +if __name__ == "__main__": + main() + + +``` + +优化暴力 + +```python +def main(): + import sys + input = sys.stdin.read + data = input().split() + + idx = 0 + n = int(data[idx]) + idx += 1 + m = int(data[idx]) + idx += 1 + sum = 0 + vec = [] + for i in range(n): + row = [] + for j in range(m): + num = int(data[idx]) + idx += 1 + row.append(num) + sum += num + vec.append(row) + + result = float('inf') + + count = 0 + # 行切分 + for i in range(n): + + for j in range(m): + count += vec[i][j] + # 遍历到行末尾时候开始统计 + if j == m - 1: + result = min(result, abs(sum - 2 * count)) + + count = 0 + # 列切分 + for j in range(m): + + for i in range(n): + count += vec[i][j] + # 遍历到列末尾时候开始统计 + if i == n - 1: + result = min(result, abs(sum - 2 * count)) + + print(result) + +if __name__ == "__main__": + main() + +``` diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index 0452615a61..9905a420c8 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -755,8 +755,160 @@ public class Main { ### Python +```python +import heapq + +class Edge: + def __init__(self, to, val): + self.to = to + self.val = val + +def dijkstra(n, m, edges, start, end): + grid = [[] for _ in range(n + 1)] + + for p1, p2, val in edges: + grid[p1].append(Edge(p2, val)) + + minDist = [float('inf')] * (n + 1) + visited = [False] * (n + 1) + + pq = [] + heapq.heappush(pq, (0, start)) + minDist[start] = 0 + + while pq: + cur_dist, cur_node = heapq.heappop(pq) + + if visited[cur_node]: + continue + + visited[cur_node] = True + + for edge in grid[cur_node]: + if not visited[edge.to] and cur_dist + edge.val < minDist[edge.to]: + minDist[edge.to] = cur_dist + edge.val + heapq.heappush(pq, (minDist[edge.to], edge.to)) + + return -1 if minDist[end] == float('inf') else minDist[end] + +# 输入 +n, m = map(int, input().split()) +edges = [tuple(map(int, input().split())) for _ in range(m)] +start = 1 # 起点 +end = n # 终点 + +# 运行算法并输出结果 +result = dijkstra(n, m, edges, start, end) +print(result) + +``` + ### Go +```go +package main + +import ( + "container/heap" + "fmt" + "math" +) + +// Edge 表示带权重的边 +type Edge struct { + to, val int +} + +// PriorityQueue 实现一个小顶堆 +type Item struct { + node, dist int +} + +type PriorityQueue []*Item + +func (pq PriorityQueue) Len() int { return len(pq) } + +func (pq PriorityQueue) Less(i, j int) bool { + return pq[i].dist < pq[j].dist +} + +func (pq PriorityQueue) Swap(i, j int) { + pq[i], pq[j] = pq[j], pq[i] +} + +func (pq *PriorityQueue) Push(x interface{}) { + *pq = append(*pq, x.(*Item)) +} + +func (pq *PriorityQueue) Pop() interface{} { + old := *pq + n := len(old) + item := old[n-1] + *pq = old[0 : n-1] + return item +} + +func dijkstra(n, m int, edges [][]int, start, end int) int { + grid := make([][]Edge, n+1) + for _, edge := range edges { + p1, p2, val := edge[0], edge[1], edge[2] + grid[p1] = append(grid[p1], Edge{to: p2, val: val}) + } + + minDist := make([]int, n+1) + for i := range minDist { + minDist[i] = math.MaxInt64 + } + visited := make([]bool, n+1) + + pq := &PriorityQueue{} + heap.Init(pq) + heap.Push(pq, &Item{node: start, dist: 0}) + minDist[start] = 0 + + for pq.Len() > 0 { + cur := heap.Pop(pq).(*Item) + + if visited[cur.node] { + continue + } + + visited[cur.node] = true + + for _, edge := range grid[cur.node] { + if !visited[edge.to] && minDist[cur.node]+edge.val < minDist[edge.to] { + minDist[edge.to] = minDist[cur.node] + edge.val + heap.Push(pq, &Item{node: edge.to, dist: minDist[edge.to]}) + } + } + } + + if minDist[end] == math.MaxInt64 { + return -1 + } + return minDist[end] +} + +func main() { + var n, m int + fmt.Scan(&n, &m) + + edges := make([][]int, m) + for i := 0; i < m; i++ { + var p1, p2, val int + fmt.Scan(&p1, &p2, &val) + edges[i] = []int{p1, p2, val} + } + + start := 1 // 起点 + end := n // 终点 + + result := dijkstra(n, m, edges, start, end) + fmt.Println(result) +} + +``` + ### Rust ### Javascript diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 7328882a1c..e904b9217e 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -806,6 +806,63 @@ public class Main { ### Python +``` +import sys + +def dijkstra(n, m, edges, start, end): + # 初始化邻接矩阵 + grid = [[float('inf')] * (n + 1) for _ in range(n + 1)] + for p1, p2, val in edges: + grid[p1][p2] = val + + # 初始化距离数组和访问数组 + minDist = [float('inf')] * (n + 1) + visited = [False] * (n + 1) + + minDist[start] = 0 # 起始点到自身的距离为0 + + for _ in range(1, n + 1): # 遍历所有节点 + minVal = float('inf') + cur = -1 + + # 选择距离源点最近且未访问过的节点 + for v in range(1, n + 1): + if not visited[v] and minDist[v] < minVal: + minVal = minDist[v] + cur = v + + if cur == -1: # 如果找不到未访问过的节点,提前结束 + break + + visited[cur] = True # 标记该节点已被访问 + + # 更新未访问节点到源点的距离 + for v in range(1, n + 1): + if not visited[v] and grid[cur][v] != float('inf') and minDist[cur] + grid[cur][v] < minDist[v]: + minDist[v] = minDist[cur] + grid[cur][v] + + return -1 if minDist[end] == float('inf') else minDist[end] + +if __name__ == "__main__": + input = sys.stdin.read + data = input().split() + n, m = int(data[0]), int(data[1]) + edges = [] + index = 2 + for _ in range(m): + p1 = int(data[index]) + p2 = int(data[index + 1]) + val = int(data[index + 2]) + edges.append((p1, p2, val)) + index += 3 + start = 1 # 起点 + end = n # 终点 + + result = dijkstra(n, m, edges, start, end) + print(result) + +``` + ### Go ### Rust diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index a97409aa3a..e8b8e1ffd3 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -404,8 +404,145 @@ Kruskal算法 时间复杂度 为 nlogn,其中n 为边的数量,适用稀疏 ### Java +```Java +import java.util.*; + +class Edge { + int l, r, val; + + Edge(int l, int r, int val) { + this.l = l; + this.r = r; + this.val = val; + } +} + +public class Main { + private static int n = 10001; + private static int[] father = new int[n]; + + // 并查集初始化 + public static void init() { + for (int i = 0; i < n; i++) { + father[i] = i; + } + } + + // 并查集的查找操作 + public static int find(int u) { + if (u == father[u]) return u; + return father[u] = find(father[u]); + } + + // 并查集的加入集合 + public static void join(int u, int v) { + u = find(u); + v = find(v); + if (u == v) return; + father[v] = u; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int v = scanner.nextInt(); + int e = scanner.nextInt(); + List edges = new ArrayList<>(); + int result_val = 0; + + for (int i = 0; i < e; i++) { + int v1 = scanner.nextInt(); + int v2 = scanner.nextInt(); + int val = scanner.nextInt(); + edges.add(new Edge(v1, v2, val)); + } + + // 执行Kruskal算法 + edges.sort(Comparator.comparingInt(edge -> edge.val)); + + // 并查集初始化 + init(); + + // 从头开始遍历边 + for (Edge edge : edges) { + int x = find(edge.l); + int y = find(edge.r); + + if (x != y) { + result_val += edge.val; + join(x, y); + } + } + System.out.println(result_val); + scanner.close(); + } +} + +``` + ### Python +```python +class Edge: + def __init__(self, l, r, val): + self.l = l + self.r = r + self.val = val + +n = 10001 +father = list(range(n)) + +def init(): + global father + father = list(range(n)) + +def find(u): + if u != father[u]: + father[u] = find(father[u]) + return father[u] + +def join(u, v): + u = find(u) + v = find(v) + if u != v: + father[v] = u + +def kruskal(v, edges): + edges.sort(key=lambda edge: edge.val) + init() + result_val = 0 + + for edge in edges: + x = find(edge.l) + y = find(edge.r) + if x != y: + result_val += edge.val + join(x, y) + + return result_val + +if __name__ == "__main__": + import sys + input = sys.stdin.read + data = input().split() + + v = int(data[0]) + e = int(data[1]) + + edges = [] + index = 2 + for _ in range(e): + v1 = int(data[index]) + v2 = int(data[index + 1]) + val = int(data[index + 2]) + edges.append(Edge(v1, v2, val)) + index += 3 + + result_val = kruskal(v, edges) + print(result_val) + +``` + + ### Go ### Rust diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index 782bd143dd..7eab3f5f0f 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -520,8 +520,140 @@ int main() { ### Java +```Java +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int v = scanner.nextInt(); + int e = scanner.nextInt(); + + // 初始化邻接矩阵,所有值初始化为一个大值,表示无穷大 + int[][] grid = new int[v + 1][v + 1]; + for (int i = 0; i <= v; i++) { + Arrays.fill(grid[i], 10001); + } + + // 读取边的信息并填充邻接矩阵 + for (int i = 0; i < e; i++) { + int x = scanner.nextInt(); + int y = scanner.nextInt(); + int k = scanner.nextInt(); + grid[x][y] = k; + grid[y][x] = k; + } + + // 所有节点到最小生成树的最小距离 + int[] minDist = new int[v + 1]; + Arrays.fill(minDist, 10001); + + // 记录节点是否在树里 + boolean[] isInTree = new boolean[v + 1]; + + // Prim算法主循环 + for (int i = 1; i < v; i++) { + int cur = -1; + int minVal = Integer.MAX_VALUE; + + // 选择距离生成树最近的节点 + for (int j = 1; j <= v; j++) { + if (!isInTree[j] && minDist[j] < minVal) { + minVal = minDist[j]; + cur = j; + } + } + + // 将最近的节点加入生成树 + isInTree[cur] = true; + + // 更新非生成树节点到生成树的距离 + for (int j = 1; j <= v; j++) { + if (!isInTree[j] && grid[cur][j] < minDist[j]) { + minDist[j] = grid[cur][j]; + } + } + } + + // 统计结果 + int result = 0; + for (int i = 2; i <= v; i++) { + result += minDist[i]; + } + System.out.println(result); + scanner.close(); + } +} + +``` + ### Python +```python +def prim(v, e, edges): + import sys + import heapq + + # 初始化邻接矩阵,所有值初始化为一个大值,表示无穷大 + grid = [[10001] * (v + 1) for _ in range(v + 1)] + + # 读取边的信息并填充邻接矩阵 + for edge in edges: + x, y, k = edge + grid[x][y] = k + grid[y][x] = k + + # 所有节点到最小生成树的最小距离 + minDist = [10001] * (v + 1) + + # 记录节点是否在树里 + isInTree = [False] * (v + 1) + + # Prim算法主循环 + for i in range(1, v): + cur = -1 + minVal = sys.maxsize + + # 选择距离生成树最近的节点 + for j in range(1, v + 1): + if not isInTree[j] and minDist[j] < minVal: + minVal = minDist[j] + cur = j + + # 将最近的节点加入生成树 + isInTree[cur] = True + + # 更新非生成树节点到生成树的距离 + for j in range(1, v + 1): + if not isInTree[j] and grid[cur][j] < minDist[j]: + minDist[j] = grid[cur][j] + + # 统计结果 + result = sum(minDist[2:v+1]) + return result + +if __name__ == "__main__": + import sys + input = sys.stdin.read + data = input().split() + + v = int(data[0]) + e = int(data[1]) + + edges = [] + index = 2 + for _ in range(e): + x = int(data[index]) + y = int(data[index + 1]) + k = int(data[index + 2]) + edges.append((x, y, k)) + index += 3 + + result = prim(v, e, edges) + print(result) + +``` + ### Go ### Rust diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index a4bb54e940..0e478c68b0 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -93,7 +93,6 @@ int main() { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627110604.png) - 如果,我们想统计,在vec数组上 下标 2 到下标 5 之间的累加和,那是不是就用 p[5] - p[1] 就可以了。 为什么呢? @@ -114,7 +113,11 @@ p[5] - p[1] 就是 红色部分的区间和。 而 p 数组是我们之前就计算好的累加和,所以后面每次求区间和的之后 我们只需要 O(1)的操作。 +**特别注意**: 在使用前缀和求解的时候,要特别注意 求解区间。 + +如上图,如果我们要求 区间下标 [2, 5] 的区间和,那么应该是 p[5] - p[1],而不是 p[5] - p[2]。 +很多录友在使用前缀和的时候,分不清前缀和的区间,建议画一画图,模拟一下 思路会更清晰。 ```CPP #include @@ -142,6 +145,8 @@ int main() { ``` +C++ 代码 面对大量数据 读取 输出操作,最好用scanf 和 printf,耗时会小很多: + ```CPP #include #include @@ -168,24 +173,89 @@ int main() { ``` -```CPP +## 其他语言版本 -#include -using namespace std; +### Java +```Java -int main(){ - int n, a, b; - cin >> n; - vector vec(n + 1); - vector p(n + 1, 0); - for(int i = 1; i <= n; i++) { - scanf("%d", &vec[i]); - p[i] = p[i - 1] + vec[i]; - } - while(~scanf("%d%d", &a, &b)){ - printf("%d\n", p[b + 1] - p[a]); +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int n = scanner.nextInt(); + int[] vec = new int[n]; + int[] p = new int[n]; + + int presum = 0; + for (int i = 0; i < n; i++) { + vec[i] = scanner.nextInt(); + presum += vec[i]; + p[i] = presum; + } + + while (scanner.hasNextInt()) { + int a = scanner.nextInt(); + int b = scanner.nextInt(); + + int sum; + if (a == 0) { + sum = p[b]; + } else { + sum = p[b] - p[a - 1]; + } + System.out.println(sum); + } + + scanner.close(); } - return 0; -} +} + + +``` + +### Python + +```python + +import sys +input = sys.stdin.read + +def main(): + data = input().split() + index = 0 + n = int(data[index]) + index += 1 + vec = [] + for i in range(n): + vec.append(int(data[index + i])) + index += n + + p = [0] * n + presum = 0 + for i in range(n): + presum += vec[i] + p[i] = presum + + results = [] + while index < len(data): + a = int(data[index]) + b = int(data[index + 1]) + index += 2 + + if a == 0: + sum_value = p[b] + else: + sum_value = p[b] - p[a - 1] + + results.append(sum_value) + + for result in results: + print(result) + +if __name__ == "__main__": + main() + ``` diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 4eab01a8ec..5bc4be7b22 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -78,7 +78,7 @@ circle 我们拿题目中示例来画一个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402103135.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240705161426.png) 图中 节点1 到 节点4 的最短路径是多少(题目中的最低运输成本) (注意边可以为负数的) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index 96f9a9c81e..feaaa1f3c3 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -65,7 +65,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402115614.png) -图中,节点2 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 +图中,节点1 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 所以本题就是求:起点最多经过k + 1 条边到达终点的最短距离。 diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 159cdb9f04..1b6f662eba 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -83,7 +83,7 @@ 如果笔试的时候出一道原题 (笔试都是ACM模式,部分面试也是ACM模式),不少熟练刷力扣的录友都难住了,因为不知道图应该怎么存,也不知道自己存的图如何去遍历。 -所以这也是为什么我要让大家练习 ACM模式 +所以这也是为什么我要让大家练习 ACM模式,也是我为什么 在代码随想录图论讲解中,不惜自己亲自出题,让大家统一练习ACM模式。 -------- diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 03048825c5..226d0f13c5 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -114,11 +114,11 @@ void dfs(const vector>& graph, int key, vector& visited) { ```C++ // 写法二:处理下一个要访问的节点 void dfs(const vector>& graph, int key, vector& visited) { - list keys = rooms[key]; + list keys = graph[key]; for (int key : keys) { if (visited[key] == false) { // 确认下一个是没访问过的节点 visited[key] = true; - dfs(rooms, key, visited); + dfs(graph, key, visited); } } } @@ -202,11 +202,11 @@ int main() { using namespace std; void dfs(const vector>& graph, int key, vector& visited) { - list keys = rooms[key]; + list keys = graph[key]; for (int key : keys) { if (visited[key] == false) { // 确认下一个是没访问过的节点 visited[key] = true; - dfs(rooms, key, visited); + dfs(graph, key, visited); } } } @@ -223,7 +223,7 @@ int main() { } vector visited(n + 1, false); - visited[0] = true; // 节点1 预先处理 + visited[1] = true; // 节点1 预先处理 dfs(graph, 1, visited); for (int i = 1; i <= n; i++) { diff --git "a/problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" "b/problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" new file mode 100644 index 0000000000..c1aa38e132 --- /dev/null +++ "b/problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" @@ -0,0 +1,147 @@ + +# 135. 获取连通的相邻节点列表 + +本题是一个 “阅读理解”题,其实题目的算法很简单,但理解题意很费劲。 + +题目描述中的【提示信息】 是我后加上去了,华为笔试的时候没有这个 【提示信息】。 + +相信没有 【提示信息】大家理解题意 平均要多用半个小时。 + +思路: + +1. 将第一行数据加入set中 +2. 后面输出数据,判断是否在 set里 +3. 最后把结果排个序 + + +```CPP +#include +#include +#include +#include +using namespace std; +int main() { + unordered_set uset; + int n, a; + cin >> n; + while (n--) { + cin >> a; + uset.insert(a); + } + int m, x, vlan_id; + long long tb; + vector vecTB; + cin >> m; + while(m--) { + cin >> tb; + cin >> x; + vector vecVlan_id(x); + for (int i = 0; i < x; i++) { + cin >> vecVlan_id[i]; + } + for (int i = 0; i < x; i++) { + if (uset.find(vecVlan_id[i]) != uset.end()) { + vecTB.push_back(tb); + break; + } + } + + } + cout << vecTB.size() << endl; + if (vecTB.size() != 0) { + sort(vecTB.begin(), vecTB.end()); + for (int i = 0; i < vecTB.size() ; i++) cout << vecTB[i] << " "; + } +} + +``` + +## 其他语言版本 + +### Java + +```Java +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Set uset = new HashSet<>(); + int n = scanner.nextInt(); + while (n-- > 0) { + int a = scanner.nextInt(); + uset.add(a); + } + + int m = scanner.nextInt(); + List vecTB = new ArrayList<>(); + while (m-- > 0) { + long tb = scanner.nextLong(); + int x = scanner.nextInt(); + List vecVlan_id = new ArrayList<>(); + for (int i = 0; i < x; i++) { + vecVlan_id.add(scanner.nextInt()); + } + for (int vlanId : vecVlan_id) { + if (uset.contains(vlanId)) { + vecTB.add(tb); + break; + } + } + } + + System.out.println(vecTB.size()); + if (!vecTB.isEmpty()) { + Collections.sort(vecTB); + for (long tb : vecTB) { + System.out.print(tb + " "); + } + } + } +} + +``` + +### Python + +```python +def main(): + import sys + input = sys.stdin.read + data = input().split() + + index = 0 + n = int(data[index]) + index += 1 + uset = set() + for _ in range(n): + a = int(data[index]) + index += 1 + uset.add(a) + + m = int(data[index]) + index += 1 + vecTB = [] + while m > 0: + tb = int(data[index]) + index += 1 + x = int(data[index]) + index += 1 + vecVlan_id = [] + for _ in range(x): + vecVlan_id.append(int(data[index])) + index += 1 + for vlan_id in vecVlan_id: + if vlan_id in uset: + vecTB.append(tb) + break + m -= 1 + + print(len(vecTB)) + if vecTB: + vecTB.sort() + print(" ".join(map(str, vecTB))) + +if __name__ == "__main__": + main() +``` diff --git "a/problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" "b/problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" new file mode 100644 index 0000000000..1c58f4abaf --- /dev/null +++ "b/problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" @@ -0,0 +1,148 @@ + +# 字符串处理器 + +纯模拟,但情况比较多,非常容易 空指针异常。 + +大家要注意,边界问题 以及 负数问题。 + +整体代码如下: + +```CPP +#include +using namespace std; + int main() { + int index = 0; + long long optNum; + string s; + string cmd; + while(cin >> cmd){ + //cout << s << endl; + if(cmd == "insert") { + string buff; + cin >> buff; + s.insert(index, buff); + index += buff.size(); + } + else if(cmd == "move") { + cin >> optNum; + if(optNum > 0 && index + optNum <= s.size()) index += optNum; + if(optNum < 0 && index >= -optNum) index += optNum; + } + else if(cmd == "delete") { + cin >> optNum; + if(index >= optNum && optNum > 0){ + s.erase(index - optNum, optNum); + index -= optNum; + } + } + else if(cmd == "copy") { + if(index > 0) { + string tmp = s.substr(0, index); + s.insert(index, tmp); + } + } + else if(cmd == "end") { + for(int i = 0; i < index; i++) { + cout << s[i]; + } + cout << '|'; + for(int i = index; i < s.size(); i++) cout << s[i]; + + break; + } + } + return 0; +} + +``` + +## 其他语言版本 + +### Java + +```Java +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + StringBuilder s = new StringBuilder(); + int index = 0; + int optNum; + + while (true) { + String cmd = scanner.next(); + if (cmd.equals("insert")) { + String buff = scanner.next(); + s.insert(index, buff); + index += buff.length(); + } else if (cmd.equals("move")) { + optNum = scanner.nextInt(); + if (optNum > 0 && index + optNum <= s.length()) index += optNum; + if (optNum < 0 && index >= -optNum) index += optNum; + } else if (cmd.equals("delete")) { + optNum = scanner.nextInt(); + if (index >= optNum && optNum > 0) { + s.delete(index - optNum, index); + index -= optNum; + } + } else if (cmd.equals("copy")) { + if (index > 0) { + String tmp = s.substring(0, index); + s.insert(index, tmp); + } + } else if (cmd.equals("end")) { + System.out.print(s.substring(0, index) + '|' + s.substring(index)); + break; + } + } + scanner.close(); + } +} +``` + +### Python + +```python +def main(): + import sys + input = sys.stdin.read + data = input().split() + s = "" + index = 0 + i = 0 + + while i < len(data): + cmd = data[i] + i += 1 + if cmd == "insert": + buff = data[i] + i += 1 + s = s[:index] + buff + s[index:] + index += len(buff) + elif cmd == "move": + optNum = int(data[i]) + i += 1 + if optNum > 0 and index + optNum <= len(s): + index += optNum + elif optNum < 0 and index >= -optNum: + index += optNum + elif cmd == "delete": + optNum = int(data[i]) + i += 1 + if index >= optNum and optNum > 0: + s = s[:index - optNum] + s[index:] + index -= optNum + elif cmd == "copy": + if index > 0: + tmp = s[:index] + s = s[:index] + tmp + s[index:] + elif cmd == "end": + print(s[:index] + '|' + s[index:]) + break + +if __name__ == "__main__": + main() + + +``` diff --git "a/problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" "b/problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" new file mode 100644 index 0000000000..a1519bc6b0 --- /dev/null +++ "b/problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" @@ -0,0 +1,192 @@ + +# 137. 消息传输 + +这道题目,普通广搜就可以解决。 + +这里说一下几点注意事项: + +1、 题目描述中,注意 n 是列数,m是行数 + +这是造成很多录友周赛的时候提交 返回 【运行错误】的罪魁祸首,如果 输入用例是 正方形,那没问题,如果后台输入用例是矩形, n 和 m 搞反了,就会数组越界。 + +矩阵是 m * n ,但输入的顺序却是 先输入n 再输入 m。 + +这会让很多人把矩阵的 n 和 m 搞反。 + +其实规范出题,就应该是n 行,m列,然后 先输入n,在输入m。 + +只能说 大厂出题的人,也不是专业出题的,所以会在 非算法方面一不小心留下很多 “bug”,消耗大家的精力。 + +2、再写广搜的时候,可能担心会无限循环 + +即 A 走到 B,B又走到A,A又走到B ,这种情况,一般来说 广搜都是用一个 visit数组来标记的。 + +但本题不用,因为 不会重复走的,题图里的信号都是正数,根据距离判断大小 可以保证不走回头路。 + +```CPP +#include +#include +#include +using namespace std; +const int inf = 1e6; +int main () { + int n, m, startx, starty; + cin >> n >> m; + cin >> startx >> starty; + vector> grid(m, vector(n)); + vector> dis(m, vector(n, inf)); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + cin >> grid[i][j]; + } + } + queue> que; + int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; + que.push(pair(startx, starty)); + dis[startx][starty] = 0; + while(!que.empty()) { + pair cur = que.front(); que.pop(); + for (int i = 0; i < 4; i++) { + int newx = cur.first + dir[i][1]; + int newy = cur.second + dir[i][0]; + if (newx < 0 || newx >= m || newy < 0 || newy >= n || grid[cur.first][cur.second] == 0) continue; + + if (dis[newx][newy] > dis[cur.first][cur.second] + grid[cur.first][cur.second]) { + dis[newx][newy] = dis[cur.first][cur.second] + grid[cur.first][cur.second]; + que.push(pair(newx, newy)); + } + } + } + int result = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (dis[i][j] == inf) { + cout << -1 << endl; + return 0; + } + result = max(result, dis[i][j]); + } + } + cout << result << endl; +} +``` + +## 其他语言版本 + +### Java + +```Java +import java.util.*; + +public class Main { + static final int INF = 1000000; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int startX = scanner.nextInt(); + int startY = scanner.nextInt(); + + int[][] grid = new int[m][n]; + int[][] dis = new int[m][n]; + + for (int i = 0; i < m; i++) { + Arrays.fill(dis[i], INF); + for (int j = 0; j < n; j++) { + grid[i][j] = scanner.nextInt(); + } + } + + Queue queue = new LinkedList<>(); + int[][] directions = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; + + queue.add(new int[]{startX, startY}); + dis[startX][startY] = 0; + + while (!queue.isEmpty()) { + int[] current = queue.poll(); + for (int[] dir : directions) { + int newX = current[0] + dir[0]; + int newY = current[1] + dir[1]; + if (newX >= 0 && newX < m && newY >= 0 && newY < n && grid[current[0]][current[1]] != 0) { + if (dis[newX][newY] > dis[current[0]][current[1]] + grid[current[0]][current[1]]) { + dis[newX][newY] = dis[current[0]][current[1]] + grid[current[0]][current[1]]; + queue.add(new int[]{newX, newY}); + } + } + } + } + + int result = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (dis[i][j] == INF) { + System.out.println(-1); + return; + } + result = Math.max(result, dis[i][j]); + } + } + + System.out.println(result); + scanner.close(); + } +} +``` + +### Python + +```Python +from collections import deque + +inf = 1000000 + +def main(): + import sys + input = sys.stdin.read + data = input().split() + index = 0 + + n = int(data[index]) + m = int(data[index+1]) + startx = int(data[index+2]) + starty = int(data[index+3]) + index += 4 + + grid = [] + dis = [[inf] * n for _ in range(m)] + + for i in range(m): + grid.append([int(data[index+j]) for j in range(n)]) + index += n + + directions = [(0, 1), (1, 0), (-1, 0), (0, -1)] + queue = deque() + queue.append((startx, starty)) + dis[startx][starty] = 0 + + while queue: + curx, cury = queue.popleft() + for dx, dy in directions: + newx, newy = curx + dx, cury + dy + if 0 <= newx < m and 0 <= newy < n and grid[curx][cury] != 0: + if dis[newx][newy] > dis[curx][cury] + grid[curx][cury]: + dis[newx][newy] = dis[curx][cury] + grid[curx][cury] + queue.append((newx, newy)) + + result = 0 + for i in range(m): + for j in range(n): + if dis[i][j] == inf: + print(-1) + return + result = max(result, dis[i][j]) + + print(result) + +if __name__ == "__main__": + main() + + +``` diff --git "a/problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" "b/problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" new file mode 100644 index 0000000000..9ff0b684d7 --- /dev/null +++ "b/problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" @@ -0,0 +1,101 @@ + +# 可爱串 + +整体思路,就含有 子序列的字符串数量 减去 含有子串的字符串数量。 + +因为子序列数量已经是包含子串数量的。 剩下的就是 只有子序列 且没有子串的 字符串数量。 + + +需要注意我们求的不是 长度为 i 的字符串里有多少个 red 子序列。 + +**而是 可以有多少个 长度为i 的字符串 含有子序列 red** + +同理,可以有多少个长度为i的字符串含有 red 子串 + +认清这一点很重要! + +### 求子串 + +dp2[i][3] 长度为i 且 含有子串 red 的字符串数量 有多少 + +dp2[i][2] 长度为i 且 含有子串 re 的字符串数量有多少 + +dp2[i][1] 长度为 i 且 含有子串 r 的字符串数量有多少 + +dp2[1][0] 长度为 i 且 含有 只有 de, ee , e, d的字符串的字符串数量有多少。 + +```CPP +// 求子串 +dp2[0][0] = 1; +for(int i = 1;i <= n; i++) { + dp2[i][0] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0] * 2) % mod; // 含有 re 的可以把 r改成d, 含有r 的可以改成 + dp2[i][1] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0]) % mod; + dp2[i][2] = (dp2[i - 1][1]); + dp2[i][3] = (dp2[i - 1][3] * 3 + dp2[i - 1][2]) % mod; +} +`` + +### 求子序列 + +dp1[i][3] 长度为i 且 含有子序列 red 的字符串数量 有多少 + +dp2[i][2] 长度为i 且 含有子序列 re 的字符串数量有多少 + +dp2[i][1] 长度为 i 且 含有子序列 r 的字符串数量有多少 + +dp2[1][0] 长度为 i 且 含有 只含有 e 和 d 的字符串的字符串数量有多少。 + +```CPP + +// 求子序列 +dp1[0][0]=1; +for(int i=1;i<=n;i++) +{ + dp1[i][0] = (dp1[i - 1][0] * 2) % mod; + dp1[i][1] = (dp1[i - 1][0] + dp1[i - 1][1] * 2) % mod; + dp1[i][2] = (dp1[i - 1][1] + dp1[i - 1][2] * 2) % mod; + dp1[i][3] = (dp1[i - 1][2] + dp1[i - 1][3] * 3) % mod; +} +``` + + + +```CPP + +#include +using namespace std; + +using ll=long long; +const int mod=1e9+7; + +int main() +{ + int n; + + cin>>n; + vector> dp1(n + 1,vector (4,0)); + vector> dp2(n + 1,vector (4,0)); + // 求子串 + dp2[0][0] = 1; + for(int i = 1;i <= n; i++) { + dp2[i][0] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0] * 2) % mod; + dp2[i][1] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0]) % mod; + dp2[i][2] = (dp2[i - 1][1]); + dp2[i][3] = (dp2[i - 1][3] * 3 + dp2[i - 1][2]) % mod; + } + + // 求子序列 + dp1[0][0]=1; + for(int i=1;i<=n;i++) + { + dp1[i][0] = (dp1[i - 1][0] * 2) % mod; + dp1[i][1] = (dp1[i - 1][0] + dp1[i - 1][1] * 2) % mod; + dp1[i][2] = (dp1[i - 1][1] + dp1[i - 1][2] * 2) % mod; + dp1[i][3] = (dp1[i - 1][2] + dp1[i - 1][3] * 3) % mod; + } + + cout<<(dp1[n][3] - dp2[n][3])%mod; + +} + +``` diff --git "a/problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" "b/problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" new file mode 100644 index 0000000000..5425a605b8 --- /dev/null +++ "b/problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" @@ -0,0 +1,30 @@ + + +本题和[代码随想录:两个字符串的删除操作](https://www.programmercarl.com/0583.%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C.html) 思路基本是一样的。 + + +```CPP +#include +#include +using namespace std; +int main() { + string s1, s2; + cin >> s1 >> s2; + vector> dp(s1.size() + 1, vector(s2.size() + 1, 0)); + + // s1 如果变成空串的最小删除ASCLL值综合 + for (int i = 1; i <= s1.size(); i++) dp[i][0] = dp[i - 1][0] + s1[i - 1]; + // s2 如果变成空串的最小删除ASCLL值综合 + for (int j = 1; j <= s2.size(); j++) dp[0][j] = dp[0][j - 1] + s2[j - 1]; + + for (int i = 1; i <= s1.size(); i++) { + for (int j = 1; j <= s2.size(); j++) { + if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; + else dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1]); + } + } + cout << dp[s1.size()][s2.size()] << endl; + +} +``` + diff --git "a/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" "b/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" new file mode 100644 index 0000000000..362ea9714a --- /dev/null +++ "b/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" @@ -0,0 +1,93 @@ + + + +# 图论为什么统一使用ACM模式 + +代码随想录图论章节给大家统一换成ACM输入输出模式。 + +图论是在笔试还有面试中,通常都是以ACM模式来考察大家,而大家习惯在力扣刷题(核心代码模式),核心代码模式对图的存储和输出都隐藏了。 + +而图论题目的输出输出 相对其他类型题目来说是最难处理的。 + +ACM模式是最考察候选人对代码细节把控程度的, 图的构成,图的输出,这些只有ACM输入输出模式才能体现出来。 + +### 输入的细节 + +图论的输入难在 图的存储结构,**如果没有练习过 邻接表和邻接矩阵 ,很多录友是写不出来的**。 + +而力扣上是直接给好现成的 数据结构,可以直接用,所以练习不到图的输入,也练习不到邻接表和邻接矩阵。 + +**如果连邻接表 和 邻接矩阵都不知道或者写不出来的话,可以说 图论没有入门**。 + +举个例子,对于力扣 [797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/description/) ,录友了解深度优先搜索之后,这道题目就是模板题,是送分题。 + +如果面试的时候出一道原题 (笔试都是ACM模式,部分面试也是ACM模式),不少熟练刷力扣的录友都难住了,**因为不知道图应该怎么存,也不知道自己存的图如何去遍历**。 + +即使面试的时候,有的面试官,让你用核心代码模式做题,当你写出代码后,**面试官补充一句:这个图 你是怎么存的**? + +难道和面试官说:我只知道图的算法,但我不知道图怎么存。 + +后面大家在刷 代码随想录图论第一题[98. 所有可达路径](./0098.所有可达路径.md) 的时候,就可以感受到图存储的难点所在。 + +所以这也是为什么我要让大家练习 ACM模式,也是我为什么 在代码随想录图论讲解中,不惜自己亲自出题,让大家统一练习ACM模式。 + +### 输出的细节 + +同样,图论的输出也有细节,例如 求节点1 到节点5的所有路径, 输出可能是: + +``` +1 2 4 5 +1 3 5 +``` + +表示有两条路可以到节点5, 那储存这个结果需要二维数组,最后在一起输出,力扣是直接return数组就好了,但 ACM模式要求我们自己输出,这里有就细节了。 + +就拿 只输出一行数据,输出 `1 2 4 5` 来说, + +很多录友代码可能直接就这么写了: + +```CPP +for (int i = 0 ; i < result.size(); i++) { + cout << result[i] << " "; +} +``` + +这么写输出的结果是 `1 2 4 5 `, 发现结果是对的,一提交,发现OJ返回 格式错误 或者 结果错误。 + +如果没练习过这种输出方式的录友,就开始怀疑了,这结果一样一样的,怎么就不对,我在力扣上提交都是对的! + +**大家要注意,5 后面要不要有空格**! + +上面这段代码输出,5后面是加上了空格了,如果判题机判断 结果的长度,标准答案`1 2 4 5`长度是7,而上面代码输出的长度是 8,很明显就是不对的。 + +所以正确的写法应该是: + +```CPP +for (int i = 0 ; i < result.size() - 1; i++) { + cout << result[i] << " "; +} +cout << result[result.size() - 1]; +``` + +这么写,最后一个元素后面就没有空格了。 + +这是很多录友经常疏忽的,也是大家刷习惯了 力扣(核心代码模式)根本不会注意到的细节。 + +**同样在工程开发中,这些细节都是影响系统稳定运行的因素之一**。 + +**ACM模式 除了考验算法思路,也考验 大家对 代码的把控力度**, 而 核心代码模式 只注重算法的解题思路,所以输入输出这些就省略掉了。 + + +### 其他 + +**大家如果熟练ACM模式,那么核心代码模式没问题,但反过来就不一定了**。 + +而且我在讲解图论的时候,最头疼的就是找题,在力扣上 找题总是找不到符合思路且来完整表达算法精髓的题目。 + +特别是最短路算法相关的题目,例如 Bellman_ford系列 ,Floyd ,A * 等等总是找不到符合思路的题目。 + +索性统一我自己来出题,这其中也是巨大的工作量。为了给大家带来极致的学习体验,我在很多细节上都下了功夫。 + +等大家将图论刷完,就会感受到我的良苦用心。加油 + + diff --git "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" index 18d63e2d7b..50c3615733 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -3,6 +3,8 @@ 这一篇我们正式开始图论! +代码随想录图论中的算法题目将统一使用ACM模式,[为什么要使用ACM模式](./图论为什么用ACM模式.md) + ## 图的基本概念 二维坐标中,两点可以连成线,多个点连成的线就构成了图。 diff --git "a/problems/kamacoder/\345\245\275\344\272\214\345\217\211\346\240\221.md" "b/problems/kamacoder/\345\245\275\344\272\214\345\217\211\346\240\221.md" new file mode 100644 index 0000000000..fe48f2fd46 --- /dev/null +++ "b/problems/kamacoder/\345\245\275\344\272\214\345\217\211\346\240\221.md" @@ -0,0 +1,104 @@ + +本题和 [96.不同的二叉搜索树](https://www.programmercarl.com/0096.%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html) 比较像 + +* 取模这里很容易出错 +* 过程中所用到的数值都有可能超过int,所以要改用longlong + +```CPP +#include +#include +using namespace std; + +long long mod = 1e9 + 7; +long long dp(int t, vector& memory) { + if (t % 2 == 0) return 0; + if (t == 1) return 1; + if (memory[t] != -1) return memory[t]; + + long long result = 0; + // 枚举左右子树节点的数量 + for (int i = 1; i < t; i += 2) { + long long leftNum = dp(i, memory); // 左子树节点数量为i + long long rightNum = dp(t - i - 1, memory); // 右子树节点数量为t - i - 1 + result += (leftNum * rightNum) % mod; // 注意这里是乘的关系 + result %= mod; + } + memory[t] = result; + return result; +} +int main() { + int n; + cin >> n; + vector memory(n + 1, -1); + cout << dp(n, memory) << endl; +} +``` + + +```CPP +#include +#include +#include + +using namespace std; + +const int MOD = 1000000007; + +int main() { + int num; + cin >> num; + + if (num % 2 == 0) { + cout << 0 << endl; + return 0; + } + + vector dp(num + 1, 0); + dp[1] = 1; + + for (int i = 3; i <= num; i += 2) { + for (int j = 1; j <= i - 2; j += 2) { + dp[i] = (dp[i] + dp[j] * dp[i - 1 - j]) % MOD; + } + } + + cout << dp[num] << endl; + return 0; +} + +``` + + +第二题的代码 + +#include +using namespace std; + +long fastexp(long base,long n,long mod){ + long answer = 1; + while(n > 0){ + if(n % 2 == 1){ + answer = (answer * base) % mod; + } + base = (base * base) % mod; + n /= 2; + } + return answer; +} +int kawaiiStrings(int n) { + // write code here + std::vector f(n + 1), g(n + 1), h(n + 1); + long mod = 1000000007; + for (long i = 2; i <= n; i++) g[i] = (g[i - 1] * 2 + (i - 1) * fastexp(2,i-2,mod)) % mod; + for (long i = 3; i <= n; i++) f[i] = ((f[i - 1] * 3) % mod + g[i - 1]) % mod; + for (long i = 3; i <= n; i++) h[i] = (fastexp(3, i - 3, mod) + h[i - 1] * 3 - h[i - 3]) % mod; + return (f[n]-h[n]+mod)%mod; + +} + +int main(){ + int n; + cin >> n; + cout << kawaiiStrings(n) << endl; + return 0; +} diff --git "a/problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" "b/problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" deleted file mode 100644 index bdd8222ed5..0000000000 --- "a/problems/kamacoder/\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" +++ /dev/null @@ -1,50 +0,0 @@ - - -```CPP -#include -using namespace std; - int main() { - int index = 0; - long long optNum; - string s; - string cmd; - while(cin >> cmd){ - //cout << s << endl; - if(cmd == "insert"){ - string buff; - cin >> buff; - s.insert(index, buff); - index += buff.size(); - } - else if(cmd == "move"){ - cin >> optNum; - if(optNum > 0 && index + optNum <= s.size()) index += optNum; - if(optNum < 0 && index >= -optNum) index += optNum; - } - else if(cmd == "delete"){ - cin >> optNum; - if(index >= optNum && optNum != 0){ - s.erase(index - optNum, optNum); - index -= optNum; - } - } - else if(cmd == "copy"){ - if(index > 0) { - string tmp = s.substr(0, index); - s.insert(index, tmp); - } - } - else if(cmd == "end"){ - for(int i = 0; i < index; i++) { - cout << s[i]; - } - cout << '|'; - for(int i = index; i < s.size(); i++){ - cout << s[i]; - } - break; - } - } - return 0; - } -``` diff --git "a/problems/kamacoder/\345\256\214\347\276\216\346\225\260.md" "b/problems/kamacoder/\345\256\214\347\276\216\346\225\260.md" new file mode 100644 index 0000000000..1f801d7666 --- /dev/null +++ "b/problems/kamacoder/\345\256\214\347\276\216\346\225\260.md" @@ -0,0 +1,29 @@ + +```CPP +#include +#include +using namespace std; +int countOnes(long long num) { + int zeroCount = 0; + while (num > 0) { + if (num % 10 != 0) { // 检查最低位是否为0 + zeroCount++; + } + num /= 10; // 移除最低位 + } + return zeroCount; +} +int main() { + int n; + cin >> n; + vector vec(n); + for (int i = 0; i < n; i++) cin >> vec[i]; + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (countOnes(vec[i] * vec[j]) == 1) result++; + } + } + cout << result << endl; +} +``` diff --git "a/problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" "b/problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" new file mode 100644 index 0000000000..fd9f6ab356 --- /dev/null +++ "b/problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" @@ -0,0 +1,109 @@ + + + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int[] a = new int[n]; + int[] b = new int[n]; + + for (int i = 0; i < n; i++) { + a[i] = sc.nextInt(); + } + + for (int i = 0; i < n; i++) { + b[i] = sc.nextInt(); + } + + int p = -1, s = -1; + for (int i = 0; i < n; i++) { + if (a[i] == b[i]) p = i; + else break; + } + + for (int j = n - 1 ; j >= 0 ; j--) { + if (a[j] == b[j]) s = j; + else break; + } + + + boolean[][] dp = new boolean[n][n]; + int res = 0; + + for (int j = 0; j < n; j++) { + for (int i = j ; i >= 0 ; i--) { + if (i == j) dp[i][j] = a[i] == b[i]; + else if (i + 1 == j) dp[i][j] = (a[i] == b[j] && a[j] == b[i]); + else { + dp[i][j] = dp[i+1][j-1] && (a[i] == b[j] && a[j] == b[i]); + } + if (dp[i][j] && (i == 0 || p >= i-1) && (j == n - 1 || j+1 >= s)) res++; + } + } + System.out.println(res); + } + +} + + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + int[] a = new int[n]; + int[] b = new int[n]; + + for (int i = 0; i < n; i++) { + a[i] = sc.nextInt(); + } + + for (int i = 0; i < n; i++) { + b[i] = sc.nextInt(); + } + + int count = 0; + + // 遍历所有可能的区间 + for (int left = 0; left < n; left++) { + for (int right = left; right < n; right++) { + // 检查翻转区间 [left, right] 后,a 是否可以变成 b + if (canTransform(a, b, left, right)) { + count++; + } + } + } + + System.out.println(count); + } + + private static boolean canTransform(int[] a, int[] b, int left, int right) { + // 提前检查翻转区间的值是否可以匹配 + for (int i = left, j = right; i <= right; i++, j--) { + if (a[i] != b[j]) { + return false; + } + } + + // 检查翻转区间外的值是否匹配 + for (int i = 0; i < left; i++) { + if (a[i] != b[i]) { + return false; + } + } + + for (int i = right + 1; i < a.length; i++) { + if (a[i] != b[i]) { + return false; + } + } + + return true; + } +} diff --git "a/problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" "b/problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" new file mode 100644 index 0000000000..68aeb84505 --- /dev/null +++ "b/problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" @@ -0,0 +1,81 @@ + + + + +```CPP +#include +#include +#include + +using namespace std; + +// 定义二叉树节点结构 +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +// 根据层序遍历数组构建二叉树 +TreeNode* constructBinaryTree(const vector& levelOrder) { + if (levelOrder.empty()) return NULL; + + TreeNode* root = new TreeNode(stoi(levelOrder[0])); + queue q; + q.push(root); + int i = 1; + + while (!q.empty() && i < levelOrder.size()) { + TreeNode* current = q.front(); + q.pop(); + + if (i < levelOrder.size() && levelOrder[i] != "null") { + current->left = new TreeNode(stoi(levelOrder[i])); + q.push(current->left); + } + i++; + + if (i < levelOrder.size() && levelOrder[i] != "null") { + current->right = new TreeNode(stoi(levelOrder[i])); + q.push(current->right); + } + i++; + } + + return root; +} + +int result = 0; +int dfs(TreeNode* node) { + if (node == NULL) return 0; + int leftPath = dfs(node->left); + int rightPath = dfs(node->right); + + int leftNum = 0, rightNum = 0; + if (node->left != NULL && node->left->val == node->val) { + leftNum = leftPath + 1; + } + if (node->right != NULL && node->right->val == node->val) { + rightNum = rightPath + 1; + } + result = max(result, leftNum + rightNum); + return max(leftNum, rightNum); + +} + + +int main() { + int n; + cin >> n; + vector levelOrder(n); + for (int i = 0; i < n ; i++) cin >> levelOrder[i]; + + TreeNode* root = constructBinaryTree(levelOrder); + dfs(root); + cout << result << endl; + + return 0; +} +``` + diff --git "a/problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" "b/problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" deleted file mode 100644 index 791b6b6819..0000000000 --- "a/problems/kamacoder/\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" +++ /dev/null @@ -1,47 +0,0 @@ - - - -```CPP -#include -#include -#include -#include -using namespace std; -int main() { - unordered_set uset; - int n, a; - cin >> n; - while (n--) { - cin >> a; - uset.insert(a); - } - int m, x, vlan_id; - long long tb; - vector vecTB; - cin >> m; - while(m--) { - cin >> tb; - cin >> x; - vector vecVlan_id(x); - for (int i = 0; i < x; i++) { - cin >> vecVlan_id[i]; - } - for (int i = 0; i < x; i++) { - if (uset.find(vecVlan_id[i]) != uset.end()) { - vecTB.push_back(tb); - break; - } - } - - } - - cout << vecTB.size() << endl; - if (vecTB.size() != 0) { - sort(vecTB.begin(), vecTB.end()); - for (int i = 0; i < vecTB.size() ; i++) cout << vecTB[i] << " "; - } - - -} - -``` From 3848c96acd298f3b3593bb78eeb91dd1377a7d82 Mon Sep 17 00:00:00 2001 From: kimoge <1579457263@qq.com> Date: Mon, 15 Jul 2024 23:12:22 +0800 Subject: [PATCH 1186/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00097=5F=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...16\351\200\233\345\205\254\345\233\255.md" | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index 5465c356f9..e8d92cc29f 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -339,7 +339,7 @@ int main() { } } -``` +``` ## 空间优化 @@ -426,6 +426,68 @@ floyd算法的时间复杂度相对较高,适合 稠密图且源点较多的 ### Python +基于三维数组的Floyd + +```python +if __name__ == '__main__': + max_int = 10005 # 设置最大路径,因为边最大距离为10^4 + + n, m = map(int, input().split()) + + grid = [[[max_int] * (n+1) for _ in range(n+1)] for _ in range(n+1)] # 初始化三维dp数组 + + for _ in range(m): + p1, p2, w = map(int, input().split()) + grid[p1][p2][0] = w + grid[p2][p1][0] = w + + # 开始floyd + for k in range(1, n+1): + for i in range(1, n+1): + for j in range(1, n+1): + grid[i][j][k] = min(grid[i][j][k-1], grid[i][k][k-1] + grid[k][j][k-1]) + + # 输出结果 + z = int(input()) + for _ in range(z): + start, end = map(int, input().split()) + if grid[start][end][n] == max_int: + print(-1) + else: + print(grid[start][end][n]) +``` + +基于二维数组的Floyd + +```python +if __name__ == '__main__': + max_int = 10005 # 设置最大路径,因为边最大距离为10^4 + + n, m = map(int, input().split()) + + grid = [[max_int]*(n+1) for _ in range(n+1)] # 初始化二维dp数组 + + for _ in range(m): + p1, p2, val = map(int, input().split()) + grid[p1][p2] = val + grid[p2][p1] = val + + # 开始floyd + for k in range(1, n+1): + for i in range(1, n+1): + for j in range(1, n+1): + grid[i][j] = min(grid[i][j], grid[i][k] + grid[k][j]) + + # 输出结果 + z = int(input()) + for _ in range(z): + start, end = map(int, input().split()) + if grid[start][end] == max_int: + print(-1) + else: + print(grid[start][end]) +``` + ### Go ### Rust From 42987012694e2370572ed30ff5ff6f201e613c02 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 16 Jul 2024 10:33:07 +0800 Subject: [PATCH 1187/1533] =?UTF-8?q?106.=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\344\272\214\345\217\211\346\240\221.md" | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index 3518343fc4..bde61a7551 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -794,6 +794,60 @@ func rebuild(inorder []int, postorder []int, rootIdx int, l, r int) *TreeNode { } ``` +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func buildTree(inorder []int, postorder []int) *TreeNode { + if len(postorder) == 0 { + return nil + } + + // 后序遍历数组最后一个元素,就是当前的中间节点 + rootValue := postorder[len(postorder)-1] + root := &TreeNode{Val:rootValue} + + // 叶子结点 + if len(postorder) == 1 { + return root + } + + // 找到中序遍历的切割点 + var delimiterIndex int + for delimiterIndex = 0; delimiterIndex < len(inorder); delimiterIndex++ { + if inorder[delimiterIndex] == rootValue { + break; + } + } + + // 切割中序数组 + // 左闭右开区间:[0, delimiterIndex) + leftInorder := inorder[:delimiterIndex] + // [delimiterIndex + 1, end) + rightInorder := inorder[delimiterIndex+1:] + + // postorder 舍弃末尾元素 + postorder = postorder[:len(postorder)-1] + + // 切割后序数组 + // 依然左闭右开,注意这里使用了左中序数组大小作为切割点 + // [0, len(leftInorder)) + leftPostorder := postorder[:len(leftInorder)] + // [len(leftInorder), end) + rightPostorder := postorder[len(leftInorder):] + + root.Left = buildTree(leftInorder, leftPostorder) + root.Right = buildTree(rightInorder, rightPostorder) + + return root +} +``` + 105 从前序与中序遍历序列构造二叉树 ```go @@ -829,6 +883,60 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode { } ``` +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func buildTree(preorder []int, inorder []int) *TreeNode { + if len(preorder) == 0 { + return nil + } + + // 前序遍历数组第一个元素,就是当前的中间节点 + rootValue := preorder[0] + root := &TreeNode{Val:rootValue} + + // 叶子结点 + if len(preorder) == 1 { + return root + } + + // 找到中序遍历的切割点 + var delimiterIndex int + for delimiterIndex = 0; delimiterIndex < len(inorder); delimiterIndex++ { + if inorder[delimiterIndex] == rootValue { + break + } + } + + // 切割中序数组 + // 左闭右开区间:[0, delimiterIndex) + leftInorder := inorder[:delimiterIndex] + // [delimiterIndex + 1, end) + rightInorder := inorder[delimiterIndex+1:] + + // preorder 舍弃首位元素 + preorder = preorder[1:] + + // 切割前序数组 + // 依然左闭右开,注意这里使用了左中序数组大小作为切割点 + // [0, len(leftInorder)) + leftPreorder := preorder[:len(leftInorder)] + // [len(leftInorder), end) + rightPreorder := preorder[len(leftInorder):] + + root.Left = buildTree(leftPreorder, leftInorder) + root.Right = buildTree(rightPreorder, rightInorder) + + return root +} +``` + ### JavaScript From 5db796f3e2643db83e6829a0d7cfb459bb9781b5 Mon Sep 17 00:00:00 2001 From: kimoge <1579457263@qq.com> Date: Wed, 17 Jul 2024 09:53:30 +0800 Subject: [PATCH 1188/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=A1=E7=A0=81?= =?UTF-8?q?=E7=BD=910099.=E5=B2=9B=E5=B1=BF=E7=9A=84=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E6=B7=B1=E6=90=9C=EF=BC=8C=E6=B7=BB=E5=8A=A0Python3=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\346\267\261\346\220\234.md" | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 37f7086a2b..94ce8d5b9c 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -185,6 +185,100 @@ int main() { ### Python +版本一 + +```python +direction = [[0, 1], [1, 0], [0, -1], [-1, 0]] # 四个方向:上、右、下、左 + + +def dfs(grid, visited, x, y): + """ + 对一块陆地进行深度优先遍历并标记 + """ + for i, j in direction: + next_x = x + i + next_y = y + j + # 下标越界,跳过 + if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]): + continue + # 未访问的陆地,标记并调用深度优先搜索 + if not visited[next_x][next_y] and grid[next_x][next_y] == 1: + visited[next_x][next_y] = True + dfs(grid, visited, next_x, next_y) + + +if __name__ == '__main__': + # 版本一 + n, m = map(int, input().split()) + + # 邻接矩阵 + grid = [] + for i in range(n): + grid.append(list(map(int, input().split()))) + + # 访问表 + visited = [[False] * m for _ in range(n)] + + res = 0 + for i in range(n): + for j in range(m): + # 判断:如果当前节点是陆地,res+1并标记访问该节点,使用深度搜索标记相邻陆地。 + if grid[i][j] == 1 and not visited[i][j]: + res += 1 + visited[i][j] = True + dfs(grid, visited, i, j) + + print(res) +``` + +版本二 + +```python +direction = [[0, 1], [1, 0], [0, -1], [-1, 0]] # 四个方向:上、右、下、左 + + +def dfs(grid, visited, x, y): + """ + 对一块陆地进行深度优先遍历并标记 + """ + # 与版本一的差别,在调用前增加判断终止条件 + if visited[x][y] or grid[x][y] == 0: + return + visited[x][y] = True + + for i, j in direction: + next_x = x + i + next_y = y + j + # 下标越界,跳过 + if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]): + continue + # 由于判断条件放在了方法首部,此处直接调用dfs方法 + dfs(grid, visited, next_x, next_y) + + +if __name__ == '__main__': + # 版本二 + n, m = map(int, input().split()) + + # 邻接矩阵 + grid = [] + for i in range(n): + grid.append(list(map(int, input().split()))) + + # 访问表 + visited = [[False] * m for _ in range(n)] + + res = 0 + for i in range(n): + for j in range(m): + # 判断:如果当前节点是陆地,res+1并标记访问该节点,使用深度搜索标记相邻陆地。 + if grid[i][j] == 1 and not visited[i][j]: + res += 1 + dfs(grid, visited, i, j) + + print(res) +``` + ### Go ### Rust From 750ce672ea5ff0dafe96cde854d8f0aeedacfb51 Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Wed, 17 Jul 2024 14:05:50 +0800 Subject: [PATCH 1189/1533] =?UTF-8?q?fix:=20=E5=8D=A1=E7=A0=81=E7=BD=91010?= =?UTF-8?q?6.=E5=B2=9B=E5=B1=BF=E7=9A=84=E5=91=A8=E9=95=BF=20=E8=A1=A8?= =?UTF-8?q?=E8=BF=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 340630398f..48400a95f6 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -66,7 +66,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120105.png) -该录友的下边空格出界了,则说明找到一条边。 +该陆地的下边空格出界了,则说明找到一条边。 C++代码如下:(详细注释) From 7aa230ed40e7583a61ec6b6fb38500b17c0dfe27 Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Wed, 17 Jul 2024 14:13:19 +0800 Subject: [PATCH 1190/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910098.=E6=89=80=E6=9C=89=E5=8F=AF=E8=BE=BE?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\350\276\276\350\267\257\345\276\204.md" | 146 +++++++++++++++++- 1 file changed, 142 insertions(+), 4 deletions(-) diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 073d1a2e21..3281303dec 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -75,7 +75,7 @@ ## 插曲 -------------- +------------- 本题和力扣 [797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/description/) 是一样的,录友了解深度优先搜索之后,这道题目就是模板题,是送分题。 @@ -475,7 +475,7 @@ public class Main { } } } -``` +``` #### 邻接表写法 ```java @@ -564,7 +564,7 @@ def main(): if __name__ == "__main__": main() -``` +``` #### 邻接表写法 ``` python @@ -608,6 +608,145 @@ if __name__ == "__main__": ### Javascript +#### 邻接矩阵写法 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async ()=>(await iter.next()).value; + + +let graph; +let N, M; +// 收集符合条件的路径 +let result = []; +// 1节点到终点的路径 +let path = []; + +// 创建邻接矩阵,初始化邻接矩阵 +async function initGraph(){ + let line; + + line = await readline(); + [N, M] = line.split(' ').map(i => parseInt(i)) + graph = new Array(N + 1).fill(0).map(() => new Array(N + 1).fill(0)) + + while(M--){ + line = await readline() + const strArr = line ? line.split(' ').map(i => parseInt(i)) : undefined + strArr ? graph[strArr[0]][strArr[1]] = 1 : null + } +}; + +// 深度搜索 +function dfs(graph, x, n){ + // 当前遍历节点为x, 到达节点为n + if(x == n){ + result.push([...path]) + return + } + for(let i = 1 ; i <= n ; i++){ + if(graph[x][i] == 1){ + path.push(i) + dfs(graph, i, n ) + path.pop(i) + } + } +}; + +(async function(){ + // 创建邻接矩阵,初始化邻接矩阵 + await initGraph(); + + // 从节点1开始深度搜索 + path.push(1); + + // 深度搜索 + dfs(graph, 1, N ); + + // 输出 + if(result.length > 0){ + result.forEach(i => { + console.log(i.join(' ')) + }) + }else{ + console.log(-1) + } + +})(); + +``` + + + +#### 邻接表写法 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph; +let N, M; + +// 收集符合条件的路径 +let result = []; +// 1节点到终点的路径 +let path = []; + +// 创建邻接表,初始化邻接表 +async function initGraph() { + let line; + line = await readline(); + [N, M] = line.split(' ').map(i => parseInt(i)) + graph = new Array(N + 1).fill(0).map(() => new Array()) + + while (line = await readline()) { + const strArr = line.split(' ').map(i => parseInt(i)) + strArr ? graph[strArr[0]].push(strArr[1]) : null + } +}; + +// 深度搜索 +async function dfs(graph, x, n) { + // 当前遍历节点为x, 到达节点为n + if (x == n) { + result.push([...path]) + return + } + + graph[x].forEach(i => { + path.push(i) + dfs(graph, i, n) + path.pop(i) + }) +}; + +(async function () { + // 创建邻接表,初始化邻接表 + await initGraph(); + + // 从节点1开始深度搜索 + path.push(1); + + // 深度搜索 + dfs(graph, 1, N); + + // 输出 + if (result.length > 0) { + result.forEach(i => { + console.log(i.join(' ')) + }) + } else { + console.log(-1) + } +})(); +``` + ### TypeScript ### PhP @@ -628,4 +767,3 @@ if __name__ == "__main__": - From 08a437eb3a2114316b4f4881d2311dcf983c7e08 Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Wed, 17 Jul 2024 14:20:18 +0800 Subject: [PATCH 1191/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910099.=E5=B2=9B=E5=B1=BF=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E5=B9=BF=E6=90=9C=200099.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E6=B7=B1=E6=90=9C=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\345\271\277\346\220\234.md" | 81 +++++++++++++++++++ ...60\351\207\217\346\267\261\346\220\234.md" | 75 +++++++++++++++++ 2 files changed, 156 insertions(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index fc7f38e913..b9bdb79695 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -198,6 +198,87 @@ int main() { ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph +let N, M +let visited +let result = 0 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x, y)开始广度优先遍历 + * @param {*} graph 地图 + * @param {*} visited 访问过的节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const bfs = (graph, visited, x, y) => { + let queue = [] + queue.push([x, y]) + visited[x][y] = true //只要加入队列就立刻标记为访问过 + + while (queue.length) { + let [x, y] = queue.shift() + for (let i = 0; i < 4; i++) { + let nextx = x + dir[i][0] + let nexty = y + dir[i][1] + if(nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if(!visited[nextx][nexty] && graph[nextx][nexty] === 1){ + queue.push([nextx, nexty]) + visited[nextx][nexty] = true + } + } + } + +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 统计岛屿数 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (!visited[i][j] && graph[i][j] === 1) { + // 遇到没访问过的陆地,+1 + result++ + + // 广度优先遍历,将相邻陆地标记为已访问 + bfs(graph, visited, i, j) + } + } + } + console.log(result); +})() +``` + + + ### TypeScript ### PhP diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 37f7086a2b..46f7e779a6 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -191,6 +191,81 @@ int main() { ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph +let N, M +let visited +let result = 0 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + +/** + * @description: 从节点x,y开始深度优先遍历 + * @param {*} graph 是地图,也就是一个二维数组 + * @param {*} visited 标记访问过的节点,不要重复访问 + * @param {*} x 表示开始搜索节点的下标 + * @param {*} y 表示开始搜索节点的下标 + * @return {*} + */ +const dfs = (graph, visited, x, y) => { + for (let i = 0; i < 4; i++) { + const nextx = x + dir[i][0] + const nexty = y + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if (!visited[nextx][nexty] && graph[nextx][nexty] === 1) { + visited[nextx][nexty] = true + dfs(graph, visited, nextx, nexty) + } + } +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 统计岛屿数 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (!visited[i][j] && graph[i][j] === 1) { + // 标记已访问 + visited[i][j] = true + + // 遇到没访问过的陆地,+1 + result++ + + // 深度优先遍历,将相邻陆地标记为已访问 + dfs(graph, visited, i, j) + } + } + } + console.log(result); +})() +``` + + + ### TypeScript ### PhP From 449099e2963f1522efed65378530596e1ad422a7 Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Wed, 17 Jul 2024 14:24:30 +0800 Subject: [PATCH 1192/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910100.=E5=B2=9B=E5=B1=BF=E7=9A=84=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E9=9D=A2=E7=A7=AF=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 19292be7a4..43e7e968ca 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -232,6 +232,96 @@ public: ### Javascript +```javascript +// 广搜版 + +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +let visited // 访问过的节点 +let result = 0 // 最大岛屿面积 +let count = 0 // 岛屿内节点数 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x, y)开始广度优先遍历 + * @param {*} graph 地图 + * @param {*} visited 访问过的节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const bfs = (graph, visited, x, y) => { + let queue = [] + queue.push([x, y]) + count++ + visited[x][y] = true //只要加入队列就立刻标记为访问过 + + while (queue.length) { + let [xx, yy] = queue.shift() + for (let i = 0; i < 4; i++) { + let nextx = xx + dir[i][0] + let nexty = yy + dir[i][1] + if(nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if(!visited[nextx][nexty] && graph[nextx][nexty] === 1){ + queue.push([nextx, nexty]) + count++ + visited[nextx][nexty] = true + } + } + } + +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 统计最大岛屿面积 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (!visited[i][j] && graph[i][j] === 1) { //遇到没有访问过的陆地 + // 重新计算面积 + count = 0 + + // 广度优先遍历,统计岛屿内节点数,并将岛屿标记为已访问 + bfs(graph, visited, i, j) + + // 更新最大岛屿面积 + result = Math.max(result, count) + } + } + } + console.log(result); +})() +``` + + + ### TypeScript ### PhP From b80f731427ca6527d2f3689933585e52a24895ee Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 17 Jul 2024 22:24:28 -0700 Subject: [PATCH 1193/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200104.=E5=BB=BA?= =?UTF-8?q?=E9=80=A0=E6=9C=80=E5=A4=A7=E5=B2=9B=E5=B1=BF=20DFS=E8=A7=A3?= =?UTF-8?q?=E6=B3=95Java=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\345\262\233\345\261\277.md" | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index b68e44424b..7d67b7fcb1 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -258,6 +258,111 @@ int main() { ## 其他语言版本 ### Java +```Java +public class Main { + // 该方法采用 DFS + // 定义全局变量 + // 记录每次每个岛屿的面积 + static int count; + // 对每个岛屿进行标记 + static int mark; + // 定义二维数组表示四个方位 + static int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + + // DFS 进行搜索,将每个岛屿标记为不同的数字 + public static void dfs(int[][] grid, int x, int y, boolean[][] visited) { + // 当遇到边界,直接return + if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) return; + // 遇到已经访问过的或者遇到海水,直接返回 + if (visited[x][y] || grid[x][y] == 0) return; + + visited[x][y] = true; + count++; + grid[x][y] = mark; + + // 继续向下层搜索 + dfs(grid, x, y + 1, visited); + dfs(grid, x, y - 1, visited); + dfs(grid, x + 1, y, visited); + dfs(grid, x - 1, y, visited); + } + + public static void main (String[] args) { + // 接收输入 + Scanner sc = new Scanner(System.in); + int m = sc.nextInt(); + int n = sc.nextInt(); + + int[][] grid = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + grid[i][j] = sc.nextInt(); + } + } + + // 初始化mark变量,从2开始(区别于0水,1岛屿) + mark = 2; + + // 定义二位boolean数组记录该位置是否被访问 + boolean[][] visited = new boolean[m][n]; + + // 定义一个HashMap,记录某片岛屿的标记号和面积 + HashMap getSize = new HashMap<>(); + + // 定义一个HashSet,用来判断某一位置水四周是否存在不同标记编号的岛屿 + HashSet set = new HashSet<>(); + + // 定义一个boolean变量,看看DFS之后,是否全是岛屿 + boolean isAllIsland = true; + + // 遍历二维数组进行DFS搜索,标记每片岛屿的编号,记录对应的面积 + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 0) isAllIsland = false; + if (grid[i][j] == 1) { + count = 0; + dfs(grid, i, j, visited); + getSize.put(mark, count); + mark++; + } + } + } + + int result = 0; + if (isAllIsland) result = m * n; + + // 对标记完的grid继续遍历,判断每个水位置四周是否有岛屿,并记录下四周不同相邻岛屿面积之和 + // 每次计算完一个水位置周围可能存在的岛屿面积之和,更新下result变量 + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 0) { + set.clear(); + // 当前水位置变更为岛屿,所以初始化为1 + int curSize = 1; + + for (int[] dir : dirs) { + int curRow = i + dir[0]; + int curCol = j + dir[1]; + + if (curRow < 0 || curRow >= m || curCol < 0 || curCol >= n) continue; + int curMark = grid[curRow][curCol]; + // 如果当前相邻的岛屿已经遍历过或者HashMap中不存在这个编号,继续搜索 + if (set.contains(curMark) || !getSize.containsKey(curMark)) continue; + set.add(curMark); + curSize += getSize.get(curMark); + } + + result = Math.max(result, curSize); + } + } + } + + // 打印结果 + System.out.println(result); + } +} + +``` ### Python From 0ed1eb394baaa66b0f7a51542fd6acf7f3880d02 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 17 Jul 2024 22:31:23 -0700 Subject: [PATCH 1194/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200103.=E6=B0=B4?= =?UTF-8?q?=E6=B5=81=E9=97=AE=E9=A2=98=20DFS=E8=A7=A3=E6=B3=95Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 259321b685..cc6e65aa68 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -281,6 +281,78 @@ for (int j = 0; j < m; j++) { ## 其他语言版本 ### Java +```Java +public class Main { + + // 采用 DFS 进行搜索 + public static void dfs(int[][] heights, int x, int y, boolean[][] visited, int preH) { + // 遇到边界或者访问过的点,直接返回 + if (x < 0 || x >= heights.length || y < 0 || y >= heights[0].length || visited[x][y]) return; + // 不满足水流入条件的直接返回 + if (heights[x][y] < preH) return; + // 满足条件,设置为true,表示可以从边界到达此位置 + visited[x][y] = true; + + // 向下一层继续搜索 + dfs(heights, x + 1, y, visited, heights[x][y]); + dfs(heights, x - 1, y, visited, heights[x][y]); + dfs(heights, x, y + 1, visited, heights[x][y]); + dfs(heights, x, y - 1, visited, heights[x][y]); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int m = sc.nextInt(); + int n = sc.nextInt(); + + int[][] heights = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + heights[i][j] = sc.nextInt(); + } + } + + // 初始化两个二位boolean数组,代表两个边界 + boolean[][] pacific = new boolean[m][n]; + boolean[][] atlantic = new boolean[m][n]; + + // 从左右边界出发进行DFS + for (int i = 0; i < m; i++) { + dfs(heights, i, 0, pacific, Integer.MIN_VALUE); + dfs(heights, i, n - 1, atlantic, Integer.MIN_VALUE); + } + + // 从上下边界出发进行DFS + for (int j = 0; j < n; j++) { + dfs(heights, 0, j, pacific, Integer.MIN_VALUE); + dfs(heights, m - 1, j, atlantic, Integer.MIN_VALUE); + } + + // 当两个边界二维数组在某个位置都为true时,符合题目要求 + List> res = new ArrayList<>(); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (pacific[i][j] && atlantic[i][j]) { + res.add(Arrays.asList(i, j)); + } + } + } + + // 打印结果 + for (List list : res) { + for (int k = 0; k < list.size(); k++) { + if (k == 0) { + System.out.print(list.get(k) + " "); + } else { + System.out.print(list.get(k)); + } + } + System.out.println(); + } + } +} + +``` ### Python From ef99b46cf853a92f3104f366ac07600e7b9756b9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 18 Jul 2024 02:06:43 -0700 Subject: [PATCH 1195/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200110.=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=8E=A5=E9=BE=99=20BFS=E8=A7=A3=E6=B3=95Jav?= =?UTF-8?q?a=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\344\270\262\346\216\245\351\276\231.md" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 5a60dbb7df..feeec6ddcc 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -153,6 +153,68 @@ int main() { ## 其他语言版本 ### Java +```Java +public class Main { + // BFS方法 + public static int ladderLength(String beginWord, String endWord, List wordList) { + // 使用set作为查询容器,效率更高 + HashSet set = new HashSet<>(wordList); + + // 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串 + Queue queue = new LinkedList<>(); + + // 声明一个hashMap存储遍历到的字符串以及所走过的路径path + HashMap visitMap = new HashMap<>(); + queue.offer(beginWord); + visitMap.put(beginWord, 1); + + while (!queue.isEmpty()) { + String curWord = queue.poll(); + int path = visitMap.get(curWord); + + for (int i = 0; i < curWord.length(); i++) { + char[] ch = curWord.toCharArray(); + // 每个位置尝试26个字母 + for (char k = 'a'; k <= 'z'; k++) { + ch[i] = k; + + String newWord = new String(ch); + if (newWord.equals(endWord)) return path + 1; + + // 如果这个新字符串存在于容器且之前未被访问到 + if (set.contains(newWord) && !visitMap.containsKey(newWord)) { + visitMap.put(newWord, path + 1); + queue.offer(newWord); + } + } + } + } + + return 0; + } + + public static void main (String[] args) { + /* code */ + // 接收输入 + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + sc.nextLine(); + String[] strs = sc.nextLine().split(" "); + + List wordList = new ArrayList<>(); + for (int i = 0; i < N; i++) { + wordList.add(sc.nextLine()); + } + + // wordList.add(strs[1]); + + // 打印结果 + int result = ladderLength(strs[0], strs[1], wordList); + System.out.println(result); + } +} + +``` ### Python From 612ff79f4f2e1efe60511f52d27d510e9eb417fe Mon Sep 17 00:00:00 2001 From: kimoge <1579457263@qq.com> Date: Fri, 19 Jul 2024 10:41:04 +0800 Subject: [PATCH 1196/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=A1=E7=A0=819?= =?UTF-8?q?9=5F=E5=A2=9E=E5=8A=A0Python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\345\271\277\346\220\234.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index b9bdb79695..def79bf771 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -192,6 +192,56 @@ int main() { ### Python +```python +from collections import deque + +# 四个方向 +position = [[0, 1], [1, 0], [0, -1], [-1, 0]] + + +def bfs(grid, visited, x, y): + """ + 广度优先搜索对陆地进行标记 + """ + + que = deque() # 创建队列 + + # 标记当前节点并加入队列 + visited[x][y] = True + que.append([x, y]) + + while que: + cur_x, cur_y = que.popleft() # 取出队首节点 + for i, j in position: + next_x = cur_x + i + next_y = cur_y + j + # 下一节点下标越界,跳过 + if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]): + continue + # 下一节点是陆地且未被访问,标记节点并加入队列 + if grid[next_x][next_y] == 1 and not visited[next_x][next_y]: + visited[next_x][next_y] = True + que.append([next_x, next_y]) + + +n, m = map(int, input().split()) +# 邻接矩阵 +grid = [] +for i in range(n): + grid.append(list(map(int, input().split()))) + +visited = [[False] * m for _ in range(n)] # 访问表 + +res = 0 +for i in range(n): + for j in range(m): + if grid[i][j] == 1 and not visited[i][j]: + res += 1 + bfs(grid, visited, i, j) + +print(res) +``` + ### Go ### Rust From cc7a9433e05d450b8f8bc732a8b15a27359bdd78 Mon Sep 17 00:00:00 2001 From: kimoge <1579457263@qq.com> Date: Fri, 19 Jul 2024 10:42:00 +0800 Subject: [PATCH 1197/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8D=A1=E7=A0=811?= =?UTF-8?q?00=5F=E5=A2=9E=E5=8A=A0Python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 43e7e968ca..024e509fcc 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -226,6 +226,100 @@ public: ### Python +DFS + +```python +# 四个方向 +position = [[0, 1], [1, 0], [0, -1], [-1, 0]] +count = 0 + + +def dfs(grid, visited, x, y): + """ + 深度优先搜索,对一整块陆地进行标记 + """ + global count # 定义全局变量,便于传递count值 + for i, j in position: + cur_x = x + i + cur_y = y + j + # 下标越界,跳过 + if cur_x < 0 or cur_x >= len(grid) or cur_y < 0 or cur_y >= len(grid[0]): + continue + if not visited[cur_x][cur_y] and grid[cur_x][cur_y] == 1: + visited[cur_x][cur_y] = True + count += 1 + dfs(grid, visited, cur_x, cur_y) + + +n, m = map(int, input().split()) +# 邻接矩阵 +grid = [] +for i in range(n): + grid.append(list(map(int, input().split()))) +# 访问表 +visited = [[False] * m for _ in range(n)] + +result = 0 # 记录最终结果 +for i in range(n): + for j in range(m): + if grid[i][j] == 1 and not visited[i][j]: + count = 1 + visited[i][j] = True + dfs(grid, visited, i, j) + result = max(count, result) + +print(result) +``` + +BFS + +```python +from collections import deque + +position = [[0, 1], [1, 0], [0, -1], [-1, 0]] # 四个方向 +count = 0 + + +def bfs(grid, visited, x, y): + """ + 广度优先搜索对陆地进行标记 + """ + global count # 声明全局变量 + que = deque() + que.append([x, y]) + while que: + cur_x, cur_y = que.popleft() + for i, j in position: + next_x = cur_x + i + next_y = cur_y + j + # 下标越界,跳过 + if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]): + continue + if grid[next_x][next_y] == 1 and not visited[next_x][next_y]: + visited[next_x][next_y] = True + count += 1 + que.append([next_x, next_y]) + + +n, m = map(int, input().split()) +# 邻接矩阵 +grid = [] +for i in range(n): + grid.append(list(map(int, input().split()))) +visited = [[False] * m for _ in range(n)] # 访问表 + +result = 0 # 记录最终结果 +for i in range(n): + for j in range(m): + if grid[i][j] == 1 and not visited[i][j]: + count = 1 + visited[i][j] = True + bfs(grid, visited, i, j) + res = max(result, count) + +print(result) +``` + ### Go ### Rust From 530f512211b155cc1643f3c0bdf2d05ee4bd1c0c Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Fri, 19 Jul 2024 14:18:14 +0800 Subject: [PATCH 1198/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910101.=E5=AD=A4=E5=B2=9B=E7=9A=84=E6=80=BB?= =?UTF-8?q?=E9=9D=A2=E7=A7=AF=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\346\200\273\351\235\242\347\247\257.md" | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 25de67ecd2..a1793337f6 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -242,6 +242,173 @@ print(count) ### Javascript +#### 深搜版 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +let count = 0 // 孤岛的总面积 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x,y)开始深度优先遍历地图 + * @param {*} graph 地图 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const dfs = (graph, x, y) => { + if(graph[x][y] === 0) return + graph[x][y] = 0 // 标记为海洋 + for (let i = 0; i < 4; i++) { + let nextx = x + dir[i][0] + let nexty = y + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + dfs(graph, nextx, nexty) + } +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 遍历地图左右两边 + for (let i = 0; i < N; i++) { + if (graph[i][0] === 1) dfs(graph, i, 0) + if (graph[i][M - 1] === 1) dfs(graph, i, M - 1) + } + + // 遍历地图上下两边 + for (let j = 0; j < M; j++) { + if (graph[0][j] === 1) dfs(graph, 0, j) + if (graph[N - 1][j] === 1) dfs(graph, N - 1, j) + } + + count = 0 + // 统计孤岛的总面积 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (graph[i][j] === 1) count++ + } + } + console.log(count); +})() +``` + + + +#### 广搜版 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +let count = 0 // 孤岛的总面积 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x,y)开始广度优先遍历地图 + * @param {*} graph 地图 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const bfs = (graph, x, y) => { + let queue = [] + queue.push([x, y]) + graph[x][y] = 0 // 只要加入队列,立刻标记 + + while (queue.length) { + let [xx, yy] = queue.shift() + for (let i = 0; i < 4; i++) { + let nextx = xx + dir[i][0] + let nexty = yy + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if (graph[nextx][nexty] === 1) { + queue.push([nextx, nexty]) + graph[nextx][nexty] = 0 // 只要加入队列,立刻标记 + } + } + } + +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 遍历地图左右两边 + for (let i = 0; i < N; i++) { + if (graph[i][0] === 1) bfs(graph, i, 0) + if (graph[i][M - 1] === 1) bfs(graph, i, M - 1) + } + + // 遍历地图上下两边 + for (let j = 0; j < M; j++) { + if (graph[0][j] === 1) bfs(graph, 0, j) + if (graph[N - 1][j] === 1) bfs(graph, N - 1, j) + } + + count = 0 + // 统计孤岛的总面积 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (graph[i][j] === 1) count++ + } + } + console.log(count); +})() +``` + + + ### TypeScript ### PhP From 3dce79d5873c71ddeadf2e9304a20272ff11987d Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Fri, 19 Jul 2024 14:22:29 +0800 Subject: [PATCH 1199/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910102.=E6=B2=89=E6=B2=A1=E5=AD=A4=E5=B2=9B=20J?= =?UTF-8?q?S=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\262\241\345\255\244\345\262\233.md" | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 5491a8e3d8..2b16da0472 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -146,6 +146,171 @@ int main() { ### Javascript +#### 深搜版 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x,y)开始深度优先遍历地图 + * @param {*} graph 地图 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const dfs = (graph, x, y) => { + if (graph[x][y] !== 1) return + graph[x][y] = 2 // 标记为非孤岛陆地 + + for (let i = 0; i < 4; i++) { + let nextx = x + dir[i][0] + let nexty = y + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + dfs(graph, nextx, nexty) + } +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 遍历地图左右两边 + for (let i = 0; i < N; i++) { + if (graph[i][0] === 1) dfs(graph, i, 0) + if (graph[i][M - 1] === 1) dfs(graph, i, M - 1) + } + + // 遍历地图上下两边 + for (let j = 0; j < M; j++) { + if (graph[0][j] === 1) dfs(graph, 0, j) + if (graph[N - 1][j] === 1) dfs(graph, N - 1, j) + } + + + // 遍历地图,将孤岛沉没,即将陆地1标记为0;将非孤岛陆地2标记为1 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (graph[i][j] === 1) graph[i][j] = 0 + else if (graph[i][j] === 2) graph[i][j] = 1 + } + console.log(graph[i].join(' ')); + } +})() +``` + + + +#### 广搜版 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x,y)开始广度优先遍历地图 + * @param {*} graph 地图 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const bfs = (graph, x, y) => { + let queue = [] + queue.push([x, y]) + graph[x][y] = 2 // 标记为非孤岛陆地 + + while (queue.length) { + let [xx, yy] = queue.shift() + + for (let i = 0; i < 4; i++) { + let nextx = xx + dir[i][0] + let nexty = yy + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if (graph[nextx][nexty] === 1) bfs(graph, nextx, nexty) + } + } +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 遍历地图左右两边 + for (let i = 0; i < N; i++) { + if (graph[i][0] === 1) bfs(graph, i, 0) + if (graph[i][M - 1] === 1) bfs(graph, i, M - 1) + } + + // 遍历地图上下两边 + for (let j = 0; j < M; j++) { + if (graph[0][j] === 1) bfs(graph, 0, j) + if (graph[N - 1][j] === 1) bfs(graph, N - 1, j) + } + + + // 遍历地图,将孤岛沉没,即将陆地1标记为0;将非孤岛陆地2标记为1 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (graph[i][j] === 1) graph[i][j] = 0 + else if (graph[i][j] === 2) graph[i][j] = 1 + } + console.log(graph[i].join(' ')); + } +})() +``` + + + ### TypeScript ### PhP From 8d29bef3f7b5ce7064a0d0ca041f99914cb2ccf2 Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Sun, 21 Jul 2024 22:01:29 +0800 Subject: [PATCH 1200/1533] feat:add algorithm of go python js java etc. feat:add algorithm of go python js java etc. --- ...60\351\207\217\345\271\277\346\220\234.md" | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index b9bdb79695..3c069b4412 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -190,10 +190,164 @@ int main() { ### Java +```java + +import java.util.Scanner; + +public class Main { + static int[][] dir = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} }; // 四个方向 + + public static void dfs(int[][] grid, boolean[][] visited, int x, int y) { + for (int i = 0; i < 4; i++) { + int nextx = x + dir[i][0]; + int nexty = y + dir[i][1]; + if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过 + if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 没有访问过的 同时 是陆地的 + visited[nextx][nexty] = true; + dfs(grid, visited, nextx, nexty); + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] grid = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + grid[i][j] = scanner.nextInt(); + } + } + + boolean[][] visited = new boolean[n][m]; + + int result = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + visited[i][j] = true; + result++; // 遇到没访问过的陆地,+1 + dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true + } + } + } + + System.out.println(result); + } +} + + + +``` + + ### Python +```python + +def dfs(grid, visited, x, y): + dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 四个方向 + for d in dir: + nextx, nexty = x + d[0], y + d[1] + if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]): + if not visited[nextx][nexty] and grid[nextx][nexty] == 1: # 没有访问过的 同时 是陆地的 + visited[nextx][nexty] = True + dfs(grid, visited, nextx, nexty) + +def main(): + n, m = map(int, input().split()) + grid = [list(map(int, input().split())) for _ in range(n)] + visited = [[False] * m for _ in range(n)] + + result = 0 + for i in range(n): + for j in range(m): + if not visited[i][j] and grid[i][j] == 1: + visited[i][j] = True + result += 1 # 遇到没访问过的陆地,+1 + dfs(grid, visited, i, j) # 将与其链接的陆地都标记上 True + + print(result) + +if __name__ == "__main__": + main() + + + +``` + + ### Go +```go + +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +var dir = [4][2]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向 + +func dfs(grid [][]int, visited [][]bool, x, y int) { + for i := 0; i < 4; i++ { + nextx := x + dir[i][0] + nexty := y + dir[i][1] + if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) { + continue // 越界了,直接跳过 + } + if !visited[nextx][nexty] && grid[nextx][nexty] == 1 { // 没有访问过的 同时 是陆地的 + visited[nextx][nexty] = true + dfs(grid, visited, nextx, nexty) + } + } +} + +func main() { + reader := bufio.NewReader(os.Stdin) + var n, m int + fmt.Scanf("%d %d", &n, &m) + + grid := make([][]int, n) + for i := 0; i < n; i++ { + grid[i] = make([]int, m) + line, _ := reader.ReadString('\n') + line = strings.TrimSpace(line) + elements := strings.Split(line, " ") + for j := 0; j < m; j++ { + grid[i][j], _ = strconv.Atoi(elements[j]) + } + } + + visited := make([][]bool, n) + for i := 0; i < n; i++ { + visited[i] = make([]bool, m) + } + + result := 0 + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + if !visited[i][j] && grid[i][j] == 1 { + visited[i][j] = true + result++ // 遇到没访问过的陆地,+1 + dfs(grid, visited, i, j) // 将与其链接的陆地都标记上 true + } + } + } + + fmt.Println(result) +} + + +``` + + + ### Rust ### Javascript @@ -283,12 +437,62 @@ const bfs = (graph, visited, x, y) => { ### PhP +```PHP + += count($grid) || $nexty < 0 || $nexty >= count($grid[0])) { + continue; // 越界了,直接跳过 + } + if (!$visited[$nextx][$nexty] && $grid[$nextx][$nexty] == 1) { // 没有访问过的 同时 是陆地的 + $visited[$nextx][$nexty] = true; + dfs($grid, $visited, $nextx, $nexty); + } + } +} + +function main() { + fscanf(STDIN, "%d %d", $n, $m); + $grid = []; + for ($i = 0; $i < $n; $i++) { + $grid[$i] = array_map('intval', explode(' ', trim(fgets(STDIN)))); + } + + $visited = array_fill(0, $n, array_fill(0, $m, false)); + + $result = 0; + for ($i = 0; $i < $n; $i++) { + for ($j = 0; $j < $m; $j++) { + if (!$visited[$i][$j] && $grid[$i][$j] == 1) { + $visited[$i][$j] = true; + $result++; // 遇到没访问过的陆地,+1 + dfs($grid, $visited, $i, $j); // 将与其链接的陆地都标记上 true + } + } + } + + echo $result . PHP_EOL; +} + +main(); +?> + + +``` + + ### Swift ### Scala ### C# + ### Dart ### C From 9eb819e3be9d75b2311837911594ee646e17954d Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Sun, 21 Jul 2024 15:34:30 -0700 Subject: [PATCH 1201/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200106.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E7=9A=84=E5=91=A8=E9=95=BF=20Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\347\232\204\345\221\250\351\225\277.md" | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 48400a95f6..6f3462c52d 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -157,7 +157,62 @@ int main() { ## 其他语言版本 -### Java +### Java +```Java +import java.util.*; + +public class Main { + // 每次遍历到1,探索其周围4个方向,并记录周长,最终合计 + // 声明全局变量,dirs表示4个方向 + static int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + // 统计每单个1的周长 + static int count; + + // 探索其周围4个方向,并记录周长 + public static void helper(int[][] grid, int x, int y) { + for (int[] dir : dirs) { + int nx = x + dir[0]; + int ny = y + dir[1]; + + // 遇到边界或者水,周长加一 + if (nx < 0 || nx >= grid.length || ny < 0 || ny >= grid[0].length + || grid[nx][ny] == 0) { + count++; + } + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + // 接收输入 + int M = sc.nextInt(); + int N = sc.nextInt(); + + int[][] grid = new int[M][N]; + for (int i = 0; i < M; i++) { + for (int j = 0; j < N; j++) { + grid[i][j] = sc.nextInt(); + } + } + + int result = 0; // 总周长 + for (int i = 0; i < M; i++) { + for (int j = 0; j < N; j++) { + if (grid[i][j] == 1) { + count = 0; + helper(grid, i, j); + // 更新总周长 + result += count; + } + } + } + + // 打印结果 + System.out.println(result); + } +} +``` ### Python From c7efbc1949688b59d20e626e138507aebca6e482 Mon Sep 17 00:00:00 2001 From: muzi-xiaoren <106290465+muzi-xiaoren@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:21:29 +0800 Subject: [PATCH 1202/1533] =?UTF-8?q?Update=200349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86.md--Ruby=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index e17e940f5f..77dfc50a61 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -511,7 +511,7 @@ object Solution { ``` -###Ruby +### Ruby: ```ruby def intersection(nums1, nums2) hash = {} From 1c2ac1587f86433b29ffb9618a35ebf9bd9eece3 Mon Sep 17 00:00:00 2001 From: robotsouper Date: Mon, 22 Jul 2024 08:51:23 -0400 Subject: [PATCH 1203/1533] =?UTF-8?q?Leetcode=20131(Java):=20=E7=9B=B8?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E6=80=9D=E8=B7=AF=E4=BD=86=E4=B8=AA=E4=BA=BA?= =?UTF-8?q?=E8=AE=A4=E4=B8=BA=E6=9B=B4=E5=A5=BD=E7=90=86=E8=A7=A3=E7=9A=84?= =?UTF-8?q?=E5=86=99=E6=B3=95=EF=BC=8C=E9=81=B5=E5=BE=AA=E5=89=8D=E4=B8=A4?= =?UTF-8?q?=E9=A2=98=EF=BC=88combination=20sum=EF=BC=89=E7=9A=84=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BB=8E=E8=80=8C=E6=9B=B4=E5=8A=A0=E6=98=93=E6=87=82?= =?UTF-8?q?=EF=BC=8C=E5=AF=B9=E4=BA=8Ecur=E7=9A=84=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=94=BE=E5=BC=83deque=EF=BC=8C=E4=BF=9D=E6=8C=81=E4=BC=A0?= =?UTF-8?q?=E7=BB=9F=E4=BB=8D=E7=84=B6=E4=BD=BF=E7=94=A8arraylist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\345\233\236\346\226\207\344\270\262.md" | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 822d4399f1..4a8686518b 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -310,39 +310,35 @@ public: ### Java ```Java class Solution { - List> lists = new ArrayList<>(); - Deque deque = new LinkedList<>(); - + //保持前几题一贯的格式, initialization + List> res = new ArrayList<>(); + List cur = new ArrayList<>(); public List> partition(String s) { - backTracking(s, 0); - return lists; + backtracking(s, 0, new StringBuilder()); + return res; } - - private void backTracking(String s, int startIndex) { - //如果起始位置大于s的大小,说明找到了一组分割方案 - if (startIndex >= s.length()) { - lists.add(new ArrayList(deque)); + private void backtracking(String s, int start, StringBuilder sb){ + //因为是起始位置一个一个加的,所以结束时start一定等于s.length,因为进入backtracking时一定末尾也是回文,所以cur是满足条件的 + if (start == s.length()){ + //注意创建一个新的copy + res.add(new ArrayList<>(cur)); return; } - for (int i = startIndex; i < s.length(); i++) { - //如果是回文子串,则记录 - if (isPalindrome(s, startIndex, i)) { - String str = s.substring(startIndex, i + 1); - deque.addLast(str); - } else { - continue; + //像前两题一样从前往后搜索,如果发现回文,进入backtracking,起始位置后移一位,循环结束照例移除cur的末位 + for (int i = start; i < s.length(); i++){ + sb.append(s.charAt(i)); + if (check(sb)){ + cur.add(sb.toString()); + backtracking(s, i + 1, new StringBuilder()); + cur.remove(cur.size() -1 ); } - //起始位置后移,保证不重复 - backTracking(s, i + 1); - deque.removeLast(); } } - //判断是否是回文串 - private boolean isPalindrome(String s, int startIndex, int end) { - for (int i = startIndex, j = end; i < j; i++, j--) { - if (s.charAt(i) != s.charAt(j)) { - return false; - } + + //helper method, 检查是否是回文 + private boolean check(StringBuilder sb){ + for (int i = 0; i < sb.length()/ 2; i++){ + if (sb.charAt(i) != sb.charAt(sb.length() - 1 - i)){return false;} } return true; } From 22a6522a37ed279ac9749ca394f7c9c1835c1c57 Mon Sep 17 00:00:00 2001 From: Relsola Date: Wed, 24 Jul 2024 00:05:41 +0800 Subject: [PATCH 1204/1533] =?UTF-8?q?Update=20707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=20=20=E4=BC=98=E5=8C=96=20C=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84=E5=92=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\351\223\276\350\241\250.md" | 135 +++++++++--------- 1 file changed, 65 insertions(+), 70 deletions(-) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 743c1d7fa6..141156cf3e 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -168,109 +168,103 @@ private: ### C: ```C -typedef struct MyLinkedList { - int val; - struct MyLinkedList* next; -}MyLinkedList; +typedef struct Node { + int val; + struct Node* next; +} Node; + + +typedef struct { + int size; + Node* data; +} MyLinkedList; /** Initialize your data structure here. */ MyLinkedList* myLinkedListCreate() { - //这个题必须用虚拟头指针,参数都是一级指针,头节点确定后没法改指向了!!! - MyLinkedList* head = (MyLinkedList *)malloc(sizeof (MyLinkedList)); - head->next = NULL; - return head; + MyLinkedList* obj = (MyLinkedList*)malloc(sizeof(MyLinkedList)); + Node* head = (Node*)malloc(sizeof(Node)); + head->next = (void*)0; + obj->data = head; + obj->size = 0; + return obj; } /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ int myLinkedListGet(MyLinkedList* obj, int index) { - MyLinkedList *cur = obj->next; - for (int i = 0; cur != NULL; i++){ - if (i == index){ - return cur->val; - } - else{ - cur = cur->next; - } + if (index < 0 || index >= obj->size) return -1; + + Node* cur = obj->data; + while (index-- >= 0) { + cur = cur->next; } - return -1; + + return cur->val; } /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */ void myLinkedListAddAtHead(MyLinkedList* obj, int val) { - MyLinkedList *nhead = (MyLinkedList *)malloc(sizeof (MyLinkedList)); - nhead->val = val; - nhead->next = obj->next; - obj->next = nhead; + Node* node = (Node*)malloc(sizeof(Node)); + node->val = val; + node->next = obj->data->next; + obj->data->next = node; + obj->size++; } /** Append a node of value val to the last element of the linked list. */ void myLinkedListAddAtTail(MyLinkedList* obj, int val) { - MyLinkedList *cur = obj; - while(cur->next != NULL){ + Node* cur = obj->data; + while (cur->next != ((void*)0)) { cur = cur->next; } - MyLinkedList *ntail = (MyLinkedList *)malloc(sizeof (MyLinkedList)); - ntail->val = val; - ntail->next = NULL; - cur->next = ntail; + + Node* tail = (Node*)malloc(sizeof(Node)); + tail->val = val; + tail->next = (void*)0; + cur->next = tail; + obj->size++; } /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */ void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) { - if (index == 0){ - myLinkedListAddAtHead(obj, val); - return; - } - MyLinkedList *cur = obj->next; - for (int i = 1 ;cur != NULL; i++){ - if (i == index){ - MyLinkedList* newnode = (MyLinkedList *)malloc(sizeof (MyLinkedList)); - newnode->val = val; - newnode->next = cur->next; - cur->next = newnode; - return; - } - else{ - cur = cur->next; - } + if (index > obj->size) return; + + Node* cur = obj->data; + while (index-- > 0) { + cur = cur->next; } + + Node* node = (Node*)malloc(sizeof(Node)); + node->val = val; + node->next = cur->next; + cur->next = node; + obj->size++; } /** Delete the index-th node in the linked list, if the index is valid. */ void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) { - if (index == 0){ - MyLinkedList *tmp = obj->next; - if (tmp != NULL){ - obj->next = tmp->next; - free(tmp); - } - return; - } - MyLinkedList *cur = obj->next; - for (int i = 1 ;cur != NULL && cur->next != NULL; i++){ - if (i == index){ - MyLinkedList *tmp = cur->next; - if (tmp != NULL) { - cur->next = tmp->next; - free(tmp); - } - return; - } - else{ - cur = cur->next; - } + if (index < 0 || index >= obj->size) return; + + Node* cur = obj->data; + while (index-- > 0) { + cur = cur->next; } - + + Node* temp = cur->next; + cur->next = temp->next; + free(temp); + obj->size--; } void myLinkedListFree(MyLinkedList* obj) { - while(obj != NULL){ - MyLinkedList *tmp = obj; - obj = obj->next; - free(tmp); - } + Node* tmp = obj->data; + while (tmp != NULL) { + Node* n = tmp; + tmp = tmp->next; + free(n); + } + free(obj); } /** @@ -1569,3 +1563,4 @@ public class MyLinkedList + From 3e6dc44fa1965cbaafbb4b3f9119b1a40183a481 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 25 Jul 2024 10:36:23 +0800 Subject: [PATCH 1205/1533] =?UTF-8?q?131.=E5=88=86=E5=89=B2=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E4=B8=B2=E5=A2=9E=E5=8A=A0Go=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=E4=BC=98=E5=8C=96=E5=9B=9E=E6=96=87=E4=B8=B2?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\345\233\236\346\226\207\344\270\262.md" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 4a8686518b..4eca0ddf53 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -527,6 +527,7 @@ class Solution: ``` ### Go +回溯 基本版 ```go var ( path []string // 放已经回文的子串 @@ -565,6 +566,63 @@ func isPalindrome(s string) bool { } ``` +回溯+动态规划优化回文串判断 +```go +var ( + result [][]string + path []string // 放已经回文的子串 + isPalindrome [][]bool // 放事先计算好的是否回文子串的结果 +) + +func partition(s string) [][]string { + result = make([][]string, 0) + path = make([]string, 0) + computePalindrome(s) + backtracing(s, 0) + return result +} + +func backtracing(s string, startIndex int) { + // 如果起始位置已经大于s的大小,说明已经找到了一组分割方案了 + if startIndex >= len(s) { + tmp := make([]string, len(path)) + copy(tmp, path) + result = append(result, tmp) + return + } + for i := startIndex; i < len(s); i++ { + if isPalindrome[startIndex][i] { // 是回文子串 + // 获取[startIndex,i]在s中的子串 + path = append(path, s[startIndex:i+1]) + } else { // 不是回文,跳过 + continue + } + backtracing(s, i + 1) // 寻找i+1为起始位置的子串 + path = path[:len(path)-1] // 回溯过程,弹出本次已经添加的子串 + } +} + +func computePalindrome(s string) { + // isPalindrome[i][j] 代表 s[i:j](双边包括)是否是回文字串 + isPalindrome = make([][]bool, len(s)) + for i := 0; i < len(isPalindrome); i++ { + isPalindrome[i] = make([]bool, len(s)) + } + for i := len(s)-1; i >= 0; i-- { + // 需要倒序计算, 保证在i行时, i+1行已经计算好了 + for j := i; j < len(s); j++ { + if j == i { + isPalindrome[i][j] = true + } else if j - i == 1 { + isPalindrome[i][j] = s[i] == s[j] + } else { + isPalindrome[i][j] = s[i] == s[j] && isPalindrome[i+1][j-1] + } + } + } +} +``` + ### JavaScript ```js From 11ddcbdac4667d27cc730fba2d35bfa9ec6bb66c Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 25 Jul 2024 11:30:14 +0800 Subject: [PATCH 1206/1533] =?UTF-8?q?93.=E5=A4=8D=E5=8E=9FIP=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 73d5e3c3b2..d1300a3933 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -143,7 +143,7 @@ for (int i = startIndex; i < s.size(); i++) { 代码如下: ```CPP -// 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法 +// 判断字符串s在左闭右闭区间[start, end]所组成的数字是否合法 bool isValid(const string& s, int start, int end) { if (start > end) { return false; @@ -208,7 +208,7 @@ private: } else break; // 不合法,直接结束本层循环 } } - // 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法 + // 判断字符串s在左闭右闭区间[start, end]所组成的数字是否合法 bool isValid(const string& s, int start, int end) { if (start > end) { return false; From 1c9f885fa52047f942bd8627c0ffd835e5a8c629 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 25 Jul 2024 14:50:10 +0800 Subject: [PATCH 1207/1533] =?UTF-8?q?90.=E5=AD=90=E9=9B=86II=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Go=E4=BD=BF=E7=94=A8used=E6=95=B0=E7=BB=84=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0090.\345\255\220\351\233\206II.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 03bbd1dca3..939ef369c9 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -310,6 +310,43 @@ class Solution: ``` ### Go +使用used数组 +```Go +var ( + result [][]int + path []int +) + +func subsetsWithDup(nums []int) [][]int { + result = make([][]int, 0) + path = make([]int, 0) + used := make([]bool, len(nums)) + sort.Ints(nums) // 去重需要排序 + backtracing(nums, 0, used) + return result +} + +func backtracing(nums []int, startIndex int, used []bool) { + tmp := make([]int, len(path)) + copy(tmp, path) + result = append(result, tmp) + for i := startIndex; i < len(nums); i++ { + // used[i - 1] == true,说明同一树枝candidates[i - 1]使用过 + // used[i - 1] == false,说明同一树层candidates[i - 1]使用过 + // 而我们要对同一树层使用过的元素进行跳过 + if i > 0 && nums[i] == nums[i-1] && used[i-1] == false { + continue + } + path = append(path, nums[i]) + used[i] = true + backtracing(nums, i + 1, used) + path = path[:len(path)-1] + used[i] = false + } +} +``` + +不使用used数组 ```Go var ( path []int From c3541789614a1703154125f3e7a26592f5f2bc5e Mon Sep 17 00:00:00 2001 From: Lifan Sun Date: Thu, 25 Jul 2024 16:11:38 +0800 Subject: [PATCH 1208/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=E9=80=92=E5=BD=92?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=20C++=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 64124fbb3c..f6b5ef6dbb 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -149,7 +149,35 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1) +**也可以通过递归的思路解决本题:** +基础情况:对于空链表,不需要移除元素。 + +递归情况:首先检查头节点的值是否为 val,如果是则移除头节点,答案即为在头节点的后续节点上递归的结果;如果头节点的值不为 val,则答案为头节点与在头节点的后续节点上递归得到的新链表拼接的结果。 + +```CPP +class Solution { +public: + ListNode* removeElements(ListNode* head, int val) { + // 基础情况:空链表 + if (head == nullptr) { + return nullptr; + } + + // 递归处理 + if (head->val == val) { + ListNode* newHead = removeElements(head->next, val); + delete head; + return newHead; + } else { + head->next = removeElements(head->next, val); + return head; + } + } +}; +``` +* 时间复杂度:O(n) +* 空间复杂度:O(n) ## 其他语言版本 From e828a4de861b4ecb5f2f68f91feeda1b1f17d2b9 Mon Sep 17 00:00:00 2001 From: SaladDay <92240037+SaladDay@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:26:01 +0800 Subject: [PATCH 1209/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=90=E6=A0=87?= =?UTF-8?q?=E9=A2=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0474.\344\270\200\345\222\214\351\233\266.md" | 2 +- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 7b46abeece..72d622437a 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -615,7 +615,7 @@ impl Solution { } } ``` -## C +### C ```c #define max(a, b) ((a) > (b) ? (a) : (b)) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 92616ed188..2e519b96fa 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -585,7 +585,7 @@ impl Solution { } } ``` -## C +### C ```c int getSum(int * nums, int numsSize){ From 2435807a68ba9b7915b4aa6323251dbd0d7a39ac Mon Sep 17 00:00:00 2001 From: HJHuangUM <57804285+Wogwan@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:03:19 -0700 Subject: [PATCH 1210/1533] update problem/151.md format --- ...4\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index b6b34be394..a000519862 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -474,6 +474,7 @@ class Solution: words = s.split() #type(words) --- list words = words[::-1] # 反转单词 return ' '.join(words) #列表转换成字符串 +``` ### Go: From 2d9604baa5dcc544f6610601a13a28d6b6b56117 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 26 Jul 2024 11:35:38 +0800 Subject: [PATCH 1211/1533] Update --- README.md | 4 +- ...55\344\271\260\345\234\237\345\234\260.md" | 6 +- ...8.\345\214\272\351\227\264\345\222\214.md" | 16 +- ...57\350\276\276\350\267\257\345\276\204.md" | 9 +- ...\344\275\231\350\277\236\346\216\245II.md" | 1 + ...75\351\231\205\350\261\241\346\243\213.md" | 59 +++++ ...72\351\227\264\347\277\273\350\275\254.md" | 132 ++++++++++ ...40\351\231\244\346\200\273\345\222\214.md" | 108 ++++++++ ...14\345\200\274\350\267\257\345\276\204.md" | 237 ++++++++++++++++++ ...01\345\255\227\347\254\246\344\270\262.md" | 66 +++++ ...27\347\232\204\346\216\222\345\210\227.md" | 98 ++++++++ ...6.\344\274\240\351\200\201\346\240\221.md" | 65 +++++ ...11\347\217\240\344\272\222\346\226\245.md" | 40 +++ ...14\345\220\214\350\212\261\351\241\272.md" | 53 ++++ ...65\347\232\204\346\235\203\345\200\274.md" | 46 ++++ ...40\351\231\244\346\200\273\345\222\214.md" | 30 --- .../\345\245\275\346\225\260\347\273\204.md" | 56 +++++ ...72\351\227\264\347\277\273\350\275\254.md" | 109 -------- ...14\345\200\274\350\267\257\345\276\204.md" | 81 ------ ...04\346\200\273\347\273\223\347\257\207.md" | 7 + 20 files changed, 991 insertions(+), 232 deletions(-) create mode 100644 "problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" create mode 100644 "problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" create mode 100644 "problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" create mode 100644 "problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" create mode 100644 "problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" create mode 100644 "problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" create mode 100644 "problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" create mode 100644 "problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" create mode 100644 "problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" create mode 100644 "problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" delete mode 100644 "problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" create mode 100644 "problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" delete mode 100644 "problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" delete mode 100644 "problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" diff --git a/README.md b/README.md index 19d1bebbef..96852d706d 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,9 @@ 5. [数组:209.长度最小的子数组](./problems/0209.长度最小的子数组.md) 6. [数组:区间和](./problems/kamacoder/0058.区间和.md) 6. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md) -7. [数组:总结篇](./problems/数组总结篇.md) +7. [数组:区间和](./problems/kamacoder/0058.区间和.md) +8. [数组:开发商购买土地](./problems/kamacoder/0044.开发商购买土地.md) +9. [数组:总结篇](./problems/数组总结篇.md) ## 链表 diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" index 73e9e8c5f4..37bb98ed2d 100644 --- "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -1,6 +1,10 @@ # 44. 开发商购买土地 +> 本题为代码随想录后续扩充题目,还没有视频讲解,顺便让大家练习一下ACM输入输出模式(笔试面试必备) + +[题目链接](https://kamacoder.com/problempage.php?pid=1044) + 【题目描述】 在一个城市区域内,被划分成了n * m个连续的区块,每个区块都拥有不同的权值,代表着其土地价值。目前,有两家开发公司,A 公司和 B 公司,希望购买这个城市区域的土地。 @@ -57,7 +61,7 @@ 如果本题要求 任何两个行(或者列)之间的数值总和,大家在[0058.区间和](./0058.区间和.md) 的基础上 应该知道怎么求。 -就是前缀和的思路,先统计好,前n行的和 q[n],如果要求矩阵 a 行到 b行 之间的总和,那么就 q[b] - q[a - 1]就好。 +就是前缀和的思路,先统计好,前n行的和 q[n],如果要求矩阵 a行 到 b行 之间的总和,那么就 q[b] - q[a - 1]就好。 至于为什么是 a - 1,大家去看 [0058.区间和](./0058.区间和.md) 的分析,使用 前缀和 要注意 区间左右边的开闭情况。 diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index 0e478c68b0..f5ce08dcbf 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -1,6 +1,8 @@ # 58. 区间和 +> 本题为代码随想录后续扩充题目,还没有视频讲解,顺便让大家练习一下ACM输入输出模式(笔试面试必备) + [题目链接](https://kamacoder.com/problempage.php?pid=1070) 题目描述 @@ -97,11 +99,11 @@ int main() { 为什么呢? -p[1] = vec[0] + vec[1]; +`p[1] = vec[0] + vec[1];` -p[5] = vec[0] + vec[1] + vec[2] + vec[3] + vec[4] + vec[5]; +`p[5] = vec[0] + vec[1] + vec[2] + vec[3] + vec[4] + vec[5];` -p[5] - p[1] = vec[2] + vec[3] + vec[4] + vec[5]; +`p[5] - p[1] = vec[2] + vec[3] + vec[4] + vec[5];` 这不就是我们要求的 下标 2 到下标 5 之间的累加和吗。 @@ -109,15 +111,17 @@ p[5] - p[1] = vec[2] + vec[3] + vec[4] + vec[5]; ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627111319.png) -p[5] - p[1] 就是 红色部分的区间和。 +`p[5] - p[1]` 就是 红色部分的区间和。 -而 p 数组是我们之前就计算好的累加和,所以后面每次求区间和的之后 我们只需要 O(1)的操作。 +而 p 数组是我们之前就计算好的累加和,所以后面每次求区间和的之后 我们只需要 O(1) 的操作。 **特别注意**: 在使用前缀和求解的时候,要特别注意 求解区间。 如上图,如果我们要求 区间下标 [2, 5] 的区间和,那么应该是 p[5] - p[1],而不是 p[5] - p[2]。 -很多录友在使用前缀和的时候,分不清前缀和的区间,建议画一画图,模拟一下 思路会更清晰。 +**很多录友在使用前缀和的时候,分不清前缀和的区间,建议画一画图,模拟一下 思路会更清晰**。 + +本题C++代码如下: ```CPP #include diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 073d1a2e21..5e87cd7ee1 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -422,7 +422,8 @@ int main() { ## 其他语言版本 ### Java -#### 邻接矩阵写法 + +邻接矩阵写法 ```java import java.util.ArrayList; import java.util.List; @@ -477,7 +478,7 @@ public class Main { } ``` -#### 邻接表写法 +邻接表写法 ```java import java.util.ArrayList; import java.util.LinkedList; @@ -533,7 +534,7 @@ public class Main { } ``` ### Python -#### 邻接矩阵写法 +邻接矩阵写法 ``` python def dfs(graph, x, n, path, result): if x == n: @@ -566,7 +567,7 @@ if __name__ == "__main__": main() ``` -#### 邻接表写法 +邻接表写法 ``` python from collections import defaultdict diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 9c2f403941..80fe8777fd 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -225,6 +225,7 @@ int main() { vec.push_back(i); } } + // 情况一、情况二 if (vec.size() > 0) { // 放在vec里的边已经按照倒叙放的,所以这里就优先删vec[0]这条边 if (isTreeAfterRemoveEdge(edges, vec[0])) { diff --git "a/problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" "b/problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" new file mode 100644 index 0000000000..966aced348 --- /dev/null +++ "b/problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" @@ -0,0 +1,59 @@ + +# 113.国际象棋 + +广搜,但本题如果广搜枚举马和象的话会超时。 + +广搜要只枚举马的走位,同时判断是否在对角巷直接走象 + +```CPP +#include +using namespace std; +const int N = 100005, mod = 1000000007; +using ll = long long; +int n, ans; +int dir[][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, -1}, {-2, 1}}; +int main() { + int x1, y1, x2, y2; + cin >> n; + while (n--) { + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + if (x1 == x2 && y1 == y2) { + cout << 0 << endl; + continue; + } + // 判断象走一步到达 + int d = abs(x1 - x2) - abs(y1 - y2); + if (!d) {cout << 1 << endl; continue;} + // 判断马走一步到达 + bool one = 0; + for (int i = 0; i < 8; ++i) { + int dx = x1 + dir[i][0], dy = y1 + dir[i][1]; + if (dx == x2 && dy == y2) { + cout << 1 << endl; + one = true; + break; + } + } + if (one) continue; + // 接下来为两步的逻辑, 象走两步或者马走一步,象走一步 + // 象直接两步可以到达,这个计算是不是同颜色的格子,象可以在两步到达所有同颜色的格子 + int d2 = abs(x1 - x2) + abs(y1 - y2); + if (d2 % 2 == 0) { + cout << 2 << endl; + continue; + } + // 接下来判断马 + 象的组合 + bool two = 0; + for (int i = 0; i < 8; ++i) { + int dx = x1 + dir[i][0], dy = y1 + dir[i][1]; + int d = abs(dx - x2) - abs(dy - y2); + if (!d) {cout << 2 << endl; two = true; break;} + } + if (two) continue; + // 剩下的格子全都是三步到达的 + cout << 3 << endl; + } + return 0; +} + +``` diff --git "a/problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" "b/problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" new file mode 100644 index 0000000000..6e10aab7b8 --- /dev/null +++ "b/problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" @@ -0,0 +1,132 @@ + +# 121. 小红的区间翻转 + +比较暴力的方式,就是直接模拟, 枚举所有 区间,然后检查其翻转的情况。 + +在检查翻转的时候,需要一些代码优化,否则容易超时。 + +```CPP +#include +#include +using namespace std; + +bool canTransform(const vector& a, const vector& b, int left, int right) { + // 提前检查翻转区间的值是否可以匹配 + for (int i = left, j = right; i <= right; i++, j--) { + if (a[i] != b[j]) { + return false; + } + } + // 检查翻转区间外的值是否匹配 + for (int i = 0; i < left; i++) { + if (a[i] != b[i]) { + return false; + } + } + for (int i = right + 1; i < a.size(); i++) { + if (a[i] != b[i]) { + return false; + } + } + return true; +} + +int main() { + int n; + cin >> n; + + vector a(n); + vector b(n); + + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + + for (int i = 0; i < n; i++) { + cin >> b[i]; + } + + int count = 0; + + // 遍历所有可能的区间 + for (int left = 0; left < n; left++) { + for (int right = left; right < n; right++) { + // 检查翻转区间 [left, right] 后,a 是否可以变成 b + if (canTransform(a, b, left, right)) { + count++; + } + } + } + cout << count << endl; + return 0; +} +``` + +也可以事先计算好,最长公共前缀,和最长公共后缀。 + +在公共前缀和公共后缀之间的部分进行翻转操作,这样我们可以减少很多不必要的翻转尝试。 + +通过在公共前缀和后缀之间的部分,找到可以通过翻转使得 a 和 b 相等的区间。 + +以下 为评论区 卡码网用户:码鬼的C++代码 + +```CPP +#include +#include + +using namespace std; + +int main() { + int n; + cin >> n; + vector a(n), b(n); + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + for (int i = 0; i < n; i++) { + cin >> b[i]; + } + + vector prefix(n, 0), suffix(n, 0); + + // 计算前缀相等的位置 + int p = 0; + while (p < n && a[p] == b[p]) { + prefix[p] = 1; + p++; + } + + // 计算后缀相等的位置 + int s = n - 1; + while (s >= 0 && a[s] == b[s]) { + suffix[s] = 1; + s--; + } + + int count = 0; + + // 遍历所有可能的区间 + for (int i = 0; i < n - 1; i++) { + for (int j = i + 1; j < n; j++) { + // 判断前缀和后缀是否相等 + if ((i == 0 || prefix[i - 1] == 1) && (j == n - 1 || suffix[j + 1] == 1)) { + // 判断翻转后的子数组是否和目标数组相同 + bool is_palindrome = true; + for (int k = 0; k <= (j - i) / 2; k++) { + if (a[i + k] != b[j - k]) { + is_palindrome = false; + break; + } + } + if (is_palindrome) { + count++; + } + } + } + } + + cout << count << endl; + + return 0; +} +``` diff --git "a/problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" "b/problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" new file mode 100644 index 0000000000..ff34581ff0 --- /dev/null +++ "b/problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" @@ -0,0 +1,108 @@ + +# 142. 两个字符串的最小 ASCII 删除总和 + +本题和[代码随想录:两个字符串的删除操作](https://www.programmercarl.com/0583.%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C.html) 思路基本是一样的。 + +属于编辑距离问题,如果想彻底了解,建议看看「代码随想录」的编辑距离总结篇。 + +本题dp数组含义: + +dp[i][j] 表示 以i-1为结尾的字符串word1,和以j-1位结尾的字符串word2,想要达到相等,所需要删除元素的最小ASCII 删除总和。 + +如果 s1[i - 1] 与 s2[j - 1] 相同,则不用删:`dp[i][j] = dp[i - 1][j - 1]` + +如果 s1[i - 1] 与 s2[j - 1] 不相同,删word1 的 最小删除和: `dp[i - 1][j] + s1[i - 1]` ,删word2的最小删除和: `dp[i][j - 1] + s2[j - 1]` + +取最小值: `dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1])` + + + +```CPP +#include +#include +using namespace std; +int main() { + string s1, s2; + cin >> s1 >> s2; + vector> dp(s1.size() + 1, vector(s2.size() + 1, 0)); + + // s1 如果变成空串的最小删除ASCLL值综合 + for (int i = 1; i <= s1.size(); i++) dp[i][0] = dp[i - 1][0] + s1[i - 1]; + // s2 如果变成空串的最小删除ASCLL值综合 + for (int j = 1; j <= s2.size(); j++) dp[0][j] = dp[0][j - 1] + s2[j - 1]; + + for (int i = 1; i <= s1.size(); i++) { + for (int j = 1; j <= s2.size(); j++) { + if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; + else dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1]); + } + } + cout << dp[s1.size()][s2.size()] << endl; +} +``` + +### Java + +```Java +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String s1 = scanner.nextLine(); + String s2 = scanner.nextLine(); + int[][] dp = new int[s1.length() + 1][s2.length() + 1]; + + // s1 如果变成空串的最小删除ASCII值综合 + for (int i = 1; i <= s1.length(); i++) { + dp[i][0] = dp[i - 1][0] + s1.charAt(i - 1); + } + // s2 如果变成空串的最小删除ASCII值综合 + for (int j = 1; j <= s2.length(); j++) { + dp[0][j] = dp[0][j - 1] + s2.charAt(j - 1); + } + + for (int i = 1; i <= s1.length(); i++) { + for (int j = 1; j <= s2.length(); j++) { + if (s1.charAt(i - 1) == s2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + } else { + dp[i][j] = Math.min(dp[i - 1][j] + s1.charAt(i - 1), dp[i][j - 1] + s2.charAt(j - 1)); + } + } + } + System.out.println(dp[s1.length()][s2.length()]); + scanner.close(); + } +} + + +``` + +### python + +```python +def min_delete_sum(s1: str, s2: str) -> int: + dp = [[0] * (len(s2) + 1) for _ in range(len(s1) + 1)] + + # s1 如果变成空串的最小删除ASCII值综合 + for i in range(1, len(s1) + 1): + dp[i][0] = dp[i - 1][0] + ord(s1[i - 1]) + # s2 如果变成空串的最小删除ASCII值综合 + for j in range(1, len(s2) + 1): + dp[0][j] = dp[0][j - 1] + ord(s2[j - 1]) + + for i in range(1, len(s1) + 1): + for j in range(1, len(s2) + 1): + if s1[i - 1] == s2[j - 1]: + dp[i][j] = dp[i - 1][j - 1] + else: + dp[i][j] = min(dp[i - 1][j] + ord(s1[i - 1]), dp[i][j - 1] + ord(s2[j - 1])) + + return dp[len(s1)][len(s2)] + +if __name__ == "__main__": + s1 = input().strip() + s2 = input().strip() + print(min_delete_sum(s1, s2)) +``` diff --git "a/problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" "b/problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" new file mode 100644 index 0000000000..bf46c895f8 --- /dev/null +++ "b/problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" @@ -0,0 +1,237 @@ + + +# 143. 最长同值路径 + + +本题两个考点: + +1. 层序遍历构造二叉树 +2. 树形dp,找出最长路径 + +对于写代码不多,或者动手能力比较差的录友,第一个 构造二叉树 基本就被卡主了。 + +```CPP +#include +#include +#include + +using namespace std; + +// 定义二叉树节点结构 +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +// 根据层序遍历数组构建二叉树 +TreeNode* constructBinaryTree(const vector& levelOrder) { + if (levelOrder.empty()) return NULL; + + TreeNode* root = new TreeNode(stoi(levelOrder[0])); + queue q; + q.push(root); + int i = 1; + + while (!q.empty() && i < levelOrder.size()) { + TreeNode* current = q.front(); + q.pop(); + + if (i < levelOrder.size() && levelOrder[i] != "null") { + current->left = new TreeNode(stoi(levelOrder[i])); + q.push(current->left); + } + i++; + + if (i < levelOrder.size() && levelOrder[i] != "null") { + current->right = new TreeNode(stoi(levelOrder[i])); + q.push(current->right); + } + i++; + } + + return root; +} + +int result = 0; + +// 树形DP +int dfs(TreeNode* node) { + if (node == NULL) return 0; + int leftPath = dfs(node->left); + int rightPath = dfs(node->right); + + int leftNum = 0, rightNum = 0; + if (node->left != NULL && node->left->val == node->val) { + leftNum = leftPath + 1; + } + if (node->right != NULL && node->right->val == node->val) { + rightNum = rightPath + 1; + } + result = max(result, leftNum + rightNum); + return max(leftNum, rightNum); + +} + + +int main() { + int n; + cin >> n; + vector levelOrder(n); + for (int i = 0; i < n ; i++) cin >> levelOrder[i]; + + TreeNode* root = constructBinaryTree(levelOrder); + dfs(root); + cout << result << endl; + + return 0; +} +``` + +### Java + +```Java +import java.util.*; + +class TreeNode { + int val; + TreeNode left, right; + TreeNode(int x) { + val = x; + left = null; + right = null; + } +} + +public class Main { + public static int result = 0; + + public static TreeNode constructBinaryTree(List levelOrder) { + if (levelOrder.isEmpty()) return null; + + TreeNode root = new TreeNode(Integer.parseInt(levelOrder.get(0))); + Queue queue = new LinkedList<>(); + queue.add(root); + int i = 1; + + while (!queue.isEmpty() && i < levelOrder.size()) { + TreeNode current = queue.poll(); + + if (i < levelOrder.size() && !levelOrder.get(i).equals("null")) { + current.left = new TreeNode(Integer.parseInt(levelOrder.get(i))); + queue.add(current.left); + } + i++; + + if (i < levelOrder.size() && !levelOrder.get(i).equals("null")) { + current.right = new TreeNode(Integer.parseInt(levelOrder.get(i))); + queue.add(current.right); + } + i++; + } + + return root; + } + + public static int dfs(TreeNode node) { + if (node == null) return 0; + int leftPath = dfs(node.left); + int rightPath = dfs(node.right); + + int leftNum = 0, rightNum = 0; + if (node.left != null && node.left.val == node.val) { + leftNum = leftPath + 1; + } + if (node.right != null && node.right.val == node.val) { + rightNum = rightPath + 1; + } + result = Math.max(result, leftNum + rightNum); + return Math.max(leftNum, rightNum); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + sc.nextLine(); // consume the newline character + List levelOrder = new ArrayList<>(); + for (int i = 0; i < n; i++) { + levelOrder.add(sc.next()); + } + TreeNode root = constructBinaryTree(levelOrder); + dfs(root); + System.out.println(result); + sc.close(); + } +} + +``` + +### python + +```python +from typing import List, Optional +from collections import deque +import sys + +class TreeNode: + def __init__(self, val: int = 0, left: 'TreeNode' = None, right: 'TreeNode' = None): + self.val = val + self.left = left + self.right = right + +def construct_binary_tree(level_order: List[str]) -> Optional[TreeNode]: + if not level_order: + return None + + root = TreeNode(int(level_order[0])) + queue = deque([root]) + i = 1 + + while queue and i < len(level_order): + current = queue.popleft() + + if i < len(level_order) and level_order[i] != "null": + current.left = TreeNode(int(level_order[i])) + queue.append(current.left) + i += 1 + + if i < len(level_order) and level_order[i] != "null": + current.right = TreeNode(int(level_order[i])) + queue.append(current.right) + i += 1 + + return root + +result = 0 + +def dfs(node: Optional[TreeNode]) -> int: + global result + if node is None: + return 0 + + left_path = dfs(node.left) + right_path = dfs(node.right) + + left_num = right_num = 0 + if node.left is not None and node.left.val == node.val: + left_num = left_path + 1 + if node.right is not None and node.right.val == node.val: + right_num = right_path + 1 + + result = max(result, left_num + right_num) + return max(left_num, right_num) + +if __name__ == "__main__": + input = sys.stdin.read + data = input().strip().split() + + n = int(data[0]) + level_order = data[1:] + + root = construct_binary_tree(level_order) + dfs(root) + print(result) + + +``` diff --git "a/problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" new file mode 100644 index 0000000000..1528fdbd3b --- /dev/null +++ "b/problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" @@ -0,0 +1,66 @@ + +# 0144.字典序最小的01字符串 + +贪心思路:移动尽可能 移动前面的1 ,这样可以是 字典序最小 + +从前到后遍历,遇到 0 ,就用前面的 1 来交换 + +```CPP +#include +#include +using namespace std; +int main() { + int n,k; + cin >> n >> k; + string s; + cin >> s; + for(int i = 0; i < n && k > 0; i++) { + if(s[i] == '0') { + // 开始用前面的 1 来交换 + int j = i; + while(j > 0 && s[j - 1] == '1' && k > 0) { + swap(s[j], s[j - 1]); + --j; + --k; + } + } + } + cout << s << endl; + return 0; +} + +``` + +Java: + +```Java + +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int k = scanner.nextInt(); + scanner.nextLine(); // 消耗掉换行符 + String s = scanner.nextLine(); + char[] ch = s.toCharArray(); + + for (int i = 0; i < n && k > 0; i++) { + if (ch[i] == '0') { + // 开始用前面的 1 来交换 + int j = i; + while (j > 0 && ch[j - 1] == '1' && k > 0) { + char tmp = ch[j]; + ch[j] = ch[j - 1]; + ch[j - 1] = tmp; + j--; + k--; + } + } + } + + System.out.println(new String(ch)); + } +} +``` diff --git "a/problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" "b/problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" new file mode 100644 index 0000000000..757fe0b267 --- /dev/null +++ "b/problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" @@ -0,0 +1,98 @@ + +# 145. 数组子序列的排列 + +每个元素出现的次数相乘就可以了。 + +注意 “长度为 m 的数组,1 到 m 每个元素都出现过,且恰好出现 1 次。” ,题目中有n个元素,所以我们要统计的就是 1 到 n 元素出现的个数。 + +因为如果有一个元素x 大于n了, 那不可能出现 长度为x的数组 且 1 到 x 每个元素都出现过。 + +```CPP +#include "bits/stdc++.h" +using namespace std; +int main(){ + int n; + int x; + cin >> n; + unordered_map umap; + for(int i = 0; i < n; ++i){ + cin >> x; + if(umap.find(x) != umap.end()) umap[x]++; + else umap[x] = 1; + } + long long res = 0; + long long num = 1; + for (int i = 1; i <= n; i++) { + if (umap.find(i) == umap.end()) break; // 如果i都没出现,后面得数也不能 1 到 m 每个元素都出现过 + num = (num * umap[i]) % 1000000007; + res += num; + res %= 1000000007; + } + cout << res << endl; +} + +``` + +```Java + +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + Map map = new HashMap<>(); + for (int i = 0; i < n; i++) { + int x = sc.nextInt(); + map.put(x, map.getOrDefault(x, 0) + 1); + } + long res = 0; + long num = 1; + for (int i = 1; i <= n; i++) { + if (!map.containsKey(i)) break; // 如果i都没出现,后面得数也不能1到m每个元素都出现过 + num = (num * map.get(i)) % 1000000007; + res += num; + res %= 1000000007; + } + System.out.println(res); + sc.close(); + } +} + +``` + + +```python +def main(): + import sys + input = sys.stdin.read + data = input().split() + + n = int(data[0]) + umap = {} + + for i in range(1, n + 1): + x = int(data[i]) + if x in umap: + umap[x] += 1 + else: + umap[x] = 1 + + res = 0 + num = 1 + MOD = 1000000007 + + for i in range(1, n + 1): + if i not in umap: + break # 如果i都没出现,后面得数也不能1到m每个元素都出现过 + num = (num * umap[i]) % MOD + res = (res + num) % MOD + + print(res) + +if __name__ == "__main__": + main() + +``` diff --git "a/problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" "b/problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" new file mode 100644 index 0000000000..4cafb0402f --- /dev/null +++ "b/problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" @@ -0,0 +1,65 @@ + + + + +# 146. 传送树 + +本题题意是比较绕的,我后面给补上了 【提示信息】对 题目输出样例讲解一下,相对会容易理解的多。 + +```CPP +#include +#include +#include +using namespace std; + +vector> edge; // 邻接表来存图 +vector nxt; +int n; + +/* + * 递归函数,用于找到每个节点的下一个传送门节点,并记录在nxt数组中。 + * 遍历当前节点的所有子节点,递归调用findNext以确保子节点的nxt值已经计算出来。 + * 更新当前节点的nxt值为其子节点中编号最小的节点。 + * 如果当前节点是叶子节点(即没有子节点),则将其nxt值设置为自身。 + */ +void findNext(int node) { + for (int v : edge[node]) { + findNext(v); + if (nxt[node] == -1 || nxt[node] > min(v, nxt[v])) { + nxt[node] = min(v, nxt[v]); + } + } + + // 叶子节点 + if (nxt[node] == -1) { + nxt[node] = node; + } +} + +// 计算从节点u出发经过若干次传送门到达叶子节点所需的步数。 +// 通过不断访问nxt节点,直到到达叶子节点,记录访问的节点数。 +int get(int u) { + int cnt = 1; + while (nxt[u] != u) { + cnt++; + u = nxt[u]; + } + return cnt; +} + +int main() { + cin >> n; + edge.resize(n + 1); + nxt.resize(n + 1, -1); + for (int i = 1; i <= n; ++i) { + int a, b; + cin >> a >> b; + edge[a].push_back(b); + } + findNext(1); + for (int i = 1; i <= n; ++i) { + cout << get(i) << ' '; + } +} + +``` diff --git "a/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" "b/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" new file mode 100644 index 0000000000..ed14b3ce26 --- /dev/null +++ "b/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" @@ -0,0 +1,40 @@ + +1. 如果k * 3 大于 n 了,那说明一定没结果,如果没想明白,大家举个例子试试看 +2. 分别求出三个红珠子之间的距离 +3. 对这三段距离从小到大排序 y1, y2, y3 +4. 如果第一段距离y1 小于k,说明需要交换 k - y 次, 同理 第二段距离y2 小于k,说明需要交换 k - y2 次 +5. y1 y2 都调整好了,不用计算y3,因为 y3是距离最大 + + +```CPP +#include +using namespace std; + +int main(){ + int t; + cin >> t; + int n, k, a1, a2, a3; + vector dis(3); + + while (t--) { + cin >> n >> k >> a1 >> a2 >> a3; + if(k * 3 > n){ + cout << -1 << endl; + continue; + } + dis[0] = min(abs(a1 - a2), n - abs(a1 - a2)); + dis[1] = min(abs(a1 - a3), n - abs(a1 - a3)); + dis[2] = min(abs(a3 - a2), n - abs(a3 - a2)); + + sort(dis.begin(), dis.end()); + + int result = 0; + + if (dis[0] < k) result += (k - dis[0]); + if (dis[1] < k) result += (k - dis[1]); + + cout << result << endl; + } + return 0; +} +``` diff --git "a/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" "b/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" new file mode 100644 index 0000000000..b5d27a24f6 --- /dev/null +++ "b/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" @@ -0,0 +1,53 @@ + +首先我们要定义一个结构体,来存放我们的数据 + +`map<花色,{同一花色牌集合,同一花色的牌对应的牌数量}>` + +再遍历 每一个花色下,每一个牌 的数量 + +代码如下详细注释: + + +```CPP +#include +using namespace std; + +string cards[] = {"H","S","D","C"}; +typedef long long ll; +struct color +{ + set st; // 同一花色 牌的集合 + map cnt; // 同一花色 牌对应的数量 +}; +unordered_map umap; + +int main() { + int n; + cin >> n; + for (int i = 0; i < n; i++) { + int x, y; + string card; + cin >> x >> y >> card; + umap[card].st.insert(x); + umap[card].cnt[x] += y; + } + ll sum = 0; + // 遍历每一个花色 + for (string cardOne : cards) { + color colorOne = umap[cardOne]; + // 遍历 同花色 每一个牌 + for (int number : colorOne.st) { + ll numberCount = colorOne.cnt[number]; // 获取牌为number的数量是 numberCount + + // 统计 number 到 number + 4 都是否有牌,用cal 把 number 到number+4 的数量记下来 + ll cal = numberCount; + for (int j = number + 1; j <= number + 4; j++) cal = min(cal, colorOne.cnt[j]); + // 统计结果 + sum += cal; + // 把统计过的同花顺数量减下去 + for (int j = number + 1; j <= number + 4; j++) colorOne.cnt[j] -= cal; + } + } + cout << sum << endl; +} +``` diff --git "a/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" "b/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" new file mode 100644 index 0000000000..25fbd78128 --- /dev/null +++ "b/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" @@ -0,0 +1,46 @@ + + +按照动态规划的思路,每增加一位,对已有结果会有什么变化呢? + +输入 字符串s 里面有 0 和 1 + +我们先计算出 s[i-1] 的 子串权值和 a + +如果 s[i] 和 s[i - 1] 相同 + +下标0 到 i 这段字符串 的子串权值和就是 a + 1,即算上s[i]本身的连续串数量加上 + +如果 s[i] 和 s[i - 1] 不相同 + + + + + + +```CPP +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + string s; + cin >> s; + + long long result = 1; + long long a = 1; + + for (int i = 1; i < n; ++i) { + if (s[i] == s[i - 1]) { + a += 1; + result += a; + } else { + a = a + i + 1; + result += a; + } + } + cout << result << endl; + return 0; +} +``` diff --git "a/problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" "b/problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" deleted file mode 100644 index 5425a605b8..0000000000 --- "a/problems/kamacoder/\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" +++ /dev/null @@ -1,30 +0,0 @@ - - -本题和[代码随想录:两个字符串的删除操作](https://www.programmercarl.com/0583.%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C.html) 思路基本是一样的。 - - -```CPP -#include -#include -using namespace std; -int main() { - string s1, s2; - cin >> s1 >> s2; - vector> dp(s1.size() + 1, vector(s2.size() + 1, 0)); - - // s1 如果变成空串的最小删除ASCLL值综合 - for (int i = 1; i <= s1.size(); i++) dp[i][0] = dp[i - 1][0] + s1[i - 1]; - // s2 如果变成空串的最小删除ASCLL值综合 - for (int j = 1; j <= s2.size(); j++) dp[0][j] = dp[0][j - 1] + s2[j - 1]; - - for (int i = 1; i <= s1.size(); i++) { - for (int j = 1; j <= s2.size(); j++) { - if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; - else dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1]); - } - } - cout << dp[s1.size()][s2.size()] << endl; - -} -``` - diff --git "a/problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" "b/problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" new file mode 100644 index 0000000000..55853fa86c --- /dev/null +++ "b/problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" @@ -0,0 +1,56 @@ + + +算是贪心 + +整体思路是移动到中间位置(中位数),一定是 移动次数最小的。 + +有一个数可以不改变,对数组排序之后, 最小数 和 最大数 一定是移动次数最多的,所以分别保留最小 和 最大的不变。 + +中间可能有两个位置,所以要计算中间偏前 和 中间偏后的 + +代码如下: + +```CPP + +#include + +using namespace std; + +int main() { + int n; + cin >> n; + vector arr(n); + for (int i = 0; i < n; ++i) { + cin >> arr[i]; + } + sort(arr.begin(), arr.end()); + + if (arr[0] == arr[n - 1]) { + cout << 1 << endl; + return 0; + } + long cnt = 0L; + long cnt1 = 0L; + + // 如果要保留一个不改变,要不不改最小的,要不不改最大的。 + + // 取中间偏前的位置 + long mid = arr[(n - 2) / 2]; + + // 不改最大的 + for (int i = 0; i < n - 1; i++) { + cnt += abs(arr[i] - mid); + } + + // 取中间偏后的位置 + mid = arr[n / 2]; + + // 不改最小的 + for (int i = 1; i < n; i++) { + cnt1 += abs(arr[i] - mid); + } + + cout << min(cnt, cnt1) << endl; + return 0; +} +``` diff --git "a/problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" "b/problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" deleted file mode 100644 index fd9f6ab356..0000000000 --- "a/problems/kamacoder/\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" +++ /dev/null @@ -1,109 +0,0 @@ - - - -import java.util.Scanner; - -public class Main { - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - int[] a = new int[n]; - int[] b = new int[n]; - - for (int i = 0; i < n; i++) { - a[i] = sc.nextInt(); - } - - for (int i = 0; i < n; i++) { - b[i] = sc.nextInt(); - } - - int p = -1, s = -1; - for (int i = 0; i < n; i++) { - if (a[i] == b[i]) p = i; - else break; - } - - for (int j = n - 1 ; j >= 0 ; j--) { - if (a[j] == b[j]) s = j; - else break; - } - - - boolean[][] dp = new boolean[n][n]; - int res = 0; - - for (int j = 0; j < n; j++) { - for (int i = j ; i >= 0 ; i--) { - if (i == j) dp[i][j] = a[i] == b[i]; - else if (i + 1 == j) dp[i][j] = (a[i] == b[j] && a[j] == b[i]); - else { - dp[i][j] = dp[i+1][j-1] && (a[i] == b[j] && a[j] == b[i]); - } - if (dp[i][j] && (i == 0 || p >= i-1) && (j == n - 1 || j+1 >= s)) res++; - } - } - System.out.println(res); - } - -} - - -import java.util.Scanner; - -public class Main { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - - int[] a = new int[n]; - int[] b = new int[n]; - - for (int i = 0; i < n; i++) { - a[i] = sc.nextInt(); - } - - for (int i = 0; i < n; i++) { - b[i] = sc.nextInt(); - } - - int count = 0; - - // 遍历所有可能的区间 - for (int left = 0; left < n; left++) { - for (int right = left; right < n; right++) { - // 检查翻转区间 [left, right] 后,a 是否可以变成 b - if (canTransform(a, b, left, right)) { - count++; - } - } - } - - System.out.println(count); - } - - private static boolean canTransform(int[] a, int[] b, int left, int right) { - // 提前检查翻转区间的值是否可以匹配 - for (int i = left, j = right; i <= right; i++, j--) { - if (a[i] != b[j]) { - return false; - } - } - - // 检查翻转区间外的值是否匹配 - for (int i = 0; i < left; i++) { - if (a[i] != b[i]) { - return false; - } - } - - for (int i = right + 1; i < a.length; i++) { - if (a[i] != b[i]) { - return false; - } - } - - return true; - } -} diff --git "a/problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" "b/problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" deleted file mode 100644 index 68aeb84505..0000000000 --- "a/problems/kamacoder/\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" +++ /dev/null @@ -1,81 +0,0 @@ - - - - -```CPP -#include -#include -#include - -using namespace std; - -// 定义二叉树节点结构 -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// 根据层序遍历数组构建二叉树 -TreeNode* constructBinaryTree(const vector& levelOrder) { - if (levelOrder.empty()) return NULL; - - TreeNode* root = new TreeNode(stoi(levelOrder[0])); - queue q; - q.push(root); - int i = 1; - - while (!q.empty() && i < levelOrder.size()) { - TreeNode* current = q.front(); - q.pop(); - - if (i < levelOrder.size() && levelOrder[i] != "null") { - current->left = new TreeNode(stoi(levelOrder[i])); - q.push(current->left); - } - i++; - - if (i < levelOrder.size() && levelOrder[i] != "null") { - current->right = new TreeNode(stoi(levelOrder[i])); - q.push(current->right); - } - i++; - } - - return root; -} - -int result = 0; -int dfs(TreeNode* node) { - if (node == NULL) return 0; - int leftPath = dfs(node->left); - int rightPath = dfs(node->right); - - int leftNum = 0, rightNum = 0; - if (node->left != NULL && node->left->val == node->val) { - leftNum = leftPath + 1; - } - if (node->right != NULL && node->right->val == node->val) { - rightNum = rightPath + 1; - } - result = max(result, leftNum + rightNum); - return max(leftNum, rightNum); - -} - - -int main() { - int n; - cin >> n; - vector levelOrder(n); - for (int i = 0; i < n ; i++) cin >> levelOrder[i]; - - TreeNode* root = constructBinaryTree(levelOrder); - dfs(root); - cout << result << endl; - - return 0; -} -``` - diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index f026e41b11..7c2fd94724 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -117,6 +117,13 @@ 相信大家有遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,拆了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。 +### 前缀和 + +> 代码随想录后续补充题目 + +* [数组:求取区间和](https://programmercarl.com/kamacoder/0058.区间和.html) + +前缀和的思路其实很简单,但非常实用,如果没接触过的录友,也很难想到这个解法维度,所以 这是开阔思路 而难度又不高的好题。 ## 总结 From d3e60d2f9f27ea1a616ec8b8e0bcea1347716686 Mon Sep 17 00:00:00 2001 From: HJHuangUM <57804285+Wogwan@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:50:23 -0700 Subject: [PATCH 1212/1533] 07/25/2024 Correct typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Correct small typo in 双指针总结.md --- ...\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" index 02aec2e43e..a77490cdfe 100644 --- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" +++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" @@ -69,7 +69,7 @@ for (int i = 0; i < array.size(); i++) { 其实使用双指针也可以解决1.两数之和的问题,只不过1.两数之和求的是两个元素的下标,没法用双指针,如果改成求具体两个元素的数值就可以了,大家可以尝试用双指针做一个leetcode上两数之和的题目,就可以体会到我说的意思了。 -使用了哈希法解决了两数之和,但是哈希法并不使用于三数之和! +使用了哈希法解决了两数之和,但是哈希法并不适用于三数之和! 使用哈希法的过程中要把符合条件的三元组放进vector中,然后在去去重,这样是非常费时的,很容易超时,也是三数之和通过率如此之低的根源所在。 From 957c60dbfb02a6262d8e2eaa50539a4080acdc5f Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Sat, 27 Jul 2024 12:12:46 +0800 Subject: [PATCH 1213/1533] =?UTF-8?q?Update=200100.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md=20of=20php=20a?= =?UTF-8?q?nd=20go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0100.岛屿的最大面积.md of php and go --- ...00\345\244\247\351\235\242\347\247\257.md" | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 024e509fcc..ea62edc25c 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -322,6 +322,72 @@ print(result) ### Go +``` go + +package main + +import ( + "fmt" +) + +var count int +var dir = [][]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向 + +func dfs(grid [][]int, visited [][]bool, x, y int) { + for i := 0; i < 4; i++ { + nextx := x + dir[i][0] + nexty := y + dir[i][1] + if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) { + continue // 越界了,直接跳过 + } + if !visited[nextx][nexty] && grid[nextx][nexty] == 1 { // 没有访问过的 同时 是陆地的 + visited[nextx][nexty] = true + count++ + dfs(grid, visited, nextx, nexty) + } + } +} + +func main() { + var n, m int + fmt.Scan(&n, &m) + + grid := make([][]int, n) + for i := 0; i < n; i++ { + grid[i] = make([]int, m) + for j := 0; j < m; j++ { + fmt.Scan(&grid[i][j]) + } + } + + visited := make([][]bool, n) + for i := 0; i < n; i++ { + visited[i] = make([]bool, m) + } + + result := 0 + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + if !visited[i][j] && grid[i][j] == 1 { + count = 1 // 因为dfs处理下一个节点,所以这里遇到陆地了就先计数,dfs处理接下来的相邻陆地 + visited[i][j] = true + dfs(grid, visited, i, j) + if count > result { + result = count + } + } + } + } + + fmt.Println(result) +} + + + +``` + + + ### Rust ### Javascript @@ -420,6 +486,65 @@ const bfs = (graph, visited, x, y) => { ### PhP +``` php + += count($grid) || $nexty < 0 || $nexty >= count($grid[0])) continue; // 越界了,直接跳过 + if (!$visited[$nextx][$nexty] && $grid[$nextx][$nexty] == 1) { // 没有访问过的 同时 是陆地的 + $visited[$nextx][$nexty] = true; + $count++; + dfs($grid, $visited, $nextx, $nexty, $count, $dir); + } + } +} + +// Main function +function main() { + $input = trim(fgets(STDIN)); + list($n, $m) = explode(' ', $input); + + $grid = []; + for ($i = 0; $i < $n; $i++) { + $input = trim(fgets(STDIN)); + $grid[] = array_map('intval', explode(' ', $input)); + } + + $visited = []; + for ($i = 0; $i < $n; $i++) { + $visited[] = array_fill(0, $m, false); + } + + $result = 0; + $count = 0; + $dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 + + for ($i = 0; $i < $n; $i++) { + for ($j = 0; $j < $m; $j++) { + if (!$visited[$i][$j] && $grid[$i][$j] == 1) { + $count = 1; // 因为dfs处理下一个节点,所以这里遇到陆地了就先计数,dfs处理接下来的相邻陆地 + $visited[$i][$j] = true; + dfs($grid, $visited, $i, $j, $count, $dir); // 将与其链接的陆地都标记上 true + $result = max($result, $count); + } + } + } + + echo $result . "\n"; +} + +main(); + +?> + + +``` + + ### Swift ### Scala From 1ccadf6ee5fcafa30d6df61e93420f4662b6d10e Mon Sep 17 00:00:00 2001 From: hakyurxinf <96514344+hakyurxinf@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:46:21 +0800 Subject: [PATCH 1214/1533] =?UTF-8?q?Create=20107=E5=AF=BB=E6=89=BE?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E8=B7=AF=E5=BE=84Java=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\345\276\204Java\344\273\243\347\240\201" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" diff --git "a/107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" "b/107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" new file mode 100644 index 0000000000..7b83ca2622 --- /dev/null +++ "b/107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" @@ -0,0 +1,56 @@ +JAVA: + +```Java + +import java.util.*; + +public class Main{ + public static void main(String[] args) { + int N, M; + Scanner scanner = new Scanner(System.in); + N = scanner.nextInt(); + M = scanner.nextInt(); + DisJoint disJoint = new DisJoint(N + 1); + for (int i = 0; i < M; ++i) { + disJoint.join(scanner.nextInt(), scanner.nextInt()); + } + if(disJoint.isSame(scanner.nextInt(), scanner.nextInt())) { + System.out.println("1"); + } else { + System.out.println("0"); + } + } + +} + +//并查集模板 +class DisJoint{ + private int[] father; + + public DisJoint(int N) { + father = new int[N]; + for (int i = 0; i < N; ++i){ + father[i] = i; + } + } + + public int find(int n) { + return n == father[n] ? n : (father[n] = find(father[n])); + } + + public void join (int n, int m) { + n = find(n); + m = find(m); + if (n == m) return; + father[m] = n; + } + + public boolean isSame(int n, int m){ + n = find(n); + m = find(m); + return n == m; + } + +} + +``` From 13c293e343d954ab0197d8cd8882b2fc374edbd8 Mon Sep 17 00:00:00 2001 From: dongxuhua Date: Sun, 28 Jul 2024 17:26:11 +0800 Subject: [PATCH 1215/1533] =?UTF-8?q?0098.=E6=89=80=E6=9C=89=E5=8F=AF?= =?UTF-8?q?=E8=BE=BE=E8=B7=AF=E5=BE=84=E5=A2=9E=E5=8A=A0Go=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\350\276\276\350\267\257\345\276\204.md" | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 2d422a4f77..db35524ffd 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -605,6 +605,125 @@ if __name__ == "__main__": ``` ### Go +#### 邻接矩阵写法 +```go +package main + +import ( + "fmt" +) + +var result [][]int // 收集符合条件的路径 +var path []int // 1节点到终点的路径 + +func dfs(graph [][]int, x, n int) { + // 当前遍历的节点x 到达节点n + if x == n { // 找到符合条件的一条路径 + temp := make([]int, len(path)) + copy(temp, path) + result = append(result, temp) + return + } + for i := 1; i <= n; i++ { // 遍历节点x链接的所有节点 + if graph[x][i] == 1 { // 找到 x链接的节点 + path = append(path, i) // 遍历到的节点加入到路径中来 + dfs(graph, i, n) // 进入下一层递归 + path = path[:len(path)-1] // 回溯,撤销本节点 + } + } +} + +func main() { + var n, m int + fmt.Scanf("%d %d", &n, &m) + + // 节点编号从1到n,所以申请 n+1 这么大的数组 + graph := make([][]int, n+1) + for i := range graph { + graph[i] = make([]int, n+1) + } + + for i := 0; i < m; i++ { + var s, t int + fmt.Scanf("%d %d", &s, &t) + // 使用邻接矩阵表示无向图,1 表示 s 与 t 是相连的 + graph[s][t] = 1 + } + + path = append(path, 1) // 无论什么路径已经是从1节点出发 + dfs(graph, 1, n) // 开始遍历 + + // 输出结果 + if len(result) == 0 { + fmt.Println(-1) + } else { + for _, pa := range result { + for i := 0; i < len(pa)-1; i++ { + fmt.Print(pa[i], " ") + } + fmt.Println(pa[len(pa)-1]) + } + } +} +``` + +#### 邻接表写法 +```go +package main + +import ( + "fmt" +) + +var result [][]int +var path []int + +func dfs(graph [][]int, x, n int) { + if x == n { + temp := make([]int, len(path)) + copy(temp, path) + result = append(result, temp) + return + } + for _, i := range graph[x] { + path = append(path, i) + dfs(graph, i, n) + path = path[:len(path)-1] + } +} + +func main() { + var n, m int + fmt.Scanf("%d %d", &n, &m) + + graph := make([][]int, n+1) + for i := 0; i <= n; i++ { + graph[i] = make([]int, 0) + } + + for m > 0 { + var s, t int + fmt.Scanf("%d %d", &s, &t) + graph[s] = append(graph[s], t) + m-- + } + + path = append(path, 1) + dfs(graph, 1, n) + + if len(result) == 0 { + fmt.Println(-1) + } else { + for _, pa := range result { + for i := 0; i < len(pa)-1; i++ { + fmt.Print(pa[i], " ") + } + fmt.Println(pa[len(pa)-1]) + } + } +} +``` + ### Rust ### Javascript From c910803ed927f7f94068f1c9d7bdec045c8880ad Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Mon, 29 Jul 2024 09:51:26 +0800 Subject: [PATCH 1216/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910103.=E6=B0=B4=E6=B5=81=E9=97=AE=E9=A2=98=20J?= =?UTF-8?q?S=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 344 ++++++++++++++++++ 1 file changed, 344 insertions(+) diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index cc6e65aa68..b19e4778f1 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -362,6 +362,350 @@ public class Main { ### Javascript +#### 深搜 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x,y)开始深度优先遍历地图 + * @param {*} graph 地图 + * @param {*} visited 可访问节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const dfs = (graph, visited, x, y) => { + if (visited[x][y]) return + visited[x][y] = true // 标记为可访问 + + for (let i = 0; i < 4; i++) { + let nextx = x + dir[i][0] + let nexty = y + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界,跳过 + if (graph[x][y] < graph[nextx][nexty]) continue //不能流过.跳过 + dfs(graph, visited, nextx, nexty) + } +} + + +/** + * @description: 判断地图上的(x, y)是否可以到达第一组边界和第二组边界 + * @param {*} x 坐标 + * @param {*} y 坐标 + * @return {*} true可以到达,false不可以到达 + */ +const isResult = (x, y) => { + let visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + let isFirst = false //是否可到达第一边界 + let isSecond = false //是否可到达第二边界 + + // 深搜,将(x, y)可到达的所有节点做标记 + dfs(graph, visited, x, y) + + // 判断能否到第一边界左边 + for (let i = 0; i < N; i++) { + if (visited[i][0]) { + isFirst = true + break + } + } + + // 判断能否到第一边界上边 + for (let j = 0; j < M; j++) { + if (visited[0][j]) { + isFirst = true + break + } + } + + // 判断能否到第二边界右边 + for (let i = 0; i < N; i++) { + if (visited[i][M - 1]) { + isSecond = true + break + } + } + + // 判断能否到第二边界下边 + for (let j = 0; j < M; j++) { + if (visited[N - 1][j]) { + isSecond = true + break + } + } + + return isFirst && isSecond +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 遍历地图,判断是否能到达第一组边界和第二组边界 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (isResult(i, j)) console.log(i + ' ' + j); + } + } +})() +``` + + + +#### 广搜-解法一 + +```java +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x,y)开始广度优先遍历地图 + * @param {*} graph 地图 + * @param {*} visited 可访问节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const bfs = (graph, visited, x, y) => { + let queue = [] + queue.push([x, y]) + visited[x][y] = true + + while (queue.length) { + const [xx, yy] = queue.shift() + for (let i = 0; i < 4; i++) { + let nextx = xx + dir[i][0] + let nexty = yy + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过 + + // 可访问或者不能流过, 跳过 (注意这里是graph[xx][yy] < graph[nextx][nexty], 不是graph[x][y] < graph[nextx][nexty]) + if (visited[nextx][nexty] || graph[xx][yy] < graph[nextx][nexty]) continue + + queue.push([nextx, nexty]) + visited[nextx][nexty] = true + + } + } +} + + +/** + * @description: 判断地图上的(x, y)是否可以到达第一组边界和第二组边界 + * @param {*} x 坐标 + * @param {*} y 坐标 + * @return {*} true可以到达,false不可以到达 + */ +const isResult = (x, y) => { + let visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + let isFirst = false //是否可到达第一边界 + let isSecond = false //是否可到达第二边界 + + // 深搜,将(x, y)可到达的所有节点做标记 + bfs(graph, visited, x, y) + + // console.log(visited); + + // 判断能否到第一边界左边 + for (let i = 0; i < N; i++) { + if (visited[i][0]) { + isFirst = true + break + } + } + + // 判断能否到第一边界上边 + for (let j = 0; j < M; j++) { + if (visited[0][j]) { + isFirst = true + break + } + } + + // 判断能否到第二边界右边 + for (let i = 0; i < N; i++) { + if (visited[i][M - 1]) { + isSecond = true + break + } + } + + // 判断能否到第二边界下边 + for (let j = 0; j < M; j++) { + if (visited[N - 1][j]) { + isSecond = true + break + } + } + + return isFirst && isSecond +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 遍历地图,判断是否能到达第一组边界和第二组边界 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (isResult(i, j)) console.log(i + ' ' + j); + } + } +})() +``` + + + +#### 广搜-解法二 + +从第一边界和第二边界开始向高处流, 标记可以流到的位置, 两个边界都能到达的位置就是所求结果 + +```javascript + const r1 = require('readline').createInterface({ input: process.stdin }); + // 创建readline接口 + let iter = r1[Symbol.asyncIterator](); + // 创建异步迭代器 + const readline = async () => (await iter.next()).value; + + let graph // 地图 + let N, M // 地图大小 + const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + + + // 读取输入,初始化地图 + const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } + } + + + /** + * @description: 从(x,y)开始广度优先遍历地图 + * @param {*} graph 地图 + * @param {*} visited 可访问节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ + const bfs = (graph, visited, x, y) => { + if(visited[x][y]) return + + let queue = [] + queue.push([x, y]) + visited[x][y] = true + + while (queue.length) { + const [xx, yy] = queue.shift() + for (let i = 0; i < 4; i++) { + let nextx = xx + dir[i][0] + let nexty = yy + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过 + + // 可访问或者不能流过, 跳过 (注意因为是从边界往高处流, 所以这里是graph[xx][yy] >= graph[nextx][nexty], 还要注意不是graph[xx][yy] >= graph[nextx][nexty]) + if (visited[nextx][nexty] || graph[xx][yy] >= graph[nextx][nexty]) continue + + queue.push([nextx, nexty]) + visited[nextx][nexty] = true + } + } + } + + (async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 记录第一边界可到达的节点 + let firstBorder = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + // 记录第二边界可到达的节点 + let secondBorder = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + // 第一边界左边和第二边界右边 + for (let i = 0; i < N; i++) { + bfs(graph, firstBorder, i, 0) + bfs(graph, secondBorder, i, M - 1) + } + + // 第一边界上边和第二边界下边 + for (let j = 0; j < M; j++) { + bfs(graph, firstBorder, 0, j) + bfs(graph, secondBorder, N - 1, j) + } + + // 遍历地图,判断是否能到达第一组边界和第二组边界 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (firstBorder[i][j] && secondBorder[i][j]) console.log(i + ' ' + j); + } + } + })() +``` + + + ### TypeScript ### PhP From 6fa4ad4a91d5cbc80a7d98035a763e1afd2054e7 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 30 Jul 2024 09:35:12 +0800 Subject: [PATCH 1217/1533] =?UTF-8?q?455.=E5=88=86=E5=8F=91=E9=A5=BC?= =?UTF-8?q?=E5=B9=B2=E5=A2=9E=E5=8A=A0Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\217\221\351\245\274\345\271\262.md" | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 9f59d3adfb..22dd7570c8 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -226,21 +226,36 @@ class Solution: ``` ### Go -```golang -//排序后,局部最优 + +版本一 大饼干优先 +```Go func findContentChildren(g []int, s []int) int { - sort.Ints(g) - sort.Ints(s) - - // 从小到大 - child := 0 - for sIdx := 0; child < len(g) && sIdx < len(s); sIdx++ { - if s[sIdx] >= g[child] {//如果饼干的大小大于或等于孩子的为空则给与,否则不给予,继续寻找选一个饼干是否符合 - child++ + sort.Ints(g) + sort.Ints(s) + index := len(s) - 1 + result := 0 + for i := len(g) - 1; i >= 0; i-- { + if index >= 0 && s[index] >= g[i] { + result++ + index-- + } } - } + return result +} +``` - return child +版本二 小饼干优先 +```Go +func findContentChildren(g []int, s []int) int { + sort.Ints(g) + sort.Ints(s) + index := 0 + for i := 0; i < len(s); i++ { + if index < len(g) && g[index] <= s[i] { + index++ + } + } + return index } ``` From 029fffcf367c9b46e9ddd949a69ca44a5949871c Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Tue, 30 Jul 2024 09:52:42 +0800 Subject: [PATCH 1218/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910109.=E5=86=97=E4=BD=99=E8=BF=9E=E6=8E=A5II?= =?UTF-8?q?=20=E8=A1=A8=E8=BF=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 9c2f403941..916a2fd169 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -78,7 +78,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527120531.png) -对于情况二,删掉构成环的边就可以了。 +对于情况三,删掉构成环的边就可以了。 ## 写代码 From fac95a0a430ae42ea682fc6a89dab9b2394230f1 Mon Sep 17 00:00:00 2001 From: Violet_Fu <156555169+iamziqian@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:07:20 -0700 Subject: [PATCH 1219/1533] =?UTF-8?q?Update=201005.K=E6=AC=A1=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Java的版本二题解 --- ...04\346\225\260\347\273\204\345\222\214.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index fa27d3b7c3..4e04960098 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -128,6 +128,36 @@ class Solution { } } + +// 版本二:排序数组并贪心地尽可能将负数翻转为正数,再根据剩余的k值调整最小元素的符号,从而最大化数组的总和。 +class Solution { + public int largestSumAfterKNegations(int[] nums, int k) { + if (nums.length == 1) return nums[0]; + + // 排序:先把负数处理了 + Arrays.sort(nums); + + for (int i = 0; i < nums.length && k > 0; i++) { // 贪心点, 通过负转正, 消耗尽可能多的k + if (nums[i] < 0) { + nums[i] = -nums[i]; + k--; + } + } + + // 退出循环, k > 0 || k < 0 (k消耗完了不用讨论) + if (k % 2 == 1) { // k > 0 && k is odd:对于负数:负-正-负-正 + Arrays.sort(nums); // 再次排序得到剩余的负数,或者最小的正数 + nums[0] = -nums[0]; + } + // k > 0 && k is even,flip数字不会产生影响: 对于负数: 负-正-负;对于正数:正-负-正 + + int sum = 0; + for (int num : nums) { // 计算最大和 + sum += num; + } + return sum; + } +} ``` ### Python From 382053654cc322d845bbc4b9250270c8e6dee7d0 Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Tue, 30 Jul 2024 20:15:56 +0800 Subject: [PATCH 1220/1533] =?UTF-8?q?Update=200101.=E5=AD=A4=E5=B2=9B?= =?UTF-8?q?=E7=9A=84=E6=80=BB=E9=9D=A2=E7=A7=AF.md=20of=20other=20language?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0101.孤岛的总面积.md of other languages --- ...04\346\200\273\351\235\242\347\247\257.md" | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index a1793337f6..3faf784cf5 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -185,6 +185,77 @@ int main() { ### Java +``` java + +import java.util.*; + +public class Main { + private static int count = 0; + private static final int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; // 四个方向 + + private static void bfs(int[][] grid, int x, int y) { + Queue que = new LinkedList<>(); + que.add(new int[]{x, y}); + grid[x][y] = 0; // 只要加入队列,立刻标记 + count++; + while (!que.isEmpty()) { + int[] cur = que.poll(); + int curx = cur[0]; + int cury = cur[1]; + for (int i = 0; i < 4; i++) { + int nextx = curx + dir[i][0]; + int nexty = cury + dir[i][1]; + if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过 + if (grid[nextx][nexty] == 1) { + que.add(new int[]{nextx, nexty}); + count++; + grid[nextx][nexty] = 0; // 只要加入队列立刻标记 + } + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] grid = new int[n][m]; + + // 读取网格 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + grid[i][j] = scanner.nextInt(); + } + } + + // 从左侧边,和右侧边向中间遍历 + for (int i = 0; i < n; i++) { + if (grid[i][0] == 1) bfs(grid, i, 0); + if (grid[i][m - 1] == 1) bfs(grid, i, m - 1); + } + + // 从上边和下边向中间遍历 + for (int j = 0; j < m; j++) { + if (grid[0][j] == 1) bfs(grid, 0, j); + if (grid[n - 1][j] == 1) bfs(grid, n - 1, j); + } + + count = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) bfs(grid, i, j); + } + } + + System.out.println(count); + } +} + + + +``` + + ### Python ```python from collections import deque @@ -238,6 +309,97 @@ print(count) ``` ### Go +``` go + +package main + +import ( + "fmt" +) + +var count int +var dir = [4][2]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向 + +func bfs(grid [][]int, x, y int) { + queue := [][2]int{{x, y}} + grid[x][y] = 0 // 只要加入队列,立刻标记 + count++ + + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + curx, cury := cur[0], cur[1] + + for i := 0; i < 4; i++ { + nextx := curx + dir[i][0] + nexty := cury + dir[i][1] + + if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) { + continue // 越界了,直接跳过 + } + + if grid[nextx][nexty] == 1 { + queue = append(queue, [2]int{nextx, nexty}) + count++ + grid[nextx][nexty] = 0 // 只要加入队列立刻标记 + } + } + } +} + +func main() { + var n, m int + fmt.Scan(&n, &m) + + grid := make([][]int, n) + for i := range grid { + grid[i] = make([]int, m) + } + + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + fmt.Scan(&grid[i][j]) + } + } + + // 从左侧边,和右侧边向中间遍历 + for i := 0; i < n; i++ { + if grid[i][0] == 1 { + bfs(grid, i, 0) + } + if grid[i][m-1] == 1 { + bfs(grid, i, m-1) + } + } + + // 从上边和下边向中间遍历 + for j := 0; j < m; j++ { + if grid[0][j] == 1 { + bfs(grid, 0, j) + } + if grid[n-1][j] == 1 { + bfs(grid, n-1, j) + } + } + + // 清空之前的计数 + count = 0 + + // 遍历所有位置 + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + if grid[i][j] == 1 { + bfs(grid, i, j) + } + } + } + + fmt.Println(count) +} + + +``` + ### Rust ### Javascript From c49dd3b1595fe4b8e2adb991b0882a78774ca32f Mon Sep 17 00:00:00 2001 From: Violet_Fu <156555169+iamziqian@users.noreply.github.com> Date: Tue, 30 Jul 2024 13:21:10 -0700 Subject: [PATCH 1221/1533] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9?= =?UTF-8?q?=E7=AB=99.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0134.加油站的Java解法三 --- ...4.\345\212\240\346\262\271\347\253\231.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index c88b43b199..1329bd9a73 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -249,6 +249,29 @@ class Solution { } } ``` +``` +// 解法3 +class Solution { + public int canCompleteCircuit(int[] gas, int[] cost) { + int tank = 0; // 当前油量 + int totalGas = 0; // 总加油量 + int totalCost = 0; // 总油耗 + int start = 0; // 起点 + for (int i = 0; i < gas.length; i++) { + totalGas += gas[i]; + totalCost += cost[i]; + + tank += gas[i] - cost[i]; + if (tank < 0) { // tank 变为负数 意味着 从0到i之间出发都不能顺利环路一周,因为在此i点必会没油 + tank = 0; // reset tank,类似于题目53.最大子树和reset sum + start = i + 1; // 起点变为i点往后一位 + } + } + if (totalCost > totalGas) return -1; + return start; + } +} +``` ### Python 暴力法 From bf846e4a25e8952fb4d65cbf4ff8b2a46f3dc968 Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Wed, 31 Jul 2024 09:39:45 +0800 Subject: [PATCH 1222/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910053.=E5=AF=BB=E5=AE=9D-Kruskal=20=E8=A1=A8?= =?UTF-8?q?=E8=BF=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0053.\345\257\273\345\256\235-Kruskal.md" | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index e8b8e1ffd3..fb816fb1af 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -38,7 +38,7 @@ 5 6 2 5 7 1 6 7 1 -``` +``` 输出示例: @@ -79,7 +79,7 @@ kruscal的思路: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111114204.png) --------- +-------- 选边(4,5),节点4 和 节点 5 不在同一个集合,生成树可以添加边(4,5) ,并将节点4,节点5 放到同一个集合。 @@ -87,7 +87,7 @@ kruscal的思路: **大家判断两个节点是否在同一个集合,就看图中两个节点是否有绿色的粗线连着就行** ------- +------ (这里在强调一下,以下选边是按照上面排序好的边的数组来选择的) @@ -95,13 +95,13 @@ kruscal的思路: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112105834.png) ---------- +--------- 选边(2,6),节点2 和 节点6 不在同一个集合,生成树添加边(2,6),并将节点2,节点6 放到同一个集合。 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110214.png) --------- +-------- 选边(3,4),节点3 和 节点4 不在同一个集合,生成树添加边(3,4),并将节点3,节点4 放到同一个集合。 @@ -113,7 +113,7 @@ kruscal的思路: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110637.png) ------------ +----------- 选边(5,7),节点5 和 节点7 在同一个集合,不做计算。 @@ -122,7 +122,7 @@ kruscal的思路: 后面遍历 边(3,2),(2,4),(5,6) 同理,都因两个节点已经在同一集合,不做计算。 -------- +------- 此时 我们就已经生成了一个最小生成树,即: @@ -230,7 +230,7 @@ int main() { 如果题目要求将最小生成树的边输出的话,应该怎么办呢? -Kruskal 算法 输出边的话,相对prim 要容易很多,因为 Kruskal 本来就是直接操作边,边的结构自然清晰,不用像 prim一样 需要再节点练成线输出边 (因为prim是对节点操作,而 Kruskal是对边操作,这是本质区别) +Kruskal 算法 输出边的话,相对prim 要容易很多,因为 Kruskal 本来就是直接操作边,边的结构自然清晰,不用像 prim一样 需要再将节点连成线输出边 (因为prim是对节点操作,而 Kruskal是对边操作,这是本质区别) 本题中,边的结构为: From 573cfcf3c00dd19b4dd4e1f275bb14c3d9f6acb1 Mon Sep 17 00:00:00 2001 From: DengTao <54075362+only-tao@users.noreply.github.com> Date: Wed, 31 Jul 2024 13:51:43 +0800 Subject: [PATCH 1223/1533] =?UTF-8?q?Update=2020201017=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这里应该是"全局变量"吧 --- ...\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index bbb14502c0..03148b1575 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -40,7 +40,7 @@ * 陷阱二 -在一个有序序列求最值的时候,不要定义一个全局遍历,然后遍历序列更新全局变量求最值。因为最值可能就是int 或者 longlong的最小值。 +在一个有序序列求最值的时候,不要定义一个全局变量,然后遍历序列更新全局变量求最值。因为最值可能就是int 或者 longlong的最小值。 推荐要通过前一个数值(pre)和后一个数值比较(cur),得出最值。 From e0237b5b4f151e84762d66280f464effe48f440d Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 31 Jul 2024 14:39:37 +0800 Subject: [PATCH 1224/1533] =?UTF-8?q?134.=E5=8A=A0=E6=B2=B9=E7=AB=99?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E8=B4=AA=E5=BF=83=E7=AE=97=E6=B3=95?= =?UTF-8?q?=EF=BC=88=E6=96=B9=E6=B3=95=E4=B8=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\345\212\240\346\262\271\347\253\231.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index 1329bd9a73..7ac9f0f933 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -345,6 +345,37 @@ class Solution: ``` ### Go + +贪心算法(方法一) +```go +func canCompleteCircuit(gas []int, cost []int) int { + curSum := 0 + min := math.MaxInt64 + for i := 0; i < len(gas); i++ { + rest := gas[i] - cost[i] + curSum += rest + if curSum < min { + min = curSum + } + } + if curSum < 0 { + return -1 + } + if min >= 0 { + return 0 + } + for i := len(gas) - 1; i > 0; i-- { + rest := gas[i] - cost[i] + min += rest + if min >= 0 { + return i + } + } + return -1 +} +``` + +贪心算法(方法二) ```go func canCompleteCircuit(gas []int, cost []int) int { curSum := 0 From d1be6070c23c883d3d606397edb9bc923e639a5e Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 1 Aug 2024 10:25:20 +0800 Subject: [PATCH 1225/1533] Update --- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 34 +- ...11\347\217\240\344\272\222\346\226\245.md" | 44 +- ...14\345\220\214\350\212\261\351\241\272.md" | 69 ++ ...9.\345\245\275\346\225\260\347\273\204.md" | 52 +- ...65\347\232\204\346\235\203\345\200\274.md" | 54 +- ...47\241\20001\350\203\214\345\214\205-1.md" | 623 ++++++------------ ...47\241\20001\350\203\214\345\214\205-2.md" | 393 +++++------ 7 files changed, 603 insertions(+), 666 deletions(-) rename "problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" => "problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" (52%) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 2480b12c0a..5fe06d349d 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -158,16 +158,11 @@ 边:节点5 -> 节点6,权值为-2 ,minDist[6] > minDist[5] + (-2) ,更新 minDist[6] = minDist[5] + (-2) = 3 - 2 = 1 -如图: - -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110509.png) - +如图,将节点3加入队列,因为节点6已经在队列里,所以不用重复添加 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240729161116.png) -因为节点3 和 节点6 都曾经加入过队列,不用重复加入,避免重复计算。 - -在代码中我们可以用一个数组 visited 来记录入过队列的元素,加入过队列的元素,不再重复入队列。 - +所以我们在加入队列的过程可以有一个优化,用visited数组记录已经加入队列的元素,已经在队列的元素不用重复加入 -------------- @@ -175,11 +170,12 @@ 节点6作为终点,没有可以出发的边。 +同理从队列中取出节点3,也没有可以出发的边 + 所以直接从队列中取出,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115424.png) - ---------- 这样我们就完成了基于队列优化的bellman_ford的算法模拟过程。 @@ -190,12 +186,12 @@ 在上面模拟过程中,我们每次都要知道 一个节点作为出发点连接了哪些节点。 -如果想方便知道这些数据,就需要使用邻接表来存储这个图,如果对于邻接表不了解的话,可以看 [kama0047.参会dijkstra堆](./kama0047.参会dijkstra堆.md) 中 图的存储 部分。 +如果想方便知道这些数据,就需要使用邻接表来存储这个图,如果对于邻接表不了解的话,可以看 [kama0047.参会dijkstra堆](./0047.参会dijkstra堆.md) 中 图的存储 部分。 整体代码如下: -```CPP +``` #include #include #include @@ -215,7 +211,9 @@ int main() { int n, m, p1, p2, val; cin >> n >> m; - vector> grid(n + 1); // 邻接表 + vector> grid(n + 1); + + vector isInQueue(n + 1); // 加入优化,已经在队里里的元素不用重复添加 // 将所有边保存起来 for(int i = 0; i < m; i++){ @@ -230,24 +228,26 @@ int main() { minDist[start] = 0; queue que; - que.push(start); // 队列里放入起点 + que.push(start); while (!que.empty()) { int node = que.front(); que.pop(); - + isInQueue[node] = false; // 从队列里取出的时候,要取消标记 for (Edge edge : grid[node]) { int from = node; int to = edge.to; int value = edge.val; if (minDist[to] > minDist[from] + value) { // 开始松弛 - minDist[to] = minDist[from] + value; - que.push(to); + minDist[to] = minDist[from] + value; + if (isInQueue[to] == false) { // 已经在队列里的元素不用重复添加 + que.push(to); + isInQueue[to] = true; + } } } } - if (minDist[end] == INT_MAX) cout << "unconnected" << endl; // 不能到达终点 else cout << minDist[end] << endl; // 到达终点最短路径 } diff --git "a/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" "b/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" index ed14b3ce26..2ef5d39291 100644 --- "a/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" +++ "b/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" @@ -1,11 +1,12 @@ +# 三珠互斥 + 1. 如果k * 3 大于 n 了,那说明一定没结果,如果没想明白,大家举个例子试试看 2. 分别求出三个红珠子之间的距离 3. 对这三段距离从小到大排序 y1, y2, y3 4. 如果第一段距离y1 小于k,说明需要交换 k - y 次, 同理 第二段距离y2 小于k,说明需要交换 k - y2 次 5. y1 y2 都调整好了,不用计算y3,因为 y3是距离最大 - ```CPP #include using namespace std; @@ -29,12 +30,49 @@ int main(){ sort(dis.begin(), dis.end()); int result = 0; - if (dis[0] < k) result += (k - dis[0]); if (dis[1] < k) result += (k - dis[1]); - + cout << result << endl; } return 0; } ``` + +Java代码: + +```Java +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int t = scanner.nextInt(); + + while (t-- > 0) { + int n = scanner.nextInt(); + int k = scanner.nextInt(); + int a1 = scanner.nextInt(); + int a2 = scanner.nextInt(); + int a3 = scanner.nextInt(); + if (k * 3 > n) { + System.out.println(-1); + continue; + } + + List dis = new ArrayList<>(3); + dis.add(Math.min(Math.abs(a1 - a2), n - Math.abs(a1 - a2))); + dis.add(Math.min(Math.abs(a1 - a3), n - Math.abs(a1 - a3))); + dis.add(Math.min(Math.abs(a3 - a2), n - Math.abs(a3 - a2))); + + Collections.sort(dis); + + int result = 0; + if (dis.get(0) < k) result += (k - dis.get(0)); + if (dis.get(1) < k) result += (k - dis.get(1)); + + System.out.println(result); + } + } +} +``` diff --git "a/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" "b/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" index b5d27a24f6..cf8d325fba 100644 --- "a/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" +++ "b/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" @@ -1,4 +1,6 @@ +# 扑克牌同花顺 + 首先我们要定义一个结构体,来存放我们的数据 `map<花色,{同一花色牌集合,同一花色的牌对应的牌数量}>` @@ -51,3 +53,70 @@ int main() { cout << sum << endl; } ``` + +Java代码如下: + +```Java + +import java.util.*; + +public class Main { + static String[] cards = {"H", "S", "D", "C"}; // 花色数组 + + static class Color { + Set st; // 同一花色牌的集合 + Map cnt; // 同一花色牌对应的数量 + + Color() { + st = new HashSet<>(); // 初始化集合 + cnt = new HashMap<>(); // 初始化映射 + } + } + + static Map umap = new HashMap<>(); // 用于存储每种花色对应的Color对象 + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); // 读取牌的数量 + + for (int i = 0; i < n; i++) { + int x = scanner.nextInt(); // 读取牌的值 + int y = scanner.nextInt(); // 读取牌的数量 + String card = scanner.next(); // 读取牌的花色 + + umap.putIfAbsent(card, new Color()); // 如果不存在该花色,则创建一个新的Color对象 + umap.get(card).st.add(x); // 将牌的值加入集合 + umap.get(card).cnt.put(x, umap.get(card).cnt.getOrDefault(x, 0L) + y); // 更新牌的数量 + } + + long sum = 0; // 结果累加器 + + // 遍历每一种花色 + for (String cardOne : cards) { + Color colorOne = umap.getOrDefault(cardOne, new Color()); // 获取对应花色的Color对象 + + // 遍历同花色的每一张牌 + for (int number : colorOne.st) { + long numberCount = colorOne.cnt.get(number); // 获取当前牌的数量 + + // 计算从当前牌到number+4的最小数量 + long cal = numberCount; + for (int j = number + 1; j <= number + 4; j++) { + cal = Math.min(cal, colorOne.cnt.getOrDefault(j, 0L)); // 更新cal为最小值 + } + + // 将结果累加到sum + sum += cal; + + // 将统计过的同花顺数量减去 + for (int j = number + 1; j <= number + 4; j++) { + colorOne.cnt.put(j, colorOne.cnt.getOrDefault(j, 0L) - cal); + } + } + } + + System.out.println(sum); // 输出结果 + } +} + +``` diff --git "a/problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" "b/problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" similarity index 52% rename from "problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" rename to "problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" index 55853fa86c..09088168bc 100644 --- "a/problems/kamacoder/\345\245\275\346\225\260\347\273\204.md" +++ "b/problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" @@ -1,6 +1,7 @@ +# 149. 好数组 -算是贪心 +贪心思路: 整体思路是移动到中间位置(中位数),一定是 移动次数最小的。 @@ -11,9 +12,7 @@ 代码如下: ```CPP - #include - using namespace std; int main() { @@ -54,3 +53,50 @@ int main() { return 0; } ``` + +Java代码如下: + +```Java + +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + long[] arr = new long[n]; + for (int i = 0; i < n; ++i) { + arr[i] = scanner.nextLong(); + } + Arrays.sort(arr); + + if (arr[0] == arr[n - 1]) { + System.out.println(1); + return; + } + long cnt = 0L; + long cnt1 = 0L; + + // 如果要保留一个不改变,要不不改最小的,要不不改最大的。 + + // 取中间偏前的位置 + long mid = arr[(n - 2) / 2]; + + // 不改最大的 + for (int i = 0; i < n - 1; i++) { + cnt += Math.abs(arr[i] - mid); + } + + // 取中间偏后的位置 + mid = arr[n / 2]; + + // 不改最小的 + for (int i = 1; i < n; i++) { + cnt1 += Math.abs(arr[i] - mid); + } + + System.out.println(Math.min(cnt, cnt1)); + } +} + +``` diff --git "a/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" "b/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" index 25fbd78128..503d6a23ac 100644 --- "a/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" +++ "b/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" @@ -1,21 +1,7 @@ +# 150. 极长连续段的权值 -按照动态规划的思路,每增加一位,对已有结果会有什么变化呢? - -输入 字符串s 里面有 0 和 1 - -我们先计算出 s[i-1] 的 子串权值和 a - -如果 s[i] 和 s[i - 1] 相同 - -下标0 到 i 这段字符串 的子串权值和就是 a + 1,即算上s[i]本身的连续串数量加上 - -如果 s[i] 和 s[i - 1] 不相同 - - - - - +动态规划,枚举最后边节点的情况: ```CPP #include @@ -32,9 +18,11 @@ int main() { long long a = 1; for (int i = 1; i < n; ++i) { + // 加上本身长度为1的子串 if (s[i] == s[i - 1]) { a += 1; - result += a; + result += a; + // 以最右节点为终点,每个子串的级长连续段都+1,再加本身长度为1的子串 } else { a = a + i + 1; result += a; @@ -44,3 +32,35 @@ int main() { return 0; } ``` + +Java代码如下: + +```Java +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + String s = scanner.next(); + + long result = 1; + long a = 1; + + for (int i = 1; i < n; ++i) { + // 加上本身长度为1的子串 + if (s.charAt(i) == s.charAt(i - 1)) { + a += 1; + result += a; + // 以最右节点为终点,每个子串的级长连续段都+1,再加本身长度为1的子串 + } else { + a = a + i + 1; + result += a; + } + } + + System.out.println(result); + } +} + +``` diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index fa11fb94bc..0c78e3f695 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -17,21 +17,15 @@ ## 思路 +正式开始讲解背包问题! -这周我们正式开始讲解背包问题! - -背包问题的经典资料当然是:背包九讲。在公众号「代码随想录」后台回复:背包九讲,就可以获得背包九讲的pdf。 - -但说实话,背包九讲对于小白来说确实不太友好,看起来还是有点费劲的,而且都是伪代码理解起来也吃力。 - -对于面试的话,其实掌握01背包,和完全背包,就够用了,最多可以再来一个多重背包。 +对于面试的话,其实掌握01背包和完全背包,就够用了,最多可以再来一个多重背包。 如果这几种背包,分不清,我这里画了一个图,如下: ![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407.png) - -至于背包九讲其他背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 +除此以外其他类型的背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 而完全背包又是也是01背包稍作变化而来,即:完全背包的物品数量是无限的。 @@ -53,7 +47,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 这样其实是没有从底向上去思考,而是习惯性想到了背包,那么暴力的解法应该是怎么样的呢? -每一件物品其实只有两个状态,取或者不取,所以可以使用回溯法搜索出所有的情况,那么时间复杂度就是$o(2^n)$,这里的n表示物品数量。 +每一件物品其实只有两个状态,取或者不取,所以可以使用回溯法搜索出所有的情况,那么时间复杂度就是O(2^n),这里的n表示物品数量。 **所以暴力的解法是指数级别的时间复杂度。进而才需要动态规划的解法来进行优化!** @@ -73,30 +67,111 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 以下讲解和图示中出现的数字都是以这个例子为例。 + ### 二维dp数组01背包 依然动规五部曲分析一波。 1. 确定dp数组以及下标的含义 -对于背包问题,有一种写法, 是使用二维数组,即**dp[i][j] 表示从下标为[0-i]的物品里任意取,放进容量为j的背包,价值总和最大是多少**。 +我们需要使用二维数组,为什么呢? + +因为有两个维度需要表示,分别是:物品 和 背包容量 -只看这个二维数组的定义,大家一定会有点懵,看下面这个图: +如图,二维数组为 dp[i][j]。 ![动态规划-背包问题1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103003361.png) +那么这里 i 、j、dp[i][j] 分别表示什么呢? + +i 来表示物品、j表示背包容量。 + +(如果想用j 表示物品,j表示背包容量 行不行? 都可以的,个人习惯而已) + +我们来尝试把上面的 二维表格填写一下。 + +动态规划的思路是根据子问题的求解推导出整体的最优解。 + +我们先看把物品0 放入背包的情况: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730113455.png) + +背包容量为0,放不下物品0,此时背包里的价值为0。 + +背包容量为1,可以放下物品0,此时背包里的价值为15. + +背包容量为2,依然可以放下物品0 (注意 01背包里物品只有一个),此时背包里的价值为15。 + +以此类推。 + +再看把物品1 放入背包: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730114228.png) + +背包容量为 0,放不下物品0 或者物品1,此时背包里的价值为0。 + +背包容量为 1,只能放下物品1,背包里的价值为15。 + +背包容量为 2,只能放下物品1,背包里的价值为15。 + +背包容量为 3,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包可以放物品2 或者 物品1,物品2价值更大,背包里的价值为20。 + +背包容量为 4,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包都可都放下,背包价值为35。 + +以上举例,是比较容易看懂,我主要是通过这个例子,来帮助大家明确dp数组的含义。 + +上图中,我们看 dp[1][4] 表示什么意思呢。 + +任取 物品0,物品1 放进容量为4的背包里,最大价值是 dp[1][4]。 + +通过这个举例,我们来进一步明确dp数组的含义。 + +即**dp[i][j] 表示从下标为[0-i]的物品里任意取,放进容量为j的背包,价值总和最大是多少**。 + **要时刻记着这个dp数组的含义,下面的一些步骤都围绕这dp数组的含义进行的**,如果哪里看懵了,就来回顾一下i代表什么,j又代表什么。 2. 确定递推公式 -再回顾一下dp[i][j]的含义:从下标为[0-i]的物品里任意取,放进容量为j的背包,价值总和最大是多少。 +这里在把基本信息给出来: + +| | 重量 | 价值 | +| ----- | ---- | ---- | +| 物品0 | 1 | 15 | +| 物品1 | 3 | 20 | +| 物品2 | 4 | 30 | + +对于递推公式,首先我们要明确有哪些方向可以推导出 dp[i][j]。 + +这里我们dp[1][4]的状态来举例: + +绝对 dp[1][4],就是放物品1 ,还是不放物品1。 + +如果不放物品1, 那么背包的价值应该是 dp[0][4] 即 容量为4的背包,只放物品0的情况。 + +推导方向如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174246.png) -那么可以有两个方向推出来dp[i][j], -* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以背包内的价值依然和前面相同。) +如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 需要重量为3,此时背包剩下容量为1。 + +容量为1,只考虑放物品0 的最大价值是 dp[0][1],这个值我们之前就计算过。 + +所以 放物品1 的情况 = dp[0][1] + 物品1 的重量,推导方向如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174436.png) + +两种情况,分别是放物品1 和 不放物品1,我们要取最大值(毕竟求的是最大价值) + +`dp[1][4] = max(dp[0][4], dp[0][1] + 物品1 的重量) ` + +以上过程,抽象化如下: + +* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。 + * **放物品i**:由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]的时候不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 -所以递归公式: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); +递归公式: `dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);` 3. dp数组如何初始化 @@ -108,13 +183,13 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 在看其他情况。 -状态转移方程 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); 可以看出i 是由 i-1 推导出来,那么i为0的时候就一定要初始化。 +状态转移方程 `dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);` 可以看出i 是由 i-1 推导出来,那么i为0的时候就一定要初始化。 dp[0][j],即:i为0,存放编号0的物品的时候,各个容量的背包所能存放的最大价值。 -那么很明显当 j < weight[0]的时候,dp[0][j] 应该是 0,因为背包容量比编号0的物品重量还小。 +那么很明显当 `j < weight[0]`的时候,dp[0][j] 应该是 0,因为背包容量比编号0的物品重量还小。 -当j >= weight[0]时,dp[0][j] 应该是value[0],因为背包容量放足够放编号0物品。 +当`j >= weight[0]`时,dp[0][j] 应该是value[0],因为背包容量放足够放编号0物品。 代码初始化如下: @@ -147,7 +222,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 最后初始化代码如下: -``` +```CPP // 初始化 dp vector> dp(weight.size(), vector(bagweight + 1, 0)); for (int j = weight[0]; j <= bagweight; j++) { @@ -171,7 +246,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 那么我先给出先遍历物品,然后遍历背包重量的代码。 -``` +```CPP // weight数组的大小 就是物品个数 for(int i = 1; i < weight.size(); i++) { // 遍历物品 for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 @@ -186,7 +261,7 @@ for(int i = 1; i < weight.size(); i++) { // 遍历物品 例如这样: -``` +```CPP // weight数组的大小 就是物品个数 for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 for(int i = 1; i < weight.size(); i++) { // 遍历物品 @@ -200,7 +275,7 @@ for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 **要理解递归的本质和递推的方向**。 -dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); 递归公式中可以看出dp[i][j]是靠dp[i-1][j]和dp[i - 1][j - weight[i]]推导出来的。 +`dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);` 递归公式中可以看出dp[i][j]是靠dp[i-1][j]和dp[i - 1][j - weight[i]]推导出来的。 dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括正上方向),那么先遍历物品,再遍历背包的过程如图所示: @@ -232,50 +307,20 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。 -```cpp -void test_2_wei_bag_problem1() { - vector weight = {1, 3, 4}; - vector value = {15, 20, 30}; - int bagweight = 4; - - // 二维数组 - vector> dp(weight.size(), vector(bagweight + 1, 0)); - - // 初始化 - for (int j = weight[0]; j <= bagweight; j++) { - dp[0][j] = value[0]; - } - - // weight数组的大小 就是物品个数 - for(int i = 1; i < weight.size(); i++) { // 遍历物品 - for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 - if (j < weight[i]) dp[i][j] = dp[i - 1][j]; - else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); - - } - } - - cout << dp[weight.size() - 1][bagweight] << endl; -} - -int main() { - test_2_wei_bag_problem1(); -} - -``` - 本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的,代码如下: ```CPP - -//二维dp数组实现 #include using namespace std; -int n, bagweight;// bagweight代表行李箱空间 -void solve() { +int main() { + int n, bagweight;// bagweight代表行李箱空间 + + cin >> n >> bagweight; + vector weight(n, 0); // 存储每件物品所占空间 vector value(n, 0); // 存储每件物品价值 + for(int i = 0; i < n; ++i) { cin >> weight[i]; } @@ -294,33 +339,28 @@ void solve() { for(int i = 1; i < weight.size(); i++) { // 遍历科研物品 for(int j = 0; j <= bagweight; j++) { // 遍历行李箱容量 - // 如果装不下这个物品,那么就继承dp[i - 1][j]的值 - if (j < weight[i]) dp[i][j] = dp[i - 1][j]; - // 如果能装下,就将值更新为 不装这个物品的最大值 和 装这个物品的最大值 中的 最大值 - // 装这个物品的最大值由容量为j - weight[i]的包任意放入序号为[0, i - 1]的最大值 + 该物品的价值构成 - else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + if (j < weight[i]) dp[i][j] = dp[i - 1][j]; // 如果装不下这个物品,那么就继承dp[i - 1][j]的值 + else { + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + } } } - cout << dp[weight.size() - 1][bagweight] << endl; -} + cout << dp[n - 1][bagweight] << endl; -int main() { - while(cin >> n >> bagweight) { - solve(); - } return 0; } + ``` ## 总结 -讲了这么多才刚刚把二维dp的01背包讲完,**这里大家其实可以发现最简单的是推导公式了,推导公式估计看一遍就记下来了,但难就难在如何初始化和遍历顺序上**。 +背包问题 是动态规划里的经典类型题目,大家要细细品味。 可能有的同学并没有注意到初始化 和 遍历顺序的重要性,我们后面做力扣上背包面试题目的时候,大家就会感受出来了。 -下一篇 还是理论基础,我们再来讲一维dp数组实现的01背包(滚动数组),分析一下和二维有什么区别,在初始化和遍历顺序上又有什么差异,敬请期待! +下一篇 还是理论基础,我们再来讲一维dp数组实现的01背包(滚动数组),分析一下和二维有什么区别,在初始化和遍历顺序上又有什么差异。 @@ -329,120 +369,42 @@ int main() { ### Java -```java -public class BagProblem { +```Java +import java.util.Scanner; + +public class Main { public static void main(String[] args) { - int[] weight = {1,3,4}; - int[] value = {15,20,30}; - int bagSize = 4; - testWeightBagProblem(weight,value,bagSize); - } + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int bagweight = scanner.nextInt(); - /** - * 动态规划获得结果 - * @param weight 物品的重量 - * @param value 物品的价值 - * @param bagSize 背包的容量 - */ - public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){ - - // 创建dp数组 - int goods = weight.length; // 获取物品的数量 - int[][] dp = new int[goods][bagSize + 1]; - - // 初始化dp数组 - // 创建数组后,其中默认的值就是0 - for (int j = weight[0]; j <= bagSize; j++) { - dp[0][j] = value[0]; - } + int[] weight = new int[n]; + int[] value = new int[n]; - // 填充dp数组 - for (int i = 1; i < weight.length; i++) { - for (int j = 1; j <= bagSize; j++) { - if (j < weight[i]) { - /** - * 当前背包的容量都没有当前物品i大的时候,是不放物品i的 - * 那么前i-1个物品能放下的最大价值就是当前情况的最大价值 - */ - dp[i][j] = dp[i-1][j]; - } else { - /** - * 当前背包的容量可以放下物品i - * 那么此时分两种情况: - * 1、不放物品i - * 2、放物品i - * 比较这两种情况下,哪种背包中物品的最大价值最大 - */ - dp[i][j] = Math.max(dp[i-1][j] , dp[i-1][j-weight[i]] + value[i]); - } - } + for (int i = 0; i < n; ++i) { + weight[i] = scanner.nextInt(); } - - // 打印dp数组 - for (int i = 0; i < goods; i++) { - for (int j = 0; j <= bagSize; j++) { - System.out.print(dp[i][j] + "\t"); - } - System.out.println("\n"); + for (int j = 0; j < n; ++j) { + value[j] = scanner.nextInt(); } - } -} - -``` -```java -import java.util.Arrays; + int[][] dp = new int[n][bagweight + 1]; -public class BagProblem { - public static void main(String[] args) { - int[] weight = {1,3,4}; - int[] value = {15,20,30}; - int bagSize = 4; - testWeightBagProblem(weight,value,bagSize); - } + for (int j = weight[0]; j <= bagweight; j++) { + dp[0][j] = value[0]; + } - /** - * 初始化 dp 数组做了简化(给物品增加冗余维)。这样初始化dp数组,默认全为0即可。 - * dp[i][j] 表示从下标为[0 - i-1]的物品里任意取,放进容量为j的背包,价值总和最大是多少。 - * 其实是模仿背包重量从 0 开始,背包容量 j 为 0 的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为 0。 - * 可选物品也可以从无开始,也就是没有物品可选,即dp[0][j],这样无论背包容量为多少,背包价值总和一定为 0。 - * @param weight 物品的重量 - * @param value 物品的价值 - * @param bagSize 背包的容量 - */ - public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){ - - // 创建dp数组 - int goods = weight.length; // 获取物品的数量 - int[][] dp = new int[goods + 1][bagSize + 1]; // 给物品增加冗余维,i = 0 表示没有物品可选 - - // 初始化dp数组,默认全为0即可 - // 填充dp数组 - for (int i = 1; i <= goods; i++) { - for (int j = 1; j <= bagSize; j++) { - if (j < weight[i - 1]) { // i - 1 对应物品 i - /** - * 当前背包的容量都没有当前物品i大的时候,是不放物品i的 - * 那么前i-1个物品能放下的最大价值就是当前情况的最大价值 - */ + for (int i = 1; i < n; i++) { + for (int j = 0; j <= bagweight; j++) { + if (j < weight[i]) { dp[i][j] = dp[i - 1][j]; } else { - /** - * 当前背包的容量可以放下物品i - * 那么此时分两种情况: - * 1、不放物品i - * 2、放物品i - * 比较这两种情况下,哪种背包中物品的最大价值最大 - */ - dp[i][j] = Math.max(dp[i - 1][j] , dp[i - 1][j - weight[i - 1]] + value[i - 1]); // i - 1 对应物品 i + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); } } } - // 打印dp数组 - for(int[] arr : dp){ - System.out.println(Arrays.toString(arr)); - } + System.out.println(dp[n - 1][bagweight]); } } @@ -450,137 +412,71 @@ public class BagProblem { ### Python -无参数版 ```python -def test_2_wei_bag_problem1(): - weight = [1, 3, 4] - value = [15, 20, 30] - bagweight = 4 +n, bagweight = map(int, input().split()) - # 二维数组 - dp = [[0] * (bagweight + 1) for _ in range(len(weight))] +weight = list(map(int, input().split())) +value = list(map(int, input().split())) - # 初始化 - for j in range(weight[0], bagweight + 1): - dp[0][j] = value[0] +dp = [[0] * (bagweight + 1) for _ in range(n)] - # weight数组的大小就是物品个数 - for i in range(1, len(weight)): # 遍历物品 - for j in range(bagweight + 1): # 遍历背包容量 - if j < weight[i]: - dp[i][j] = dp[i - 1][j] - else: - dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]) +for j in range(weight[0], bagweight + 1): + dp[0][j] = value[0] - print(dp[len(weight) - 1][bagweight]) - -test_2_wei_bag_problem1() - -``` -有参数版 -```python -def test_2_wei_bag_problem1(weight, value, bagweight): - # 二维数组 - dp = [[0] * (bagweight + 1) for _ in range(len(weight))] - - # 初始化 - for j in range(weight[0], bagweight + 1): - dp[0][j] = value[0] - - # weight数组的大小就是物品个数 - for i in range(1, len(weight)): # 遍历物品 - for j in range(bagweight + 1): # 遍历背包容量 - if j < weight[i]: - dp[i][j] = dp[i - 1][j] - else: - dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]) - - return dp[len(weight) - 1][bagweight] - -if __name__ == "__main__": - - weight = [1, 3, 4] - value = [15, 20, 30] - bagweight = 4 - - result = test_2_wei_bag_problem1(weight, value, bagweight) - print(result) +for i in range(1, n): + for j in range(bagweight + 1): + if j < weight[i]: + dp[i][j] = dp[i - 1][j] + else: + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]) +print(dp[n - 1][bagweight]) ``` ### Go ```go -func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { - // 定义dp数组 - dp := make([][]int, len(weight)) - for i, _ := range dp { - dp[i] = make([]int, bagweight+1) - } - // 初始化 - for j := bagweight; j >= weight[0]; j-- { - dp[0][j] = dp[0][j-weight[0]] + value[0] - } - // 递推公式 - for i := 1; i < len(weight); i++ { - //正序,也可以倒序 - for j := 0; j <= bagweight; j++ { - if j < weight[i] { - dp[i][j] = dp[i-1][j] - } else { - dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) - } - } - } - return dp[len(weight)-1][bagweight] -} - -func max(a,b int) int { - if a > b { - return a - } - return b -} - -func main() { - weight := []int{1,3,4} - value := []int{15,20,30} - test_2_wei_bag_problem1(weight,value,4) -} ``` ### Javascript ```js -function testWeightBagProblem (weight, value, size) { - // 定义 dp 数组 - const len = weight.length, - dp = Array(len).fill().map(() => Array(size + 1).fill(0)); +const readline = require('readline').createInterface({ + input: process.stdin, + output: process.stdout +}); + +let input = []; - // 初始化 - for(let j = weight[0]; j <= size; j++) { +readline.on('line', (line) => { + input.push(line); +}); + +readline.on('close', () => { + let [n, bagweight] = input[0].split(' ').map(Number); + let weight = input[1].split(' ').map(Number); + let value = input[2].split(' ').map(Number); + + let dp = Array.from({ length: n }, () => Array(bagweight + 1).fill(0)); + + for (let j = weight[0]; j <= bagweight; j++) { dp[0][j] = value[0]; } - // weight 数组的长度len 就是物品个数 - for(let i = 1; i < len; i++) { // 遍历物品 - for(let j = 0; j <= size; j++) { // 遍历背包容量 - if(j < weight[i]) dp[i][j] = dp[i - 1][j]; - else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + for (let i = 1; i < n; i++) { + for (let j = 0; j <= bagweight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + } } } - console.table(dp) + console.log(dp[n - 1][bagweight]); +}); - return dp[len - 1][size]; -} - -function test () { - console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); -} - -test(); ``` @@ -589,157 +485,62 @@ test(); ```c #include #include -#include - -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#define ARR_SIZE(a) (sizeof((a)) / sizeof((a)[0])) -#define BAG_WEIGHT 4 - -void backPack(int* weights, int weightSize, int* costs, int costSize, int bagWeight) { - // 开辟dp数组 - int dp[weightSize][bagWeight + 1]; - memset(dp, 0, sizeof(int) * weightSize * (bagWeight + 1)); - - int i, j; - // 当背包容量大于物品0的重量时,将物品0放入到背包中 - for(j = weights[0]; j <= bagWeight; ++j) { - dp[0][j] = costs[0]; - } - - // 先遍历物品,再遍历重量 - for(j = 1; j <= bagWeight; ++j) { - for(i = 1; i < weightSize; ++i) { - // 如果当前背包容量小于物品重量 - if(j < weights[i]) - // 背包物品的价值等于背包不放置当前物品时的价值 - dp[i][j] = dp[i-1][j]; - // 若背包当前重量可以放置物品 - else - // 背包的价值等于放置该物品或不放置该物品的最大值 - dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - weights[i]] + costs[i]); - } - } - printf("%d\n", dp[weightSize - 1][bagWeight]); +int max(int a, int b) { + return a > b ? a : b; } -int main(int argc, char* argv[]) { - int weights[] = {1, 3, 4}; - int costs[] = {15, 20, 30}; - backPack(weights, ARR_SIZE(weights), costs, ARR_SIZE(costs), BAG_WEIGHT); - return 0; -} -``` +int main() { + int n, bagweight; + scanf("%d %d", &n, &bagweight); + int *weight = (int *)malloc(n * sizeof(int)); + int *value = (int *)malloc(n * sizeof(int)); -### TypeScript - -```typescript -function testWeightBagProblem( - weight: number[], - value: number[], - size: number -): number { - /** - * dp[i][j]: 前i个物品,背包容量为j,能获得的最大价值 - * dp[0][*]: u=weight[0],u之前为0,u之后(含u)为value[0] - * dp[*][0]: 0 - * ... - * dp[i][j]: max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]); - */ - const goodsNum: number = weight.length; - const dp: number[][] = new Array(goodsNum) - .fill(0) - .map((_) => new Array(size + 1).fill(0)); - for (let i = weight[0]; i <= size; i++) { - dp[0][i] = value[0]; - } - for (let i = 1; i < goodsNum; i++) { - for (let j = 1; j <= size; j++) { - if (j < weight[i]) { - dp[i][j] = dp[i - 1][j]; - } else { - dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); - } + for (int i = 0; i < n; ++i) { + scanf("%d", &weight[i]); + } + for (int j = 0; j < n; ++j) { + scanf("%d", &value[j]); } - } - return dp[goodsNum - 1][size]; -} -// test -const weight = [1, 3, 4]; -const value = [15, 20, 30]; -const size = 4; -console.log(testWeightBagProblem(weight, value, size)); -``` - -### Scala - -```scala -object Solution { - // 01背包 - def test_2_wei_bag_problem1(): Unit = { - var weight = Array[Int](1, 3, 4) - var value = Array[Int](15, 20, 30) - var baseweight = 4 - // 二维数组 - var dp = Array.ofDim[Int](weight.length, baseweight + 1) + int **dp = (int **)malloc(n * sizeof(int *)); + for (int i = 0; i < n; ++i) { + dp[i] = (int *)malloc((bagweight + 1) * sizeof(int)); + for (int j = 0; j <= bagweight; ++j) { + dp[i][j] = 0; + } + } - // 初始化 - for (j <- weight(0) to baseweight) { - dp(0)(j) = value(0) + for (int j = weight[0]; j <= bagweight; j++) { + dp[0][j] = value[0]; } - // 遍历 - for (i <- 1 until weight.length; j <- 1 to baseweight) { - if (j - weight(i) >= 0) dp(i)(j) = dp(i - 1)(j - weight(i)) + value(i) - dp(i)(j) = math.max(dp(i)(j), dp(i - 1)(j)) + for (int i = 1; i < n; i++) { + for (int j = 0; j <= bagweight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + } + } } - // 打印数组 - dp.foreach(x => println("[" + x.mkString(",") + "]")) + printf("%d\n", dp[n - 1][bagweight]); - dp(weight.length - 1)(baseweight) // 最终返回 - } + for (int i = 0; i < n; ++i) { + free(dp[i]); + } + free(dp); + free(weight); + free(value); - def main(args: Array[String]): Unit = { - test_2_wei_bag_problem1() - } + return 0; } -``` - -### Rust - -```rust -pub struct Solution; -impl Solution { - pub fn wei_bag_problem1(weight: Vec, value: Vec, bag_size: usize) -> usize { - let mut dp = vec![vec![0; bag_size + 1]; weight.len()]; - for j in weight[0]..=weight.len() { - dp[0][j] = value[0]; - } +``` - for i in 1..weight.len() { - for j in 0..=bag_size { - match j < weight[i] { - true => dp[i][j] = dp[i - 1][j], - false => dp[i][j] = dp[i - 1][j].max(dp[i - 1][j - weight[i]] + value[i]), - } - } - } - dp[weight.len() - 1][bag_size] - } -} -#[test] -fn test_wei_bag_problem1() { - println!( - "{}", - Solution::wei_bag_problem1(vec![1, 3, 4], vec![15, 20, 30], 4) - ); -} -```

diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index b5838c5df8..c61a72cc40 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -56,17 +56,31 @@ 1. 确定dp数组的定义 +关于dp数组的定义,我在 [01背包理论基础](https://programmercarl.com/背包理论基础01背包-1.html) 有详细讲解 + 在一维dp数组中,dp[j]表示:容量为j的背包,所背的物品价值可以最大为dp[j]。 2. 一维dp数组的递推公式 -dp[j]为 容量为j的背包所背的最大价值,那么如何推导dp[j]呢? +二维dp数组的递推公式为: `dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);` + +公式是怎么来的 在这里 [01背包理论基础](https://programmercarl.com/背包理论基础01背包-1.html) 有详细讲解。 + +一维dp数组,其实就上上一层 dp[i-1] 这一层 拷贝的 dp[i]来。 + +所以在 上面递推公式的基础上,去掉i这个维度就好。 + +递推公式为:`dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);` + +以下为分析: + +dp[j]为 容量为j的背包所背的最大价值。 dp[j]可以通过dp[j - weight[i]]推导出来,dp[j - weight[i]]表示容量为j - weight[i]的背包所背的最大价值。 -dp[j - weight[i]] + value[i] 表示 容量为 j - 物品i重量 的背包 加上 物品i的价值。(也就是容量为j的背包,放入物品i了之后的价值即:dp[j]) +`dp[j - weight[i]] + value[i]` 表示 容量为 [j - 物品i重量] 的背包 加上 物品i的价值。(也就是容量为j的背包,放入物品i了之后的价值即:dp[j]) -此时dp[j]有两个选择,一个是取自己dp[j] 相当于 二维dp数组中的dp[i-1][j],即不放物品i,一个是取dp[j - weight[i]] + value[i],即放物品i,指定是取最大的,毕竟是求最大价值, +此时dp[j]有两个选择,一个是取自己dp[j] 相当于 二维dp数组中的dp[i-1][j],即不放物品i,一个是取`dp[j - weight[i]] + value[i]`,即放物品i,指定是取最大的,毕竟是求最大价值, 所以递归公式为: @@ -145,10 +159,6 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 因为一维dp的写法,背包容量一定是要倒序遍历(原因上面已经讲了),如果遍历背包容量放在上一层,那么每个dp[j]就只会放入一个物品,即:背包里只放入了一个物品。 -倒序遍历的原因是,本质上还是一个对二维数组的遍历,并且右下角的值依赖上一层左上角的值,因此需要保证左边的值仍然是上一层的,从右向左覆盖。 - -(这里如果读不懂,就再回想一下dp[j]的定义,或者就把两个for循环顺序颠倒一下试试!) - **所以一维dp数组的背包在遍历顺序上和二维其实是有很大差异的!**,这一点大家一定要注意。 5. 举例推导dp数组 @@ -158,31 +168,6 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 ![动态规划-背包问题9](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103614769.png) - -C++代码如下: - -```CPP -void test_1_wei_bag_problem() { - vector weight = {1, 3, 4}; - vector value = {15, 20, 30}; - int bagWeight = 4; - - // 初始化 - vector dp(bagWeight + 1, 0); - for(int i = 0; i < weight.size(); i++) { // 遍历物品 - for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); - } - } - cout << dp[bagWeight] << endl; -} - -int main() { - test_1_wei_bag_problem(); -} - -``` - 本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的,代码如下: ```CPP @@ -256,251 +241,229 @@ int main() { 即使代码没有通过,也会有自己的逻辑去debug,这样就思维清晰了。 -接下来就要开始用这两天的理论基础去做力扣上的背包面试题目了,录友们握紧扶手,我们要上高速啦! - - - ## 其他语言版本 ### Java ```java +import java.util.Scanner; + +public class Main { public static void main(String[] args) { - int[] weight = {1, 3, 4}; - int[] value = {15, 20, 30}; - int bagWight = 4; - testWeightBagProblem(weight, value, bagWight); - } + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int bagweight = scanner.nextInt(); - public static void testWeightBagProblem(int[] weight, int[] value, int bagWeight){ - int wLen = weight.length; - //定义dp数组:dp[j]表示背包容量为j时,能获得的最大价值 - int[] dp = new int[bagWeight + 1]; - //遍历顺序:先遍历物品,再遍历背包容量 - for (int i = 0; i < wLen; i++){ - for (int j = bagWeight; j >= weight[i]; j--){ - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); - } + int[] weight = new int[n]; + int[] value = new int[n]; + + for (int i = 0; i < n; ++i) { + weight[i] = scanner.nextInt(); } - //打印dp数组 - for (int j = 0; j <= bagWeight; j++){ - System.out.print(dp[j] + " "); + for (int j = 0; j < n; ++j) { + value[j] = scanner.nextInt(); } - } -``` + int[][] dp = new int[n][bagweight + 1]; + for (int j = weight[0]; j <= bagweight; j++) { + dp[0][j] = value[0]; + } + for (int i = 1; i < n; i++) { + for (int j = 0; j <= bagweight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + } + } + } -### Python -无参版 -```python -def test_1_wei_bag_problem(): - weight = [1, 3, 4] - value = [15, 20, 30] - bagWeight = 4 + System.out.println(dp[n - 1][bagweight]); + } +} - # 初始化 - dp = [0] * (bagWeight + 1) - for i in range(len(weight)): # 遍历物品 - for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) +``` - print(dp[bagWeight]) -test_1_wei_bag_problem() -``` -有参版 + +### Python + ```python -def test_1_wei_bag_problem(weight, value, bagWeight): - # 初始化 - dp = [0] * (bagWeight + 1) - for i in range(len(weight)): # 遍历物品 - for j in range(bagWeight, weight[i] - 1, -1): # 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) +n, bagweight = map(int, input().split()) - return dp[bagWeight] +weight = list(map(int, input().split())) +value = list(map(int, input().split())) +dp = [[0] * (bagweight + 1) for _ in range(n)] -if __name__ == "__main__": +for j in range(weight[0], bagweight + 1): + dp[0][j] = value[0] - weight = [1, 3, 4] - value = [15, 20, 30] - bagweight = 4 +for i in range(1, n): + for j in range(bagweight + 1): + if j < weight[i]: + dp[i][j] = dp[i - 1][j] + else: + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]) - result = test_1_wei_bag_problem(weight, value, bagweight) - print(result) +print(dp[n - 1][bagweight]) ``` ### Go ```go -func test_1_wei_bag_problem(weight, value []int, bagWeight int) int { - // 定义 and 初始化 - dp := make([]int,bagWeight+1) - // 递推顺序 - for i := 0 ;i < len(weight) ; i++ { - // 这里必须倒序,区别二维,因为二维dp保存了i的状态 - for j:= bagWeight; j >= weight[i] ; j-- { - // 递推公式 - dp[j] = max(dp[j], dp[j-weight[i]]+value[i]) - } - } - //fmt.Println(dp) - return dp[bagWeight] -} - -func max(a,b int) int { - if a > b { - return a - } - return b -} +package main +import ( + "fmt" +) func main() { - weight := []int{1,3,4} - value := []int{15,20,30} - test_1_wei_bag_problem(weight,value,4) -} -``` + var n, bagweight int + fmt.Scan(&n, &bagweight) -### JavaScript + weight := make([]int, n) + value := make([]int, n) -```js - -function testWeightBagProblem(wight, value, size) { - const len = wight.length, - dp = Array(size + 1).fill(0); - for(let i = 1; i <= len; i++) { - for(let j = size; j >= wight[i - 1]; j--) { - dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]); + for i := 0; i < n; i++ { + fmt.Scan(&weight[i]) + } + for j := 0; j < n; j++ { + fmt.Scan(&value[j]) } - } - return dp[size]; -} - -function test () { - console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); -} + dp := make([][]int, n) + for i := range dp { + dp[i] = make([]int, bagweight+1) + } -test(); -``` + for j := weight[0]; j <= bagweight; j++ { + dp[0][j] = value[0] + } -### C -```c -#include -#include - -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#define ARR_SIZE(arr) ((sizeof((arr))) / sizeof((arr)[0])) -#define BAG_WEIGHT 4 - -void test_back_pack(int* weights, int weightSize, int* values, int valueSize, int bagWeight) { - int dp[bagWeight + 1]; - memset(dp, 0, sizeof(int) * (bagWeight + 1)); - - int i, j; - // 先遍历物品 - for(i = 0; i < weightSize; ++i) { - // 后遍历重量。从后向前遍历 - for(j = bagWeight; j >= weights[i]; --j) { - dp[j] = MAX(dp[j], dp[j - weights[i]] + values[i]); + for i := 1; i < n; i++ { + for j := 0; j <= bagweight; j++ { + if j < weight[i] { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) + } } } - // 打印最优结果 - printf("%d\n", dp[bagWeight]); + fmt.Println(dp[n-1][bagweight]) } -int main(int argc, char** argv) { - int weights[] = {1, 3, 4}; - int values[] = {15, 20, 30}; - test_back_pack(weights, ARR_SIZE(weights), values, ARR_SIZE(values), BAG_WEIGHT); - return 0; -} -``` - -### TypeScript - -```typescript -function testWeightBagProblem( - weight: number[], - value: number[], - size: number -): number { - const goodsNum: number = weight.length; - const dp: number[] = new Array(size + 1).fill(0); - for (let i = 0; i < goodsNum; i++) { - for (let j = size; j >= weight[i]; j--) { - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); +func max(a, b int) int { + if a > b { + return a } - } - return dp[size]; + return b } -const weight = [1, 3, 4]; -const value = [15, 20, 30]; -const size = 4; -console.log(testWeightBagProblem(weight, value, size)); ``` -### Scala +### JavaScript + +```js +const readline = require('readline').createInterface({ + input: process.stdin, + output: process.stdout +}); + +let input = []; + +readline.on('line', (line) => { + input.push(line); +}); -```scala -object Solution { - // 滚动数组 - def test_1_wei_bag_problem(): Unit = { - var weight = Array[Int](1, 3, 4) - var value = Array[Int](15, 20, 30) - var baseweight = 4 +readline.on('close', () => { + let [n, bagweight] = input[0].split(' ').map(Number); + let weight = input[1].split(' ').map(Number); + let value = input[2].split(' ').map(Number); - // dp数组 - var dp = new Array[Int](baseweight + 1) + let dp = Array.from({ length: n }, () => Array(bagweight + 1).fill(0)); - // 遍历 - for (i <- 0 until weight.length; j <- baseweight to weight(i) by -1) { - dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i)) + for (let j = weight[0]; j <= bagweight; j++) { + dp[0][j] = value[0]; } - // 打印数组 - println("[" + dp.mkString(",") + "]") - } + for (let i = 1; i < n; i++) { + for (let j = 0; j <= bagweight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); + } + } + } + + console.log(dp[n - 1][bagweight]); +}); + - def main(args: Array[String]): Unit = { - test_1_wei_bag_problem() - } -} ``` -### Rust +### C +```c +#include +#include + +int max(int a, int b) { + return a > b ? a : b; +} -```rust -pub struct Solution; +int main() { + int n, bagweight; + scanf("%d %d", &n, &bagweight); -impl Solution { - pub fn wei_bag_problem2(weight: Vec, value: Vec, bag_size: usize) -> usize { - let mut dp = vec![0; bag_size + 1]; - for i in 0..weight.len() { - for j in (weight[i]..=bag_size).rev() { - if j >= weight[i] { - dp[j] = dp[j].max(dp[j - weight[i]] + value[i]); - } + int *weight = (int *)malloc(n * sizeof(int)); + int *value = (int *)malloc(n * sizeof(int)); + + for (int i = 0; i < n; ++i) { + scanf("%d", &weight[i]); + } + for (int j = 0; j < n; ++j) { + scanf("%d", &value[j]); + } + + int **dp = (int **)malloc(n * sizeof(int *)); + for (int i = 0; i < n; ++i) { + dp[i] = (int *)malloc((bagweight + 1) * sizeof(int)); + for (int j = 0; j <= bagweight; ++j) { + dp[i][j] = 0; + } + } + + for (int j = weight[0]; j <= bagweight; j++) { + dp[0][j] = value[0]; + } + + for (int i = 1; i < n; i++) { + for (int j = 0; j <= bagweight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); } } - dp[dp.len() - 1] } -} -#[test] -fn test_wei_bag_problem2() { - println!( - "{}", - Solution::wei_bag_problem2(vec![1, 3, 4], vec![15, 20, 30], 4) - ); + printf("%d\n", dp[n - 1][bagweight]); + + for (int i = 0; i < n; ++i) { + free(dp[i]); + } + free(dp); + free(weight); + free(value); + + return 0; } + ``` From 0d7f8b03150a2114946192ef181fa8e00d5c5bf8 Mon Sep 17 00:00:00 2001 From: kyshen Date: Sat, 3 Aug 2024 23:52:26 +0800 Subject: [PATCH 1226/1533] feat: python solution, DFS --- ...64\346\265\201\351\227\256\351\242\230.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index b19e4778f1..9a34bf094f 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -355,6 +355,62 @@ public class Main { ``` ### Python +```Python +first = set() +second = set() +directions = [[-1, 0], [0, 1], [1, 0], [0, -1]] + +def dfs(i, j, graph, visited, side): + if visited[i][j]: + return + + visited[i][j] = True + side.add((i, j)) + + for x, y in directions: + new_x = i + x + new_y = j + y + if ( + 0 <= new_x < len(graph) + and 0 <= new_y < len(graph[0]) + and int(graph[new_x][new_y]) >= int(graph[i][j]) + ): + dfs(new_x, new_y, graph, visited, side) + +def main(): + global first + global second + + N, M = map(int, input().strip().split()) + graph = [] + for _ in range(N): + row = input().strip().split() + graph.append(row) + + # 是否可到达第一边界 + visited = [[False] * M for _ in range(N)] + for i in range(M): + dfs(0, i, graph, visited, first) + for i in range(N): + dfs(i, 0, graph, visited, first) + + # 是否可到达第二边界 + visited = [[False] * M for _ in range(N)] + for i in range(M): + dfs(N - 1, i, graph, visited, second) + for i in range(N): + dfs(i, M - 1, graph, visited, second) + + # 可到达第一边界和第二边界 + res = first & second + + for x, y in res: + print(f"{x} {y}") + + +if __name__ == "__main__": + main() +``` ### Go From ba773d93bedd6bdfd7841d5b0aa13244ace303eb Mon Sep 17 00:00:00 2001 From: YimvPi Date: Sat, 3 Aug 2024 14:45:16 -0400 Subject: [PATCH 1227/1533] Change 'len' to 'length' --- ...\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index df586ff99f..f8092503e3 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -308,7 +308,7 @@ class Solution: class Solution: def maxProfit(self, prices: List[int]) -> int: length = len(prices) - if len == 0: + if length == 0: return 0 dp = [[0] * 2 for _ in range(length)] dp[0][0] = -prices[0] From 5fc9aa68a3f483b41ddae1fe735cfe21a676d7dd Mon Sep 17 00:00:00 2001 From: Hard <2321291138@qq.com> Date: Mon, 5 Aug 2024 17:24:26 +0800 Subject: [PATCH 1228/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00116.=E5=A1=AB?= =?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?= =?UTF-8?q?=E9=92=88.md=20Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\347\202\271\346\214\207\351\222\210.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" index ca36ac6f3e..98bd4e41c1 100644 --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -169,6 +169,56 @@ class Solution { } ``` +```Java +// 迭代法 +class Solution { + public Node connect(Node root) { + if (root == null) { + return root; + } + + Queue queue = new LinkedList<>(); + + queue.add(root); + + while (!queue.isEmpty()) { + int size = queue.size(); + + // 每层的第一个节点 + Node cur = queue.poll(); + if (cur.left != null) { + queue.add(cur.left); + } + if (cur.right != null) { + queue.add(cur.right); + } + + // 因为已经移除了每层的第一个节点,所以将 0 改为 1 + while (size-- > 1) { + Node next = queue.poll(); + + if (next.left != null) { + queue.add(next.left); + } + if (next.right != null) { + queue.add(next.right); + } + + // 当前节点指向同层的下一个节点 + cur.next = next; + // 更新当前节点 + cur = next; + } + + // 每层的最后一个节点不指向 null 在力扣也能过 + cur.next = null; + } + + return root; + } +} +``` + ### Python ```python @@ -443,3 +493,4 @@ public class Solution + From a91e01c8a4479f9a2871d895ccc5c0af283d8a9c Mon Sep 17 00:00:00 2001 From: Hard <2321291138@qq.com> Date: Mon, 5 Aug 2024 17:25:35 +0800 Subject: [PATCH 1229/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90226.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91.md=20=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 824968f0f3..e501b29878 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -14,7 +14,7 @@ ![226.翻转二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192644329.png) -这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,权当一个乐子哈) +这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,全当一个乐子哈) ## 算法公开课 @@ -1033,3 +1033,4 @@ public TreeNode InvertTree(TreeNode root) { + From 2272d0e1e303f93d6a61e33674c799d848817824 Mon Sep 17 00:00:00 2001 From: Yang-Changhui <2275029710@qq.com> Date: Mon, 5 Aug 2024 23:05:41 +0800 Subject: [PATCH 1230/1533] =?UTF-8?q?add=20problems/kamacoder/0110.?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=8E=A5=E9=BE=99.md=20python?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\344\270\262\346\216\245\351\276\231.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index feeec6ddcc..5d13e3689e 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -217,6 +217,38 @@ public class Main { ``` ### Python +```Python +def judge(s1,s2): + count=0 + for i in range(len(s1)): + if s1[i]!=s2[i]: + count+=1 + return count==1 + +if __name__=='__main__': + n=int(input()) + beginstr,endstr=map(str,input().split()) + if beginstr==endstr: + print(0) + exit() + strlist=[] + for i in range(n): + strlist.append(input()) + + # use bfs + visit=[False for i in range(n)] + queue=[[beginstr,1]] + while queue: + str,step=queue.pop(0) + if judge(str,endstr): + print(step+1) + exit() + for i in range(n): + if visit[i]==False and judge(strlist[i],str): + visit[i]=True + queue.append([strlist[i],step+1]) + print(0) +``` ### Go From a13de64145b21f31b42e6162623b25fe7103f863 Mon Sep 17 00:00:00 2001 From: kyshen Date: Mon, 5 Aug 2024 23:21:33 +0800 Subject: [PATCH 1231/1533] feat: 0104 python DFS solution --- ...00\345\244\247\345\262\233\345\261\277.md" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 7d67b7fcb1..7132217b53 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -366,6 +366,79 @@ public class Main { ### Python +```Python +import collections + +directions = [[-1, 0], [0, 1], [0, -1], [1, 0]] +area = 0 + +def dfs(i, j, grid, visited, num): + global area + + if visited[i][j]: + return + + visited[i][j] = True + grid[i][j] = num # 标记岛屿号码 + area += 1 + + for x, y in directions: + new_x = i + x + new_y = j + y + if ( + 0 <= new_x < len(grid) + and 0 <= new_y < len(grid[0]) + and grid[new_x][new_y] == "1" + ): + dfs(new_x, new_y, grid, visited, num) + + +def main(): + global area + + N, M = map(int, input().strip().split()) + grid = [] + for i in range(N): + grid.append(input().strip().split()) + visited = [[False] * M for _ in range(N)] + rec = collections.defaultdict(int) + + cnt = 2 + for i in range(N): + for j in range(M): + if grid[i][j] == "1": + area = 0 + dfs(i, j, grid, visited, cnt) + rec[cnt] = area # 纪录岛屿面积 + cnt += 1 + + res = 0 + for i in range(N): + for j in range(M): + if grid[i][j] == "0": + max_island = 1 # 将水变为陆地,故从1开始计数 + v = set() + for x, y in directions: + new_x = i + x + new_y = j + y + if ( + 0 <= new_x < len(grid) + and 0 <= new_y < len(grid[0]) + and grid[new_x][new_y] != "0" + and grid[new_x][new_y] not in v # 岛屿不可重复 + ): + max_island += rec[grid[new_x][new_y]] + v.add(grid[new_x][new_y]) + res = max(res, max_island) + + if res == 0: + return max(rec.values()) # 无水的情况 + return res + +if __name__ == "__main__": + print(main()) +``` + ### Go ### Rust From d38135b219e667f1638b9184b015043a87ed002a Mon Sep 17 00:00:00 2001 From: fang_chenfang <1217690899@qq.com> Date: Tue, 6 Aug 2024 10:36:18 +0800 Subject: [PATCH 1232/1533] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Java版本第二个for循环内的剪枝操作直接return会漏解,应该是break --- .../0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 8e34713d37..f45dd939af 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -264,7 +264,7 @@ class Solution { // nums[i]+nums[j] > target 直接返回, 剪枝操作 if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) { - return result; + break; } if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重 From d5353e1735d2d80a28a7a653d97dade4bf7c66e3 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 6 Aug 2024 11:07:14 +0800 Subject: [PATCH 1233/1533] =?UTF-8?q?62.=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=A2=9E=E5=8A=A0Go=E6=95=B0=E8=AE=BA=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\220\214\350\267\257\345\276\204.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index b451704b4c..5b46caa9e3 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -371,6 +371,7 @@ class Solution: ``` ### Go +动态规划 ```Go func uniquePaths(m int, n int) int { dp := make([][]int, m) @@ -390,6 +391,26 @@ func uniquePaths(m int, n int) int { } ``` +数论方法 +```Go +func uniquePaths(m int, n int) int { + numerator := 1 + denominator := m - 1 + count := m - 1 + t := m + n - 2 + for count > 0 { + numerator *= t + t-- + for denominator != 0 && numerator % denominator == 0 { + numerator /= denominator + denominator-- + } + count-- + } + return numerator +} +``` + ### Javascript ```Javascript From 268c5b2adc9adad9aa43b3517278722beeb24859 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 6 Aug 2024 11:26:17 +0800 Subject: [PATCH 1234/1533] Update --- ...6.\345\205\250\346\216\222\345\210\227.md" | 7 +- ...4.\347\233\256\346\240\207\345\222\214.md" | 71 ++++++++-- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 2 +- ...14\347\232\204\347\247\230\345\257\206.md" | 127 ++++++++++++++++++ ...32\344\277\241\346\240\241\345\207\206.md" | 121 +++++++++++++++++ 5 files changed, 313 insertions(+), 15 deletions(-) create mode 100644 "problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" create mode 100644 "problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index 638a2a7c45..1ef80a142d 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -42,7 +42,8 @@ 我以[1,2,3]为例,抽象成树形结构如下: -![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20211027181706.png) + +![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) ### 回溯三部曲 @@ -54,7 +55,7 @@ 但排列问题需要一个used数组,标记已经选择的元素,如图橘黄色部分所示: -![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20211027181706.png) +![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) 代码如下: @@ -66,7 +67,7 @@ void backtracking (vector& nums, vector& used) * 递归终止条件 -![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209174225145.png) +![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) 可以看出叶子节点,就是收割结果的地方。 diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 92616ed188..9d13067e5d 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -134,17 +134,17 @@ x = (target + sum) / 2 大家看到(target + sum) / 2 应该担心计算的过程中向下取整有没有影响。 -这么担心就对了,例如sum 是5,S是2的话其实就是无解的,所以: +这么担心就对了,例如sum是5,target是2 的话其实就是无解的,所以: ```CPP (C++代码中,输入的S 就是题目描述的 target) -if ((S + sum) % 2 == 1) return 0; // 此时没有方案 +if ((target + sum) % 2 == 1) return 0; // 此时没有方案 ``` -同时如果 S的绝对值已经大于sum,那么也是没有方案的。 +同时如果target 的绝对值已经大于sum,那么也是没有方案的。 + ```CPP -(C++代码中,输入的S 就是题目描述的 target) -if (abs(S) > sum) return 0; // 此时没有方案 +if (abs(target) > sum) return 0; // 此时没有方案 ``` 再回归到01背包问题,为什么是01背包呢? @@ -213,9 +213,9 @@ dp[j]其他下标对应的数值也应该初始化为0,从递推公式也可 5. 举例推导dp数组 -输入:nums: [1, 1, 1, 1, 1], S: 3 +输入:nums: [1, 1, 1, 1, 1], target: 3 -bagSize = (S + sum) / 2 = (3 + 5) / 2 = 4 +bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: @@ -226,12 +226,12 @@ C++代码如下: ```CPP class Solution { public: - int findTargetSumWays(vector& nums, int S) { + int findTargetSumWays(vector& nums, int target) { int sum = 0; for (int i = 0; i < nums.size(); i++) sum += nums[i]; - if (abs(S) > sum) return 0; // 此时没有方案 - if ((S + sum) % 2 == 1) return 0; // 此时没有方案 - int bagSize = (S + sum) / 2; + if (abs(target) > sum) return 0; // 此时没有方案 + if ((target + sum) % 2 == 1) return 0; // 此时没有方案 + int bagSize = (target + sum) / 2; vector dp(bagSize + 1, 0); dp[0] = 1; for (int i = 0; i < nums.size(); i++) { @@ -655,3 +655,52 @@ public class Solution + + +class Solution { +public: + int findTargetSumWays(vector& nums, int target) { + int sum = 0; + for (int i = 0; i < nums.size(); i++) sum += nums[i]; + if (abs(target) > sum) return 0; // 此时没有方案 + if ((target + sum) % 2 == 1) return 0; // 此时没有方案 + int bagSize = (target + sum) / 2; + + vector> dp(nums.size(), vector(bagSize + 1, 0)); + + if (nums[0] <= bagSize) dp[0][nums[0]] = 1; + + dp[0][0] = 1; + + int numZero = 0; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] == 0) numZero++; + dp[i][0] = (int) pow(2.0, numZero); + } + + for (int i = 1; i < nums.size(); i++) { + for (int j = 0; j <= bagSize; j++) { + if (nums[i] > j) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; + } + } + for (int i = 0; i < nums.size(); i++) { + for (int j = 0; j <= bagSize; j++) { + cout << dp[i][j] << " "; + } + cout << endl; + } + return dp[nums.size() - 1][bagSize]; + } +}; + +1 1 0 0 0 +1 2 1 0 0 +1 3 3 1 0 +1 4 6 4 1 +1 5 10 10 5 + +初始化 如果没有0, dp[i][0] = 1; 即所有元素都不取。 + +用元素 取与不取来举例 + diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 5fe06d349d..3aa5365561 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -64,7 +64,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240328104119.png) -本图中,对所有边进行松弛,真正有效的松弛,只有松弛 边(节点1->节点2) 和 边(节点1->节点5) 。 +本图中,对所有边进行松弛,真正有效的松弛,只有松弛 边(节点1->节点2) 和 边(节点1->节点3) 。 而松弛 边(节点4->节点6) ,边(节点5->节点3)等等 都是无效的操作,因为 节点4 和 节点 5 都是没有被计算过的节点。 diff --git "a/problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" "b/problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" new file mode 100644 index 0000000000..3859f7b039 --- /dev/null +++ "b/problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" @@ -0,0 +1,127 @@ +# 151. 手机流畅运行的秘密 + +[题目链接](https://kamacoder.com/problempage.php?pid=1229) + +先运行 能留下电量多的 任务,才能有余电运行其他任务。 + +任务1,1:10 ,运行完 能留下 9个电 + +任务2,2:12,运行完 能留下 10个电 + +任务3,3:10,运行完 能留下 7个电。 + +运行顺序: 任务2 -> 任务1 -> 任务3 + +按照 最低初始电量 - 耗电量,从大到小排序。 + +计算总电量,需要 从小到大 遍历, 不断取 总电量 + 任务耗电量 与 任务最低初始电量 的最大值。 + +```CPP +#include +using namespace std; + +bool cmp(const pair& taskA, const pair& taskB) { + return (taskA.second - taskA.first) < (taskB.second - taskB.first); +} +int main() { + string str, tmp; + vector> tasks; + + //处理输入 + getline(cin, str); + stringstream ss(str); + while (getline(ss, tmp, ',')) { + int p = tmp.find(":"); + string a = tmp.substr(0, p); + string b = tmp.substr(p + 1); + tasks.push_back({stoi(a), stoi(b)}); + } + + // 按照差值从小到大排序 + sort(tasks.begin(), tasks.end(), cmp); + + // 收集结果 + int result = 0; + for (int i = 0 ; i < tasks.size(); i++) { + result = max(result + tasks[i].first, tasks[i].second); + } + + result = result <= 4800 ? result : -1; + cout << result << endl; + +} +``` + +Java版本: + +```Java +import java.util.*; +import java.util.stream.Collectors; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String str = sc.nextLine(); + String[] tasksArray = str.split(","); + List tasks = Arrays.stream(tasksArray) + .map(task -> { + String[] parts = task.split(":"); + return new Pair(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); + }) + .collect(Collectors.toList()); + + // 按照差值从小到大排序 + Collections.sort(tasks, (taskA, taskB) -> + (taskA.second - taskA.first) - (taskB.second - taskB.first) + ); + + // 收集结果 + int result = 0; + for (Pair task : tasks) { + result = Math.max(result + task.first, task.second); + } + + result = result <= 4800 ? result : -1; + System.out.println(result); + } +} + +class Pair { + int first; + int second; + + Pair(int first, int second) { + this.first = first; + this.second = second; + } +} + +``` + +Python版本: + +```python +def main(): + import sys + input = sys.stdin.read + + str = input().strip() + tasks = [] + for tmp in str.split(','): + a, b = map(int, tmp.split(':')) + tasks.append((a, b)) + + # 按照差值从小到大排序 + tasks.sort(key=lambda task: task[1] - task[0]) + + # 收集结果 + result = 0 + for task in tasks: + result = max(result + task[0], task[1]) + + result = result if result <= 4800 else -1 + print(result) + +if __name__ == "__main__": + main() +``` diff --git "a/problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" "b/problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" new file mode 100644 index 0000000000..afb5d8eaf4 --- /dev/null +++ "b/problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" @@ -0,0 +1,121 @@ + + +# 152. 小米手机通信校准 + +[题目链接](https://kamacoder.com/problempage.php?pid=1230) + +一道模拟题,但比较考察 代码能力。 + +遍历去找 里 freq 最近的 freg就好, 需要记录刚遍历过的的freg和 loss,因为可能有 相邻一样的 freg。 + +```CPP +#include +using namespace std; + +int main() { + int freq; + cin >> freq; + string data; + double result = 0; + int last_freg = 0; // 记录上一个 freg + int last_loss = 0; // 记录上一个loss + while(cin >> data) { + int index = data.find(':'); + int freg = stoi(data.substr(0, index)); // 获取 freg 和 loss + int loss = stoi(data.substr(index + 1)); + // 两遍一样 + if(abs(freg - freq) == abs(last_freg - freq)) { + result = (double)(last_loss + loss)/2.0; + } // 否则更新最新的result + else if(abs(freg - freq) < abs(last_freg - freq)){ + result = (double)loss; + } + last_freg = freg; + last_loss = loss; + } + printf("%.1lf\n", result); + return 0; +} + +``` + +Java 版本: + +```Java + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int freq = sc.nextInt(); + sc.nextLine(); // 读取换行符 + + String inputLine = sc.nextLine(); // 读取包含所有后续输入的行 + String[] data = inputLine.split(" "); // 根据空格分割输入 + + double result = 0; + int lastFreq = 0; // 记录上一个 freg + int lastLoss = 0; // 记录上一个 loss + + for (String entry : data) { + int index = entry.indexOf(':'); + int freg = Integer.parseInt(entry.substring(0, index)); // 获取 freg 和 loss + int loss = Integer.parseInt(entry.substring(index + 1)); + + // 两遍一样 + if (Math.abs(freg - freq) == Math.abs(lastFreq - freq)) { + result = (double) (lastLoss + loss) / 2.0; + } + // 否则更新最新的 result + else if (Math.abs(freg - freq) < Math.abs(lastFreq - freq)) { + result = (double) loss; + } + + lastFreq = freg; + lastLoss = loss; + } + + System.out.printf("%.1f\n", result); + sc.close(); + } +} + +``` + +Python版本: + +```python +def main(): + import sys + input = sys.stdin.read + data = input().split() + + freq = int(data[0]) + result = 0 + last_freg = 0 # 记录上一个 freg + last_loss = 0 # 记录上一个 loss + + for i in range(1, len(data)): + item = data[i] + index = item.find(':') + freg = int(item[:index]) # 获取 freg 和 loss + loss = int(item[index + 1:]) + + # 两遍一样 + if abs(freg - freq) == abs(last_freg - freq): + result = (last_loss + loss) / 2.0 + # 否则更新最新的 result + elif abs(freg - freq) < abs(last_freg - freq): + result = loss + + last_freg = freg + last_loss = loss + + print(f"{result:.1f}") + +if __name__ == "__main__": + main() + + +``` From 0d6080ba5cd0d9a3548851758cf2858e5aa999da Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 6 Aug 2024 16:21:23 +0800 Subject: [PATCH 1235/1533] =?UTF-8?q?=20=E6=9B=B4=E6=96=B0spfa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 3aa5365561..07317d6b04 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -162,7 +162,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240729161116.png) -所以我们在加入队列的过程可以有一个优化,用visited数组记录已经加入队列的元素,已经在队列的元素不用重复加入 +所以我们在加入队列的过程可以有一个优化,**用visited数组记录已经在队列里的元素,已经在队列的元素不用重复加入** -------------- @@ -233,7 +233,7 @@ int main() { while (!que.empty()) { int node = que.front(); que.pop(); - isInQueue[node] = false; // 从队列里取出的时候,要取消标记 + isInQueue[node] = false; // 从队列里取出的时候,要取消标记,我们只保证已经在队列里的元素不用重复加入 for (Edge edge : grid[node]) { int from = node; int to = edge.to; From 0cdfc3fef87a285f3ddc0e9ddce491c1f38a69de Mon Sep 17 00:00:00 2001 From: ma <501847822@qq.com> Date: Wed, 7 Aug 2024 10:46:56 +0800 Subject: [PATCH 1236/1533] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md=20=E6=9B=B4=E6=96=B0=20Java=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...233\233\346\225\260\344\271\213\345\222\214.md" | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 8e34713d37..961de9ba74 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -251,8 +251,8 @@ class Solution { for (int i = 0; i < nums.length; i++) { - // nums[i] > target 直接返回, 剪枝操作 - if (nums[i] > 0 && nums[i] > target) { + // nums[i] >= 0 && nums[i] > target 直接返回, 剪枝操作 + if (nums[i] >= 0 && nums[i] > target) { return result; } @@ -262,10 +262,10 @@ class Solution { for (int j = i + 1; j < nums.length; j++) { - // nums[i]+nums[j] > target 直接返回, 剪枝操作 - if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) { - return result; - } + // nums[i] >= 0 && nums[i] + nums[j] > target 剪枝操作 + if (nums[i] >= 0 && nums[i] + nums[j] > target) { + break; + } if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重 continue; @@ -274,7 +274,7 @@ class Solution { int left = j + 1; int right = nums.length - 1; while (right > left) { - // nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出 + // nums[j] + nums[i] + nums[left] + nums[right] int 可能会溢出,需要转为 long long sum = (long) nums[i] + nums[j] + nums[left] + nums[right]; if (sum > target) { right--; From 43d28e6bd3fa92f45a18dc00da7cd2470e0872f7 Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 7 Aug 2024 17:06:16 +0800 Subject: [PATCH 1237/1533] =?UTF-8?q?343.=E6=95=B4=E6=95=B0=E6=8B=86?= =?UTF-8?q?=E5=88=86=E5=A2=9E=E5=8A=A0Go=E8=B4=AA=E5=BF=83=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\225\260\346\213\206\345\210\206.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 205b220144..7295627fde 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -309,6 +309,8 @@ class Solution: ``` ### Go + +动态规划 ```go func integerBreak(n int) int { /** @@ -338,6 +340,28 @@ func max(a, b int) int{ } ``` +贪心 +```go +func integerBreak(n int) int { + if n == 2 { + return 1 + } + if n == 3 { + return 2 + } + if n == 4 { + return 4 + } + result := 1 + for n > 4 { + result *= 3 + n -= 3 + } + result *= n + return result +} +``` + ### Javascript ```Javascript var integerBreak = function(n) { From 00405e519283b681daca6bb0f9dc5d292c1cc9ab Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Wed, 7 Aug 2024 07:13:31 -0700 Subject: [PATCH 1238/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00094.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93I.md=20Ja?= =?UTF-8?q?va=E7=89=88=E6=9C=AC=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\347\211\251\350\277\220\350\276\223I.md" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index f72bfc00a4..45ca131361 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -392,6 +392,63 @@ Bellman_ford 是可以计算 负权值的单源最短路算法。 ## 其他语言版本 ### Java +```Java +public class Main { + + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + List edges = new ArrayList<>(); + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + edges.add(new Edge(from, to, val)); + } + + // Represents the minimum distance from the current node to the original node + int[] minDist = new int[n + 1]; + + // Initialize the minDist array + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[1] = 0; + + // Starts the loop to relax all edges n - 1 times to update minDist array + for (int i = 1; i < n; i++) { + + for (Edge edge : edges) { + // Updates the minDist array + if (minDist[edge.from] != Integer.MAX_VALUE && (minDist[edge.from] + edge.val) < minDist[edge.to]) { + minDist[edge.to] = minDist[edge.from] + edge.val; + } + } + } + + // Outcome printing + if (minDist[n] == Integer.MAX_VALUE) { + System.out.println("unconnected"); + } else { + System.out.println(minDist[n]); + } + } +} + +``` ### Python From 2ecb6a5e71a40e6cae9f02c8bb0755f4ba8249d0 Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Wed, 7 Aug 2024 10:16:28 -0700 Subject: [PATCH 1239/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00094.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93I-SPFA=20?= =?UTF-8?q?Java=E7=89=88=E6=9C=AC=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 07317d6b04..ce38391943 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -352,6 +352,77 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹 ## 其他语言版本 ### Java +```Java +import java.util.*; + +public class Main { + + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + List> graph = new ArrayList<>(); + + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + graph.get(from).add(new Edge(from, to, val)); + } + + // Declare the minDist array to record the minimum distance form current node to the original node + int[] minDist = new int[n + 1]; + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[1] = 0; + + // Declare a queue to store the updated nodes instead of traversing all nodes each loop for more efficiency + Queue queue = new LinkedList<>(); + queue.offer(1); + + // Declare a boolean array to record if the current node is in the queue to optimise the processing + boolean[] isInQueue = new boolean[n + 1]; + + while (!queue.isEmpty()) { + int curNode = queue.poll(); + isInQueue[curNode] = false; // Represents the current node is not in the queue after being polled + for (Edge edge : graph.get(curNode)) { + if (minDist[edge.to] > minDist[edge.from] + edge.val) { // Start relaxing the edge + minDist[edge.to] = minDist[edge.from] + edge.val; + if (!isInQueue[edge.to]) { // Don't add the node if it's already in the queue + queue.offer(edge.to); + isInQueue[edge.to] = true; + } + } + } + } + + // Outcome printing + if (minDist[n] == Integer.MAX_VALUE) { + System.out.println("unconnected"); + } else { + System.out.println(minDist[n]); + } + } +} + +``` ### Python From a1365a6017da35ed336b3e1546a7a9fda167696b Mon Sep 17 00:00:00 2001 From: Taylor Zhu Date: Wed, 7 Aug 2024 17:46:03 -0700 Subject: [PATCH 1240/1533] =?UTF-8?q?Update=200232.=E7=94=A8=E6=A0=88?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ”模式“ -> ”模拟“ --- ...\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index e8a3d2ec33..7017107bf8 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -44,7 +44,7 @@ queue.empty(); // 返回 false 这是一道模拟题,不涉及到具体算法,考察的就是对栈和队列的掌握程度。 -使用栈来模式队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈**一个输入栈,一个输出栈**,这里要注意输入栈和输出栈的关系。 +使用栈来模拟队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈**一个输入栈,一个输出栈**,这里要注意输入栈和输出栈的关系。 下面动画模拟以下队列的执行过程: From d90d5fbdca5a82420521a960fc04f8fe067aaacf Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Thu, 8 Aug 2024 14:07:41 +0800 Subject: [PATCH 1241/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910104.=E5=BB=BA=E9=80=A0=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=B2=9B=E5=B1=BF=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\345\262\233\345\261\277.md" | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 7d67b7fcb1..d1f27e6dbd 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -372,6 +372,123 @@ public class Main { ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +let visited // 访问过的节点, 标记岛屿 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + +let count = 0 // 统计岛屿面积 +let areaMap = new Map() // 存储岛屿面积 + + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + visited = new Array(N).fill(0).map(() => new Array(M).fill(0)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + +/** + * @description: 从(x,y)开始深度优先遍历地图 + * @param {*} graph 地图 + * @param {*} visited 可访问节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @param {*} mark 当前岛屿的标记 + * @return {*} + */ +const dfs = (graph, visited, x, y, mark) => { + if (visited[x][y] != 0) return + visited[x][y] = mark + count++ + + for (let i = 0; i < 4; i++) { + let nextx = x + dir[i][0] + let nexty = y + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过 + + // 已访问过, 或者是海洋, 跳过 + if (visited[nextx][nexty] != 0 || graph[nextx][nexty] == 0) continue + + dfs(graph, visited, nextx, nexty, mark) + } +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + let isAllLand = true //标记整个地图都是陆地 + + let mark = 2 // 标记岛屿 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (graph[i][j] == 0 && isAllLand) isAllLand = false + if (graph[i][j] === 1 && visited[i][j] === 0) { + count = 0 + dfs(graph, visited, i, j, mark) + areaMap.set(mark, count) + mark++ + } + } + } + + // 如果全是陆地, 直接返回面积 + if (isAllLand) { + console.log(N * M); + return + } + + let result = 0 // 记录最后结果 + let visitedIsland = new Map() //标记访问过的岛屿, 因为海洋四周可能是同一个岛屿, 需要标记避免重复统计面积 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (visited[i][j] === 0) { + count = 1 // 记录连接之后的岛屿数量 + visitedIsland.clear() // 每次使用时,清空 + + // 计算海洋周围岛屿面积 + for (let m = 0; m < 4; m++) { + const nextx = i + dir[m][0] + const nexty = j + dir[m][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过 + + const island = visited[nextx][nexty] + if (island == 0 || visitedIsland.get(island)) continue// 四周是海洋或者访问过的陆地 跳过 + + // 标记为访问, 计算面积 + visitedIsland.set(island, true) + count += areaMap.get(visited[nextx][nexty]) + } + + result = Math.max(result, count) + } + } + } + + console.log(result); +})() +``` + + + ### TypeScript ### PhP From aff4169f1cd5248411021027cd176002f6b5a378 Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Thu, 8 Aug 2024 14:11:08 +0800 Subject: [PATCH 1242/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910107.=E5=AF=BB=E6=89=BE=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E7=9A=84=E8=B7=AF=E5=BE=84=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\347\232\204\350\267\257\345\276\204.md" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 906609c99a..97b2714c90 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -168,6 +168,72 @@ int main() { ### Javascript +```java +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + + +let N, M // 节点数和边数 +let source, destination // 起点 终点 +let father = [] // 并查集 + + +// 并查集初始化 +const init = () => { + for (let i = 1; i <= N; i++) father[i] = i; +} + +// 并查集里寻根的过程 +const find = (u) => { + return u == father[u] ? u : father[u] = find(father[u]) +} + +// 将v->u 这条边加入并查集 +const join = (u, v) => { + u = find(u) + v = find(v) + if (u == v) return // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u +} + +// 判断 u 和 v是否找到同一个根 +const isSame = (u, v) => { + u = find(u) + v = find(v) + return u == v +} + + +(async function () { + // 读取第一行输入 + let line = await readline(); + [N, M] = line.split(' ').map(Number); + + // 初始化并查集 + father = new Array(N) + init() + + // 读取边信息, 加入并查集 + for (let i = 0; i < M; i++) { + line = await readline() + line = line.split(' ').map(Number) + join(line[0], line[1]) + } + + // 读取起点和终点 + line = await readline(); //JS注意这里的冒号 + [source, destination] = line.split(' ').map(Number) + + if (isSame(source, destination)) return console.log(1); + console.log(0); +})() +``` + + + ### TypeScript ### PhP From 006fb935ffc246c2818d64d5efc3b55cb73e965e Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Thu, 8 Aug 2024 14:13:25 +0800 Subject: [PATCH 1243/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910108.=E5=86=97=E4=BD=99=E8=BF=9E=E6=8E=A5=20J?= =?UTF-8?q?S=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\344\275\231\350\277\236\346\216\245.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 7088413967..8acb2f10cb 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -141,6 +141,70 @@ int main() { ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + + +let N // 节点数和边数 +let father = [] // 并查集 + + +// 并查集初始化 +const init = () => { + for (let i = 1; i <= N; i++) father[i] = i; +} + +// 并查集里寻根的过程 +const find = (u) => { + return u == father[u] ? u : father[u] = find(father[u]) +} + +// 将v->u 这条边加入并查集 +const join = (u, v) => { + u = find(u) + v = find(v) + if (u == v) return // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u +} + +// 判断 u 和 v是否找到同一个根 +const isSame = (u, v) => { + u = find(u) + v = find(v) + return u == v +} + + +(async function () { + // 读取第一行输入 + let line = await readline(); + N = Number(line); + + // 初始化并查集 + father = new Array(N) + init() + + // 读取边信息, 加入并查集 + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + + if (!isSame(line[0], line[1])) { + join(line[0], line[1]) + }else{ + console.log(line[0], line[1]); + break + } + } +})() +``` + + + ### TypeScript ### PhP From a2aefe49b28d1a3b2bf6fd6b3e1aba76b0e5522d Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Thu, 8 Aug 2024 14:18:08 +0800 Subject: [PATCH 1244/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910109.=E5=86=97=E4=BD=99=E8=BF=9E=E6=8E=A5II?= =?UTF-8?q?=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\231\350\277\236\346\216\245II.md" | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index b13daec7bf..62b319c39b 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -254,6 +254,119 @@ int main() { ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + + +let N // 节点数和边数 +let father = [] // 并查集 +let edges = [] // 边集 +let inDegree = [] // 入度 + + +// 并查集初始化 +const init = () => { + for (let i = 1; i <= N; i++) father[i] = i; +} + +// 并查集里寻根的过程 +const find = (u) => { + return u == father[u] ? u : father[u] = find(father[u]) +} + +// 将v->u 这条边加入并查集 +const join = (u, v) => { + u = find(u) + v = find(v) + if (u == v) return // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[v] = u +} + +// 判断 u 和 v是否找到同一个根 +const isSame = (u, v) => { + u = find(u) + v = find(v) + return u == v +} + +// 判断删除一条边后是不是树 +const isTreeAfterRemoveEdge = (edges, edge) => { + // 初始化并查集 + init() + + for (let i = 0; i < N; i++) { + if (i == edge) continue + if (isSame(edges[i][0], edges[i][1])) { // 构成有向环了,一定不是树 + return false + } + join(edges[i][0], edges[i][1]) + } + return true +} + +// 在有向图里找到删除的那条边, 使其成为树 +const getRemoveEdge = (edges) => { + // 初始化并查集 + init() + + for (let i = 0; i < N; i++) { + if (isSame(edges[i][0], edges[i][1])) { // 构成有向环了,就是要删除的边 + console.log(edges[i][0], edges[i][1]); + return + } else { + join(edges[i][0], edges[i][1]) + } + } +} + + +(async function () { + // 读取第一行输入 + let line = await readline(); + N = Number(line); + + // 读取边信息, 统计入度 + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + + edges.push(line) + + inDegree[line[1]] = (inDegree[line[1]] || 0) + 1 + } + + // 找到入度为2的节点 + let vec = [] // 记录入度为2的边(如果有的话就两条边) + // 找入度为2的节点所对应的边,注意要倒序,因为优先删除最后出现的一条边 + for (let i = N - 1; i >= 0; i--) { + if (inDegree[edges[i][1]] == 2) { + vec.push(i) + } + } + + // 情况一、情况二 + if (vec.length > 0) { + // 放在vec里的边已经按照倒叙放的,所以这里就优先删vec[0]这条边 + if (isTreeAfterRemoveEdge(edges, vec[0])) { + console.log(edges[vec[0]][0], edges[vec[0]][1]); + } else { + console.log(edges[vec[1]][0], edges[vec[1]][1]); + } + return 0 + } + + // 情况三 + // 明确没有入度为2的情况,那么一定有有向环,找到构成环的边返回就可以了 + getRemoveEdge(edges) +})() +``` + + + ### TypeScript ### PhP From 047c57ab4063945aeca9b1032fe74e8f5d14a4e8 Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Thu, 8 Aug 2024 09:34:28 -0700 Subject: [PATCH 1245/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00095.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93II=20?= =?UTF-8?q?=E5=9F=BA=E4=BA=8EDPFA=E7=89=88=E6=9C=AC=E7=9A=84Java=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\211\251\350\277\220\350\276\223II.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 5bc4be7b22..6226eb0161 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -244,6 +244,92 @@ int main() { ## 其他语言版本 ### Java +```Java +import java.util.*; + +public class Main { + // 基于SPFA方法 + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + List> graph = new ArrayList<>(); + + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + graph.get(from).add(new Edge(from, to, val)); + } + + // Declare the minDist array to record the minimum distance form current node to the original node + int[] minDist = new int[n + 1]; + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[1] = 0; + + // Declare a queue to store the updated nodes instead of traversing all nodes each loop for more efficiency + Queue queue = new LinkedList<>(); + queue.offer(1); + + // Declare an array to record the times each node has been offered in the queue + int[] count = new int[n + 1]; + count[1]++; + + // Declare a boolean array to record if the current node is in the queue to optimise the processing + boolean[] isInQueue = new boolean[n + 1]; + + // Declare a boolean value to check if there is a negative weight loop inside the graph + boolean flag = false; + + while (!queue.isEmpty()) { + int curNode = queue.poll(); + isInQueue[curNode] = false; // Represents the current node is not in the queue after being polled + for (Edge edge : graph.get(curNode)) { + if (minDist[edge.to] > minDist[edge.from] + edge.val) { // Start relaxing the edge + minDist[edge.to] = minDist[edge.from] + edge.val; + if (!isInQueue[edge.to]) { // Don't add the node if it's already in the queue + queue.offer(edge.to); + count[edge.to]++; + isInQueue[edge.to] = true; + } + + if (count[edge.to] == n) { // If some node has been offered in the queue more than n-1 times + flag = true; + while (!queue.isEmpty()) queue.poll(); + break; + } + } + } + } + + if (flag) { + System.out.println("circle"); + } else if (minDist[n] == Integer.MAX_VALUE) { + System.out.println("unconnected"); + } else { + System.out.println(minDist[n]); + } + } +} + +``` ### Python From d2163ac3f698a4f2f0c4077a40db4e81cab42e14 Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Thu, 8 Aug 2024 09:43:47 -0700 Subject: [PATCH 1246/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00095.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93II=20?= =?UTF-8?q?=E5=9F=BA=E4=BA=8ESPFA=E7=89=88=E6=9C=AC=E7=9A=84Java=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 6226eb0161..f1025e4a7f 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -248,7 +248,7 @@ int main() { import java.util.*; public class Main { - // 基于SPFA方法 + // 基于Bellman_ford-SPFA方法 // Define an inner class Edge static class Edge { int from; From ab6cb84cb905dc3ededa4165653435afd38c34ed Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Thu, 8 Aug 2024 17:14:40 -0700 Subject: [PATCH 1247/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00096.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93III=20?= =?UTF-8?q?=E5=9F=BA=E4=BA=8EBellman=5Fford=E4=B8=80=E8=88=AC=E8=A7=A3?= =?UTF-8?q?=E6=B3=95Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...347\211\251\350\277\220\350\276\223III.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index feaaa1f3c3..dacd23d11d 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -636,6 +636,71 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 ## 其他语言版本 ### Java +```Java +import java.util.*; + +public class Main { + // 基于Bellman_for一般解法解决单源最短路径问题 + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + + List graph = new ArrayList<>(); + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + graph.add(new Edge(from, to, val)); + } + + int src = sc.nextInt(); + int dst = sc.nextInt(); + int k = sc.nextInt(); + + int[] minDist = new int[n + 1]; + int[] minDistCopy; + + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[src] = 0; + + for (int i = 0; i < k + 1; i++) { // Relax all edges k + 1 times + minDistCopy = Arrays.copyOf(minDist, n + 1); + for (Edge edge : graph) { + int from = edge.from; + int to = edge.to; + int val = edge.val; + // Use minDistCopy to calculate minDist + if (minDistCopy[from] != Integer.MAX_VALUE && minDist[to] > minDistCopy[from] + val) { + minDist[to] = minDistCopy[from] + val; + } + } + } + + // Output printing + if (minDist[dst] == Integer.MAX_VALUE) { + System.out.println("unreachable"); + } else { + System.out.println(minDist[dst]); + } + } +} + +``` ### Python From 93c206794beafcf6c2416e59291351984982e2b9 Mon Sep 17 00:00:00 2001 From: markwang Date: Fri, 9 Aug 2024 14:22:09 +0800 Subject: [PATCH 1248/1533] =?UTF-8?q?01=E8=83=8C=E5=8C=85-2=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Go=E4=B8=80=E7=BB=B4=E6=95=B0=E7=BB=84,=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4=E6=95=B0=E7=BB=84=E7=A7=BB=E8=87=B3-1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-1.md" | 52 +++++++++++++ ...47\241\20001\350\203\214\345\214\205-2.md" | 73 +++++++++---------- 2 files changed, 86 insertions(+), 39 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 0c78e3f695..6b9be72d6e 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -437,6 +437,58 @@ print(dp[n - 1][bagweight]) ### Go ```go +package main + +import ( + "fmt" +) + +func main() { + var n, bagweight int // bagweight代表行李箱空间 + fmt.Scan(&n, &bagweight) + + weight := make([]int, n) // 存储每件物品所占空间 + value := make([]int, n) // 存储每件物品价值 + + for i := 0; i < n; i++ { + fmt.Scan(&weight[i]) + } + for j := 0; j < n; j++ { + fmt.Scan(&value[j]) + } + // dp数组, dp[i][j]代表行李箱空间为j的情况下,从下标为[0, i]的物品里面任意取,能达到的最大价值 + dp := make([][]int, n) + for i := range dp { + dp[i] = make([]int, bagweight+1) + } + + // 初始化, 因为需要用到dp[i - 1]的值 + // j < weight[0]已在上方被初始化为0 + // j >= weight[0]的值就初始化为value[0] + for j := weight[0]; j <= bagweight; j++ { + dp[0][j] = value[0] + } + + for i := 1; i < n; i++ { // 遍历科研物品 + for j := 0; j <= bagweight; j++ { // 遍历行李箱容量 + if j < weight[i] { + dp[i][j] = dp[i-1][j] // 如果装不下这个物品,那么就继承dp[i - 1][j]的值 + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) + } + } + } + + fmt.Println(dp[n-1][bagweight]) +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} + ``` ### Javascript diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index c61a72cc40..6485da1f9e 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -318,50 +318,45 @@ print(dp[n - 1][bagweight]) package main import ( - "fmt" + "fmt" ) func main() { - var n, bagweight int - fmt.Scan(&n, &bagweight) - - weight := make([]int, n) - value := make([]int, n) - - for i := 0; i < n; i++ { - fmt.Scan(&weight[i]) - } - for j := 0; j < n; j++ { - fmt.Scan(&value[j]) - } - - dp := make([][]int, n) - for i := range dp { - dp[i] = make([]int, bagweight+1) - } - - for j := weight[0]; j <= bagweight; j++ { - dp[0][j] = value[0] - } - - for i := 1; i < n; i++ { - for j := 0; j <= bagweight; j++ { - if j < weight[i] { - dp[i][j] = dp[i-1][j] - } else { - dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) - } - } - } - - fmt.Println(dp[n-1][bagweight]) + // 读取 M 和 N + var M, N int + fmt.Scan(&M, &N) + + costs := make([]int, M) + values := make([]int, M) + + for i := 0; i < M; i++ { + fmt.Scan(&costs[i]) + } + for j := 0; j < M; j++ { + fmt.Scan(&values[j]) + } + + // 创建一个动态规划数组dp,初始值为0 + dp := make([]int, N + 1) + + // 外层循环遍历每个类型的研究材料 + for i := 0; i < M; i++ { + // 内层循环从 N 空间逐渐减少到当前研究材料所占空间 + for j := N; j >= costs[i]; j-- { + // 考虑当前研究材料选择和不选择的情况,选择最大值 + dp[j] = max(dp[j], dp[j-costs[i]] + values[i]) + } + } + + // 输出dp[N],即在给定 N 行李空间可以携带的研究材料最大价值 + fmt.Println(dp[N]) } -func max(a, b int) int { - if a > b { - return a - } - return b +func max(x, y int) int { + if x > y { + return x + } + return y } ``` From bb2803ce967080ca6274b854458176a345b5fcee Mon Sep 17 00:00:00 2001 From: markwang Date: Fri, 9 Aug 2024 15:05:17 +0800 Subject: [PATCH 1249/1533] =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-1.md" | 82 +++++++++---------- ...47\241\20001\350\203\214\345\214\205-2.md" | 68 +++++++-------- 2 files changed, 75 insertions(+), 75 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 6b9be72d6e..352b301a8b 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -440,53 +440,53 @@ print(dp[n - 1][bagweight]) package main import ( - "fmt" + "fmt" ) func main() { - var n, bagweight int // bagweight代表行李箱空间 - fmt.Scan(&n, &bagweight) - - weight := make([]int, n) // 存储每件物品所占空间 - value := make([]int, n) // 存储每件物品价值 - - for i := 0; i < n; i++ { - fmt.Scan(&weight[i]) - } - for j := 0; j < n; j++ { - fmt.Scan(&value[j]) - } - // dp数组, dp[i][j]代表行李箱空间为j的情况下,从下标为[0, i]的物品里面任意取,能达到的最大价值 - dp := make([][]int, n) - for i := range dp { - dp[i] = make([]int, bagweight+1) - } - - // 初始化, 因为需要用到dp[i - 1]的值 - // j < weight[0]已在上方被初始化为0 - // j >= weight[0]的值就初始化为value[0] - for j := weight[0]; j <= bagweight; j++ { - dp[0][j] = value[0] - } - - for i := 1; i < n; i++ { // 遍历科研物品 - for j := 0; j <= bagweight; j++ { // 遍历行李箱容量 - if j < weight[i] { - dp[i][j] = dp[i-1][j] // 如果装不下这个物品,那么就继承dp[i - 1][j]的值 - } else { - dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) - } - } - } - - fmt.Println(dp[n-1][bagweight]) + var n, bagweight int // bagweight代表行李箱空间 + fmt.Scan(&n, &bagweight) + + weight := make([]int, n) // 存储每件物品所占空间 + value := make([]int, n) // 存储每件物品价值 + + for i := 0; i < n; i++ { + fmt.Scan(&weight[i]) + } + for j := 0; j < n; j++ { + fmt.Scan(&value[j]) + } + // dp数组, dp[i][j]代表行李箱空间为j的情况下,从下标为[0, i]的物品里面任意取,能达到的最大价值 + dp := make([][]int, n) + for i := range dp { + dp[i] = make([]int, bagweight + 1) + } + + // 初始化, 因为需要用到dp[i - 1]的值 + // j < weight[0]已在上方被初始化为0 + // j >= weight[0]的值就初始化为value[0] + for j := weight[0]; j <= bagweight; j++ { + dp[0][j] = value[0] + } + + for i := 1; i < n; i++ { // 遍历科研物品 + for j := 0; j <= bagweight; j++ { // 遍历行李箱容量 + if j < weight[i] { + dp[i][j] = dp[i-1][j] // 如果装不下这个物品,那么就继承dp[i - 1][j]的值 + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) + } + } + } + + fmt.Println(dp[n-1][bagweight]) } func max(x, y int) int { - if x > y { - return x - } - return y + if x > y { + return x + } + return y } ``` diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 6485da1f9e..cd8f317c52 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -313,50 +313,50 @@ for i in range(1, n): print(dp[n - 1][bagweight]) ``` -### Go +### Go ```go package main import ( - "fmt" + "fmt" ) func main() { - // 读取 M 和 N - var M, N int - fmt.Scan(&M, &N) - - costs := make([]int, M) - values := make([]int, M) - - for i := 0; i < M; i++ { - fmt.Scan(&costs[i]) - } - for j := 0; j < M; j++ { - fmt.Scan(&values[j]) - } - - // 创建一个动态规划数组dp,初始值为0 - dp := make([]int, N + 1) - - // 外层循环遍历每个类型的研究材料 - for i := 0; i < M; i++ { - // 内层循环从 N 空间逐渐减少到当前研究材料所占空间 - for j := N; j >= costs[i]; j-- { - // 考虑当前研究材料选择和不选择的情况,选择最大值 - dp[j] = max(dp[j], dp[j-costs[i]] + values[i]) - } - } - - // 输出dp[N],即在给定 N 行李空间可以携带的研究材料最大价值 - fmt.Println(dp[N]) + // 读取 M 和 N + var M, N int + fmt.Scan(&M, &N) + + costs := make([]int, M) + values := make([]int, M) + + for i := 0; i < M; i++ { + fmt.Scan(&costs[i]) + } + for j := 0; j < M; j++ { + fmt.Scan(&values[j]) + } + + // 创建一个动态规划数组dp,初始值为0 + dp := make([]int, N + 1) + + // 外层循环遍历每个类型的研究材料 + for i := 0; i < M; i++ { + // 内层循环从 N 空间逐渐减少到当前研究材料所占空间 + for j := N; j >= costs[i]; j-- { + // 考虑当前研究材料选择和不选择的情况,选择最大值 + dp[j] = max(dp[j], dp[j-costs[i]] + values[i]) + } + } + + // 输出dp[N],即在给定 N 行李空间可以携带的研究材料最大价值 + fmt.Println(dp[N]) } func max(x, y int) int { - if x > y { - return x - } - return y + if x > y { + return x + } + return y } ``` From a681794bc82acab371c8f73076b16d60f29708d3 Mon Sep 17 00:00:00 2001 From: DengSchoo <46556279+DengSchoo@users.noreply.github.com> Date: Fri, 9 Aug 2024 16:49:23 +0800 Subject: [PATCH 1250/1533] =?UTF-8?q?chore:=20Move=20file's=20content=20"l?= =?UTF-8?q?eetcode-master/107=E5=AF=BB=E6=89=BE=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E8=B7=AF=E5=BE=84Java=E4=BB=A3=E7=A0=81.md"=20to=20"leetcode-m?= =?UTF-8?q?aster/problems/kamacoder/0107.=E5=AF=BB=E6=89=BE=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E8=B7=AF=E5=BE=84.md"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "leetcode-master/107寻找存在的路径Java代码.md" file merged in the wrong place and the content of it should be written in the "0107.寻找存在的路径.md". --- ...50\347\232\204\350\267\257\345\276\204.md" | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 906609c99a..70339a9da1 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -158,7 +158,62 @@ int main() { ## 其他语言版本 -### Java +### Java + +```Java + +import java.util.*; + +public class Main{ + public static void main(String[] args) { + int N, M; + Scanner scanner = new Scanner(System.in); + N = scanner.nextInt(); + M = scanner.nextInt(); + DisJoint disJoint = new DisJoint(N + 1); + for (int i = 0; i < M; ++i) { + disJoint.join(scanner.nextInt(), scanner.nextInt()); + } + if(disJoint.isSame(scanner.nextInt(), scanner.nextInt())) { + System.out.println("1"); + } else { + System.out.println("0"); + } + } + +} + +//并查集模板 +class DisJoint{ + private int[] father; + + public DisJoint(int N) { + father = new int[N]; + for (int i = 0; i < N; ++i){ + father[i] = i; + } + } + + public int find(int n) { + return n == father[n] ? n : (father[n] = find(father[n])); + } + + public void join (int n, int m) { + n = find(n); + m = find(m); + if (n == m) return; + father[m] = n; + } + + public boolean isSame(int n, int m){ + n = find(n); + m = find(m); + return n == m; + } + +} + +``` ### Python From 216ee8d1e934826e5fc7f4cf1499bd3b711462d0 Mon Sep 17 00:00:00 2001 From: DengSchoo <46556279+DengSchoo@users.noreply.github.com> Date: Fri, 9 Aug 2024 17:59:30 +0800 Subject: [PATCH 1251/1533] =?UTF-8?q?chore:=20Move=20file's=20content=20"l?= =?UTF-8?q?eetcode-master/107=E5=AF=BB=E6=89=BE=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E8=B7=AF=E5=BE=84Java=E4=BB=A3=E7=A0=81.md"=20to=20leetcode-ma?= =?UTF-8?q?ster/problems/kamacoder/0107.=E5=AF=BB=E6=89=BE=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chore: Move file's content "leetcode-master/107寻找存在的路径Java代码.md" to leetcode-master/problems/kamacoder/0107.寻找存在的路径.md --- ...7\345\276\204Java\344\273\243\347\240\201" | 56 ------------------- 1 file changed, 56 deletions(-) delete mode 100644 "107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" diff --git "a/107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" "b/107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" deleted file mode 100644 index 7b83ca2622..0000000000 --- "a/107\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204Java\344\273\243\347\240\201" +++ /dev/null @@ -1,56 +0,0 @@ -JAVA: - -```Java - -import java.util.*; - -public class Main{ - public static void main(String[] args) { - int N, M; - Scanner scanner = new Scanner(System.in); - N = scanner.nextInt(); - M = scanner.nextInt(); - DisJoint disJoint = new DisJoint(N + 1); - for (int i = 0; i < M; ++i) { - disJoint.join(scanner.nextInt(), scanner.nextInt()); - } - if(disJoint.isSame(scanner.nextInt(), scanner.nextInt())) { - System.out.println("1"); - } else { - System.out.println("0"); - } - } - -} - -//并查集模板 -class DisJoint{ - private int[] father; - - public DisJoint(int N) { - father = new int[N]; - for (int i = 0; i < N; ++i){ - father[i] = i; - } - } - - public int find(int n) { - return n == father[n] ? n : (father[n] = find(father[n])); - } - - public void join (int n, int m) { - n = find(n); - m = find(m); - if (n == m) return; - father[m] = n; - } - - public boolean isSame(int n, int m){ - n = find(n); - m = find(m); - return n == m; - } - -} - -``` From d71abfa7f6001df1d7be95b2df790651a57cc0ee Mon Sep 17 00:00:00 2001 From: kyshen Date: Fri, 9 Aug 2024 22:53:20 +0800 Subject: [PATCH 1252/1533] feat: 0105 python BFS solution --- ...50\345\217\257\350\276\276\346\200\247.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 226d0f13c5..cd880a83d4 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -290,6 +290,42 @@ int main() { ### Java ### Python +BFS算法 +```Python +import collections + +path = set() # 纪录 BFS 所经过之节点 + +def bfs(root, graph): + global path + + que = collections.deque([root]) + while que: + cur = que.popleft() + path.add(cur) + + for nei in graph[cur]: + que.append(nei) + graph[cur] = [] + return + +def main(): + N, K = map(int, input().strip().split()) + graph = collections.defaultdict(list) + for _ in range(K): + src, dest = map(int, input().strip().split()) + graph[src].append(dest) + + bfs(1, graph) + if path == {i for i in range(1, N + 1)}: + return 1 + return -1 + + +if __name__ == "__main__": + print(main()) + +``` ### Go From a868ae4d2e985da8abe18a1d9e91378a305bc8ce Mon Sep 17 00:00:00 2001 From: SecondaryRainbow <927497402@qq.com> Date: Sun, 11 Aug 2024 20:40:35 +0800 Subject: [PATCH 1253/1533] =?UTF-8?q?977=E9=A2=98=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了977题Go语言代码附近的格式,以及C#代码附近的格式,使其能够正常显示。(原本Go与python代码合到一起了) --- ...25\260\347\273\204\347\232\204\345\271\263\346\226\271.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 7119dda585..effa905541 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -207,6 +207,7 @@ class Solution: new_list.append(nums[left] ** 2) left += 1 return new_list[::-1] +``` ### Go: @@ -539,7 +540,7 @@ public class Solution { return result; } } - +``` C# LINQ: ```csharp public class Solution { @@ -553,3 +554,4 @@ public class Solution { + From 49aa077428baef0079a7ef22e82b8a113eca4f40 Mon Sep 17 00:00:00 2001 From: liujiahang Date: Mon, 12 Aug 2024 00:57:42 +0800 Subject: [PATCH 1254/1533] =?UTF-8?q?=E8=AE=A2=E6=AD=A3=E6=96=87=E4=BB=B60?= =?UTF-8?q?099.=E5=B2=9B=E5=B1=BF=E7=9A=84=E6=95=B0=E9=87=8F=E5=B9=BF?= =?UTF-8?q?=E6=90=9C.md=E6=96=87=E4=BB=B6=EF=BC=8Cpython=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=98=AF=E6=B7=B1=E6=90=9C=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\345\271\277\346\220\234.md" | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 3c069b4412..9771877b2d 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -246,29 +246,36 @@ public class Main { ```python -def dfs(grid, visited, x, y): - dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 四个方向 - for d in dir: - nextx, nexty = x + d[0], y + d[1] - if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]): - if not visited[nextx][nexty] and grid[nextx][nexty] == 1: # 没有访问过的 同时 是陆地的 - visited[nextx][nexty] = True - dfs(grid, visited, nextx, nexty) +from collections import deque +directions = [[0, 1], [1, 0], [0, -1], [-1, 0]] +def bfs(grid, visited, x, y): + que = deque([]) + que.append([x,y]) + while que: + cur_x, cur_y = que.popleft() + for i, j in directions: + next_x = cur_x + i + next_y = cur_y + j + if next_y < 0 or next_x < 0 or next_x >= len(grid) or next_y >= len(grid[0]): + continue + if not visited[next_x][next_y] and grid[next_x][next_y] == 1: + visited[next_x][next_y] = True + que.append([next_x, next_y]) + def main(): n, m = map(int, input().split()) - grid = [list(map(int, input().split())) for _ in range(n)] + grid = [] + for i in range(n): + grid.append(list(map(int, input().split()))) visited = [[False] * m for _ in range(n)] - - result = 0 + res = 0 for i in range(n): for j in range(m): - if not visited[i][j] and grid[i][j] == 1: - visited[i][j] = True - result += 1 # 遇到没访问过的陆地,+1 - dfs(grid, visited, i, j) # 将与其链接的陆地都标记上 True - - print(result) + if grid[i][j] == 1 and not visited[i][j]: + res += 1 + bfs(grid, visited, i, j) + print(res) if __name__ == "__main__": main() From 06d15bb5917f61112388154b6db63e55e9f647f2 Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Tue, 13 Aug 2024 09:14:53 +0800 Subject: [PATCH 1255/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910110.=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=8E=A5?= =?UTF-8?q?=E9=BE=99=20JS=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\344\270\262\346\216\245\351\276\231.md" | 76 ++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index feeec6ddcc..b96290b6b5 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -38,7 +38,7 @@ ebc dec dfc yhn -``` +``` 输出示例 @@ -224,6 +224,80 @@ public class Main { ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let N //输入字符串个数 +let beginStr //开始字符串 +let endStr //结束字符串 +let strSet = new Set() //字符串集合 +let visitedMap = new Map() //访问过的字符串 + +// 读取输入,初始化地图 +const init = async () => { + let line = await readline(); + line = line.trim() + N = Number(line); + + line = await readline(); + line = line.trim().split(' ') + beginStr = line[0] + endStr = line[1] + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.trim() + strSet.add(line.trim()) + } +} + +(async function () { + + // 读取输入,初始化数据 + await init() + + // 初始化队列 + let queue = [] + queue.push(beginStr) + + // 初始化visitMap + visitedMap.set(beginStr, 1) + + while (queue.length) { + let word = queue.shift() + let path = visitedMap.get(word) + + // 遍历26个字母 + for (let i = 0; i < word.length; i++) { + let newWord = word.split('') // 用一个新字符串替换str,因为每次要置换一个字符 + for (let j = 0; j < 26; j++) { + newWord[i] = String.fromCharCode('a'.charCodeAt() + j) + // 发现替换字母后,字符串与终点字符串相同 + if (newWord.join('') === endStr) { + console.log(path + 1); + return 0; // 找到了路径 + } + + // 字符串集合里出现了newWord,并且newWord没有被访问过 + if (strSet.has(newWord.join('')) && !visitedMap.has(newWord.join(''))) { + // 添加访问信息,并将新字符串放到队列中 + queue.push(newWord.join('')) + visitedMap.set(newWord.join(''), path + 1) + } + } + } + } + + console.log(0); +})() +``` + + + ### TypeScript ### PhP From ce3995c8fd6f5a98ca2ac4a94ad1d70d8d42a88e Mon Sep 17 00:00:00 2001 From: wangya <1264178545@qq.com> Date: Tue, 13 Aug 2024 09:26:26 +0800 Subject: [PATCH 1256/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8D=A1?= =?UTF-8?q?=E7=A0=81=E7=BD=910117.=E8=BD=AF=E4=BB=B6=E6=9E=84=E5=BB=BA=20J?= =?UTF-8?q?S=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\344\273\266\346\236\204\345\273\272.md" | 76 ++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index 05cb735815..bb0c9aec72 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -193,7 +193,7 @@ 理解思想后,确实不难,但代码写起来也不容易。 -为了每次可以找到所有节点的入度信息,我们要在初始话的时候,就把每个节点的入度 和 每个节点的依赖关系做统计。 +为了每次可以找到所有节点的入度信息,我们要在初始化的时候,就把每个节点的入度 和 每个节点的依赖关系做统计。 代码如下: @@ -451,6 +451,80 @@ if __name__ == "__main__": ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + + +let N, M // 节点数和边数 +let inDegrees = [] // 入度 +let umap = new Map() // 记录文件依赖关系 +let result = [] // 结果 + + +// 根据输入, 初始化数据 +const init = async () => { + // 读取第一行输入 + let line = await readline(); + [N, M] = line.split(' ').map(Number) + + inDegrees = new Array(N).fill(0) + + // 读取边集 + while (M--) { + line = await readline(); + let [x, y] = line.split(' ').map(Number) + + // 记录入度 + inDegrees[y]++ + + // 记录x指向哪些文件 + if (!umap.has(x)) { + umap.set(x, [y]) + } else { + umap.get(x).push(y) + } + } +} + +(async function () { + // 根据输入, 初始化数据 + await init() + + let queue = [] // 入度为0的节点 + for (let i = 0; i < N; i++) { + if (inDegrees[i] == 0) { + queue.push(i) + } + } + + while (queue.length) { + let cur = queue.shift() //当前文件 + + result.push(cur) + + let files = umap.get(cur) // 当前文件指向的文件 + + // 当前文件指向的文件入度减1 + if (files && files.length) { + for (let i = 0; i < files.length; i++) { + inDegrees[files[i]]-- + if (inDegrees[files[i]] == 0) queue.push(files[i]) + } + } + } + + // 这里result.length == N 一定要判断, 因为可能存在环 + if (result.length == N) return console.log(result.join(' ')) + console.log(-1) +})() +``` + + + ### TypeScript ### PhP From 76b2653adb3f081e918da9c43935059d677f8ac0 Mon Sep 17 00:00:00 2001 From: cyxiwai Date: Tue, 13 Aug 2024 21:54:42 +0800 Subject: [PATCH 1257/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86num18?= =?UTF-8?q?=E7=AC=AC=E5=8D=81=E5=85=AB=E9=A2=98=E5=9B=9B=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=E7=9A=84=E5=BE=AA=E7=8E=AF=E5=88=A4=E6=96=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=86return=20result?= =?UTF-8?q?=E4=B8=BAbreak=EF=BC=8C=E5=8F=AF=E4=BB=A5ac=E5=8A=9B=E6=89=A3?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 8e34713d37..f45dd939af 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -264,7 +264,7 @@ class Solution { // nums[i]+nums[j] > target 直接返回, 剪枝操作 if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) { - return result; + break; } if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重 From 247cfabf08d2e281fd32c65eac6c620633a3e64d Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 14 Aug 2024 14:22:29 +0800 Subject: [PATCH 1258/1533] =?UTF-8?q?416.=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86=E5=A2=9E=E5=8A=A0Go=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4dp=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\345\222\214\345\255\220\351\233\206.md" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 4d2e6bf656..5bd7ff6c81 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -453,6 +453,7 @@ class Solution: ``` ### Go: +一维dp ```go // 分割等和子集 动态规划 // 时间复杂度O(n^2) 空间复杂度O(n) @@ -480,6 +481,44 @@ func canPartition(nums []int) bool { } ``` +二维dp +```go +func canPartition(nums []int) bool { + sum := 0 + for _, val := range nums { + sum += val + } + if sum % 2 == 1 { + return false + } + target := sum / 2 + dp := make([][]int, len(nums)) + for i := range dp { + dp[i] = make([]int, target + 1) + } + for j := nums[0]; j <= target; j++ { + dp[0][j] = nums[0] + } + for i := 1; i < len(nums); i++ { + for j := 0; j <= target; j++ { + if j < nums[i] { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-nums[i]] + nums[i]) + } + } + } + return dp[len(nums)-1][target] == target +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + ### JavaScript: ```js From 0f887ab3460d4bed850762cb1aef4b95c99a9ed6 Mon Sep 17 00:00:00 2001 From: zhangwt <52098594+zhangwt-cn@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:44:52 +0800 Subject: [PATCH 1259/1533] =?UTF-8?q?fix:=20=E6=9B=B4=E6=AD=A3=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E4=B8=8B`=E5=8C=BA=E9=97=B4=E5=92=8C`=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 96852d706d..ac33b58659 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,6 @@ 5. [数组:209.长度最小的子数组](./problems/0209.长度最小的子数组.md) 6. [数组:区间和](./problems/kamacoder/0058.区间和.md) 6. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md) -7. [数组:区间和](./problems/kamacoder/0058.区间和.md) 8. [数组:开发商购买土地](./problems/kamacoder/0044.开发商购买土地.md) 9. [数组:总结篇](./problems/数组总结篇.md) From de57c317f96bf49b20dd3dd80ee6317d59e67671 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 15 Aug 2024 16:39:04 +0800 Subject: [PATCH 1260/1533] =?UTF-8?q?1049.=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8FII?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E4=BA=8C=E7=BB=B4dp=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\232\204\351\207\215\351\207\217II.md" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 4f2cc9e350..b40ed114eb 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -313,6 +313,8 @@ class Solution: ``` ### Go: + +一维dp ```go func lastStoneWeightII(stones []int) int { // 15001 = 30 * 1000 /2 +1 @@ -341,6 +343,43 @@ func max(a, b int) int { } ``` +二维dp +```go +func lastStoneWeightII(stones []int) int { + sum := 0 + for _, val := range stones { + sum += val + } + target := sum / 2 + + dp := make([][]int, len(stones)) + for i := range dp { + dp[i] = make([]int, target + 1) + } + for j := stones[0]; j <= target; j++ { + dp[0][j] = stones[0] + } + + for i := 1; i < len(stones); i++ { + for j := 0; j <= target; j++ { + if stones[i] > j { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-stones[i]] + stones[i]) + } + } + } + return (sum - dp[len(stones)-1][target]) - dp[len(stones)-1][target] +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + ### JavaScript: ```javascript From eef6abbbc320f49436306247a592cb2f84be0604 Mon Sep 17 00:00:00 2001 From: kyshen Date: Sun, 18 Aug 2024 01:20:14 +0800 Subject: [PATCH 1261/1533] fix: typo --- .../0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index bb0c9aec72..348187d6f1 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -11,7 +11,7 @@ 输入描述: -第一行输入两个正整数 M, N。表示 N 个文件之间拥有 M 条依赖关系。 +第一行输入两个正整数 N, M。表示 N 个文件之间拥有 M 条依赖关系。 后续 M 行,每行两个正整数 S 和 T,表示 T 文件依赖于 S 文件。 From 7ff07aa66e72e9f7cdf22814b8cba6aa3c4d2eaa Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Sun, 18 Aug 2024 11:10:47 +0800 Subject: [PATCH 1262/1533] =?UTF-8?q?Update=200102.=E6=B2=89=E6=B2=A1?= =?UTF-8?q?=E5=AD=A4=E5=B2=9B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0102.沉没孤岛.md --- ...11\346\262\241\345\255\244\345\262\233.md" | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 2b16da0472..5cfeff7662 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -138,8 +138,128 @@ int main() { ### Java +```JAVA + +import java.util.Scanner; + +public class Main { + static int[][] dir = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} }; // 保存四个方向 + + public static void dfs(int[][] grid, int x, int y) { + grid[x][y] = 2; + for (int[] d : dir) { + int nextX = x + d[0]; + int nextY = y + d[1]; + // 超过边界 + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue; + // 不符合条件,不继续遍历 + if (grid[nextX][nextY] == 0 || grid[nextX][nextY] == 2) continue; + dfs(grid, nextX, nextY); + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] grid = new int[n][m]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + grid[i][j] = scanner.nextInt(); + } + } + + // 步骤一: + // 从左侧边,和右侧边 向中间遍历 + for (int i = 0; i < n; i++) { + if (grid[i][0] == 1) dfs(grid, i, 0); + if (grid[i][m - 1] == 1) dfs(grid, i, m - 1); + } + + // 从上边和下边 向中间遍历 + for (int j = 0; j < m; j++) { + if (grid[0][j] == 1) dfs(grid, 0, j); + if (grid[n - 1][j] == 1) dfs(grid, n - 1, j); + } + + // 步骤二、步骤三 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) grid[i][j] = 0; + if (grid[i][j] == 2) grid[i][j] = 1; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + System.out.print(grid[i][j] + " "); + } + System.out.println(); + } + + scanner.close(); + } +} + + +``` + + ### Python +```python + +def dfs(grid, x, y): + grid[x][y] = 2 + directions = [(-1, 0), (0, -1), (1, 0), (0, 1)] # 四个方向 + for dx, dy in directions: + nextx, nexty = x + dx, y + dy + # 超过边界 + if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]): + continue + # 不符合条件,不继续遍历 + if grid[nextx][nexty] == 0 or grid[nextx][nexty] == 2: + continue + dfs(grid, nextx, nexty) + +def main(): + n, m = map(int, input().split()) + grid = [[int(x) for x in input().split()] for _ in range(n)] + + # 步骤一: + # 从左侧边,和右侧边 向中间遍历 + for i in range(n): + if grid[i][0] == 1: + dfs(grid, i, 0) + if grid[i][m - 1] == 1: + dfs(grid, i, m - 1) + + # 从上边和下边 向中间遍历 + for j in range(m): + if grid[0][j] == 1: + dfs(grid, 0, j) + if grid[n - 1][j] == 1: + dfs(grid, n - 1, j) + + # 步骤二、步骤三 + for i in range(n): + for j in range(m): + if grid[i][j] == 1: + grid[i][j] = 0 + if grid[i][j] == 2: + grid[i][j] = 1 + + # 打印结果 + for row in grid: + print(' '.join(map(str, row))) + +if __name__ == "__main__": + main() + +``` + + ### Go ### Rust From 1efaa188770ddcf3cda62df5068fe28cb105ef37 Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Sun, 18 Aug 2024 12:48:19 +0800 Subject: [PATCH 1263/1533] =?UTF-8?q?Update=200105.=E6=9C=89=E5=90=91?= =?UTF-8?q?=E5=9B=BE=E7=9A=84=E5=AE=8C=E5=85=A8=E5=8F=AF=E8=BE=BE=E6=80=A7?= =?UTF-8?q?.md=20for=20java=20python=20and=20go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0105.有向图的完全可达性.md for java python and go --- ...50\345\217\257\350\276\276\346\200\247.md" | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 226d0f13c5..30baa64924 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -289,10 +289,154 @@ int main() { ### Java +```java + +import java.util.*; + +public class Main { + + public static void dfs(List> graph, int key, boolean[] visited) { + for (int neighbor : graph.get(key)) { + if (!visited[neighbor]) { // Check if the next node is not visited + visited[neighbor] = true; + dfs(graph, neighbor, visited); + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + List> graph = new ArrayList<>(); + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int s = scanner.nextInt(); + int t = scanner.nextInt(); + graph.get(s).add(t); + } + + boolean[] visited = new boolean[n + 1]; + visited[1] = true; // Process node 1 beforehand + dfs(graph, 1, visited); + + for (int i = 1; i <= n; i++) { + if (!visited[i]) { + System.out.println(-1); + return; + } + } + System.out.println(1); + } +} + + +``` + + ### Python +``` python + +def dfs(graph, key, visited): + for neighbor in graph[key]: + if not visited[neighbor]: # Check if the next node is not visited + visited[neighbor] = True + dfs(graph, neighbor, visited) + +def main(): + import sys + input = sys.stdin.read + data = input().split() + + n = int(data[0]) + m = int(data[1]) + + graph = [[] for _ in range(n + 1)] + index = 2 + for _ in range(m): + s = int(data[index]) + t = int(data[index + 1]) + graph[s].append(t) + index += 2 + + visited = [False] * (n + 1) + visited[1] = True # Process node 1 beforehand + dfs(graph, 1, visited) + + for i in range(1, n + 1): + if not visited[i]: + print(-1) + return + + print(1) + +if __name__ == "__main__": + main() + + +``` + ### Go +```go + +package main + +import ( + "bufio" + "fmt" + "os" +) + +func dfs(graph [][]int, key int, visited []bool) { + visited[key] = true + for _, neighbor := range graph[key] { + if !visited[neighbor] { + dfs(graph, neighbor, visited) + } + } +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + var n, m int + fmt.Sscanf(scanner.Text(), "%d %d", &n, &m) + + graph := make([][]int, n+1) + for i := 0; i <= n; i++ { + graph[i] = make([]int, 0) + } + + for i := 0; i < m; i++ { + scanner.Scan() + var s, t int + fmt.Sscanf(scanner.Text(), "%d %d", &s, &t) + graph[s] = append(graph[s], t) + } + + visited := make([]bool, n+1) + + dfs(graph, 1, visited) + + for i := 1; i <= n; i++ { + if !visited[i] { + fmt.Println(-1) + return + } + } + fmt.Println(1) +} + + +``` + + ### Rust ### Javascript From d03a507431cf7c123324be4f1924575f7d4b8ae0 Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Sun, 18 Aug 2024 12:53:45 +0800 Subject: [PATCH 1264/1533] =?UTF-8?q?Update=200106.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E5=91=A8=E9=95=BF.md=20for=20python=20go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0106.岛屿的周长.md for python go --- ...77\347\232\204\345\221\250\351\225\277.md" | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 6f3462c52d..235d9445d1 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -216,8 +216,131 @@ public class Main { ### Python +```python + +def main(): + import sys + input = sys.stdin.read + data = input().split() + + # 读取 n 和 m + n = int(data[0]) + m = int(data[1]) + + # 初始化 grid + grid = [] + index = 2 + for i in range(n): + grid.append([int(data[index + j]) for j in range(m)]) + index += m + + sum_land = 0 # 陆地数量 + cover = 0 # 相邻数量 + + for i in range(n): + for j in range(m): + if grid[i][j] == 1: + sum_land += 1 + # 统计上边相邻陆地 + if i - 1 >= 0 and grid[i - 1][j] == 1: + cover += 1 + # 统计左边相邻陆地 + if j - 1 >= 0 and grid[i][j - 1] == 1: + cover += 1 + # 不统计下边和右边,避免重复计算 + + result = sum_land * 4 - cover * 2 + print(result) + +if __name__ == "__main__": + main() + + +``` + ### Go +```go + +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + line := scanner.Text() + + n, m := parseInput(line) + + // 初始化 grid + grid := make([][]int, n) + for i := range grid { + grid[i] = make([]int, m) + } + + // 读入 grid 数据 + for i := 0; i < n; i++ { + scanner.Scan() + line := scanner.Text() + values := parseLine(line, m) + for j := 0; j < m; j++ { + grid[i][j] = values[j] + } + } + + sum := 0 // 陆地数量 + cover := 0 // 相邻数量 + + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + if grid[i][j] == 1 { + sum++ // 统计总的陆地数量 + + // 统计上边相邻陆地 + if i-1 >= 0 && grid[i-1][j] == 1 { + cover++ + } + // 统计左边相邻陆地 + if j-1 >= 0 && grid[i][j-1] == 1 { + cover++ + } + // 为什么没统计下边和右边? 因为避免重复计算 + } + } + } + + fmt.Println(sum*4 - cover*2) +} + +// parseInput 解析 n 和 m +func parseInput(line string) (int, int) { + parts := strings.Split(line, " ") + n, _ := strconv.Atoi(parts[0]) + m, _ := strconv.Atoi(parts[1]) + return n, m +} + +// parseLine 解析一行中的多个值 +func parseLine(line string, count int) []int { + parts := strings.Split(line, " ") + values := make([]int, count) + for i := 0; i < count; i++ { + values[i], _ = strconv.Atoi(parts[i]) + } + return values +} + + +``` + + ### Rust ### Javascript From b5b2aa3cef279c7451ddb5fcb69cd0087f1e73b6 Mon Sep 17 00:00:00 2001 From: Charlie Yang <104724079+sxdtywm@users.noreply.github.com> Date: Sun, 18 Aug 2024 12:59:04 +0800 Subject: [PATCH 1265/1533] =?UTF-8?q?Update=200107.=E5=AF=BB=E6=89=BE?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E8=B7=AF=E5=BE=84.md=20for=20python?= =?UTF-8?q?=20and=20go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0107.寻找存在的路径.md for python and go --- ...50\347\232\204\350\267\257\345\276\204.md" | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 70339a9da1..06d0737738 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -217,8 +217,127 @@ class DisJoint{ ### Python +```python + +class UnionFind: + def __init__(self, size): + self.parent = list(range(size + 1)) # 初始化并查集 + + def find(self, u): + if self.parent[u] != u: + self.parent[u] = self.find(self.parent[u]) # 路径压缩 + return self.parent[u] + + def union(self, u, v): + root_u = self.find(u) + root_v = self.find(v) + if root_u != root_v: + self.parent[root_v] = root_u + + def is_same(self, u, v): + return self.find(u) == self.find(v) + + +def main(): + import sys + input = sys.stdin.read + data = input().split() + + index = 0 + n = int(data[index]) + index += 1 + m = int(data[index]) + index += 1 + + uf = UnionFind(n) + + for _ in range(m): + s = int(data[index]) + index += 1 + t = int(data[index]) + index += 1 + uf.union(s, t) + + source = int(data[index]) + index += 1 + destination = int(data[index]) + + if uf.is_same(source, destination): + print(1) + else: + print(0) + +if __name__ == "__main__": + main() + + +``` + + ### Go +```go + +package main + +import ( + "fmt" +) + +const MaxNodes = 101 + +var n int +var father [MaxNodes]int + +// 初始化并查集 +func initialize() { + for i := 1; i <= n; i++ { + father[i] = i + } +} + +// 并查集里寻根的过程 +func find(u int) int { + if u == father[u] { + return u + } + father[u] = find(father[u]) + return father[u] +} + +// 判断 u 和 v 是否找到同一个根 +func isSame(u, v int) bool { + return find(u) == find(v) +} + +// 将 v->u 这条边加入并查集 +func join(u, v int) { + rootU := find(u) + rootV := find(v) + if rootU != rootV { + father[rootV] = rootU + } +} + +func main() { + var m, s, t, source, destination int + fmt.Scan(&n, &m) + initialize() + for i := 0; i < m; i++ { + fmt.Scan(&s, &t) + join(s, t) + } + fmt.Scan(&source, &destination) + if isSame(source, destination) { + fmt.Println(1) + } else { + fmt.Println(0) + } +} + + +``` + ### Rust ### Javascript From aefb7aa83d8eb7ed08dd57e277edfd8c2a6b76ea Mon Sep 17 00:00:00 2001 From: HJHuangUM <57804285+Wogwan@users.noreply.github.com> Date: Sun, 18 Aug 2024 14:58:33 -0700 Subject: [PATCH 1266/1533] update kamacoder/0101 Python solution remove duplicate split() function --- ...62\233\347\232\204\346\200\273\351\235\242\347\247\257.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index a1793337f6..09146dcf42 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -190,10 +190,10 @@ int main() { from collections import deque # 处理输入 -n, m = list(map(int, input().strip().split())) +n, m = list(map(int, input().strip())) g = [] for _ in range(n): - row = list(map(int, input().strip().split())) + row = list(map(int, input().strip())) g.append(row) # 定义四个方向、孤岛面积(遍历完边缘后会被重置) From 418168efafe7e85f556e35b476aa8fbcb73f404b Mon Sep 17 00:00:00 2001 From: HJHuangUM <57804285+Wogwan@users.noreply.github.com> Date: Sun, 18 Aug 2024 18:05:53 -0700 Subject: [PATCH 1267/1533] update Python solutions for kama102 and kama104 Provide BFS solution --- ...11\346\262\241\345\255\244\345\262\233.md" | 57 ++++++++++++ ...00\345\244\247\345\262\233\345\261\277.md" | 87 +++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 2b16da0472..eec12e5fb8 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -140,6 +140,63 @@ int main() { ### Python +#### 广搜版 +```Python +from collections import deque + +n, m = list(map(int, input().split())) +g = [] +for _ in range(n): + row = list(map(int,input().split())) + g.append(row) + +directions = [(1,0),(-1,0),(0,1),(0,-1)] +count = 0 + +def bfs(r,c,mode): + global count + q = deque() + q.append((r,c)) + count += 1 + + while q: + r, c = q.popleft() + if mode: + g[r][c] = 2 + + for di in directions: + next_r = r + di[0] + next_c = c + di[1] + if next_c < 0 or next_c >= m or next_r < 0 or next_r >= n: + continue + if g[next_r][next_c] == 1: + q.append((next_r,next_c)) + if mode: + g[r][c] = 2 + + count += 1 + + +for i in range(n): + if g[i][0] == 1: bfs(i,0,True) + if g[i][m-1] == 1: bfs(i, m-1,True) + +for j in range(m): + if g[0][j] == 1: bfs(0,j,1) + if g[n-1][j] == 1: bfs(n-1,j,1) + +for i in range(n): + for j in range(m): + if g[i][j] == 2: + g[i][j] = 1 + else: + g[i][j] = 0 + +for row in g: + print(" ".join(map(str, row))) +``` + + ### Go ### Rust diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 7132217b53..cd647d5ee1 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -366,6 +366,93 @@ public class Main { ### Python + +#### BFS +```Python +from typing import List +from collections import defaultdict + +class Solution: + def __init__(self): + self.direction = [(1,0),(-1,0),(0,1),(0,-1)] + self.res = 0 + self.count = 0 + self.idx = 1 + self.count_area = defaultdict(int) + + def max_area_island(self, grid: List[List[int]]) -> int: + if not grid or len(grid) == 0 or len(grid[0]) == 0: + return 0 + + for i in range(len(grid)): + for j in range(len(grid[0])): + if grid[i][j] == 1: + self.count = 0 + self.idx += 1 + self.dfs(grid,i,j) + # print(grid) + self.check_area(grid) + # print(self.count_area) + + if self.check_largest_connect_island(grid=grid): + return self.res + 1 + return max(self.count_area.values()) + + def dfs(self,grid,row,col): + grid[row][col] = self.idx + self.count += 1 + for dr,dc in self.direction: + _row = dr + row + _col = dc + col + if 0<=_row Date: Wed, 21 Aug 2024 23:35:24 +0800 Subject: [PATCH 1268/1533] fix: typo --- ...\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index e904b9217e..c0a490b343 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -150,7 +150,7 @@ minDist数组数值初始化为int最大值。 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 * 源点到节点2的最短距离为1,小于原minDist[2]的数值max,更新minDist[2] = 1 -* 源点到节点3的最短距离为4,小于原minDist[3]的数值max,更新minDist[4] = 4 +* 源点到节点3的最短距离为4,小于原minDist[3]的数值max,更新minDist[3] = 4 可能有录友问:为啥和 minDist[2] 比较? From 7c76dafadade38406ba958136734928e7def2721 Mon Sep 17 00:00:00 2001 From: Yu Li <64682660+yooli23@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:45:48 -0400 Subject: [PATCH 1269/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixed a typo --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 36458ee6a7..e7146bcf5a 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -114,7 +114,7 @@ i 来表示物品、j表示背包容量。 背包容量为 2,只能放下物品1,背包里的价值为15。 -背包容量为 3,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包可以放物品2 或者 物品1,物品2价值更大,背包里的价值为20。 +背包容量为 3,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包可以放物品1 或者 物品0,物品1价值更大,背包里的价值为20。 背包容量为 4,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包都可都放下,背包价值为35。 From 051459a5426fc6ad44e069bbd7ab22b3f5d737b8 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Tue, 27 Aug 2024 10:45:45 +0800 Subject: [PATCH 1270/1533] =?UTF-8?q?feat:=20=E5=8B=95=E6=85=8B=E8=A6=8F?= =?UTF-8?q?=E5=8A=83leetcode#714=EF=BC=8C=E6=96=B0=E5=A2=9Epython=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...200\201\350\247\204\345\210\222\357\274\211.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 73714147e0..b0e8b141f5 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -188,6 +188,20 @@ class Solution: return max(dp[-1][0], dp[-1][1]) ``` +```python +class Solution: + def maxProfit(self, prices: List[int], fee: int) -> int: + # 持有股票手上的最大現金 + hold = -prices[0] - fee + # 不持有股票手上的最大現金 + not_hold = 0 + for price in prices[1:]: + new_hold = max(hold, not_hold - price - fee) + new_not_hold = max(not_hold, hold + price) + hold, not_hold = new_hold, new_not_hold + return not_hold +``` + ### Go: ```go From c5686b417d24a1d999d34ba9cf29d85ce77fe602 Mon Sep 17 00:00:00 2001 From: Jasen <141482690+JasenWn@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:32:59 +0800 Subject: [PATCH 1271/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00503.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II?= =?UTF-8?q?=E7=9A=84python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\244\247\345\205\203\347\264\240II.md" | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 62066d8521..5751bb9171 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -168,6 +168,7 @@ class Solution { ``` ### Python: +> 版本一: ```python class Solution: @@ -181,6 +182,34 @@ class Solution: stack.append(i%len(nums)) return dp ``` + +> 版本二:针对版本一的优化 + +```python3 +class Solution: + def nextGreaterElements(self, nums: List[int]) -> List[int]: + res = [-1] * len(nums) + stack = [] + #第一次遍历nums + for i, num in enumerate(nums): + while stack and num > nums[stack[-1]]: + res[stack[-1]] = num + stack.pop() + stack.append(i) + #此时stack仍有剩余,有部分数‘无下一个更大元素’待修正 + #第二次遍历nums + for num in nums: + #一旦stack为空,就表明所有数都有下一个更大元素,可以返回结果 + if not stack: + return res + while stack and num > nums[stack[-1]]: + res[stack[-1]] = num + stack.pop() + #不要将已经有下一个更大元素的数加入栈,这样会重复赋值,只需对第一次遍历剩余的数再尝试寻找下一个更大元素即可 + #最后仍有部分最大数无法有下一个更大元素,返回结果 + return res +``` + ### Go: ```go @@ -203,7 +232,6 @@ func nextGreaterElements(nums []int) []int { return result } ``` - ### JavaScript: ```JS From 574cef48b31fcb551c9bf15c66fea67cdce6beb4 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 29 Aug 2024 20:39:18 +0800 Subject: [PATCH 1272/1533] Update --- README.md | 2 +- ...33\346\225\260\344\271\213\345\222\214.md" | 59 +-- ...47\347\232\204\347\237\251\345\275\242.md" | 4 +- ...25\350\257\215\346\213\206\345\210\206.md" | 2 +- ...27\345\256\236\347\216\260\346\240\210.md" | 11 +- ...4.\347\233\256\346\240\207\345\222\214.md" | 366 +++++++++++++----- ...74\232dijkstra\346\234\264\347\264\240.md" | 2 +- .../0053.\345\257\273\345\256\235-prim.md" | 4 +- ...\347\211\251\350\277\220\350\276\223II.md" | 55 +++ ...60\351\207\217\345\271\277\346\220\234.md" | 70 ---- ...00\345\244\247\345\262\233\345\261\277.md" | 2 +- ...\344\275\231\350\277\236\346\216\245II.md" | 13 +- ...9.\345\256\214\347\276\216\346\225\260.md" | 0 ...75\344\272\214\345\217\211\346\240\221.md" | 0 ...57\345\276\204\350\256\241\346\225\260.md" | 95 +++++ ...27\344\270\255\344\275\215\346\225\260.md" | 68 ++++ ...40\351\231\244\344\273\243\344\273\267.md" | 106 +++++ ...30\346\226\227\345\272\217\345\210\227.md" | 68 ++++ ...01\345\244\215\346\235\202\345\272\246.md" | 59 +++ ...14\344\275\231\346\226\271\347\250\213.md" | 50 +++ ...64\346\225\260\344\271\230\346\263\225.md" | 62 +++ ...04\346\212\230\347\272\277\346\256\265.md" | 88 +++++ ...04\345\220\210\345\270\226\345\255\220.md" | 61 +++ ...16\347\211\210\346\226\271\346\241\210.md" | 154 ++++++++ .../\347\254\254\344\270\200\351\242\230.md" | 85 ++++ .../\347\254\254\344\270\211\351\242\230.md" | 76 ++++ .../\347\254\254\344\272\214\351\242\230.md" | 78 ++++ problems/qita/tulunfabu.md | 239 ++++++++++++ ...22\346\200\273\347\273\223\347\257\207.md" | 2 +- ...47\241\20001\350\203\214\345\214\205-1.md" | 31 +- 30 files changed, 1681 insertions(+), 231 deletions(-) rename "problems/kamacoder/\345\256\214\347\276\216\346\225\260.md" => "problems/kamacoder/0139.\345\256\214\347\276\216\346\225\260.md" (100%) rename "problems/kamacoder/\345\245\275\344\272\214\345\217\211\346\240\221.md" => "problems/kamacoder/0141.\345\245\275\344\272\214\345\217\211\346\240\221.md" (100%) create mode 100644 "problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" create mode 100644 "problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" create mode 100644 "problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" create mode 100644 "problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" create mode 100644 "problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" create mode 100644 "problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" create mode 100644 "problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" create mode 100644 "problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" create mode 100644 "problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" create mode 100644 "problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" create mode 100644 "problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" create mode 100644 "problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" create mode 100644 "problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" create mode 100644 problems/qita/tulunfabu.md diff --git a/README.md b/README.md index 96852d706d..53ea8dd048 100644 --- a/README.md +++ b/README.md @@ -373,7 +373,7 @@ ## 图论 -通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor +**[图论正式发布](./problems/qita/tulunfabu.md)** 1. [图论:理论基础](./problems/kamacoder/图论理论基础.md) 2. [图论:深度优先搜索理论基础](./problems/kamacoder/图论深搜理论基础.md) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 8e34713d37..64923e41fc 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -244,56 +244,61 @@ int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** return ### Java: ```Java -class Solution { +import java.util.*; + +public class Solution { public List> fourSum(int[] nums, int target) { - List> result = new ArrayList<>(); - Arrays.sort(nums); - - for (int i = 0; i < nums.length; i++) { - - // nums[i] > target 直接返回, 剪枝操作 - if (nums[i] > 0 && nums[i] > target) { - return result; + Arrays.sort(nums); // 排序数组 + List> result = new ArrayList<>(); // 结果集 + for (int k = 0; k < nums.length; k++) { + // 剪枝处理 + if (nums[k] > target && nums[k] >= 0) { + break; } - - if (i > 0 && nums[i - 1] == nums[i]) { // 对nums[i]去重 + // 对nums[k]去重 + if (k > 0 && nums[k] == nums[k - 1]) { continue; } - - for (int j = i + 1; j < nums.length; j++) { - - // nums[i]+nums[j] > target 直接返回, 剪枝操作 - if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) { - return result; - } - - if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重 + for (int i = k + 1; i < nums.length; i++) { + // 第二级剪枝 + if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) { + break; + } + // 对nums[i]去重 + if (i > k + 1 && nums[i] == nums[i - 1]) { continue; } - - int left = j + 1; + int left = i + 1; int right = nums.length - 1; while (right > left) { - // nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出 - long sum = (long) nums[i] + nums[j] + nums[left] + nums[right]; + long sum = (long) nums[k] + nums[i] + nums[left] + nums[right]; if (sum > target) { right--; } else if (sum < target) { left++; } else { - result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); + result.add(Arrays.asList(nums[k], nums[i], nums[left], nums[right])); // 对nums[left]和nums[right]去重 while (right > left && nums[right] == nums[right - 1]) right--; while (right > left && nums[left] == nums[left + 1]) left++; - - left++; right--; + left++; } } } } return result; } + + public static void main(String[] args) { + Solution solution = new Solution(); + int[] nums = {1, 0, -1, 0, -2, 2}; + int target = 0; + List> results = solution.fourSum(nums, target); + for (List result : results) { + System.out.println(result); + } + } } ``` diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 1c4d7f591f..c08a3045a5 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -188,13 +188,13 @@ public: 开头为什么要加元素0? -如果数组本身是降序的,例如 [8,6,4,2],在 8 入栈后,6 开始与8 进行比较,此时我们得到 mid(8),rigt(6),但是得不到 left。 +如果数组本身是降序的,例如 [8,6,4,2],在 8 入栈后,6 开始与8 进行比较,此时我们得到 mid(8),right(6),但是得不到 left。 (mid、left,right 都是对应版本一里的逻辑) 因为 将 8 弹出之后,栈里没有元素了,那么为了避免空栈取值,直接跳过了计算结果的逻辑。 -之后又将6 加入栈(此时8已经弹出了),然后 就是 4 与 栈口元素 8 进行比较,周而复始,那么计算的最后结果resutl就是0。 如图所示: +之后又将6 加入栈(此时8已经弹出了),然后 就是 4 与 栈口元素 6 进行比较,周而复始,那么计算的最后结果result就是0。 如图所示: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221164533.png) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 816892d5d7..29748e2780 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -243,7 +243,7 @@ public: 使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221123205105.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240809155103.png) 最后dp[s.size()] = 0 即 dp[13] = 0 ,而不是1,因为先用 "apple" 去遍历的时候,dp[8]并没有被赋值为1 (还没用"pen"),所以 dp[13]也不能变成1。 diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 8b9abd35a8..f0fe3a3c30 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -131,7 +131,7 @@ public: } }; ``` -* 时间复杂度: pop为O(n),其他为O(1) +* 时间复杂度: pop为O(n),top为O(n),其他为O(1) * 空间复杂度: O(n) ## 优化 @@ -147,17 +147,14 @@ class MyStack { public: queue que; - /** Initialize your data structure here. */ MyStack() { } - /** Push element x onto stack. */ void push(int x) { que.push(x); } - /** Removes the element on top of the stack and returns that element. */ int pop() { int size = que.size(); size--; @@ -170,9 +167,6 @@ public: return result; } - /** Get the top element. - ** Can not use back() direactly. - */ int top(){ int size = que.size(); size--; @@ -187,13 +181,12 @@ public: return result; } - /** Returns whether the stack is empty. */ bool empty() { return que.empty(); } }; ``` -* 时间复杂度: pop为O(n),其他为O(1) +* 时间复杂度: pop为O(n),top为O(n),其他为O(1) * 空间复杂度: O(n) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 33e3e953d6..82d330b727 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -49,7 +49,7 @@ * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -如果跟着「代码随想录」一起学过[回溯算法系列](https://programmercarl.com/回溯总结.html)的录友,看到这道题,应该有一种直觉,就是感觉好像回溯法可以爆搜出来。 +如果跟着「代码随想录」一起学过[回溯算法系列](https://programmercarl.com/回溯总结.html)的录友,看到这道题,应该有一种直觉,就是感觉好像回溯法可以暴搜出来。 事实确实如此,下面我也会给出相应的代码,只不过会超时。 @@ -118,9 +118,7 @@ public: 也可以使用记忆化回溯,但这里我就不在回溯上下功夫了,直接看动规吧 -### 动态规划 - -如何转化为01背包问题呢。 +### 动态规划 (二维dp数组) 假设加法的总和为x,那么减法对应的总和就是sum - x。 @@ -132,7 +130,7 @@ x = (target + sum) / 2 这里的x,就是bagSize,也就是我们后面要求的背包容量。 -大家看到(target + sum) / 2 应该担心计算的过程中向下取整有没有影响。 +大家看到`(target + sum) / 2` 应该担心计算的过程中向下取整有没有影响。 这么担心就对了,例如sum是5,target是2 的话其实就是无解的,所以: @@ -147,8 +145,6 @@ if ((target + sum) % 2 == 1) return 0; // 此时没有方案 if (abs(target) > sum) return 0; // 此时没有方案 ``` -再回归到01背包问题,为什么是01背包呢? - 因为每个物品(题目中的1)只用一次! 这次和之前遇到的背包问题不一样了,之前都是求容量为j的背包,最多能装多少。 @@ -157,60 +153,261 @@ if (abs(target) > sum) return 0; // 此时没有方案 1. 确定dp数组以及下标的含义 -dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 +先用 二维 dp数组求解本题,dp[i][j]:使用 下标为[0, i]的nums[i]能够凑满j(包括j)这么大容量的包,有dp[i][j]种方法。 -其实也可以使用二维dp数组来求解本题,dp[i][j]:使用 下标为[0, i]的nums[i]能够凑满j(包括j)这么大容量的包,有dp[i][j]种方法。 - -下面我都是统一使用一维数组进行讲解, 二维降为一维(滚动数组),其实就是上一层拷贝下来,这个我在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)也有介绍。 +01背包为什么这么定义dp数组,我在[0-1背包理论基础](https://www.programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-1.html)中 确定dp数组的含义里讲解过。 2. 确定递推公式 -有哪些来源可以推出dp[j]呢? +我们先手动推导一下,这个二维数组里面的数值。 -只要搞到nums[i],凑成dp[j]就有dp[j - nums[i]] 种方法。 +先只考虑物品0,如图: -例如:dp[j],j 为5, +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808161747.png) -* 已经有一个1(nums[i]) 的话,有 dp[4]种方法 凑成 容量为5的背包。 -* 已经有一个2(nums[i]) 的话,有 dp[3]种方法 凑成 容量为5的背包。 -* 已经有一个3(nums[i]) 的话,有 dp[2]种方法 凑成 容量为5的背包 -* 已经有一个4(nums[i]) 的话,有 dp[1]种方法 凑成 容量为5的背包 -* 已经有一个5 (nums[i])的话,有 dp[0]种方法 凑成 容量为5的背包 +(这里的所有物品,都是题目中的数字1)。 -那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。 +装满背包容量为0 的方法个数是1,即 放0件物品。 -所以求组合类问题的公式,都是类似这种: +装满背包容量为1 的方法个数是1,即 放物品0。 -``` -dp[j] += dp[j - nums[i]] -``` +装满背包容量为2 的方法个数是0,目前没有办法能装满容量为2的背包。 -**这个公式在后面在讲解背包解决排列组合问题的时候还会用到!** +接下来 考虑 物品0 和 物品1,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162052.png) + +装满背包容量为0 的方法个数是1,即 放0件物品。 + +装满背包容量为1 的方法个数是2,即 放物品0 或者 放物品1。 + +装满背包容量为2 的方法个数是1,即 放物品0 和 放物品1。 + +其他容量都不能装满,所以方法是0。 + +接下来 考虑 物品0 、物品1 和 物品2 ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162533.png) + +装满背包容量为0 的方法个数是1,即 放0件物品。 + +装满背包容量为1 的方法个数是3,即 放物品0 或者 放物品1 或者 放物品2。 + +装满背包容量为2 的方法个数是3,即 放物品0 和 放物品1、放物品0 和 物品 2、 放物品1 和 物品2。 + +装满背包容量为3的方法个数是1,即 放物品0 和 物品1 和 物品2。 + +通过以上举例,我们来看 dp[2][2] 可以有哪些方向推出来。 + +如图红色部分: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808163312.png) + +dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物品1 和 物品2, 如图所示,三种方法: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826111946.png) + +**容量为2 的背包,如果不放 物品2 有几种方法呢**? + +有 dp[1][2] 种方法,即 背包容量为2,只考虑物品0 和 物品1 ,有 dp[1][2] 种方法,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826112805.png) + +**容量为2 的背包, 如果放 物品2 有几种方法呢**? + +首先 要在背包里 先把物品2的容量空出来, 装满 刨除物品2容量 的背包 有几种方法呢? + +刨除物品2容量后的背包容量为 1。 + +此时装满背包容量为1 有 dp[1][1] 种方法,即: 不放物品2,背包容量为1,只考虑物品 0 和 物品 1,有 dp[1][1] 种方法。 + +如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826113043.png) + +有录友可能疑惑,这里计算的是放满 容量为2的背包 有几种方法,那物品2去哪了? + +在上面图中,你把物品2补上就好,同样是两种方法。 + +dp[2][2] = 容量为2的背包不放物品2有几种方法 + 容量为2的背包不放物品2有几种方法 + +所以 dp[2][2] = dp[1][2] + dp[1][1] ,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826113258.png) + +以上过程,抽象化如下: + +* **不放物品i**:即背包容量为j,里面不放物品i,装满有dp[i - 1][j]中方法。 + +* **放物品i**: 即:先空出物品i的容量,背包容量为(j - 物品i容量),放满背包有 dp[i - 1][j - 物品i容量] 种方法。 + +本题中,物品i的容量是nums[i],价值也是nums[i]。 + +递推公式:dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; + +考到这个递推公式,我们应该注意到,`j - nums[i]` 作为数组下标,如果 `j - nums[i]` 小于零呢? + +说明背包容量装不下 物品i,所以此时装满背包的方法值 等于 不放物品i的装满背包的方法,即:dp[i][j] = dp[i - 1][j]; + +所以递推公式: + +```CPP +if (nums[i] > j) dp[i][j] = dp[i - 1][j]; +else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; +``` 3. dp数组如何初始化 -从递推公式可以看出,在初始化的时候dp[0] 一定要初始化为1,因为dp[0]是在公式中一切递推结果的起源,如果dp[0]是0的话,递推结果将都是0。 +先明确递推的方向,如图,求解 dp[2][2] 是由 上方和左上方推出。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826115800.png) -这里有录友可能认为从dp数组定义来说 dp[0] 应该是0,也有录友认为dp[0]应该是1。 +那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: -其实不要硬去解释它的含义,咱就把 dp[0]的情况带入本题看看应该等于多少。 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827103507.png) -如果数组[0] ,target = 0,那么 bagSize = (target + sum) / 2 = 0。 dp[0]也应该是1, 也就是说给数组里的元素 0 前面无论放加法还是减法,都是 1 种方法。 +关于dp[0][0]的值,在上面的递推公式讲解中已经讲过,装满背包容量为0 的方法数量是1,即 放0件物品。 -所以本题我们应该初始化 dp[0] 为 1。 +那么最上行dp[0][j] 如何初始化呢? -可能有同学想了,那 如果是 数组[0,0,0,0,0] target = 0 呢。 +dp[0][j]:只放物品0, 把容量为j的背包填满有几种方法。 -其实 此时最终的dp[0] = 32,也就是这五个零 子集的所有组合情况,但此dp[0]非彼dp[0],dp[0]能算出32,其基础是因为dp[0] = 1 累加起来的。 +只有背包容量为 物品0 的容量的时候,方法为1,正好装满。 -dp[j]其他下标对应的数值也应该初始化为0,从递推公式也可以看出,dp[j]要保证是0的初始值,才能正确的由dp[j - nums[i]]推导出来。 +其他情况下,要不是装不满,要不是装不下。 +所以初始化:dp[0][nums[0]] = 1 ,其他均为0 。 + +表格最左列也要初始化,dp[i][0] : 背包容量为0, 放物品0 到 物品i,装满有几种方法。 + +都是有一种方法,就是放0件物品。 + +即 dp[i][0] = 1 4. 确定遍历顺序 -在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中,我们讲过对于01背包问题一维dp的遍历,nums放在外循环,target在内循环,且内循环倒序。 +在明确递推方向时,我们知道 当前值 是由上方和左上方推出。 + +那么我们的遍历顺序一定是 从上到下,从左到右。 + +因为只有这样,我们才能基于之前的数值做推导。 + +例如下图,如果上方没数值,左上方没数值,就无法推出 dp[2][2]。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827105427.png) + +那么是先 从上到下 ,再从左到右遍历,例如这样: + +```CPP +for (int i = 1; i < nums.size(); i++) { // 行,遍历物品 + for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 + } +} +``` + +还是先 从左到右,再从上到下呢,例如这样: + +```CPP +for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 + for (int i = 1; i < nums.size(); i++) { // 行,遍历物品 + } +} +``` + +**其实以上两种遍历都可以**! (但仅针对二维DP数组是这样的) + +这一点我在 [01背包理论基础](https://www.programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-1.html)中的 遍历顺序部分讲过。 + +这里我再画图讲一下,以求dp[2][2]为例,当先从上到下,再从左到右遍历,矩阵是这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827110933.png) + +当先从左到右,再从上到下遍历,矩阵是这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827111013.png) + +这里大家可以看出,无论是以上哪种遍历,都不影响 dp[2][2]的求值,用来 推导 dp[2][2] 的数值都在。 +5. 举例推导dp数组 + +输入:nums: [1, 1, 1, 1, 1], target: 3 + +bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 + +dp数组状态变化如下: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827111612.png) + +这么大的矩阵,我们是可以自己手动模拟出来的。 + +在模拟的过程中,既可以帮我们寻找规律,也可以帮我们验证 递推公式加遍历顺序是不是按照我们想象的结果推进的。 + + +最后二维dp数组的C++代码如下: + +```CPP +class Solution { +public: + int findTargetSumWays(vector& nums, int target) { + int sum = 0; + for (int i = 0; i < nums.size(); i++) sum += nums[i]; + if (abs(target) > sum) return 0; // 此时没有方案 + if ((target + sum) % 2 == 1) return 0; // 此时没有方案 + int bagSize = (target + sum) / 2; + + vector> dp(nums.size(), vector(bagSize + 1, 0)); + + // 初始化最上行 + if (nums[0] <= bagSize) dp[0][nums[0]] = 1; + + // 初始化最左列,最左列其他数值在递推公式中就完成了赋值 + dp[0][0] = 1; + + int numZero = 0; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] == 0) numZero++; + dp[i][0] = (int) pow(2.0, numZero); + } + + // 以下遍历顺序行列可以颠倒 + for (int i = 1; i < nums.size(); i++) { // 行,遍历物品 + for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 + if (nums[i] > j) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; + } + } + return dp[nums.size() - 1][bagSize]; + } +}; +``` + +### 动态规划 (一维dp数组) + +将二维dp数组压缩成一维dp数组,我们在 [01背包理论基础(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) 讲过滚动数组,原理是一样的,即重复利用每一行的数值。 + +既然是重复利用每一行,就是将二维数组压缩成一行。 + +dp[i][j] 去掉 行的维度,即 dp[j],表示:填满j(包括j)这么大容积的包,有dp[j]种方法。 + +2. 确定递推公式 + +二维DP数组递推公式: `dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]];` + +去掉维度i 之后,递推公式:`dp[j] = dp[j] + dp[j - nums[i]]` ,即:`dp[j] += dp[j - nums[i]]` + +**这个公式在后面在讲解背包解决排列组合问题的时候还会用到!** + +3. dp数组如何初始化 + +在上面 二维dp数组中,我们讲解过 dp[0][0] 初始为1,这里dp[0] 同样初始为1 ,即装满背包为0的方法有一种,放0件物品。 + +4. 确定遍历顺序 + +在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中,我们系统讲过对于01背包问题一维dp的遍历。 + +遍历物品放在外循环,遍历背包在内循环,且内循环倒序(为了保证物品只使用一次)。 + 5. 举例推导dp数组 输入:nums: [1, 1, 1, 1, 1], target: 3 @@ -221,7 +418,9 @@ dp数组状态变化如下: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210125120743274.jpg) -C++代码如下: +大家可以和 二维dp数组的打印结果做一下对比。 + +一维DP的C++代码如下: ```CPP class Solution { @@ -248,23 +447,51 @@ public: * 空间复杂度:O(m),m为背包容量 -## 总结 -此时 大家应该不禁想起,我们之前讲过的[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)是不是应该也可以用dp来做啊? +### 拓展 -是的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)要求的是把所有组合列出来,还是要使用回溯法爆搜的。 +关于一维dp数组的递推公式解释,也可以从以下维度来理解。 (**但还是从二维DP数组到一维DP数组这样更容易理解一些**) -本题还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为: +2. 确定递推公式 + +有哪些来源可以推出dp[j]呢? + +只要搞到nums[i],凑成dp[j]就有dp[j - nums[i]] 种方法。 + +例如:dp[j],j 为5, + +* 已经有一个1(nums[i]) 的话,有 dp[4]种方法 凑成 容量为5的背包。 +* 已经有一个2(nums[i]) 的话,有 dp[3]种方法 凑成 容量为5的背包。 +* 已经有一个3(nums[i]) 的话,有 dp[2]种方法 凑成 容量为5的背包 +* 已经有一个4(nums[i]) 的话,有 dp[1]种方法 凑成 容量为5的背包 +* 已经有一个5 (nums[i])的话,有 dp[0]种方法 凑成 容量为5的背包 + +那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。 + +所以求组合类问题的公式,都是类似这种: -```CPP -dp[j] += dp[j - nums[i]]; ``` +dp[j] += dp[j - nums[i]] +``` + + +## 总结 + +此时 大家应该不禁想起,我们之前讲过的[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)是不是应该也可以用dp来做啊? -后面我们在讲解完全背包的时候,还会用到这个递推公式! +是可以求的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)要求的是把所有组合列出来,还是要使用回溯法暴搜的。 +本题还是有点难度,理解上从二维DP数组更容易理解,做题上直接用一维DP更简洁一些。 +大家可以选择哪种方式自己更容易理解。 +在后面得题目中,在求装满背包有几种方法的情况下,递推公式一般为: +```CPP +dp[j] += dp[j - nums[i]]; +``` + +我们在讲解完全背包的时候,还会用到这个递推公式! ## 其他语言版本 @@ -359,13 +586,6 @@ class Solution { } } - // 打印dp数组 - // for(int i = 0; i < nums.length; i++) { - // for(int j = 0; j <= left; j++) { - // System.out.print(dp[i][j] + " "); - // } - // System.out.println(""); - // } return dp[nums.length - 1][left]; @@ -656,51 +876,3 @@ public class Solution - -class Solution { -public: - int findTargetSumWays(vector& nums, int target) { - int sum = 0; - for (int i = 0; i < nums.size(); i++) sum += nums[i]; - if (abs(target) > sum) return 0; // 此时没有方案 - if ((target + sum) % 2 == 1) return 0; // 此时没有方案 - int bagSize = (target + sum) / 2; - - vector> dp(nums.size(), vector(bagSize + 1, 0)); - - if (nums[0] <= bagSize) dp[0][nums[0]] = 1; - - dp[0][0] = 1; - - int numZero = 0; - for (int i = 0; i < nums.size(); i++) { - if (nums[i] == 0) numZero++; - dp[i][0] = (int) pow(2.0, numZero); - } - - for (int i = 1; i < nums.size(); i++) { - for (int j = 0; j <= bagSize; j++) { - if (nums[i] > j) dp[i][j] = dp[i - 1][j]; - else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; - } - } - for (int i = 0; i < nums.size(); i++) { - for (int j = 0; j <= bagSize; j++) { - cout << dp[i][j] << " "; - } - cout << endl; - } - return dp[nums.size() - 1][bagSize]; - } -}; - -1 1 0 0 0 -1 2 1 0 0 -1 3 3 1 0 -1 4 6 4 1 -1 5 10 10 5 - -初始化 如果没有0, dp[i][0] = 1; 即所有元素都不取。 - -用元素 取与不取来举例 - diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index e904b9217e..c0a490b343 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -150,7 +150,7 @@ minDist数组数值初始化为int最大值。 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 * 源点到节点2的最短距离为1,小于原minDist[2]的数值max,更新minDist[2] = 1 -* 源点到节点3的最短距离为4,小于原minDist[3]的数值max,更新minDist[4] = 4 +* 源点到节点3的最短距离为4,小于原minDist[3]的数值max,更新minDist[3] = 4 可能有录友问:为啥和 minDist[2] 比较? diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index 7ad050c780..c71624b55a 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -176,7 +176,7 @@ minDist 数组 里的数值初始化为 最大数,因为本题 节点距离不 所有非生成树的节点距离 最小生成树(节点1、节点2、节点3 )的距离都已经跟新了 。 -* 节点 4 和 节点 3的距离为 1,和原先的距离值 2 小,所以更新minDist[3]为1。 +* 节点 4 和 节点 3的距离为 1,和原先的距离值 2 小,所以更新minDist[4]为1。 上面为什么我们只比较 节点4 和 节点3 的距离呢? @@ -213,7 +213,7 @@ minDist 数组 里的数值初始化为 最大数,因为本题 节点距离不 minDist数组已经更新了 所有非生成树的节点距离 最小生成树(节点1、节点2、节点3、节点4 )的距离 。 -* 节点 5 和 节点 4的距离为 1,和原先的距离值 2 小,所以更新minDist[4]为1。 +* 节点 5 和 节点 4的距离为 1,和原先的距离值 2 小,所以更新minDist[5]为1。 ### 6 diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 5bc4be7b22..9bece92b2b 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -247,6 +247,61 @@ int main() { ### Python +```python +import sys + +def main(): + input = sys.stdin.read + data = input().split() + index = 0 + + n = int(data[index]) + index += 1 + m = int(data[index]) + index += 1 + + grid = [] + for i in range(m): + p1 = int(data[index]) + index += 1 + p2 = int(data[index]) + index += 1 + val = int(data[index]) + index += 1 + # p1 指向 p2,权值为 val + grid.append([p1, p2, val]) + + start = 1 # 起点 + end = n # 终点 + + minDist = [float('inf')] * (n + 1) + minDist[start] = 0 + flag = False + + for i in range(1, n + 1): # 这里我们松弛n次,最后一次判断负权回路 + for side in grid: + from_node = side[0] + to = side[1] + price = side[2] + if i < n: + if minDist[from_node] != float('inf') and minDist[to] > minDist[from_node] + price: + minDist[to] = minDist[from_node] + price + else: # 多加一次松弛判断负权回路 + if minDist[from_node] != float('inf') and minDist[to] > minDist[from_node] + price: + flag = True + + if flag: + print("circle") + elif minDist[end] == float('inf'): + print("unconnected") + else: + print(minDist[end]) + +if __name__ == "__main__": + main() + +``` + ### Go ### Rust diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 3c069b4412..a5a40162ac 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -192,50 +192,6 @@ int main() { ```java -import java.util.Scanner; - -public class Main { - static int[][] dir = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} }; // 四个方向 - - public static void dfs(int[][] grid, boolean[][] visited, int x, int y) { - for (int i = 0; i < 4; i++) { - int nextx = x + dir[i][0]; - int nexty = y + dir[i][1]; - if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过 - if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 没有访问过的 同时 是陆地的 - visited[nextx][nexty] = true; - dfs(grid, visited, nextx, nexty); - } - } - } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int n = scanner.nextInt(); - int m = scanner.nextInt(); - int[][] grid = new int[n][m]; - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - grid[i][j] = scanner.nextInt(); - } - } - - boolean[][] visited = new boolean[n][m]; - - int result = 0; - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - if (!visited[i][j] && grid[i][j] == 1) { - visited[i][j] = true; - result++; // 遇到没访问过的陆地,+1 - dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true - } - } - } - - System.out.println(result); - } -} @@ -246,32 +202,6 @@ public class Main { ```python -def dfs(grid, visited, x, y): - dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 四个方向 - for d in dir: - nextx, nexty = x + d[0], y + d[1] - if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]): - if not visited[nextx][nexty] and grid[nextx][nexty] == 1: # 没有访问过的 同时 是陆地的 - visited[nextx][nexty] = True - dfs(grid, visited, nextx, nexty) - -def main(): - n, m = map(int, input().split()) - grid = [list(map(int, input().split())) for _ in range(n)] - visited = [[False] * m for _ in range(n)] - - result = 0 - for i in range(n): - for j in range(m): - if not visited[i][j] and grid[i][j] == 1: - visited[i][j] = True - result += 1 # 遇到没访问过的陆地,+1 - dfs(grid, visited, i, j) # 将与其链接的陆地都标记上 True - - print(result) - -if __name__ == "__main__": - main() diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 7d67b7fcb1..c08434f941 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -17,7 +17,7 @@ 输出描述: -输出一个整数,表示最大的岛屿面积。如果矩阵中不存在岛屿,则输出 0。 +输出一个整数,表示最大的岛屿面积。 输入示例: diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index b13daec7bf..8c20a0e08f 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -7,16 +7,21 @@ 题目描述 -有向树指满足以下条件的有向图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。有向树拥有 n 个节点和 n - 1 条边。 +有一种有向树,该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。有向树拥有 n 个节点和 n - 1 条边。如图:  + -输入一个有向图,该图由一个有着 n 个节点(节点编号 从 1 到 n),n 条边,请返回一条可以删除的边,使得删除该条边之后该有向图可以被当作一颗有向树。 +现在有一个有向图,有向图是在有向树中的两个没有直接链接的节点中间添加一条有向边。如图: + + + +输入一个有向图,该图由一个有着 n 个节点(节点编号 从 1 到 n),n 条边,请返回一条可以删除的边,使得删除该条边之后该有向图可以被当作一颗有向树。 输入描述 第一行输入一个整数 N,表示有向图中节点和边的个数。 -后续 N 行,每行输入两个整数 s 和 t,代表 s 节点有一条连接 t 节点的单向边 +后续 N 行,每行输入两个整数 s 和 t,代表这是 s 节点连接并指向 t 节点的单向边 输出描述 @@ -37,7 +42,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527112633.png) + 在删除 2 3 后有向图可以变为一棵合法的有向树,所以输出 2 3 diff --git "a/problems/kamacoder/\345\256\214\347\276\216\346\225\260.md" "b/problems/kamacoder/0139.\345\256\214\347\276\216\346\225\260.md" similarity index 100% rename from "problems/kamacoder/\345\256\214\347\276\216\346\225\260.md" rename to "problems/kamacoder/0139.\345\256\214\347\276\216\346\225\260.md" diff --git "a/problems/kamacoder/\345\245\275\344\272\214\345\217\211\346\240\221.md" "b/problems/kamacoder/0141.\345\245\275\344\272\214\345\217\211\346\240\221.md" similarity index 100% rename from "problems/kamacoder/\345\245\275\344\272\214\345\217\211\346\240\221.md" rename to "problems/kamacoder/0141.\345\245\275\344\272\214\345\217\211\346\240\221.md" diff --git "a/problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" "b/problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" new file mode 100644 index 0000000000..2c5562b392 --- /dev/null +++ "b/problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" @@ -0,0 +1,95 @@ + + +# 权值优势路径计数 + +[题目链接](https://kamacoder.com/problempage.php?pid=1231) + +1、构建二叉树:首先根据层序遍历的序列构建二叉树。这可以通过使用队列来实现,队列中存储当前节点及其索引,确保可以正确地将子节点添加到父节点下。 + +2、路径遍历:使用深度优先搜索(DFS)遍历所有从根到叶子的路径。在遍历过程中,维护一个计数器跟踪当前路径中权值为 1 和权值为 0 的节点的数量。 + +3、计数满足条件的路径:每当到达一个叶子节点时,检查当前路径的权值 1 的节点数量是否比权值 0 的节点数量多 1。如果满足,递增一个全局计数器。 + + +```CPP + +#include +#include +#include + +using namespace std; + +struct TreeNode { + int val; + TreeNode *left, *right; + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} +}; + +// DFS遍历二叉树,并计算满足条件的路径数量 +void countPaths(TreeNode* node, int count1, int count0, int& result) { + if (!node) return; + + // 更新当前路径中1和0的数量 + node->val == 1 ? count1++ : count0++; + + // 检查当前节点是否为叶子节点 + if (!node->left && !node->right) { + // 检查1的数量是否比0的数量多1 + if (count1 == count0 + 1) { + result++; + } + return; + } + + // 递归访问左右子节点 + countPaths(node->left, count1, count0, result); + countPaths(node->right, count1, count0, result); +} + +int main() { + int N; + cin >> N; + + vector nums(N); + for (int i = 0; i < N; ++i) { + cin >> nums[i]; + } + + if (nums.empty()) { + cout << 0 << endl; + return 0; + } + + // 根据层序遍历的输入构建二叉树 + queue q; + TreeNode* root = new TreeNode(nums[0]); + q.push(root); + int index = 1; + + while (!q.empty() && index < N) { + TreeNode* node = q.front(); + q.pop(); + + if (index < N && nums[index] != -1) { + node->left = new TreeNode(nums[index]); + q.push(node->left); + } + index++; + + if (index < N && nums[index] != -1) { + node->right = new TreeNode(nums[index]); + q.push(node->right); + } + index++; + } + + // 计算满足条件的路径数 + int result = 0; + countPaths(root, 0, 0, result); + + cout << result << endl; + + return 0; +} + +``` diff --git "a/problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" "b/problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" new file mode 100644 index 0000000000..90e5b7a413 --- /dev/null +++ "b/problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" @@ -0,0 +1,68 @@ + +# 序列中位数 + +[题目链接](https://kamacoder.com/problempage.php?pid=1232) + +注意给的数组默认不是有序的! + +模拟题,排序之后,取中位数,然后按照b数组 删 a数组中元素,再取中位数。 + +```CPP +#include +using namespace std; + +// 计算并返回中位数 +double findMedian(vector& nums) { + int n = nums.size(); + if (n % 2 == 1) { + return nums[n / 2]; // 奇数长度,返回中间的元素 + } else { + // 偶数长度,返回中间两个元素的平均值 + return (nums[n / 2] + nums[n / 2 - 1]) / 2.0; + } +} + + +int main(){ + int t; + cin >> t; + while(t--){ + int n; + cin>> n; + vector a(n); + vector b(n - 1); + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + for(int i = 0; i < n - 1; i++){ + cin >> b[i]; + } + vector nums = a; + vector answers; + + sort(nums.begin(), nums.end()); + + // 把中位数放进结果集 + answers.push_back(findMedian(nums)); + + for(int i = 0; i < n - 1; i++){ + + int target = a[b[i]]; + // 删除目标值 + nums.erase(find(nums.begin(), nums.end(), target)); + // 把中位数放进结果集 + answers.push_back(findMedian(nums)); + + } + + for(auto answer : answers){ + // 判断是否是整数 + if(answer == (int)answer) printf("%d ", (int)answer); + else printf("%.1f ", answer); + } + cout << endl; + } + +} + +``` diff --git "a/problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" "b/problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" new file mode 100644 index 0000000000..d79d695548 --- /dev/null +++ "b/problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" @@ -0,0 +1,106 @@ + +# 最小化频率的删除代价 + +[题目链接](https://kamacoder.com/problempage.php?pid=1233) + +计数和排序: + +* 使用 map 或 unordered_map 对数组 a 中每个元素出现的次数进行统计。 +* 将统计结果存入一个 vector>,其中 pair 的第一个元素是元素的出现次数,第二个元素是元素本身。 +* 按出现次数从大到小排序这个 vector。 + +确定最小 f(a): + +* 从最大出现次数开始尝试减少 f(a)。为此,从最高频次的元素开始逐步向下考虑较少出现的元素,计算达到更低 f(a) 所需删除的元素数量。 +* 使用一个累加器 count 来记录需要删除的元素数量,直到这个数量超过允许的最大删除数量 k 或恰好等于 k。在此过程中,尽量使 f(a) 达到最小。 + +计算达到 f(a) 的代价: + +* 计算完成后,需要确定达到最小 f(a) 的确切代价。首先,为每个元素确定在不超过 k 的前提下可以删除的最大数量,以使得 f(a) 最小。 +* 对于每个元素,如果它的数量超过了新的 f(a),则计算减少到 f(a) 所需删除的具体元素数,记录下来。 + +计算具体删除代价: + +* 遍历原数组,对于每个需要删除的元素,根据其位置累加删除代价。每删除一个元素,相应地减少其在删除列表中的计数。当某元素需要删除的数量减至 0 时,从删除列表中移除该元素。 + + +```CPP + +#include +#include +#include +#include + +using namespace std; + +int main() { + int n, k; + cin >> n >> k; + + vector a(n); + for (int i = 0; i < n; ++i) { + cin >> a[i]; + } + + unordered_map umap; // 使用map来统计每个元素的出现频率 + for (int i = 0; i < n; ++i) { + umap[a[i]]++; // 统计每个元素的出现次数 + } + + vector> table; + for (auto& pair : umap) { + table.push_back({pair.second, pair.first}); // 将元素和其频率作为一个pair放入table中 + } + + sort(table.begin(), table.end(), greater<>()); // 将table按照频率从大到小排序 + + int count = 0; // 用来计算已经删除的元素总数 + int minVal = table[0].first; // 从最高频率开始 + for (int i = 0; i < table.size(); ++i) { + int freq = table[i].first; + count += (minVal - freq) * i; // 累加删除元素的代价 + if (count > k) break; // 如果超过了k,停止循环 + else if (count == k) { + minVal = freq; + break; + } else minVal = freq; + } + if (count < k) { + int addDel = (k - count) / table.size(); // 如果删除的代价还没达到k,计算还可以进一步减少的频率 + minVal -= addDel; // 减少相应的频率 + } + + if (minVal < 0) { + minVal = 0; // 确保最小频率值不小于0 + } + + unordered_map deleteList; // 用来存储需要删除的元素及其数量 + for (auto& elem : table) { + int num = elem.first; + int ind = elem.second; + if (num > minVal) { + deleteList[ind] = num - minVal; // 如果元素频率大于最小值,计算需要删除的数量 + } else { + break; + } + } + + int cost = 0; // 计算总的删除代价 + for (int i = 0; i < n; ++i) { + if (deleteList.find(a[i]) != deleteList.end()) { + cost += i + 1; // 删除的代价是元素的索引+1 + deleteList[a[i]]--; // 删除一个元素 + if (deleteList[a[i]] == 0) { + deleteList.erase(a[i]); // 如果元素已经全部删除,从列表中移除 + if (deleteList.empty()) { + break; // 如果没有元素需要删除了,结束循环 + } + } + } + } + + cout << minVal << " " << cost << endl; + return 0; +} + +``` diff --git "a/problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" "b/problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" new file mode 100644 index 0000000000..b7bea97458 --- /dev/null +++ "b/problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" @@ -0,0 +1,68 @@ + +# 勇敢牛牛战斗序列 + +[题目链接](https://kamacoder.com/problempage.php?pid=1234) + +贪心思路,对数组从小到大排序之后,先取最右边,再取最左边,循环反复。 + +```CPP +#include + +using namespace std; + +int main() { + int n; + cin >> n; + vector a(n); // 使用 vector 存储整数数组 + for (int i = 0; i < n; i++) { + cin >> a[i]; // 读取数组 + } + sort(a.begin(), a.end()); // 对数组进行排序 + + long long ans = 0; // 使用 long long 存储结果,以防溢出 + int cur = 0; + int left = 0, right = n - 1; + while (left <= right) { + if (cur < a[right]) { + ans += a[right] - cur; + } + cur = a[left]; + right--; + left++; + } + cout << ans << endl; // 输出结果 + return 0; +} +``` + + + +```Java +import java.util.Arrays; +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = sc.nextInt(); + } + Arrays.sort(a); + long ans = 0; + int cur = 0; + int left = 0, right = a.length - 1; + while (left <= right) { + if (cur < a[right]) { + ans = ans + a[right] - cur; + } + cur = a[left]; + right--; + left++; + } + System.out.println(ans); + } +} +``` diff --git "a/problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" "b/problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" new file mode 100644 index 0000000000..9ca4630df1 --- /dev/null +++ "b/problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" @@ -0,0 +1,59 @@ + +# 最大化密码复杂度 + +[题目链接](https://kamacoder.com/problempage.php?pid=1235) + +注意**边界处理**,对于字符串的首尾位置,需要特别处理,因为它们只有一个相邻字符。 +* 遍历字符串 s,寻找 '?' 字符。 +* 对于每个 '?' 字符,选择一个字符填充,使其与前后字符都不同。这样做的目的是最大化密码的复杂度,即尽可能使相邻的字符不同。 +* 如果 '?' 是第一个或最后一个字符,或者无法找到与前后都不同的字符,选择与前一个或后一个字符不同的字符。 + + +```CPP +#include +#include +#include + +using namespace std; + +int main() { + int n, m; + string s; + cin >> n >> m >> s; + + if (n == 1) { + cout << 0 << endl; + return 0; + } + + // 统一处理包括左右字符的情况 + for (int i = 0; i < n; ++i) { + if (s[i] == '?') { + bool found = false; + for (char j = 'a'; j < 'a' + m; ++j) { + // 避免第一个字符 和 最后一个字符,因为两个字符只有一个相邻字符,没有左右相邻字符 + if ((i == 0 || s[i - 1] != j) && (i == n - 1 || s[i + 1] != j)) { + s[i] = j; + found = true; + break; + } + } + // 如果没有找到合适的字符,就和附近字符保持一致 + if (!found) { + if (i > 0) s[i] = s[i - 1]; + else s[i] = s[i + 1]; + } + } + } + + // 计算结果 + int result = 0; + for (int i = 0; i < n - 1; ++i) { + if (s[i] != s[i + 1]) result++; + } + + cout << result << endl; + return 0; +} + +``` diff --git "a/problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" "b/problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" new file mode 100644 index 0000000000..b99481d2de --- /dev/null +++ "b/problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" @@ -0,0 +1,50 @@ + +# 同余方程 + +题目链接:https://kamacoder.com/problempage.php?pid=1236 + +我们需要求出满足以下条件的最小正整数 x:`ax≡1 (mod b)` + +这意味着我们需要找到 x 使得 ax 除以 b 的余数是 1。这个问题实际上是一个典型的 模反元素 问题。 + +解题思路: + +* 为了求出最小的 x,我们可以使用 扩展欧几里得算法 来求出 a 对模 b 的逆元。 +* 这个算法能够求解 ax + by = gcd(a, b) 的一组整数解 (x, y),而在 gcd(a, b) = 1 的情况下,x 即为所求的模逆元。 +* 扩展欧几里得算法:扩展欧几里得算法可以通过递归或者迭代的方式实现。 + +下面给出C++代码实现: + +```CPP +#include +using namespace std; + +// 扩展欧几里得:计算 ax + by = gcd(a, b) 的解 +long long extended_gcd(long long a, long long b, long long &x, long long &y) { + if (b == 0) { + x = 1; + y = 0; + return a; + } + long long x1, y1; + long long gcd = extended_gcd(b, a % b, x1, y1); + x = y1; + y = x1 - (a / b) * y1; + return gcd; +} + +int main() { + long long a, b; + cin >> a >> b; + + long long x, y; + long long gcd = extended_gcd(a, b, x, y); + + // 由于我们只需要模 b 的正整数解,所以我们要保证 x 是正数 + x = (x % b + b) % b; + + cout << x << endl; + + return 0; +} +``` diff --git "a/problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" "b/problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" new file mode 100644 index 0000000000..642cb74694 --- /dev/null +++ "b/problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" @@ -0,0 +1,62 @@ + +# 大整数乘法 + +题目链接:https://kamacoder.com/problempage.php?pid=1237 + +思路: + +我们可以使用模拟手算乘法的方法,即「逐位相乘累加」,对于每一位的乘法结果,我们将其加到相应的结果位置上。最终将累加的结果输出。 + +具体步骤: + +* 初始化结果数组:结果数组的长度应该是两个数字长度之和,因为最大长度的结果不会超过这个长度。 +* 逐位相乘:从右往左遍历两个字符串的每一位,逐位相乘,并加到结果数组的相应位置。 +* 处理进位:在每一步累加之后处理进位,保证每个位置的值小于10。 + +将结果数组转化为字符串:从结果数组的最高位开始,忽略前导零,然后将数组转化为字符串。 + +```CPP +#include +#include +#include + +using namespace std; + +string multiply(string num1, string num2) { + int len1 = num1.size(); + int len2 = num2.size(); + vector result(len1 + len2, 0); + + // 逐位相乘 + for (int i = len1 - 1; i >= 0; i--) { + for (int j = len2 - 1; j >= 0; j--) { + int mul = (num1[i] - '0') * (num2[j] - '0'); + int sum = mul + result[i + j + 1]; + + result[i + j + 1] = sum % 10; + result[i + j] += sum / 10; + } + } + + // 将结果转换为字符串,跳过前导零 + string product; + for (int num : result) { + if (!(product.empty() && num == 0)) { // 跳过前导零 + product.push_back(num + '0'); + } + } + + return product.empty() ? "0" : product; +} + +int main() { + string num1, num2; + cin >> num1 >> num2; + + string result = multiply(num1, num2); + cout << result << endl; + + return 0; +} + +``` diff --git "a/problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" "b/problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" new file mode 100644 index 0000000000..35c3ea3283 --- /dev/null +++ "b/problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" @@ -0,0 +1,88 @@ + +# 二维平面上的折线段 + +题目链接:https://kamacoder.com/problempage.php?pid=1238 + +这个问题要求我们在一条折线段上,根据移动的固定距离 s 进行标记点的计算。 + +为了实现这一点,我们需要对折线段进行分段处理,并根据每段的长度来确定标记点的位置。 + +解题思路: + +1. 输入与初步处理: + * 首先,读取所有点的坐标。 + * 计算每一段折线的长度,并逐段累积总长度。 +2. 确定标记点: + * 从起点开始,每次沿着折线段前进 s 的距离,直到到达终点。 + * 对于每个标记点,根据当前段的起点和终点,计算出该点的精确坐标。 +3. 输出所有标记点的坐标,格式为 x, y。 + +```CPP + +#include +#include +#include +#include + +using namespace std; + +// 定义一个点的结构体 +struct Point { + double x, y; +}; + +// 计算两点之间的距离 +double distance(const Point& a, const Point& b) { + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + +int main() { + int n; + cin >> n; + + vector points(n); + for (int i = 0; i < n; i++) { + cin >> points[i].x >> points[i].y; + } + + double s; + cin >> s; + + double total_length = 0.0; + vector segment_lengths(n - 1); + + // 计算每段长度和总长度 + for (int i = 0; i < n - 1; i++) { + segment_lengths[i] = distance(points[i], points[i + 1]); + total_length += segment_lengths[i]; + } + + // 从起点开始标记 + Point current_point = points[0]; + double accumulated_distance = 0.0; + + cout << fixed << setprecision(5); + cout << current_point.x << ", " << current_point.y << endl; + + while (accumulated_distance + s <= total_length) { + accumulated_distance += s; + double remaining_distance = accumulated_distance; + + for (int i = 0; i < n - 1; i++) { + if (remaining_distance <= segment_lengths[i]) { + double ratio = remaining_distance / segment_lengths[i]; + double new_x = points[i].x + ratio * (points[i + 1].x - points[i].x); + double new_y = points[i].y + ratio * (points[i + 1].y - points[i].y); + current_point = {new_x, new_y}; + cout << current_point.x << ", " << current_point.y << endl; + break; + } else { + remaining_distance -= segment_lengths[i]; + } + } + } + + return 0; +} + +``` diff --git "a/problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" "b/problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" new file mode 100644 index 0000000000..84471f8cd6 --- /dev/null +++ "b/problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" @@ -0,0 +1,61 @@ + +# 讨厌鬼的组合帖子 + +[题目链接](https://kamacoder.com/problempage.php?pid=1239) + +这个问题本质上是要找到两个数组的子集,使得这两个子集之间的差的绝对值最大。 + +问题可以简化为寻找两个数列之间最大可能的差的绝对值。 + +贪心思路如下: + +计算差异,首先,我们可以计算每个帖子的点赞数和点踩数的差值 d[i] = a[i] - b[i]。这样问题就转化为选择这些差值的一个子集,使得子集中所有元素的和的绝对值最大。 + +遍历可能性,要使得一个数的绝对值尽可能大,可以尝试最大化这个数,或者最小化这个数(使其尽可能小于零)。我们可以分别尝试将所有正的差值加在一起,以及将所有负的差值加在一起。 + +计算最大吸引度: + +* 将所有正的差值求和得到一个总和。 +* 将所有负的差值求和得到另一个总和。 +* 最后,吸引度即为这两个总和的绝对值中的较大者。 + + +```CPP + +#include +#include +#include + +using namespace std; + +int main() { + int n; + cin >> n; + + vector a(n), b(n); + for (int i = 0; i < n; ++i) { + cin >> a[i]; + } + for (int i = 0; i < n; ++i) { + cin >> b[i]; + } + + long long positive_sum = 0; + long long negative_sum = 0; + + for (int i = 0; i < n; ++i) { + int difference = a[i] - b[i]; + if (difference > 0) { + positive_sum += difference; + } else if (difference < 0) { + negative_sum += difference; + } + } + + // 最大吸引度是正总和或负总和的绝对值中的较大者 + cout << max(abs(positive_sum), abs(negative_sum)) << endl; + + return 0; +} +``` + diff --git "a/problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" "b/problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" new file mode 100644 index 0000000000..6b82b74b6f --- /dev/null +++ "b/problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" @@ -0,0 +1,154 @@ + +# 小红的第16版方案 + +[题目链接](https://kamacoder.com/problempage.php?pid=1240) + +暴力解法: (数据量已经出最大了,C++能过,java、python、go都过不了) + +```CPP +#include +using namespace std; +int main() { + int n, m; + int l, r; + cin >> n >> m; + vector a(n + 1); + vector angry(n + 1); + for (int i = 1; i <= n; i++) cin >> a[i]; + for (int i = 1; i <= m; i++) { + cin >> l >> r; + for (int j = l; j <= r; j++) { + angry[j]++; + if (angry[j] > a[j]) { + cout << i - 1 << endl; + return 0; + } + } + } + cout << m << endl; + return 0; +} +``` + +使用 差分数组,代码如下: + + +```CPP +#include +#include +using namespace std; + +int main() { + int n, m; + cin >> n >> m; + + vector a(n + 1); + for (int i = 1; i <= n; ++i) { + cin >> a[i]; + } + + vector diff(n + 1, 0); // 差分数组,多一个元素用于处理边界情况 + + int l, r; + for (int i = 1; i <= m; ++i) { + cin >> l >> r; + diff[l]++; + if (r + 1 <= n) diff[r + 1]--; + } + + int current_anger = 0; // 当前的愤怒值 + for (int i = 1; i <= n; ++i) { + current_anger += diff[i]; // 计算差分数组的前缀和,得到最终的愤怒值 + if (current_anger > a[i]) { + cout << i - 1 << endl; // 如果当前的愤怒值超过阈值,输出最后一个没有问题的方案编号 + return 0; + } + } + + cout << m << endl; // 如果所有修改完成后都没有超过阈值,返回最后一个方案的编号 + return 0; +} +``` + +过不了,因为差分数组只能知道是哪个人超过了阈值,不能知道是第几次修改超过的 + +最后 优化思路: + +* 差分数组(Difference Array):依然使用差分数组来处理区间更新。 +* 二分查找:通过二分查找来确定最早发生愤怒值超出阈值的操作,而不是逐次模拟每一次修改。 + +步骤: + +* 创建一个差分数组 diff 用于处理区间增加操作。 +* 在 [1, m] 的范围内进行二分查找,确定导致某个人愤怒值超过阈值的最早的修改次数。 +* 对每个二分查找的中间值 mid,我们累积应用前 mid 次操作,然后检查是否有任何人的愤怒值超过了阈值。 +* 如果 mid 之前没有超标,则继续向右查找;否则向左缩小范围。 +* 在二分查找完成后,输出找到的第一个导致愤怒值超标的操作次数。 + +```CPP +#include +#include +#include + +using namespace std; + +bool isValid(const vector& a, const vector& diff, int n, int m) { + vector anger(n + 1, 0); + int current_anger = 0; + for (int i = 1; i <= n; ++i) { + current_anger += diff[i]; + if (current_anger > a[i]) { + return false; // 超出愤怒阈值 + } + } + return true; // 没有任何人超出愤怒阈值 +} + +int main() { + int n, m; + cin >> n >> m; + + vector a(n + 1); // 愤怒阈值数组 + for (int i = 1; i <= n; ++i) { + cin >> a[i]; + } + + vector> operations(m + 1); // 保存每次操作的区间 + for (int i = 1; i <= m; ++i) { + int l, r; + cin >> l >> r; + operations[i] = {l, r}; + } + + int left = 1, right = m, result = m; + + while (left <= right) { + int mid = left + (right - left) / 2; + + // 构建差分数组,只考虑前 mid 次操作 + vector diff(n + 2, 0); + for (int i = 1; i <= mid; ++i) { + int l = operations[i].first; + int r = operations[i].second; + diff[l]++; + if (r + 1 <= n) { + diff[r + 1]--; + } + } + + if (isValid(a, diff, n, mid)) { + left = mid + 1; // 如果在mid次操作后没有超标,继续向右搜索 + } else { + result = mid - 1; // 如果在mid次操作后超标,向左搜索 + right = mid - 1; + } + } + + cout << result << endl; + return 0; +} + +``` + +* 时间复杂度:O(n + m * log m),其中 n 是成员数量,m 是操作次数。二分查找的时间复杂度为 O(log m),每次二分查找中通过差分数组检查愤怒值的复杂度为 O(n)。 +* 空间复杂度:O(n + m),主要用于存储差分数组和操作数组。 diff --git "a/problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" "b/problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" new file mode 100644 index 0000000000..989296845d --- /dev/null +++ "b/problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" @@ -0,0 +1,85 @@ + + +## 解题思路 + +1、初始分析 + +- 给定一个排列 `p`,我们首先构建一个 `pos` 数组,使得 `pos[i]` 表示 `i` 在排列 `p` 中的位置。 +- 我们需要判断数组 `a` 是否是一个优秀数组,即 `pos[a[i]] < pos[a[i+1]] <= pos[a[i]] + d` 对于所有 `i` 都成立。 +- 我们的目标是通过最少的相邻元素交换,使得数组 `a` 不再是一个优秀数组。 + +2、思路 + +- 要使数组 `a` 不再是优秀数组,我们只需要打破条件 `pos[a[i]] < pos[a[i+1]] <= pos[a[i]] + d` 中的某一个。 +- 一种简单的做法是让 `pos[a[i]]` 和 `pos[a[i+1]]` 之间的距离超过 `d`,或者直接让 `pos[a[i]] >= pos[a[i+1]]`。 + +3、具体方法 + +- 只需要考虑 `a` 中相邻元素的顺序,并判断如何交换 `p` 中相邻元素使得其顺序被打破。 +- 假设我们需要在 `p` 中交换某些元素来实现上述目标,那么最小的交换次数是将 `a[i]` 和 `a[i+1]` 的位置交换。 +- 如果 `pos[a[i]] + 1 == pos[a[i+1]]`,则需要一步交换。 + +4、特别情况 + +- 还需要考虑,如果通过交换相邻元素无法解决问题的情况。比如 `pos[a[i+1]]` 的位置无法移到 `pos[a[i]]` 的前面或超过 `d`。 + +C++代码如下: + + +```cpp +#include +#include +#include +#include + +using namespace std; + +int main() { + int n, m, d; + cin >> n >> m >> d; + + vector p(n + 1); + vector pos(n + 1); + + // 读取排列 p,并构建位置数组 pos + for (int i = 1; i <= n; i++) { + cin >> p[i]; + pos[p[i]] = i; + } + + vector a(m); + for (int i = 0; i < m; i++) { + cin >> a[i]; + } + + int min_operations = INT_MAX; + + // 遍历数组 a 的相邻元素 + for (int i = 0; i < m - 1; i++) { + int current_pos = pos[a[i]]; + int next_pos = pos[a[i + 1]]; + + // 检查 pos[a[i]] < pos[a[i+1]] <= pos[a[i]] + d 是否成立 + if (current_pos < next_pos && next_pos <= current_pos + d) { + // 计算需要的最少操作次数 + int distance = next_pos - current_pos; + + // Case 1: 交换 current_pos 和 next_pos + min_operations = min(min_operations, distance); + + // Case 2: 如果 next_pos + d <= n,考虑使 pos[a[i+1]] 超过 pos[a[i]] + d + if (current_pos + d + 1 <= n) { + min_operations = min(min_operations, d + 1 - distance); + } + } else { + min_operations = 0; + } + } + + cout << min_operations << endl; + return 0; +} + +``` + +时间复杂度为 O(m) diff --git "a/problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" "b/problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" new file mode 100644 index 0000000000..976e617a5c --- /dev/null +++ "b/problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" @@ -0,0 +1,76 @@ + + +贪心思路 + +为了保证字典序最大,我们优先放置字母 `b`,然后再放置字母 `a`。在放置字符时,我们还需注意不能超过连续 `k` 次相同字符: + +- 如果当前已经连续放置了 `k` 次相同字符,必须切换到另一个字符。 +- 每次放置字符后,相应的字符数量减少,同时更新当前字符的连续计数。 + +实现步骤: + +- **初始化**:根据输入的 `x`, `y`, `k` 值,检查是否有可能构造出满足条件的字符串。初始化结果字符串的大小,并设置初始计数器。 +- **循环放置字符**: + - 优先放置字符 `b`,如果 `b` 的数量已经足够,或者已经放置了 `k` 次字符 `b`,则放置字符 `a`。 + - 如果已经放置了 `k` 次相同字符,则强制切换到另一个字符。 + +C++代码如下: + +```CPP +#include +#include +using namespace std; + +int main() { + int countA, countB, maxRepeat; + cin >> countA >> countB >> maxRepeat; + + // 检查是否有可能生成满足条件的字符串 + if (countA > (countB + 1) * maxRepeat || countB > (countA + 1) * maxRepeat) { + cout << -1 << endl; + return 0; + } + + string result(countA + countB, ' '); // 预先分配字符串大小 + int currentA = 0, currentB = 0; // 当前连续 'a' 和 'b' 的计数 + int pos = 0; // 当前填充位置 + + while (countA > 0 || countB > 0) { + // 当可以继续添加 'a' 或 'b' 且没有超过最大连续限制时 + if (currentA < maxRepeat && currentB < maxRepeat) { + if (countA <= countB * maxRepeat) { + result[pos++] = 'b'; + countB--; + currentB++; + currentA = 0; + } else { + result[pos++] = 'a'; + countA--; + currentA++; + currentB = 0; + } + } + + // 当当前字符达到最大连续限制时,切换到另一个字符 + if (currentA == maxRepeat || currentB == maxRepeat) { + if (result[pos - 1] == 'a') { + result[pos++] = 'b'; + countB--; + currentB = 1; + currentA = 0; + } else { + result[pos++] = 'a'; + countA--; + currentA = 1; + currentB = 0; + } + } + } + + cout << result << endl; + return 0; +} + +``` + +时间复杂度:O(n) diff --git "a/problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" "b/problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" new file mode 100644 index 0000000000..addbcf4418 --- /dev/null +++ "b/problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" @@ -0,0 +1,78 @@ + +## 解题思路 + +贪心思路 + +- **计算相邻元素差值**: + - 对于数组 `a`,计算每对相邻元素的差值 `diff[i] = a[i+1] - a[i]`。 + - 如果 `diff[i]` 为负数,意味着 `a[i+1]` 比 `a[i]` 小或相等,需要通过操作使 `a[i+1]` 变大。 + +- **确定最小操作次数**: + - 计算所有相邻元素中的最小差值 `minDifference`,即 `minDifference = min(diff[i])`。 + - 如果 `minDifference` 为负数或零,则需要进行 `-minDifference + 1` 次操作,使得 `a[i+1]` 大于 `a[i]`,从而使数组严格递增。 + +- **实现细节**: + - 遍历数组的每对相邻元素,找出最小的差值。 + - 根据最小差值,计算出最少的操作次数。 + + + +```CPP +#include +#include +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + vector arr(n); // 用于存储输入数组 + vector differences; // 用于存储相邻元素的差值 + + for(int i = 0; i < n; i++) { + cin >> arr[i]; + if(i > 0) differences.push_back(arr[i] - arr[i - 1]); + + } + + int minDifference = INT_MAX; + + // 寻找最小的差值 + for(int diff : differences) { + if(diff < minDifference) { + minDifference = diff; + } + } + + // 如果最小差值是负数或零,计算所需的操作次数 + int minOperations = max(0, -minDifference + 1); + + cout << minOperations << endl; + + return 0; +} + +``` +关于 `-minDifference + 1` 为什么要 + 1 解释: + +对于数组 `a` 中相邻的两个元素 `a[i]` 和 `a[i+1]`,我们计算它们的差值 `diff = a[i+1] - a[i]`。 + +- **目标**:要使 `a[i] < a[i+1]`,需要 `diff > 0`。 +- 如果 `diff < 0`,说明 `a[i+1]` 比 `a[i]` 小,这时候 `a` 不是严格递增的。 +- 如果 `diff = 0`,说明 `a[i+1]` 和 `a[i]` 相等,这时也不满足严格递增。 + +解释 `-minDifference + 1` + +1. **当 `minDifference < 0` 时**: + - 假设 `minDifference` 是所有相邻差值中的最小值,并且它是一个负数。 + - 例如,`minDifference = -3`,表示 `a[i+1] - a[i] = -3`,也就是 `a[i+1]` 比 `a[i]` 小 `3`。 + - 要让 `a[i+1] > a[i]`,我们至少需要使 `a[i+1] - a[i]` 从 `-3` 增加到 `1`。因此需要增加 `4`,即 `(-(-3)) + 1 = 3 + 1 = 4` 次操作。 + +2. **当 `minDifference = 0` 时**: + - `minDifference` 等于 `0`,表示 `a[i+1] - a[i] = 0`,即 `a[i+1]` 和 `a[i]` 相等。 + - 为了使 `a[i+1] > a[i]`,我们至少需要进行一次操作,使得 `a[i+1]` 大于 `a[i]`。 + + diff --git a/problems/qita/tulunfabu.md b/problems/qita/tulunfabu.md new file mode 100644 index 0000000000..28ee463850 --- /dev/null +++ b/problems/qita/tulunfabu.md @@ -0,0 +1,239 @@ + + +# 图论正式发布! + +录友们! 今天和大家正式宣布:大家期盼已久的「代码随想录图论」正式发布了。 + +**一年多来,日日夜夜,伏案编码、思考、写作,就为了今天给录友们一个交代**! + +我知道录友们在等图论等太久了,其实我比大家都着急。 + +![大家一直都在催](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105618.png) + +图论完整版目前已经开放在代码随想录网站:programmercarl.com + +**「代码随想录图论」共分为 五大模块**,共有三十一篇长文讲解: + +* 深搜与广搜 +* 并查集 +* 最小生成树 +* 拓扑排序 +* 最短路算法 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104436.png) + +**耗时一年之久,代码随想录图论 终于面世了**! + +一年前 23年的3月份 我刚刚更新完了 [代码随想录算法公开课](https://mp.weixin.qq.com/s/xsKjrnB4GyWApm4BYxshvg) ,这是我亲自录制的 140期 算法视频讲解,目前口碑极佳。 + +录完公开课之后,我就开始筹划更新图论内容了,无奈图论内容真的很庞大。 + +关于图论章节,**可以说 是代码随想录所有章节里画图数量最多,工程量最大的一个章节**,整个图论 画图就400百多幅。 + +随便截一些图,大家感受一下: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104703.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104824.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104852.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104926.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105007.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105030.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105106.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105143.png) + +具体内容,大家可以去代码随想录网站(programmercarl.com)去看看,非常精彩! + +期间有很多录友再催:**卡哥怎么不更新图论呢? 卡哥是不是不打算更新图论了**? 等等 + +每次我都要去解释一波。 + +如果我想 快速出图论章节,可以很快! + +**但我不想做 降低 代码随想录整体质量和口碑的事情**。 + +所以关于现在发布的「代码随想录图论」,我可以很自信的说,**这是市面上大家能看到的,最全、最细致图论算法教程**。 + +**我在写作的时候没有避开任何雷区,完全遇雷排雷,然后让大家舒舒服服的走过去**。 + +什么是雷区? + +很多知识点 ,大家去看资料的时候会发现是 没有讲解的或者一笔带过, 因为这种雷区知识点 很难讲清楚或者需要花费大量的时间去讲明白。 + +一些知识点是这样的:**自己一旦懂了就知道是那么回事,但要写出来,要给别人讲清楚,是很难的一件事**。 + +这些知识点同样是雷区,就是大家在看 教程或者算法讲解的时候,作者避而不谈的部分。 + +例如: 深搜为什么有两种写法,同样的广搜为什么有一种写法超时了,bellman_ford 为什么 要松弛 n - 1次,负权回路对最短路求解的影响 等等。 + +这一点大家在阅读代码随想录图论的时候,**可以感受到 我对细节讲解的把控程度**。 + +## 为什么要出图论 + +图论是很重要的章节,也是 大家求职笔试面试,无论是社招还是校招,都会考察的知识内容。 + +而且图论应用广泛,在大家做项目开发的时候,或多或少都会用到图论相关知识。 + +例如:通信网络(拓扑排序、最短路算法),社交网络(深搜、广搜),路径优化(最短路算法),任务调度(拓扑排序),生物信息学(基因为节点,基因关系为边),游戏开发(A * 算法等)等等 + +为了保质保量更新图论,**市面上所有的算法书籍,我都看过**! + +**反复确认 思路正确性的同时,不仅感叹 市面上的算法书籍 在图论方面的 “缺斤少两**” 。 + +大名鼎鼎的《算法4》 以图论内容详细且图解多 而被大家好评, + +以最短路算法为例,《算法4》,只讲解了 Dijkstra(堆优化)、SPFA (Bellman-Ford算法基于队列) 和 拓扑排序, + +而 dijkstra朴素版、Bellman_ford 朴素版、bellman_ford之单源有限最短路、Floyd 、A * 算法 以及各个最短路算法的优劣,并没有讲解。 + +其他算法书籍 更是对图论的很多知识点一笔带过。 + +而在 代码随想录图论章节,**仅仅是 最短路算法方面,我就详细讲解了如下内容**: + +* dijkstra朴素版 +* dijkstra堆优化版 +* Bellman_ford +* Bellman_ford 队列优化算法(又名SPFA) +* bellman_ford 算法判断负权回路 +* bellman_ford之单源有限最短路 +* Floyd 算法精讲 +* 启发式搜索:A * 算法 + +**常见最短路算法,我都没有落下**。 + +而且大家在看很多算法书籍是没有编程题目配合练习,这样学习效果大打折扣, 一些书籍有编程题目配合练习但知识点严重不全。 + +## 出题 + +我在讲解图论的时候,最头疼的就是找题,在力扣上 找题总是找不到符合思路且来完整表达算法精髓的题目。 + +特别是最短路算法相关的题目,例如 Bellman_ford系列 ,Floyd ,A * 等等总是找不到符合思路的题目。 + +所以我索性就自己出题吧,**这也是 卡码网(kamacoder.com)诞生的一个原因之一**。 + +**为了给大家带来极致的学习体验,我在很多细节上都下了功**夫。 + +卡码网专门给大家准备的ACM输入输出模式,**图论是在笔试还有面试中,通常都是以ACM模式来考察大家**,而大家习惯在力扣刷题(核心代码模式),核心代码模式对图的存储和输出都隐藏了。 + +**图论题目的输出输出相对其他章节的题目来说是最难处理的**。 + +### 输入的细节 + +图论的输入难在 图的存储结构,**如果没有练习过 邻接表和邻接矩阵 ,很多录友是写不出来的**。 + +而力扣上是直接给好现成的 数据结构,可以直接用,所以练习不到图的输入,也练习不到邻接表和邻接矩阵。 + +ACM输入输出模式是最考察候选人对代码细节把控程度。 + +如果熟练ACM模式,那么核心代码模式基本没问题,但反过来就不一定了。 + +### 输出的细节 + +同样,图论的输出也有细节,例如 求节点1 到节点5的所有路径, 输出可能是: + +``` +1 2 4 5 +1 3 5 +``` + +表示有两条路可以到节点5, 那储存这个结果需要二维数组,最后在一起输出,力扣是直接return数组就好了,但 ACM模式要求我们自己输出,这里有就细节了。 + +就拿 只输出一行数据,输出 `1 2 4 5` 来说, + +很多录友代码可能直接就这么写了: + +```CPP +for (int i = 0 ; i < result.size(); i++) { + cout << result[i] << " "; +} +``` + +这么写输出的结果是 `1 2 4 5 `, 发现结果是对的,一提交,发现OJ返回 格式错误 或者 结果错误。 + +如果没练习过这种输出方式的录友,就开始怀疑了,这结果一样一样的,怎么就不对,我在力扣上提交都是对的! + +**大家要注意,5 后面要不要有空格**! + +上面这段代码输出,5后面是加上了空格了,如果判题机判断 结果的长度,标准答案`1 2 4 5`长度是7,而上面代码输出的长度是 8,很明显就是不对的。 + +所以正确的写法应该是: + +```CPP +for (int i = 0 ; i < result.size() - 1; i++) { + cout << result[i] << " "; +} +cout << result[result.size() - 1]; +``` + +这么写,最后一个元素后面就没有空格了。 + +这是很多录友经常疏忽的,也是大家刷习惯了 力扣(核心代码模式)根本不会注意到的细节。 + +**同样在工程开发中,这些细节都是影响系统稳定运行的因素之一**。 + +**ACM模式 除了考验算法思路,也考验 大家对 代码的把控力度**, 而 核心代码模式 只注重算法的解题思路,所以输入输出这些就省略掉了。 + +## 情怀 + +大家可以发现,**现在 用心给大家更新硬核且免费资料的博主 已经不多了**。 + +这一年的空闲时间,如果我用来打磨付费课程或者付费项目,或者干脆把图论做成付费专栏 加上现在的影响力,一定可以 “狠狠赚一笔”。 + +对我来说,有些钱可以赚,有些钱不赚。 + +如果持续关注代码随想录的录友可以发现:代码随想录不仅仅优质题解和视频免费,还有 [ACM模版配套25题](https://mp.weixin.qq.com/s/ai_Br_jSayeV2ELIYvnMYQ)、 [设计模式精讲配套23题](https://mp.weixin.qq.com/s/Wmu8jW4ezCi4CQ0uT9v9iA)、[每周举办大厂笔试真题(制作真题是十分费时的)](https://mp.weixin.qq.com/s/ULTehoK4GbdbQIdauKYt1Q), 这些都是免费优质资源。 + +**在付费与免费之间,我一直都在努力寻找平衡**。 + +很多录友之所以付费加入 [知识星球](https://mp.weixin.qq.com/s/65Vrq6avJkuTqofnz361Rw) 或者 [算法训练营](https://mp.weixin.qq.com/s/vkbcihvdNvBu1W4-bExoXA) ,也是因为看了这些免费资源想支持我一下。 + +“不忘初心”,说出来很容易,**但真正能随着岁月的流淌 坚持初心,是非常非常难的事情**。 + +**诱惑太多!有惰性的诱惑,有利益的诱惑**。 + +正如我之前说的:“代码随想录” 这五个字,我是会用一生去经营。 + +**免费硬核的算法内容是 代码随想录的立身之本**,也是 大家为什么学算法学编程首选代码随想录的根本所在。 + +当大家通过 代码随想录 提升了编程与算法能力,考上研或者找到好工作的时候,于我来说已经是很幸福的事情: + +![对笔试帮助大](https://code-thinking-1253855093.file.myqcloud.com/pics/20230914172536.png) + +![华为od将近满分](https://code-thinking-1253855093.file.myqcloud.com/pics/20230914172607.png) + +![研究生复试](https://code-thinking-1253855093.file.myqcloud.com/pics/20240621103130.png) + +![红包感谢代码随想录366](https://code-thinking-1253855093.file.myqcloud.com/pics/20231123151310.png) + +![上岸亚马逊](https://code-thinking-1253855093.file.myqcloud.com/pics/20240206174151.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220718094112.png) + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220718094332.png) + +至此**图论内容 已完全免费开放在代码随想录网站(programmercarl.com),造福广大学习编程的录友们**! + +Github 也已经同步 :https://github.com/youngyangyang04/leetcode-master ,关于其他语言版本,欢迎录友们去仓库提交PR + +## 后序 + +关于图论PDF版本,我后面会整理出来,免费发放给大家。 + +关于图论视频版本,不出意外,应该在年底开始在B站更新,同样免费开放。 + +总之,代码随想录会持续更新下去,无论是文字版还是视频版。 + +希望大家 不用 非要到找工作的时候 或者要考研的时候 才想到代码随想录。 + +**代码是作品,算法更是艺术**,时不时来欣赏一段解决关键问题的优雅代码,也是一种享受。 + +最后,**愿录友们学有所成,归来仍看代码随想录**! + + + diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index 3dda376855..dd1646d6d8 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -11,7 +11,7 @@ 关于动态规划,在专题第一篇[关于动态规划,你该了解这些!](https://programmercarl.com/动态规划理论基础.html)就说了动规五部曲,**而且强调了五部对解动规题目至关重要!** -这是Carl做过一百多道动规题目总结出来的经验结晶啊,如果大家跟着「代码随想哦」刷过动规专题,一定会对这动规五部曲的作用感受极其深刻。 +这是Carl做过一百多道动规题目总结出来的经验结晶啊,如果大家跟着「代码随想录」刷过动规专题,一定会对这动规五部曲的作用感受极其深刻。 动规五部曲分别为: diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 2e1cd52c23..e744383810 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -29,7 +29,7 @@ 而完全背包又是也是01背包稍作变化而来,即:完全背包的物品数量是无限的。 -**所以背包问题的理论基础重中之重是01背包,一定要理解透!** +**所以背包问题的理论基础重中之重是01背包,一定要理解透**! leetcode上没有纯01背包的问题,都是01背包应用方面的题目,也就是需要转化为01背包问题。 @@ -67,6 +67,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 以下讲解和图示中出现的数字都是以这个例子为例。 +(为了方便表述,下面描述 统一用 容量为XX的背包,放下容量(重量)为XX的物品,物品的价值是XX) ### 二维dp数组01背包 @@ -76,7 +77,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 我们需要使用二维数组,为什么呢? -因为有两个维度需要表示,分别是:物品 和 背包容量 +因为有两个维度需要分别表示:物品 和 背包容量 如图,二维数组为 dp[i][j]。 @@ -110,13 +111,13 @@ i 来表示物品、j表示背包容量。 背包容量为 0,放不下物品0 或者物品1,此时背包里的价值为0。 -背包容量为 1,只能放下物品1,背包里的价值为15。 +背包容量为 1,只能放下物品0,背包里的价值为15。 -背包容量为 2,只能放下物品1,背包里的价值为15。 +背包容量为 2,只能放下物品0,背包里的价值为15。 -背包容量为 3,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包可以放物品2 或者 物品1,物品2价值更大,背包里的价值为20。 +背包容量为 3,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包可以放物品1 或者 物品0,物品1价值更大,背包里的价值为20。 -背包容量为 4,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包都可都放下,背包价值为35。 +背包容量为 4,上一行同一状态,背包只能放物品0,这次也可以选择物品1了,背包可以放下物品0 和 物品1,背包价值为35。 以上举例,是比较容易看懂,我主要是通过这个例子,来帮助大家明确dp数组的含义。 @@ -144,7 +145,10 @@ i 来表示物品、j表示背包容量。 这里我们dp[1][4]的状态来举例: -绝对 dp[1][4],就是放物品1 ,还是不放物品1。 +求取 dp[1][4] 有两种情况: + +1. 放物品1 +2. 还是不放物品1 如果不放物品1, 那么背包的价值应该是 dp[0][4] 即 容量为4的背包,只放物品0的情况。 @@ -152,24 +156,23 @@ i 来表示物品、j表示背包容量。 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174246.png) - -如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 需要重量为3,此时背包剩下容量为1。 +如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 容量为1,只考虑放物品0 的最大价值是 dp[0][1],这个值我们之前就计算过。 -所以 放物品1 的情况 = dp[0][1] + 物品1 的重量,推导方向如图: +所以 放物品1 的情况 = dp[0][1] + 物品1 的价值,推导方向如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174436.png) 两种情况,分别是放物品1 和 不放物品1,我们要取最大值(毕竟求的是最大价值) -`dp[1][4] = max(dp[0][4], dp[0][1] + 物品1 的重量) ` +`dp[1][4] = max(dp[0][4], dp[0][1] + 物品1 的价值) ` 以上过程,抽象化如下: -* **不放物品i**:由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。 +* **不放物品i**:背包容量为j,里面不放物品i的最大价值是dp[i - 1][j]。 -* **放物品i**:由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]的时候不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 +* **放物品i**:背包空出物品i的容量后,背包容量为j - weight[i],dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]且不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 递归公式: `dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);` @@ -235,7 +238,6 @@ for (int j = weight[0]; j <= bagweight; j++) { 4. 确定遍历顺序 - 在如下图中,可以看出,有两个遍历的维度:物品与背包重量 ![动态规划-背包问题3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010314055.png) @@ -364,7 +366,6 @@ int main() { - ## 其他语言版本 ### Java From 0846cd122e93e2472976164484d971990bd68ffa Mon Sep 17 00:00:00 2001 From: feobay Date: Fri, 30 Aug 2024 16:05:11 +0800 Subject: [PATCH 1273/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00309.=E6=9C=80?= =?UTF-8?q?=E4=BD=B3=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=90=AB=E5=86=B7=E5=86=BB=E6=9C=9FGo=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=B8=80=E7=BB=B4=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\345\206\267\345\206\273\346\234\237.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 95689a4805..4913b8bd57 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -357,6 +357,42 @@ func max(a, b int) int { } ``` +```go +// 一维优化版本 +// 时间复杂度O(n), 空间复杂度O(1) +func maxProfit(prices []int) int { + + // 0: 持有,一直持有和买入 + // 1: 不持有,一直不持有(不包含前一天卖出,因为这样的一天是冷静期,状态有区别) + // 2:不持有,今天卖出 + // 3:冷静期,前一天卖出(一直不持有) + dp0, dp1, dp2, dp3 := -prices[0], 0, 0, 0 + + n := len(prices) + + for i := 1; i < n; i++ { + t0 := max(dp0, max(dp1, dp3)-prices[i]) + t1 := max(dp1, dp3) + t2 := dp0 + prices[i] + t3 := dp2 + + // 更新 + dp0, dp1, dp2, dp3 = t0, t1, t2, t3 + } + + return max(dp1, max(dp2, dp3)) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + + + ### Javascript: > 不同的状态定义 感觉更容易理解些 @@ -540,3 +576,4 @@ impl Solution { + From f38dc8aedc147c824b5eaa3621f399a623e9b681 Mon Sep 17 00:00:00 2001 From: feobay Date: Fri, 30 Aug 2024 16:19:40 +0800 Subject: [PATCH 1274/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00188.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAIVGo=E7=89=88=E6=9C=AC=E7=A9=BA=E9=97=B4=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272IV.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index def6927756..0b1622ac5a 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -404,6 +404,47 @@ func max188(a, b int) int { } ``` +版本三:空间优化版本 + +```go +func maxProfit(k int, prices []int) int { + n := len(prices) + // k次交易,2 * k种状态 + // 状态从1开始计算,避免判断 + // 奇数时持有(保持或买入) + // 偶数时不持有(保持或卖出) + dp := make([][]int, 2) + dp[0] = make([]int, k * 2 + 1) + dp[1] = make([]int, k * 2 + 1) + + // 奇数状态时持有,i += 2 + for i := 1; i <= k * 2; i += 2 { + dp[0][i] = -prices[0] + } + + for i := 1; i < len(prices); i++ { + for j := 1; j <= k * 2; j++ { + if j % 2 == 1 { + dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1] - prices[i]) + } else { + dp[i % 2][j] = max(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1] + prices[i]) + } + } + } + + return dp[(n - 1) % 2][k * 2] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + + + ### JavaScript: ```javascript @@ -558,3 +599,4 @@ impl Solution { + From 830caef1222ddda1ab606f794c44e28e1be9b209 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:06:47 +0800 Subject: [PATCH 1275/1533] =?UTF-8?q?Update=200044.=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E5=95=86=E8=B4=AD=E4=B9=B0=E5=9C=9F=E5=9C=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\344\271\260\345\234\237\345\234\260.md" | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" index 37bb98ed2d..ea2c696ef6 100644 --- "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -388,3 +388,85 @@ if __name__ == "__main__": main() ``` +### C + +前缀和 +```c +#include +#include + +int main() +{ + int n = 0, m = 0, ret_ver = 0, ret_hor = 0; + + // 读取行和列的值 + scanf("%d%d", &n, &m); + // 动态分配数组a(横)和b(纵)的空间 + int *a = (int *)malloc(sizeof(int) * n); + int *b = (int *)malloc(sizeof(int) * m); + + // 初始化数组a和b + for (int i = 0; i < n; i++) + { + a[i] = 0; + } + for (int i = 0; i < m; i++) + { + b[i] = 0; + } + + // 读取区块权值并计算每行和每列的总权值 + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + int tmp; + scanf("%d", &tmp); + a[i] += tmp; + b[j] += tmp; + } + } + + // 计算每列以及每行的前缀和 + for (int i = 1; i < n; i++) + { + a[i] += a[i - 1]; + } + for (int i = 1; i < m; i++) + { + b[i] += b[i - 1]; + } + + // 初始化ret_ver和ret_hor为最大可能值 + ret_hor = a[n - 1]; + ret_ver = b[m - 1]; + + // 计算按行划分的最小差异 + int ret2 = 0; + while (ret2 < n) + { + ret_hor = (ret_hor > abs(a[n - 1] - 2 * a[ret2])) ? abs(a[n - 1] - 2 * a[ret2]) : ret_hor; + // 原理同列,但更高级 + ret2++; + } + // 计算按列划分的最小差异 + int ret1 = 0; + while (ret1 < m) + { + if (ret_ver > abs(b[m - 1] - 2 * b[ret1])) + { + ret_ver = abs(b[m - 1] - 2 * b[ret1]); + } + ret1++; + } + + // 输出最小差异 + printf("%d\n", (ret_ver <= ret_hor) ? ret_ver : ret_hor); + + // 释放分配的内存 + free(a); + free(b); + return 0; +} + +``` From 2c52c3c363e13803ff9b46c16e9ec37ead8fe41c Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Fri, 30 Aug 2024 21:31:45 +0800 Subject: [PATCH 1276/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2058.=20=E5=8C=BA?= =?UTF-8?q?=E9=97=B4=E5=92=8C=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...8.\345\214\272\351\227\264\345\222\214.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index f5ce08dcbf..c5a84a2967 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -263,3 +263,45 @@ if __name__ == "__main__": main() ``` +### C + +```C +#include +#include + +int main(int argc, char *argv[]) +{ + int num; + // 读取数组长度 + scanf("%d", &num); + + // 使用动态内存分配而不是静态数组,以适应不同的输入大小 + int *a = (int *)malloc((num + 1) * sizeof(int)); + + // 初始化前缀和数组的第一个元素为0 + a[0] = 0; + + // 读取数组元素并计算前缀和 + for (int i = 1; i <= num; i++) + { + int mm; + scanf("%d", &mm); + // 累加前缀和 + a[i] = a[i - 1] + mm; + } + + int m, n; + // 循环读取区间并计算区间和,直到输入结束 + // scanf()返回成功匹配和赋值的个数,到达文件末尾则返回 EOF + while (scanf("%d%d", &m, &n) == 2) + { + // 输出区间和,注意区间是左闭右开,因此a[n+1]是包含n的元素的前缀和 + printf("%d\n", a[n+1] - a[m]); + } + + // 释放之前分配的内存 + free(a); + return 0; +} + +``` From 8dc91092774defa6ab621b1d50de1968df6cc7ba Mon Sep 17 00:00:00 2001 From: wangq635 <643797037@qq.com> Date: Sun, 1 Sep 2024 19:49:39 +0800 Subject: [PATCH 1277/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=EF=BC=8C=E5=A2=9E=E5=8A=A0python?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E7=9A=84=E8=BF=AD=E4=BB=A3=E6=B3=95=EF=BC=88?= =?UTF-8?q?=E7=B2=BE=E7=AE=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\346\223\215\344\275\234.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 6b9e58347d..c56a262443 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -370,6 +370,30 @@ class Solution: ``` + +迭代法(精简) +```python +class Solution: + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + if not root: # 如果根节点为空,创建新节点作为根节点并返回 + return TreeNode(val) + cur = root + while cur: + if val < cur.val: + if not cur.left: # 如果此时父节点的左子树为空 + cur.left = TreeNode(val) # 将新节点连接到父节点的左子树 + return root + else: + cur = cur.left + elif val > cur.val: + if not cur.right: # 如果此时父节点的左子树为空 + cur.right = TreeNode(val) # 将新节点连接到父节点的右子树 + return root + else: + cur = cur.right + +``` + ----- ### Go From 75da0248bd134384cea412df2043b15bc0640631 Mon Sep 17 00:00:00 2001 From: wangq635 <643797037@qq.com> Date: Sun, 1 Sep 2024 20:22:42 +0800 Subject: [PATCH 1278/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=200701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=EF=BC=8C=E9=80=92=E5=BD=92=E6=B3=95?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BA=8C=E5=BA=94=E8=AF=A5=E6=98=AF=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\346\223\215\344\275\234.md" | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index c56a262443..25d39486f3 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -283,32 +283,10 @@ class Solution: return TreeNode(val) self.traversal(root, val) return root - ``` 递归法(版本二) ```python -class Solution: - def insertIntoBST(self, root, val): - if root is None: - return TreeNode(val) - parent = None - cur = root - while cur: - parent = cur - if val < cur.val: - cur = cur.left - else: - cur = cur.right - if val < parent.val: - parent.left = TreeNode(val) - else: - parent.right = TreeNode(val) - return root -``` - -递归法(版本三) -```python class Solution: def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: if root is None or root.val == val: @@ -326,7 +304,7 @@ class Solution: return root ``` -递归法(版本四) +递归法(版本三) ```python class Solution: def insertIntoBST(self, root, val): @@ -340,10 +318,9 @@ class Solution: root.right = self.insertIntoBST(root.right, val) return root - ``` -迭代法 +迭代法(版本一) ```python class Solution: def insertIntoBST(self, root, val): @@ -366,9 +343,28 @@ class Solution: else: parent.right = node # 将新节点连接到父节点的右子树 - return root + return root +``` - +迭代法(版本二) +```python +class Solution: + def insertIntoBST(self, root, val): + if root is None: + return TreeNode(val) + parent = None + cur = root + while cur: + parent = cur + if val < cur.val: + cur = cur.left + else: + cur = cur.right + if val < parent.val: + parent.left = TreeNode(val) + else: + parent.right = TreeNode(val) + return root ``` 迭代法(精简) From 0f5a20826a0a531669eb4819e2da67dac30d2f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E7=AC=9B?= <128707187+catherinexrk@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:41:19 +0800 Subject: [PATCH 1279/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00058=E5=8C=BA?= =?UTF-8?q?=E9=97=B4=E5=92=8C=20JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...8.\345\214\272\351\227\264\345\222\214.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index f5ce08dcbf..5125f5f8b8 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -263,3 +263,52 @@ if __name__ == "__main__": main() ``` + +### JavaScript + +``` JavaScript + +function prefixSum() { + const readline = require('readline'); + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + let inputLines = []; + rl.on('line', (line) => { + inputLines.push(line.trim()); + }); + + rl.on('close', () => { + // 读取项数 n + const n = parseInt(inputLines[0]); + + // 使用前缀和,复杂度控制在 O(1) + let sum = new Array(n); + sum[0] = parseInt(inputLines[1]); + + // 计算前缀和数组 + for (let i = 1; i < n; i++) { + let value = parseInt(inputLines[i + 1]); + sum[i] = sum[i - 1] + value; + } + + // 处理区间和查询 + for (let i = n + 1; i < inputLines.length; i++) { + let [left, right] = inputLines[i].split(' ').map(Number); + + if (left === 0) { + console.log(sum[right]); + } else { + console.log(sum[right] - sum[left - 1]); + } + } + }); +} + + +``` + + From f4e653d1194b360836964db2bbf9654a9aabf412 Mon Sep 17 00:00:00 2001 From: Zihan Qiu Date: Mon, 2 Sep 2024 09:24:26 -0500 Subject: [PATCH 1280/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=860383.?= =?UTF-8?q?=E8=B5=8E=E9=87=91=E4=BF=A1=E7=9A=84python=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BA=8C13=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 6148 bytes ...3.\350\265\216\351\207\221\344\277\241.md" | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index f89600e9deb6f2c531cd456675779b0e188cf6e0..f7ce0fd5038358c7a0df77b968ed993daff0639c 100644 GIT binary patch delta 98 zcmZoMXfc=|&e%S&P;8=}q9_vs0|O%ig8&0V4nrzKE< Date: Wed, 4 Sep 2024 11:02:32 +0800 Subject: [PATCH 1281/1533] Update --- ...163.\344\274\230\347\247\200\346\225\260\347\273\204.md" | 3 +++ ...164.\345\215\207\345\272\217\346\225\260\347\273\204.md" | 3 +++ ...\217\346\227\240\351\207\215\345\244\215\344\270\262.md" | 6 ++++++ 3 files changed, 12 insertions(+) rename "problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" => "problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" (97%) rename "problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" => "problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" (96%) rename "problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" => "problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" (95%) diff --git "a/problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" "b/problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" similarity index 97% rename from "problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" rename to "problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" index 989296845d..4967f907be 100644 --- "a/problems/kamacoder/\347\254\254\344\270\200\351\242\230.md" +++ "b/problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" @@ -1,4 +1,7 @@ +# 优秀数组 + +[题目链接](https://kamacoder.com/problempage.php?pid=1241) ## 解题思路 diff --git "a/problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" "b/problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" similarity index 96% rename from "problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" rename to "problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" index addbcf4418..2a35b715bf 100644 --- "a/problems/kamacoder/\347\254\254\344\272\214\351\242\230.md" +++ "b/problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" @@ -1,3 +1,6 @@ +# 升序数组 + +[题目链接](https://kamacoder.com/problempage.php?pid=1241) ## 解题思路 diff --git "a/problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" "b/problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" similarity index 95% rename from "problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" rename to "problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" index 976e617a5c..c7be98d239 100644 --- "a/problems/kamacoder/\347\254\254\344\270\211\351\242\230.md" +++ "b/problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" @@ -1,4 +1,10 @@ +# 最大字典序无重复串 + +[题目链接](https://kamacoder.com/problempage.php?pid=1243) + + +## 解题思路 贪心思路 From 74097173006cabb33f75e331e0aa05256210c13f Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:38:10 +0800 Subject: [PATCH 1282/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E5=AF=BB?= =?UTF-8?q?=E5=AE=9D-Kruskal.mdC=E8=AF=AD=E8=A8=80=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0053.\345\257\273\345\256\235-Kruskal.md" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index fb816fb1af..7a69388293 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -562,4 +562,75 @@ if __name__ == "__main__": ### Dart ### C +并查集方法一 +```c +// 定义边结构体,包含两个顶点vex1和vex2以及它们之间的权重val +struct Edge +{ + int vex1, vex2, val; +}; + +// 冒泡排序函数,用于按边的权重val不减序排序边数组 +void bubblesort(struct Edge *a, int numsize) +{ + for (int i = 0; i < numsize - 1; ++i) + { + + for (int j = 0; j < numsize - i - 1; ++j) + { + if (a[j].val > a[j + 1].val) + { + struct Edge temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } +} + +int main() +{ + int v, e; + int v1, v2, val; + int ret = 0; + + scanf("%d%d", &v, &e); + struct Edge *edg = (struct Edge *)malloc(sizeof(struct Edge) * e); + int *conne_gra = (int *)malloc(sizeof(int) * (v + 1)); + + // 初始化连通图数组,每个顶点初始时只与自己相连通 + for (int i = 0; i <= v; ++i) + { + conne_gra[i] = i; + } + + // 读取所有边的信息并存储到edg(存储所有边)数组中 + for (int i = 0; i < e; ++i) + { + scanf("%d%d%d", &v1, &v2, &val); + edg[i].vex1 = v1; + edg[i].vex2 = v2; + edg[i].val = val; + } + bubblesort(edg, e); // 调用冒泡排序函数对边进行排序 + + // 遍历所有边,执行Kruskal算法来找到最小生成树 + for (int i = 0; i < e; ++i) + { + if (conne_gra[edg[i].vex1] != conne_gra[edg[i].vex2]) + { // 如果当前边的两个顶点不在同一个连通分量中 + int tmp1 = conne_gra[edg[i].vex1], tmp2 = conne_gra[edg[i].vex2]; + for (int k = 1; k <= v; ++k) + { // 将所有属于tmp2的顶点合并到tmp1的连通分量中 + if (conne_gra[k] == tmp2) + conne_gra[k] = tmp1; + } + ret += edg[i].val; // 将当前边的权重加到最小生成树的权重中 + } + } + printf("%d", ret); + return 0; +} + +``` From adc34fb2f50db35891460d482b260957b797a41c Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:43:22 +0800 Subject: [PATCH 1283/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00053.=E5=AF=BB?= =?UTF-8?q?=E5=AE=9D-Kruskal.mdC=E8=AF=AD=E8=A8=80=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0053.\345\257\273\345\256\235-Kruskal.md" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 7a69388293..52724956e8 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -634,3 +634,66 @@ int main() } ``` +并查集方法二 +```c +// 定义边结构体,包含两个顶点vex1和vex2以及它们之间的权重val (略,同上) +// 冒泡排序函数,用于按边的权重val不减序排序边数组(略,同上) + +// 并查集的查找操作 +int find(int m, int *father) +{ // 如果当前节点是其自身的父节点,则直接返回该节点 + // 否则递归查找其父节点的根,并将当前节点直接连接到根节点 + return (m == father[m]) ? m : (father[m] = find(father[m], father)); // 路径压缩 +} + +// 并查集的加入集合 +void Union(int m, int n, int *father) +{ + int x = find(m, father); + int y = find(n, father); + if (x == y) + return; // 如果发现根相同,则说明在一个集合,不用两个节点相连直接返回 + father[y] = x; +} + +int main() +{ + int v, e; + int v1, v2, val; + int ret = 0; + + scanf("%d%d", &v, &e); + struct Edge *edg = (struct Edge *)malloc(sizeof(struct Edge) * e); + int *conne_gra = (int *)malloc(sizeof(int) * (v + 1)); + // 初始化连通图数组,每个顶点初始时只与自己相连通 + for (int i = 0; i <= v; ++i) + { + conne_gra[i] = i; + } + // 读取所有边的信息并存储到edg(存储所有边)数组中 + for (int i = 0; i < e; ++i) + { + scanf("%d%d%d", &v1, &v2, &val); + edg[i].vex1 = v1; + edg[i].vex2 = v2; + edg[i].val = val; + } + bubblesort(edg, e); // 调用冒泡排序函数对边进行排序 + + // Kruskal算法的实现,通过边数组构建最小生成树 + int j = 0, count = 0; + while (v > 1) + { + if (find(edg[j].vex1, conne_gra) != find(edg[j].vex2, conne_gra)) + { + ret += edg[j].val; // 将当前边的权重加到最小生成树的权重中 + Union(edg[j].vex1, edg[j].vex2, conne_gra); + v--; + } + j++; + } + printf("%d", ret); + return 0; +} + +``` From fd0f78cac25912fd6e9eff6cfa51244a1e60fd6c Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:45:00 +0800 Subject: [PATCH 1284/1533] =?UTF-8?q?=E5=8A=A0=E5=A4=B4=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 52724956e8..7f5ec3c4e4 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -564,6 +564,8 @@ if __name__ == "__main__": ### C 并查集方法一 ```c +#include +#include // 定义边结构体,包含两个顶点vex1和vex2以及它们之间的权重val struct Edge @@ -636,6 +638,9 @@ int main() ``` 并查集方法二 ```c +#include +#include + // 定义边结构体,包含两个顶点vex1和vex2以及它们之间的权重val (略,同上) // 冒泡排序函数,用于按边的权重val不减序排序边数组(略,同上) From d3ad53e4f2157158276ec84b3704d4ce7858b25c Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:51:11 +0800 Subject: [PATCH 1285/1533] =?UTF-8?q?Update=200053.=E5=AF=BB=E5=AE=9D-Krus?= =?UTF-8?q?kal.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 7f5ec3c4e4..cb24fd17f3 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -607,7 +607,7 @@ int main() conne_gra[i] = i; } - // 读取所有边的信息并存储到edg(存储所有边)数组中 + // 读取所有边的信息并存储到edg(存储所有边的)数组中 for (int i = 0; i < e; ++i) { scanf("%d%d%d", &v1, &v2, &val); @@ -670,12 +670,13 @@ int main() scanf("%d%d", &v, &e); struct Edge *edg = (struct Edge *)malloc(sizeof(struct Edge) * e); int *conne_gra = (int *)malloc(sizeof(int) * (v + 1)); + // 初始化连通图数组,每个顶点初始时只与自己相连通 for (int i = 0; i <= v; ++i) { conne_gra[i] = i; } - // 读取所有边的信息并存储到edg(存储所有边)数组中 + // 读取所有边的信息并存储到edg(存储所有边的)数组中 for (int i = 0; i < e; ++i) { scanf("%d%d%d", &v1, &v2, &val); @@ -683,6 +684,7 @@ int main() edg[i].vex2 = v2; edg[i].val = val; } + bubblesort(edg, e); // 调用冒泡排序函数对边进行排序 // Kruskal算法的实现,通过边数组构建最小生成树 From a3075308faacf803abe2e1ad6e89b5c3c5252c1c Mon Sep 17 00:00:00 2001 From: kyshen Date: Wed, 4 Sep 2024 23:00:44 +0800 Subject: [PATCH 1286/1533] feat: 0094 bellman-ford python solution --- ...7\347\211\251\350\277\220\350\276\223I.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 45ca131361..3737fe0149 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -451,6 +451,33 @@ public class Main { ``` ### Python +```Python +def main(): + n, m = map(int, input().strip().split()) + edges = [] + for _ in range(m): + src, dest, weight = map(int, input().strip().split()) + edges.append([src, dest, weight]) + + minDist = [float("inf")] * (n + 1) + minDist[1] = 0 # 起点处距离为0 + + for i in range(1, n): + updated = False + for src, dest, weight in edges: + if minDist[src] != float("inf") and minDist[src] + weight < minDist[dest]: + minDist[dest] = minDist[src] + weight + updated = True + if not updated: # 若边不再更新,即停止回圈 + break + + if minDist[-1] == float("inf"): # 返还终点权重 + return "unconnected" + return minDist[-1] + +if __name__ == "__main__": + print(main()) +``` ### Go From e391a247292b798c6afe86e19cf6b691d8f2de84 Mon Sep 17 00:00:00 2001 From: kyshen Date: Fri, 6 Sep 2024 22:39:29 +0800 Subject: [PATCH 1287/1533] feat: 0094 python solution SPFA --- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index ce38391943..142bb5281b 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -425,7 +425,44 @@ public class Main { ``` ### Python - +```Python +import collections + +def main(): + n, m = map(int, input().strip().split()) + edges = [] + for _ in range(m): + src, dest, weight = map(int, input().strip().split()) + edges.append([src, dest, weight]) + + minDist = [float("inf")] * (n + 1) + minDist[1] = 0 + que = collections.deque([1]) + visited = [False] * (n + 1) + visited[1] = True + + while que: + cur = que.popleft() + visited[cur] = True + updated = False + for src, dest, weight in edges: + if minDist[src] != float("inf") and minDist[src] + weight < minDist[dest]: + minDist[dest] = minDist[src] + weight + updated = True + if visited[dest] == False: + que.append(dest) + visited[dest] = True + + if not updated: + break + + if minDist[-1] == float("inf"): + return "unconnected" + return minDist[-1] + +if __name__ == "__main__": + print(main()) +``` ### Go ### Rust From e24c307c07b7f70eca2f48d01d3c56f318b82dd3 Mon Sep 17 00:00:00 2001 From: kyshen Date: Fri, 6 Sep 2024 23:59:51 +0800 Subject: [PATCH 1288/1533] fix: error saving the edges --- ...47\211\251\350\277\220\350\276\223I-SPFA.md" | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 142bb5281b..b3f42bf800 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -430,10 +430,10 @@ import collections def main(): n, m = map(int, input().strip().split()) - edges = [] + edges = [[] for _ in range(n + 1)] for _ in range(m): src, dest, weight = map(int, input().strip().split()) - edges.append([src, dest, weight]) + edges[src].append([dest, weight]) minDist = [float("inf")] * (n + 1) minDist[1] = 0 @@ -443,18 +443,13 @@ def main(): while que: cur = que.popleft() - visited[cur] = True - updated = False - for src, dest, weight in edges: - if minDist[src] != float("inf") and minDist[src] + weight < minDist[dest]: - minDist[dest] = minDist[src] + weight - updated = True + visited[cur] = False + for dest, weight in edges[cur]: + if minDist[cur] != float("inf") and minDist[cur] + weight < minDist[dest]: + minDist[dest] = minDist[cur] + weight if visited[dest] == False: que.append(dest) visited[dest] = True - - if not updated: - break if minDist[-1] == float("inf"): return "unconnected" From 56306fc00b5fc111d66390f3d8c5bb0d25ffa166 Mon Sep 17 00:00:00 2001 From: wangq635 <643797037@qq.com> Date: Sat, 7 Sep 2024 19:50:44 +0800 Subject: [PATCH 1289/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92=EF=BC=9A01=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=EF=BC=88=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E6=95=B0=E7=BB=84=EF=BC=89=E7=9A=84python=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=BB=9A=E5=8A=A8=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\241\20001\350\203\214\345\214\205-2.md" | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index cd8f317c52..830958ef17 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -294,23 +294,18 @@ public class Main { ```python n, bagweight = map(int, input().split()) - weight = list(map(int, input().split())) value = list(map(int, input().split())) -dp = [[0] * (bagweight + 1) for _ in range(n)] +dp = [0] * (bagweight + 1) # 创建一个动态规划数组dp,初始值为0 -for j in range(weight[0], bagweight + 1): - dp[0][j] = value[0] +dp[0] = 0 # 初始化dp[0] = 0,背包容量为0,价值最大为0 -for i in range(1, n): - for j in range(bagweight + 1): - if j < weight[i]: - dp[i][j] = dp[i - 1][j] - else: - dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]) +for i in range(n): # 应该先遍历物品,如果遍历背包容量放在上一层,那么每个dp[j]就只会放入一个物品 + for j in range(bagweight, weight[i]-1, -1): # 倒序遍历背包容量是为了保证物品i只被放入一次 + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) -print(dp[n - 1][bagweight]) +print(dp[bagweight]) ``` ### Go From 02a8ec1c1380f74ec6dcc587225a2430a8180e04 Mon Sep 17 00:00:00 2001 From: cyxiwai Date: Mon, 9 Sep 2024 13:20:08 +0800 Subject: [PATCH 1290/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E4=BA=86kama0105.=E6=9C=89=E5=90=91=E5=9B=BE=E7=9A=84=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E5=8F=AF=E8=BE=BE=E6=80=A7=E7=9A=84Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\217\257\350\276\276\346\200\247.md" | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 765f9a27f7..838b021245 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -294,37 +294,52 @@ int main() { import java.util.*; public class Main { + public static List> adjList = new ArrayList<>(); - public static void dfs(List> graph, int key, boolean[] visited) { - for (int neighbor : graph.get(key)) { - if (!visited[neighbor]) { // Check if the next node is not visited - visited[neighbor] = true; - dfs(graph, neighbor, visited); - } + public static void dfs(boolean[] visited, int key) { + if (visited[key]) { + return; } - } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int n = scanner.nextInt(); - int m = scanner.nextInt(); - - List> graph = new ArrayList<>(); - for (int i = 0; i <= n; i++) { - graph.add(new ArrayList<>()); + visited[key] = true; + List nextKeys = adjList.get(key); + for (int nextKey : nextKeys) { + dfs(visited, nextKey); } + } - for (int i = 0; i < m; i++) { - int s = scanner.nextInt(); - int t = scanner.nextInt(); - graph.get(s).add(t); + public static void bfs(boolean[] visited, int key) { + Queue queue = new LinkedList(); + queue.add(key); + visited[key] = true; + while (!queue.isEmpty()) { + int curKey = queue.poll(); + List list = adjList.get(curKey); + for (int nextKey : list) { + if (!visited[nextKey]) { + queue.add(nextKey); + visited[nextKey] = true; + } + } } + } - boolean[] visited = new boolean[n + 1]; - visited[1] = true; // Process node 1 beforehand - dfs(graph, 1, visited); - - for (int i = 1; i <= n; i++) { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int vertices_num = sc.nextInt(); + int line_num = sc.nextInt(); + for (int i = 0; i < vertices_num; i++) { + adjList.add(new LinkedList<>()); + }//Initialization + for (int i = 0; i < line_num; i++) { + int s = sc.nextInt(); + int t = sc.nextInt(); + adjList.get(s - 1).add(t - 1); + }//构造邻接表 + boolean[] visited = new boolean[vertices_num]; + dfs(visited, 0); +// bfs(visited, 0); + + for (int i = 0; i < vertices_num; i++) { if (!visited[i]) { System.out.println(-1); return; @@ -334,7 +349,6 @@ public class Main { } } - ``` From ad0d129c25429f798bf6ee6e9812397742cd9fe5 Mon Sep 17 00:00:00 2001 From: cyxiwai Date: Mon, 9 Sep 2024 13:24:10 +0800 Subject: [PATCH 1291/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B9=B6=E8=A1=A5?= =?UTF-8?q?=E5=85=85=E4=BA=86kama0099.=E5=B2=9B=E5=B1=BF=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E9=87=8Fdfs=E5=92=8Cbfs=E7=9A=84=E7=89=88=E6=9C=AC=EF=BC=8C?= =?UTF-8?q?=E9=99=84=E6=9C=89=E8=AF=A6=E7=BB=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\345\271\277\346\220\234.md" | 53 +++++++++++++++++-- ...60\351\207\217\346\267\261\346\220\234.md" | 45 ++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 9fca9a0489..3047575391 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -191,10 +191,57 @@ int main() { ### Java ```java +import java.util.*; + +public class Main { + public static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};//下右上左逆时针遍历 + + public static void bfs(int[][] grid, boolean[][] visited, int x, int y) { + Queue queue = new LinkedList();//定义坐标队列,没有现成的pair类,在下面自定义了 + queue.add(new pair(x, y)); + visited[x][y] = true;//遇到入队直接标记为优先, + // 否则出队时才标记的话会导致重复访问,比如下方节点会在右下顺序的时候被第二次访问入队 + while (!queue.isEmpty()) { + int curX = queue.peek().first; + int curY = queue.poll().second;//当前横纵坐标 + for (int i = 0; i < 4; i++) { + //顺时针遍历新节点next,下面记录坐标 + int nextX = curX + dir[i][0]; + int nextY = curY + dir[i][1]; + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) { + continue; + }//去除越界部分 + if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) { + queue.add(new pair(nextX, nextY)); + visited[nextX][nextY] = true;//逻辑同上 + } + } + } + } - - - + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int m = sc.nextInt(); + int n = sc.nextInt(); + int[][] grid = new int[m][n]; + boolean[][] visited = new boolean[m][n]; + int ans = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + grid[i][j] = sc.nextInt(); + } + } + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (!visited[i][j] && grid[i][j] == 1) { + ans++; + bfs(grid, visited, i, j); + } + } + } + System.out.println(ans); + } +} ``` diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index b257ca9a73..6ac7ba3b62 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -182,7 +182,52 @@ int main() { ## 其他语言版本 ### Java +```java +import java.util.Scanner; + +public class Main { + public static int[][] dir ={{0,1},{1,0},{-1,0},{0,-1}}; + public static void dfs(boolean[][] visited,int x,int y ,int [][]grid) + { + for (int i = 0; i < 4; i++) { + int nextX=x+dir[i][0]; + int nextY=y+dir[i][1]; + if(nextY<0||nextX<0||nextX>= grid.length||nextY>=grid[0].length) + continue; + if(!visited[nextX][nextY]&&grid[nextX][nextY]==1) + { + visited[nextX][nextY]=true; + dfs(visited,nextX,nextY,grid); + } + } + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int m= sc.nextInt(); + int n = sc.nextInt(); + int[][] grid = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + grid[i][j]=sc.nextInt(); + } + } + boolean[][]visited =new boolean[m][n]; + int ans = 0; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if(!visited[i][j]&&grid[i][j]==1) + { + ans++; + visited[i][j]=true; + dfs(visited,i,j,grid); + } + } + } + System.out.println(ans); + } +} +``` ### Python 版本一 From 866032c16d5977a0e55d44b8086c60712032275a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=97=A8=E5=97=A8=E5=97=A8?= <111850394+QinWeijia111@users.noreply.github.com> Date: Wed, 11 Sep 2024 15:26:40 +0800 Subject: [PATCH 1292/1533] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Python语言版本的双指针去除空格算法的非调包算法 --- ...14\347\232\204\345\215\225\350\257\215.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index a000519862..bf486bdceb 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -475,7 +475,45 @@ class Solution: words = words[::-1] # 反转单词 return ' '.join(words) #列表转换成字符串 ``` +(版本四) 将字符串转换为列表后,使用双指针去除空格 +```python +class Solution: + def single_reverse(self, s, start: int, end: int): + while start < end: + s[start], s[end] = s[end], s[start] + start += 1 + end -= 1 + def reverseWords(self, s: str) -> str: + result = "" + fast = 0 + # 1. 首先将原字符串反转并且除掉空格, 并且加入到新的字符串当中 + # 由于Python字符串的不可变性,因此只能转换为列表进行处理 + s = list(s) + s.reverse() + while fast < len(s): + if s[fast] != " ": + if len(result) != 0: + result += " " + while s[fast] != " " and fast < len(s): + result += s[fast] + fast += 1 + else: + fast += 1 + # 2.其次将每个单词进行翻转操作 + slow = 0 + fast = 0 + result = list(result) + while fast <= len(result): + if fast == len(result) or result[fast] == " ": + self.single_reverse(result, slow, fast - 1) + slow = fast + 1 + fast += 1 + else: + fast += 1 + + return "".join(result) +``` ### Go: 版本一: From 4f9d9491ae07d68cdd671a28b1b33c294569e00d Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 11 Sep 2024 17:30:52 +0800 Subject: [PATCH 1293/1533] =?UTF-8?q?494.=E7=9B=AE=E6=A0=87=E5=92=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E4=BA=8C=E7=BB=B4dp=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 82d330b727..dc8d6c0fbb 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -675,6 +675,57 @@ class Solution: ``` ### Go +二维dp +```go +func findTargetSumWays(nums []int, target int) int { + sum := 0 + for _, v := range nums { + sum += v + } + if math.Abs(float64(target)) > float64(sum) { + return 0 // 此时没有方案 + } + if (target + sum) % 2 == 1 { + return 0 // 此时没有方案 + } + bagSize := (target + sum) / 2 + + dp := make([][]int, len(nums)) + for i := range dp { + dp[i] = make([]int, bagSize + 1) + } + + // 初始化最上行 + if nums[0] <= bagSize { + dp[0][nums[0]] = 1 + } + + // 初始化最左列,最左列其他数值在递推公式中就完成了赋值 + dp[0][0] = 1 + + var numZero float64 + for i := range nums { + if nums[i] == 0 { + numZero++ + } + dp[i][0] = int(math.Pow(2, numZero)) + } + + // 以下遍历顺序行列可以颠倒 + for i := 1; i < len(nums); i++ { // 行,遍历物品 + for j := 0; j <= bagSize; j++ { // 列,遍历背包 + if nums[i] > j { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i]] + } + } + } + return dp[len(nums)-1][bagSize] +} +``` + +一维dp ```go func findTargetSumWays(nums []int, target int) int { sum := 0 From b5e3b801d37b290b48d03cdca1ef3a81d680e95b Mon Sep 17 00:00:00 2001 From: Elio Zhou <30508272+eliozh@users.noreply.github.com> Date: Wed, 11 Sep 2024 18:56:36 +0800 Subject: [PATCH 1294/1533] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化Rust版本0236.二叉树的最近公共祖先代码 --- ...\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 5e80e702f3..2e94f7a7e3 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -454,7 +454,11 @@ impl Solution { p: Option>>, q: Option>>, ) -> Option>> { - if root == p || root == q || root.is_none() { + if root.is_none() { + return root; + } + if Rc::ptr_eq(root.as_ref().unwrap(), p.as_ref().unwrap()) + || Rc::ptr_eq(root.as_ref().unwrap(), q.as_ref().unwrap()) { return root; } let left = Self::lowest_common_ancestor( From b7ddc4a4abdb8cde5e020d47d7ed37e18a9df91b Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 12 Sep 2024 16:07:01 +0800 Subject: [PATCH 1295/1533] =?UTF-8?q?518.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2II=E5=A2=9E=E5=8A=A0Go=E4=BA=8C=E7=BB=B4dp=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\351\222\261\345\205\221\346\215\242II.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 255912d62b..bef62b304d 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -268,6 +268,7 @@ class Solution: ### Go: +一维dp ```go func change(amount int, coins []int) int { // 定义dp数组 @@ -286,6 +287,29 @@ func change(amount int, coins []int) int { return dp[amount] } ``` +二维dp +```go +func change(amount int, coins []int) int { + dp := make([][]int, len(coins)) + for i := range dp { + dp[i] = make([]int, amount + 1) + dp[i][0] = 1 + } + for j := coins[0]; j <= amount; j++ { + dp[0][j] += dp[0][j-coins[0]] + } + for i := 1; i < len(coins); i++ { + for j := 1; j <= amount; j++ { + if j < coins[i] { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = dp[i][j-coins[i]] + dp[i-1][j] + } + } + } + return dp[len(coins)-1][amount] +} +``` ### Rust: From b5161e725dc25e2994a903935858d557707cef18 Mon Sep 17 00:00:00 2001 From: markwang Date: Fri, 13 Sep 2024 09:49:36 +0800 Subject: [PATCH 1296/1533] =?UTF-8?q?kama57.=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?=E8=A1=A5=E5=85=85Go=E8=A7=A3=E6=B3=95package=E5=92=8Cimport?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\203\214\345\214\205\347\211\210\346\234\254.md" | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 07e0261e8f..c51a590baf 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -184,6 +184,16 @@ if __name__ == '__main__': ### Go: ```go +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + func climbStairs(n int, m int) int { dp := make([]int, n+1) dp[0] = 1 From 8be83e8138532b03aa7b114a7bd84dae325b213c Mon Sep 17 00:00:00 2001 From: cyxiwai Date: Fri, 13 Sep 2024 10:02:13 +0800 Subject: [PATCH 1297/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B9=B6=E8=A1=A5?= =?UTF-8?q?=E5=85=85=E4=BA=86kama0109.=E5=86=97=E4=BD=99=E9=93=BE=E6=8E=A5?= =?UTF-8?q?II=E7=9A=84Java=E7=89=88=E6=9C=AC=EF=BC=8C=E9=99=84=E6=9C=89?= =?UTF-8?q?=E8=AF=A6=E7=BB=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\231\350\277\236\346\216\245II.md" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index fd834357fd..bd707bf6ae 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -250,7 +250,105 @@ int main() { ## 其他语言版本 ### Java +```java +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Main { + static int n; + static int[] father = new int[1001]; // 并查集数组 + + // 并查集初始化 + public static void init() { + for (int i = 1; i <= n; ++i) { + father[i] = i; + } + } + + // 并查集里寻根的过程 + public static int find(int u) { + if (u == father[u]) return u; + return father[u] = find(father[u]); // 路径压缩 + } + + // 将 v->u 这条边加入并查集 + public static void join(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + father[v] = u; // 合并两棵树 + } + } + // 判断 u 和 v 是否有同一个根 + public static boolean same(int u, int v) { + return find(u) == find(v); + } + + // 在有向图里找到删除的那条边,使其变成树 + public static void getRemoveEdge(List edges) { + init(); // 初始化并查集 + for (int i = 0; i < n; i++) { // 遍历所有的边 + if (same(edges.get(i)[0], edges.get(i)[1])) { // 如果构成有向环了,就是要删除的边 + System.out.println(edges.get(i)[0] + " " + edges.get(i)[1]); + return; + } else { + join(edges.get(i)[0], edges.get(i)[1]); + } + } + } + + // 删一条边之后判断是不是树 + public static boolean isTreeAfterRemoveEdge(List edges, int deleteEdge) { + init(); // 初始化并查集 + for (int i = 0; i < n; i++) { + if (i == deleteEdge) continue; + if (same(edges.get(i)[0], edges.get(i)[1])) { // 如果构成有向环了,一定不是树 + return false; + } + join(edges.get(i)[0], edges.get(i)[1]); + } + return true; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + List edges = new ArrayList<>(); // 存储所有的边 + + n = sc.nextInt(); // 顶点数 + int[] inDegree = new int[n + 1]; // 记录每个节点的入度 + for (int i = 0; i < n; i++) { + int s = sc.nextInt(); // 边的起点 + int t = sc.nextInt(); // 边的终点 + inDegree[t]++; + edges.add(new int[]{s, t}); // 将边加入列表 + } + + List vec = new ArrayList<>(); // 记录入度为2的边(如果有的话就两条边) + // 找入度为2的节点所对应的边,注意要倒序,因为优先删除最后出现的一条边 + for (int i = n - 1; i >= 0; i--) { + if (inDegree[edges.get(i)[1]] == 2) { + vec.add(i); + } + } + + // 情况一、情况二 + if (vec.size() > 0) { + // vec里的边已经按照倒叙放的,所以优先删 vec.get(0) 这条边 + if (isTreeAfterRemoveEdge(edges, vec.get(0))) { + System.out.println(edges.get(vec.get(0))[0] + " " + edges.get(vec.get(0))[1]); + } else { + System.out.println(edges.get(vec.get(1))[0] + " " + edges.get(vec.get(1))[1]); + } + return; + } + + // 处理情况三:明确没有入度为2的情况,一定有有向环,找到构成环的边返回即可 + getRemoveEdge(edges); + } +} +``` ### Python ### Go From d6cd5e0ceff6c26dde8083b81b9316b832721bfc Mon Sep 17 00:00:00 2001 From: Yufan SHENG Date: Fri, 13 Sep 2024 14:28:49 +1000 Subject: [PATCH 1298/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00110=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91Python3=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95=E7=B2=BE=E7=AE=80=E7=89=88=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\350\241\241\344\272\214\345\217\211\346\240\221.md" | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index f8071333f0..ec120bcce5 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -609,10 +609,13 @@ class Solution: while stack: node = stack.pop() if node: - stack.append(node) + stack.append(node) # 中 stack.append(None) - if node.left: stack.append(node.left) - if node.right: stack.append(node.right) + # 采用数组进行迭代,先将右子树加入,保证左节点能够先出栈 + if node.right: # 右 + stack.append(node.right) + if node.left: # 左 + stack.append(node.left) else: real_node = stack.pop() left, right = height_map.get(real_node.left, 0), height_map.get(real_node.right, 0) From 507d9e04308e1859558912a3797e6f9f59106303 Mon Sep 17 00:00:00 2001 From: Yufan Sheng <18829237653@163.com> Date: Fri, 13 Sep 2024 14:40:58 +1000 Subject: [PATCH 1299/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91=E4=B8=AD=E7=9A=84=E4=B8=8A?= =?UTF-8?q?=E6=AC=A1=E4=BF=AE=E6=94=B9=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index ec120bcce5..a4339ac3d7 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -611,7 +611,7 @@ class Solution: if node: stack.append(node) # 中 stack.append(None) - # 采用数组进行迭代,先将右子树加入,保证左节点能够先出栈 + # 采用数组进行迭代,先将右节点加入,保证左节点能够先出栈 if node.right: # 右 stack.append(node.right) if node.left: # 左 From 513440ad8f22c6d9a14ceb23a78e0dfde7649c67 Mon Sep 17 00:00:00 2001 From: Chemxy Date: Fri, 13 Sep 2024 19:34:17 +0800 Subject: [PATCH 1300/1533] =?UTF-8?q?new=20=EF=BC=9A=E6=96=B0=E5=A2=9ECang?= =?UTF-8?q?jie=E8=A7=A3=E9=A2=98=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...07\345\255\220\345\272\217\345\210\227.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 9ee7bef38a..f1a146b71c 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -337,6 +337,29 @@ pub fn length_of_lis(nums: Vec) -> i32 { } ``` +### Cangjie: + +```cangjie +func lengthOfLIS(nums: Array): Int64 { + let n = nums.size + if (n <= 1) { + return n + } + + let dp = Array(n, item: 1) + var res = 0 + for (i in 1..n) { + for (j in 0..i) { + if (nums[i] > nums[j]) { + dp[i] = max(dp[i], dp[j] + 1) + } + } + res = max(dp[i], res) + } + return res +} +``` +

From 8127baf3fe23d735a4135900605ad9ebc3ebee63 Mon Sep 17 00:00:00 2001 From: Chemxy Date: Fri, 13 Sep 2024 20:40:37 +0800 Subject: [PATCH 1301/1533] =?UTF-8?q?new=EF=BC=9A=E6=96=B0=E5=A2=9ECangjie?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=EF=BC=9A=E6=9C=80=E9=95=BF=E8=BF=9E=E7=BB=AD?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\242\236\345\272\217\345\210\227.md" | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index cebb552bf8..fe882e0594 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -491,7 +491,24 @@ int findLengthOfLCIS(int* nums, int numsSize) { return result; } ``` - +### Cangjie +```cangjie +func findLengthOfLCIS(nums: Array): Int64 { + let n = nums.size + if (n <= 1) { + return n + } + let dp = Array(n, repeat: 1) + var res = 0 + for (i in 1..n) { + if (nums[i] > nums[i - 1]) { + dp[i] = dp[i - 1] + 1 + } + res = max(res, dp[i]) + } + return res +} +``` From 60f34628ebadd556d639ae3792af98df65c3476a Mon Sep 17 00:00:00 2001 From: Chemxy Date: Fri, 13 Sep 2024 20:42:36 +0800 Subject: [PATCH 1302/1533] =?UTF-8?q?new=EF=BC=9A=E6=96=B0=E5=A2=9ECangjie?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=EF=BC=9A=E6=9C=80=E9=95=BF=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=AD=90=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...215\345\255\220\346\225\260\347\273\204.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 6c8e7101a1..19520d131d 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -581,6 +581,24 @@ int findLength(int* nums1, int nums1Size, int* nums2, int nums2Size) { } ``` +### Cangjie +```cangjie +func findLength(nums1: Array, nums2: Array): Int64 { + let n = nums1.size + let m = nums2.size + let dp = Array(n + 1, {_ => Array(m + 1, item: 0)}) + var res = 0 + for (i in 1..=n) { + for (j in 1..=m) { + if (nums1[i - 1] == nums2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1 + } + res = max(res, dp[i][j]) + } + } + return res +} +```

From d742497b528e028cff4f756524ed1d9ddc24c8da Mon Sep 17 00:00:00 2001 From: Chemxy Date: Fri, 13 Sep 2024 23:05:37 +0800 Subject: [PATCH 1303/1533] =?UTF-8?q?new=EF=BC=9A=E6=96=B0=E5=A2=9ECangjie?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=EF=BC=9A=E6=9C=80=E9=95=BF=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...261\345\255\220\345\272\217\345\210\227.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 7fa7bb68e1..93df987e3d 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -399,6 +399,24 @@ int longestCommonSubsequence(char* text1, char* text2) { } ``` +### Cangjie +```cangjie +func longestCommonSubsequence(text1: String, text2: String): Int64 { + let n = text1.size + let m = text2.size + let dp = Array(n + 1, {_ => Array(m + 1, repeat: 0)}) + for (i in 1..=n) { + for (j in 1..=m) { + if (text1[i - 1] == text2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1 + } else { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + } + } + } + return dp[n][m] +} +```

From 8b90ac06bf65ccfa036baa6629a37eb948008108 Mon Sep 17 00:00:00 2001 From: Chemxy Date: Fri, 13 Sep 2024 23:08:46 +0800 Subject: [PATCH 1304/1533] =?UTF-8?q?fix=EF=BC=9A=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E9=97=B4=E8=B7=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" | 2 ++ ...\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" | 1 + ...\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" | 1 + 3 files changed, 4 insertions(+) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index fe882e0594..57a38404b2 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -491,7 +491,9 @@ int findLengthOfLCIS(int* nums, int numsSize) { return result; } ``` + ### Cangjie + ```cangjie func findLengthOfLCIS(nums: Array): Int64 { let n = nums.size diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 19520d131d..1391926a76 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -582,6 +582,7 @@ int findLength(int* nums1, int nums1Size, int* nums2, int nums2Size) { ``` ### Cangjie + ```cangjie func findLength(nums1: Array, nums2: Array): Int64 { let n = nums1.size diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 93df987e3d..25f3283864 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -400,6 +400,7 @@ int longestCommonSubsequence(char* text1, char* text2) { ``` ### Cangjie + ```cangjie func longestCommonSubsequence(text1: String, text2: String): Int64 { let n = text1.size From 465129122676512c0ec83963e1138b0ad051ac7f Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sat, 14 Sep 2024 11:22:01 +0800 Subject: [PATCH 1305/1533] Update --- ...20\345\255\227\347\254\246\344\270\262.md" | 199 ++++++++++++++++-- ...4.\347\233\256\346\240\207\345\222\214.md" | 39 +++- problems/kamacoder/.DS_Store | Bin 0 -> 6148 bytes ...27\344\275\231\350\277\236\346\216\245.md" | 53 ++++- ...7\232\204\346\224\273\345\207\273astar.md" | 32 +++ ...47\241\20001\350\203\214\345\214\205-2.md" | 46 ++-- 6 files changed, 319 insertions(+), 50 deletions(-) create mode 100644 problems/kamacoder/.DS_Store diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 5142579681..254d921dbd 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -56,10 +56,66 @@ ![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728104931.png) -所以判断字符串s是否由重复子串组成,只要两个s拼接在一起,里面还出现一个s的话,就说明是由重复子串组成。 当然,我们在判断 s + s 拼接的字符串里是否出现一个s的的时候,**要刨除 s + s 的首字符和尾字符**,这样避免在s+s中搜索出原来的s,我们要搜索的是中间拼接出来的s。 + +以上证明的充分性,接下来证明必要性: + +如果有一个字符串s,在 s + s 拼接后, 不算首尾字符,如果能凑成s字符串,说明s 一定是重复子串组成。 + +如图,字符串s,图中数字为数组下标,在 s + s 拼接后, 不算首尾字符,中间凑成s字符串。 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910115555.png) + +图中,因为中间拼接成了s,根据红色框 可以知道 s[4] = s[0], s[5] = s[1], s[0] = s[2], s[1] = s[3] s[2] = s[4] ,s[3] = s[5] + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910115819.png) + +以上相等关系我们串联一下: + +s[4] = s[0] = s[2] + +s[5] = s[1] = s[3] + + +即:s[4],s[5] = s[0],s[1] = s[2],s[3] + +**说明这个字符串,是由 两个字符 s[0] 和 s[1] 重复组成的**! + +这里可以有录友想,凭什么就是这样组成的s呢,我换一个方式组成s 行不行,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910120751.png) + +s[3] = s[0],s[4] = s[1] ,s[5] = s[2],s[0] = s[3],s[1] = s[4],s[2] = s[5] + +以上相等关系串联: + +s[3] = s[0] + +s[1] = s[4] + +s[2] = s[5] + +s[0] s[1] s[2] = s[3] s[4] s[5] + +和以上推导过程一样,最后可以推导出,这个字符串是由 s[0] ,s[1] ,s[2] 重复组成。 + +如果是这样的呢,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910121236.png) + +s[1] = s[0],s[2] = s[1] ,s[3] = s[2],s[4] = s[3],s[5] = s[4],s[0] = s[5] + +以上相等关系串联 + +s[0] = s[1] = s[2] = s[3] = s[4] = s[5] + +最后可以推导出,这个字符串是由 s[0] 重复组成。 + +以上 充分和必要性都证明了,所以判断字符串s是否由重复子串组成,只要两个s拼接在一起,里面还出现一个s的话,就说明是由重复子串组成。 + + 代码如下: ```CPP @@ -76,13 +132,14 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1) -不过这种解法还有一个问题,就是 我们最终还是要判断 一个字符串(s + s)是否出现过 s 的过程,大家可能直接用contains,find 之类的库函数。 却忽略了实现这些函数的时间复杂度(暴力解法是m * n,一般库函数实现为 O(m + n))。 +不过这种解法还有一个问题,就是 我们最终还是要判断 一个字符串(s + s)是否出现过 s 的过程,大家可能直接用contains,find 之类的库函数, 却忽略了实现这些函数的时间复杂度(暴力解法是m * n,一般库函数实现为 O(m + n))。 如果我们做过 [28.实现strStr](https://programmercarl.com/0028.实现strStr.html) 题目的话,其实就知道,**实现一个 高效的算法来判断 一个字符串中是否出现另一个字符串是很复杂的**,这里就涉及到了KMP算法。 ### KMP #### 为什么会使用KMP + 以下使用KMP方式讲解,强烈建议大家先把以下两个视频看了,理解KMP算法,再来看下面讲解,否则会很懵。 * [视频讲解版:帮你把KMP算法学个通透!(理论篇)](https://www.bilibili.com/video/BV1PD4y1o7nd/) @@ -91,7 +148,9 @@ public: 在一个串中查找是否出现过另一个串,这是KMP的看家本领。那么寻找重复子串怎么也涉及到KMP算法了呢? -KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一个匹配过的位置继续匹配,靠的是有计算好的前缀表。 前缀表里,统计了各个位置为终点字符串的最长相同前后缀的长度。 +KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一个匹配过的位置继续匹配,靠的是有计算好的前缀表。 + +前缀表里,统计了各个位置为终点字符串的最长相同前后缀的长度。 那么 最长相同前后缀和重复子串的关系又有什么关系呢。 @@ -100,16 +159,61 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 * 前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串; * 后缀是指不包含第一个字符的所有以最后一个字符结尾的连续子串 -在由重复子串组成的字符串中,最长相等前后缀不包含的子串就是最小重复子串,这里拿字符串s:abababab 来举例,ab就是最小重复单位,如图所示: +#### 充分性证明 + +如果一个字符串s是由重复子串组成,那么 最长相等前后缀不包含的子串一定是字符串s的最小重复子串。 + +证明: 如果s 是有是有最小重复子串p组成。 + +即 s = n * p + +那么相同前后缀可以是这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110257.png) + +也可以是这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110316.png) + +最长的相等前后缀,也就是这样: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110841.png) + +这里有录友就想:如果字符串s 是有是有最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样: -![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728205249.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114348.png) +如果这样的话,因为前后缀要相同,所以 p2 = p1,p3 = p2,如图: -#### 如何找到最小重复子串 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114818.png) -这里有同学就问了,为啥一定是开头的ab呢。 其实最关键还是要理解 最长相等前后缀,如图: +p2 = p1,p3 = p2 即: p1 = p2 = p3 -![图四](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728212157.png) +说明 p = p1 * 3。 + +这样p 就不是最小重复子串了,不符合我们定义的条件。 + +所以,**如果这个字符串s是由重复子串组成,那么最长相等前后缀不包含的子串是字符串s的最小重复子串**。 + +#### 必要性证明 + +以上是充分性证明,以下是必要性证明: + +**如果 最长相等前后缀不包含的子串是字符串s的最小重复子串, 那么字符串s一定由重复子串组成吗**? + +最长相等前后缀不包含的子串已经是字符串s的最小重复子串,那么字符串s一定由重复子串组成,这个不需要证明了。 + +关键是要要证明:最长相等前后缀不包含的子串什么时候才是字符串s的最小重复子串呢。 + +情况一, 最长相等前后缀不包含的子串的长度 比 字符串s的一半的长度还大,那一定不是字符串s的重复子串 + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240911110236.png) + +-------------- + +情况二,最长相等前后缀不包含的子串的长度 可以被 字符串s的长度整除,如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910174249.png) 步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,所以 s[0] 一定和 s[2]相同,s[1] 一定和 s[3]相同,即:,s[0]s[1]与s[2]s[3]相同 。 @@ -121,28 +225,79 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 所以字符串s,s[0]s[1]与s[2]s[3]相同, s[2]s[3] 与 s[4]s[5]相同,s[4]s[5] 与 s[6]s[7] 相同。 -正是因为 最长相等前后缀的规则,当一个字符串由重复子串组成的,最长相等前后缀不包含的子串就是最小重复子串。 +可以推出,在由重复子串组成的字符串中,最长相等前后缀不包含的子串就是最小重复子串。 + +即 s[0]s[1] 是最小重复子串 + + +以上推导中,录友可能想,你怎么知道 s[0] 和 s[1] 就不相同呢? s[0] 为什么就不能使最小重复子串。 + +如果 s[0] 和 s[1] 也相同,同时 s[0]s[1]与s[2]s[3]相同,s[2]s[3] 与 s[4]s[5]相同,s[4]s[5] 与 s[6]s[7] 相同,那么这个字符串就是有一个字符构成的字符串。 + +那么它的最长相同前后缀,就不是上图中的前后缀,而是这样的的前后缀: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910175053.png) + +录友可能再问,由一个字符组成的字符串,最长相等前后缀凭什么就是这样的。 + +有这种疑惑的录友,就是还不知道 最长相等前后缀 是怎么算的。 + +可以看这里:[KMP讲解](https://programmercarl.com/0028.%E5%AE%9E%E7%8E%B0strStr.html),再去回顾一下。 -#### 简单推理 +或者说,自己举个例子,`aaaaaa`,这个字符串,他的最长相等前后缀是什么? -这里再给出一个数学推导,就容易理解很多。 +同上以上推导,最长相等前后缀不包含的子串的长度只要被 字符串s的长度整除,就是一定是最小重复子串。 -假设字符串s使用多个重复子串构成(这个子串是最小重复单位),重复出现的子字符串长度是x,所以s是由n * x组成。 +---------------- -因为字符串s的最长相同前后缀的长度一定是不包含s本身,所以 最长相同前后缀长度必然是m * x,而且 n - m = 1,(这里如果不懂,看上面的推理) +**情况三,最长相等前后缀不包含的子串的长度 不被 字符串s的长度整除得情况**,如图: -所以如果 nx % (n - m)x = 0,就可以判定有重复出现的子字符串。 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913115854.png) -next 数组记录的就是最长相同前后缀 [字符串:KMP算法精讲](https://programmercarl.com/0028.实现strStr.html) 这里介绍了什么是前缀,什么是后缀,什么又是最长相同前后缀), 如果 next[len - 1] != -1,则说明字符串有最长相同的前后缀(就是字符串里的前缀子串和后缀子串相同的最长长度)。 -最长相等前后缀的长度为:next[len - 1] + 1。(这里的next数组是以统一减一的方式计算的,因此需要+1,两种计算next数组的具体区别看这里:[字符串:KMP算法精讲](https://programmercarl.com/0028.实现strStr.html)) +步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,t[2] 与 k[2]相同。 + +所以 s[0] 与 s[3]相同,s[1] 与 s[4]相同,s[2] 与s[5],即:,s[0]s[1]与s[2]s[3]相同 。 + +步骤二: 因为在同一个字符串位置,所以 t[3] 与 k[0]相同,t[4] 与 k[1]相同。 + + +步骤三: 因为 这是相等的前缀和后缀,t[3] 与 k[3]相同 ,t[4]与k[5] 相同,所以,s[3]一定和s[6]相同,s[4]一定和s[7]相同,即:s[3]s[4] 与 s[6]s[7]相同。 + + +以上推导,可以得出 s[0],s[1],s[2] 与 s[3],s[4],s[5] 相同,s[3]s[4] 与 s[6]s[7]相同。 + +那么 最长相等前后缀不包含的子串的长度 不被 字符串s的长度整除 ,就不是s的重复子串 + +----------- + +充分条件:如果字符串s是由重复子串组成,那么 最长相等前后缀不包含的子串 一定是 s的最小重复子串。 + +必要条件:如果字符串s的最长相等前后缀不包含的子串 是 s最小重复子串,那么 s是由重复子串组成。 + +在必要条件,这个是 显而易见的,都已经假设 最长相等前后缀不包含的子串 是 s的最小重复子串了,那s必然是重复子串。 + +关键是需要证明, 字符串s的最长相等前后缀不包含的子串 什么时候才是 s最小重复子串。 + +同上我们证明了,当 最长相等前后缀不包含的子串的长度 可以被 字符串s的长度整除,那么不包含的子串 就是s的最小重复子串。 + + +------------- + + +### 代码分析 + +next 数组记录的就是最长相同前后缀( [字符串:KMP算法精讲](https://programmercarl.com/0028.实现strStr.html)), 如果 `next[len - 1] != -1`,则说明字符串有最长相同的前后缀(就是字符串里的前缀子串和后缀子串相同的最长长度)。 + +最长相等前后缀的长度为:`next[len - 1] + 1`。(这里的next数组是以统一减一的方式计算的,因此需要+1,两种计算next数组的具体区别看这里:[字符串:KMP算法精讲](https://programmercarl.com/0028.实现strStr.html)) 数组长度为:len。 -如果len % (len - (next[len - 1] + 1)) == 0 ,则说明数组的长度正好可以被 (数组长度-最长相等前后缀的长度) 整除 ,说明该字符串有重复的子字符串。 +`len - (next[len - 1] + 1)` 是最长相等前后缀不包含的子串的长度。 -**数组长度减去最长相同前后缀的长度相当于是第一个周期的长度,也就是一个周期的长度,如果这个周期可以被整除,就说明整个数组就是这个周期的循环。** +如果`len % (len - (next[len - 1] + 1)) == 0` ,则说明数组的长度正好可以被 最长相等前后缀不包含的子串的长度 整除 ,说明该字符串有重复的子字符串。 +### 打印数组 **强烈建议大家把next数组打印出来,看看next数组里的规律,有助于理解KMP算法** @@ -150,10 +305,14 @@ next 数组记录的就是最长相同前后缀 [字符串:KMP算法精讲](ht ![459.重复的子字符串_1](https://code-thinking.cdn.bcebos.com/pics/459.重复的子字符串_1.png) -next[len - 1] = 7,next[len - 1] + 1 = 8,8就是此时字符串asdfasdfasdf的最长相同前后缀的长度。 +`next[len - 1] = 7`,`next[len - 1] + 1 = 8`,8就是此时字符串asdfasdfasdf的最长相同前后缀的长度。 + +`(len - (next[len - 1] + 1))` 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 为最长相同前后缀不包含的子串长度 + -(len - (next[len - 1] + 1)) 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 4正好可以被 12(字符串的长度) 整除,所以说明有重复的子字符串(asdf)。 +4可以被 12(字符串的长度) 整除,所以说明有重复的子字符串(asdf)。 +### 打码实现 C++代码如下:(这里使用了前缀表统一减一的实现方式) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 82d330b727..19d34e4f94 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -61,7 +61,7 @@ left + right = sum,而sum是固定的。right = sum - left -公式来了, left - (sum - left) = target 推导出 left = (target + sum)/2 。 +left - (sum - left) = target 推导出 left = (target + sum)/2 。 target是固定的,sum是固定的,left就可以求出来。 @@ -126,7 +126,7 @@ public: x = (target + sum) / 2 -**此时问题就转化为,装满容量为x的背包,有几种方法**。 +**此时问题就转化为,用nums装满容量为x的背包,有几种方法**。 这里的x,就是bagSize,也就是我们后面要求的背包容量。 @@ -161,6 +161,8 @@ if (abs(target) > sum) return 0; // 此时没有方案 我们先手动推导一下,这个二维数组里面的数值。 +------------ + 先只考虑物品0,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808161747.png) @@ -173,6 +175,8 @@ if (abs(target) > sum) return 0; // 此时没有方案 装满背包容量为2 的方法个数是0,目前没有办法能装满容量为2的背包。 +-------------- + 接下来 考虑 物品0 和 物品1,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162052.png) @@ -185,6 +189,8 @@ if (abs(target) > sum) return 0; // 此时没有方案 其他容量都不能装满,所以方法是0。 +----------------- + 接下来 考虑 物品0 、物品1 和 物品2 ,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162533.png) @@ -193,10 +199,12 @@ if (abs(target) > sum) return 0; // 此时没有方案 装满背包容量为1 的方法个数是3,即 放物品0 或者 放物品1 或者 放物品2。 -装满背包容量为2 的方法个数是3,即 放物品0 和 放物品1、放物品0 和 物品 2、 放物品1 和 物品2。 +装满背包容量为2 的方法个数是3,即 放物品0 和 放物品1、放物品0 和 物品2、放物品1 和 物品2。 装满背包容量为3的方法个数是1,即 放物品0 和 物品1 和 物品2。 +--------------- + 通过以上举例,我们来看 dp[2][2] 可以有哪些方向推出来。 如图红色部分: @@ -229,7 +237,7 @@ dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物 在上面图中,你把物品2补上就好,同样是两种方法。 -dp[2][2] = 容量为2的背包不放物品2有几种方法 + 容量为2的背包不放物品2有几种方法 +dp[2][2] = 容量为2的背包不放物品2有几种方法 + 容量为2的背包放物品2有几种方法 所以 dp[2][2] = dp[1][2] + dp[1][1] ,如图: @@ -284,6 +292,29 @@ dp[0][j]:只放物品0, 把容量为j的背包填满有几种方法。 即 dp[i][0] = 1 +但这里有例外,就是如果 物品数值就是0呢? + +如果有两个物品,物品0为0, 物品1为0,装满背包容量为0的方法有几种。 + +* 放0件物品 +* 放物品0 +* 放物品1 +* 放物品0 和 物品1 + +此时是有4种方法。 + +其实就是算数组里有t个0,然后按照组合数量求,即 2^t 。 + +初始化如下: + +```CPP +int numZero = 0; +for (int i = 0; i < nums.size(); i++) { + if (nums[i] == 0) numZero++; + dp[i][0] = (int) pow(2.0, numZero); +} +``` + 4. 确定遍历顺序 在明确递推方向时,我们知道 当前值 是由上方和左上方推出。 diff --git a/problems/kamacoder/.DS_Store b/problems/kamacoder/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0= costs[i]; j--) { + // 考虑当前研究材料选择和不选择的情况,选择最大值 + dp[j] = Math.max(dp[j], dp[j - costs[i]] + values[i]); } } - System.out.println(dp[n - 1][bagweight]); + // 输出 dp[N],即在给定 N 行李空间可以携带的研究材料的最大价值 + System.out.println(dp[N]); + + scanner.close(); } } From 2de1a8647fc7eec86042f16bd8191b6dd5b48587 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sat, 14 Sep 2024 12:01:45 +0800 Subject: [PATCH 1306/1533] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E9=9D=9E=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=9A=8F=E6=83=B3=E5=BD=95=E4=BD=93=E7=B3=BB=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=E5=88=B0=E5=8D=A1=E7=A0=81=E7=BD=91=E4=BB=93=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...66\350\241\214\345\210\227\345\274\217.md" | 27 -- .../0112.\346\214\221\346\210\230boss.md" | 26 -- ...75\351\231\205\350\261\241\346\243\213.md" | 59 ----- ...04\345\271\263\345\235\207\346\225\260.md" | 29 --- ...04\350\243\205\346\211\213\346\234\272.md" | 67 ----- ...47\346\225\260\345\207\217\346\263\225.md" | 91 ------- ...72\351\227\264\347\277\273\350\275\254.md" | 132 ---------- ...43\346\234\200\345\244\247\345\200\274.md" | 127 ---------- ...60\347\273\204\346\236\204\351\200\240.md" | 52 ---- ...76\345\215\216\345\270\226\345\255\220.md" | 38 --- ...04\346\234\200\345\244\247\345\222\214.md" | 66 ----- ...22\345\210\227\350\257\242\351\227\256.md" | 29 --- ...16\350\265\260\345\205\254\350\267\257.md" | 35 --- ...13\347\263\225\345\210\207\345\211\262.md" | 51 ---- ...46\344\270\262\345\217\230\346\215\242.md" | 78 ------ ...21\344\270\212\346\237\223\350\211\262.md" | 134 ---------- ...2.\345\244\271\345\220\203\346\227\227.md" | 47 ---- ...00\345\260\217\346\255\245\346\225\260.md" | 77 ------ ...02\347\202\271\345\210\227\350\241\250.md" | 147 ----------- ...62\345\244\204\347\220\206\345\231\250.md" | 148 ----------- ...10\346\201\257\344\274\240\350\276\223.md" | 192 -------------- ...9.\345\217\257\347\210\261\344\270\262.md" | 101 -------- ...9.\345\256\214\347\276\216\346\225\260.md" | 29 --- ...75\344\272\214\345\217\211\346\240\221.md" | 104 -------- ...40\351\231\244\346\200\273\345\222\214.md" | 108 -------- ...14\345\200\274\350\267\257\345\276\204.md" | 237 ------------------ ...01\345\255\227\347\254\246\344\270\262.md" | 66 ----- ...27\347\232\204\346\216\222\345\210\227.md" | 98 -------- ...6.\344\274\240\351\200\201\346\240\221.md" | 65 ----- ...11\347\217\240\344\272\222\346\226\245.md" | 78 ------ ...14\345\220\214\350\212\261\351\241\272.md" | 122 --------- ...9.\345\245\275\346\225\260\347\273\204.md" | 102 -------- ...65\347\232\204\346\235\203\345\200\274.md" | 66 ----- ...14\347\232\204\347\247\230\345\257\206.md" | 127 ---------- ...32\344\277\241\346\240\241\345\207\206.md" | 121 --------- ...57\345\276\204\350\256\241\346\225\260.md" | 95 ------- ...27\344\270\255\344\275\215\346\225\260.md" | 68 ----- ...40\351\231\244\344\273\243\344\273\267.md" | 106 -------- ...30\346\226\227\345\272\217\345\210\227.md" | 68 ----- ...01\345\244\215\346\235\202\345\272\246.md" | 59 ----- ...14\344\275\231\346\226\271\347\250\213.md" | 50 ---- ...64\346\225\260\344\271\230\346\263\225.md" | 62 ----- ...04\346\212\230\347\272\277\346\256\265.md" | 88 ------- ...04\345\220\210\345\270\226\345\255\220.md" | 61 ----- ...30\347\247\200\346\225\260\347\273\204.md" | 88 ------- ...07\345\272\217\346\225\260\347\273\204.md" | 81 ------ ...40\351\207\215\345\244\215\344\270\262.md" | 82 ------ 47 files changed, 3984 deletions(-) delete mode 100644 "problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" delete mode 100644 "problems/kamacoder/0112.\346\214\221\346\210\230boss.md" delete mode 100644 "problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" delete mode 100644 "problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" delete mode 100644 "problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" delete mode 100644 "problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" delete mode 100644 "problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" delete mode 100644 "problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" delete mode 100644 "problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" delete mode 100644 "problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" delete mode 100644 "problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" delete mode 100644 "problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" delete mode 100644 "problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" delete mode 100644 "problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" delete mode 100644 "problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" delete mode 100644 "problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" delete mode 100644 "problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" delete mode 100644 "problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" delete mode 100644 "problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" delete mode 100644 "problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" delete mode 100644 "problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" delete mode 100644 "problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" delete mode 100644 "problems/kamacoder/0139.\345\256\214\347\276\216\346\225\260.md" delete mode 100644 "problems/kamacoder/0141.\345\245\275\344\272\214\345\217\211\346\240\221.md" delete mode 100644 "problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" delete mode 100644 "problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" delete mode 100644 "problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" delete mode 100644 "problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" delete mode 100644 "problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" delete mode 100644 "problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" delete mode 100644 "problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" delete mode 100644 "problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" delete mode 100644 "problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" delete mode 100644 "problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" delete mode 100644 "problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" delete mode 100644 "problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" delete mode 100644 "problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" delete mode 100644 "problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" delete mode 100644 "problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" delete mode 100644 "problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" delete mode 100644 "problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" delete mode 100644 "problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" delete mode 100644 "problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" delete mode 100644 "problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" delete mode 100644 "problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" delete mode 100644 "problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" delete mode 100644 "problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" diff --git "a/problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" "b/problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" deleted file mode 100644 index efb304ebd1..0000000000 --- "a/problems/kamacoder/0111.\346\236\204\351\200\240\344\272\214\351\230\266\350\241\214\345\210\227\345\274\217.md" +++ /dev/null @@ -1,27 +0,0 @@ - -# 111. 构造二阶行列式 - -暴力模拟就好,每个数不超过 20, 暴力枚举其实也没多大。 - -```CPP -#include -using namespace std; -int main() { - int n; - cin >> n; - for (int x = 1; x <= 20; x++) { - for (int y = 1; y <= 20; y++) { - for (int i = 1; i <= 20; i++) { - for (int j = 1; j <= 20; j++) { - if ((x * j - y * i) == n) { - cout << x << " " << y << endl; - cout << i << " " << j << endl; - return 0; - } - } - } - } - } - cout << -1 << endl; -} -``` diff --git "a/problems/kamacoder/0112.\346\214\221\346\210\230boss.md" "b/problems/kamacoder/0112.\346\214\221\346\210\230boss.md" deleted file mode 100644 index 781fb6e802..0000000000 --- "a/problems/kamacoder/0112.\346\214\221\346\210\230boss.md" +++ /dev/null @@ -1,26 +0,0 @@ - -# 112. 挑战boss - -本题题意有点绕,注意看一下 题目描述中的【提示信息】,但是在笔试中,是不给这样的提示信息的。 - -简单模拟: - -```CPP -#include -using namespace std; -int main() { - int n, a, b, k = 0; - cin >> n >> a >> b; - string s; - cin >> s; - int result = 0; - for (int i = 0; i < s.size(); i++) { - int cur = a + k * b; - result += cur; - ++k; - if (s[i] == 'x') k = 0; - } - cout << result << endl; - return 0; -} -``` diff --git "a/problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" "b/problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" deleted file mode 100644 index 966aced348..0000000000 --- "a/problems/kamacoder/0113.\345\233\275\351\231\205\350\261\241\346\243\213.md" +++ /dev/null @@ -1,59 +0,0 @@ - -# 113.国际象棋 - -广搜,但本题如果广搜枚举马和象的话会超时。 - -广搜要只枚举马的走位,同时判断是否在对角巷直接走象 - -```CPP -#include -using namespace std; -const int N = 100005, mod = 1000000007; -using ll = long long; -int n, ans; -int dir[][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, -1}, {-2, 1}}; -int main() { - int x1, y1, x2, y2; - cin >> n; - while (n--) { - scanf("%d%d%d%d", &x1, &y1, &x2, &y2); - if (x1 == x2 && y1 == y2) { - cout << 0 << endl; - continue; - } - // 判断象走一步到达 - int d = abs(x1 - x2) - abs(y1 - y2); - if (!d) {cout << 1 << endl; continue;} - // 判断马走一步到达 - bool one = 0; - for (int i = 0; i < 8; ++i) { - int dx = x1 + dir[i][0], dy = y1 + dir[i][1]; - if (dx == x2 && dy == y2) { - cout << 1 << endl; - one = true; - break; - } - } - if (one) continue; - // 接下来为两步的逻辑, 象走两步或者马走一步,象走一步 - // 象直接两步可以到达,这个计算是不是同颜色的格子,象可以在两步到达所有同颜色的格子 - int d2 = abs(x1 - x2) + abs(y1 - y2); - if (d2 % 2 == 0) { - cout << 2 << endl; - continue; - } - // 接下来判断马 + 象的组合 - bool two = 0; - for (int i = 0; i < 8; ++i) { - int dx = x1 + dir[i][0], dy = y1 + dir[i][1]; - int d = abs(dx - x2) - abs(dy - y2); - if (!d) {cout << 2 << endl; two = true; break;} - } - if (two) continue; - // 剩下的格子全都是三步到达的 - cout << 3 << endl; - } - return 0; -} - -``` diff --git "a/problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" "b/problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" deleted file mode 100644 index 1188d9159d..0000000000 --- "a/problems/kamacoder/0114.\345\260\217\346\254\247\347\232\204\345\271\263\345\235\207\346\225\260.md" +++ /dev/null @@ -1,29 +0,0 @@ - -# 114. 小欧的平均数 - -这道题非常的脑筋急转弯, 读题都要理解半天。 - -初步读题,感觉好像是求 如何最小加减,得到三个数的平均数。 - -但题意不是这样的。 - -小欧的说的三个数平衡,只是三个数里 任何两个数 相加都能被2整除, 那么 也就是说,这三个数 要么都是 奇数,要么都是偶数,才能达到小欧所说的平衡。 - -所以题目要求的,就是,三个数,最小加减1 几次 可以让三个数都变成奇数,或者都变成偶数。 - -所以最终的结果 不是1 就是0,没有其他的。 - -录友可能想,题目出的这么绕干啥? 没办法,企业的笔试题就是这样的。 - -```CPP -#include -#include -using namespace std; -int main() { - int x, y, z; - cin >> x >> y >> z; - int count = (x % 2 == 0) + (y % 2 == 0) + (z % 2 == 0); - cout << min(3 - count, count); -} -``` - diff --git "a/problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" "b/problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" deleted file mode 100644 index 8cae4a78a5..0000000000 --- "a/problems/kamacoder/0115.\347\273\204\350\243\205\346\211\213\346\234\272.md" +++ /dev/null @@ -1,67 +0,0 @@ - -# 115. 组装手机 - -这道题是比较难得哈希表题目。 把代码随想录哈希表章节理解透彻,做本题没问题。 - -思路是 - -1. 用哈希表记录 外壳售价 和 手机零件售价 出现的次数 -2. 记录总和出现的次数 -3. 遍历总和,减去 外壳售价,看 手机零件售价出现了几次 -4. 最后累加,取最大值 - -有一个需要注意的点: 数字可以重复,在计算个数的时候,如果计算重复的数字 - -例如 如果输入是 - -``` -4 -1 1 1 1 -1 1 1 1 -``` -那么输出应该是 4, 外壳售价 和 手机零件售价 是可以重复的。 - -代码如下: - -```CPP -#include -#include -#include -#include -using namespace std; -int main() { - int n; - cin >> n; - vector aVec(n, 0); - vector bVec(n, 0); - unordered_map aUmap; - unordered_map bUmap; - for (int i = 0; i < n; i++) { - cin >> aVec[i]; - aUmap[aVec[i]]++; - } - for (int i = 0; i < n; i++) { - cin >> bVec[i]; - bUmap[bVec[i]]++; - } - unordered_set uset; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++){ - uset.insert(aVec[i] + bVec[j]); - } - } - int result = 0; - for (int sum : uset) { - //cout << p.first << endl; - int count = 0; - for (pair p : aUmap) { - //cout << p.first - aVec[i] << endl; - if (sum - p.first > 0 && bUmap[sum - p.first] != 0) { - count += min(bUmap[sum - p.first], p.second); - } - } - result = max(result, count); - } - cout << result << endl; -} -``` diff --git "a/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" "b/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" deleted file mode 100644 index 84d55249fd..0000000000 --- "a/problems/kamacoder/0121.\345\244\247\346\225\260\345\207\217\346\263\225.md" +++ /dev/null @@ -1,91 +0,0 @@ - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

- -# 大数减法 - -本题测试数据超过int 和 longlong了,所以考察的使用 string 来模拟 两个大数的 加减操作。 - -当然如果使用python或者Java 使用库函数都可以水过。 - -使用字符串来模拟过程,需要处理以下几个问题: - -* 负号处理:要考虑正负数的处理,如果大数相减的结果是负数,需要在结果前加上负号。 -* 大数比较:在进行减法之前,需要确定哪个数大,以便知道结果是否需要添加负号。 -* 位数借位:处理大数相减时的借位问题,这类似于手动减法。 - -```CPP -#include -#include -#include -using namespace std; - -// 比较两个字符串表示的数字,返回1表示a > b,0表示a == b,-1表示a < b -int compareStrings(const string& a, const string& b) { - if (a.length() > b.length()) return 1; - if (a.length() < b.length()) return -1; - return a.compare(b); -} - -// 去除字符串左侧的前导零 -string removeLeadingZeros(const string& num) { - size_t start = 0; - while (start < num.size() && num[start] == '0') { - start++; - } - return start == num.size() ? "0" : num.substr(start); -} - -// 大数相减,假设a >= b -string subtractStrings(const string& a, const string& b) { - string result; - int len1 = a.length(), len2 = b.length(); - int carry = 0; - - for (int i = 0; i < len1; i++) { - int digitA = a[len1 - 1 - i] - '0'; - int digitB = i < len2 ? b[len2 - 1 - i] - '0' : 0; - - int digit = digitA - digitB - carry; - if (digit < 0) { - digit += 10; - carry = 1; - } else { - carry = 0; - } - - result.push_back(digit + '0'); - } - - // 去除结果中的前导零 - reverse(result.begin(), result.end()); - return removeLeadingZeros(result); -} - -string subtractLargeNumbers(const string& num1, const string& num2) { - string a = num1, b = num2; - - // 比较两个数的大小 - int cmp = compareStrings(a, b); - - if (cmp == 0) { - return "0"; // 如果两个数相等,结果为0 - } else if (cmp < 0) { - // 如果a < b,交换它们并在结果前加上负号 - swap(a, b); - return "-" + subtractStrings(a, b); - } else { - return subtractStrings(a, b); - } -} - -int main() { - string num1, num2; - cin >> num1 >> num2; - - string result = subtractLargeNumbers(num1, num2); - cout << result << endl; - - return 0; -} - -``` diff --git "a/problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" "b/problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" deleted file mode 100644 index 6e10aab7b8..0000000000 --- "a/problems/kamacoder/0121.\345\260\217\347\272\242\347\232\204\345\214\272\351\227\264\347\277\273\350\275\254.md" +++ /dev/null @@ -1,132 +0,0 @@ - -# 121. 小红的区间翻转 - -比较暴力的方式,就是直接模拟, 枚举所有 区间,然后检查其翻转的情况。 - -在检查翻转的时候,需要一些代码优化,否则容易超时。 - -```CPP -#include -#include -using namespace std; - -bool canTransform(const vector& a, const vector& b, int left, int right) { - // 提前检查翻转区间的值是否可以匹配 - for (int i = left, j = right; i <= right; i++, j--) { - if (a[i] != b[j]) { - return false; - } - } - // 检查翻转区间外的值是否匹配 - for (int i = 0; i < left; i++) { - if (a[i] != b[i]) { - return false; - } - } - for (int i = right + 1; i < a.size(); i++) { - if (a[i] != b[i]) { - return false; - } - } - return true; -} - -int main() { - int n; - cin >> n; - - vector a(n); - vector b(n); - - for (int i = 0; i < n; i++) { - cin >> a[i]; - } - - for (int i = 0; i < n; i++) { - cin >> b[i]; - } - - int count = 0; - - // 遍历所有可能的区间 - for (int left = 0; left < n; left++) { - for (int right = left; right < n; right++) { - // 检查翻转区间 [left, right] 后,a 是否可以变成 b - if (canTransform(a, b, left, right)) { - count++; - } - } - } - cout << count << endl; - return 0; -} -``` - -也可以事先计算好,最长公共前缀,和最长公共后缀。 - -在公共前缀和公共后缀之间的部分进行翻转操作,这样我们可以减少很多不必要的翻转尝试。 - -通过在公共前缀和后缀之间的部分,找到可以通过翻转使得 a 和 b 相等的区间。 - -以下 为评论区 卡码网用户:码鬼的C++代码 - -```CPP -#include -#include - -using namespace std; - -int main() { - int n; - cin >> n; - vector a(n), b(n); - for (int i = 0; i < n; i++) { - cin >> a[i]; - } - for (int i = 0; i < n; i++) { - cin >> b[i]; - } - - vector prefix(n, 0), suffix(n, 0); - - // 计算前缀相等的位置 - int p = 0; - while (p < n && a[p] == b[p]) { - prefix[p] = 1; - p++; - } - - // 计算后缀相等的位置 - int s = n - 1; - while (s >= 0 && a[s] == b[s]) { - suffix[s] = 1; - s--; - } - - int count = 0; - - // 遍历所有可能的区间 - for (int i = 0; i < n - 1; i++) { - for (int j = i + 1; j < n; j++) { - // 判断前缀和后缀是否相等 - if ((i == 0 || prefix[i - 1] == 1) && (j == n - 1 || suffix[j + 1] == 1)) { - // 判断翻转后的子数组是否和目标数组相同 - bool is_palindrome = true; - for (int k = 0; k <= (j - i) / 2; k++) { - if (a[i + k] != b[j - k]) { - is_palindrome = false; - break; - } - } - if (is_palindrome) { - count++; - } - } - } - } - - cout << count << endl; - - return 0; -} -``` diff --git "a/problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" deleted file mode 100644 index 7820d01e59..0000000000 --- "a/problems/kamacoder/0122.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ /dev/null @@ -1,127 +0,0 @@ - -# 滑动窗口最大值 - -本题是 [代码随想录:滑动窗口最大值](https://www.programmercarl.com/0239.%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.html) 的升级版。 - -在[代码随想录:滑动窗口最大值](https://www.programmercarl.com/0239.%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.html) 中详细讲解了如何求解 滑动窗口的最大值。 - -那么求滑动窗口的最小值原理也是一样的, 大家稍加思考,把优先级队列里的 大于 改成小于 就行了。 - -求最大值的优先级队列(从大到小) -``` -while (!que.empty() && value > que.back()) { -``` - -求最小值的优先级队列(从小到大) -``` -while (!que.empty() && value > que.back()) { -``` - -这样在滑动窗口里 最大值最小值都求出来了,遍历一遍找出 差值最大的就好。 - -至于输入,需要一波字符串处理,比较考察基本功。 - -CPP代码如下: - -```CPP -#include -#include -#include -#include -#include -using namespace std; -class MyBigQueue { //单调队列(从大到小) -public: - deque que; // 使用deque来实现单调队列 - // 每次弹出的时候,比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。 - // 同时pop之前判断队列当前是否为空。 - void pop(int value) { - if (!que.empty() && value == que.front()) { - que.pop_front(); - } - } - // 如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。 - // 这样就保持了队列里的数值是单调从大到小的了。 - void push(int value) { - while (!que.empty() && value > que.back()) { - que.pop_back(); - } - que.push_back(value); - - } - // 查询当前队列里的最大值 直接返回队列前端也就是front就可以了。 - int front() { - return que.front(); - } -}; - -class MySmallQueue { //单调队列(从小到大) -public: - deque que; - - void pop(int value) { - if (!que.empty() && value == que.front()) { - que.pop_front(); - } - } - - // 和上面队列的区别是这里换成了小于, - void push(int value) { - while (!que.empty() && value < que.back()) { - que.pop_back(); - } - que.push_back(value); - - } - - int front() { - return que.front(); - } -}; - -int main() { - string input; - - getline(cin, input); - - vector nums; - int k; - - // 找到并截取nums的部分 - int numsStart = input.find('['); - int numsEnd = input.find(']'); - string numsStr = input.substr(numsStart + 1, numsEnd - numsStart - 1); - // cout << numsStr << endl; - - // 用字符串流处理nums字符串,提取数字 - stringstream ss(numsStr); - string temp; - while (getline(ss, temp, ',')) { - nums.push_back(stoi(temp)); - } - - // 找到并提取k的值 - int kStart = input.find("k = ") + 4; - k = stoi(input.substr(kStart)); - - MyBigQueue queB; // 获取区间最大值 - MySmallQueue queS; // 获取区间最小值 - // vector result; - for (int i = 0; i < k; i++) { // 先将前k的元素放进队列 - queB.push(nums[i]); - queS.push(nums[i]); - } - - int result = queB.front() - queS.front(); - for (int i = k; i < nums.size(); i++) { - queB.pop(nums[i - k]); // 滑动窗口移除最前面元素 - queB.push(nums[i]); // 滑动窗口前加入最后面的元素 - - queS.pop(nums[i - k]); - queS.push(nums[i]); - - result = max (result, queB.front() - queS.front()); - } - cout << result << endl; -} -``` diff --git "a/problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" "b/problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" deleted file mode 100644 index ef66dec8bc..0000000000 --- "a/problems/kamacoder/0123.\345\260\217\347\272\242\347\232\204\346\225\260\347\273\204\346\236\204\351\200\240.md" +++ /dev/null @@ -1,52 +0,0 @@ - -121. 小红的数组构造 - -本题大家不要想着真去模拟数组的情况,那样就想复杂了。 - -数组只能是:1k、2k、3k ... (n-1)k、nk,这样 总和就是最小的。 - -注意最后的和可能超过int,所以用 long long。 - -代码如下: - -```CPP -#include -using namespace std; -int main () { - long long result = 0; - int n, k; - cin >> n >> k; - for (int i = 1; i <= n; i++) { - result += i * k; - } - cout << result << endl; -} -``` - -优化思路: - - -由于要计算1到n的整数之和,可以利用等差数列求和公式来优化计算。 - -和公式:1 + 2 + 3 + ... + n = n * (n + 1) / 2 - -因此,总和 result = k * (n * (n + 1) / 2) - -```CPP - -#include -using namespace std; - -int main() { - long long result = 0; - int n, k; - cin >> n >> k; - - // 使用等差数列求和公式进行计算 - result = k * (n * (n + 1LL) / 2); - - cout << result << endl; - return 0; -} - -``` diff --git "a/problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" "b/problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" deleted file mode 100644 index 2855c89efa..0000000000 --- "a/problems/kamacoder/0124.\347\262\276\345\215\216\345\270\226\345\255\220.md" +++ /dev/null @@ -1,38 +0,0 @@ - - -# 122.精华帖子 - - -开辟一个数组,默认都是0,把精华帖标记为1. - -使用前缀和,快速计算出,k 范围内 有多少个精华帖。 - -前缀和要特别注意区间问题,即 vec[i+k] - vec[i] 求得区间和是 (i, i + k] 这个区间,注意这是一个左开右闭的区间。 - -所以前缀和 很容易漏掉 vec[0] 这个数值的计算 - -```CPP -#include -#include -using namespace std; -int main() { - int n, m, k, l, r; - cin >> n >> m >> k; - vector vec(n); - while (m--) { - cin >> l >> r; - for (int i = l; i < r; i++) vec[i] = 1; - } - int result = 0; - for (int i = 0; i < k; i++) result += vec[i]; // 提前预处理result,包含vec[0]的区间,否则前缀和容易漏掉这个区间 - - for (int i = 1; i < n; i++) { - vec[i] += vec[i - 1]; - } - - for (int i = 0; i < n - k; i++) { - result = max (result, vec[i + k] - vec[i]); - } - cout << result << endl; -} -``` diff --git "a/problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" "b/problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" deleted file mode 100644 index 32833b2fb2..0000000000 --- "a/problems/kamacoder/0125.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\346\234\200\345\244\247\345\222\214.md" +++ /dev/null @@ -1,66 +0,0 @@ - -# 123.连续子数组最大和 - -这道题目可以说是 [代码随想录,动态规划:最大子序和](https://www.programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html) 的升级版。 - -题目求的是 可以替换一个数字 之后 的 连续子数组最大和。 - -如果替换的是数组下标 i 的元素。 - -那么可以用 [代码随想录,动态规划:最大子序和](https://www.programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html) 的方法,先求出 [0 - i) 区间的 最大子序和 dp1 和 (i, n)的最大子序和dp2 。 - -然后在遍历一遍i, 计算 dp1 + dp2 + vec[i] 的最大值就可以。 - -正序遍历,求出 [0 - i) 区间的 最大子序,dp[ i - 1] 表示 是 包括下标i - 1(以vec[i - 1]为结尾)的最大连续子序列和为dp[i - 1]。 - -所以 在计算区间 (i, n)即 dp2 的时候,我们要倒叙。 因为我们求的是以 包括下标i + 1 为起始位置的最大连续子序列和为dp[i + 1]。 - -这样 dp1 + dp2 + vec[i] 才是一个完整区间。 - -这里就体现出对 dp数组定义的把控,本题如果对 dp数组含义理解不清,其实是不容易做出来的。 - -代码: - -```CPP -#include -#include -#include -using namespace std; -int main() { - int t, n, x; - cin >> t; - while (t--) { - cin >> n >> x; - vector vec(n); - for (int i = 0; i < n; i++) cin >> vec[i]; - vector dp1(n); - dp1[0] = vec[0]; - int res = vec[0]; - // 从前向后统计最大子序和 - for (int i = 1; i < n; i++) { - dp1[i] = max(dp1[i - 1] + vec[i], vec[i]); // 状态转移公式 - res = max(res, dp1[i]); - } - - res = max(res, vec[n - 1]); - // 从后向前统计最大子序和 - vector dp2(n); - dp2[n - 1] = vec[n - 1]; - for (int i = n - 2; i >= 0; i--) { - dp2[i] = max(dp2[i + 1] + vec[i], vec[i]); - - } - - for (int i = 0 ; i < n ; i++) { - int dp1res = 0; - if (i > 0) dp1res = max(dp1[i-1], 0); - int dp2res = 0; - if (i < n - 1 ) dp2res = max(dp2[i+1], 0); - - res = max(res, dp1res + dp2res + x); - } - cout << res << endl; - } - -} -``` diff --git "a/problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" "b/problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" deleted file mode 100644 index 3be7f75bf8..0000000000 --- "a/problems/kamacoder/0127.\345\260\217\347\276\216\347\232\204\346\216\222\345\210\227\350\257\242\351\227\256.md" +++ /dev/null @@ -1,29 +0,0 @@ - -# 小美的排列询问 - -模拟题,注意 x 和y 不分先后 - -```CPP - -#include -#include -using namespace std; -int main() { - int n, x, y; - cin >> n; - vector vec(n, 0); - for (int i =0; i < n; i++) { - cin >> vec[i]; - } - cin >> x >> y; - for (int i = 0; i < n - 1; i++) { - if (x == vec[i] && y == vec[i + 1]) || (y == vec[i] && x == vec[i + 1]) ) { - cout << "Yes" << endl; - return 0; - } - } - cout << "No" << endl; - -} - -``` diff --git "a/problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" "b/problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" deleted file mode 100644 index dea68b9090..0000000000 --- "a/problems/kamacoder/0128.\345\260\217\347\276\216\350\265\260\345\205\254\350\267\257.md" +++ /dev/null @@ -1,35 +0,0 @@ - -# 小美走公路 - -在处理环形情况的时候,很多录友容易算懵了,不是多算一个数,就是少算一个数。 - -这里这样的题目,最好的方式是将 两个环展开,首尾相连,这样我们就可以通过 直线的思维去解题了 - -两个注意点: - -1. x 可以比 y 大,题目没规定 x 和y 的大小顺序 -2. 累计相加的数可能超过int - - -```CPP -#include -#include -using namespace std; -int main () { - int n; - cin >> n; - vector vec(2* n + 1, 0); - for (int i = 1; i <= n; i++) { - cin >> vec[i]; - vec[n + i] = vec[i]; - } - int x, y; - cin >> x >> y; - int xx = min(x ,y); // 注意点1:x 可以比 y 大 - int yy = max(x, y); - long long a = 0, b = 0; // 注意点2:相加的数可能超过int - for (int i = xx; i < yy; i++) a += vec[i]; - for (int i = yy; i < xx + n; i++ ) b += vec[i]; - cout << min(a, b) << endl; -} -``` diff --git "a/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" "b/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" deleted file mode 100644 index ae77478f13..0000000000 --- "a/problems/kamacoder/0129.\345\260\217\347\276\216\347\232\204\350\233\213\347\263\225\345\210\207\345\211\262.md" +++ /dev/null @@ -1,51 +0,0 @@ - -# 小美的蛋糕切割 - -二维前缀和,不了解前缀和的录友 可以自行查一下,是一个很容易理解的算法思路 - -```CPP - -#include -#include -#include - -using namespace std; -int main () { - int n, m; - cin >> n >> m; - int sum = 0; - vector> vec(n, vector(m, 0)) ; - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - cin >> vec[i][j]; - sum += vec[i][j]; - } - } - // 统计横向 - vector horizontal(n, 0); - for (int i = 0; i < n; i++) { - for (int j = 0 ; j < m; j++) { - horizontal[i] += vec[i][j]; - } - } - // 统计纵向 - vector vertical(m , 0); - for (int j = 0; j < m; j++) { - for (int i = 0 ; i < n; i++) { - vertical[j] += vec[i][j]; - } - } - int result = INT_MAX; - int horizontalCut = 0; - for (int i = 0 ; i < n; i++) { - horizontalCut += horizontal[i]; - result = min(result, abs(sum - horizontalCut - horizontalCut)); - } - int verticalCut = 0; - for (int j = 0; j < m; j++) { - verticalCut += vertical[j]; - result = min(result, abs(sum - verticalCut - verticalCut)); - } - cout << result << endl; -} -``` diff --git "a/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" "b/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" deleted file mode 100644 index 9f9d789930..0000000000 --- "a/problems/kamacoder/0130.\345\260\217\347\276\216\347\232\204\345\255\227\347\254\246\344\270\262\345\217\230\346\215\242.md" +++ /dev/null @@ -1,78 +0,0 @@ - -# 130.小美的字符串变换 - -本题是[岛屿数量](./0099.岛屿的数量广搜.md)的进阶版,主要思路和代码都是一样的,统计一个图里岛屿的数量,也是染色问题。 - -1、 先枚举各个可能出现的矩阵 -2、 针对矩阵经行广搜染色(深搜,并查集一样可以) -3、 统计岛屿数量最小的数量。 - -```CPP -#include -#include -#include -#include -using namespace std; - -// 广搜代码同 卡码网:99. 岛屿数量 -int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 -void bfs(const vector>& grid, vector>& visited, int x, int y, char a) { - queue> que; - que.push({x, y}); - visited[x][y] = true; // 只要加入队列,立刻标记 - while(!que.empty()) { - pair cur = que.front(); que.pop(); - int curx = cur.first; - int cury = cur.second; - for (int i = 0; i < 4; i++) { - int nextx = curx + dir[i][0]; - int nexty = cury + dir[i][1]; - if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 - if (!visited[nextx][nexty] && grid[nextx][nexty] == a) { - que.push({nextx, nexty}); - visited[nextx][nexty] = true; // 只要加入队列立刻标记 - } - } - } -} - -int main() { - int n; - string s; - cin >> n; - int result = INT_MAX; - cin >> s; - for (int k = 1; k < n; k++) { - if (n % k != 0) continue; - // 计算出 矩阵的 行 和 列 - int x = n / k; - int y = k; - //cout << x << " " << y << endl; - vector> vec(x, vector(y, 0)); - // 填装矩阵 - int sCount = 0; - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { - vec[i][j] = s[sCount++]; - } - } - - // 开始广搜染色 - vector> visited(x, vector(y, false)); - int count = 0; - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { - - if (!visited[i][j]) { - count++; // 遇到没访问过的陆地,+1 - bfs(vec, visited, i, j, vec[i][j]); // 将与其链接的陆地都标记上 true - } - } - } - // 取岛屿数量最少的 - result = min (result, count); - - } - cout << result << endl; -} -``` diff --git "a/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" "b/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" deleted file mode 100644 index 36b3c38cc2..0000000000 --- "a/problems/kamacoder/0131.\345\260\217\347\276\216\347\232\204\346\240\221\344\270\212\346\237\223\350\211\262.md" +++ /dev/null @@ -1,134 +0,0 @@ -# 131. 小美的树上染色 - -本题为树形dp 稍有难度,主要在于 递推公式上。 - -dp数组的定义: - -dp[cur][1] :当前节点染色,那么当前节点为根节点及其左右子节点中,可以染色的最大数量 - -dp[cur][0] :当前节点不染色,那么当前节点为根节点及其左右子节点中,可以染色的最大数量 - -关于 dp转移方程 - -1、 情况一: - -如果当前节点不染色,那就去 子节点 染色 或者 不染色的最大值。 - -`dp[cur][0] += max(dp[child][0], dp[child][1]);` - - -2、情况二: - -那么当前节点染色的话,这种情况就不好想了。 - -首先这不是二叉树,每一个节点都有可能 会有n个子节点。 - -所以我们要分别讨论,每一个子节点的情况 对父节点的影响。 - -那么父节点 针对每种情况,就要去 最大值, 也就是 `dp[cur][1] = max(dp[cur][1], 每个自孩子的情况)` - -如图,假如节点1 是我们要计算的父节点,节点2是我们这次要计算的子节点。 - -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240617204601.png) - -选中一个节点2 作为我们这次计算的子节点,父节点染色的话,子节点必染色。 - -接下来就是计算 父节点1和该子节点2染色的话, 以子节点2 为根的 染色节点的最大数量 。 - -是:节点2不染色 且 以节点2为根节点的最大 染色数量 + 2, + 2 是因为 节点 1 和 节点2 要颜色了,染色节点增加两个。 - -代码:`dp[child][0] + 2` - -细心的录友会发现,那我们只计算了 红色框里面的,那么框外 最大的染色数量是多少呢? - -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240617205709.png) - - -先看 作为子节点的节点2 为根节点的最大染色数量是多少? 取一个最值,即 节点2染色 或者 不染色取最大值。 - -代码:`max(dp[child][0], dp[child][1])` - -那么红框以外的 染色最大节点数量 就是 `dp[cur][0] - max(dp[child][0], dp[child][1])` - -(cur是节点1,child是节点2) - -红框以外的染色最大数量 + 父节点1和该子节点2染色的话 以子节点2 为根的 染色节点的最大数量 就是 节点1 染色的最大节点数量。 - -代码: - -`dp[cur][1] = max(dp[cur][1], dp[cur][0] - max(dp[child][0], dp[child][1]) + dp[child][0] + 2);` - -整体代码如下: - -```CPP - -#include -#include -#include -#include -#include - -using namespace std; - -int maxN = 10005; -vector> dp (maxN, vector(2, 0)); -vector> grid(maxN); // 邻接表 -vector value(maxN); // 存储每个节点的权值 - - -// 在树上进行动态规划的函数 -void dpOnTheTree(int cur) { - - for (int child : grid[cur]) { - // 后序遍历,从下向上计算 - dpOnTheTree(child); - // 情况一 - dp[cur][0] += max(dp[child][0], dp[child][1]); - - } - - // 计算dp[1] - 当前节点染色 - for (int child : grid[cur]) { - long mul = value[cur] * value[child]; // 当前节点和相邻节点权值的乘积 - long sqrtNum = (long) sqrt(mul); - - if (sqrtNum * sqrtNum == mul) { // 如果乘积是完全平方数 - // 情况二 - // dp[cur][0] 表示所有子节点 染色或者不染色的 最大染色数量 - // max(dp[child][0], dp[child][1]) 需要染色节点的孩子节点的最大染色数量 - // dp[cur][0] - max(dp[child][0], dp[child][1]) 除了要染色的节点及其子节点,其他孩子的最大染色数量 - // 最后 + dp[child][0] + 2 , 就是本节点染色的最大染色节点数量 - dp[cur][1] = max(dp[cur][1], dp[cur][0] - max(dp[child][0], dp[child][1]) + dp[child][0] + 2); - } - } - -} - -int main() { - - int n; - cin >> n; // 输入节点数量 - - // 读取节点权值 - for (int i = 1; i <= n; ++i) { - cin >> value[i]; - } - - // 构建树的邻接表 - for (int i = 1; i < n; ++i) { - int x, y; - cin >> x >> y; - grid[x].push_back(y); - } - - // 从根节点(节点1)开始进行动态规划 - dpOnTheTree(1); - - // 输出最大染色节点数量 - cout << max(dp[1][0], dp[1][1]) << endl; - - return 0; -} - -``` - diff --git "a/problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" "b/problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" deleted file mode 100644 index 2ec50bfb12..0000000000 --- "a/problems/kamacoder/0132.\345\244\271\345\220\203\346\227\227.md" +++ /dev/null @@ -1,47 +0,0 @@ - -# 132. 夹吃棋 - -[题目链接](https://kamacoder.com/problempage.php?pid=1209) - -这道题是模拟题,但很多录友可能想复杂了。 - -行方向,白棋吃,只有这样的布局 `o*o`,黑棋吃,只有这样的布局 `*o*` - -列方向也是同理的。 - -想到这一点,本题的代码就容易写了, C++代码如下: - -```CPP -#include -#include -using namespace std; -int main() { - int n; - cin >> n; - while (n--) { - int black = 0, white = 0; - vector grid(3, ""); - // 判断行 - for (int i = 0; i < 3; i++) { - cin >> grid[i]; - if (grid[i] == "o*o") white++; - if (grid[i] == "*o*") black++; - } - // 判断列 - for (int i = 0; i < 3; i++) { - string s; - s += grid[0][i]; - s += grid[1][i]; - s += grid[2][i]; - if (s == "o*o") white++; - if (s == "*o*") black++; - } - // 如果一个棋盘的局面没有一方被夹吃或者黑白双方都被对面夹吃,则认为是平局 - if ((!white && !black) || (white && black)) cout << "draw" << endl; - // 白棋赢 - else if (white && !black) cout << "yukan" << endl; - // 黑棋赢 - else cout << "kou" << endl; - } -} -``` diff --git "a/problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" "b/problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" deleted file mode 100644 index ca681df412..0000000000 --- "a/problems/kamacoder/0134.\347\232\207\345\220\216\347\247\273\345\212\250\347\232\204\346\234\200\345\260\217\346\255\245\346\225\260.md" +++ /dev/null @@ -1,77 +0,0 @@ - -# 134. 皇后移动的最小步数 - -[题目链接](https://kamacoder.com/problempage.php?pid=1211) - -本题和 [代码随想录-不同路径](https://www.programmercarl.com/0062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84.html) 有一些类似。 - -关键是弄清楚递推公式 - -一共分三个情况, - -情况一,向右移动: - -然后从 (i, j) 再向右走 到 (i, k)。 无论k 多大,步数只加1 : - -`dp[i][k] = dp[i][j] + 1` - -那么 `dp[i][k]` 也有可能 从其他方向得到,例如 从上到下, 或者斜上方到达 dp[i][k] - -本题我们要求最小步数,所以取最小值:`dp[i][k] = min(dp[i][k], dp[i][j] + 1);` - -情况二,向下移动: - -从 (i, j) 再向下走 到 (k, j)。 无论k 多大,步数只加1 : - -`dp[k][j] = dp[i][j] + 1;` - -同理 `dp[i][k]` 也有可能 从其他方向得到,取最小值:`dp[k][j] = min(dp[k][j], dp[i][j] + 1);` - -情况三,右下方移动: - -从 (i, j) 再向右下方移动 到 (i + k, j + k)。 无论k 多大,步数只加1 : - -`dp[i + k][j + k] = dp[i][j] + 1` - -同理 `dp[i + k][j + k]` 也有可能 从其他方向得到,取最小值:`dp[i + k][j + k] = min(dp[i + k][j + k], dp[i][j] + 1);` - - -```CPP -#include -#include -using namespace std; -const int INF = 4e6; // 最多步数也就是 2000 * 2000 -int main() { - int n, m; - cin >> n >> m; - vector> grid(n, vector(m)); - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - cin >> grid[i][j]; - } - } - vector> dp(n, vector(m, INF)); - dp[0][0] = 0; - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - if (grid[i][j] == '*') continue; - // 向右移动k个格子 - for (int k = j + 1; k < m && grid[i][k] == '.'; k++) { - dp[i][k] = min(dp[i][k], dp[i][j] + 1); - } - // 向下移动 k个格子 - for (int k = i + 1; k < n && grid[k][j] == '.'; k++) { - dp[k][j] = min(dp[k][j], dp[i][j] + 1); - } - // 向右下移动k个格子 - for (int k = 1; i + k < n && j + k < m && grid[i + k][j + k] == '.'; k++) { - dp[i + k][j + k] = min(dp[i + k][j + k], dp[i][j] + 1); - } - } - } - if (dp[n - 1][m - 1] == INF) cout << -1 << endl; - else cout << dp[n - 1][m - 1] << endl; -} -``` - - diff --git "a/problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" "b/problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" deleted file mode 100644 index c1aa38e132..0000000000 --- "a/problems/kamacoder/0135.\350\216\267\345\217\226\350\277\236\351\200\232\347\232\204\347\233\270\351\202\273\350\212\202\347\202\271\345\210\227\350\241\250.md" +++ /dev/null @@ -1,147 +0,0 @@ - -# 135. 获取连通的相邻节点列表 - -本题是一个 “阅读理解”题,其实题目的算法很简单,但理解题意很费劲。 - -题目描述中的【提示信息】 是我后加上去了,华为笔试的时候没有这个 【提示信息】。 - -相信没有 【提示信息】大家理解题意 平均要多用半个小时。 - -思路: - -1. 将第一行数据加入set中 -2. 后面输出数据,判断是否在 set里 -3. 最后把结果排个序 - - -```CPP -#include -#include -#include -#include -using namespace std; -int main() { - unordered_set uset; - int n, a; - cin >> n; - while (n--) { - cin >> a; - uset.insert(a); - } - int m, x, vlan_id; - long long tb; - vector vecTB; - cin >> m; - while(m--) { - cin >> tb; - cin >> x; - vector vecVlan_id(x); - for (int i = 0; i < x; i++) { - cin >> vecVlan_id[i]; - } - for (int i = 0; i < x; i++) { - if (uset.find(vecVlan_id[i]) != uset.end()) { - vecTB.push_back(tb); - break; - } - } - - } - cout << vecTB.size() << endl; - if (vecTB.size() != 0) { - sort(vecTB.begin(), vecTB.end()); - for (int i = 0; i < vecTB.size() ; i++) cout << vecTB[i] << " "; - } -} - -``` - -## 其他语言版本 - -### Java - -```Java -import java.util.*; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - Set uset = new HashSet<>(); - int n = scanner.nextInt(); - while (n-- > 0) { - int a = scanner.nextInt(); - uset.add(a); - } - - int m = scanner.nextInt(); - List vecTB = new ArrayList<>(); - while (m-- > 0) { - long tb = scanner.nextLong(); - int x = scanner.nextInt(); - List vecVlan_id = new ArrayList<>(); - for (int i = 0; i < x; i++) { - vecVlan_id.add(scanner.nextInt()); - } - for (int vlanId : vecVlan_id) { - if (uset.contains(vlanId)) { - vecTB.add(tb); - break; - } - } - } - - System.out.println(vecTB.size()); - if (!vecTB.isEmpty()) { - Collections.sort(vecTB); - for (long tb : vecTB) { - System.out.print(tb + " "); - } - } - } -} - -``` - -### Python - -```python -def main(): - import sys - input = sys.stdin.read - data = input().split() - - index = 0 - n = int(data[index]) - index += 1 - uset = set() - for _ in range(n): - a = int(data[index]) - index += 1 - uset.add(a) - - m = int(data[index]) - index += 1 - vecTB = [] - while m > 0: - tb = int(data[index]) - index += 1 - x = int(data[index]) - index += 1 - vecVlan_id = [] - for _ in range(x): - vecVlan_id.append(int(data[index])) - index += 1 - for vlan_id in vecVlan_id: - if vlan_id in uset: - vecTB.append(tb) - break - m -= 1 - - print(len(vecTB)) - if vecTB: - vecTB.sort() - print(" ".join(map(str, vecTB))) - -if __name__ == "__main__": - main() -``` diff --git "a/problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" "b/problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" deleted file mode 100644 index 1c58f4abaf..0000000000 --- "a/problems/kamacoder/0136.\345\255\227\347\254\246\344\270\262\345\244\204\347\220\206\345\231\250.md" +++ /dev/null @@ -1,148 +0,0 @@ - -# 字符串处理器 - -纯模拟,但情况比较多,非常容易 空指针异常。 - -大家要注意,边界问题 以及 负数问题。 - -整体代码如下: - -```CPP -#include -using namespace std; - int main() { - int index = 0; - long long optNum; - string s; - string cmd; - while(cin >> cmd){ - //cout << s << endl; - if(cmd == "insert") { - string buff; - cin >> buff; - s.insert(index, buff); - index += buff.size(); - } - else if(cmd == "move") { - cin >> optNum; - if(optNum > 0 && index + optNum <= s.size()) index += optNum; - if(optNum < 0 && index >= -optNum) index += optNum; - } - else if(cmd == "delete") { - cin >> optNum; - if(index >= optNum && optNum > 0){ - s.erase(index - optNum, optNum); - index -= optNum; - } - } - else if(cmd == "copy") { - if(index > 0) { - string tmp = s.substr(0, index); - s.insert(index, tmp); - } - } - else if(cmd == "end") { - for(int i = 0; i < index; i++) { - cout << s[i]; - } - cout << '|'; - for(int i = index; i < s.size(); i++) cout << s[i]; - - break; - } - } - return 0; -} - -``` - -## 其他语言版本 - -### Java - -```Java -import java.util.Scanner; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - StringBuilder s = new StringBuilder(); - int index = 0; - int optNum; - - while (true) { - String cmd = scanner.next(); - if (cmd.equals("insert")) { - String buff = scanner.next(); - s.insert(index, buff); - index += buff.length(); - } else if (cmd.equals("move")) { - optNum = scanner.nextInt(); - if (optNum > 0 && index + optNum <= s.length()) index += optNum; - if (optNum < 0 && index >= -optNum) index += optNum; - } else if (cmd.equals("delete")) { - optNum = scanner.nextInt(); - if (index >= optNum && optNum > 0) { - s.delete(index - optNum, index); - index -= optNum; - } - } else if (cmd.equals("copy")) { - if (index > 0) { - String tmp = s.substring(0, index); - s.insert(index, tmp); - } - } else if (cmd.equals("end")) { - System.out.print(s.substring(0, index) + '|' + s.substring(index)); - break; - } - } - scanner.close(); - } -} -``` - -### Python - -```python -def main(): - import sys - input = sys.stdin.read - data = input().split() - s = "" - index = 0 - i = 0 - - while i < len(data): - cmd = data[i] - i += 1 - if cmd == "insert": - buff = data[i] - i += 1 - s = s[:index] + buff + s[index:] - index += len(buff) - elif cmd == "move": - optNum = int(data[i]) - i += 1 - if optNum > 0 and index + optNum <= len(s): - index += optNum - elif optNum < 0 and index >= -optNum: - index += optNum - elif cmd == "delete": - optNum = int(data[i]) - i += 1 - if index >= optNum and optNum > 0: - s = s[:index - optNum] + s[index:] - index -= optNum - elif cmd == "copy": - if index > 0: - tmp = s[:index] - s = s[:index] + tmp + s[index:] - elif cmd == "end": - print(s[:index] + '|' + s[index:]) - break - -if __name__ == "__main__": - main() - - -``` diff --git "a/problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" "b/problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" deleted file mode 100644 index a1519bc6b0..0000000000 --- "a/problems/kamacoder/0137.\346\266\210\346\201\257\344\274\240\350\276\223.md" +++ /dev/null @@ -1,192 +0,0 @@ - -# 137. 消息传输 - -这道题目,普通广搜就可以解决。 - -这里说一下几点注意事项: - -1、 题目描述中,注意 n 是列数,m是行数 - -这是造成很多录友周赛的时候提交 返回 【运行错误】的罪魁祸首,如果 输入用例是 正方形,那没问题,如果后台输入用例是矩形, n 和 m 搞反了,就会数组越界。 - -矩阵是 m * n ,但输入的顺序却是 先输入n 再输入 m。 - -这会让很多人把矩阵的 n 和 m 搞反。 - -其实规范出题,就应该是n 行,m列,然后 先输入n,在输入m。 - -只能说 大厂出题的人,也不是专业出题的,所以会在 非算法方面一不小心留下很多 “bug”,消耗大家的精力。 - -2、再写广搜的时候,可能担心会无限循环 - -即 A 走到 B,B又走到A,A又走到B ,这种情况,一般来说 广搜都是用一个 visit数组来标记的。 - -但本题不用,因为 不会重复走的,题图里的信号都是正数,根据距离判断大小 可以保证不走回头路。 - -```CPP -#include -#include -#include -using namespace std; -const int inf = 1e6; -int main () { - int n, m, startx, starty; - cin >> n >> m; - cin >> startx >> starty; - vector> grid(m, vector(n)); - vector> dis(m, vector(n, inf)); - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - cin >> grid[i][j]; - } - } - queue> que; - int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; - que.push(pair(startx, starty)); - dis[startx][starty] = 0; - while(!que.empty()) { - pair cur = que.front(); que.pop(); - for (int i = 0; i < 4; i++) { - int newx = cur.first + dir[i][1]; - int newy = cur.second + dir[i][0]; - if (newx < 0 || newx >= m || newy < 0 || newy >= n || grid[cur.first][cur.second] == 0) continue; - - if (dis[newx][newy] > dis[cur.first][cur.second] + grid[cur.first][cur.second]) { - dis[newx][newy] = dis[cur.first][cur.second] + grid[cur.first][cur.second]; - que.push(pair(newx, newy)); - } - } - } - int result = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (dis[i][j] == inf) { - cout << -1 << endl; - return 0; - } - result = max(result, dis[i][j]); - } - } - cout << result << endl; -} -``` - -## 其他语言版本 - -### Java - -```Java -import java.util.*; - -public class Main { - static final int INF = 1000000; - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int n = scanner.nextInt(); - int m = scanner.nextInt(); - int startX = scanner.nextInt(); - int startY = scanner.nextInt(); - - int[][] grid = new int[m][n]; - int[][] dis = new int[m][n]; - - for (int i = 0; i < m; i++) { - Arrays.fill(dis[i], INF); - for (int j = 0; j < n; j++) { - grid[i][j] = scanner.nextInt(); - } - } - - Queue queue = new LinkedList<>(); - int[][] directions = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; - - queue.add(new int[]{startX, startY}); - dis[startX][startY] = 0; - - while (!queue.isEmpty()) { - int[] current = queue.poll(); - for (int[] dir : directions) { - int newX = current[0] + dir[0]; - int newY = current[1] + dir[1]; - if (newX >= 0 && newX < m && newY >= 0 && newY < n && grid[current[0]][current[1]] != 0) { - if (dis[newX][newY] > dis[current[0]][current[1]] + grid[current[0]][current[1]]) { - dis[newX][newY] = dis[current[0]][current[1]] + grid[current[0]][current[1]]; - queue.add(new int[]{newX, newY}); - } - } - } - } - - int result = 0; - for (int i = 0; i < m; i++) { - for (int j = 0; j < n; j++) { - if (dis[i][j] == INF) { - System.out.println(-1); - return; - } - result = Math.max(result, dis[i][j]); - } - } - - System.out.println(result); - scanner.close(); - } -} -``` - -### Python - -```Python -from collections import deque - -inf = 1000000 - -def main(): - import sys - input = sys.stdin.read - data = input().split() - index = 0 - - n = int(data[index]) - m = int(data[index+1]) - startx = int(data[index+2]) - starty = int(data[index+3]) - index += 4 - - grid = [] - dis = [[inf] * n for _ in range(m)] - - for i in range(m): - grid.append([int(data[index+j]) for j in range(n)]) - index += n - - directions = [(0, 1), (1, 0), (-1, 0), (0, -1)] - queue = deque() - queue.append((startx, starty)) - dis[startx][starty] = 0 - - while queue: - curx, cury = queue.popleft() - for dx, dy in directions: - newx, newy = curx + dx, cury + dy - if 0 <= newx < m and 0 <= newy < n and grid[curx][cury] != 0: - if dis[newx][newy] > dis[curx][cury] + grid[curx][cury]: - dis[newx][newy] = dis[curx][cury] + grid[curx][cury] - queue.append((newx, newy)) - - result = 0 - for i in range(m): - for j in range(n): - if dis[i][j] == inf: - print(-1) - return - result = max(result, dis[i][j]) - - print(result) - -if __name__ == "__main__": - main() - - -``` diff --git "a/problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" "b/problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" deleted file mode 100644 index 9ff0b684d7..0000000000 --- "a/problems/kamacoder/0139.\345\217\257\347\210\261\344\270\262.md" +++ /dev/null @@ -1,101 +0,0 @@ - -# 可爱串 - -整体思路,就含有 子序列的字符串数量 减去 含有子串的字符串数量。 - -因为子序列数量已经是包含子串数量的。 剩下的就是 只有子序列 且没有子串的 字符串数量。 - - -需要注意我们求的不是 长度为 i 的字符串里有多少个 red 子序列。 - -**而是 可以有多少个 长度为i 的字符串 含有子序列 red** - -同理,可以有多少个长度为i的字符串含有 red 子串 - -认清这一点很重要! - -### 求子串 - -dp2[i][3] 长度为i 且 含有子串 red 的字符串数量 有多少 - -dp2[i][2] 长度为i 且 含有子串 re 的字符串数量有多少 - -dp2[i][1] 长度为 i 且 含有子串 r 的字符串数量有多少 - -dp2[1][0] 长度为 i 且 含有 只有 de, ee , e, d的字符串的字符串数量有多少。 - -```CPP -// 求子串 -dp2[0][0] = 1; -for(int i = 1;i <= n; i++) { - dp2[i][0] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0] * 2) % mod; // 含有 re 的可以把 r改成d, 含有r 的可以改成 - dp2[i][1] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0]) % mod; - dp2[i][2] = (dp2[i - 1][1]); - dp2[i][3] = (dp2[i - 1][3] * 3 + dp2[i - 1][2]) % mod; -} -`` - -### 求子序列 - -dp1[i][3] 长度为i 且 含有子序列 red 的字符串数量 有多少 - -dp2[i][2] 长度为i 且 含有子序列 re 的字符串数量有多少 - -dp2[i][1] 长度为 i 且 含有子序列 r 的字符串数量有多少 - -dp2[1][0] 长度为 i 且 含有 只含有 e 和 d 的字符串的字符串数量有多少。 - -```CPP - -// 求子序列 -dp1[0][0]=1; -for(int i=1;i<=n;i++) -{ - dp1[i][0] = (dp1[i - 1][0] * 2) % mod; - dp1[i][1] = (dp1[i - 1][0] + dp1[i - 1][1] * 2) % mod; - dp1[i][2] = (dp1[i - 1][1] + dp1[i - 1][2] * 2) % mod; - dp1[i][3] = (dp1[i - 1][2] + dp1[i - 1][3] * 3) % mod; -} -``` - - - -```CPP - -#include -using namespace std; - -using ll=long long; -const int mod=1e9+7; - -int main() -{ - int n; - - cin>>n; - vector> dp1(n + 1,vector (4,0)); - vector> dp2(n + 1,vector (4,0)); - // 求子串 - dp2[0][0] = 1; - for(int i = 1;i <= n; i++) { - dp2[i][0] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0] * 2) % mod; - dp2[i][1] = (dp2[i - 1][2] + dp2[i - 1][1] + dp2[i - 1][0]) % mod; - dp2[i][2] = (dp2[i - 1][1]); - dp2[i][3] = (dp2[i - 1][3] * 3 + dp2[i - 1][2]) % mod; - } - - // 求子序列 - dp1[0][0]=1; - for(int i=1;i<=n;i++) - { - dp1[i][0] = (dp1[i - 1][0] * 2) % mod; - dp1[i][1] = (dp1[i - 1][0] + dp1[i - 1][1] * 2) % mod; - dp1[i][2] = (dp1[i - 1][1] + dp1[i - 1][2] * 2) % mod; - dp1[i][3] = (dp1[i - 1][2] + dp1[i - 1][3] * 3) % mod; - } - - cout<<(dp1[n][3] - dp2[n][3])%mod; - -} - -``` diff --git "a/problems/kamacoder/0139.\345\256\214\347\276\216\346\225\260.md" "b/problems/kamacoder/0139.\345\256\214\347\276\216\346\225\260.md" deleted file mode 100644 index 1f801d7666..0000000000 --- "a/problems/kamacoder/0139.\345\256\214\347\276\216\346\225\260.md" +++ /dev/null @@ -1,29 +0,0 @@ - -```CPP -#include -#include -using namespace std; -int countOnes(long long num) { - int zeroCount = 0; - while (num > 0) { - if (num % 10 != 0) { // 检查最低位是否为0 - zeroCount++; - } - num /= 10; // 移除最低位 - } - return zeroCount; -} -int main() { - int n; - cin >> n; - vector vec(n); - for (int i = 0; i < n; i++) cin >> vec[i]; - int result = 0; - for (int i = 0; i < n; i++) { - for (int j = i + 1; j < n; j++) { - if (countOnes(vec[i] * vec[j]) == 1) result++; - } - } - cout << result << endl; -} -``` diff --git "a/problems/kamacoder/0141.\345\245\275\344\272\214\345\217\211\346\240\221.md" "b/problems/kamacoder/0141.\345\245\275\344\272\214\345\217\211\346\240\221.md" deleted file mode 100644 index fe48f2fd46..0000000000 --- "a/problems/kamacoder/0141.\345\245\275\344\272\214\345\217\211\346\240\221.md" +++ /dev/null @@ -1,104 +0,0 @@ - -本题和 [96.不同的二叉搜索树](https://www.programmercarl.com/0096.%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html) 比较像 - -* 取模这里很容易出错 -* 过程中所用到的数值都有可能超过int,所以要改用longlong - -```CPP -#include -#include -using namespace std; - -long long mod = 1e9 + 7; -long long dp(int t, vector& memory) { - if (t % 2 == 0) return 0; - if (t == 1) return 1; - if (memory[t] != -1) return memory[t]; - - long long result = 0; - // 枚举左右子树节点的数量 - for (int i = 1; i < t; i += 2) { - long long leftNum = dp(i, memory); // 左子树节点数量为i - long long rightNum = dp(t - i - 1, memory); // 右子树节点数量为t - i - 1 - result += (leftNum * rightNum) % mod; // 注意这里是乘的关系 - result %= mod; - } - memory[t] = result; - return result; -} -int main() { - int n; - cin >> n; - vector memory(n + 1, -1); - cout << dp(n, memory) << endl; -} -``` - - -```CPP -#include -#include -#include - -using namespace std; - -const int MOD = 1000000007; - -int main() { - int num; - cin >> num; - - if (num % 2 == 0) { - cout << 0 << endl; - return 0; - } - - vector dp(num + 1, 0); - dp[1] = 1; - - for (int i = 3; i <= num; i += 2) { - for (int j = 1; j <= i - 2; j += 2) { - dp[i] = (dp[i] + dp[j] * dp[i - 1 - j]) % MOD; - } - } - - cout << dp[num] << endl; - return 0; -} - -``` - - -第二题的代码 - -#include -using namespace std; - -long fastexp(long base,long n,long mod){ - long answer = 1; - while(n > 0){ - if(n % 2 == 1){ - answer = (answer * base) % mod; - } - base = (base * base) % mod; - n /= 2; - } - return answer; -} -int kawaiiStrings(int n) { - // write code here - std::vector f(n + 1), g(n + 1), h(n + 1); - long mod = 1000000007; - for (long i = 2; i <= n; i++) g[i] = (g[i - 1] * 2 + (i - 1) * fastexp(2,i-2,mod)) % mod; - for (long i = 3; i <= n; i++) f[i] = ((f[i - 1] * 3) % mod + g[i - 1]) % mod; - for (long i = 3; i <= n; i++) h[i] = (fastexp(3, i - 3, mod) + h[i - 1] * 3 - h[i - 3]) % mod; - return (f[n]-h[n]+mod)%mod; - -} - -int main(){ - int n; - cin >> n; - cout << kawaiiStrings(n) << endl; - return 0; -} diff --git "a/problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" "b/problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" deleted file mode 100644 index ff34581ff0..0000000000 --- "a/problems/kamacoder/0142.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\217ASCII\345\210\240\351\231\244\346\200\273\345\222\214.md" +++ /dev/null @@ -1,108 +0,0 @@ - -# 142. 两个字符串的最小 ASCII 删除总和 - -本题和[代码随想录:两个字符串的删除操作](https://www.programmercarl.com/0583.%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C.html) 思路基本是一样的。 - -属于编辑距离问题,如果想彻底了解,建议看看「代码随想录」的编辑距离总结篇。 - -本题dp数组含义: - -dp[i][j] 表示 以i-1为结尾的字符串word1,和以j-1位结尾的字符串word2,想要达到相等,所需要删除元素的最小ASCII 删除总和。 - -如果 s1[i - 1] 与 s2[j - 1] 相同,则不用删:`dp[i][j] = dp[i - 1][j - 1]` - -如果 s1[i - 1] 与 s2[j - 1] 不相同,删word1 的 最小删除和: `dp[i - 1][j] + s1[i - 1]` ,删word2的最小删除和: `dp[i][j - 1] + s2[j - 1]` - -取最小值: `dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1])` - - - -```CPP -#include -#include -using namespace std; -int main() { - string s1, s2; - cin >> s1 >> s2; - vector> dp(s1.size() + 1, vector(s2.size() + 1, 0)); - - // s1 如果变成空串的最小删除ASCLL值综合 - for (int i = 1; i <= s1.size(); i++) dp[i][0] = dp[i - 1][0] + s1[i - 1]; - // s2 如果变成空串的最小删除ASCLL值综合 - for (int j = 1; j <= s2.size(); j++) dp[0][j] = dp[0][j - 1] + s2[j - 1]; - - for (int i = 1; i <= s1.size(); i++) { - for (int j = 1; j <= s2.size(); j++) { - if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; - else dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1]); - } - } - cout << dp[s1.size()][s2.size()] << endl; -} -``` - -### Java - -```Java -import java.util.Scanner; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - String s1 = scanner.nextLine(); - String s2 = scanner.nextLine(); - int[][] dp = new int[s1.length() + 1][s2.length() + 1]; - - // s1 如果变成空串的最小删除ASCII值综合 - for (int i = 1; i <= s1.length(); i++) { - dp[i][0] = dp[i - 1][0] + s1.charAt(i - 1); - } - // s2 如果变成空串的最小删除ASCII值综合 - for (int j = 1; j <= s2.length(); j++) { - dp[0][j] = dp[0][j - 1] + s2.charAt(j - 1); - } - - for (int i = 1; i <= s1.length(); i++) { - for (int j = 1; j <= s2.length(); j++) { - if (s1.charAt(i - 1) == s2.charAt(j - 1)) { - dp[i][j] = dp[i - 1][j - 1]; - } else { - dp[i][j] = Math.min(dp[i - 1][j] + s1.charAt(i - 1), dp[i][j - 1] + s2.charAt(j - 1)); - } - } - } - System.out.println(dp[s1.length()][s2.length()]); - scanner.close(); - } -} - - -``` - -### python - -```python -def min_delete_sum(s1: str, s2: str) -> int: - dp = [[0] * (len(s2) + 1) for _ in range(len(s1) + 1)] - - # s1 如果变成空串的最小删除ASCII值综合 - for i in range(1, len(s1) + 1): - dp[i][0] = dp[i - 1][0] + ord(s1[i - 1]) - # s2 如果变成空串的最小删除ASCII值综合 - for j in range(1, len(s2) + 1): - dp[0][j] = dp[0][j - 1] + ord(s2[j - 1]) - - for i in range(1, len(s1) + 1): - for j in range(1, len(s2) + 1): - if s1[i - 1] == s2[j - 1]: - dp[i][j] = dp[i - 1][j - 1] - else: - dp[i][j] = min(dp[i - 1][j] + ord(s1[i - 1]), dp[i][j - 1] + ord(s2[j - 1])) - - return dp[len(s1)][len(s2)] - -if __name__ == "__main__": - s1 = input().strip() - s2 = input().strip() - print(min_delete_sum(s1, s2)) -``` diff --git "a/problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" "b/problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" deleted file mode 100644 index bf46c895f8..0000000000 --- "a/problems/kamacoder/0143.\346\234\200\351\225\277\345\220\214\345\200\274\350\267\257\345\276\204.md" +++ /dev/null @@ -1,237 +0,0 @@ - - -# 143. 最长同值路径 - - -本题两个考点: - -1. 层序遍历构造二叉树 -2. 树形dp,找出最长路径 - -对于写代码不多,或者动手能力比较差的录友,第一个 构造二叉树 基本就被卡主了。 - -```CPP -#include -#include -#include - -using namespace std; - -// 定义二叉树节点结构 -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// 根据层序遍历数组构建二叉树 -TreeNode* constructBinaryTree(const vector& levelOrder) { - if (levelOrder.empty()) return NULL; - - TreeNode* root = new TreeNode(stoi(levelOrder[0])); - queue q; - q.push(root); - int i = 1; - - while (!q.empty() && i < levelOrder.size()) { - TreeNode* current = q.front(); - q.pop(); - - if (i < levelOrder.size() && levelOrder[i] != "null") { - current->left = new TreeNode(stoi(levelOrder[i])); - q.push(current->left); - } - i++; - - if (i < levelOrder.size() && levelOrder[i] != "null") { - current->right = new TreeNode(stoi(levelOrder[i])); - q.push(current->right); - } - i++; - } - - return root; -} - -int result = 0; - -// 树形DP -int dfs(TreeNode* node) { - if (node == NULL) return 0; - int leftPath = dfs(node->left); - int rightPath = dfs(node->right); - - int leftNum = 0, rightNum = 0; - if (node->left != NULL && node->left->val == node->val) { - leftNum = leftPath + 1; - } - if (node->right != NULL && node->right->val == node->val) { - rightNum = rightPath + 1; - } - result = max(result, leftNum + rightNum); - return max(leftNum, rightNum); - -} - - -int main() { - int n; - cin >> n; - vector levelOrder(n); - for (int i = 0; i < n ; i++) cin >> levelOrder[i]; - - TreeNode* root = constructBinaryTree(levelOrder); - dfs(root); - cout << result << endl; - - return 0; -} -``` - -### Java - -```Java -import java.util.*; - -class TreeNode { - int val; - TreeNode left, right; - TreeNode(int x) { - val = x; - left = null; - right = null; - } -} - -public class Main { - public static int result = 0; - - public static TreeNode constructBinaryTree(List levelOrder) { - if (levelOrder.isEmpty()) return null; - - TreeNode root = new TreeNode(Integer.parseInt(levelOrder.get(0))); - Queue queue = new LinkedList<>(); - queue.add(root); - int i = 1; - - while (!queue.isEmpty() && i < levelOrder.size()) { - TreeNode current = queue.poll(); - - if (i < levelOrder.size() && !levelOrder.get(i).equals("null")) { - current.left = new TreeNode(Integer.parseInt(levelOrder.get(i))); - queue.add(current.left); - } - i++; - - if (i < levelOrder.size() && !levelOrder.get(i).equals("null")) { - current.right = new TreeNode(Integer.parseInt(levelOrder.get(i))); - queue.add(current.right); - } - i++; - } - - return root; - } - - public static int dfs(TreeNode node) { - if (node == null) return 0; - int leftPath = dfs(node.left); - int rightPath = dfs(node.right); - - int leftNum = 0, rightNum = 0; - if (node.left != null && node.left.val == node.val) { - leftNum = leftPath + 1; - } - if (node.right != null && node.right.val == node.val) { - rightNum = rightPath + 1; - } - result = Math.max(result, leftNum + rightNum); - return Math.max(leftNum, rightNum); - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - sc.nextLine(); // consume the newline character - List levelOrder = new ArrayList<>(); - for (int i = 0; i < n; i++) { - levelOrder.add(sc.next()); - } - TreeNode root = constructBinaryTree(levelOrder); - dfs(root); - System.out.println(result); - sc.close(); - } -} - -``` - -### python - -```python -from typing import List, Optional -from collections import deque -import sys - -class TreeNode: - def __init__(self, val: int = 0, left: 'TreeNode' = None, right: 'TreeNode' = None): - self.val = val - self.left = left - self.right = right - -def construct_binary_tree(level_order: List[str]) -> Optional[TreeNode]: - if not level_order: - return None - - root = TreeNode(int(level_order[0])) - queue = deque([root]) - i = 1 - - while queue and i < len(level_order): - current = queue.popleft() - - if i < len(level_order) and level_order[i] != "null": - current.left = TreeNode(int(level_order[i])) - queue.append(current.left) - i += 1 - - if i < len(level_order) and level_order[i] != "null": - current.right = TreeNode(int(level_order[i])) - queue.append(current.right) - i += 1 - - return root - -result = 0 - -def dfs(node: Optional[TreeNode]) -> int: - global result - if node is None: - return 0 - - left_path = dfs(node.left) - right_path = dfs(node.right) - - left_num = right_num = 0 - if node.left is not None and node.left.val == node.val: - left_num = left_path + 1 - if node.right is not None and node.right.val == node.val: - right_num = right_path + 1 - - result = max(result, left_num + right_num) - return max(left_num, right_num) - -if __name__ == "__main__": - input = sys.stdin.read - data = input().strip().split() - - n = int(data[0]) - level_order = data[1:] - - root = construct_binary_tree(level_order) - dfs(root) - print(result) - - -``` diff --git "a/problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" deleted file mode 100644 index 1528fdbd3b..0000000000 --- "a/problems/kamacoder/0144.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\347\232\20401\345\255\227\347\254\246\344\270\262.md" +++ /dev/null @@ -1,66 +0,0 @@ - -# 0144.字典序最小的01字符串 - -贪心思路:移动尽可能 移动前面的1 ,这样可以是 字典序最小 - -从前到后遍历,遇到 0 ,就用前面的 1 来交换 - -```CPP -#include -#include -using namespace std; -int main() { - int n,k; - cin >> n >> k; - string s; - cin >> s; - for(int i = 0; i < n && k > 0; i++) { - if(s[i] == '0') { - // 开始用前面的 1 来交换 - int j = i; - while(j > 0 && s[j - 1] == '1' && k > 0) { - swap(s[j], s[j - 1]); - --j; - --k; - } - } - } - cout << s << endl; - return 0; -} - -``` - -Java: - -```Java - -import java.util.*; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int n = scanner.nextInt(); - int k = scanner.nextInt(); - scanner.nextLine(); // 消耗掉换行符 - String s = scanner.nextLine(); - char[] ch = s.toCharArray(); - - for (int i = 0; i < n && k > 0; i++) { - if (ch[i] == '0') { - // 开始用前面的 1 来交换 - int j = i; - while (j > 0 && ch[j - 1] == '1' && k > 0) { - char tmp = ch[j]; - ch[j] = ch[j - 1]; - ch[j - 1] = tmp; - j--; - k--; - } - } - } - - System.out.println(new String(ch)); - } -} -``` diff --git "a/problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" "b/problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" deleted file mode 100644 index 757fe0b267..0000000000 --- "a/problems/kamacoder/0145.\346\225\260\347\273\204\345\255\220\345\272\217\345\210\227\347\232\204\346\216\222\345\210\227.md" +++ /dev/null @@ -1,98 +0,0 @@ - -# 145. 数组子序列的排列 - -每个元素出现的次数相乘就可以了。 - -注意 “长度为 m 的数组,1 到 m 每个元素都出现过,且恰好出现 1 次。” ,题目中有n个元素,所以我们要统计的就是 1 到 n 元素出现的个数。 - -因为如果有一个元素x 大于n了, 那不可能出现 长度为x的数组 且 1 到 x 每个元素都出现过。 - -```CPP -#include "bits/stdc++.h" -using namespace std; -int main(){ - int n; - int x; - cin >> n; - unordered_map umap; - for(int i = 0; i < n; ++i){ - cin >> x; - if(umap.find(x) != umap.end()) umap[x]++; - else umap[x] = 1; - } - long long res = 0; - long long num = 1; - for (int i = 1; i <= n; i++) { - if (umap.find(i) == umap.end()) break; // 如果i都没出现,后面得数也不能 1 到 m 每个元素都出现过 - num = (num * umap[i]) % 1000000007; - res += num; - res %= 1000000007; - } - cout << res << endl; -} - -``` - -```Java - -import java.util.HashMap; -import java.util.Map; -import java.util.Scanner; - -public class Main { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - Map map = new HashMap<>(); - for (int i = 0; i < n; i++) { - int x = sc.nextInt(); - map.put(x, map.getOrDefault(x, 0) + 1); - } - long res = 0; - long num = 1; - for (int i = 1; i <= n; i++) { - if (!map.containsKey(i)) break; // 如果i都没出现,后面得数也不能1到m每个元素都出现过 - num = (num * map.get(i)) % 1000000007; - res += num; - res %= 1000000007; - } - System.out.println(res); - sc.close(); - } -} - -``` - - -```python -def main(): - import sys - input = sys.stdin.read - data = input().split() - - n = int(data[0]) - umap = {} - - for i in range(1, n + 1): - x = int(data[i]) - if x in umap: - umap[x] += 1 - else: - umap[x] = 1 - - res = 0 - num = 1 - MOD = 1000000007 - - for i in range(1, n + 1): - if i not in umap: - break # 如果i都没出现,后面得数也不能1到m每个元素都出现过 - num = (num * umap[i]) % MOD - res = (res + num) % MOD - - print(res) - -if __name__ == "__main__": - main() - -``` diff --git "a/problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" "b/problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" deleted file mode 100644 index 4cafb0402f..0000000000 --- "a/problems/kamacoder/0146.\344\274\240\351\200\201\346\240\221.md" +++ /dev/null @@ -1,65 +0,0 @@ - - - - -# 146. 传送树 - -本题题意是比较绕的,我后面给补上了 【提示信息】对 题目输出样例讲解一下,相对会容易理解的多。 - -```CPP -#include -#include -#include -using namespace std; - -vector> edge; // 邻接表来存图 -vector nxt; -int n; - -/* - * 递归函数,用于找到每个节点的下一个传送门节点,并记录在nxt数组中。 - * 遍历当前节点的所有子节点,递归调用findNext以确保子节点的nxt值已经计算出来。 - * 更新当前节点的nxt值为其子节点中编号最小的节点。 - * 如果当前节点是叶子节点(即没有子节点),则将其nxt值设置为自身。 - */ -void findNext(int node) { - for (int v : edge[node]) { - findNext(v); - if (nxt[node] == -1 || nxt[node] > min(v, nxt[v])) { - nxt[node] = min(v, nxt[v]); - } - } - - // 叶子节点 - if (nxt[node] == -1) { - nxt[node] = node; - } -} - -// 计算从节点u出发经过若干次传送门到达叶子节点所需的步数。 -// 通过不断访问nxt节点,直到到达叶子节点,记录访问的节点数。 -int get(int u) { - int cnt = 1; - while (nxt[u] != u) { - cnt++; - u = nxt[u]; - } - return cnt; -} - -int main() { - cin >> n; - edge.resize(n + 1); - nxt.resize(n + 1, -1); - for (int i = 1; i <= n; ++i) { - int a, b; - cin >> a >> b; - edge[a].push_back(b); - } - findNext(1); - for (int i = 1; i <= n; ++i) { - cout << get(i) << ' '; - } -} - -``` diff --git "a/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" "b/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" deleted file mode 100644 index 2ef5d39291..0000000000 --- "a/problems/kamacoder/0147.\344\270\211\347\217\240\344\272\222\346\226\245.md" +++ /dev/null @@ -1,78 +0,0 @@ - -# 三珠互斥 - -1. 如果k * 3 大于 n 了,那说明一定没结果,如果没想明白,大家举个例子试试看 -2. 分别求出三个红珠子之间的距离 -3. 对这三段距离从小到大排序 y1, y2, y3 -4. 如果第一段距离y1 小于k,说明需要交换 k - y 次, 同理 第二段距离y2 小于k,说明需要交换 k - y2 次 -5. y1 y2 都调整好了,不用计算y3,因为 y3是距离最大 - -```CPP -#include -using namespace std; - -int main(){ - int t; - cin >> t; - int n, k, a1, a2, a3; - vector dis(3); - - while (t--) { - cin >> n >> k >> a1 >> a2 >> a3; - if(k * 3 > n){ - cout << -1 << endl; - continue; - } - dis[0] = min(abs(a1 - a2), n - abs(a1 - a2)); - dis[1] = min(abs(a1 - a3), n - abs(a1 - a3)); - dis[2] = min(abs(a3 - a2), n - abs(a3 - a2)); - - sort(dis.begin(), dis.end()); - - int result = 0; - if (dis[0] < k) result += (k - dis[0]); - if (dis[1] < k) result += (k - dis[1]); - - cout << result << endl; - } - return 0; -} -``` - -Java代码: - -```Java -import java.util.*; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int t = scanner.nextInt(); - - while (t-- > 0) { - int n = scanner.nextInt(); - int k = scanner.nextInt(); - int a1 = scanner.nextInt(); - int a2 = scanner.nextInt(); - int a3 = scanner.nextInt(); - if (k * 3 > n) { - System.out.println(-1); - continue; - } - - List dis = new ArrayList<>(3); - dis.add(Math.min(Math.abs(a1 - a2), n - Math.abs(a1 - a2))); - dis.add(Math.min(Math.abs(a1 - a3), n - Math.abs(a1 - a3))); - dis.add(Math.min(Math.abs(a3 - a2), n - Math.abs(a3 - a2))); - - Collections.sort(dis); - - int result = 0; - if (dis.get(0) < k) result += (k - dis.get(0)); - if (dis.get(1) < k) result += (k - dis.get(1)); - - System.out.println(result); - } - } -} -``` diff --git "a/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" "b/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" deleted file mode 100644 index cf8d325fba..0000000000 --- "a/problems/kamacoder/0148.\346\211\221\345\205\213\347\211\214\345\220\214\350\212\261\351\241\272.md" +++ /dev/null @@ -1,122 +0,0 @@ - -# 扑克牌同花顺 - -首先我们要定义一个结构体,来存放我们的数据 - -`map<花色,{同一花色牌集合,同一花色的牌对应的牌数量}>` - -再遍历 每一个花色下,每一个牌 的数量 - -代码如下详细注释: - - -```CPP -#include -using namespace std; - -string cards[] = {"H","S","D","C"}; -typedef long long ll; -struct color -{ - set st; // 同一花色 牌的集合 - map cnt; // 同一花色 牌对应的数量 -}; -unordered_map umap; - -int main() { - int n; - cin >> n; - for (int i = 0; i < n; i++) { - int x, y; - string card; - cin >> x >> y >> card; - umap[card].st.insert(x); - umap[card].cnt[x] += y; - } - ll sum = 0; - // 遍历每一个花色 - for (string cardOne : cards) { - color colorOne = umap[cardOne]; - // 遍历 同花色 每一个牌 - for (int number : colorOne.st) { - ll numberCount = colorOne.cnt[number]; // 获取牌为number的数量是 numberCount - - // 统计 number 到 number + 4 都是否有牌,用cal 把 number 到number+4 的数量记下来 - ll cal = numberCount; - for (int j = number + 1; j <= number + 4; j++) cal = min(cal, colorOne.cnt[j]); - // 统计结果 - sum += cal; - // 把统计过的同花顺数量减下去 - for (int j = number + 1; j <= number + 4; j++) colorOne.cnt[j] -= cal; - } - } - cout << sum << endl; -} -``` - -Java代码如下: - -```Java - -import java.util.*; - -public class Main { - static String[] cards = {"H", "S", "D", "C"}; // 花色数组 - - static class Color { - Set st; // 同一花色牌的集合 - Map cnt; // 同一花色牌对应的数量 - - Color() { - st = new HashSet<>(); // 初始化集合 - cnt = new HashMap<>(); // 初始化映射 - } - } - - static Map umap = new HashMap<>(); // 用于存储每种花色对应的Color对象 - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int n = scanner.nextInt(); // 读取牌的数量 - - for (int i = 0; i < n; i++) { - int x = scanner.nextInt(); // 读取牌的值 - int y = scanner.nextInt(); // 读取牌的数量 - String card = scanner.next(); // 读取牌的花色 - - umap.putIfAbsent(card, new Color()); // 如果不存在该花色,则创建一个新的Color对象 - umap.get(card).st.add(x); // 将牌的值加入集合 - umap.get(card).cnt.put(x, umap.get(card).cnt.getOrDefault(x, 0L) + y); // 更新牌的数量 - } - - long sum = 0; // 结果累加器 - - // 遍历每一种花色 - for (String cardOne : cards) { - Color colorOne = umap.getOrDefault(cardOne, new Color()); // 获取对应花色的Color对象 - - // 遍历同花色的每一张牌 - for (int number : colorOne.st) { - long numberCount = colorOne.cnt.get(number); // 获取当前牌的数量 - - // 计算从当前牌到number+4的最小数量 - long cal = numberCount; - for (int j = number + 1; j <= number + 4; j++) { - cal = Math.min(cal, colorOne.cnt.getOrDefault(j, 0L)); // 更新cal为最小值 - } - - // 将结果累加到sum - sum += cal; - - // 将统计过的同花顺数量减去 - for (int j = number + 1; j <= number + 4; j++) { - colorOne.cnt.put(j, colorOne.cnt.getOrDefault(j, 0L) - cal); - } - } - } - - System.out.println(sum); // 输出结果 - } -} - -``` diff --git "a/problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" "b/problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" deleted file mode 100644 index 09088168bc..0000000000 --- "a/problems/kamacoder/0149.\345\245\275\346\225\260\347\273\204.md" +++ /dev/null @@ -1,102 +0,0 @@ - -# 149. 好数组 - -贪心思路: - -整体思路是移动到中间位置(中位数),一定是 移动次数最小的。 - -有一个数可以不改变,对数组排序之后, 最小数 和 最大数 一定是移动次数最多的,所以分别保留最小 和 最大的不变。 - -中间可能有两个位置,所以要计算中间偏前 和 中间偏后的 - -代码如下: - -```CPP -#include -using namespace std; - -int main() { - int n; - cin >> n; - vector arr(n); - for (int i = 0; i < n; ++i) { - cin >> arr[i]; - } - sort(arr.begin(), arr.end()); - - if (arr[0] == arr[n - 1]) { - cout << 1 << endl; - return 0; - } - long cnt = 0L; - long cnt1 = 0L; - - // 如果要保留一个不改变,要不不改最小的,要不不改最大的。 - - // 取中间偏前的位置 - long mid = arr[(n - 2) / 2]; - - // 不改最大的 - for (int i = 0; i < n - 1; i++) { - cnt += abs(arr[i] - mid); - } - - // 取中间偏后的位置 - mid = arr[n / 2]; - - // 不改最小的 - for (int i = 1; i < n; i++) { - cnt1 += abs(arr[i] - mid); - } - - cout << min(cnt, cnt1) << endl; - return 0; -} -``` - -Java代码如下: - -```Java - -import java.util.*; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int n = scanner.nextInt(); - long[] arr = new long[n]; - for (int i = 0; i < n; ++i) { - arr[i] = scanner.nextLong(); - } - Arrays.sort(arr); - - if (arr[0] == arr[n - 1]) { - System.out.println(1); - return; - } - long cnt = 0L; - long cnt1 = 0L; - - // 如果要保留一个不改变,要不不改最小的,要不不改最大的。 - - // 取中间偏前的位置 - long mid = arr[(n - 2) / 2]; - - // 不改最大的 - for (int i = 0; i < n - 1; i++) { - cnt += Math.abs(arr[i] - mid); - } - - // 取中间偏后的位置 - mid = arr[n / 2]; - - // 不改最小的 - for (int i = 1; i < n; i++) { - cnt1 += Math.abs(arr[i] - mid); - } - - System.out.println(Math.min(cnt, cnt1)); - } -} - -``` diff --git "a/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" "b/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" deleted file mode 100644 index 503d6a23ac..0000000000 --- "a/problems/kamacoder/0150.\346\236\201\351\225\277\350\277\236\347\273\255\346\256\265\347\232\204\346\235\203\345\200\274.md" +++ /dev/null @@ -1,66 +0,0 @@ - -# 150. 极长连续段的权值 - -动态规划,枚举最后边节点的情况: - -```CPP -#include -#include -using namespace std; - -int main() { - int n; - cin >> n; - string s; - cin >> s; - - long long result = 1; - long long a = 1; - - for (int i = 1; i < n; ++i) { - // 加上本身长度为1的子串 - if (s[i] == s[i - 1]) { - a += 1; - result += a; - // 以最右节点为终点,每个子串的级长连续段都+1,再加本身长度为1的子串 - } else { - a = a + i + 1; - result += a; - } - } - cout << result << endl; - return 0; -} -``` - -Java代码如下: - -```Java -import java.util.Scanner; - -public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - int n = scanner.nextInt(); - String s = scanner.next(); - - long result = 1; - long a = 1; - - for (int i = 1; i < n; ++i) { - // 加上本身长度为1的子串 - if (s.charAt(i) == s.charAt(i - 1)) { - a += 1; - result += a; - // 以最右节点为终点,每个子串的级长连续段都+1,再加本身长度为1的子串 - } else { - a = a + i + 1; - result += a; - } - } - - System.out.println(result); - } -} - -``` diff --git "a/problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" "b/problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" deleted file mode 100644 index 3859f7b039..0000000000 --- "a/problems/kamacoder/0151.\346\211\213\346\234\272\346\265\201\347\225\205\350\277\220\350\241\214\347\232\204\347\247\230\345\257\206.md" +++ /dev/null @@ -1,127 +0,0 @@ -# 151. 手机流畅运行的秘密 - -[题目链接](https://kamacoder.com/problempage.php?pid=1229) - -先运行 能留下电量多的 任务,才能有余电运行其他任务。 - -任务1,1:10 ,运行完 能留下 9个电 - -任务2,2:12,运行完 能留下 10个电 - -任务3,3:10,运行完 能留下 7个电。 - -运行顺序: 任务2 -> 任务1 -> 任务3 - -按照 最低初始电量 - 耗电量,从大到小排序。 - -计算总电量,需要 从小到大 遍历, 不断取 总电量 + 任务耗电量 与 任务最低初始电量 的最大值。 - -```CPP -#include -using namespace std; - -bool cmp(const pair& taskA, const pair& taskB) { - return (taskA.second - taskA.first) < (taskB.second - taskB.first); -} -int main() { - string str, tmp; - vector> tasks; - - //处理输入 - getline(cin, str); - stringstream ss(str); - while (getline(ss, tmp, ',')) { - int p = tmp.find(":"); - string a = tmp.substr(0, p); - string b = tmp.substr(p + 1); - tasks.push_back({stoi(a), stoi(b)}); - } - - // 按照差值从小到大排序 - sort(tasks.begin(), tasks.end(), cmp); - - // 收集结果 - int result = 0; - for (int i = 0 ; i < tasks.size(); i++) { - result = max(result + tasks[i].first, tasks[i].second); - } - - result = result <= 4800 ? result : -1; - cout << result << endl; - -} -``` - -Java版本: - -```Java -import java.util.*; -import java.util.stream.Collectors; - -public class Main { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - String str = sc.nextLine(); - String[] tasksArray = str.split(","); - List tasks = Arrays.stream(tasksArray) - .map(task -> { - String[] parts = task.split(":"); - return new Pair(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); - }) - .collect(Collectors.toList()); - - // 按照差值从小到大排序 - Collections.sort(tasks, (taskA, taskB) -> - (taskA.second - taskA.first) - (taskB.second - taskB.first) - ); - - // 收集结果 - int result = 0; - for (Pair task : tasks) { - result = Math.max(result + task.first, task.second); - } - - result = result <= 4800 ? result : -1; - System.out.println(result); - } -} - -class Pair { - int first; - int second; - - Pair(int first, int second) { - this.first = first; - this.second = second; - } -} - -``` - -Python版本: - -```python -def main(): - import sys - input = sys.stdin.read - - str = input().strip() - tasks = [] - for tmp in str.split(','): - a, b = map(int, tmp.split(':')) - tasks.append((a, b)) - - # 按照差值从小到大排序 - tasks.sort(key=lambda task: task[1] - task[0]) - - # 收集结果 - result = 0 - for task in tasks: - result = max(result + task[0], task[1]) - - result = result if result <= 4800 else -1 - print(result) - -if __name__ == "__main__": - main() -``` diff --git "a/problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" "b/problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" deleted file mode 100644 index afb5d8eaf4..0000000000 --- "a/problems/kamacoder/0152.\345\260\217\347\261\263\346\211\213\346\234\272\351\200\232\344\277\241\346\240\241\345\207\206.md" +++ /dev/null @@ -1,121 +0,0 @@ - - -# 152. 小米手机通信校准 - -[题目链接](https://kamacoder.com/problempage.php?pid=1230) - -一道模拟题,但比较考察 代码能力。 - -遍历去找 里 freq 最近的 freg就好, 需要记录刚遍历过的的freg和 loss,因为可能有 相邻一样的 freg。 - -```CPP -#include -using namespace std; - -int main() { - int freq; - cin >> freq; - string data; - double result = 0; - int last_freg = 0; // 记录上一个 freg - int last_loss = 0; // 记录上一个loss - while(cin >> data) { - int index = data.find(':'); - int freg = stoi(data.substr(0, index)); // 获取 freg 和 loss - int loss = stoi(data.substr(index + 1)); - // 两遍一样 - if(abs(freg - freq) == abs(last_freg - freq)) { - result = (double)(last_loss + loss)/2.0; - } // 否则更新最新的result - else if(abs(freg - freq) < abs(last_freg - freq)){ - result = (double)loss; - } - last_freg = freg; - last_loss = loss; - } - printf("%.1lf\n", result); - return 0; -} - -``` - -Java 版本: - -```Java - -import java.util.Scanner; - -public class Main { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int freq = sc.nextInt(); - sc.nextLine(); // 读取换行符 - - String inputLine = sc.nextLine(); // 读取包含所有后续输入的行 - String[] data = inputLine.split(" "); // 根据空格分割输入 - - double result = 0; - int lastFreq = 0; // 记录上一个 freg - int lastLoss = 0; // 记录上一个 loss - - for (String entry : data) { - int index = entry.indexOf(':'); - int freg = Integer.parseInt(entry.substring(0, index)); // 获取 freg 和 loss - int loss = Integer.parseInt(entry.substring(index + 1)); - - // 两遍一样 - if (Math.abs(freg - freq) == Math.abs(lastFreq - freq)) { - result = (double) (lastLoss + loss) / 2.0; - } - // 否则更新最新的 result - else if (Math.abs(freg - freq) < Math.abs(lastFreq - freq)) { - result = (double) loss; - } - - lastFreq = freg; - lastLoss = loss; - } - - System.out.printf("%.1f\n", result); - sc.close(); - } -} - -``` - -Python版本: - -```python -def main(): - import sys - input = sys.stdin.read - data = input().split() - - freq = int(data[0]) - result = 0 - last_freg = 0 # 记录上一个 freg - last_loss = 0 # 记录上一个 loss - - for i in range(1, len(data)): - item = data[i] - index = item.find(':') - freg = int(item[:index]) # 获取 freg 和 loss - loss = int(item[index + 1:]) - - # 两遍一样 - if abs(freg - freq) == abs(last_freg - freq): - result = (last_loss + loss) / 2.0 - # 否则更新最新的 result - elif abs(freg - freq) < abs(last_freg - freq): - result = loss - - last_freg = freg - last_loss = loss - - print(f"{result:.1f}") - -if __name__ == "__main__": - main() - - -``` diff --git "a/problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" "b/problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" deleted file mode 100644 index 2c5562b392..0000000000 --- "a/problems/kamacoder/0153.\346\235\203\345\200\274\344\274\230\345\212\277\350\267\257\345\276\204\350\256\241\346\225\260.md" +++ /dev/null @@ -1,95 +0,0 @@ - - -# 权值优势路径计数 - -[题目链接](https://kamacoder.com/problempage.php?pid=1231) - -1、构建二叉树:首先根据层序遍历的序列构建二叉树。这可以通过使用队列来实现,队列中存储当前节点及其索引,确保可以正确地将子节点添加到父节点下。 - -2、路径遍历:使用深度优先搜索(DFS)遍历所有从根到叶子的路径。在遍历过程中,维护一个计数器跟踪当前路径中权值为 1 和权值为 0 的节点的数量。 - -3、计数满足条件的路径:每当到达一个叶子节点时,检查当前路径的权值 1 的节点数量是否比权值 0 的节点数量多 1。如果满足,递增一个全局计数器。 - - -```CPP - -#include -#include -#include - -using namespace std; - -struct TreeNode { - int val; - TreeNode *left, *right; - TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} -}; - -// DFS遍历二叉树,并计算满足条件的路径数量 -void countPaths(TreeNode* node, int count1, int count0, int& result) { - if (!node) return; - - // 更新当前路径中1和0的数量 - node->val == 1 ? count1++ : count0++; - - // 检查当前节点是否为叶子节点 - if (!node->left && !node->right) { - // 检查1的数量是否比0的数量多1 - if (count1 == count0 + 1) { - result++; - } - return; - } - - // 递归访问左右子节点 - countPaths(node->left, count1, count0, result); - countPaths(node->right, count1, count0, result); -} - -int main() { - int N; - cin >> N; - - vector nums(N); - for (int i = 0; i < N; ++i) { - cin >> nums[i]; - } - - if (nums.empty()) { - cout << 0 << endl; - return 0; - } - - // 根据层序遍历的输入构建二叉树 - queue q; - TreeNode* root = new TreeNode(nums[0]); - q.push(root); - int index = 1; - - while (!q.empty() && index < N) { - TreeNode* node = q.front(); - q.pop(); - - if (index < N && nums[index] != -1) { - node->left = new TreeNode(nums[index]); - q.push(node->left); - } - index++; - - if (index < N && nums[index] != -1) { - node->right = new TreeNode(nums[index]); - q.push(node->right); - } - index++; - } - - // 计算满足条件的路径数 - int result = 0; - countPaths(root, 0, 0, result); - - cout << result << endl; - - return 0; -} - -``` diff --git "a/problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" "b/problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" deleted file mode 100644 index 90e5b7a413..0000000000 --- "a/problems/kamacoder/0154.\345\272\217\345\210\227\344\270\255\344\275\215\346\225\260.md" +++ /dev/null @@ -1,68 +0,0 @@ - -# 序列中位数 - -[题目链接](https://kamacoder.com/problempage.php?pid=1232) - -注意给的数组默认不是有序的! - -模拟题,排序之后,取中位数,然后按照b数组 删 a数组中元素,再取中位数。 - -```CPP -#include -using namespace std; - -// 计算并返回中位数 -double findMedian(vector& nums) { - int n = nums.size(); - if (n % 2 == 1) { - return nums[n / 2]; // 奇数长度,返回中间的元素 - } else { - // 偶数长度,返回中间两个元素的平均值 - return (nums[n / 2] + nums[n / 2 - 1]) / 2.0; - } -} - - -int main(){ - int t; - cin >> t; - while(t--){ - int n; - cin>> n; - vector a(n); - vector b(n - 1); - for(int i = 0; i < n; i++){ - cin >> a[i]; - } - for(int i = 0; i < n - 1; i++){ - cin >> b[i]; - } - vector nums = a; - vector answers; - - sort(nums.begin(), nums.end()); - - // 把中位数放进结果集 - answers.push_back(findMedian(nums)); - - for(int i = 0; i < n - 1; i++){ - - int target = a[b[i]]; - // 删除目标值 - nums.erase(find(nums.begin(), nums.end(), target)); - // 把中位数放进结果集 - answers.push_back(findMedian(nums)); - - } - - for(auto answer : answers){ - // 判断是否是整数 - if(answer == (int)answer) printf("%d ", (int)answer); - else printf("%.1f ", answer); - } - cout << endl; - } - -} - -``` diff --git "a/problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" "b/problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" deleted file mode 100644 index d79d695548..0000000000 --- "a/problems/kamacoder/0155.\346\234\200\345\260\217\345\214\226\351\242\221\347\216\207\347\232\204\345\210\240\351\231\244\344\273\243\344\273\267.md" +++ /dev/null @@ -1,106 +0,0 @@ - -# 最小化频率的删除代价 - -[题目链接](https://kamacoder.com/problempage.php?pid=1233) - -计数和排序: - -* 使用 map 或 unordered_map 对数组 a 中每个元素出现的次数进行统计。 -* 将统计结果存入一个 vector>,其中 pair 的第一个元素是元素的出现次数,第二个元素是元素本身。 -* 按出现次数从大到小排序这个 vector。 - -确定最小 f(a): - -* 从最大出现次数开始尝试减少 f(a)。为此,从最高频次的元素开始逐步向下考虑较少出现的元素,计算达到更低 f(a) 所需删除的元素数量。 -* 使用一个累加器 count 来记录需要删除的元素数量,直到这个数量超过允许的最大删除数量 k 或恰好等于 k。在此过程中,尽量使 f(a) 达到最小。 - -计算达到 f(a) 的代价: - -* 计算完成后,需要确定达到最小 f(a) 的确切代价。首先,为每个元素确定在不超过 k 的前提下可以删除的最大数量,以使得 f(a) 最小。 -* 对于每个元素,如果它的数量超过了新的 f(a),则计算减少到 f(a) 所需删除的具体元素数,记录下来。 - -计算具体删除代价: - -* 遍历原数组,对于每个需要删除的元素,根据其位置累加删除代价。每删除一个元素,相应地减少其在删除列表中的计数。当某元素需要删除的数量减至 0 时,从删除列表中移除该元素。 - - -```CPP - -#include -#include -#include -#include - -using namespace std; - -int main() { - int n, k; - cin >> n >> k; - - vector a(n); - for (int i = 0; i < n; ++i) { - cin >> a[i]; - } - - unordered_map umap; // 使用map来统计每个元素的出现频率 - for (int i = 0; i < n; ++i) { - umap[a[i]]++; // 统计每个元素的出现次数 - } - - vector> table; - for (auto& pair : umap) { - table.push_back({pair.second, pair.first}); // 将元素和其频率作为一个pair放入table中 - } - - sort(table.begin(), table.end(), greater<>()); // 将table按照频率从大到小排序 - - int count = 0; // 用来计算已经删除的元素总数 - int minVal = table[0].first; // 从最高频率开始 - for (int i = 0; i < table.size(); ++i) { - int freq = table[i].first; - count += (minVal - freq) * i; // 累加删除元素的代价 - if (count > k) break; // 如果超过了k,停止循环 - else if (count == k) { - minVal = freq; - break; - } else minVal = freq; - } - if (count < k) { - int addDel = (k - count) / table.size(); // 如果删除的代价还没达到k,计算还可以进一步减少的频率 - minVal -= addDel; // 减少相应的频率 - } - - if (minVal < 0) { - minVal = 0; // 确保最小频率值不小于0 - } - - unordered_map deleteList; // 用来存储需要删除的元素及其数量 - for (auto& elem : table) { - int num = elem.first; - int ind = elem.second; - if (num > minVal) { - deleteList[ind] = num - minVal; // 如果元素频率大于最小值,计算需要删除的数量 - } else { - break; - } - } - - int cost = 0; // 计算总的删除代价 - for (int i = 0; i < n; ++i) { - if (deleteList.find(a[i]) != deleteList.end()) { - cost += i + 1; // 删除的代价是元素的索引+1 - deleteList[a[i]]--; // 删除一个元素 - if (deleteList[a[i]] == 0) { - deleteList.erase(a[i]); // 如果元素已经全部删除,从列表中移除 - if (deleteList.empty()) { - break; // 如果没有元素需要删除了,结束循环 - } - } - } - } - - cout << minVal << " " << cost << endl; - return 0; -} - -``` diff --git "a/problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" "b/problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" deleted file mode 100644 index b7bea97458..0000000000 --- "a/problems/kamacoder/0156.\345\213\207\346\225\242\347\211\233\347\211\233\346\210\230\346\226\227\345\272\217\345\210\227.md" +++ /dev/null @@ -1,68 +0,0 @@ - -# 勇敢牛牛战斗序列 - -[题目链接](https://kamacoder.com/problempage.php?pid=1234) - -贪心思路,对数组从小到大排序之后,先取最右边,再取最左边,循环反复。 - -```CPP -#include - -using namespace std; - -int main() { - int n; - cin >> n; - vector a(n); // 使用 vector 存储整数数组 - for (int i = 0; i < n; i++) { - cin >> a[i]; // 读取数组 - } - sort(a.begin(), a.end()); // 对数组进行排序 - - long long ans = 0; // 使用 long long 存储结果,以防溢出 - int cur = 0; - int left = 0, right = n - 1; - while (left <= right) { - if (cur < a[right]) { - ans += a[right] - cur; - } - cur = a[left]; - right--; - left++; - } - cout << ans << endl; // 输出结果 - return 0; -} -``` - - - -```Java -import java.util.Arrays; -import java.util.Scanner; - -public class Main { - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - int[] a = new int[n]; - for (int i = 0; i < n; i++) { - a[i] = sc.nextInt(); - } - Arrays.sort(a); - long ans = 0; - int cur = 0; - int left = 0, right = a.length - 1; - while (left <= right) { - if (cur < a[right]) { - ans = ans + a[right] - cur; - } - cur = a[left]; - right--; - left++; - } - System.out.println(ans); - } -} -``` diff --git "a/problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" "b/problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" deleted file mode 100644 index 9ca4630df1..0000000000 --- "a/problems/kamacoder/0157.\346\234\200\345\244\247\345\214\226\345\257\206\347\240\201\345\244\215\346\235\202\345\272\246.md" +++ /dev/null @@ -1,59 +0,0 @@ - -# 最大化密码复杂度 - -[题目链接](https://kamacoder.com/problempage.php?pid=1235) - -注意**边界处理**,对于字符串的首尾位置,需要特别处理,因为它们只有一个相邻字符。 -* 遍历字符串 s,寻找 '?' 字符。 -* 对于每个 '?' 字符,选择一个字符填充,使其与前后字符都不同。这样做的目的是最大化密码的复杂度,即尽可能使相邻的字符不同。 -* 如果 '?' 是第一个或最后一个字符,或者无法找到与前后都不同的字符,选择与前一个或后一个字符不同的字符。 - - -```CPP -#include -#include -#include - -using namespace std; - -int main() { - int n, m; - string s; - cin >> n >> m >> s; - - if (n == 1) { - cout << 0 << endl; - return 0; - } - - // 统一处理包括左右字符的情况 - for (int i = 0; i < n; ++i) { - if (s[i] == '?') { - bool found = false; - for (char j = 'a'; j < 'a' + m; ++j) { - // 避免第一个字符 和 最后一个字符,因为两个字符只有一个相邻字符,没有左右相邻字符 - if ((i == 0 || s[i - 1] != j) && (i == n - 1 || s[i + 1] != j)) { - s[i] = j; - found = true; - break; - } - } - // 如果没有找到合适的字符,就和附近字符保持一致 - if (!found) { - if (i > 0) s[i] = s[i - 1]; - else s[i] = s[i + 1]; - } - } - } - - // 计算结果 - int result = 0; - for (int i = 0; i < n - 1; ++i) { - if (s[i] != s[i + 1]) result++; - } - - cout << result << endl; - return 0; -} - -``` diff --git "a/problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" "b/problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" deleted file mode 100644 index b99481d2de..0000000000 --- "a/problems/kamacoder/0158.\345\220\214\344\275\231\346\226\271\347\250\213.md" +++ /dev/null @@ -1,50 +0,0 @@ - -# 同余方程 - -题目链接:https://kamacoder.com/problempage.php?pid=1236 - -我们需要求出满足以下条件的最小正整数 x:`ax≡1 (mod b)` - -这意味着我们需要找到 x 使得 ax 除以 b 的余数是 1。这个问题实际上是一个典型的 模反元素 问题。 - -解题思路: - -* 为了求出最小的 x,我们可以使用 扩展欧几里得算法 来求出 a 对模 b 的逆元。 -* 这个算法能够求解 ax + by = gcd(a, b) 的一组整数解 (x, y),而在 gcd(a, b) = 1 的情况下,x 即为所求的模逆元。 -* 扩展欧几里得算法:扩展欧几里得算法可以通过递归或者迭代的方式实现。 - -下面给出C++代码实现: - -```CPP -#include -using namespace std; - -// 扩展欧几里得:计算 ax + by = gcd(a, b) 的解 -long long extended_gcd(long long a, long long b, long long &x, long long &y) { - if (b == 0) { - x = 1; - y = 0; - return a; - } - long long x1, y1; - long long gcd = extended_gcd(b, a % b, x1, y1); - x = y1; - y = x1 - (a / b) * y1; - return gcd; -} - -int main() { - long long a, b; - cin >> a >> b; - - long long x, y; - long long gcd = extended_gcd(a, b, x, y); - - // 由于我们只需要模 b 的正整数解,所以我们要保证 x 是正数 - x = (x % b + b) % b; - - cout << x << endl; - - return 0; -} -``` diff --git "a/problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" "b/problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" deleted file mode 100644 index 642cb74694..0000000000 --- "a/problems/kamacoder/0159.\345\244\247\346\225\264\346\225\260\344\271\230\346\263\225.md" +++ /dev/null @@ -1,62 +0,0 @@ - -# 大整数乘法 - -题目链接:https://kamacoder.com/problempage.php?pid=1237 - -思路: - -我们可以使用模拟手算乘法的方法,即「逐位相乘累加」,对于每一位的乘法结果,我们将其加到相应的结果位置上。最终将累加的结果输出。 - -具体步骤: - -* 初始化结果数组:结果数组的长度应该是两个数字长度之和,因为最大长度的结果不会超过这个长度。 -* 逐位相乘:从右往左遍历两个字符串的每一位,逐位相乘,并加到结果数组的相应位置。 -* 处理进位:在每一步累加之后处理进位,保证每个位置的值小于10。 - -将结果数组转化为字符串:从结果数组的最高位开始,忽略前导零,然后将数组转化为字符串。 - -```CPP -#include -#include -#include - -using namespace std; - -string multiply(string num1, string num2) { - int len1 = num1.size(); - int len2 = num2.size(); - vector result(len1 + len2, 0); - - // 逐位相乘 - for (int i = len1 - 1; i >= 0; i--) { - for (int j = len2 - 1; j >= 0; j--) { - int mul = (num1[i] - '0') * (num2[j] - '0'); - int sum = mul + result[i + j + 1]; - - result[i + j + 1] = sum % 10; - result[i + j] += sum / 10; - } - } - - // 将结果转换为字符串,跳过前导零 - string product; - for (int num : result) { - if (!(product.empty() && num == 0)) { // 跳过前导零 - product.push_back(num + '0'); - } - } - - return product.empty() ? "0" : product; -} - -int main() { - string num1, num2; - cin >> num1 >> num2; - - string result = multiply(num1, num2); - cout << result << endl; - - return 0; -} - -``` diff --git "a/problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" "b/problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" deleted file mode 100644 index 35c3ea3283..0000000000 --- "a/problems/kamacoder/0160.\344\272\214\347\273\264\345\271\263\351\235\242\344\270\212\347\232\204\346\212\230\347\272\277\346\256\265.md" +++ /dev/null @@ -1,88 +0,0 @@ - -# 二维平面上的折线段 - -题目链接:https://kamacoder.com/problempage.php?pid=1238 - -这个问题要求我们在一条折线段上,根据移动的固定距离 s 进行标记点的计算。 - -为了实现这一点,我们需要对折线段进行分段处理,并根据每段的长度来确定标记点的位置。 - -解题思路: - -1. 输入与初步处理: - * 首先,读取所有点的坐标。 - * 计算每一段折线的长度,并逐段累积总长度。 -2. 确定标记点: - * 从起点开始,每次沿着折线段前进 s 的距离,直到到达终点。 - * 对于每个标记点,根据当前段的起点和终点,计算出该点的精确坐标。 -3. 输出所有标记点的坐标,格式为 x, y。 - -```CPP - -#include -#include -#include -#include - -using namespace std; - -// 定义一个点的结构体 -struct Point { - double x, y; -}; - -// 计算两点之间的距离 -double distance(const Point& a, const Point& b) { - return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); -} - -int main() { - int n; - cin >> n; - - vector points(n); - for (int i = 0; i < n; i++) { - cin >> points[i].x >> points[i].y; - } - - double s; - cin >> s; - - double total_length = 0.0; - vector segment_lengths(n - 1); - - // 计算每段长度和总长度 - for (int i = 0; i < n - 1; i++) { - segment_lengths[i] = distance(points[i], points[i + 1]); - total_length += segment_lengths[i]; - } - - // 从起点开始标记 - Point current_point = points[0]; - double accumulated_distance = 0.0; - - cout << fixed << setprecision(5); - cout << current_point.x << ", " << current_point.y << endl; - - while (accumulated_distance + s <= total_length) { - accumulated_distance += s; - double remaining_distance = accumulated_distance; - - for (int i = 0; i < n - 1; i++) { - if (remaining_distance <= segment_lengths[i]) { - double ratio = remaining_distance / segment_lengths[i]; - double new_x = points[i].x + ratio * (points[i + 1].x - points[i].x); - double new_y = points[i].y + ratio * (points[i + 1].y - points[i].y); - current_point = {new_x, new_y}; - cout << current_point.x << ", " << current_point.y << endl; - break; - } else { - remaining_distance -= segment_lengths[i]; - } - } - } - - return 0; -} - -``` diff --git "a/problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" "b/problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" deleted file mode 100644 index 84471f8cd6..0000000000 --- "a/problems/kamacoder/0161.\350\256\250\345\216\214\351\254\274\347\232\204\347\273\204\345\220\210\345\270\226\345\255\220.md" +++ /dev/null @@ -1,61 +0,0 @@ - -# 讨厌鬼的组合帖子 - -[题目链接](https://kamacoder.com/problempage.php?pid=1239) - -这个问题本质上是要找到两个数组的子集,使得这两个子集之间的差的绝对值最大。 - -问题可以简化为寻找两个数列之间最大可能的差的绝对值。 - -贪心思路如下: - -计算差异,首先,我们可以计算每个帖子的点赞数和点踩数的差值 d[i] = a[i] - b[i]。这样问题就转化为选择这些差值的一个子集,使得子集中所有元素的和的绝对值最大。 - -遍历可能性,要使得一个数的绝对值尽可能大,可以尝试最大化这个数,或者最小化这个数(使其尽可能小于零)。我们可以分别尝试将所有正的差值加在一起,以及将所有负的差值加在一起。 - -计算最大吸引度: - -* 将所有正的差值求和得到一个总和。 -* 将所有负的差值求和得到另一个总和。 -* 最后,吸引度即为这两个总和的绝对值中的较大者。 - - -```CPP - -#include -#include -#include - -using namespace std; - -int main() { - int n; - cin >> n; - - vector a(n), b(n); - for (int i = 0; i < n; ++i) { - cin >> a[i]; - } - for (int i = 0; i < n; ++i) { - cin >> b[i]; - } - - long long positive_sum = 0; - long long negative_sum = 0; - - for (int i = 0; i < n; ++i) { - int difference = a[i] - b[i]; - if (difference > 0) { - positive_sum += difference; - } else if (difference < 0) { - negative_sum += difference; - } - } - - // 最大吸引度是正总和或负总和的绝对值中的较大者 - cout << max(abs(positive_sum), abs(negative_sum)) << endl; - - return 0; -} -``` - diff --git "a/problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" "b/problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" deleted file mode 100644 index 4967f907be..0000000000 --- "a/problems/kamacoder/0163.\344\274\230\347\247\200\346\225\260\347\273\204.md" +++ /dev/null @@ -1,88 +0,0 @@ - -# 优秀数组 - -[题目链接](https://kamacoder.com/problempage.php?pid=1241) - -## 解题思路 - -1、初始分析 - -- 给定一个排列 `p`,我们首先构建一个 `pos` 数组,使得 `pos[i]` 表示 `i` 在排列 `p` 中的位置。 -- 我们需要判断数组 `a` 是否是一个优秀数组,即 `pos[a[i]] < pos[a[i+1]] <= pos[a[i]] + d` 对于所有 `i` 都成立。 -- 我们的目标是通过最少的相邻元素交换,使得数组 `a` 不再是一个优秀数组。 - -2、思路 - -- 要使数组 `a` 不再是优秀数组,我们只需要打破条件 `pos[a[i]] < pos[a[i+1]] <= pos[a[i]] + d` 中的某一个。 -- 一种简单的做法是让 `pos[a[i]]` 和 `pos[a[i+1]]` 之间的距离超过 `d`,或者直接让 `pos[a[i]] >= pos[a[i+1]]`。 - -3、具体方法 - -- 只需要考虑 `a` 中相邻元素的顺序,并判断如何交换 `p` 中相邻元素使得其顺序被打破。 -- 假设我们需要在 `p` 中交换某些元素来实现上述目标,那么最小的交换次数是将 `a[i]` 和 `a[i+1]` 的位置交换。 -- 如果 `pos[a[i]] + 1 == pos[a[i+1]]`,则需要一步交换。 - -4、特别情况 - -- 还需要考虑,如果通过交换相邻元素无法解决问题的情况。比如 `pos[a[i+1]]` 的位置无法移到 `pos[a[i]]` 的前面或超过 `d`。 - -C++代码如下: - - -```cpp -#include -#include -#include -#include - -using namespace std; - -int main() { - int n, m, d; - cin >> n >> m >> d; - - vector p(n + 1); - vector pos(n + 1); - - // 读取排列 p,并构建位置数组 pos - for (int i = 1; i <= n; i++) { - cin >> p[i]; - pos[p[i]] = i; - } - - vector a(m); - for (int i = 0; i < m; i++) { - cin >> a[i]; - } - - int min_operations = INT_MAX; - - // 遍历数组 a 的相邻元素 - for (int i = 0; i < m - 1; i++) { - int current_pos = pos[a[i]]; - int next_pos = pos[a[i + 1]]; - - // 检查 pos[a[i]] < pos[a[i+1]] <= pos[a[i]] + d 是否成立 - if (current_pos < next_pos && next_pos <= current_pos + d) { - // 计算需要的最少操作次数 - int distance = next_pos - current_pos; - - // Case 1: 交换 current_pos 和 next_pos - min_operations = min(min_operations, distance); - - // Case 2: 如果 next_pos + d <= n,考虑使 pos[a[i+1]] 超过 pos[a[i]] + d - if (current_pos + d + 1 <= n) { - min_operations = min(min_operations, d + 1 - distance); - } - } else { - min_operations = 0; - } - } - - cout << min_operations << endl; - return 0; -} - -``` - -时间复杂度为 O(m) diff --git "a/problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" "b/problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" deleted file mode 100644 index 2a35b715bf..0000000000 --- "a/problems/kamacoder/0164.\345\215\207\345\272\217\346\225\260\347\273\204.md" +++ /dev/null @@ -1,81 +0,0 @@ -# 升序数组 - -[题目链接](https://kamacoder.com/problempage.php?pid=1241) - -## 解题思路 - -贪心思路 - -- **计算相邻元素差值**: - - 对于数组 `a`,计算每对相邻元素的差值 `diff[i] = a[i+1] - a[i]`。 - - 如果 `diff[i]` 为负数,意味着 `a[i+1]` 比 `a[i]` 小或相等,需要通过操作使 `a[i+1]` 变大。 - -- **确定最小操作次数**: - - 计算所有相邻元素中的最小差值 `minDifference`,即 `minDifference = min(diff[i])`。 - - 如果 `minDifference` 为负数或零,则需要进行 `-minDifference + 1` 次操作,使得 `a[i+1]` 大于 `a[i]`,从而使数组严格递增。 - -- **实现细节**: - - 遍历数组的每对相邻元素,找出最小的差值。 - - 根据最小差值,计算出最少的操作次数。 - - - -```CPP -#include -#include -#include - -using namespace std; - -int main() -{ - int n; - cin >> n; - - vector arr(n); // 用于存储输入数组 - vector differences; // 用于存储相邻元素的差值 - - for(int i = 0; i < n; i++) { - cin >> arr[i]; - if(i > 0) differences.push_back(arr[i] - arr[i - 1]); - - } - - int minDifference = INT_MAX; - - // 寻找最小的差值 - for(int diff : differences) { - if(diff < minDifference) { - minDifference = diff; - } - } - - // 如果最小差值是负数或零,计算所需的操作次数 - int minOperations = max(0, -minDifference + 1); - - cout << minOperations << endl; - - return 0; -} - -``` -关于 `-minDifference + 1` 为什么要 + 1 解释: - -对于数组 `a` 中相邻的两个元素 `a[i]` 和 `a[i+1]`,我们计算它们的差值 `diff = a[i+1] - a[i]`。 - -- **目标**:要使 `a[i] < a[i+1]`,需要 `diff > 0`。 -- 如果 `diff < 0`,说明 `a[i+1]` 比 `a[i]` 小,这时候 `a` 不是严格递增的。 -- 如果 `diff = 0`,说明 `a[i+1]` 和 `a[i]` 相等,这时也不满足严格递增。 - -解释 `-minDifference + 1` - -1. **当 `minDifference < 0` 时**: - - 假设 `minDifference` 是所有相邻差值中的最小值,并且它是一个负数。 - - 例如,`minDifference = -3`,表示 `a[i+1] - a[i] = -3`,也就是 `a[i+1]` 比 `a[i]` 小 `3`。 - - 要让 `a[i+1] > a[i]`,我们至少需要使 `a[i+1] - a[i]` 从 `-3` 增加到 `1`。因此需要增加 `4`,即 `(-(-3)) + 1 = 3 + 1 = 4` 次操作。 - -2. **当 `minDifference = 0` 时**: - - `minDifference` 等于 `0`,表示 `a[i+1] - a[i] = 0`,即 `a[i+1]` 和 `a[i]` 相等。 - - 为了使 `a[i+1] > a[i]`,我们至少需要进行一次操作,使得 `a[i+1]` 大于 `a[i]`。 - - diff --git "a/problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" "b/problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" deleted file mode 100644 index c7be98d239..0000000000 --- "a/problems/kamacoder/0165.\346\234\200\345\244\247\345\255\227\345\205\270\345\272\217\346\227\240\351\207\215\345\244\215\344\270\262.md" +++ /dev/null @@ -1,82 +0,0 @@ - -# 最大字典序无重复串 - -[题目链接](https://kamacoder.com/problempage.php?pid=1243) - - -## 解题思路 - -贪心思路 - -为了保证字典序最大,我们优先放置字母 `b`,然后再放置字母 `a`。在放置字符时,我们还需注意不能超过连续 `k` 次相同字符: - -- 如果当前已经连续放置了 `k` 次相同字符,必须切换到另一个字符。 -- 每次放置字符后,相应的字符数量减少,同时更新当前字符的连续计数。 - -实现步骤: - -- **初始化**:根据输入的 `x`, `y`, `k` 值,检查是否有可能构造出满足条件的字符串。初始化结果字符串的大小,并设置初始计数器。 -- **循环放置字符**: - - 优先放置字符 `b`,如果 `b` 的数量已经足够,或者已经放置了 `k` 次字符 `b`,则放置字符 `a`。 - - 如果已经放置了 `k` 次相同字符,则强制切换到另一个字符。 - -C++代码如下: - -```CPP -#include -#include -using namespace std; - -int main() { - int countA, countB, maxRepeat; - cin >> countA >> countB >> maxRepeat; - - // 检查是否有可能生成满足条件的字符串 - if (countA > (countB + 1) * maxRepeat || countB > (countA + 1) * maxRepeat) { - cout << -1 << endl; - return 0; - } - - string result(countA + countB, ' '); // 预先分配字符串大小 - int currentA = 0, currentB = 0; // 当前连续 'a' 和 'b' 的计数 - int pos = 0; // 当前填充位置 - - while (countA > 0 || countB > 0) { - // 当可以继续添加 'a' 或 'b' 且没有超过最大连续限制时 - if (currentA < maxRepeat && currentB < maxRepeat) { - if (countA <= countB * maxRepeat) { - result[pos++] = 'b'; - countB--; - currentB++; - currentA = 0; - } else { - result[pos++] = 'a'; - countA--; - currentA++; - currentB = 0; - } - } - - // 当当前字符达到最大连续限制时,切换到另一个字符 - if (currentA == maxRepeat || currentB == maxRepeat) { - if (result[pos - 1] == 'a') { - result[pos++] = 'b'; - countB--; - currentB = 1; - currentA = 0; - } else { - result[pos++] = 'a'; - countA--; - currentA = 1; - currentB = 0; - } - } - } - - cout << result << endl; - return 0; -} - -``` - -时间复杂度:O(n) From de01d2e6fa5785b34da74505af2d480eb8cd5226 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sat, 14 Sep 2024 12:03:01 +0800 Subject: [PATCH 1307/1533] Update --- ...17\347\272\242\344\271\260\350\215\257.md" | 48 ------ ...16\347\211\210\346\226\271\346\241\210.md" | 154 ------------------ 2 files changed, 202 deletions(-) delete mode 100644 "problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" delete mode 100644 "problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" diff --git "a/problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" "b/problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" deleted file mode 100644 index 2b144fa5c5..0000000000 --- "a/problems/kamacoder/0133.\345\260\217\347\272\242\344\271\260\350\215\257.md" +++ /dev/null @@ -1,48 +0,0 @@ - -# 133. 小红买药 - -[题目链接](https://kamacoder.com/problempage.php?pid=1210) - -本题是一道直观的模拟题,但也并不简单,很多情况容易漏了,笔试现场可能要多错几次 才能把情况都想到。 - -主要是三个情况: - -* 小红没症状,药有副作用,统计加一,同时要给小红标记上症状 -* 小红有症状,药治不了,同时也没副症状 ,这时也要统计加一 -* 小红有症状,药可以治,给小红取消症状标记 - - -```CPP -#include -#include -using namespace std; -int main() { - int n, m, q, u; - cin >> n; - string s; - cin >> s; - cin >> m; - vector a(m + 1); // 因为后面u是从1开始的 - vector b(m + 1); - for (int i = 1; i <= m; i++) { - cin >> a[i] >> b[i]; - } - cin >> q; - while (q--) { - cin >> u; - int num = 0; - for (int i = 0; i < n; i++) { - // s 没症状,但b给了副作用,统计num的同时,要给s标记上症状 - if (s[i] == '0' && b[u][i] == '1') { - num ++; - s[i] = '1'; - } - // s 有症状,但 a治不了,b也没副症状 - else if (s[i] == '1' && a[u][i] == '0' && a[u][i] == '0') num++; - // s 有症状,a 可以治 - else if (s[i] == '1' && a[u][i] == '1') s[i] = '0'; - } - cout << num << endl; - } -} -``` diff --git "a/problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" "b/problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" deleted file mode 100644 index 6b82b74b6f..0000000000 --- "a/problems/kamacoder/0162.\345\260\217\347\272\242\347\232\204\347\254\25416\347\211\210\346\226\271\346\241\210.md" +++ /dev/null @@ -1,154 +0,0 @@ - -# 小红的第16版方案 - -[题目链接](https://kamacoder.com/problempage.php?pid=1240) - -暴力解法: (数据量已经出最大了,C++能过,java、python、go都过不了) - -```CPP -#include -using namespace std; -int main() { - int n, m; - int l, r; - cin >> n >> m; - vector a(n + 1); - vector angry(n + 1); - for (int i = 1; i <= n; i++) cin >> a[i]; - for (int i = 1; i <= m; i++) { - cin >> l >> r; - for (int j = l; j <= r; j++) { - angry[j]++; - if (angry[j] > a[j]) { - cout << i - 1 << endl; - return 0; - } - } - } - cout << m << endl; - return 0; -} -``` - -使用 差分数组,代码如下: - - -```CPP -#include -#include -using namespace std; - -int main() { - int n, m; - cin >> n >> m; - - vector a(n + 1); - for (int i = 1; i <= n; ++i) { - cin >> a[i]; - } - - vector diff(n + 1, 0); // 差分数组,多一个元素用于处理边界情况 - - int l, r; - for (int i = 1; i <= m; ++i) { - cin >> l >> r; - diff[l]++; - if (r + 1 <= n) diff[r + 1]--; - } - - int current_anger = 0; // 当前的愤怒值 - for (int i = 1; i <= n; ++i) { - current_anger += diff[i]; // 计算差分数组的前缀和,得到最终的愤怒值 - if (current_anger > a[i]) { - cout << i - 1 << endl; // 如果当前的愤怒值超过阈值,输出最后一个没有问题的方案编号 - return 0; - } - } - - cout << m << endl; // 如果所有修改完成后都没有超过阈值,返回最后一个方案的编号 - return 0; -} -``` - -过不了,因为差分数组只能知道是哪个人超过了阈值,不能知道是第几次修改超过的 - -最后 优化思路: - -* 差分数组(Difference Array):依然使用差分数组来处理区间更新。 -* 二分查找:通过二分查找来确定最早发生愤怒值超出阈值的操作,而不是逐次模拟每一次修改。 - -步骤: - -* 创建一个差分数组 diff 用于处理区间增加操作。 -* 在 [1, m] 的范围内进行二分查找,确定导致某个人愤怒值超过阈值的最早的修改次数。 -* 对每个二分查找的中间值 mid,我们累积应用前 mid 次操作,然后检查是否有任何人的愤怒值超过了阈值。 -* 如果 mid 之前没有超标,则继续向右查找;否则向左缩小范围。 -* 在二分查找完成后,输出找到的第一个导致愤怒值超标的操作次数。 - -```CPP -#include -#include -#include - -using namespace std; - -bool isValid(const vector& a, const vector& diff, int n, int m) { - vector anger(n + 1, 0); - int current_anger = 0; - for (int i = 1; i <= n; ++i) { - current_anger += diff[i]; - if (current_anger > a[i]) { - return false; // 超出愤怒阈值 - } - } - return true; // 没有任何人超出愤怒阈值 -} - -int main() { - int n, m; - cin >> n >> m; - - vector a(n + 1); // 愤怒阈值数组 - for (int i = 1; i <= n; ++i) { - cin >> a[i]; - } - - vector> operations(m + 1); // 保存每次操作的区间 - for (int i = 1; i <= m; ++i) { - int l, r; - cin >> l >> r; - operations[i] = {l, r}; - } - - int left = 1, right = m, result = m; - - while (left <= right) { - int mid = left + (right - left) / 2; - - // 构建差分数组,只考虑前 mid 次操作 - vector diff(n + 2, 0); - for (int i = 1; i <= mid; ++i) { - int l = operations[i].first; - int r = operations[i].second; - diff[l]++; - if (r + 1 <= n) { - diff[r + 1]--; - } - } - - if (isValid(a, diff, n, mid)) { - left = mid + 1; // 如果在mid次操作后没有超标,继续向右搜索 - } else { - result = mid - 1; // 如果在mid次操作后超标,向左搜索 - right = mid - 1; - } - } - - cout << result << endl; - return 0; -} - -``` - -* 时间复杂度:O(n + m * log m),其中 n 是成员数量,m 是操作次数。二分查找的时间复杂度为 O(log m),每次二分查找中通过差分数组检查愤怒值的复杂度为 O(n)。 -* 空间复杂度:O(n + m),主要用于存储差分数组和操作数组。 From 51f6ab5a54f7e6c3ee0518e2b1fd093b784d434a Mon Sep 17 00:00:00 2001 From: poivre Date: Wed, 18 Sep 2024 00:19:21 +0800 Subject: [PATCH 1308/1533] =?UTF-8?q?docs:=20=E6=9B=B4=E6=AD=A30459?= =?UTF-8?q?=E7=9A=84=E6=96=87=E6=9C=AC=E7=9A=84=E8=AF=AD=E5=8F=A5=E9=99=88?= =?UTF-8?q?=E8=BF=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 254d921dbd..41c2ea2d35 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -163,7 +163,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 如果一个字符串s是由重复子串组成,那么 最长相等前后缀不包含的子串一定是字符串s的最小重复子串。 -证明: 如果s 是有是有最小重复子串p组成。 +证明: 如果s 是由最小重复子串p组成。 即 s = n * p @@ -884,3 +884,4 @@ public int[] GetNext(string s) + From ee41f4df2203884a884a108bc9cc2662f4fdc823 Mon Sep 17 00:00:00 2001 From: poivre Date: Wed, 18 Sep 2024 00:27:25 +0800 Subject: [PATCH 1309/1533] =?UTF-8?q?Revert=20"docs:=20=E6=9B=B4=E6=AD=A30?= =?UTF-8?q?459=E7=9A=84=E6=96=87=E6=9C=AC=E7=9A=84=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E9=99=88=E8=BF=B0=E3=80=82"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 51f6ab5a54f7e6c3ee0518e2b1fd093b784d434a. --- ...232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 41c2ea2d35..254d921dbd 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -163,7 +163,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 如果一个字符串s是由重复子串组成,那么 最长相等前后缀不包含的子串一定是字符串s的最小重复子串。 -证明: 如果s 是由最小重复子串p组成。 +证明: 如果s 是有是有最小重复子串p组成。 即 s = n * p @@ -884,4 +884,3 @@ public int[] GetNext(string s) - From 69a413c3e65ce07144bfdb8199ee0b2d47080e33 Mon Sep 17 00:00:00 2001 From: poivre Date: Wed, 18 Sep 2024 00:28:17 +0800 Subject: [PATCH 1310/1533] =?UTF-8?q?docs:=20=E6=9B=B4=E6=AD=A30459?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=B8=AD=E6=96=87=E6=9C=AC=E7=9A=84=E9=99=88?= =?UTF-8?q?=E8=BF=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 254d921dbd..dbf32e6482 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -163,7 +163,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 如果一个字符串s是由重复子串组成,那么 最长相等前后缀不包含的子串一定是字符串s的最小重复子串。 -证明: 如果s 是有是有最小重复子串p组成。 +证明: 如果s 是由最小重复子串p组成。 即 s = n * p @@ -179,7 +179,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110841.png) -这里有录友就想:如果字符串s 是有是有最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样: +这里有录友就想:如果字符串s 是由最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114348.png) @@ -884,3 +884,4 @@ public int[] GetNext(string s) + From 75f587507dea0f21ec667a38c049b9324b982f9d Mon Sep 17 00:00:00 2001 From: DraculaJay <113758447+DraculaJay@users.noreply.github.com> Date: Sat, 21 Sep 2024 04:52:40 +0800 Subject: [PATCH 1311/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86python?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E5=89=AA=E6=9E=9D=E6=93=8D=E4=BD=9C=EF=BC=8C?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E5=89=A9=E4=BD=99=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E7=9A=84=E5=88=A4=E6=96=AD=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=89=AA=E6=9E=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\216\237IP\345\234\260\345\235\200.md" | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index d1300a3933..eb81f4b662 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -467,9 +467,37 @@ class Solution: num = int(s[start:end+1]) return 0 <= num <= 255 +回溯(版本三) - - +```python +class Solution: + def restoreIpAddresses(self, s: str) -> List[str]: + result = [] + self.backtracking(s, 0, [], result) + return result + + def backtracking(self, s, startIndex, path, result): + if startIndex == len(s): + result.append('.'.join(path[:])) + return + + for i in range(startIndex, min(startIndex+3, len(s))): + # 如果 i 往后遍历了,并且当前地址的第一个元素是 0 ,就直接退出 + if i > startIndex and s[startIndex] == '0': + break + # 比如 s 长度为 5,当前遍历到 i = 3 这个元素 + # 因为还没有执行任何操作,所以此时剩下的元素数量就是 5 - 3 = 2 ,即包括当前的 i 本身 + # path 里面是当前包含的子串,所以有几个元素就表示储存了几个地址 + # 所以 (4 - len(path)) * 3 表示当前路径至多能存放的元素个数 + # 4 - len(path) 表示至少要存放的元素个数 + if (4 - len(path)) * 3 < len(s) - i or 4 - len(path) > len(s) - i: + break + if i - startIndex == 2: + if not int(s[startIndex:i+1]) <= 255: + break + path.append(s[startIndex:i+1]) + self.backtracking(s, i+1, path, result) + path.pop() ``` ### Go From b3c0d03885496324d5cea3427ccd6cd633368e0a Mon Sep 17 00:00:00 2001 From: Runze Liao <72693579+tony8888lrz@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:56:31 -0700 Subject: [PATCH 1312/1533] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md=20Java=E7=89=88=E6=9C=AC=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=B4=AA=E5=BF=83=E7=AE=97=E6=B3=95=EF=BC=8C=E6=B8=85?= =?UTF-8?q?=E6=99=B0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\225\260\346\213\206\345\210\206.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 7295627fde..3ba23e522c 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -243,6 +243,29 @@ class Solution { } } ``` +贪心 +```Java +class Solution { + public int integerBreak(int n) { + // with 贪心 + // 通过数学原理拆出更多的3乘积越大,则 + /** + @Param: an int, the integer we need to break. + @Return: an int, the maximum integer after breaking + @Method: Using math principle to solve this problem + @Time complexity: O(1) + **/ + if(n == 2) return 1; + if(n == 3) return 2; + int result = 1; + while(n > 4) { + n-=3; + result *=3; + } + return result*n; + } +} +``` ### Python 动态规划(版本一) From fed25bf0125118e4b985fb58d122430b3141b919 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Sat, 21 Sep 2024 14:36:45 +0800 Subject: [PATCH 1313/1533] =?UTF-8?q?feat:=20=E5=9C=96=E8=AB=96100?= =?UTF-8?q?=E5=B2=9B=E5=B1=BF=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9Ejs=E6=B7=B1=E6=90=9C=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index ea62edc25c..51bfc57fd1 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -480,7 +480,84 @@ const bfs = (graph, visited, x, y) => { })() ``` +```javascript + +// 深搜版 + +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph // 地图 +let N, M // 地图大小 +let visited // 访问过的节点 +let result = 0 // 最大岛屿面积 +let count = 0 // 岛屿内节点数 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向 + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} +/** + * @description: 从(x, y)开始深度优先遍历 + * @param {*} graph 地图 + * @param {*} visited 访问过的节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const dfs = (graph, visited, x, y) => { + for (let i = 0; i < 4; i++) { + let nextx = x + dir[i][0] + let nexty = y + dir[i][1] + if(nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if(!visited[nextx][nexty] && graph[nextx][nexty] === 1){ + count++ + visited[nextx][nexty] = true + dfs(graph, visited, nextx, nexty) + } + } +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 统计最大岛屿面积 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (!visited[i][j] && graph[i][j] === 1) { //遇到没有访问过的陆地 + // 重新计算面积 + count = 1 + visited[i][j] = true + + // 深度优先遍历,统计岛屿内节点数,并将岛屿标记为已访问 + dfs(graph, visited, i, j) + + // 更新最大岛屿面积 + result = Math.max(result, count) + } + } + } + console.log(result); +})() +``` ### TypeScript From 99e85bfea1804fcc87458d858fed44d76c7996f2 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Sat, 21 Sep 2024 16:05:46 +0800 Subject: [PATCH 1314/1533] =?UTF-8?q?feat:=20=E5=9C=96=E8=AB=96101?= =?UTF-8?q?=E5=AD=A4=E5=B3=B6=E6=80=BB=E9=9D=A2=E7=A7=AF=20=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9Epython=E6=B7=B1=E6=90=9C=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\346\200\273\351\235\242\347\247\257.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 006484defb..26c92c07a6 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -307,6 +307,71 @@ for i in range(n): print(count) ``` + +```python +direction = [[1, 0], [-1, 0], [0, 1], [0, -1]] +result = 0 + +# 深度搜尋 +def dfs(grid, y, x): + grid[y][x] = 0 + global result + result += 1 + + for i, j in direction: + next_x = x + j + next_y = y + i + if (next_x < 0 or next_y < 0 or + next_x >= len(grid[0]) or next_y >= len(grid) + ): + continue + if grid[next_y][next_x] == 1 and not visited[next_y][next_x]: + visited[next_y][next_x] = True + dfs(grid, next_y, next_x) + + +# 讀取輸入值 +n, m = map(int, input().split()) +grid = [] +visited = [[False] * m for _ in range(n)] + +for i in range(n): + grid.append(list(map(int, input().split()))) + +# 處理邊界 +for j in range(m): + # 上邊界 + if grid[0][j] == 1 and not visited[0][j]: + visited[0][j] = True + dfs(grid, 0, j) + # 下邊界 + if grid[n - 1][j] == 1 and not visited[n - 1][j]: + visited[n - 1][j] = True + dfs(grid, n - 1, j) + +for i in range(n): + # 左邊界 + if grid[i][0] == 1 and not visited[i][0]: + visited[i][0] = True + dfs(grid, i, 0) + # 右邊界 + if grid[i][m - 1] == 1 and not visited[i][m - 1]: + visited[i][m - 1] = True + dfs(grid, i, m - 1) + +# 計算孤島總面積 +result = 0 # 初始化,避免使用到處理邊界時所產生的累加值 + +for i in range(n): + for j in range(m): + if grid[i][j] == 1 and not visited[i][j]: + visited[i][j] = True + dfs(grid, i, j) + +# 輸出孤島的總面積 +print(result) +``` + ### Go ``` go From 848fbd4cd5c868839c75c637ef5ea9057ab301d7 Mon Sep 17 00:00:00 2001 From: Zhihan Li <54661071+zhihali@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:22:04 +0100 Subject: [PATCH 1315/1533] =?UTF-8?q?Update=200053.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\247\345\255\220\345\272\217\345\222\214.md" | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 551c39bfac..1c7ff0cd55 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -214,6 +214,7 @@ class Solution: return result ``` +贪心法 ```python class Solution: def maxSubArray(self, nums): @@ -226,8 +227,18 @@ class Solution: if count <= 0: # 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和 count = 0 return result - - +``` +动态规划 +```python +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + dp = [0] * len(nums) + dp[0] = nums[0] + res = nums[0] + for i in range(1, len(nums)): + dp[i] = max(dp[i-1] + nums[i], nums[i]) + res = max(res, dp[i]) + return res ``` ### Go 贪心法 From 2841f59212a4f8399d53007eb01130172ce59814 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Tue, 24 Sep 2024 14:41:38 +0800 Subject: [PATCH 1316/1533] =?UTF-8?q?feat:=20108.=20=E5=86=97=E4=BD=99?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=96=B0=E5=A2=9Epython=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\344\275\231\350\277\236\346\216\245.md" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 2c13378291..18a86ad6d7 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -178,6 +178,45 @@ int main() { ### Python +```python +father = list() + +def find(u): + if u == father[u]: + return u + else: + father[u] = find(father[u]) + return father[u] + +def is_same(u, v): + u = find(u) + v = find(v) + return u == v + +def join(u, v): + u = find(u) + v = find(v) + if u != v: + father[u] = v + +if __name__ == "__main__": + # 輸入 + n = int(input()) + for i in range(n + 1): + father.append(i) + # 尋找冗余邊 + result = None + for i in range(n): + s, t = map(int, input().split()) + if is_same(s, t): + result = str(s) + ' ' + str(t) + else: + join(s, t) + + # 輸出 + print(result) +``` + ### Go ### Rust From 5c67ef4acd04b228b2f24f32a89917d9abc478b0 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Tue, 24 Sep 2024 16:13:28 +0800 Subject: [PATCH 1317/1533] =?UTF-8?q?feat:=20109.=20=E5=86=97=E4=BD=99?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5II=E6=96=B0=E5=A2=9Epython=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\231\350\277\236\346\216\245II.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index bd707bf6ae..2bd4eac6e2 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -351,6 +351,92 @@ public class Main { ``` ### Python +```python +from collections import defaultdict + +father = list() + + +def find(u): + if u == father[u]: + return u + else: + father[u] = find(father[u]) + return father[u] + + +def is_same(u, v): + u = find(u) + v = find(v) + return u == v + + +def join(u, v): + u = find(u) + v = find(v) + if u != v: + father[u] = v + + +def is_tree_after_remove_edge(edges, edge, n): + # 初始化并查集 + global father + father = [i for i in range(n + 1)] + + for i in range(len(edges)): + if i == edge: + continue + s, t = edges[i] + if is_same(s, t): # 成環,即不是有向樹 + return False + else: # 將s,t放入集合中 + join(s, t) + return True + + +def get_remove_edge(edges): + # 初始化并查集 + global father + father = [i for i in range(n + 1)] + + for s, t in edges: + if is_same(s, t): + print(s, t) + return + else: + join(s, t) + + +if __name__ == "__main__": + # 輸入 + n = int(input()) + edges = list() + in_degree = defaultdict(int) + + for i in range(n): + s, t = map(int, input().split()) + in_degree[t] += 1 + edges.append([s, t]) + + # 尋找入度為2的邊,並紀錄其下標(index) + vec = list() + for i in range(n - 1, -1, -1): + if in_degree[edges[i][1]] == 2: + vec.append(i) + + # 輸出 + if len(vec) > 0: + # 情況一:刪除輸出順序靠後的邊 + if is_tree_after_remove_edge(edges, vec[0], n): + print(edges[vec[0]][0], edges[vec[0]][1]) + # 情況二:只能刪除特定的邊 + else: + print(edges[vec[1]][0], edges[vec[1]][1]) + else: + # 情況三: 原圖有環 + get_remove_edge(edges) +``` + ### Go ### Rust From 183fe44ae0d0569f39ab525b57877d844e9d3823 Mon Sep 17 00:00:00 2001 From: Zhihan Li <54661071+zhihali@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:09:20 +0100 Subject: [PATCH 1318/1533] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9?= =?UTF-8?q?=E7=AB=99.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0134.\345\212\240\346\262\271\347\253\231.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index 7ac9f0f933..5cf50b3e56 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -158,7 +158,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i 如果 curSum<0 说明 区间和1 + 区间和2 < 0, 那么 假设从上图中的位置开始计数curSum不会小于0的话,就是 区间和2>0。 -区间和1 + 区间和2 < 0 同时 区间和2>0,只能说明区间和1 < 0, 那么就会从假设的箭头初就开始从新选择其实位置了。 +区间和1 + 区间和2 < 0 同时 区间和2>0,只能说明区间和1 < 0, 那么就会从假设的箭头初就开始从新选择起始位置了。 **那么局部最优:当前累加rest[i]的和curSum一旦小于0,起始位置至少要是i+1,因为从i之前开始一定不行。全局最优:找到可以跑一圈的起始位置**。 From 779ff9dd08a4b18d0aa8e077fcdab5e821662c08 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Wed, 25 Sep 2024 12:35:34 +0800 Subject: [PATCH 1319/1533] =?UTF-8?q?feat:=2053.=20=E5=AF=BB=E5=AE=9D?= =?UTF-8?q?=E6=96=B0=E5=A2=9Ejs=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0053.\345\257\273\345\256\235-prim.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index c71624b55a..a8dad4cbfc 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -693,6 +693,55 @@ if __name__ == "__main__": ### Rust ### Javascript +```js +function prim(v, edges) { + const grid = Array.from({ length: v + 1 }, () => new Array(v + 1).fill(10001)); // Fixed grid initialization + const minDist = new Array(v + 1).fill(10001) + const isInTree = new Array(v + 1).fill(false) + // 建構鄰接矩陣 + for(const [v1, v2, w] of edges) { + grid[v1][v2] = w + grid[v2][v1] = w + } + // prim 演算法 + for (let i = 1 ; i < v ; i++) { + let cur = -1 + let tempMinDist = Number.MAX_VALUE + // 1. 尋找距離生成樹最近的節點 + for (let j = 1 ; j < v + 1 ; j++) { + if (!isInTree[j] && minDist[j] < tempMinDist) { + tempMinDist = minDist[j] + cur = j + } + } + // 2. 將節點放入生成樹 + isInTree[cur] = true + // 3. 更新非生成樹節點與生成樹的最短距離 + for (let j = 1 ; j < v + 1 ; j++) { + if (!isInTree[j] && grid[cur][j] < minDist[j]) { + minDist[j] = grid[cur][j] + } + } + } + console.log(minDist.slice(2).reduce((acc, cur) => acc + cur, 0)) +} + + +async function main() { + const rl = require('readline').createInterface({ input: process.stdin }) + const iter = rl[Symbol.asyncIterator]() + const readline = async () => (await iter.next()).value + const [v, e] = (await readline()).split(" ").map(Number) + const edges = [] + for (let i = 0 ; i < e ; i++) { + edges.push((await readline()).split(" ").map(Number)) + } + prim(v, edges) +} + + +main() +``` ### TypeScript From 9e66f2232363c66f1c466329b518fc2d8c6525c5 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Wed, 25 Sep 2024 14:57:21 +0800 Subject: [PATCH 1320/1533] =?UTF-8?q?feat:=2053.=20=E5=AF=BB=E5=AE=9D?= =?UTF-8?q?=E6=96=B0=E5=A2=9Ekruskal=20js=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0053.\345\257\273\345\256\235-Kruskal.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index cb24fd17f3..6a22798551 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -549,6 +549,62 @@ if __name__ == "__main__": ### Javascript +```js +function kruskal(v, edges) { + const father = Array.from({ length: v + 1 }, (_, i) => i) + + function find(u){ + if (u === father[u]) { + return u + } else { + father[u] = find(father[u]) + return father[u] + } + + } + + function isSame(u, v) { + let s = find(u) + let t = find(v) + return s === t + } + + function join(u, v) { + let s = find(u) + let t = find(v) + if (s !== t) { + father[s] = t + } + } + + edges.sort((a, b) => a[2] - b[2]) + let result = 0 + for (const [v1, v2, w] of edges) { + if (!isSame(v1, v2)) { + result += w + join(v1 ,v2) + } + } + console.log(result) +} + + +async function main() { + const rl = require('readline').createInterface({ input: process.stdin }) + const iter = rl[Symbol.asyncIterator]() + const readline = async () => (await iter.next()).value + const [v, e] = (await readline()).split(" ").map(Number) + const edges = [] + for (let i = 0 ; i < e ; i++) { + edges.push((await readline()).split(" ").map(Number)) + } + kruskal(v, edges) +} + + +main() +``` + ### TypeScript ### PhP From 13a4234fce501cf300974d4f28f7728c0f91e64e Mon Sep 17 00:00:00 2001 From: xiaodaoshou <1035011225@qq.com> Date: Wed, 25 Sep 2024 20:50:05 +0800 Subject: [PATCH 1321/1533] =?UTF-8?q?0005=E6=9C=80=E9=95=BF=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E5=AD=90=E4=B8=B2=20Manacher=E7=AE=97=E6=B3=95C++?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\226\207\345\255\220\344\270\262.md" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index a13daf1e2f..b3d3b93896 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -256,7 +256,60 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(1) +### Manacher 算法 +Manacher 算法的关键在于高效利用回文的对称性,通过插入分隔符和维护中心、边界等信息,在线性时间内找到最长回文子串。这种方法避免了重复计算,是处理回文问题的最优解。 + +```c++ +//Manacher 算法 +class Solution { +public: + string longestPalindrome(string s) { + // 预处理字符串,在每个字符之间插入 '#' + string t = "#"; + for (char c : s) { + t += c; // 添加字符 + t += '#';// 添加分隔符 + } + int n = t.size();// 新字符串的长度 + vector p(n, 0);// p[i] 表示以 t[i] 为中心的回文半径 + int center = 0, right = 0;// 当前回文的中心和右边界 + + + // 遍历预处理后的字符串 + for (int i = 0; i < n; i++) { + // 如果当前索引在右边界内,利用对称性初始化 p[i] + if (i < right) { + p[i] = min(right - i, p[2 * center - i]); + } + // 尝试扩展回文 + while (i - p[i] - 1 >= 0 && i + p[i] + 1 < n && t[i - p[i] - 1] == t[i + p[i] + 1]) { + p[i]++;// 增加回文半径 + } + // 如果当前回文扩展超出右边界,更新中心和右边界 + if (i + p[i] > right) { + center = i;// 更新中心 + right = i + p[i];// 更新右边界 + } + } + // 找到最大回文半径和对应的中心 + int maxLen = 0, centerIndex = 0; + for (int i = 0; i < n; i++) { + if (p[i] > maxLen) { + maxLen = p[i];// 更新最大回文长度 + centerIndex = i;// 更新中心索引 + } + } + // 计算原字符串中回文子串的起始位置并返回 + return s.substr((centerIndex - maxLen) / 2, maxLen); + } +}; +``` + + + +* 时间复杂度:O(n) +* 空间复杂度:O(n) ## 其他语言版本 @@ -682,3 +735,4 @@ public class Solution { + From 1b08a8e5b5b1cb83ca55f2d42391791387f293dd Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Thu, 26 Sep 2024 12:56:42 +0800 Subject: [PATCH 1322/1533] =?UTF-8?q?feat:=2047.=20=E5=8F=82=E5=8A=A0?= =?UTF-8?q?=E7=A7=91=E5=AD=A6=E5=A4=A7=E4=BC=9A=E6=96=B0=E5=A2=9Ejs?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...74\232dijkstra\346\234\264\347\264\240.md" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index c0a490b343..465ad16dfc 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -869,6 +869,65 @@ if __name__ == "__main__": ### Javascript +```js +function dijkstra(grid, start, end) { + const visited = Array.from({length: end + 1}, () => false) + const minDist = Array.from({length: end + 1}, () => Number.MAX_VALUE) + minDist[start] = 0 + + for (let i = 1 ; i < end + 1 ; i++) { + let cur = -1 + let tempMinDist = Number.MAX_VALUE + // 1. 找尋與起始點距離最近且未被訪的節點 + for (let j = 1 ; j < end + 1 ; j++) { + if (!visited[j] && minDist[j] < tempMinDist) { + cur = j + tempMinDist = minDist[j] + } + } + if (cur === -1) break; + + // 2. 更新節點狀態為已拜訪 + visited[cur] = true + + // 3. 更新未拜訪節點與起始點的最短距離 + for (let j = 1 ; j < end + 1 ; j++) { + if(!visited[j] && grid[cur][j] != Number.MAX_VALUE + && grid[cur][j] + minDist[cur] < minDist[j] + ) { + minDist[j] = grid[cur][j] + minDist[cur] + } + } + } + + return minDist[end] === Number.MAX_VALUE ? -1 : minDist[end] +} + + +async function main() { + // 輸入 + const rl = require('readline').createInterface({ input: process.stdin }) + const iter = rl[Symbol.asyncIterator]() + const readline = async () => (await iter.next()).value + const [n, m] = (await readline()).split(" ").map(Number) + const grid = Array.from({length: n + 1}, + () => Array.from({length:n + 1}, () => Number.MAX_VALUE)) + for (let i = 0 ; i < m ; i++) { + const [s, e, w] = (await readline()).split(" ").map(Number) + grid[s][e] = w + } + + // dijkstra + const result = dijkstra(grid, 1, n) + + // 輸出 + console.log(result) +} + + +main() +``` + ### TypeScript ### PhP From 4c37cedc435eca094a98d9c201b43663605264d7 Mon Sep 17 00:00:00 2001 From: MAX <61301100+miaoxu404@users.noreply.github.com> Date: Thu, 26 Sep 2024 15:07:38 +0800 Subject: [PATCH 1323/1533] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91?= =?UTF-8?q?=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The term maxLen has ambiguity and does not align clearly with both the code and the problem statement. I propose changing it to maxDepth for improved clarity and consistency. --- ...\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index d69ceb6f51..c7446726f8 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -55,7 +55,7 @@ 参数必须有要遍历的树的根节点,还有就是一个int型的变量用来记录最长深度。 这里就不需要返回值了,所以递归函数的返回类型为void。 -本题还需要类里的两个全局变量,maxLen用来记录最大深度,result记录最大深度最左节点的数值。 +本题还需要类里的两个全局变量,maxDepth用来记录最大深度,result记录最大深度最左节点的数值。 代码如下: From 53a4a17b8c518b6e27166b3b9b60de44fdf9b39a Mon Sep 17 00:00:00 2001 From: Camille <59353274+Camille0512@users.noreply.github.com> Date: Thu, 26 Sep 2024 23:06:45 +0800 Subject: [PATCH 1324/1533] =?UTF-8?q?Update=200099.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8F=E5=B9=BF=E6=90=9C.md:=20Add=20Scala?= =?UTF-8?q?=20script=20for=20the=20counting=20island=20puzzle=20using=20BF?= =?UTF-8?q?S=20algo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\345\271\277\346\220\234.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 3047575391..9d31c92268 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -499,6 +499,55 @@ main(); ### Swift ### Scala +```scala +import scala.collection.mutable.Queue +import util.control.Breaks._ + +// Dev on LeetCode: https://leetcode.cn/problems/number-of-islands/description/ +object Solution { + def numIslands(grid: Array[Array[Char]]): Int = { + val row = grid.length + val col = grid(0).length + val dir = List((-1,0), (0,-1), (1,0), (0,1)) // 四个方向 + var visited = Array.fill(row)(Array.fill(col)(false)) + var counter = 0 + var que = Queue.empty[Tuple2[Int, Int]] + + (0 until row).map{ r => + (0 until col).map{ c => + breakable { + if (!visited(r)(c) && grid(r)(c) == '1') { + que.enqueue((r, c)) + visited(r)(c) // 只要加入队列,立刻标记 + } else break // 不是岛屿不进入queue,也不记录 + + while (!que.isEmpty) { + val cur = que.head + que.dequeue() + val x = cur(0) + val y = cur(1) + dir.map{ d => + val nextX = x + d(0) + val nextY = y + d(1) + breakable { + // 越界就跳过 + if (nextX < 0 || nextX >= row || nextY < 0 || nextY >= col) break + if (!visited(nextX)(nextY) && grid(nextX)(nextY) == '1') { + visited(nextX)(nextY) = true // 只要加入队列,立刻标记 + que.enqueue((nextX, nextY)) + } + } + } + } + counter = counter + 1 // 找完一个岛屿后记录一下 + } + } + } + + counter + } +} +``` ### C# From d0f6653a6f371ee22130116470030bd35b925212 Mon Sep 17 00:00:00 2001 From: MAX <61301100+miaoxu404@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:51:01 +0800 Subject: [PATCH 1325/1533] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit it's important to avoid using reserved keywords as variable names. so i suggest changing "sum" to "targetSum" In order to maintain consistency with the original LeetCode problem, the term "TreeNode" can be replaced with "Optional[TreeNode]". --- ...57\345\276\204\346\200\273\345\222\214.md" | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 6709a2fbd2..e6ccc6ae27 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -564,10 +564,10 @@ class Solution: return False - def hasPathSum(self, root: TreeNode, sum: int) -> bool: + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: if root is None: return False - return self.traversal(root, sum - root.val) + return self.traversal(root, targetSum - root.val) ``` (版本二) 递归 + 精简 @@ -579,12 +579,12 @@ class Solution: # self.left = left # self.right = right class Solution: - def hasPathSum(self, root: TreeNode, sum: int) -> bool: + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: if not root: return False - if not root.left and not root.right and sum == root.val: + if not root.left and not root.right and targetSum == root.val: return True - return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val) + return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val) ``` (版本三) 迭代 @@ -596,7 +596,7 @@ class Solution: # self.left = left # self.right = right class Solution: - def hasPathSum(self, root: TreeNode, sum: int) -> bool: + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: if not root: return False # 此时栈里要放的是pair<节点指针,路径数值> @@ -659,13 +659,13 @@ class Solution: return - def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: self.result.clear() self.path.clear() if not root: return self.result self.path.append(root.val) # 把根节点放进路径 - self.traversal(root, sum - root.val) + self.traversal(root, targetSum - root.val) return self.result ``` @@ -678,7 +678,7 @@ class Solution: # self.left = left # self.right = right class Solution: - def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: result = [] self.traversal(root, targetSum, [], result) @@ -703,7 +703,7 @@ class Solution: # self.left = left # self.right = right class Solution: - def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: if not root: return [] stack = [(root, [root.val])] From 8ff94842609eee048ec78b709a20a82339c3c093 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Fri, 27 Sep 2024 15:20:59 +0800 Subject: [PATCH 1326/1533] =?UTF-8?q?feat:=2094.=20=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93=20I=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9Ejs=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\347\211\251\350\277\220\350\276\223I.md" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 3737fe0149..9021e0feab 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -485,6 +485,45 @@ if __name__ == "__main__": ### Javascript +```js +async function main() { + // 輸入 + const rl = require('readline').createInterface({ input: process.stdin }) + const iter = rl[Symbol.asyncIterator]() + const readline = async () => (await iter.next()).value + const [n, m] = (await readline()).split(" ").map(Number) + const edges = [] + for (let i = 0 ; i < m ; i++) { + edges.push((await readline()).split(" ").map(Number)) + } + const minDist = Array.from({length: n + 1}, () => Number.MAX_VALUE) + // 起始點 + minDist[1] = 0 + + for (let i = 1 ; i < n ; i++) { + let update = false + for (const [src, desc, w] of edges) { + if (minDist[src] !== Number.MAX_VALUE && minDist[src] + w < minDist[desc]) { + minDist[desc] = minDist[src] + w + update = true + } + } + if (!update) { + break; + } + } + + // 輸出 + if (minDist[n] === Number.MAX_VALUE) { + console.log('unconnected') + } else { + console.log(minDist[n]) + } +} + +main() +``` + ### TypeScript ### PhP From c55a05a496fdb75c1f38fe49a060fa3985565342 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Fri, 27 Sep 2024 15:59:22 +0800 Subject: [PATCH 1327/1533] =?UTF-8?q?feat:=20=2094.=20=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93=20I=20SPEA?= =?UTF-8?q?=E6=96=B0=E5=A2=9Ejs=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index b3f42bf800..9ba92599bb 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -464,6 +464,60 @@ if __name__ == "__main__": ### Javascript +```js +async function main() { + // 輸入 + const rl = require('readline').createInterface({ input: process.stdin }) + const iter = rl[Symbol.asyncIterator]() + const readline = async () => (await iter.next()).value + const [n, m] = (await readline()).split(" ").map(Number) + const grid = {} + for (let i = 0 ; i < m ; i++) { + const [src, desc, w] = (await readline()).split(" ").map(Number) + if (grid.hasOwnProperty(src)) { + grid[src].push([desc, w]) + } else { + grid[src] = [[desc, w]] + } + } + const minDist = Array.from({length: n + 1}, () => Number.MAX_VALUE) + + // 起始點 + minDist[1] = 0 + + const q = [1] + const visited = Array.from({length: n + 1}, () => false) + + while (q.length) { + const src = q.shift() + const neighbors = grid[src] + visited[src] = false + if (neighbors) { + for (const [desc, w] of neighbors) { + if (minDist[src] !== Number.MAX_VALUE + && minDist[src] + w < minDist[desc]) { + minDist[desc] = minDist[src] + w + if (!visited[desc]) { + q.push(desc) + visited[desc] = true + } + + } + } + } + } + + // 輸出 + if (minDist[n] === Number.MAX_VALUE) { + console.log('unconnected') + } else { + console.log(minDist[n]) + } +} + +main() +``` + ### TypeScript ### PhP From ecf70c6e57dc5d6e7583f9d362d5420f2d6dc74a Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Fri, 27 Sep 2024 19:43:51 +0800 Subject: [PATCH 1328/1533] =?UTF-8?q?feat:=2096.=20=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93=20III=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0python=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...347\211\251\350\277\220\350\276\223III.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index dacd23d11d..567a1d8724 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -703,6 +703,42 @@ public class Main { ``` ### Python +```python +def main(): + # 輸入 + n, m = map(int, input().split()) + edges = list() + for _ in range(m): + edges.append(list(map(int, input().split() ))) + + start, end, k = map(int, input().split()) + min_dist = [float('inf') for _ in range(n + 1)] + min_dist[start] = 0 + + # 只能經過k個城市,所以從起始點到中間有(k + 1)個邊連接 + # 需要鬆弛(k + 1)次 + + for _ in range(k + 1): + update = False + min_dist_copy = min_dist.copy() + for src, desc, w in edges: + if (min_dist_copy[src] != float('inf') and + min_dist_copy[src] + w < min_dist[desc]): + min_dist[desc] = min_dist_copy[src] + w + update = True + if not update: + break + # 輸出 + if min_dist[end] == float('inf'): + print('unreachable') + else: + print(min_dist[end]) + + + +if __name__ == "__main__": + main() +``` ### Go From b32888260f0cb2f29314cf0c4d7d4dddb1f20f71 Mon Sep 17 00:00:00 2001 From: Ziyang Wen Date: Fri, 27 Sep 2024 21:17:44 +0800 Subject: [PATCH 1329/1533] =?UTF-8?q?0151python=E7=89=88=E6=9C=AC1?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index bf486bdceb..9a0cbea466 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -440,11 +440,10 @@ class Solution { ```Python class Solution: def reverseWords(self, s: str) -> str: - # 删除前后空白 - s = s.strip() # 反转整个字符串 s = s[::-1] # 将字符串拆分为单词,并反转每个单词 + # split()函数能够自动忽略多余的空白字符 s = ' '.join(word[::-1] for word in s.split()) return s @@ -1029,3 +1028,4 @@ public string ReverseWords(string s) { + From 0c29c3ed1edd5fcf5be9a8eb6dcba6fc25fb569b Mon Sep 17 00:00:00 2001 From: Ziyang Wen Date: Fri, 27 Sep 2024 21:23:03 +0800 Subject: [PATCH 1330/1533] =?UTF-8?q?0541.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2II=20=E6=80=9D=E8=B7=AF=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BA=86=E5=A4=9A=E4=BD=99=E7=9A=84=E2=80=9C=E5=9C=A8=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index 3e304fabc0..5e75d3c3f8 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -37,7 +37,7 @@ 因为要找的也就是每2 * k 区间的起点,这样写,程序会高效很多。 -**所以当需要固定规律一段一段去处理字符串的时候,要想想在在for循环的表达式上做做文章。** +**所以当需要固定规律一段一段去处理字符串的时候,要想想在for循环的表达式上做做文章。** 性能如下: @@ -505,3 +505,4 @@ impl Solution { + From 2173dd9e6430d8005df0781b4b05a9171208efc4 Mon Sep 17 00:00:00 2001 From: Ziyang Wen Date: Fri, 27 Sep 2024 21:26:55 +0800 Subject: [PATCH 1331/1533] =?UTF-8?q?0226.Python=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95=E5=B9=BF=E5=BA=A6=E4=BC=98=E5=85=88=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\275\254\344\272\214\345\217\211\346\240\221.md" | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index e501b29878..55dc3cbf83 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -459,11 +459,10 @@ class Solution: queue = collections.deque([root]) while queue: - for i in range(len(queue)): - node = queue.popleft() - node.left, node.right = node.right, node.left - if node.left: queue.append(node.left) - if node.right: queue.append(node.right) + node = queue.popleft() + node.left, node.right = node.right, node.left + if node.left: queue.append(node.left) + if node.right: queue.append(node.right) return root ``` @@ -1033,4 +1032,3 @@ public TreeNode InvertTree(TreeNode root) { - From 9a4dab8f898c175559b121f455707eb82452196f Mon Sep 17 00:00:00 2001 From: Camille <59353274+Camille0512@users.noreply.github.com> Date: Fri, 27 Sep 2024 22:49:01 +0800 Subject: [PATCH 1332/1533] =?UTF-8?q?Update=200099.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8F=E6=B7=B1=E6=90=9C.md=20Add=20Scala?= =?UTF-8?q?=20code=20to=20solve=20counting=20islands=20problem=20on=20Leet?= =?UTF-8?q?Code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\346\267\261\346\220\234.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 6ac7ba3b62..da1c07395b 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -412,6 +412,46 @@ const dfs = (graph, visited, x, y) => { ### Swift ### Scala +```scala +import util.control.Breaks._ + +object Solution { + val dir = List((-1,0), (0,-1), (1,0), (0,1)) // 四个方向 + + def dfs(grid: Array[Array[Char]], visited: Array[Array[Boolean]], row: Int, col: Int): Unit = { + (0 until 4).map { x => + val nextR = row + dir(x)(0) + val nextC = col + dir(x)(1) + breakable { + if(nextR < 0 || nextR >= grid.length || nextC < 0 || nextC >= grid(0).length) break + if (!visited(nextR)(nextC) && grid(nextR)(nextC) == '1') { + visited(nextR)(nextC) = true // 经过就记录 + dfs(grid, visited, nextR, nextC) + } + } + } + } + + def numIslands(grid: Array[Array[Char]]): Int = { + val row = grid.length + val col = grid(0).length + var visited = Array.fill(row)(Array.fill(col)(false)) + var counter = 0 + + (0 until row).map{ r => + (0 until col).map{ c => + if (!visited(r)(c) && grid(r)(c) == '1') { + visited(r)(c) = true // 经过就记录 + dfs(grid, visited, r, c) + counter += 1 + } + } + } + + counter + } +} +``` ### C# From 2f58a7b043eeb19eff88c174366db1777a6fff66 Mon Sep 17 00:00:00 2001 From: DraculaJay <113758447+DraculaJay@users.noreply.github.com> Date: Sat, 28 Sep 2024 04:55:41 +0800 Subject: [PATCH 1333/1533] =?UTF-8?q?0053=20=E6=B7=BB=E5=8A=A0python?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E8=A7=A3=E6=B3=95=E5=92=8C?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E4=BC=98=E5=8C=96=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\345\255\220\345\272\217\345\222\214.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 551c39bfac..507575647a 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -229,6 +229,42 @@ class Solution: ``` + +动态规划 + +```python +class Solution: + def maxSubArray(self, nums): + if not nums: + return 0 + dp = [0] * len(nums) # dp[i]表示包括i之前的最大连续子序列和 + dp[0] = nums[0] + result = dp[0] + for i in range(1, len(nums)): + dp[i] = max(dp[i-1]+nums[i], nums[i]) # 状态转移公式 + if dp[i] > result: + result = dp[i] # result 保存dp[i]的最大值 + return result +``` + +动态规划优化 + +```python +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_sum = float("-inf") # 初始化结果为负无穷大,方便比较取最大值 + current_sum = 0 # 初始化当前连续和 + + for num in nums: + + # 更新当前连续和 + # 如果原本的连续和加上当前数字之后没有当前数字大,说明原本的连续和是负数,那么就直接从当前数字开始重新计算连续和 + current_sum = max(current_sum+num, num) + max_sum = max(max_sum, current_sum) # 更新结果 + + return max_sum +``` + ### Go 贪心法 ```go From 0ea7587f72abd2c45cc711da696d1c580ddafa06 Mon Sep 17 00:00:00 2001 From: DraculaJay <113758447+DraculaJay@users.noreply.github.com> Date: Sat, 28 Sep 2024 05:04:22 +0800 Subject: [PATCH 1334/1533] =?UTF-8?q?0055=20=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E6=B7=BB=E5=8A=A0python=20=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\263\350\267\203\346\270\270\346\210\217.md" | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index 01fd951384..82b433d7e4 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -143,6 +143,23 @@ class Solution: return False ``` +```python +## 基于当前最远可到达位置判断 +class Solution: + def canJump(self, nums: List[int]) -> bool: + far = nums[0] + for i in range(len(nums)): + # 要考虑两个情况 + # 1. i <= far - 表示 当前位置i 可以到达 + # 2. i > far - 表示 当前位置i 无法到达 + if i > far: + return False + far = max(far, nums[i]+i) + # 如果循环正常结束,表示最后一个位置也可以到达,否则会在中途直接退出 + # 关键点在于,要想明白其实列表中的每个位置都是需要验证能否到达的 + return True +``` + ### Go ```go From 754d2d664d848a7f79b7b97347f9fcf27a03749b Mon Sep 17 00:00:00 2001 From: Zhihan Li <54661071+zhihali@users.noreply.github.com> Date: Sun, 29 Sep 2024 23:24:11 +0100 Subject: [PATCH 1335/1533] =?UTF-8?q?Update=200135.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E7=B3=96=E6=9E=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\345\217\221\347\263\226\346\236\234.md" | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index 6805857e8c..29eaa06d20 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -177,21 +177,20 @@ class Solution { ```python class Solution: def candy(self, ratings: List[int]) -> int: - candyVec = [1] * len(ratings) + n = len(ratings) + candies = [1] * n - # 从前向后遍历,处理右侧比左侧评分高的情况 - for i in range(1, len(ratings)): + # Forward pass: handle cases where right rating is higher than left + for i in range(1, n): if ratings[i] > ratings[i - 1]: - candyVec[i] = candyVec[i - 1] + 1 + candies[i] = candies[i - 1] + 1 - # 从后向前遍历,处理左侧比右侧评分高的情况 - for i in range(len(ratings) - 2, -1, -1): + # Backward pass: handle cases where left rating is higher than right + for i in range(n - 2, -1, -1): if ratings[i] > ratings[i + 1]: - candyVec[i] = max(candyVec[i], candyVec[i + 1] + 1) + candies[i] = max(candies[i], candies[i + 1] + 1) - # 统计结果 - result = sum(candyVec) - return result + return sum(candies) ``` From 5fb0a12fa14d826ee37179f6807d3e332ae46d76 Mon Sep 17 00:00:00 2001 From: suinming <0223314338aa@gmail.com> Date: Tue, 1 Oct 2024 17:39:24 +0800 Subject: [PATCH 1336/1533] =?UTF-8?q?feat:=20127.=20=E9=AA=91=E5=A3=AB?= =?UTF-8?q?=E7=9A=84=E6=94=BB=E5=87=BB=E5=A2=9E=E5=8A=A0js=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\232\204\346\224\273\345\207\273astar.md" | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 2d0481ecec..1cdba07de7 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -375,6 +375,131 @@ for _ in range(n): ### Javascript +```js +class MinHeap { + constructor() { + this.val = [] + } + push(val) { + this.val.push(val) + if (this.val.length > 1) { + this.bubbleUp() + } + } + bubbleUp() { + let pi = this.val.length - 1 + let pp = Math.floor((pi - 1) / 2) + while (pi > 0 && this.val[pp][0] > this.val[pi][0]) { + ;[this.val[pi], this.val[pp]] = [this.val[pp], this.val[pi]] + pi = pp + pp = Math.floor((pi - 1) / 2) + } + } + pop() { + if (this.val.length > 1) { + let pp = 0 + let pi = this.val.length - 1 + ;[this.val[pi], this.val[pp]] = [this.val[pp], this.val[pi]] + const min = this.val.pop() + if (this.val.length > 1) { + this.sinkDown(0) + } + return min + } else if (this.val.length == 1) { + return this.val.pop() + } + + } + sinkDown(parentIdx) { + let pp = parentIdx + let plc = pp * 2 + 1 + let prc = pp * 2 + 2 + let pt = pp // temp pointer + if (plc < this.val.length && this.val[pp][0] > this.val[plc][0]) { + pt = plc + } + if (prc < this.val.length && this.val[pt][0] > this.val[prc][0]) { + pt = prc + } + if (pt != pp) { + ;[this.val[pp], this.val[pt]] = [this.val[pt], this.val[pp]] + this.sinkDown(pt) + } + } +} + +const moves = [ + [1, 2], + [2, 1], + [-1, -2], + [-2, -1], + [-1, 2], + [-2, 1], + [1, -2], + [2, -1] +] + +function dist(a, b) { + return ((a[0] - b[0])**2 + (a[1] - b[1])**2)**0.5 +} + +function isValid(x, y) { + return x >= 1 && y >= 1 && x < 1001 && y < 1001 +} + +function bfs(start, end) { + const step = new Map() + step.set(start.join(" "), 0) + const q = new MinHeap() + q.push([dist(start, end), start[0], start[1]]) + + while(q.val.length) { + const [d, x, y] = q.pop() + // if x and y correspond to end position output result + if (x == end[0] && y == end[1]) { + console.log(step.get(end.join(" "))) + break; + } + for (const [dx, dy] of moves) { + const nx = dx + x + const ny = dy + y + if (isValid(nx, ny)) { + const newStep = step.get([x, y].join(" ")) + 1 + const newDist = dist([nx, ny], [...end]) + const s = step.get([nx, ny].join(" ")) ? + step.get([nx, ny]) : + Number.MAX_VALUE + if (newStep < s) { + q.push( + [ + newStep + newDist, + nx, + ny + ] + ) + step.set([nx, ny].join(" "), newStep) + } + } + } + } +} + +async function main() { + const rl = require('readline').createInterface({ input: process.stdin }) + const iter = rl[Symbol.asyncIterator]() + const readline = async () => (await iter.next()).value + const n = Number((await readline())) + + // find min step + for (let i = 0 ; i < n ; i++) { + const [s1, s2, t1, t2] = (await readline()).split(" ").map(Number) + bfs([s1, s2], [t1, t2]) + } +} + +main() +``` + ### TypeScript ### PhP From 07d219ecd1fb75337d6d5722a1b63635e4f19057 Mon Sep 17 00:00:00 2001 From: Yufan Sheng <18829237653@163.com> Date: Fri, 4 Oct 2024 21:05:14 +1000 Subject: [PATCH 1337/1533] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90=E4=B8=AD=E7=9A=84=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 318c3035ce..10fe577144 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -110,7 +110,7 @@ public: ``` * 时间复杂度:O(nlog n),因为有一个快排 -* 空间复杂度:O(1),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间 +* 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间 可以看出代码并不复杂。 From 2b7fc7d1e183ebcce3930b0235f7f646a6ab3871 Mon Sep 17 00:00:00 2001 From: Yufan Sheng <18829237653@163.com> Date: Fri, 4 Oct 2024 21:22:50 +1000 Subject: [PATCH 1338/1533] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=AF=A6=E7=BB=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\210\206\346\260\224\347\220\203.md" | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 10fe577144..2099275df1 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -180,19 +180,25 @@ class Solution: ```python class Solution: # 不改变原数组 def findMinArrowShots(self, points: List[List[int]]) -> int: + if len(points) == 0: + return 0 + points.sort(key = lambda x: x[0]) - sl,sr = points[0][0],points[0][1] + + # points已经按照第一个坐标正序排列,因此只需要设置一个变量,记录右侧坐标(阈值) + # 考虑一个气球范围包含两个不相交气球的情况:气球1: [1, 10], 气球2: [2, 5], 气球3: [6, 10] + curr_min_right = points[0][1] count = 1 + for i in points: - if i[0]>sr: - count+=1 - sl,sr = i[0],i[1] + if i[0] > curr_min_right: + # 当气球左侧大于这个阈值,那么一定就需要在发射一只箭,并且将阈值更新为当前气球的右侧 + count += 1 + curr_min_right = i[1] else: - sl = max(sl,i[0]) - sr = min(sr,i[1]) + # 否则的话,我们只需要求阈值和当前气球的右侧的较小值来更新阈值 + curr_min_right = min(curr_min_right, i[1]) return count - - ``` ### Go ```go From 91137dc297f7a9ca7abb2d59fd9c45afe04f2366 Mon Sep 17 00:00:00 2001 From: Jasonyou <2926593225@qq.com> Date: Thu, 3 Oct 2024 19:39:17 +0800 Subject: [PATCH 1339/1533] =?UTF-8?q?fix:=20Errata=20=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E8=BE=93=E5=87=BA->=E8=BE=93=E5=85=A5=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index db35524ffd..a2f60a90a6 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -277,7 +277,7 @@ ACM格式大家在输出结果的时候,要关注看看格式问题,特别 有录友可能会想,ACM格式就是麻烦,有空格没有空格有什么影响,结果对了不就行了? -ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输出输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。 +ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输入输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。 以上代码中,结果都存在了 result数组里(二维数组,每一行是一个结果),最后将其打印出来。(重点看注释) From 4eef0863a293b7f2cfd36214a3281ae26093022e Mon Sep 17 00:00:00 2001 From: Jasonyou <2926593225@qq.com> Date: Fri, 4 Oct 2024 21:18:01 +0800 Subject: [PATCH 1340/1533] =?UTF-8?q?feat:=200100.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF=20Java=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 123 +++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 51bfc57fd1..871925df1b 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -222,7 +222,128 @@ public: ## 其他语言版本 -### Java +### Java + +```java +import java.util.*; +import java.math.*; + +/** + * DFS版 + */ +public class Main{ + + static final int[][] dir={{0,1},{1,0},{0,-1},{-1,0}}; + static int result=0; + static int count=0; + + public static void main(String[] args){ + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] map = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j]=scanner.nextInt(); + } + } + boolean[][] visited = new boolean[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if(!visited[i][j]&&map[i][j]==1){ + count=0; + dfs(map,visited,i,j); + result= Math.max(count, result); + } + } + } + System.out.println(result); + } + + static void dfs(int[][] map,boolean[][] visited,int x,int y){ + count++; + visited[x][y]=true; + for (int i = 0; i < 4; i++) { + int nextX=x+dir[i][0]; + int nextY=y+dir[i][1]; + //水或者已经访问过的跳过 + if(nextX<0||nextY<0 + ||nextX>=map.length||nextY>=map[0].length + ||visited[nextX][nextY]||map[nextX][nextY]==0)continue; + + dfs(map,visited,nextX,nextY); + } + } +} +``` + +```java +import java.util.*; +import java.math.*; + +/** + * BFS版 + */ +public class Main { + static class Node { + int x; + int y; + + public Node(int x, int y) { + this.x = x; + this.y = y; + } + } + + static final int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + static int result = 0; + static int count = 0; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] map = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j] = scanner.nextInt(); + } + } + boolean[][] visited = new boolean[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && map[i][j] == 1) { + count = 0; + bfs(map, visited, i, j); + result = Math.max(count, result); + } + } + } + System.out.println(result); + } + + static void bfs(int[][] map, boolean[][] visited, int x, int y) { + Queue q = new LinkedList<>(); + q.add(new Node(x, y)); + visited[x][y] = true; + count++; + while (!q.isEmpty()) { + Node node = q.remove(); + for (int i = 0; i < 4; i++) { + int nextX = node.x + dir[i][0]; + int nextY = node.y + dir[i][1]; + if (nextX < 0 || nextY < 0 || nextX >= map.length || nextY >= map[0].length || visited[nextX][nextY] || map[nextX][nextY] == 0) + continue; + q.add(new Node(nextX, nextY)); + visited[nextX][nextY] = true; + count++; + } + } + } +} + +``` + ### Python From fc004742e70956263fbfba9ca48c3b80d6aee845 Mon Sep 17 00:00:00 2001 From: "Ethan.Liu" Date: Sat, 5 Oct 2024 12:26:05 +0800 Subject: [PATCH 1341/1533] =?UTF-8?q?feat:=200977.=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20python3?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E4=B8=89=E6=AD=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\345\271\263\346\226\271.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index effa905541..1a6604c2c2 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -100,6 +100,7 @@ public: ## 其他语言版本 ### Java: + 排序法 ```Java class Solution { @@ -209,6 +210,43 @@ class Solution: return new_list[::-1] ``` +```python3 +(双指针优化版本) 三步优化 + class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + """ + 整体思想:有序数组的绝对值最大值永远在两头,比较两头,平方大的插到新数组的最后 + 优 化:1. 优化所有元素为非正或非负的情况 + 2. 头尾平方的大小比较直接将头尾相加与0进行比较即可 + 3. 新的平方排序数组的插入索引可以用倒序插入实现(针对for循环,while循环不适用) + """ + + # 特殊情况, 元素都非负(优化1) + if nums[0] >= 0: + return [num ** 2 for num in nums] # 按顺序平方即可 + # 最后一个非正,全负有序的 + if nums[-1] <= 0: + return [x ** 2 for x in nums[::-1]] # 倒序平方后的数组 + + # 一般情况, 有正有负 + i = 0 # 原数组头索引 + j = len(nums) - 1 # 原数组尾部索引 + new_nums = [0] * len(nums) # 新建一个等长数组用于保存排序后的结果 + # end_index = len(nums) - 1 # 新的排序数组(是新数组)尾插索引, 每次需要减一(优化3优化了) + + for end_index in range(len(nums)-1, -1, -1): # (优化3,倒序,不用单独创建变量) + # if nums[i] ** 2 >= nums[j] ** 2: + if nums[i] + nums[j] <= 0: # (优化2) + new_nums[end_index] = nums[i] ** 2 + i += 1 + # end_index -= 1 (优化3) + else: + new_nums[end_index] = nums[j] ** 2 + j -= 1 + # end_index -= 1 (优化3) + return new_nums +``` + ### Go: ```Go From 0a33a8d05c1c430bdb56d63a360e2de0d09b6ace Mon Sep 17 00:00:00 2001 From: Nomop <13098939400@163.com> Date: Sat, 5 Oct 2024 15:21:32 +0800 Subject: [PATCH 1342/1533] =?UTF-8?q?Update=200100.=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 100.岛屿的最大面积新增Rust解法 --- ...00\345\244\247\351\235\242\347\247\257.md" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 51bfc57fd1..50e40f3a17 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -389,6 +389,144 @@ func main() { ### Rust +DFS + +``` rust +use std::io; +use std::cmp; + +// 定义四个方向 +const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + +fn dfs(grid: &Vec>, visited: &mut Vec>, x: usize, y: usize, count: &mut i32) { + if visited[x][y] || grid[x][y] == 0 { + return; // 终止条件:已访问或者遇到海水 + } + visited[x][y] = true; // 标记已访问 + *count += 1; + + for &(dx, dy) in DIRECTIONS.iter() { + let new_x = x as i32 + dx; + let new_y = y as i32 + dy; + + // 检查边界条件 + if new_x >= 0 && new_x < grid.len() as i32 && new_y >= 0 && new_y < grid[0].len() as i32 { + dfs(grid, visited, new_x as usize, new_y as usize, count); + } + } +} + +fn main() { + let mut input = String::new(); + + // 读取 n 和 m + io::stdin().read_line(&mut input); + let dims: Vec = input.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); + let (n, m) = (dims[0], dims[1]); + + // 读取 grid + let mut grid = vec![]; + for _ in 0..n { + input.clear(); + io::stdin().read_line(&mut input); + let row: Vec = input.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); + grid.push(row); + } + + // 初始化访问记录 + let mut visited = vec![vec![false; m]; n]; + let mut result = 0; + + // 遍历所有格子 + for i in 0..n { + for j in 0..m { + if !visited[i][j] && grid[i][j] == 1 { + let mut count = 0; + dfs(&grid, &mut visited, i, j, &mut count); + result = cmp::max(result, count); + } + } + } + + // 输出结果 + println!("{}", result); +} + +``` +BFS +```rust +use std::io; +use std::collections::VecDeque; + +// 定义四个方向 +const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + +fn bfs(grid: &Vec>, visited: &mut Vec>, x: usize, y: usize) -> i32 { + let mut count = 0; + let mut queue = VecDeque::new(); + queue.push_back((x, y)); + visited[x][y] = true; // 标记已访问 + + while let Some((cur_x, cur_y)) = queue.pop_front() { + count += 1; // 增加计数 + + for &(dx, dy) in DIRECTIONS.iter() { + let new_x = cur_x as i32 + dx; + let new_y = cur_y as i32 + dy; + + // 检查边界条件 + if new_x >= 0 && new_x < grid.len() as i32 && new_y >= 0 && new_y < grid[0].len() as i32 { + let new_x_usize = new_x as usize; + let new_y_usize = new_y as usize; + + // 如果未访问且是陆地,加入队列 + if !visited[new_x_usize][new_y_usize] && grid[new_x_usize][new_y_usize] == 1 { + visited[new_x_usize][new_y_usize] = true; // 标记已访问 + queue.push_back((new_x_usize, new_y_usize)); + } + } + } + } + + count +} + +fn main() { + let mut input = String::new(); + + // 读取 n 和 m + io::stdin().read_line(&mut input).expect("Failed to read line"); + let dims: Vec = input.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); + let (n, m) = (dims[0], dims[1]); + + // 读取 grid + let mut grid = vec![]; + for _ in 0..n { + input.clear(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + let row: Vec = input.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); + grid.push(row); + } + + // 初始化访问记录 + let mut visited = vec![vec![false; m]; n]; + let mut result = 0; + + // 遍历所有格子 + for i in 0..n { + for j in 0..m { + if !visited[i][j] && grid[i][j] == 1 { + let count = bfs(&grid, &mut visited, i, j); + result = result.max(count); + } + } + } + + // 输出结果 + println!("{}", result); +} + +``` ### Javascript From a663089383732a56011c1b630c30635265c316a7 Mon Sep 17 00:00:00 2001 From: jjblaack Date: Sat, 5 Oct 2024 18:07:00 +0800 Subject: [PATCH 1343/1533] =?UTF-8?q?=E5=B2=9B=E5=B1=BF=E7=9A=84=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E9=9D=A2=E7=A7=AFjava=E5=AE=9E=E7=8E=B0=EF=BC=88dfs+b?= =?UTF-8?q?fs=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\351\235\242\347\247\257.md" | 116 +++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 51bfc57fd1..38b98d0e47 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -223,7 +223,121 @@ public: ## 其他语言版本 ### Java - +DFS +```java +//这里的实现为主函数处理每个岛屿的第一块陆地 方式 +//所以是主函数直接置count为1,剩余的交给dfs来做。 +import java.util.*; +public class Main{ + static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//四个方向 + static int count = 0; + public static void dfs(boolean[][] visited, int x, int y, int[][] grid){ + for(int i = 0; i < 4; i++){ + int nextX = x + dir[i][0]; + int nextY = y + dir[i][1]; + if(nextX < 0 || nextY < 0 || nextY >= grid[0].length || nextX >= grid.length){ + continue; + } + if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){ + count++; + visited[nextX][nextY] = true; + dfs(visited, nextX, nextY, grid); + } + } + } + public static void main(String[] args){ + Scanner in = new Scanner(System.in); + int n = in.nextInt(); + int m = in.nextInt(); + int[][] grid = new int[n][m]; + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + grid[i][j] = in.nextInt(); + } + } + + int result = 0; + boolean[][] visited = new boolean[n][m]; + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + if(!visited[i][j] && grid[i][j] == 1){ + visited[i][j] = true; + count = 1; + dfs(visited, i, j, grid); + //dfs遍历完了一座岛屿,就比较count和result,保留最大的 + result = Math.max(result, count); + } + } + } + System.out.println(result); + } +} +``` +BFS +```java +import java.util.*; +public class Main{ + static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//下右上左的顺序 + static int count = 0; + public static void bfs(boolean[][] visited, int x, int y, int[][] grid){ + Queue queue = new LinkedList(); + queue.add(new pair(x,y)); + count = 1; //该岛屿的第一块陆地被visit了 + + //对这个岛屿的所有都入队,除非上下左右都没有未访问的陆地 + while(!queue.isEmpty()){ + int curX = queue.peek().x; + int curY = queue.poll().y; + //对每块陆地都进行上下左右的入队和计算(遍历),自然就是按广度优先了 + for(int i = 0; i < 4; i++){ + int nextX = curX + dir[i][0]; + int nextY = curY + dir[i][1]; + if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length){ + continue; + } + if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){ + count++; + queue.add(new pair(nextX, nextY)); + visited[nextX][nextY] = true; + } + } + } + } + + static class pair{ + int x; + int y; + pair(int x, int y){ + this.x = x; + this.y = y; + } + } + + public static void main(String[] args){ + Scanner in = new Scanner(System.in); + int n = in.nextInt(); + int m = in.nextInt(); + int[][] grid = new int[n][m]; + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + grid[i][j] = in.nextInt(); + } + } + int result = 0; + boolean[][] visited = new boolean[n][m]; + for(int i = 0; i < n; i++){ + for(int j = 0; j < m; j++){ + if(!visited[i][j] && grid[i][j] == 1){ + visited[i][j] = true; + bfs(visited, i, j, grid); + result = Math.max(result, count); + } + } + } + System.out.println(result); + } +} +``` ### Python DFS From 9c7131e253ad9b59aaa69f102f03e0154752b660 Mon Sep 17 00:00:00 2001 From: Jasonyou <2926593225@qq.com> Date: Sun, 6 Oct 2024 03:04:09 +0800 Subject: [PATCH 1344/1533] =?UTF-8?q?feate:=20=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=8E=A5=E9=BE=99=20=20Java=E4=BC=98=E5=8C=96=E7=89=88?= =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=201.=E4=BD=BF=E7=94=A8null=E6=A0=87=E5=BF=97=E5=88=86=E5=B1=82?= =?UTF-8?q?=202.=E5=AF=BB=E6=89=BE=E9=82=BB=E6=8E=A5=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=9B=9E=E6=BB=9A=E4=BF=AE=E6=94=B9=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=A4=9A=E6=AC=A1=E6=95=B0=E7=BB=84=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=203.=E4=BF=AE=E5=A4=8D=E8=BE=B9=E7=95=8C=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=EF=BC=9AbeginStr=3D=3DendStr=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=BA=94=E8=AF=A5=E8=BE=93=E5=87=BA0=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\344\270\262\346\216\245\351\276\231.md" | 108 +++++++++--------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 308e50b7f3..ef261c4d8c 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -152,66 +152,70 @@ int main() { ## 其他语言版本 -### Java +### Java + ```Java +import java.util.*; + public class Main { - // BFS方法 - public static int ladderLength(String beginWord, String endWord, List wordList) { - // 使用set作为查询容器,效率更高 - HashSet set = new HashSet<>(wordList); - - // 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串 - Queue queue = new LinkedList<>(); - - // 声明一个hashMap存储遍历到的字符串以及所走过的路径path - HashMap visitMap = new HashMap<>(); - queue.offer(beginWord); - visitMap.put(beginWord, 1); - - while (!queue.isEmpty()) { - String curWord = queue.poll(); - int path = visitMap.get(curWord); - - for (int i = 0; i < curWord.length(); i++) { - char[] ch = curWord.toCharArray(); - // 每个位置尝试26个字母 - for (char k = 'a'; k <= 'z'; k++) { - ch[i] = k; - - String newWord = new String(ch); - if (newWord.equals(endWord)) return path + 1; - - // 如果这个新字符串存在于容器且之前未被访问到 - if (set.contains(newWord) && !visitMap.containsKey(newWord)) { - visitMap.put(newWord, path + 1); - queue.offer(newWord); + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + scanner.nextLine(); + String beginStr = scanner.next(); + String endStr = scanner.next(); + scanner.nextLine(); + List wordList = new ArrayList<>(); + wordList.add(beginStr); + wordList.add(endStr); + for (int i = 0; i < n; i++) { + wordList.add(scanner.nextLine()); + } + int count = bfs(beginStr, endStr, wordList); + System.out.println(count); + } + + /** + * 广度优先搜索-寻找最短路径 + */ + public static int bfs(String beginStr, String endStr, List wordList) { + int len = 1; + Set set = new HashSet<>(wordList); + Set visited = new HashSet<>(); + Queue q = new LinkedList<>(); + visited.add(beginStr); + q.add(beginStr); + q.add(null); + while (!q.isEmpty()) { + String node = q.remove(); + //上一层结束,若下一层还有节点进入下一层 + if (node == null) { + if (!q.isEmpty()) { + len++; + q.add(null); + } + continue; + } + char[] charArray = node.toCharArray(); + //寻找邻接节点 + for (int i = 0; i < charArray.length; i++) { + //记录旧值,用于回滚修改 + char old = charArray[i]; + for (char j = 'a'; j <= 'z'; j++) { + charArray[i] = j; + String newWord = new String(charArray); + if (set.contains(newWord) && !visited.contains(newWord)) { + q.add(newWord); + visited.add(newWord); + //找到结尾 + if (newWord.equals(endStr)) return len + 1; } } + charArray[i] = old; } } - return 0; } - - public static void main (String[] args) { - /* code */ - // 接收输入 - Scanner sc = new Scanner(System.in); - int N = sc.nextInt(); - sc.nextLine(); - String[] strs = sc.nextLine().split(" "); - - List wordList = new ArrayList<>(); - for (int i = 0; i < N; i++) { - wordList.add(sc.nextLine()); - } - - // wordList.add(strs[1]); - - // 打印结果 - int result = ladderLength(strs[0], strs[1], wordList); - System.out.println(result); - } } ``` From d3804674e4b26d29800720a35d42c177a5a49e66 Mon Sep 17 00:00:00 2001 From: Jasonyou <2926593225@qq.com> Date: Mon, 7 Oct 2024 06:29:20 +0800 Subject: [PATCH 1345/1533] =?UTF-8?q?perf=EF=BC=9A=E5=B0=81=E8=A3=85?= =?UTF-8?q?=E6=88=90=E7=B1=BB=EF=BC=8C=E7=90=86=E8=A7=A3=E8=B5=B7=E6=9D=A5?= =?UTF-8?q?=E6=9B=B4=E5=AE=B9=E6=98=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\231\350\277\236\346\216\245II.md" | 168 ++++++++++-------- 1 file changed, 97 insertions(+), 71 deletions(-) diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 2bd4eac6e2..56959d8784 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -250,105 +250,131 @@ int main() { ## 其他语言版本 ### Java -```java -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; +```java +import java.util.*; + +/* + * 冗余连接II。主要问题是存在入度为2或者成环,也可能两个问题同时存在。 + * 1.判断入度为2的边 + * 2.判断是否成环(并查集) + */ + public class Main { - static int n; - static int[] father = new int[1001]; // 并查集数组 + /** + * 并查集模板 + */ + static class Disjoint { + + private final int[] father; - // 并查集初始化 - public static void init() { - for (int i = 1; i <= n; ++i) { - father[i] = i; + public Disjoint(int n) { + father = new int[n]; + for (int i = 0; i < n; i++) { + father[i] = i; + } + } + + public void join(int n, int m) { + n = find(n); + m = find(m); + if (n == m) return; + father[n] = m; + } + + public int find(int n) { + return father[n] == n ? n : (father[n] = find(father[n])); } - } - // 并查集里寻根的过程 - public static int find(int u) { - if (u == father[u]) return u; - return father[u] = find(father[u]); // 路径压缩 + public boolean isSame(int n, int m) { + return find(n) == find(m); + } } - // 将 v->u 这条边加入并查集 - public static void join(int u, int v) { - u = find(u); - v = find(v); - if (u != v) { - father[v] = u; // 合并两棵树 + static class Edge { + int s; + int t; + + public Edge(int s, int t) { + this.s = s; + this.t = t; } } - // 判断 u 和 v 是否有同一个根 - public static boolean same(int u, int v) { - return find(u) == find(v); + static class Node { + int id; + int in; + int out; } - // 在有向图里找到删除的那条边,使其变成树 - public static void getRemoveEdge(List edges) { - init(); // 初始化并查集 - for (int i = 0; i < n; i++) { // 遍历所有的边 - if (same(edges.get(i)[0], edges.get(i)[1])) { // 如果构成有向环了,就是要删除的边 - System.out.println(edges.get(i)[0] + " " + edges.get(i)[1]); - return; + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + List edges = new ArrayList<>(); + Node[] nodeMap = new Node[n + 1]; + for (int i = 1; i <= n; i++) { + nodeMap[i] = new Node(); + } + Integer doubleIn = null; + for (int i = 0; i < n; i++) { + int s = scanner.nextInt(); + int t = scanner.nextInt(); + //记录入度 + nodeMap[t].in++; + if (!(nodeMap[t].in < 2)) doubleIn = t; + Edge edge = new Edge(s, t); + edges.add(edge); + } + Edge result = null; + //存在入度为2的节点,既要消除入度为2的问题同时解除可能存在的环 + if (doubleIn != null) { + List doubleInEdges = new ArrayList<>(); + for (Edge edge : edges) { + if (edge.t == doubleIn) doubleInEdges.add(edge); + if (doubleInEdges.size() == 2) break; + } + Edge edge = doubleInEdges.get(1); + if (isTreeWithExclude(edges, edge, nodeMap)) { + result = edge; } else { - join(edges.get(i)[0], edges.get(i)[1]); + result = doubleInEdges.get(0); } + } else { + //不存在入度为2的节点,则只需要解除环即可 + result = getRemoveEdge(edges, nodeMap); } + + System.out.println(result.s + " " + result.t); } - // 删一条边之后判断是不是树 - public static boolean isTreeAfterRemoveEdge(List edges, int deleteEdge) { - init(); // 初始化并查集 - for (int i = 0; i < n; i++) { - if (i == deleteEdge) continue; - if (same(edges.get(i)[0], edges.get(i)[1])) { // 如果构成有向环了,一定不是树 + public static boolean isTreeWithExclude(List edges, Edge exculdEdge, Node[] nodeMap) { + Disjoint disjoint = new Disjoint(nodeMap.length + 1); + for (Edge edge : edges) { + if (edge == exculdEdge) continue; + //成环则不是树 + if (disjoint.isSame(edge.s, edge.t)) { return false; } - join(edges.get(i)[0], edges.get(i)[1]); + disjoint.join(edge.s, edge.t); } return true; } - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - List edges = new ArrayList<>(); // 存储所有的边 - - n = sc.nextInt(); // 顶点数 - int[] inDegree = new int[n + 1]; // 记录每个节点的入度 - for (int i = 0; i < n; i++) { - int s = sc.nextInt(); // 边的起点 - int t = sc.nextInt(); // 边的终点 - inDegree[t]++; - edges.add(new int[]{s, t}); // 将边加入列表 - } - - List vec = new ArrayList<>(); // 记录入度为2的边(如果有的话就两条边) - // 找入度为2的节点所对应的边,注意要倒序,因为优先删除最后出现的一条边 - for (int i = n - 1; i >= 0; i--) { - if (inDegree[edges.get(i)[1]] == 2) { - vec.add(i); - } - } + public static Edge getRemoveEdge(List edges, Node[] nodeMap) { + int length = nodeMap.length; + Disjoint disjoint = new Disjoint(length); - // 情况一、情况二 - if (vec.size() > 0) { - // vec里的边已经按照倒叙放的,所以优先删 vec.get(0) 这条边 - if (isTreeAfterRemoveEdge(edges, vec.get(0))) { - System.out.println(edges.get(vec.get(0))[0] + " " + edges.get(vec.get(0))[1]); - } else { - System.out.println(edges.get(vec.get(1))[0] + " " + edges.get(vec.get(1))[1]); - } - return; + for (Edge edge : edges) { + if (disjoint.isSame(edge.s, edge.t)) return edge; + disjoint.join(edge.s, edge.t); } - - // 处理情况三:明确没有入度为2的情况,一定有有向环,找到构成环的边返回即可 - getRemoveEdge(edges); + return null; } + } + ``` + ### Python ```python From 8669f2ad20f3c7f550557584fa57b4622e5862e7 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Mon, 7 Oct 2024 13:19:27 +0800 Subject: [PATCH 1346/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90459.=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 打码->代码 --- ...\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index dbf32e6482..5c86e122bb 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -312,7 +312,7 @@ next 数组记录的就是最长相同前后缀( [字符串:KMP算法精讲] 4可以被 12(字符串的长度) 整除,所以说明有重复的子字符串(asdf)。 -### 打码实现 +### 代码实现 C++代码如下:(这里使用了前缀表统一减一的实现方式) From fdae38cf7e575cc01f3db7807ca48b942dcf2cd2 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 8 Oct 2024 15:16:23 +0800 Subject: [PATCH 1347/1533] =?UTF-8?q?337.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8DIII=E5=A2=9E=E5=8A=A0Go=E9=80=92=E5=BD=92=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\256\266\345\212\253\350\210\215III.md" | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 7aae5cbf2a..12e31ababd 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -388,6 +388,90 @@ class Solution: ### Go +暴力递归 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func rob(root *TreeNode) int { + if root == nil { + return 0 + } + if root.Left == nil && root.Right == nil { + return root.Val + } + // 偷父节点 + val1 := root.Val + if root.Left != nil { + val1 += rob(root.Left.Left) + rob(root.Left.Right) // 跳过root->left,相当于不考虑左孩子了 + } + if root.Right != nil { + val1 += rob(root.Right.Left) + rob(root.Right.Right) // 跳过root->right,相当于不考虑右孩子了 + } + // 不偷父节点 + val2 := rob(root.Left) + rob(root.Right) // 考虑root的左右孩子 + return max(val1, val2) +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + +记忆化递推 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +var umap = make(map[*TreeNode]int) + +func rob(root *TreeNode) int { + if root == nil { + return 0 + } + if root.Left == nil && root.Right == nil { + return root.Val + } + if val, ok := umap[root]; ok { + return val // 如果umap里已经有记录则直接返回 + } + // 偷父节点 + val1 := root.Val + if root.Left != nil { + val1 += rob(root.Left.Left) + rob(root.Left.Right) // 跳过root->left,相当于不考虑左孩子了 + } + if root.Right != nil { + val1 += rob(root.Right.Left) + rob(root.Right.Right) // 跳过root->right,相当于不考虑右孩子了 + } + // 不偷父节点 + val2 := rob(root.Left) + rob(root.Right) // 考虑root的左右孩子 + umap[root] = max(val1, val2) // umap记录一下结果 + return max(val1, val2) +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + 动态规划 ```go From d33c76e74aa90b8ba319af04b0c7f731f6bfabca Mon Sep 17 00:00:00 2001 From: 01geek Date: Wed, 9 Oct 2024 16:28:31 +0800 Subject: [PATCH 1348/1533] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=20problems/1047.?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=89=80=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9?= =?UTF-8?q?.md=20=E4=B8=ADjava=E7=89=88=E6=9C=AC=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=9A=E5=B0=86top>0=E7=BA=A0=E6=AD=A3?= =?UTF-8?q?=E4=B8=BAtop>=3D0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 7232008a2c..51ec4e62c6 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -164,7 +164,7 @@ class Solution { int top = -1; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); - // 当 top > 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top-- + // 当 top >= 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top-- if (top >= 0 && res.charAt(top) == c) { res.deleteCharAt(top); top--; From ba2062471b0a041b748bb543d58514ceb849b21f Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 9 Oct 2024 16:58:09 +0800 Subject: [PATCH 1349/1533] Update --- ...20\345\255\227\347\254\246\344\270\262.md" | 22 +++++++-------- ...\351\222\261\345\205\221\346\215\242II.md" | 28 ++++++++++++++++--- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 254d921dbd..bee1c1022c 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -64,7 +64,7 @@ 如果有一个字符串s,在 s + s 拼接后, 不算首尾字符,如果能凑成s字符串,说明s 一定是重复子串组成。 -如图,字符串s,图中数字为数组下标,在 s + s 拼接后, 不算首尾字符,中间凑成s字符串。 +如图,字符串s,图中数字为数组下标,在 s + s 拼接后, 不算首尾字符,中间凑成s字符串。 (图中数字为数组下标) ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910115555.png) @@ -163,9 +163,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 如果一个字符串s是由重复子串组成,那么 最长相等前后缀不包含的子串一定是字符串s的最小重复子串。 -证明: 如果s 是有是有最小重复子串p组成。 - -即 s = n * p +如果s 是由最小重复子串p组成,即 s = n * p 那么相同前后缀可以是这样: @@ -203,12 +201,14 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 最长相等前后缀不包含的子串已经是字符串s的最小重复子串,那么字符串s一定由重复子串组成,这个不需要证明了。 -关键是要要证明:最长相等前后缀不包含的子串什么时候才是字符串s的最小重复子串呢。 +关键是要证明:最长相等前后缀不包含的子串什么时候才是字符串s的最小重复子串呢。 -情况一, 最长相等前后缀不包含的子串的长度 比 字符串s的一半的长度还大,那一定不是字符串s的重复子串 +情况一, 最长相等前后缀不包含的子串的长度 比 字符串s的一半的长度还大,那一定不是字符串s的重复子串,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240911110236.png) +图中:前后缀不包含的子串的长度 大于 字符串s的长度的 二分之一 + -------------- 情况二,最长相等前后缀不包含的子串的长度 可以被 字符串s的长度整除,如图: @@ -230,7 +230,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 即 s[0]s[1] 是最小重复子串 -以上推导中,录友可能想,你怎么知道 s[0] 和 s[1] 就不相同呢? s[0] 为什么就不能使最小重复子串。 +以上推导中,录友可能想,你怎么知道 s[0] 和 s[1] 就不相同呢? s[0] 为什么就不能是最小重复子串。 如果 s[0] 和 s[1] 也相同,同时 s[0]s[1]与s[2]s[3]相同,s[2]s[3] 与 s[4]s[5]相同,s[4]s[5] 与 s[6]s[7] 相同,那么这个字符串就是有一个字符构成的字符串。 @@ -246,7 +246,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 或者说,自己举个例子,`aaaaaa`,这个字符串,他的最长相等前后缀是什么? -同上以上推导,最长相等前后缀不包含的子串的长度只要被 字符串s的长度整除,就是一定是最小重复子串。 +同上以上推导,最长相等前后缀不包含的子串的长度只要被 字符串s的长度整除,最长相等前后缀不包含的子串一定是最小重复子串。 ---------------- @@ -267,7 +267,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 以上推导,可以得出 s[0],s[1],s[2] 与 s[3],s[4],s[5] 相同,s[3]s[4] 与 s[6]s[7]相同。 -那么 最长相等前后缀不包含的子串的长度 不被 字符串s的长度整除 ,就不是s的重复子串 +那么 最长相等前后缀不包含的子串的长度 不被 字符串s的长度整除 ,最长相等前后缀不包含的子串就不是s的重复子串 ----------- @@ -277,7 +277,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 在必要条件,这个是 显而易见的,都已经假设 最长相等前后缀不包含的子串 是 s的最小重复子串了,那s必然是重复子串。 -关键是需要证明, 字符串s的最长相等前后缀不包含的子串 什么时候才是 s最小重复子串。 +**关键是需要证明, 字符串s的最长相等前后缀不包含的子串 什么时候才是 s最小重复子串**。 同上我们证明了,当 最长相等前后缀不包含的子串的长度 可以被 字符串s的长度整除,那么不包含的子串 就是s的最小重复子串。 @@ -312,7 +312,7 @@ next 数组记录的就是最长相同前后缀( [字符串:KMP算法精讲] 4可以被 12(字符串的长度) 整除,所以说明有重复的子字符串(asdf)。 -### 打码实现 +### 代码实现 C++代码如下:(这里使用了前缀表统一减一的实现方式) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 255912d62b..360da5826b 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -168,23 +168,43 @@ for (int j = 0; j <= amount; j++) { // 遍历背包容量 class Solution { public: int change(int amount, vector& coins) { - vector dp(amount + 1, 0); - dp[0] = 1; + vector dp(amount + 1, 0); // 防止相加数据超int + dp[0] = 1; // 只有一种方式达到0 for (int i = 0; i < coins.size(); i++) { // 遍历物品 for (int j = coins[i]; j <= amount; j++) { // 遍历背包 dp[j] += dp[j - coins[i]]; } } - return dp[amount]; + return dp[amount]; // 返回组合数 } }; ``` +C++测试用例有两个数相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 + * 时间复杂度: O(mn),其中 m 是amount,n 是 coins 的长度 * 空间复杂度: O(m) +为了防止相加的数据 超int 也可以这么写: + +```CPP +class Solution { +public: + int change(int amount, vector& coins) { + vector dp(amount + 1, 0); + dp[0] = 1; // 只有一种方式达到0 + for (int i = 0; i < coins.size(); i++) { // 遍历物品 + for (int j = coins[i]; j <= amount; j++) { // 遍历背包 + if (dp[j] < INT_MAX - dp[j - coins[i]]) { //防止相加数据超int + dp[j] += dp[j - coins[i]]; + } + } + } + return dp[amount]; // 返回组合数 + } +}; +``` -是不是发现代码如此精简 ## 总结 From 8f692f8ff4545337e9df4790fa2cdb7f9a07c219 Mon Sep 17 00:00:00 2001 From: 01geek Date: Wed, 9 Oct 2024 17:36:56 +0800 Subject: [PATCH 1350/1533] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200150.=E9=80=86?= =?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?= =?UTF-8?q?.md=20=E7=9A=84c++=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=B0=86=E7=AC=AC?= =?UTF-8?q?21=E8=A1=8C=E4=BB=A3=E7=A0=81=E7=9A=84int=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BAlong=20long=EF=BC=8C=E5=8E=9F=E5=9B=A0=EF=BC=9A?= =?UTF-8?q?=E6=97=A2=E7=84=B6=E5=89=8D=E9=9D=A2=E8=80=83=E8=99=91=E5=88=B0?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E6=95=B0=E6=8D=AE=E5=80=BC=E5=A4=A7=E8=80=8C?= =?UTF-8?q?=E5=B0=86int=E6=94=B9=E4=B8=BA=E4=BA=86long=20long=EF=BC=8C?= =?UTF-8?q?=E9=82=A3=E4=B9=88=E6=9C=80=E5=90=8E=E7=9A=84RPN=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E7=BB=93=E6=9E=9C=E4=B9=9F=E5=8F=AF=E8=83=BD=E8=B6=85?= =?UTF-8?q?=E8=BF=87int=E8=83=BD=E8=A1=A8=E7=A4=BA=E7=9A=84=E8=8C=83?= =?UTF-8?q?=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 48e99c5baa..7d4031d7da 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -108,7 +108,7 @@ public: } } - int result = st.top(); + long long result = st.top(); st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事) return result; } From baf7ed122b1cb78b89565b83b343a4f3c5f5ec82 Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:18:04 +0800 Subject: [PATCH 1351/1533] =?UTF-8?q?feat:=201207.=E7=8B=AC=E4=B8=80?= =?UTF-8?q?=E6=97=A0=E4=BA=8C=E7=9A=84=E5=87=BA=E7=8E=B0=E6=AC=A1=E6=95=B0?= =?UTF-8?q?,=E6=96=B0=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\347\216\260\346\254\241\346\225\260.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index cd89522e4e..781badf549 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -221,6 +221,28 @@ func uniqueOccurrences(arr []int) bool { } ``` +### Rust + +```rust +use std::collections::{HashMap, HashSet}; +impl Solution { + pub fn unique_occurrences(arr: Vec) -> bool { + let mut hash = HashMap::::new(); + for x in arr { + *hash.entry(x).or_insert(0) += 1; + } + let mut set = HashSet::::new(); + for (_k, v) in hash { + if set.contains(&v) { + return false + } else { + set.insert(v); + } + } + true + } +} +``` From e43323b1e1f3556dc7bc38b4ae6bdd13508e0aea Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:18:41 +0800 Subject: [PATCH 1352/1533] =?UTF-8?q?feat:=201365.=E6=9C=89=E5=A4=9A?= =?UTF-8?q?=E5=B0=91=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E5=AD=97,=E6=96=B0=E5=A2=9Erust=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\227\347\232\204\346\225\260\345\255\227.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 94c1eb77c9..64f610968b 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -260,6 +260,22 @@ function smallerNumbersThanCurrent(nums: number[]): number[] { }; ``` +### rust +```rust +use std::collections::HashMap; +impl Solution { + pub fn smaller_numbers_than_current(nums: Vec) -> Vec { + let mut v = nums.clone(); + v.sort(); + let mut hash = HashMap::new(); + for i in 0..v.len() { + // rust中使用or_insert插入值, 如果存在就不插入,可以使用正序遍历 + hash.entry(v[i]).or_insert(i as i32); + } + nums.into_iter().map(|x| *hash.get(&x).unwrap()).collect() + } +} +```

From 30685b815cc8787fd84d760045a4e26456a51ef7 Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:19:43 +0800 Subject: [PATCH 1353/1533] =?UTF-8?q?feat:=200941.=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=B1=B1=E8=84=89=E6=95=B0=E7=BB=84,=E6=96=B0?= =?UTF-8?q?=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\261\350\204\211\346\225\260\347\273\204.md" | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" index 9f63f441c2..77167df041 100644 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" @@ -196,7 +196,22 @@ public class Solution { } ``` - +### Rust +```rust +impl Solution { + pub fn valid_mountain_array(arr: Vec) -> bool { + let mut i = 0; + let mut j = arr.len() - 1; + while i < arr.len() - 1 && arr[i] < arr[i + 1] { + i += 1; + } + while j > 0 && arr[j] < arr[j - 1] { + j -= 1; + } + i > 0 && j < arr.len() - 1 && i == j + } +} +```

From 83de8ac2a1e3f6203f25e5b792acb22bc187676d Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:25:53 +0800 Subject: [PATCH 1354/1533] =?UTF-8?q?feat:=200283.=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E9=9B=B6,=E6=96=B0=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0283.\347\247\273\345\212\250\351\233\266.md" | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index 4d8fd9a155..cbce029576 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -169,7 +169,20 @@ void moveZeroes(int* nums, int numsSize){ } ``` - +### Rust +```rust +impl Solution { + pub fn move_zeroes(nums: &mut Vec) { + let mut slow = 0; + for fast in 0..nums.len() { + if nums[fast] != 0 { + nums.swap(slow, fast); + slow += 1; + } + } + } +} +``` From 90255b837ceb114caefd6a5e57eddadf2e0ace61 Mon Sep 17 00:00:00 2001 From: "junjie2.luo" Date: Thu, 10 Oct 2024 17:31:49 +0800 Subject: [PATCH 1355/1533] =?UTF-8?q?feat:=200189.=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E6=95=B0=E7=BB=84,=E6=96=B0=E5=A2=9Erust=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\227\213\350\275\254\346\225\260\347\273\204.md" | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" index b47ee4b91f..e91109c692 100644 --- "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" +++ "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" @@ -199,7 +199,17 @@ function reverseByRange(nums: number[], left: number, right: number): void { } ``` - +### Rust +```rust +impl Solution { + pub fn rotate(nums: &mut Vec, k: i32) { + let k = k as usize % nums.len(); + nums.reverse(); + nums[..k].reverse(); + nums[k..].reverse(); + } +} +```

From a45758ef5f629807e248ea87407611bcbf42ed88 Mon Sep 17 00:00:00 2001 From: iYaovo <2293257241@qq.com> Date: Fri, 11 Oct 2024 10:13:18 +0800 Subject: [PATCH 1356/1533] =?UTF-8?q?docs:=E8=A1=A5=E5=85=850046Java?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=9A=84return=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0046.\345\205\250\346\216\222\345\210\227.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index 1ef80a142d..ab7af3ba6a 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -201,6 +201,7 @@ class Solution { public void backtrack(int[] nums, LinkedList path) { if (path.size() == nums.length) { result.add(new ArrayList<>(path)); + return; } for (int i =0; i < nums.length; i++) { // 如果path中已有,则跳过 @@ -524,3 +525,4 @@ public class Solution + From 55239579183ffebf29c7cfae52c626b996676061 Mon Sep 17 00:00:00 2001 From: markwang Date: Sat, 12 Oct 2024 10:10:05 +0800 Subject: [PATCH 1357/1533] =?UTF-8?q?122.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAII?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E6=95=B0=E7=BB=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index f0dff50506..0dced9efd4 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -251,6 +251,27 @@ func max(a, b int) int { } ``` +```go +// 动态规划 版本二 滚动数组 +func maxProfit(prices []int) int { + dp := [2][2]int{} // 注意这里只开辟了一个2 * 2大小的二维数组 + dp[0][0] = -prices[0] + dp[0][1] = 0 + for i := 1; i < len(prices); i++ { + dp[i%2][0] = max(dp[(i-1)%2][0], dp[(i - 1) % 2][1] - prices[i]) + dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0] + prices[i]) + } + return dp[(len(prices)-1)%2][1] +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + ### JavaScript: ```javascript From 19a6a4e369277cd192f6b149b008ce2d0cba23a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lane=20Zhang=20=28=E5=BC=A0=E5=81=A5=29?= Date: Mon, 14 Oct 2024 10:00:24 +0800 Subject: [PATCH 1358/1533] =?UTF-8?q?Update=200202.=E5=BF=AB=E4=B9=90?= =?UTF-8?q?=E6=95=B0.md=20=E5=A2=9E=E5=8A=A0=20Ruby=20=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\345\277\253\344\271\220\346\225\260.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 409a7471d4..39cb39fa31 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -534,6 +534,30 @@ public class Solution { } ``` +### Ruby: + +```ruby +# @param {Integer} n +# @return {Boolean} +def is_happy(n) + @occurred_nums = Set.new + + while true + n = next_value(n) + + return true if n == 1 + + return false if @occurred_nums.include?(n) + + @occurred_nums << n + end +end + +def next_value(n) + n.to_s.chars.sum { |char| char.to_i ** 2 } +end +``` +

From c6b12f2b18eb3df0606b39c386e5e441394e81b0 Mon Sep 17 00:00:00 2001 From: markwang Date: Mon, 14 Oct 2024 10:10:19 +0800 Subject: [PATCH 1359/1533] =?UTF-8?q?123.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAIII?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E7=89=88=E6=9C=AC=E4=BA=8C=E5=92=8C?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...344\275\263\346\227\266\346\234\272III.md" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index d06b4f8022..8e224a898d 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -317,6 +317,7 @@ class Solution: ### Go: ```go +// 版本一 func maxProfit(prices []int) int { dp := make([][]int, len(prices)) for i := 0; i < len(prices); i++ { @@ -344,6 +345,58 @@ func max(a, b int) int { } ``` +```go +// 版本二 +func maxProfit(prices []int) int { + if len(prices) == 0 { + return 0 + } + dp := make([]int, 5) + dp[1] = -prices[0] + dp[3] = -prices[0] + for i := 1; i < len(prices); i++ { + dp[1] = max(dp[1], dp[0] - prices[i]) + dp[2] = max(dp[2], dp[1] + prices[i]) + dp[3] = max(dp[3], dp[2] - prices[i]) + dp[4] = max(dp[4], dp[3] + prices[i]) + } + return dp[4] +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + +```go +// 版本三 +func maxProfit(prices []int) int { + if len(prices) == 0 { + return 0 + } + dp := make([][5]int, len(prices)) + dp[0][1] = -prices[0] + dp[0][3] = -prices[0] + for i := 1; i < len(prices); i++ { + dp[i][1] = max(dp[i-1][1], 0 - prices[i]) + dp[i][2] = max(dp[i-1][2], dp[i-1][1] + prices[i]) + dp[i][3] = max(dp[i-1][3], dp[i-1][2] - prices[i]) + dp[i][4] = max(dp[i-1][4], dp[i-1][3] + prices[i]) + } + return dp[len(prices)-1][4] +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + ### JavaScript: > 版本一: From 0be8c49ba72d9e3e6ccdc4b76949a07ae128b7d5 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 15 Oct 2024 09:49:57 +0800 Subject: [PATCH 1360/1533] =?UTF-8?q?0714.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E6=89=8B=E7=BB=AD=E8=B4=B9=E6=96=87=E5=AD=97=E5=8B=98=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\272\345\220\253\346\211\213\347\273\255\350\264\271.md" | 4 ++-- ...12\250\346\200\201\350\247\204\345\210\222\357\274\211.md" | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" index 88b03d9d76..8a2d976b60 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" @@ -43,7 +43,7 @@ 在[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html)中使用贪心策略不用关心具体什么时候买卖,只要收集每天的正利润,最后稳稳的就是最大利润了。 -而本题有了手续费,就要关系什么时候买卖了,因为计算所获得利润,需要考虑买卖利润可能不足以手续费的情况。 +而本题有了手续费,就要关心什么时候买卖了,因为计算所获得利润,需要考虑买卖利润可能不足以扣减手续费的情况。 如果使用贪心策略,就是最低值买,最高值(如果算上手续费还盈利)就卖。 @@ -122,7 +122,7 @@ public: * 时间复杂度:O(n) * 空间复杂度:O(n) -当然可以对空间经行优化,因为当前状态只是依赖前一个状态。 +当然可以对空间进行优化,因为当前状态只是依赖前一个状态。 C++ 代码如下: diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index b0e8b141f5..86e3e88f2b 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -46,7 +46,7 @@ * 时间复杂度:O(n) * 空间复杂度:O(1) -本题使用贪心算法并不好理解,也很容易出错,那么我们再来看看是使用动规的方法如何解题。 +本题使用贪心算法并不好理解,也很容易出错,那么我们再来看看使用动规的方法如何解题。 相对于[动态规划:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II(动态规划).html),本题只需要在计算卖出操作的时候减去手续费就可以了,代码几乎是一样的。 @@ -54,7 +54,7 @@ 这里重申一下dp数组的含义: -dp[i][0] 表示第i天持有股票所省最多现金。 +dp[i][0] 表示第i天持有股票所得最多现金。 dp[i][1] 表示第i天不持有股票所得最多现金 From abd3593c67a551bbe01136a42defabf219bb55be Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Tue, 15 Oct 2024 13:30:09 +0800 Subject: [PATCH 1361/1533] =?UTF-8?q?0454.=E5=9B=9B=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0II.html=20=E5=A2=9E=E5=8A=A0Ruby=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\225\260\347\233\270\345\212\240II.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 6231c22b2f..af19f5f7d8 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -488,6 +488,44 @@ int fourSumCount(int* nums1, int nums1Size, int* nums2, int nums2Size, int* nums } ``` +### Ruby: + +```ruby +# @param {Integer[]} nums1 +# @param {Integer[]} nums2 +# @param {Integer[]} nums3 +# @param {Integer[]} nums4 +# @return {Integer} +# 新思路:和版主的思路基本相同,只是对后面两个数组的二重循环,用一个方法调用外加一重循环替代,简化了一点。 +# 简单的说,就是把四数和变成了两个两数和的统计(结果放到两个 hash 中),然后再来一次两数和为0. +# 把四个数分成两组两个数,然后分别计算每组可能的和情况,分别存入 hash 中,key 是 和,value 是 数量; +# 最后,得到的两个 hash 只需要遍历一次,符合和为零的 value 相乘并加总。 +def four_sum_count(nums1, nums2, nums3, nums4) + num_to_count_1 = two_sum_mapping(nums1, nums2) + num_to_count_2 = two_sum_mapping(nums3, nums4) + + count_sum = 0 + + num_to_count_1.each do |num, count| + count_sum += num_to_count_2[-num] * count # 反查另一个 hash,看有没有匹配的,没有的话,hash 默认值为 0,不影响加总;有匹配的,乘积就是可能的情况 + end + + count_sum +end + +def two_sum_mapping(nums1, nums2) + num_to_count = Hash.new(0) + + nums1.each do |num1| + nums2.each do |nums2| + num_to_count[num1 + nums2] += 1 # 统计和为 num1 + nums2 的有几个 + end + end + + num_to_count +end +``` +

From a4cafe0ce750c44f327a97a9811ff0b2d2c49979 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Wed, 16 Oct 2024 11:25:49 +0800 Subject: [PATCH 1362/1533] =?UTF-8?q?0541.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2II.md=20=E5=A2=9E=E5=8A=A0Python=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=AE=9E=E7=8E=B03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...254\345\255\227\347\254\246\344\270\262II.md" | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index 5e75d3c3f8..c89b3a7b3e 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -282,7 +282,7 @@ class Solution: return ''.join(res) ``` -### Python3 (v2): +#### Python3 (v2): ```python class Solution: @@ -297,6 +297,20 @@ class Solution: return s ``` +#### Python3 (v3): +```python +class Solution: + def reverseStr(self, s: str, k: int) -> str: + i = 0 + chars = list(s) + + while i < len(chars): + chars[i:i + k] = chars[i:i + k][::-1] # 反转后,更改原值为反转后值 + i += k * 2 + + return ''.join(chars) +``` + ### Go: ```go From 27585bd4f8fc7b4f0599f231d4924641e1bf6deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lane=20Zhang=20=28=E5=BC=A0=E5=81=A5=29?= Date: Wed, 16 Oct 2024 11:27:38 +0800 Subject: [PATCH 1363/1533] =?UTF-8?q?Update=200541.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index c89b3a7b3e..b3e7b02229 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -298,6 +298,7 @@ class Solution: ``` #### Python3 (v3): + ```python class Solution: def reverseStr(self, s: str, k: int) -> str: From cb50b956ebc89019b915ce92174126f82de10e1a Mon Sep 17 00:00:00 2001 From: ch4r1ty <81227726+ch4r1ty@users.noreply.github.com> Date: Wed, 16 Oct 2024 00:01:58 -0400 Subject: [PATCH 1364/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=860027.?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86java=E6=9A=B4=E5=8A=9B=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.md" | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 480800e96b..10817ba6b7 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -131,7 +131,24 @@ public: ## 其他语言版本 ### Java: - +```java +class Solution { + public int removeElement(int[] nums, int val) { + // 暴力法 + int n = nums.length; + for (int i = 0; i < n; i++) { + if (nums[i] == val) { + for (int j = i + 1; j < n; j++) { + nums[j - 1] = nums[j]; + } + i--; + n--; + } + } + return n; + } +} +``` ```java class Solution { public int removeElement(int[] nums, int val) { From 7bbf3827d1b9aa3a18f4073533ac2934ecc4eab1 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Wed, 16 Oct 2024 14:20:41 +0800 Subject: [PATCH 1365/1533] =?UTF-8?q?0151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Python=E7=89=88=E6=9C=AC=E5=AE=9E=E7=8E=B05?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\347\232\204\345\215\225\350\257\215.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 9a0cbea466..7c0b7cb182 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -513,6 +513,29 @@ class Solution: return "".join(result) ``` + +(版本五) 遇到空格就说明前面的是一个单词,把它加入到一个数组中。 + +```python +class Solution: + def reverseWords(self, s: str) -> str: + words = [] + word = '' + s += ' ' # 帮助处理最后一个字词 + + for char in s: + if char == ' ': # 遇到空格就说明前面的可能是一个单词 + if word != '': # 确认是单词,把它加入到一个数组中 + words.append(word) + word = '' # 清空当前单词 + continue + + word += char # 收集单词的字母 + + words.reverse() + return ' '.join(words) +``` + ### Go: 版本一: From ee4c1c584cbe27ff27f4dbf0cdb858c38d574171 Mon Sep 17 00:00:00 2001 From: skyclouds2001 <95597335+skyclouds2001@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:27:53 +0800 Subject: [PATCH 1366/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8B=BC=E5=86=99?= =?UTF-8?q?=EF=BC=9AJavascript->JavaScript&Typescript->TypeScript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...344\270\244\346\225\260\344\271\213\345\222\214.md" | 2 +- ...346\225\210\347\232\204\346\213\254\345\217\267.md" | 2 +- ...344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- .../0037.\350\247\243\346\225\260\347\213\254.md" | 2 +- ...0\267\263\350\267\203\346\270\270\346\210\217II.md" | 2 +- .../0046.\345\205\250\346\216\222\345\210\227.md" | 2 +- .../0047.\345\205\250\346\216\222\345\210\227II.md" | 2 +- "problems/0051.N\347\232\207\345\220\216.md" | 2 +- ...345\244\247\345\255\220\345\272\217\345\222\214.md" | 2 +- ...350\236\272\346\227\213\347\237\251\351\230\265.md" | 2 +- ...350\267\263\350\267\203\346\270\270\346\210\217.md" | 2 +- ...345\220\210\345\271\266\345\214\272\351\227\264.md" | 2 +- ...344\270\215\345\220\214\350\267\257\345\276\204.md" | 2 +- ...4\270\215\345\220\214\350\267\257\345\276\204II.md" | 2 +- .../0070.\347\210\254\346\245\274\346\242\257.md" | 2 +- ...347\274\226\350\276\221\350\267\235\347\246\273.md" | 2 +- "problems/0077.\347\273\204\345\220\210.md" | 2 +- "problems/0078.\345\255\220\351\233\206.md" | 2 +- "problems/0090.\345\255\220\351\233\206II.md" | 2 +- ...345\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- ...345\261\202\345\272\217\351\201\215\345\216\206.md" | 10 +++++----- ...346\234\200\345\244\247\346\267\261\345\272\246.md" | 2 +- ...350\267\257\345\276\204\346\200\273\345\222\214.md" | 2 +- ...347\232\204\345\255\220\345\272\217\345\210\227.md" | 2 +- ...6\234\200\344\275\263\346\227\266\346\234\272II.md" | 2 +- .../0134.\345\212\240\346\262\271\347\253\231.md" | 2 +- ...345\210\206\345\217\221\347\263\226\346\236\234.md" | 2 +- ...347\232\204\345\255\220\346\225\260\347\273\204.md" | 2 +- ...345\217\243\346\234\200\345\244\247\345\200\274.md" | 2 +- ...345\205\250\345\271\263\346\226\271\346\225\260.md" | 2 +- ...345\215\207\345\255\220\345\272\217\345\210\227.md" | 2 +- ...345\220\253\345\206\267\345\206\273\346\234\237.md" | 2 +- ...351\233\266\351\222\261\345\205\221\346\215\242.md" | 2 +- ...345\256\211\346\216\222\350\241\214\347\250\213.md" | 2 +- ...346\225\264\346\225\260\346\213\206\345\210\206.md" | 2 +- ...346\221\206\345\212\250\345\272\217\345\210\227.md" | 2 +- ...345\220\210\346\200\273\345\222\214\342\205\243.md" | 2 +- ...351\207\215\345\273\272\351\230\237\345\210\227.md" | 2 +- ...351\207\215\345\217\240\345\214\272\351\227\264.md" | 2 +- ...345\274\225\347\210\206\346\260\224\347\220\203.md" | 2 +- ...345\210\206\345\217\221\351\245\274\345\271\262.md" | 2 +- .../0474.\344\270\200\345\222\214\351\233\266.md" | 2 +- ...345\242\236\345\255\220\345\272\217\345\210\227.md" | 2 +- .../0494.\347\233\256\346\240\207\345\222\214.md" | 2 +- ...346\263\242\351\202\243\345\245\221\346\225\260.md" | 2 +- ...346\226\207\345\255\220\345\272\217\345\210\227.md" | 2 +- ...1\233\266\351\222\261\345\205\221\346\215\242II.md" | 2 +- ...345\210\240\351\231\244\346\223\215\344\275\234.md" | 2 +- ...345\233\236\346\226\207\345\255\220\344\270\262.md" | 2 +- ...351\200\222\345\242\236\345\272\217\345\210\227.md" | 2 +- ...345\220\253\346\211\213\347\273\255\350\264\271.md" | 2 +- ...346\200\201\350\247\204\345\210\222\357\274\211.md" | 2 +- ...345\242\236\347\232\204\346\225\260\345\255\227.md" | 2 +- ...345\255\227\346\257\215\345\214\272\351\227\264.md" | 2 +- ...346\252\254\346\260\264\346\211\276\351\233\266.md" | 2 +- ...346\216\247\344\272\214\345\217\211\346\240\221.md" | 2 +- ...347\273\204\347\232\204\345\271\263\346\226\271.md" | 4 ++-- ...347\232\204\346\225\260\347\273\204\345\222\214.md" | 2 +- ...345\255\230\345\234\250\350\267\257\345\276\204.md" | 2 +- ...47.\345\217\202\344\274\232dijkstra\345\240\206.md" | 2 +- ...202\344\274\232dijkstra\346\234\264\347\264\240.md" | 2 +- .../0053.\345\257\273\345\256\235-Kruskal.md" | 2 +- .../kamacoder/0053.\345\257\273\345\256\235-prim.md" | 2 +- ...4\247\347\211\251\350\277\220\350\276\223I-SPFA.md" | 2 +- ...50\264\247\347\211\251\350\277\220\350\276\223I.md" | 2 +- ...0\264\247\347\211\251\350\277\220\350\276\223II.md" | 2 +- ...\264\247\347\211\251\350\277\220\350\276\223III.md" | 2 +- ...346\230\216\351\200\233\345\205\254\345\233\255.md" | 2 +- ...345\217\257\350\276\276\350\267\257\345\276\204.md" | 2 +- ...346\225\260\351\207\217\345\271\277\346\220\234.md" | 2 +- ...346\225\260\351\207\217\346\267\261\346\220\234.md" | 2 +- ...346\234\200\345\244\247\351\235\242\347\247\257.md" | 2 +- ...347\232\204\346\200\273\351\235\242\347\247\257.md" | 2 +- ...346\262\211\346\262\241\345\255\244\345\262\233.md" | 2 +- ...346\260\264\346\265\201\351\227\256\351\242\230.md" | 2 +- ...346\234\200\345\244\247\345\262\233\345\261\277.md" | 2 +- ...345\205\250\345\217\257\350\276\276\346\200\247.md" | 2 +- ...345\261\277\347\232\204\345\221\250\351\225\277.md" | 2 +- ...345\234\250\347\232\204\350\267\257\345\276\204.md" | 2 +- ...345\206\227\344\275\231\350\277\236\346\216\245.md" | 2 +- ...5\206\227\344\275\231\350\277\236\346\216\245II.md" | 2 +- ...347\254\246\344\270\262\346\216\245\351\276\231.md" | 2 +- ...350\275\257\344\273\266\346\236\204\345\273\272.md" | 2 +- ...43\253\347\232\204\346\224\273\345\207\273astar.md" | 2 +- ...351\200\222\345\275\222\351\201\215\345\216\206.md" | 2 +- ...237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- ...345\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +- 87 files changed, 92 insertions(+), 92 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 044eac1424..e982ae129b 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -341,7 +341,7 @@ impl Solution { } ``` -### Javascript: +### JavaScript: ```javascript var twoSum = function (nums, target) { diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index d310f4153c..f2f5cdd13f 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -275,7 +275,7 @@ def is_valid(strs) end ``` -### Javascript: +### JavaScript: ```javascript var isValid = function (s) { diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index 23dba84b4f..305bb7ccba 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -286,7 +286,7 @@ func swapPairs(head *ListNode) *ListNode { } ``` -### Javascript: +### JavaScript: ```javascript var swapPairs = function (head) { diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index 70d52e9e3a..6a9f69bd07 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -460,7 +460,7 @@ func isvalid(row, col int, k byte, board [][]byte) bool { -### Javascript +### JavaScript ```Javascript var solveSudoku = function(board) { diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 3eeec2689c..8919d39367 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -374,7 +374,7 @@ func max(a, b int) int { } ``` -### Javascript +### JavaScript ```Javascript var jump = function(nums) { diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index 1ef80a142d..4302bdbfcb 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -271,7 +271,7 @@ func dfs(nums []int, cur int) { } ``` -### Javascript +### JavaScript ```js diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index 56006a7744..08e3c616a2 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -283,7 +283,7 @@ func dfs(nums []int, cur int) { } ``` -### Javascript +### JavaScript ```javascript var permuteUnique = function (nums) { diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 6ced679cd4..38fc07e790 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -451,7 +451,7 @@ func isValid(n, row, col int, chessboard [][]string) bool { ``` -### Javascript +### JavaScript ```Javascript /** * @param {number} n diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 1c7ff0cd55..69a7d19088 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -290,7 +290,7 @@ pub fn max_sub_array(nums: Vec) -> i32 { } ``` -### Javascript: +### JavaScript: ```Javascript var maxSubArray = function(nums) { diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 6c0bad4058..3b7afb9099 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -260,7 +260,7 @@ class Solution { } ``` -### Javascript +### JavaScript ``` /** * @param {number[][]} matrix diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index 01fd951384..cbbf5980cb 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -166,7 +166,7 @@ func max(a, b int ) int { } ``` -### Javascript +### JavaScript ```Javascript var canJump = function(nums) { diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 134a9028a6..538be69336 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -215,7 +215,7 @@ func max56(a, b int) int { ``` -### Javascript +### JavaScript ```javascript var merge = function (intervals) { intervals.sort((a, b) => a[0] - b[0]); diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 5b46caa9e3..7025135ade 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -411,7 +411,7 @@ func uniquePaths(m int, n int) int { } ``` -### Javascript +### JavaScript ```Javascript var uniquePaths = function(m, n) { diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index daf3d8c5a1..13923abeca 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -465,7 +465,7 @@ func uniquePathsWithObstacles(obstacleGrid [][]int) int { } ``` -### Javascript +### JavaScript ```Javascript var uniquePathsWithObstacles = function(obstacleGrid) { diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 1d0b192fe4..a2f664a495 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -327,7 +327,7 @@ func climbStairs(n int) int { return dp[n] } ``` -### Javascript +### JavaScript ```Javascript var climbStairs = function(n) { // dp[i] 为第 i 阶楼梯有多少种方法爬到楼顶 diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index b8de8bdcc7..0da3bf5093 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -313,7 +313,7 @@ func Min(args ...int) int { } ``` -### Javascript: +### JavaScript: ```javascript const minDistance = (word1, word2) => { diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 3a271ff8b4..c4be5a3804 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -468,7 +468,7 @@ func dfs(n int, k int, start int) { } ``` -### Javascript +### JavaScript 未剪枝: ```js diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 723d99a18b..0c368b41ed 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -246,7 +246,7 @@ func dfs(nums []int, start int) { } ``` -### Javascript +### JavaScript ```Javascript var subsets = function(nums) { diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 939ef369c9..811d3cc005 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -376,7 +376,7 @@ func dfs(nums []int, start int) { ``` -### Javascript +### JavaScript ```Javascript diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index e0e773100e..25d79aff74 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -221,7 +221,7 @@ func numTrees(n int)int{ } ``` -### Javascript +### JavaScript ```Javascript const numTrees =(n) => { diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index cad324148d..98e1e98a6e 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -356,7 +356,7 @@ func levelOrder(root *TreeNode) (res [][]int) { } ``` -#### Javascript: +#### JavaScript: ```javascript var levelOrder = function(root) { @@ -759,7 +759,7 @@ func levelOrderBottom(root *TreeNode) [][]int { } ``` -#### Javascript: +#### JavaScript: ```javascript var levelOrderBottom = function (root) { @@ -1101,7 +1101,7 @@ func rightSideView(root *TreeNode) []int { } ``` -#### Javascript: +#### JavaScript: ```javascript var rightSideView = function(root) { @@ -1421,7 +1421,7 @@ func averageOfLevels(root *TreeNode) []float64 { } ``` -#### Javascript: +#### JavaScript: ```javascript var averageOfLevels = function(root) { @@ -2109,7 +2109,7 @@ func largestValues(root *TreeNode) []int { } ``` -#### Javascript: +#### JavaScript: ```javascript var largestValues = function (root) { diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 607e195b42..fdc9009603 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -604,7 +604,7 @@ func maxDepth(root *Node) int { } ``` -### Javascript : +### JavaScript : 104.二叉树的最大深度 diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index e6ccc6ae27..b97013e67a 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -830,7 +830,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { } ``` -### Javascript +### JavaScript 0112.路径总和 diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 8682b88d13..cf24c4c180 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -265,7 +265,7 @@ func numDistinct(s string, t string) int { } ``` -### Javascript: +### JavaScript: ```javascript const numDistinct = (s, t) => { diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index e255723d2a..6663a66d12 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -249,7 +249,7 @@ func max(a, b int) int { } ``` -### Javascript: +### JavaScript: 贪心 diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index 5cf50b3e56..0248760da6 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -396,7 +396,7 @@ func canCompleteCircuit(gas []int, cost []int) int { } ``` -### Javascript +### JavaScript 暴力: ```js var canCompleteCircuit = function(gas, cost) { diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index 6805857e8c..177d23e723 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -234,7 +234,7 @@ func findMax(num1 int, num2 int) int { } ``` -### Javascript +### JavaScript ```Javascript var candy = function(ratings) { let candys = new Array(ratings.length).fill(1) diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index e399ac9006..c6d89976d0 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -266,7 +266,7 @@ var minSubArrayLen = function(target, nums) { }; ``` -### Typescript: +### TypeScript: ```typescript function minSubArrayLen(target: number, nums: number[]): number { diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 23bf615b4a..9c71778f0f 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -401,7 +401,7 @@ func maxSlidingWindow(nums []int, k int) []int { } ``` -### Javascript: +### JavaScript: ```javascript /** diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index 570632bdf6..dc5a7e9ec9 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -346,7 +346,7 @@ func min(a, b int) int { } ``` -### Javascript: +### JavaScript: ```Javascript // 先遍历物品,再遍历背包 diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index f1a146b71c..442938c06f 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -248,7 +248,7 @@ func lengthOfLIS(nums []int ) int { } ``` -### Javascript: +### JavaScript: ```javascript const lengthOfLIS = (nums) => { diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 4913b8bd57..480b086605 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -393,7 +393,7 @@ func max(a, b int) int { -### Javascript: +### JavaScript: > 不同的状态定义 感觉更容易理解些 ```javascript 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 0ed5cf688e..e55e20bedf 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" @@ -427,7 +427,7 @@ impl Solution { } ``` -### Javascript: +### JavaScript: ```javascript // 遍历物品 diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 1d9c524b13..ed8149d08e 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -535,7 +535,7 @@ func findItinerary(tickets [][]string) []string { } ``` -### Javascript +### JavaScript ```Javascript diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 3ba23e522c..5d0110f67e 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -385,7 +385,7 @@ func integerBreak(n int) int { } ``` -### Javascript +### JavaScript ```Javascript var integerBreak = function(n) { let dp = new Array(n + 1).fill(0) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 9e6714ce9d..9cf1ed8c35 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -466,7 +466,7 @@ func max(a, b int) int { } ``` -### Javascript +### JavaScript **贪心** diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index e96f8dc646..ba8546c662 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -254,7 +254,7 @@ func combinationSum4(nums []int, target int) int { } ``` -### Javascript: +### JavaScript: ```javascript const combinationSum4 = (nums, target) => { diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index d92a7f3f58..d6fc415b68 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -270,7 +270,7 @@ func reconstructQueue(people [][]int) [][]int { } ``` -### Javascript +### JavaScript ```Javascript var reconstructQueue = function(people) { diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 209616024e..d6321315d7 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -311,7 +311,7 @@ func min(a, b int) int { } ``` -### Javascript +### JavaScript - 按右边界排序 ```Javascript var eraseOverlapIntervals = function(intervals) { diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 318c3035ce..9b9eef1d53 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -220,7 +220,7 @@ func min(a, b int) int { } ``` -### Javascript +### JavaScript ```Javascript var findMinArrowShots = function(points) { points.sort((a, b) => { diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 22dd7570c8..5e8fb73094 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -278,7 +278,7 @@ pub fn find_content_children(mut children: Vec, mut cookies: Vec) -> i } ``` -### Javascript +### JavaScript ```js var findContentChildren = function (g, s) { diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 72d622437a..e514e7292b 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -362,7 +362,7 @@ func max(a,b int) int { } ``` -### Javascript +### JavaScript ```javascript const findMaxForm = (strs, m, n) => { const dp = Array.from(Array(m+1), () => Array(n+1).fill(0)); diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 8f642a5fc9..7832095a10 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -375,7 +375,7 @@ func dfs(nums []int, start int) { } ``` -### Javascript +### JavaScript ```Javascript diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 0872460907..206fdf89ea 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -791,7 +791,7 @@ func abs(x int) int { } ``` -### Javascript +### JavaScript ```javascript const findTargetSumWays = (nums, target) => { diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 21b07802f1..1c4127fcd2 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -292,7 +292,7 @@ func fib(n int) int { return c } ``` -### Javascript +### JavaScript 解法一 ```Javascript var fib = function(n) { diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index af36b94d29..166310aaff 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -224,7 +224,7 @@ func longestPalindromeSubseq(s string) int { } ``` -### Javascript: +### JavaScript: ```javascript const longestPalindromeSubseq = (s) => { diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 7023121292..0d35fb7cdf 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -349,7 +349,7 @@ impl Solution { } ``` -### Javascript: +### JavaScript: ```javascript const change = (amount, coins) => { diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 14a55631e6..8e63e82cb8 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -287,7 +287,7 @@ func min(a, b int) int { return b } ``` -### Javascript: +### JavaScript: ```javascript // 方法一 diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 89e4ad1166..6f0fea4a8d 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -465,7 +465,7 @@ func countSubstrings(s string) int { } ``` -### Javascript: +### JavaScript: > 动态规划 ```javascript diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 57a38404b2..8b967092bc 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -359,7 +359,7 @@ impl Solution { ``` -### Javascript: +### JavaScript: > 动态规划: ```javascript diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" index 88b03d9d76..c87607a23c 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" @@ -243,7 +243,7 @@ func maxProfit(prices []int, fee int) int { return res } ``` -### Javascript +### JavaScript ```Javascript // 贪心思路 var maxProfit = function(prices, fee) { diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index b0e8b141f5..3f5f929ce2 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -226,7 +226,7 @@ func max(a, b int) int { } ``` -### Javascript: +### JavaScript: ```javascript const maxProfit = (prices,fee) => { diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index 3d46d5ad04..8de22fe2f4 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -292,7 +292,7 @@ func monotoneIncreasingDigits(N int) int { } ``` -### Javascript +### JavaScript ```Javascript var monotoneIncreasingDigits = function(n) { n = n.toString() diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 5218692735..19d862db14 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -312,7 +312,7 @@ func max(a, b int) int { } ``` -### Javascript +### JavaScript ```Javascript var partitionLabels = function(s) { let hash = {} diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index 804ff13c12..b7887d456e 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -226,7 +226,7 @@ func lemonadeChange(bills []int) bool { } ``` -### Javascript +### JavaScript ```Javascript var lemonadeChange = function(bills) { let fiveCount = 0 diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index d59496c87d..327c54f72a 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -536,7 +536,7 @@ func min(a, b int) int { ``` -### Javascript +### JavaScript ```Javascript var minCameraCover = function(root) { diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index effa905541..3f16951df7 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -263,7 +263,7 @@ impl Solution { } } ``` -### Javascript: +### JavaScript: ```Javascript /** @@ -289,7 +289,7 @@ var sortedSquares = function(nums) { }; ``` -### Typescript: +### TypeScript: 双指针法: diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 4e04960098..1a7817775a 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -207,7 +207,7 @@ func largestSumAfterKNegations(nums []int, K int) int { ``` -### Javascript +### JavaScript ```Javascript var largestSumAfterKNegations = function(nums, k) { diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 2c5901ae22..93e9b66365 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -213,7 +213,7 @@ class Solution: return find(source) == find(destination) ``` -### Javascript: +### JavaScript: Javascript 并查集解法如下: diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index 9905a420c8..75c12f8a96 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -911,7 +911,7 @@ func main() { ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 465ad16dfc..e794690a5c 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -867,7 +867,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ```js function dijkstra(grid, start, end) { diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 6a22798551..861efe6806 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -547,7 +547,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ```js function kruskal(v, edges) { diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index a8dad4cbfc..bbcea0261b 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -692,7 +692,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ```js function prim(v, edges) { const grid = Array.from({ length: v + 1 }, () => new Array(v + 1).fill(10001)); // Fixed grid initialization diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index b3f42bf800..6881f072ad 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -462,7 +462,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 3737fe0149..b20d25ef2e 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -483,7 +483,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index edfd52e02d..3a3066890f 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -392,7 +392,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index dacd23d11d..8e8152634e 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -708,7 +708,7 @@ public class Main { ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index e8d92cc29f..8b3078fc01 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -492,7 +492,7 @@ if __name__ == '__main__': ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index db35524ffd..734435f92a 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -726,7 +726,7 @@ func main() { ### Rust -### Javascript +### JavaScript #### 邻接矩阵写法 diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 3047575391..68b475a02d 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -360,7 +360,7 @@ func main() { ### Rust -### Javascript +### JavaScript ```javascript const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 6ac7ba3b62..33d0c9dc75 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -328,7 +328,7 @@ if __name__ == '__main__': ### Rust -### Javascript +### JavaScript ```javascript const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 51bfc57fd1..a3e2a99e9e 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -390,7 +390,7 @@ func main() { ### Rust -### Javascript +### JavaScript ```javascript // 广搜版 diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 26c92c07a6..af5ce089d5 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -467,7 +467,7 @@ func main() { ### Rust -### Javascript +### JavaScript #### 深搜版 diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 4eabc5162a..a7e28df52f 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -322,7 +322,7 @@ for row in g: ### Rust -### Javascript +### JavaScript #### 深搜版 diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 9a34bf094f..a09bf089ad 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -416,7 +416,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript #### 深搜 diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 3dcbfee6db..ffb6cae34d 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -530,7 +530,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ```javascript const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 838b021245..3bfcfb40df 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -489,7 +489,7 @@ func main() { ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 235d9445d1..91a1a438c6 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -343,7 +343,7 @@ func parseLine(line string, count int) []int { ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 09ad0d5e3d..779907c8fd 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -340,7 +340,7 @@ func main() { ### Rust -### Javascript +### JavaScript ```java const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 18a86ad6d7..efbbb6d203 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -221,7 +221,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ```javascript const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 2bd4eac6e2..4365c27f43 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -441,7 +441,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ```javascript const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 308e50b7f3..6541faba20 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -254,7 +254,7 @@ if __name__=='__main__': ### Rust -### Javascript +### JavaScript ```javascript const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index 348187d6f1..58c1776304 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -449,7 +449,7 @@ if __name__ == "__main__": ### Rust -### Javascript +### JavaScript ```javascript const r1 = require('readline').createInterface({ input: process.stdin }); diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 2d0481ecec..80fc033bb7 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -373,7 +373,7 @@ for _ in range(n): ### Rust -### Javascript +### JavaScript ### TypeScript diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index f2a97f4de0..8f61b8c69a 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -283,7 +283,7 @@ func postorderTraversal(root *TreeNode) (res []int) { } ``` -### Javascript: +### JavaScript: 前序遍历: ```Javascript diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 2747f179d1..e7ad8d1dab 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -492,7 +492,7 @@ func max(x, y int) int { ``` -### Javascript +### JavaScript ```js const readline = require('readline').createInterface({ diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 3a50ee7bbf..ae1e9e0b6d 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -392,7 +392,7 @@ func main() { fmt.Println(test_CompletePack2(weight, price, 4)) } ``` -### Javascript: +### JavaScript: ```Javascript // 先遍历物品,再遍历背包容量 From 7683e173384162dd8a95b2a5412b56a230a2f858 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 17 Oct 2024 13:28:39 +0800 Subject: [PATCH 1367/1533] =?UTF-8?q?1143.=E6=9C=80=E9=95=BF=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97=E6=96=87=E5=AD=97=E5=8B=98?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 25f3283864..6d05ccf3f8 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -80,7 +80,7 @@ if (text1[i - 1] == text2[j - 1]) { 先看看dp[i][0]应该是多少呢? -test1[0, i-1]和空串的最长公共子序列自然是0,所以dp[i][0] = 0; +text1[0, i-1]和空串的最长公共子序列自然是0,所以dp[i][0] = 0; 同理dp[0][j]也是0。 From 09fef9f8f8172418eaeeab631a6d10e774080a12 Mon Sep 17 00:00:00 2001 From: markwang Date: Thu, 17 Oct 2024 15:58:28 +0800 Subject: [PATCH 1368/1533] =?UTF-8?q?1035.=E4=B8=8D=E7=9B=B8=E4=BA=A4?= =?UTF-8?q?=E7=9A=84=E7=BA=BF=E9=A2=98=E7=9B=AE=E6=8F=8F=E8=BF=B0=E5=8F=8A?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=90=8D=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\344\272\244\347\232\204\347\272\277.md" | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 8ee52c5d48..5164e1f7ff 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -8,11 +8,16 @@ [力扣题目链接](https://leetcode.cn/problems/uncrossed-lines/) -我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数。 +在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。 -现在,我们可以绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == B[j],且我们绘制的直线不与任何其他连线(非水平线)相交。 +现在,可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,这些直线需要同时满足: -以这种方法绘制线条,并返回我们可以绘制的最大连线数。 +* nums1[i] == nums2[j] +* 且绘制的直线不与任何其他连线(非水平线)相交。 + +请注意,连线即使在端点也不能相交:每个数字只能属于一条连线。 + +以这种方法绘制线条,并返回可以绘制的最大连线数。 ![1035.不相交的线](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032116363533.png) @@ -26,16 +31,16 @@ 相信不少录友看到这道题目都没啥思路,我们来逐步分析一下。 -绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == B[j],且直线不能相交! +绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,只要 nums1[i] == nums2[j],且直线不能相交! -直线不能相交,这就是说明在字符串A中 找到一个与字符串B相同的子序列,且这个子序列不能改变相对顺序,只要相对顺序不改变,链接相同数字的直线就不会相交。 +直线不能相交,这就是说明在字符串nums1中 找到一个与字符串nums2相同的子序列,且这个子序列不能改变相对顺序,只要相对顺序不改变,连接相同数字的直线就不会相交。 -拿示例一A = [1,4,2], B = [1,2,4]为例,相交情况如图: +拿示例一nums1 = [1,4,2], nums2 = [1,2,4]为例,相交情况如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914145158.png) -其实也就是说A和B的最长公共子序列是[1,4],长度为2。 这个公共子序列指的是相对顺序不变(即数字4在字符串A中数字1的后面,那么数字4也应该在字符串B数字1的后面) +其实也就是说nums1和nums2的最长公共子序列是[1,4],长度为2。 这个公共子序列指的是相对顺序不变(即数字4在字符串nums1中数字1的后面,那么数字4也应该在字符串nums2数字1的后面) 这么分析完之后,大家可以发现:**本题说是求绘制的最大连线数,其实就是求两个字符串的最长公共子序列的长度!** @@ -52,18 +57,18 @@ ```CPP class Solution { public: - int maxUncrossedLines(vector& A, vector& B) { - vector> dp(A.size() + 1, vector(B.size() + 1, 0)); - for (int i = 1; i <= A.size(); i++) { - for (int j = 1; j <= B.size(); j++) { - if (A[i - 1] == B[j - 1]) { + int maxUncrossedLines(vector& nums1, vector& nums2) { + vector> dp(nums1.size() + 1, vector(nums2.size() + 1, 0)); + for (int i = 1; i <= nums1.size(); i++) { + for (int j = 1; j <= nums2.size(); j++) { + if (nums1[i - 1] == nums2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } - return dp[A.size()][B.size()]; + return dp[nums1.size()][nums2.size()]; } }; ``` @@ -110,11 +115,11 @@ public: ```python class Solution: - def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: - dp = [[0] * (len(B)+1) for _ in range(len(A)+1)] - for i in range(1, len(A)+1): - for j in range(1, len(B)+1): - if A[i-1] == B[j-1]: + def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int: + dp = [[0] * (len(nums2)+1) for _ in range(len(nums1)+1)] + for i in range(1, len(nums1)+1): + for j in range(1, len(nums2)+1): + if nums1[i-1] == nums2[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) @@ -124,23 +129,22 @@ class Solution: ### Go: ```go -func maxUncrossedLines(A []int, B []int) int { - m, n := len(A), len(B) - dp := make([][]int, m+1) +func maxUncrossedLines(nums1 []int, nums2 []int) int { + dp := make([][]int, len(nums1) + 1) for i := range dp { - dp[i] = make([]int, n+1) + dp[i] = make([]int, len(nums2) + 1) } - for i := 1; i <= len(A); i++ { - for j := 1; j <= len(B); j++ { - if (A[i - 1] == B[j - 1]) { + for i := 1; i <= len(nums1); i++ { + for j := 1; j <= len(nums2); j++ { + if (nums1[i - 1] == nums2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1 } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) } } } - return dp[m][n] + return dp[len(nums1)][len(nums2)] } From 048ec95f07174e991fb0309200d5d14334957238 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Mon, 21 Oct 2024 13:10:23 +0800 Subject: [PATCH 1369/1533] =?UTF-8?q?0232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md=20=E6=97=B6=E9=97=B4=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=BA=94=E4=B8=BAO(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" | 2 +- ...\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 7017107bf8..657567cfdf 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -113,7 +113,7 @@ public: ``` -* 时间复杂度: push和empty为O(1), pop和peek为O(n) +* 时间复杂度: 都为O(1)。pop和peek看起来像O(n),实际上一个循环n会被使用n次,最后还是O(1)。 * 空间复杂度: O(n) diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" index a77490cdfe..409e80ab98 100644 --- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" +++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" @@ -77,7 +77,7 @@ for (int i = 0; i < array.size(); i++) { 时间复杂度可以做到O(n^2),但还是比较费时的,因为不好做剪枝操作。 -所以这道题目使用双指针法才是最为合适的,用双指针做这道题目才能就能真正体会到,**通过前后两个指针不算向中间逼近,在一个for循环下完成两个for循环的工作。** +所以这道题目使用双指针法才是最为合适的,用双指针做这道题目才能就能真正体会到,**通过前后两个指针不断向中间逼近,在一个for循环下完成两个for循环的工作。** 只用双指针法时间复杂度为O(n^2),但比哈希法的O(n^2)效率高得多,哈希法在使用两层for循环的时候,能做的剪枝操作很有限。 From e0c1e2e31784dfb13ea8f710ff216bcdccf5d3bf Mon Sep 17 00:00:00 2001 From: zhangrunzhe Date: Mon, 21 Oct 2024 19:02:33 +0800 Subject: [PATCH 1370/1533] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=9802.07.?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\347\233\270\344\272\244.md" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index b0f2e8eaea..48944b5e4e 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -535,6 +535,45 @@ public ListNode GetIntersectionNode(ListNode headA, ListNode headB) } ``` +### Swift: +```swift +func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? { + var lenA = 0 + var lenB = 0 + var nodeA = headA + var nodeB = headB + // 计算链表A和链表B的长度 + while nodeA != nil { + lenA += 1 + nodeA = nodeA?.next + } + while nodeB != nil { + lenB += 1 + nodeB = nodeB?.next + } + // 重置指针 + nodeA = headA + nodeB = headB + // 如果链表A更长,让它先走lenA-lenB步 + if lenA > lenB { + for _ in 0..<(lenA - lenB) { + nodeA = nodeA?.next + } + } else if lenB > lenA { + // 如果链表B更长,让它先走lenB-lenA步 + for _ in 0..<(lenB - lenA) { + nodeB = nodeB?.next + } + } + // 同时遍历两个链表,寻找交点 + while nodeA !== nodeB { + nodeA = nodeA?.next + nodeB = nodeB?.next + } + return nodeA +} +``` +

From fba159b1356bf4fe55658c05090b1b3ae7956e10 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Tue, 22 Oct 2024 09:20:21 +0800 Subject: [PATCH 1371/1533] =?UTF-8?q?0239.=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20=E5=8A=A0=E5=85=A5=20?= =?UTF-8?q?Python=20=E7=89=88=E6=9C=AC=E8=A7=A3=E6=B3=95=E4=BA=8C=EF=BC=9A?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E7=94=A8=E5=8D=95=E8=B0=83=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...43\346\234\200\345\244\247\345\200\274.md" | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 23bf615b4a..aae1b4d25a 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -299,7 +299,7 @@ class Solution { ``` ### Python: - +#### 解法一:使用自定义的单调队列类 ```python from collections import deque @@ -339,6 +339,36 @@ class Solution: return result ``` +#### 解法二:直接用单调队列 +```python +from collections import deque + + +class Solution: + def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: + max_list = [] # 结果集合 + kept_nums = deque() # 单调队列 + + for i in range(len(nums)): + update_kept_nums(kept_nums, nums[i]) # 右侧新元素加入 + + if i >= k and nums[i - k] == kept_nums[0]: # 左侧旧元素如果等于单调队列头元素,需要移除头元素 + kept_nums.popleft() + + if i >= k - 1: + max_list.append(kept_nums[0]) + + return max_list + + +def update_kept_nums(kept_nums, num): # num 是新加入的元素 + # 所有小于新元素的队列尾部元素,在新元素出现后,都是没有价值的,都需要被移除 + while kept_nums and num > kept_nums[-1]: + kept_nums.pop() + + kept_nums.append(num) +``` + ### Go: ```go From e5ebc38dddeb6a356f892d7e3d930f9f257790b1 Mon Sep 17 00:00:00 2001 From: Kristy-an Date: Mon, 21 Oct 2024 21:12:06 -0500 Subject: [PATCH 1372/1533] fix python block code highlight problem --- ...70\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 5751bb9171..056b7096c0 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -185,7 +185,7 @@ class Solution: > 版本二:针对版本一的优化 -```python3 +```python class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: res = [-1] * len(nums) From 97c4d1baec4789f7fe8aa656b459fc63509548a7 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Tue, 22 Oct 2024 10:22:27 +0800 Subject: [PATCH 1373/1533] =?UTF-8?q?0239.=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20=E5=8A=A0=E5=85=A5=20?= =?UTF-8?q?Python=20=E7=89=88=E6=9C=AC=E6=96=B0=E8=A7=A3=E6=B3=95=EF=BC=9A?= =?UTF-8?q?=E7=94=A8"=E5=A0=86=E6=8E=92=E5=BA=8F"=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...43\346\234\200\345\244\247\345\200\274.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 23bf615b4a..2db1942afb 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -339,6 +339,32 @@ class Solution: return result ``` +#### 新解法:用"堆排序"实现 +* 时间复杂度:`O(n * log(n))`, 比`单调队列`解法要慢。 + +```python +import heapq + + +class Solution: + def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: + results = [] + num_index_list = [] # 将用“堆排序”对它进行排序,元素为 (num, index) 元组 + + for i in range(len(nums)): + # 把 nums[i] 值取负数,最大的就到最小,合乎 Python 堆排序从小到大的规则。 + # 还要把 index (i) 存入,因为通过 i 可知道对应的 num 何时不能再被使用(num 已经处在左侧窗口的更左边) + heapq.heappush(num_index_list, (-nums[i], i)) + # num_index_list[0]是最小值所在 tuple;'<= i - k' 表示 num 已经处在左侧窗口的更左边 + while num_index_list[0][1] <= i - k: # while 表示所有过气 num 都要丢弃 + heapq.heappop(num_index_list) # 丢弃最小值 + + if i >= k - 1: + results.append(-num_index_list[0][0]) # 第一个就是最小值,负最小值就是最大值,加入结果集 + + return results +``` + ### Go: ```go From ad93277357f14ab7a7436c8ee92c74e65fb37fd3 Mon Sep 17 00:00:00 2001 From: Kuxry Date: Tue, 22 Oct 2024 11:46:28 +0900 Subject: [PATCH 1374/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20Ruby=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index f6b5ef6dbb..c2f1d44910 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -737,7 +737,45 @@ public class Solution } } ``` +### Ruby# + +```ruby +# 定义链表节点 +class ListNode + attr_accessor :val, :next + def initialize(val = 0, _next = nil) + @val = val + @next = _next + end +end + +# 删除链表中值为 val 的节点 +def remove_elements(head, val) + # 创建一个虚拟头节点,这样可以简化删除头节点的处理 + # 虚拟头节点的值为 0,指向当前链表的头节点 + dummy = ListNode.new(0) + dummy.next = head + + # 初始化当前节点为虚拟头节点 + current = dummy + + # 遍历链表,直到当前节点的下一个节点为空 + while current.next + # 如果当前节点的下一个节点的值等于 val + if current.next.val == val + # 跳过该节点,即将当前节点的 next 指向下一个节点的 next + current.next = current.next.next + else + # 否则继续遍历,当前节点向前移动 + current = current.next + end + end + + # 返回删除 val 后的新链表的头节点,虚拟头节点的 next 就是新的头节点 + dummy.next +end +```

From 6e6093752fb67bbcd9d67211013c1d6ac740e0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lane=20Zhang=20=28=E5=BC=A0=E5=81=A5=29?= Date: Tue, 22 Oct 2024 11:00:13 +0800 Subject: [PATCH 1375/1533] =?UTF-8?q?Update=200239.=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change `O(n * log(n))` to `O(n log(n))` --- ...\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 2db1942afb..95c38acb2f 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -340,7 +340,7 @@ class Solution: ``` #### 新解法:用"堆排序"实现 -* 时间复杂度:`O(n * log(n))`, 比`单调队列`解法要慢。 +* 时间复杂度:`O(n log(n))`, 比`单调队列`解法要慢。 ```python import heapq From 82468b6f2ae5e399306e3deedb0af540671b5417 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 22 Oct 2024 11:31:15 +0800 Subject: [PATCH 1376/1533] =?UTF-8?q?583.=E4=B8=A4=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=E4=BA=8C=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\351\231\244\346\223\215\344\275\234.md" | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 14a55631e6..57bd3abea6 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -33,7 +33,7 @@ dp[i][j]:以i-1为结尾的字符串word1,和以j-1位结尾的字符串word2,想要达到相等,所需要删除元素的最少次数。 -这里dp数组的定义有点点绕,大家要撸清思路。 +这里dp数组的定义有点点绕,大家要理清思路。 2. 确定递推公式 @@ -255,6 +255,8 @@ class Solution(object): ``` ### Go: +动态规划一 + ```go func minDistance(word1 string, word2 string) int { dp := make([][]int, len(word1)+1) @@ -287,6 +289,35 @@ func min(a, b int) int { return b } ``` + +动态规划二 + +```go +func minDistance(word1 string, word2 string) int { + dp := make([][]int, len(word1) + 1) + for i := range dp { + dp[i] = make([]int, len(word2) + 1) + } + for i := 1; i <= len(word1); i++ { + for j := 1; j <= len(word2); j++ { + if word1[i-1] == word2[j-1] { + dp[i][j] = dp[i-1][j-1] + 1 + } else { + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + } + } + } + return len(word1) + len(word2) - dp[len(word1)][len(word2)] * 2 +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + ### Javascript: ```javascript From dcbd5b4674c763dd2e156318cee383a79861f954 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Wed, 23 Oct 2024 10:07:56 +0800 Subject: [PATCH 1377/1533] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md=20=E5=8E=BB=E6=8E=89=20?= =?UTF-8?q?Python=20=E7=89=88=E6=9C=AC=E4=B8=AD=E6=97=A0=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\204\350\277\255\344\273\243\351\201\215\345\216\206.md" | 4 +--- ...70\216\351\230\237\345\210\227\346\200\273\347\273\223.md" | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 5f59c38897..ba63d06b89 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -262,8 +262,6 @@ class Solution: # 中序遍历-迭代-LC94_二叉树的中序遍历 class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: - if not root: - return [] stack = [] # 不能提前将root结点加入stack中 result = [] cur = root @@ -280,7 +278,7 @@ class Solution: cur = cur.right return result ``` - ```python +```python # 后序遍历-迭代-LC145_二叉树的后序遍历 class Solution: diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index 113f4a06a3..df022c779b 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -107,7 +107,7 @@ cd a/b/c/../../ 设计单调队列的时候,pop,和push操作要保持如下规则: 1. pop(value):如果窗口移除的元素value等于单调队列的出口元素,那么队列弹出元素,否则不用任何操作 -2. push(value):如果push的元素value大于入口元素的数值,那么就将队列出口的元素弹出,直到push元素的数值小于等于队列入口元素的数值为止 +2. push(value):如果push的元素value大于入口元素的数值,那么就将队列入口的元素弹出,直到push元素的数值小于等于队列入口元素的数值为止 保持如上规则,每次窗口移动的时候,只要问que.front()就可以返回当前窗口的最大值。 From a36358e06868764fab05b2821497991aded21256 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Wed, 23 Oct 2024 10:22:58 +0800 Subject: [PATCH 1378/1533] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md=20=E5=8A=A0=E5=85=A5=20?= =?UTF-8?q?Python=20=E7=89=88=E6=9C=AC=E7=9A=84=E5=90=8E=E5=BA=8F=E9=81=8D?= =?UTF-8?q?=E5=8E=86=E6=96=B0=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\344\273\243\351\201\215\345\216\206.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 5f59c38897..00d24692d7 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -303,6 +303,44 @@ class Solution: return result[::-1] ``` +#### Python 后序遍历的迭代新解法: +* 本解法不同于前文介绍的`逆转前序遍历调整后的结果`,而是采用了对每个结点直接处理。 + +```python +class Solution: + def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + values = [] + stack = [] + popped_nodes = set() # 记录值已经被收割了的 nodes + current = root + + while current or stack: + if current: # 一次处理完一个结点和他的左右儿子结点,不处理孙子结点,孙子结点由左右儿子等会分别处理。 + stack.append(current) # 入栈自己 + + if current.right: + stack.append(current.right) # 入栈右儿子 + + if current.left: # 因为栈是后进先出,后序是‘左右中’,所以后加左儿子 + stack.append(current.left) # 入栈左儿子 + + current = None # 会导致后面A处出栈 + continue + + node = stack.pop() # A处,出的是左儿子,如果无左儿子,出的就是右儿子,如果连右儿子也没有,出的就是自己了。 + + # 如果 node 是叶子结点,就可以收割了;如果左右儿子都已经被收割了,也可以收割 + if (node.left is None or node.left in popped_nodes) and \ + (node.right is None or node.right in popped_nodes): + popped_nodes.add(node) + values.append(node.val) + continue + + current = node # 不符合收割条件,说明 node 下还有未入栈的儿子,就去入栈 + + return values +``` + ### Go: > 迭代法前序遍历 From bf540436f290e509fa8ae5d77cb75f250c154866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lane=20Zhang=20=28=E5=BC=A0=E5=81=A5=29?= Date: Wed, 23 Oct 2024 10:33:48 +0800 Subject: [PATCH 1379/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\344\273\243\351\201\215\345\216\206.md" | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 00d24692d7..3b4b30fbae 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -240,14 +240,14 @@ class Solution { # 前序遍历-迭代-LC144_二叉树的前序遍历 class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: - # 根结点为空则返回空列表 + # 根节点为空则返回空列表 if not root: return [] stack = [root] result = [] while stack: node = stack.pop() - # 中结点先处理 + # 中节点先处理 result.append(node.val) # 右孩子先入栈 if node.right: @@ -264,19 +264,19 @@ class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: if not root: return [] - stack = [] # 不能提前将root结点加入stack中 + stack = [] # 不能提前将root节点加入stack中 result = [] cur = root while cur or stack: - # 先迭代访问最底层的左子树结点 + # 先迭代访问最底层的左子树节点 if cur: stack.append(cur) cur = cur.left - # 到达最左结点后处理栈顶结点 + # 到达最左节点后处理栈顶节点 else: cur = stack.pop() result.append(cur.val) - # 取栈顶元素右结点 + # 取栈顶元素右节点 cur = cur.right return result ``` @@ -291,7 +291,7 @@ class Solution: result = [] while stack: node = stack.pop() - # 中结点先处理 + # 中节点先处理 result.append(node.val) # 左孩子先入栈 if node.left: @@ -304,7 +304,7 @@ class Solution: ``` #### Python 后序遍历的迭代新解法: -* 本解法不同于前文介绍的`逆转前序遍历调整后的结果`,而是采用了对每个结点直接处理。 +* 本解法不同于前文介绍的`逆转前序遍历调整后的结果`,而是采用了对每个节点直接处理。 ```python class Solution: @@ -315,7 +315,7 @@ class Solution: current = root while current or stack: - if current: # 一次处理完一个结点和他的左右儿子结点,不处理孙子结点,孙子结点由左右儿子等会分别处理。 + if current: # 一次处理完一个节点和他的左右儿子节点,不处理孙子节点,孙子节点由左右儿子等会分别处理。 stack.append(current) # 入栈自己 if current.right: @@ -329,7 +329,7 @@ class Solution: node = stack.pop() # A处,出的是左儿子,如果无左儿子,出的就是右儿子,如果连右儿子也没有,出的就是自己了。 - # 如果 node 是叶子结点,就可以收割了;如果左右儿子都已经被收割了,也可以收割 + # 如果 node 是叶子节点,就可以收割了;如果左右儿子都已经被收割了,也可以收割 if (node.left is None or node.left in popped_nodes) and \ (node.right is None or node.right in popped_nodes): popped_nodes.add(node) From 8b7027f2b7e911f4474c9c4c545c3a031639e16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lane=20Zhang=20=28=E5=BC=A0=E5=81=A5=29?= Date: Wed, 23 Oct 2024 12:30:11 +0800 Subject: [PATCH 1380/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E6=B3=A8=E9=87=8A=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\204\350\277\255\344\273\243\351\201\215\345\216\206.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 3b4b30fbae..79b5492945 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -304,14 +304,14 @@ class Solution: ``` #### Python 后序遍历的迭代新解法: -* 本解法不同于前文介绍的`逆转前序遍历调整后的结果`,而是采用了对每个节点直接处理。 +* 本解法不同于前文介绍的`逆转前序遍历调整后的结果`,而是采用了对每个节点直接处理。这个实现方法在面试中不容易写出来,在下一节,我将改造本代码,奉上代码更简洁、更套路化、更容易实现的统一方法。 ```python class Solution: def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: values = [] stack = [] - popped_nodes = set() # 记录值已经被收割了的 nodes + popped_nodes = set() # 记录值已经被收割了的 nodes,这是关键,已经被收割的节点还在树中,还会被访问到,但逻辑上已经等同于 null 节点。 current = root while current or stack: From b8a4613c53be3d295cf8d56993284777927ca9f1 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Wed, 23 Oct 2024 14:14:42 +0800 Subject: [PATCH 1381/1533] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md=20=E5=8A=A0?= =?UTF-8?q?=E5=85=A5"Set=E6=A0=87=E8=AE=B0=E6=B3=95"=E5=92=8C=E7=9B=B8?= =?UTF-8?q?=E5=85=B3Python=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\350\277\255\344\273\243\346\263\225.md" | 69 +++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 13c507375b..a1ac9dd2b3 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -27,7 +27,12 @@ **那我们就将访问的节点放入栈中,把要处理的节点也放入栈中但是要做标记。** -如何标记呢,**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法也可以叫做标记法。 +如何标记呢? + +* 方法一:**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法可以叫做`空指针标记法`。 + +* 方法二:**当一个节点被`pop()`后,把该节点放入一个`Set`中,表示该节点被处理过了,下次再处理这个节点时,直接收割。** +这种方法可以叫做`Set标记法`,样例代码见下文`Python Set标记法`。 方法二更容易理解,在面试中更容易写出来。 ### 迭代法中序遍历 @@ -234,7 +239,7 @@ class Solution { ### Python: -迭代法前序遍历: +> 迭代法前序遍历(空指针标记法): ```python class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: @@ -257,7 +262,7 @@ class Solution: return result ``` -迭代法中序遍历: +> 迭代法中序遍历(空指针标记法): ```python class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: @@ -282,7 +287,7 @@ class Solution: return result ``` -迭代法后序遍历: +> 迭代法后序遍历(空指针标记法): ```python class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: @@ -306,6 +311,62 @@ class Solution: return result ``` +> 中序遍历,统一迭代(Set标记法): +```python +class Solution: + def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + values = [] + stack = [] if root is None else [root] + popped_nodes = set() # 用于记录一个节点是否被 pop() 过 + + while stack: + node = stack.pop() + # 说明节点是之前被pop过又被加回来,现在又要出栈,就可以直接收割了, + # 因为节点的左右儿子已经按次序入栈,节点的使命已经完成。 + if node in popped_nodes: + values.append(node.val) + continue + + popped_nodes.add(node) # 记录第一次出栈,第一次出栈的目的是为了把左右儿子和自己按次序入栈 + + if node.right: # 中序遍历是'左中右',右儿子最先入栈,最后出栈 + stack.append(node.right) + + stack.append(node) # 把自己加回到栈中,位置居中 + + if node.left: + stack.append(node.left) # 左儿子最后入栈,最先出栈 + + return values +``` + +> 后序遍历,统一迭代(Set标记法): +```python +class Solution: + def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + values = [] + stack = [] if root is None else [root] + popped_nodes = set() # 用于记录一个节点是否被 pop() 过 + + while stack: + node = stack.pop() + # 说明节点是之前被pop过又被加回来,现在又要出栈,就可以直接收割了, + # 因为节点的左右儿子已经按次序入栈,节点的使命已经完成。 + if node in popped_nodes: + values.append(node.val) + continue + + popped_nodes.add(node) # 记录第一次出栈,第一次出栈的目的是为了把左右儿子和自己按次序入栈 + + stack.append(node) # 后序遍历是'左右中',节点自己最先入栈,最后出栈 + + if node.right: + stack.append(node.right) # 右儿子位置居中 + + if node.left: + stack.append(node.left) # 左儿子最后入栈,最先出栈 +``` + ### Go: > 前序遍历统一迭代法 From 6eb35a973796c440fde91ae5ef66fe75c1dfb426 Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 23 Oct 2024 16:19:36 +0800 Subject: [PATCH 1382/1533] =?UTF-8?q?503.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=E5=A2=9E=E5=8A=A0Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\244\247\345\205\203\347\264\240II.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 5751bb9171..fdbdc1cf3c 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -213,6 +213,40 @@ class Solution: ### Go: ```go +// 版本一 +func nextGreaterElements(nums []int) []int { + // 拼接一个新的nums + numsNew := make([]int, len(nums) * 2) + copy(numsNew, nums) + copy(numsNew[len(nums):], nums) + // 用新的nums大小来初始化result + result := make([]int, len(numsNew)) + for i := range result { + result[i] = -1 + } + + // 开始单调栈 + st := []int{0} + for i := 1; i < len(numsNew); i++ { + if numsNew[i] < numsNew[st[len(st)-1]] { + st = append(st, i) + } else if numsNew[i] == numsNew[st[len(st)-1]] { + st = append(st, i) + } else { + for len(st) > 0 && numsNew[i] > numsNew[st[len(st)-1]] { + result[st[len(st)-1]] = numsNew[i] + st = st[:len(st)-1] + } + st = append(st, i) + } + } + result = result[:len(result)/2] + return result +} +``` + +```go +// 版本二 func nextGreaterElements(nums []int) []int { length := len(nums) result := make([]int,length) From 46bb8d64082111260b0f5e8042443ce257c95d9c Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:26:42 +0800 Subject: [PATCH 1383/1533] =?UTF-8?q?=E9=94=99=E5=AD=97=200669.=E4=BF=AE?= =?UTF-8?q?=E5=89=AA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index aef8465943..325733862c 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -22,7 +22,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 83565ed41768333098fe78694f9990b280f64879 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:28:57 +0800 Subject: [PATCH 1384/1533] =?UTF-8?q?=E9=94=99=E5=AD=970450.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index f6057f44a5..cab9880af0 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -26,7 +26,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 0c674bb9179b06aa055fb0c10d3432f985b12f28 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:31:04 +0800 Subject: [PATCH 1385/1533] =?UTF-8?q?Update=200235.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E7=A5=96=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 597c2dffd3..192bb031dc 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -38,7 +38,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 041ac6bbdc0da8d557702d2fac7c51f6b1e7c8c4 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:31:48 +0800 Subject: [PATCH 1386/1533] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 7fe64ad21d..2533a618ed 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -21,7 +21,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 12236fa616878d49799cd0d6606d8a7f0d026ff8 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:32:25 +0800 Subject: [PATCH 1387/1533] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 023eeea50a..fb4ca7d88d 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -22,7 +22,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 779c16ca104556d442ddf23501026528495eb3a5 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:32:53 +0800 Subject: [PATCH 1388/1533] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 93b3fb5400..c89f8031e8 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -35,7 +35,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 2d62182265b9825f0eed8a451f1119558867db46 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:35:56 +0800 Subject: [PATCH 1389/1533] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 2e94f7a7e3..8572ec2d5f 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -36,7 +36,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 From a8ac9f9ae075115972ee5bf578890eb4198e4e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lane=20Zhang=20=28=E5=BC=A0=E5=81=A5=29?= Date: Thu, 24 Oct 2024 09:15:07 +0800 Subject: [PATCH 1390/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md=20?= =?UTF-8?q?=E5=BE=AE=E8=B0=83=20stack=20=E8=B5=8B=E5=80=BC=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\237\344\270\200\350\277\255\344\273\243\346\263\225.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index a1ac9dd2b3..3a74a9327b 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -316,7 +316,7 @@ class Solution: class Solution: def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: values = [] - stack = [] if root is None else [root] + stack = [root] if root else [] popped_nodes = set() # 用于记录一个节点是否被 pop() 过 while stack: @@ -345,7 +345,7 @@ class Solution: class Solution: def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: values = [] - stack = [] if root is None else [root] + stack = [root] if root else [] popped_nodes = set() # 用于记录一个节点是否被 pop() 过 while stack: From 775accb7fdb783c2ec253c59c4e387c852a22166 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Thu, 24 Oct 2024 10:41:30 +0800 Subject: [PATCH 1391/1533] =?UTF-8?q?0226.=E7=BF=BB=E8=BD=AC=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md=20=E4=BF=AE=E6=AD=A3=E5=87=A0=E5=A4=84=20?= =?UTF-8?q?Python=20=E4=BB=A3=E7=A0=81=E4=B8=8D=E4=B8=A5=E8=B0=A8=E8=AF=B4?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\344\272\214\345\217\211\346\240\221.md" | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index e501b29878..285f971adb 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -81,7 +81,7 @@ if (root == NULL) return root; 3. 确定单层递归的逻辑 -因为是先前序遍历,所以先进行交换左右孩子节点,然后反转左子树,反转右子树。 +因为是前序遍历,所以先进行交换左右孩子节点,然后反转左子树,反转右子树。 ```cpp swap(root->left, root->right); @@ -348,14 +348,13 @@ class Solution: while stack: node = stack.pop() node.left, node.right = node.right, node.left + if node.right: + stack.append(node.right) if node.left: stack.append(node.left) - if node.right: - stack.append(node.right) return root ``` - 递归法:中序遍历: ```python # Definition for a binary tree node. @@ -374,7 +373,7 @@ class Solution: return root ``` -迭代法:中序遍历: +迭代法,伪中序遍历(结果是对的,看起来像是中序遍历,实际上它是前序遍历,只不过把中间节点处理逻辑放到了中间。还是要用'统一写法'才是真正的中序遍历): ```python # Definition for a binary tree node. # class TreeNode: @@ -386,18 +385,17 @@ class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return None - stack = [root] + stack = [root] while stack: - node = stack.pop() - if node.left: - stack.append(node.left) - node.left, node.right = node.right, node.left - if node.left: - stack.append(node.left) + node = stack.pop() + if node.right: + stack.append(node.right) + node.left, node.right = node.right, node.left # 放到中间,依然是前序遍历 + if node.right: + stack.append(node.right) return root ``` - 递归法:后序遍历: ```python # Definition for a binary tree node. @@ -416,7 +414,7 @@ class Solution: return root ``` -迭代法:后序遍历: +迭代法,伪后序遍历(结果是对的,看起来像是后序遍历,实际上它是前序遍历,只不过把中间节点处理逻辑放到了最后。还是要用'统一写法'才是真正的后序遍历): ```python # Definition for a binary tree node. # class TreeNode: @@ -427,23 +425,19 @@ class Solution: class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: - return None - stack = [root] + return None + stack = [root] while stack: - node = stack.pop() + node = stack.pop() + if node.right: + stack.append(node.right) if node.left: stack.append(node.left) - if node.right: - stack.append(node.right) node.left, node.right = node.right, node.left return root ``` - - - - 迭代法:广度优先遍历(层序遍历): ```python # Definition for a binary tree node. From 2ddf04ff1432c5a3f0fdf81061913a5c7fcccd47 Mon Sep 17 00:00:00 2001 From: zhangrunzhe Date: Thu, 24 Oct 2024 18:04:48 +0800 Subject: [PATCH 1392/1533] =?UTF-8?q?=E5=8F=B3=E6=97=8B=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=20swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\255\227\347\254\246\344\270\262.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 363d9ffa1b..2b32cb44d2 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -350,7 +350,29 @@ function reverseStr(s, start, end) { ### Swift: +```swift +func rotateWords(_ s: String, _ k: Int) -> String { + var chars = Array(s) + // 先反转整体 + reverseWords(&chars, start: 0, end: s.count - 1) + // 反转前半段 + reverseWords(&chars, start: 0, end: k - 1) + // 反转后半段 + reverseWords(&chars, start: k, end: s.count - 1) + return String(chars) +} +// 反转start...end 的字符数组 +func reverseWords(_ chars: inout [Character], start: Int, end: Int) { + var left = start + var right = end + while left < right, right < chars.count { + (chars[left], chars[right]) = (chars[right], chars[left]) + left += 1 + right -= 1 + } +} +``` ### PHP: From d64a5359383d7d1fe399b33f2319f7253cd131f7 Mon Sep 17 00:00:00 2001 From: HONGYAN ZHAO Date: Thu, 24 Oct 2024 21:07:17 -0500 Subject: [PATCH 1393/1533] Debug python version, change strip() to split() --- ...62\233\347\232\204\346\200\273\351\235\242\347\247\257.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 26c92c07a6..408501ff84 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -261,10 +261,10 @@ public class Main { from collections import deque # 处理输入 -n, m = list(map(int, input().strip())) +n, m = list(map(int, input().split())) g = [] for _ in range(n): - row = list(map(int, input().strip())) + row = list(map(int, input().split())) g.append(row) # 定义四个方向、孤岛面积(遍历完边缘后会被重置) From 492f45f53d6e46bfed1d91cc91fe0a3d91964ead Mon Sep 17 00:00:00 2001 From: HONGYAN ZHAO Date: Thu, 24 Oct 2024 21:08:52 -0500 Subject: [PATCH 1394/1533] debug python version, set visited[x][y] to True in the beginning of bfs --- ...7\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 9d31c92268..522e19c398 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -254,6 +254,7 @@ directions = [[0, 1], [1, 0], [0, -1], [-1, 0]] def bfs(grid, visited, x, y): que = deque([]) que.append([x,y]) + visited[x][y] = True while que: cur_x, cur_y = que.popleft() for i, j in directions: From b9a060f195f704a09b7acf50e3ff36cb090471d0 Mon Sep 17 00:00:00 2001 From: Captainzw <80784797+Captain-zhangw@users.noreply.github.com> Date: Sat, 26 Oct 2024 20:16:14 +0800 Subject: [PATCH 1395/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20README.md?= =?UTF-8?q?=E4=B8=AD=20=E2=80=9Cbellman=5Fford=E4=B9=8B=E5=8D=95=E6=BA=90?= =?UTF-8?q?=E6=9C=89=E9=99=90=E6=9C=80=E7=9F=AD=E8=B7=AF=E2=80=9D=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3bb575d1a6..d1f4f3a8be 100644 --- a/README.md +++ b/README.md @@ -400,7 +400,7 @@ 24. [图论:Bellman_ford 算法](./problems/kamacoder/0094.城市间货物运输I.md) 25. [图论:Bellman_ford 队列优化算法(又名SPFA)](./problems/kamacoder/0094.城市间货物运输I-SPFA.md) 26. [图论:Bellman_ford之判断负权回路](./problems/kamacoder/0095.城市间货物运输II.md) -27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0095.城市间货物运输II.md) +27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0096.城市间货物运输III.md) 28. [图论:Floyd 算法](./problems/kamacoder/0097.小明逛公园.md) 29. [图论:A * 算法](./problems/kamacoder/0126.骑士的攻击astar.md) 30. [图论:最短路算法总结篇](./problems/kamacoder/最短路问题总结篇.md) From 74909977cd13549da4368f09b550da2474f1c807 Mon Sep 17 00:00:00 2001 From: Captainzw <80784797+Captain-zhangw@users.noreply.github.com> Date: Sat, 26 Oct 2024 20:31:53 +0800 Subject: [PATCH 1396/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200096.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93III.md=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=94=99=E8=AF=AF=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\211\251\350\277\220\350\276\223III.md" | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index 567a1d8724..1facf7b2e9 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -51,15 +51,15 @@ ## 思路 -本题为单源有限最短路问题,同样是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 延伸题目。 +本题为单源有限最短路问题,同样是 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 延伸题目。 注意题目中描述是 **最多经过 k 个城市的条件下,而不是一定经过k个城市,也可以经过的城市数量比k小,但要最短的路径**。 -在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 中我们讲了:**对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**。 +在 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 中我们讲了:**对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离**。 节点数量为n,起点到终点,最多是 n-1 条边相连。 那么对所有边松弛 n-1 次 就一定能得到 起点到达 终点的最短距离。 -(如果对以上讲解看不懂,建议详看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) ) +(如果对以上讲解看不懂,建议详看 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) ) 本题是最多经过 k 个城市, 那么是 k + 1条边相连的节点。 这里可能有录友想不懂为什么是k + 1,来看这个图: @@ -71,7 +71,7 @@ 对所有边松弛一次,相当于计算 起点到达 与起点一条边相连的节点 的最短距离,那么对所有边松弛 k + 1次,就是求 起点到达 与起点k + 1条边相连的节点的 最短距离。 -**注意**: 本题是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 的拓展题,如果对 bellman_ford 没有深入了解,强烈建议先看 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 再做本题。 +**注意**: 本题是 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 的拓展题,如果对 bellman_ford 没有深入了解,强烈建议先看 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 再做本题。 理解以上内容,其实本题代码就很容易了,bellman_ford 标准写法是松弛 n-1 次,本题就松弛 k + 1次就好。 @@ -366,19 +366,19 @@ int main() { ## 拓展二(本题本质) -那么前面讲解过的 [94.城市间货物运输I](./kama94.城市间货物运输I.md) 和 [95.城市间货物运输II](./kama95.城市间货物运输II.md) 也是bellman_ford经典算法,也没使用 minDist_copy,怎么就没问题呢? +那么前面讲解过的 [94.城市间货物运输I](./0094.城市间货物运输I.md) 和 [95.城市间货物运输II](./0095.城市间货物运输II.md) 也是bellman_ford经典算法,也没使用 minDist_copy,怎么就没问题呢? > 如果没看过我上面这两篇讲解的话,建议详细学习上面两篇,再看我下面讲的区别,否则容易看不懂。 -[94.城市间货物运输I](./kama94.城市间货物运输I.md), 是没有 负权回路的,那么 多松弛多少次,对结果都没有影响。 +[94.城市间货物运输I](./0094.城市间货物运输I.md), 是没有 负权回路的,那么 多松弛多少次,对结果都没有影响。 求 节点1 到 节点n 的最短路径,松弛n-1 次就够了,松弛 大于 n-1次,结果也不会变。 那么在对所有边进行第一次松弛的时候,如果基于 本次计算的 minDist 来计算 minDist (相当于多做松弛了),也是对最终结果没影响。 -[95.城市间货物运输II](./kama95.城市间货物运输II.md) 是判断是否有 负权回路,一旦有负权回路, 对所有边松弛 n-1 次以后,在做松弛 minDist 数值一定会变,根据这一点来判断是否有负权回路。 +[95.城市间货物运输II](./0095.城市间货物运输II.md) 是判断是否有 负权回路,一旦有负权回路, 对所有边松弛 n-1 次以后,在做松弛 minDist 数值一定会变,根据这一点来判断是否有负权回路。 -所以,[95.城市间货物运输II](./kama95.城市间货物运输II.md) 只需要判断minDist数值变化了就行,而 minDist 的数值对不对,并不是我们关心的。 +所以,[95.城市间货物运输II](./0095.城市间货物运输II.md) 只需要判断minDist数值变化了就行,而 minDist 的数值对不对,并不是我们关心的。 那么本题 为什么计算minDist 一定要基于上次 的 minDist 数值。 From 7cb657dc508465362d74b6133bd060c42eddf610 Mon Sep 17 00:00:00 2001 From: Captainzw <80784797+Captain-zhangw@users.noreply.github.com> Date: Sat, 26 Oct 2024 20:33:16 +0800 Subject: [PATCH 1397/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200095.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93II.md=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=94=99=E8=AF=AF=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...264\247\347\211\251\350\277\220\350\276\223II.md" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index edfd52e02d..ac6ccf3c0d 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -54,7 +54,7 @@ circle ## 思路 -本题是 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 延伸题目。 +本题是 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 延伸题目。 本题是要我们判断 负权回路,也就是图中出现环且环上的边总权值为负数。 @@ -64,7 +64,7 @@ circle 接下来我们来看 如何使用 bellman_ford 算法来判断 负权回路。 -在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 中 我们讲了 bellman_ford 算法的核心就是一句话:对 所有边 进行 n-1 次松弛。 同时文中的 【拓展】部分, 我们也讲了 松弛n次以上 会怎么样? +在 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 中 我们讲了 bellman_ford 算法的核心就是一句话:对 所有边 进行 n-1 次松弛。 同时文中的 【拓展】部分, 我们也讲了 松弛n次以上 会怎么样? 在没有负权回路的图中,松弛 n 次以上 ,结果不会有变化。 @@ -72,7 +72,7 @@ circle 那么每松弛一次,都会更新最短路径,所以结果会一直有变化。 -(如果对于 bellman_ford 不了解的录友,建议详细看这里:[kama94.城市间货物运输I](./kama94.城市间货物运输I.md)) +(如果对于 bellman_ford 不了解的录友,建议详细看这里:[kama94.城市间货物运输I](./0094.城市间货物运输I.md)) 以上为理论分析,接下来我们再画图举例。 @@ -94,13 +94,13 @@ circle 如果在负权回路多绕两圈,三圈,无穷圈,那么我们的总成本就会无限小, 如果要求最小成本的话,你会发现本题就无解了。 -在 bellman_ford 算法中,松弛 n-1 次所有的边 就可以求得 起点到任何节点的最短路径,松弛 n 次以上,minDist数组(记录起到到其他节点的最短距离)中的结果也不会有改变 (如果对 bellman_ford 算法 不了解,也不知道 minDist 是什么,建议详看上篇讲解[kama94.城市间货物运输I](./kama94.城市间货物运输I.md)) +在 bellman_ford 算法中,松弛 n-1 次所有的边 就可以求得 起点到任何节点的最短路径,松弛 n 次以上,minDist数组(记录起到到其他节点的最短距离)中的结果也不会有改变 (如果对 bellman_ford 算法 不了解,也不知道 minDist 是什么,建议详看上篇讲解[kama94.城市间货物运输I](./0094.城市间货物运输I.md)) 而本题有负权回路的情况下,一直都会有更短的最短路,所以 松弛 第n次,minDist数组 也会发生改变。 -那么解决本题的 核心思路,就是在 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 的基础上,再多松弛一次,看minDist数组 是否发生变化。 +那么解决本题的 核心思路,就是在 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 的基础上,再多松弛一次,看minDist数组 是否发生变化。 -代码和 [kama94.城市间货物运输I](./kama94.城市间货物运输I.md) 基本是一样的,如下:(关键地方已注释) +代码和 [kama94.城市间货物运输I](./0094.城市间货物运输I.md) 基本是一样的,如下:(关键地方已注释) ```CPP #include From 55963190f82417db91a09f6f8a0a9fa080e87059 Mon Sep 17 00:00:00 2001 From: htzhu Date: Sat, 26 Oct 2024 21:45:08 -0700 Subject: [PATCH 1398/1533] =?UTF-8?q?Update=200738.=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python代码里大写的N会报错,把N改为了小写n。 --- ...36\347\232\204\346\225\260\345\255\227.md" | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index 3d46d5ad04..be4411dd8c 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -190,9 +190,9 @@ class Solution: 贪心(版本一) ```python class Solution: - def monotoneIncreasingDigits(self, N: int) -> int: + def monotoneIncreasingDigits(self, n: int) -> int: # 将整数转换为字符串 - strNum = str(N) + strNum = str(n) # flag用来标记赋值9从哪里开始 # 设置为字符串长度,为了防止第二个for循环在flag没有被赋值的情况下执行 flag = len(strNum) @@ -216,9 +216,9 @@ class Solution: 贪心(版本二) ```python class Solution: - def monotoneIncreasingDigits(self, N: int) -> int: + def monotoneIncreasingDigits(self, n: int) -> int: # 将整数转换为字符串 - strNum = list(str(N)) + strNum = list(str(n)) # 从右往左遍历字符串 for i in range(len(strNum) - 1, 0, -1): @@ -238,9 +238,9 @@ class Solution: ```python class Solution: - def monotoneIncreasingDigits(self, N: int) -> int: + def monotoneIncreasingDigits(self, n: int) -> int: # 将整数转换为字符串 - strNum = list(str(N)) + strNum = list(str(n)) # 从右往左遍历字符串 for i in range(len(strNum) - 1, 0, -1): @@ -258,8 +258,8 @@ class Solution: ```python class Solution: - def monotoneIncreasingDigits(self, N: int) -> int: - strNum = str(N) + def monotoneIncreasingDigits(self, n: int) -> int: + strNum = str(n) for i in range(len(strNum) - 1, 0, -1): # 如果当前字符比前一个字符小,说明需要修改前一个字符 if strNum[i - 1] > strNum[i]: @@ -272,12 +272,12 @@ class Solution: ``` ### Go ```go -func monotoneIncreasingDigits(N int) int { +func monotoneIncreasingDigits(n int) int { s := strconv.Itoa(N)//将数字转为字符串,方便使用下标 ss := []byte(s)//将字符串转为byte数组,方便更改。 n := len(ss) if n <= 1 { - return N + return n } for i := n-1; i > 0; i-- { if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1,后面的全部置为9 From e317e8761ea063ea8cbe88474c9c8ef7a7e954dc Mon Sep 17 00:00:00 2001 From: markwang Date: Mon, 28 Oct 2024 10:15:08 +0800 Subject: [PATCH 1399/1533] =?UTF-8?q?84.=E6=9F=B1=E7=8A=B6=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 123 +++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index c08a3045a5..5c6f407321 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -474,7 +474,128 @@ class Solution: ### Go: -> 单调栈 +暴力解法 + +```go +func largestRectangleArea(heights []int) int { + sum := 0 + for i := 0; i < len(heights); i++ { + left, right := i, i + for left >= 0 { + if heights[left] < heights[i] { + break + } + left-- + } + for right < len(heights) { + if heights[right] < heights[i] { + break + } + right++ + } + w := right - left - 1 + h := heights[i] + sum = max(sum, w * h) + } + return sum +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + +双指针解法 + +```go +func largestRectangleArea(heights []int) int { + size := len(heights) + minLeftIndex := make([]int, size) + minRightIndex := make([]int, size) + + // 记录每个柱子 左边第一个小于该柱子的下标 + minLeftIndex[0] = -1 // 注意这里初始化,防止下面while死循环 + for i := 1; i < size; i++ { + t := i - 1 + // 这里不是用if,而是不断向左寻找的过程 + for t >= 0 && heights[t] >= heights[i] { + t = minLeftIndex[t] + } + minLeftIndex[i] = t + } + // 记录每个柱子 右边第一个小于该柱子的下标 + minRightIndex[size - 1] = size; // 注意这里初始化,防止下面while死循环 + for i := size - 2; i >= 0; i-- { + t := i + 1 + // 这里不是用if,而是不断向右寻找的过程 + for t < size && heights[t] >= heights[i] { + t = minRightIndex[t] + } + minRightIndex[i] = t + } + // 求和 + result := 0 + for i := 0; i < size; i++ { + sum := heights[i] * (minRightIndex[i] - minLeftIndex[i] - 1) + result = max(sum, result) + } + return result +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + +单调栈 + +```go +func largestRectangleArea(heights []int) int { + result := 0 + heights = append([]int{0}, heights...) // 数组头部加入元素0 + heights = append(heights, 0) // 数组尾部加入元素0 + st := []int{0} + + // 第一个元素已经入栈,从下标1开始 + for i := 1; i < len(heights); i++ { + if heights[i] > heights[st[len(st)-1]] { + st = append(st, i) + } else if heights[i] == heights[st[len(st)-1]] { + st = st[:len(st)-1] + st = append(st, i) + } else { + for len(st) > 0 && heights[i] < heights[st[len(st)-1]] { + mid := st[len(st)-1] + st = st[:len(st)-1] + if len(st) > 0 { + left := st[len(st)-1] + right := i + w := right - left - 1 + h := heights[mid] + result = max(result, w * h) + } + } + st = append(st, i) + } + } + return result +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + +单调栈精简 ```go func largestRectangleArea(heights []int) int { From abefd5840b64b3f82148bf383499bd854f20e42d Mon Sep 17 00:00:00 2001 From: zhoutianyi Date: Sun, 27 Oct 2024 23:39:32 -0400 Subject: [PATCH 1400/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=92=8C=20go=20=E5=9B=9E=E6=BA=AF=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 0872460907..09e77b123a 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -706,6 +706,31 @@ class Solution: ``` ### Go +回溯法思路 +```go +func findTargetSumWays(nums []int, target int) int { + var result int + var backtracking func(nums []int, target int, index int, currentSum int) + + backtracking = func(nums []int, target int, index int, currentSum int) { + if index == len(nums) { + if currentSum == target { + result++ + } + return + } + + // 选择加上当前数字 + backtracking(nums, target, index+1, currentSum+nums[index]) + + // 选择减去当前数字 + backtracking(nums, target, index+1, currentSum-nums[index]) + } + + backtracking(nums, target, 0, 0) + return result +} +``` 二维dp ```go func findTargetSumWays(nums []int, target int) int { From 497bb7cbf358a4db63dcd32650513ae7e949bedd Mon Sep 17 00:00:00 2001 From: hui <760261797@qq.com> Date: Tue, 29 Oct 2024 16:40:27 +0800 Subject: [PATCH 1401/1533] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=AC=94=E8=AF=AF?= =?UTF-8?q?=EF=BC=8C=E6=95=B0=E7=BB=84=E4=B8=8E=E5=9B=BE=E7=89=87=E4=B8=8D?= =?UTF-8?q?=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 9e6714ce9d..88b59ffc72 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -72,7 +72,7 @@ #### 情况一:上下坡中有平坡 -例如 [1,2,2,2,1]这样的数组,如图: +例如 [1,2,2,2,2,1]这样的数组,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106170449.png) From 28de17ec3921f86376f493a781727e7589d26b73 Mon Sep 17 00:00:00 2001 From: Kuxry Date: Wed, 30 Oct 2024 09:54:39 +0900 Subject: [PATCH 1402/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200126.=E9=AA=91?= =?UTF-8?q?=E5=A3=AB=E7=9A=84=E6=94=BB=E5=87=BBastar=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\232\204\346\224\273\345\207\273astar.md" | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 2d0481ecec..403fe1face 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -389,7 +389,170 @@ for _ in range(n): ### C +```C +#include +#include +#include + +// 定义一个结构体,表示棋盘上骑士的位置和相关的 A* 算法参数 +typedef struct { + int x, y; // 骑士在棋盘上的坐标 + int g; // 从起点到当前节点的实际消耗 + int h; // 从当前节点到目标节点的估计消耗(启发式函数值) + int f; // 总的估计消耗(f = g + h) +} Knight; + +#define MAX_HEAP_SIZE 2000000 // 优先队列的最大容量 + +// 定义一个优先队列,使用最小堆来实现 A* 算法中的 Open 列表 +typedef struct { + Knight data[MAX_HEAP_SIZE]; + int size; +} PriorityQueue; + +// 初始化优先队列 +void initQueue(PriorityQueue *pq) { + pq->size = 0; +} + +// 将骑士节点插入优先队列 +void push(PriorityQueue *pq, Knight k) { + if (pq->size >= MAX_HEAP_SIZE) { + // 堆已满,无法插入新节点 + return; + } + int i = pq->size++; + pq->data[i] = k; + // 上滤操作,维护最小堆的性质,使得 f 值最小的节点在堆顶 + while (i > 0) { + int parent = (i - 1) / 2; + if (pq->data[parent].f <= pq->data[i].f) { + break; + } + // 交换父节点和当前节点 + Knight temp = pq->data[parent]; + pq->data[parent] = pq->data[i]; + pq->data[i] = temp; + i = parent; + } +} + +// 从优先队列中弹出 f 值最小的骑士节点 +Knight pop(PriorityQueue *pq) { + Knight min = pq->data[0]; + pq->size--; + pq->data[0] = pq->data[pq->size]; + // 下滤操作,维护最小堆的性质 + int i = 0; + while (1) { + int left = 2 * i + 1; + int right = 2 * i + 2; + int smallest = i; + if (left < pq->size && pq->data[left].f < pq->data[smallest].f) { + smallest = left; + } + if (right < pq->size && pq->data[right].f < pq->data[smallest].f) { + smallest = right; + } + if (smallest == i) { + break; + } + // 交换当前节点与最小子节点 + Knight temp = pq->data[smallest]; + pq->data[smallest] = pq->data[i]; + pq->data[i] = temp; + i = smallest; + } + return min; +} + +// 判断优先队列是否为空 +int isEmpty(PriorityQueue *pq) { + return pq->size == 0; +} + +// 启发式函数:计算从当前位置到目标位置的欧几里得距离的平方(避免开方,提高效率) +int heuristic(int x, int y, int goal_x, int goal_y) { + int dx = x - goal_x; + int dy = y - goal_y; + return dx * dx + dy * dy; // 欧几里得距离的平方 +} + +// 用于记录从起点到棋盘上每个位置的最小移动次数 +int moves[1001][1001]; + +// 骑士在棋盘上的8个可能移动方向 +int dir[8][2] = { + {-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, + {2, 1}, {2, -1}, {1, -2}, {-1, -2} +}; + +// 使用 A* 算法寻找从起点到目标点的最短路径 +int astar(int start_x, int start_y, int goal_x, int goal_y) { + PriorityQueue pq; + initQueue(&pq); + + // 初始化 moves 数组,-1 表示未访问过的位置 + memset(moves, -1, sizeof(moves)); + moves[start_x][start_y] = 0; // 起点位置的移动次数为 0 + + // 初始化起始节点 + Knight start; + start.x = start_x; + start.y = start_y; + start.g = 0; // 从起点到起点的消耗为 0 + start.h = heuristic(start_x, start_y, goal_x, goal_y); + start.f = start.g + start.h; // 总的估计消耗 + + push(&pq, start); // 将起始节点加入优先队列 + + while (!isEmpty(&pq)) { + Knight current = pop(&pq); // 取出 f 值最小的节点 + + // 如果已经到达目标位置,返回所需的最小移动次数 + if (current.x == goal_x && current.y == goal_y) { + return moves[current.x][current.y]; + } + + // 遍历当前节点的所有可能移动方向 + for (int i = 0; i < 8; i++) { + int nx = current.x + dir[i][0]; + int ny = current.y + dir[i][1]; + + // 检查新位置是否在棋盘范围内且未被访问过 + if (nx >= 1 && nx <= 1000 && ny >= 1 && ny <= 1000 && moves[nx][ny] == -1) { + moves[nx][ny] = moves[current.x][current.y] + 1; // 更新移动次数 + + // 创建新节点,表示骑士移动到的新位置 + Knight neighbor; + neighbor.x = nx; + neighbor.y = ny; + neighbor.g = current.g + 5; // 每次移动的消耗为 5(骑士移动的距离平方) + neighbor.h = heuristic(nx, ny, goal_x, goal_y); + neighbor.f = neighbor.g + neighbor.h; + + push(&pq, neighbor); // 将新节点加入优先队列 + } + } + } + + return -1; // 如果无法到达目标位置,返回 -1 +} +int main() { + int n; + scanf("%d", &n); + while (n--) { + int a1, a2, b1, b2; // 起点和目标点的坐标 + scanf("%d %d %d %d", &a1, &a2, &b1, &b2); + + int result = astar(a1, a2, b1, b2); // 使用 A* 算法计算最短路径 + printf("%d\n", result); // 输出所需的最小移动次数 + } + return 0; +} + +``` From ef36f40e4365cff7113c0f084d9cc3058de6e469 Mon Sep 17 00:00:00 2001 From: Kuxry Date: Wed, 30 Oct 2024 10:34:03 +0900 Subject: [PATCH 1403/1533] =?UTF-8?q?=20=E6=B7=BB=E5=8A=A00126.=E9=AA=91?= =?UTF-8?q?=E5=A3=AB=E7=9A=84=E6=94=BB=E5=87=BBastar=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\345\243\253\347\232\204\346\224\273\345\207\273astar.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 403fe1face..e3ce98f655 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -500,7 +500,7 @@ int astar(int start_x, int start_y, int goal_x, int goal_y) { Knight start; start.x = start_x; start.y = start_y; - start.g = 0; // 从起点到起点的消耗为 0 + start.g = 0; start.h = heuristic(start_x, start_y, goal_x, goal_y); start.f = start.g + start.h; // 总的估计消耗 @@ -547,7 +547,7 @@ int main() { scanf("%d %d %d %d", &a1, &a2, &b1, &b2); int result = astar(a1, a2, b1, b2); // 使用 A* 算法计算最短路径 - printf("%d\n", result); // 输出所需的最小移动次数 + printf("%d\n", result); // 输出最小移动次数 } return 0; } From 2602359ec9a62a08987234d95c7e3cc2571894ad Mon Sep 17 00:00:00 2001 From: Kuxry Date: Wed, 30 Oct 2024 10:48:07 +0900 Subject: [PATCH 1404/1533] =?UTF-8?q?=20=E6=B7=BB=E5=8A=A00126.=E9=AA=91?= =?UTF-8?q?=E5=A3=AB=E7=9A=84=E6=94=BB=E5=87=BBastar=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index e3ce98f655..3bbf4169e0 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -402,7 +402,7 @@ typedef struct { int f; // 总的估计消耗(f = g + h) } Knight; -#define MAX_HEAP_SIZE 2000000 // 优先队列的最大容量 +#define MAX_HEAP_SIZE 2000000 // 假设优先队列的最大容量 // 定义一个优先队列,使用最小堆来实现 A* 算法中的 Open 列表 typedef struct { From b3c79d848410230687321964ee39f238fb9f030c Mon Sep 17 00:00:00 2001 From: donghuanjie Date: Thu, 31 Oct 2024 19:38:10 -0700 Subject: [PATCH 1405/1533] modified 707 code, move the ListNode class inside, reformat the code --- ...76\350\256\241\351\223\276\350\241\250.md" | 128 +++++++++--------- 1 file changed, 63 insertions(+), 65 deletions(-) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 0cb2f2f2fa..ed1726d942 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -422,38 +422,38 @@ void myLinkedListFree(MyLinkedList* obj) { ```Java //单链表 -class ListNode { - int val; - ListNode next; - ListNode(){} - ListNode(int val) { - this.val=val; - } -} class MyLinkedList { + + class ListNode { + int val; + ListNode next; + ListNode(int val) { + this.val=val; + } + } //size存储链表元素的个数 - int size; - //虚拟头结点 - ListNode head; + private int size; + //注意这里记录的是虚拟头结点 + private ListNode head; //初始化链表 public MyLinkedList() { - size = 0; - head = new ListNode(0); + this.size = 0; + this.head = new ListNode(0); } - //获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点 + //获取第index个节点的数值,注意index是从0开始的,第0个节点就是虚拟头结点 public int get(int index) { //如果index非法,返回-1 if (index < 0 || index >= size) { return -1; } - ListNode currentNode = head; - //包含一个虚拟头节点,所以查找第 index+1 个节点 + ListNode cur = head; + //第0个节点是虚拟头节点,所以查找第 index+1 个节点 for (int i = 0; i <= index; i++) { - currentNode = currentNode.next; + cur = cur.next; } - return currentNode.val; + return cur.val; } public void addAtHead(int val) { @@ -473,7 +473,6 @@ class MyLinkedList { while (cur.next != null) { cur = cur.next; } - cur.next = newNode; size++; @@ -485,55 +484,53 @@ class MyLinkedList { // 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点 // 如果 index 大于链表的长度,则返回空 public void addAtIndex(int index, int val) { - if (index > size) { + if (index < 0 || index > size) { return; } - if (index < 0) { - index = 0; - } - size++; + //找到要插入节点的前驱 - ListNode pred = head; + ListNode pre = head; for (int i = 0; i < index; i++) { - pred = pred.next; + pre = pre.next; } - ListNode toAdd = new ListNode(val); - toAdd.next = pred.next; - pred.next = toAdd; + ListNode newNode = new ListNode(val); + newNode.next = pre.next; + pre.next = newNode; + size++; } - //删除第index个节点 public void deleteAtIndex(int index) { if (index < 0 || index >= size) { return; } - size--; - //因为有虚拟头节点,所以不用对Index=0的情况进行特殊处理 - ListNode pred = head; + + //因为有虚拟头节点,所以不用对index=0的情况进行特殊处理 + ListNode pre = head; for (int i = 0; i < index ; i++) { - pred = pred.next; + pre = pre.next; } - pred.next = pred.next.next; + pre.next = pre.next.next; + size--; } } +``` +```Java //双链表 -class ListNode{ - int val; - ListNode next,prev; - ListNode() {}; - ListNode(int val){ - this.val = val; - } -} - - class MyLinkedList { + class ListNode{ + int val; + ListNode next, prev; + ListNode(int val){ + this.val = val; + } + } + //记录链表中元素的数量 - int size; + private int size; //记录链表的虚拟头结点和尾结点 - ListNode head,tail; + private ListNode head, tail; public MyLinkedList() { //初始化操作 @@ -541,25 +538,25 @@ class MyLinkedList { this.head = new ListNode(0); this.tail = new ListNode(0); //这一步非常关键,否则在加入头结点的操作中会出现null.next的错误!!! - head.next=tail; - tail.prev=head; + this.head.next = tail; + this.tail.prev = head; } public int get(int index) { //判断index是否有效 - if(index>=size){ + if(index < 0 || index >= size){ return -1; } - ListNode cur = this.head; + ListNode cur = head; //判断是哪一边遍历时间更短 if(index >= size / 2){ //tail开始 cur = tail; - for(int i=0; i< size-index; i++){ + for(int i = 0; i < size - index; i++){ cur = cur.prev; } }else{ - for(int i=0; i<= index; i++){ + for(int i = 0; i <= index; i++){ cur = cur.next; } } @@ -568,24 +565,23 @@ class MyLinkedList { public void addAtHead(int val) { //等价于在第0个元素前添加 - addAtIndex(0,val); + addAtIndex(0, val); } public void addAtTail(int val) { //等价于在最后一个元素(null)前添加 - addAtIndex(size,val); + addAtIndex(size, val); } public void addAtIndex(int index, int val) { - //index大于链表长度 - if(index>size){ + //判断index是否有效 + if(index < 0 || index > size){ return; } - size++; //找到前驱 - ListNode pre = this.head; - for(int i=0; i=size){ + //判断index是否有效 + if(index < 0 || index >= size){ return; } + //删除操作 - size--; - ListNode pre = this.head; - for(int i=0; i Date: Fri, 1 Nov 2024 14:15:14 +0800 Subject: [PATCH 1406/1533] =?UTF-8?q?992.=E6=8C=89=E5=A5=87=E5=81=B6?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84II=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E6=8F=8F=E8=BF=B0=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?Go=E6=96=B9=E6=B3=95=E4=BA=8C=E5=92=8C=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\272\217\346\225\260\347\273\204II.md" | 77 +++++++++++++------ 1 file changed, 54 insertions(+), 23 deletions(-) diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" index 1ac6800c08..28680dbf0f 100644 --- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" +++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" @@ -11,9 +11,9 @@ [力扣题目链接](https://leetcode.cn/problems/sort-array-by-parity-ii/) -给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 +给定一个非负整数数组 nums, nums 中一半整数是奇数,一半整数是偶数。 -对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。 +对数组进行排序,以便当 nums[i] 为奇数时,i 也是奇数;当 nums[i] 为偶数时, i 也是偶数。 你可以返回任何满足上述条件的数组作为答案。 @@ -35,17 +35,17 @@ ```CPP class Solution { public: - vector sortArrayByParityII(vector& A) { - vector even(A.size() / 2); // 初始化就确定数组大小,节省开销 - vector odd(A.size() / 2); - vector result(A.size()); + vector sortArrayByParityII(vector& nums) { + vector even(nums.size() / 2); // 初始化就确定数组大小,节省开销 + vector odd(nums.size() / 2); + vector result(nums.size()); int evenIndex = 0; int oddIndex = 0; int resultIndex = 0; - // 把A数组放进偶数数组,和奇数数组 - for (int i = 0; i < A.size(); i++) { - if (A[i] % 2 == 0) even[evenIndex++] = A[i]; - else odd[oddIndex++] = A[i]; + // 把nums数组放进偶数数组,和奇数数组 + for (int i = 0; i < nums.size(); i++) { + if (nums[i] % 2 == 0) even[evenIndex++] = nums[i]; + else odd[oddIndex++] = nums[i]; } // 把偶数数组,奇数数组分别放进result数组中 for (int i = 0; i < evenIndex; i++) { @@ -62,22 +62,22 @@ public: ### 方法二 -以上代码我是建了两个辅助数组,而且A数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助树,代码如下: +以上代码我是建了两个辅助数组,而且nums数组还相当于遍历了两次,用辅助数组的好处就是思路清晰,优化一下就是不用这两个辅助数组,代码如下: ```CPP class Solution { public: - vector sortArrayByParityII(vector& A) { - vector result(A.size()); + vector sortArrayByParityII(vector& nums) { + vector result(nums.size()); int evenIndex = 0; // 偶数下标 int oddIndex = 1; // 奇数下标 - for (int i = 0; i < A.size(); i++) { - if (A[i] % 2 == 0) { - result[evenIndex] = A[i]; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] % 2 == 0) { + result[evenIndex] = nums[i]; evenIndex += 2; } else { - result[oddIndex] = A[i]; + result[oddIndex] = nums[i]; oddIndex += 2; } } @@ -96,15 +96,15 @@ public: ```CPP class Solution { public: - vector sortArrayByParityII(vector& A) { + vector sortArrayByParityII(vector& nums) { int oddIndex = 1; - for (int i = 0; i < A.size(); i += 2) { - if (A[i] % 2 == 1) { // 在偶数位遇到了奇数 - while(A[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数 - swap(A[i], A[oddIndex]); // 替换 + for (int i = 0; i < nums.size(); i += 2) { + if (nums[i] % 2 == 1) { // 在偶数位遇到了奇数 + while(nums[oddIndex] % 2 != 0) oddIndex += 2; // 在奇数位找一个偶数 + swap(nums[i], nums[oddIndex]); // 替换 } } - return A; + return nums; } }; ``` @@ -253,6 +253,37 @@ func sortArrayByParityII(nums []int) []int { } return result; } + +// 方法二 +func sortArrayByParityII(nums []int) []int { + result := make([]int, len(nums)) + evenIndex := 0 // 偶数下标 + oddIndex := 1 // 奇数下标 + for _, v := range nums { + if v % 2 == 0 { + result[evenIndex] = v + evenIndex += 2 + } else { + result[oddIndex] = v + oddIndex += 2 + } + } + return result +} + +// 方法三 +func sortArrayByParityII(nums []int) []int { + oddIndex := 1 + for i := 0; i < len(nums); i += 2 { + if nums[i] % 2 == 1 { // 在偶数位遇到了奇数 + for nums[oddIndex] % 2 != 0 { + oddIndex += 2 // 在奇数位找一个偶数 + } + nums[i], nums[oddIndex] = nums[oddIndex], nums[i] + } + } + return nums +} ``` ### JavaScript From cc1cdb07db6598c98e804ad40af5fd958062a5d8 Mon Sep 17 00:00:00 2001 From: donghuanjie Date: Fri, 1 Nov 2024 01:12:34 -0700 Subject: [PATCH 1407/1533] add java recursion version of 203 --- ...76\350\241\250\345\205\203\347\264\240.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index f6b5ef6dbb..d51895aa63 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -337,6 +337,37 @@ public ListNode removeElements(ListNode head, int val) { ``` +递归 + +```java +/** + * 时间复杂度 O(n) + * 空间复杂度 O(n) + * @param head + * @param val + * @return + */ +class Solution { + public ListNode removeElements(ListNode head, int val) { + if (head == null) { + return head; + } + + // 假设 removeElements() 返回后面完整的已经去掉val节点的子链表 + // 在当前递归层用当前节点接住后面的子链表 + // 随后判断当前层的node是否需要被删除,如果是,就返回 + // 也可以先判断是否需要删除当前node,但是这样条件语句会比较不好想 + head.next = removeElements(head.next, val); + if (head.val == val) { + return head.next; + } + return head; + + // 实际上就是还原一个从尾部开始重新构建链表的过程 + } +} +``` + ### Python: ```python From 4c397b64f5f4aba97f9d9034182b83bd8a062db6 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Mon, 4 Nov 2024 12:07:27 +0800 Subject: [PATCH 1408/1533] =?UTF-8?q?0450.=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9=20=E5=8A=A0=E5=85=A5Ruby=E9=80=92=E5=BD=92=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index f6057f44a5..7523c50a80 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -801,6 +801,40 @@ impl Solution { } ``` +### Ruby +> 递归法: +```ruby +# @param {TreeNode} root +# @param {Integer} key +# @return {TreeNode} +def delete_node(root, key) + return nil if root.nil? + + right = root.right + left = root.left + + if root.val == key + return right if left.nil? + return left if right.nil? + + node = right + while node.left + node = node.left + end + node.left = left + + return right + end + + if root.val > key + root.left = delete_node(left, key) + else + root.right = delete_node(right, key) + end + + return root +end +```

From 8c389cb67328c37e3744ec52126aa6dcfec399b2 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Tue, 5 Nov 2024 13:28:37 +0800 Subject: [PATCH 1409/1533] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8"boolean=E6=A0=87=E8=AE=B0=E6=B3=95"=E5=B9=B6=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=9B=B8=E5=85=B3Python=E4=BB=A3=E7=A0=81=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\350\277\255\344\273\243\346\263\225.md" | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index a1ac9dd2b3..412aec8cde 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -31,8 +31,8 @@ * 方法一:**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法可以叫做`空指针标记法`。 -* 方法二:**当一个节点被`pop()`后,把该节点放入一个`Set`中,表示该节点被处理过了,下次再处理这个节点时,直接收割。** -这种方法可以叫做`Set标记法`,样例代码见下文`Python Set标记法`。 方法二更容易理解,在面试中更容易写出来。 +* 方法二:**加一个 `boolean` 值跟随每个节点,`false` (默认值) 表示需要为该节点和它的左右儿子安排在栈中的位次,`true` 表示该节点的位次之前已经安排过了,可以收割节点了。** +这种方法可以叫做`boolean 标记法`,样例代码见下文`C++ 和 Python 的 boolean 标记法`。 这种方法更容易理解,在面试中更容易写出来。 ### 迭代法中序遍历 @@ -311,60 +311,59 @@ class Solution: return result ``` -> 中序遍历,统一迭代(Set标记法): +> 中序遍历,统一迭代(boolean 标记法): ```python class Solution: def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: values = [] - stack = [] if root is None else [root] - popped_nodes = set() # 用于记录一个节点是否被 pop() 过 + stack = [(root, False)] if root else [] # 多加一个参数,False 为默认值,含义见下文 while stack: - node = stack.pop() - # 说明节点是之前被pop过又被加回来,现在又要出栈,就可以直接收割了, - # 因为节点的左右儿子已经按次序入栈,节点的使命已经完成。 - if node in popped_nodes: + node, visited = stack.pop() # 多加一个 visited 参数,使“迭代统一写法”成为一件简单的事 + + if visited: # visited 为 True,表示该节点和两个儿子的位次之前已经安排过了,现在可以收割节点了 values.append(node.val) continue - popped_nodes.add(node) # 记录第一次出栈,第一次出栈的目的是为了把左右儿子和自己按次序入栈 - - if node.right: # 中序遍历是'左中右',右儿子最先入栈,最后出栈 - stack.append(node.right) + # visited 当前为 False, 表示初次访问本节点,此次访问的目的是“把自己和两个儿子在栈中安排好位次”。 + # 中序遍历是'左中右',右儿子最先入栈,最后出栈。 + if node.right: + stack.append((node.right, False)) - stack.append(node) # 把自己加回到栈中,位置居中 + stack.append((node, True)) # 把自己加回到栈中,位置居中。同时,设置 visited 为 True,表示下次再访问本节点时,允许收割 if node.left: - stack.append(node.left) # 左儿子最后入栈,最先出栈 + stack.append((node.left, False)) # 左儿子最后入栈,最先出栈 return values ``` -> 后序遍历,统一迭代(Set标记法): +> 后序遍历,统一迭代(boolean 标记法): ```python class Solution: def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: values = [] - stack = [] if root is None else [root] - popped_nodes = set() # 用于记录一个节点是否被 pop() 过 + stack = [(root, False)] if root else [] # 多加一个参数,False 为默认值,含义见下文 while stack: - node = stack.pop() - # 说明节点是之前被pop过又被加回来,现在又要出栈,就可以直接收割了, - # 因为节点的左右儿子已经按次序入栈,节点的使命已经完成。 - if node in popped_nodes: + node, visited = stack.pop() # 多加一个 visited 参数,使“迭代统一写法”成为一件简单的事 + + if visited: # visited 为 True,表示该节点和两个儿子位次之前已经安排过了,现在可以收割节点了 values.append(node.val) continue - popped_nodes.add(node) # 记录第一次出栈,第一次出栈的目的是为了把左右儿子和自己按次序入栈 - - stack.append(node) # 后序遍历是'左右中',节点自己最先入栈,最后出栈 + # visited 当前为 False, 表示初次访问本节点,此次访问的目的是“把自己和两个儿子在栈中安排好位次” + # 后序遍历是'左右中',节点自己最先入栈,最后出栈。 + # 同时,设置 visited 为 True,表示下次再访问本节点时,允许收割。 + stack.append((node, True)) if node.right: - stack.append(node.right) # 右儿子位置居中 + stack.append((node.right, False)) # 右儿子位置居中 if node.left: - stack.append(node.left) # 左儿子最后入栈,最先出栈 + stack.append((node.left, False)) # 左儿子最后入栈,最先出栈 + + return values ``` ### Go: From 6c2e5a0c5e6b0a6d520c80331c99c53d80ea3b1a Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Tue, 5 Nov 2024 14:42:35 +0800 Subject: [PATCH 1410/1533] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md=20=E4=B8=BA"bo?= =?UTF-8?q?olean=E6=A0=87=E8=AE=B0=E6=B3=95"=E5=8A=A0=E4=B8=8AC++=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\350\277\255\344\273\243\346\263\225.md" | 79 ++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 412aec8cde..037cf1109b 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -36,7 +36,7 @@ ### 迭代法中序遍历 -中序遍历代码如下:(详细注释) +> 中序遍历(空指针标记法)代码如下:(详细注释) ```CPP class Solution { @@ -75,6 +75,45 @@ public: 可以看出我们将访问的节点直接加入到栈中,但如果是处理的节点则后面放入一个空节点, 这样只有空节点弹出的时候,才将下一个节点放进结果集。 +> 中序遍历(boolean 标记法): +```c++ +class Solution { +public: + vector inorderTraversal(TreeNode* root) { + vector result; + stack> st; + if (root != nullptr) + st.push(make_pair(root, false)); // 多加一个参数,false 为默认值,含义见下文注释 + + while (!st.empty()) { + auto node = st.top().first; + auto visited = st.top().second; //多加一个 visited 参数,使“迭代统一写法”成为一件简单的事 + st.pop(); + + if (visited) { // visited 为 True,表示该节点和两个儿子位次之前已经安排过了,现在可以收割节点了 + result.push_back(node->val); + continue; + } + + // visited 当前为 false, 表示初次访问本节点,此次访问的目的是“把自己和两个儿子在栈中安排好位次”。 + + // 中序遍历是'左中右',右儿子最先入栈,最后出栈。 + if (node->right) + st.push(make_pair(node->right, false)); + + // 把自己加回到栈中,位置居中。 + // 同时,设置 visited 为 true,表示下次再访问本节点时,允许收割。 + st.push(make_pair(node, true)); + + if (node->left) + st.push(make_pair(node->left, false)); // 左儿子最后入栈,最先出栈 + } + + return result; + } +}; +``` + 此时我们再来看前序遍历代码。 ### 迭代法前序遍历 @@ -110,7 +149,7 @@ public: ### 迭代法后序遍历 -后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) +> 后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) ```CPP class Solution { @@ -141,6 +180,42 @@ public: }; ``` +> 迭代法后序遍历(boolean 标记法): +```c++ +class Solution { +public: + vector postorderTraversal(TreeNode* root) { + vector result; + stack> st; + if (root != nullptr) + st.push(make_pair(root, false)); // 多加一个参数,false 为默认值,含义见下文 + + while (!st.empty()) { + auto node = st.top().first; + auto visited = st.top().second; //多加一个 visited 参数,使“迭代统一写法”成为一件简单的事 + st.pop(); + + if (visited) { // visited 为 True,表示该节点和两个儿子位次之前已经安排过了,现在可以收割节点了 + result.push_back(node->val); + continue; + } + + // visited 当前为 false, 表示初次访问本节点,此次访问的目的是“把自己和两个儿子在栈中安排好位次”。 + // 后序遍历是'左右中',节点自己最先入栈,最后出栈。 + // 同时,设置 visited 为 true,表示下次再访问本节点时,允许收割。 + st.push(make_pair(node, true)); + + if (node->right) + st.push(make_pair(node->right, false)); // 右儿子位置居中 + + if (node->left) + st.push(make_pair(node->left, false)); // 左儿子最后入栈,最先出栈 + } + + return result; + } +}; +``` ## 总结 此时我们写出了统一风格的迭代法,不用在纠结于前序写出来了,中序写不出来的情况了。 From 85241be1ea8a07b6449b2ef24d4abfacf4211bc1 Mon Sep 17 00:00:00 2001 From: DengSchoo <46556279+DengSchoo@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:41:16 +0800 Subject: [PATCH 1411/1533] chore: create .gitignore file create a .gitignore file to avoid uploading useless files, such as ".DS_Store". --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..4bd29f751f --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea/ +.DS_Store +.vscode +.temp +.cache +*.iml +__pycache__ From 55e8b33e1d9f9236b939d2be7a0160eecfb2beb8 Mon Sep 17 00:00:00 2001 From: DengSchoo <46556279+DengSchoo@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:42:16 +0800 Subject: [PATCH 1412/1533] chore: remove useless file ".DS_Store" --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index f7ce0fd5038358c7a0df77b968ed993daff0639c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKF-`+P3>-s>lW0;>?icvMDhe;i2T%y2IdUiw)YtKC#$F>OS}4*`z*zFmt?k*> zO>sU0u+`h{4p;&h=#KdCr8m#JcXpK#M~gE?JmCe$-SKhks=p67cfczSn6SkOe>+Vh z5ABoho5Oyb_Rq|DUM2;kfE17dQa}n^q(HXM;pQTiilu-Q_zw#B_o2`oYvSB6J{??Q z1RyRL4&!so62#&GVojVIGD5RPB{r(%h+&P+c*(k&I5%u`SlrBW@@C5k#o~6xOO(TE zL#5>MJ5G)LxE2gD;uo< From a4f9f01eb48b2a7d929154be69fd6cfe9dc9ea19 Mon Sep 17 00:00:00 2001 From: MAX <61301100+miaoxu404@users.noreply.github.com> Date: Tue, 5 Nov 2024 20:52:38 -0700 Subject: [PATCH 1413/1533] =?UTF-8?q?Update=200101.=E5=AD=A4=E5=B2=9B?= =?UTF-8?q?=E7=9A=84=E6=80=BB=E9=9D=A2=E7=A7=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对101.孤岛的总面积的python解法深搜版的补充,以及对广搜版的部分修改 --- ...04\346\200\273\351\235\242\347\247\257.md" | 67 +++++++++++++++++-- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 006484defb..fb2eb747e4 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -257,14 +257,62 @@ public class Main { ### Python +#### 深搜版 +```python +position = [[1, 0], [0, 1], [-1, 0], [0, -1]] +count = 0 + +def dfs(grid, x, y): + global count + grid[x][y] = 0 + count += 1 + for i, j in position: + next_x = x + i + next_y = y + j + if next_x < 0 or next_y < 0 or next_x >= len(grid) or next_y >= len(grid[0]): + continue + if grid[next_x][next_y] == 1: + dfs(grid, next_x, next_y) + +n, m = map(int, input().split()) + +# 邻接矩阵 +grid = [] +for i in range(n): + grid.append(list(map(int, input().split()))) + +# 清除边界上的连通分量 +for i in range(n): + if grid[i][0] == 1: + dfs(grid, i, 0) + if grid[i][m - 1] == 1: + dfs(grid, i, m - 1) + +for j in range(m): + if grid[0][j] == 1: + dfs(grid, 0, j) + if grid[n - 1][j] == 1: + dfs(grid, n - 1, j) + +count = 0 # 将count重置为0 +# 统计内部所有剩余的连通分量 +for i in range(n): + for j in range(m): + if grid[i][j] == 1: + dfs(grid, i, j) + +print(count) +``` + +#### 广搜版 ```python from collections import deque # 处理输入 -n, m = list(map(int, input().strip())) +n, m = list(map(int, input().split())) g = [] for _ in range(n): - row = list(map(int, input().strip())) + row = list(map(int, input().split())) g.append(row) # 定义四个方向、孤岛面积(遍历完边缘后会被重置) @@ -293,17 +341,22 @@ def bfs(r, c): for i in range(n): - if g[i][0] == 1: bfs(i, 0) - if g[i][m-1] == 1: bfs(i, m-1) + if g[i][0] == 1: + bfs(i, 0) + if g[i][m-1] == 1: + bfs(i, m-1) for i in range(m): - if g[0][i] == 1: bfs(0, i) - if g[n-1][i] == 1: bfs(n-1, i) + if g[0][i] == 1: + bfs(0, i) + if g[n-1][i] == 1: + bfs(n-1, i) count = 0 for i in range(n): for j in range(m): - if g[i][j] == 1: bfs(i, j) + if g[i][j] == 1: + bfs(i, j) print(count) ``` From 9e6cc0e627c20a28c8928e76cb8cde342a5e0b25 Mon Sep 17 00:00:00 2001 From: swjtuhjf Date: Thu, 7 Nov 2024 23:44:18 +0800 Subject: [PATCH 1414/1533] =?UTF-8?q?0053.=E5=AF=BB=E5=AE=9D-prim=201.=20?= =?UTF-8?q?=E5=88=A0=E6=8E=89=E6=96=87=E7=AB=A0=E4=B8=AD=E5=86=97=E4=BD=99?= =?UTF-8?q?=E7=9A=84=E7=A9=BA=E6=A0=BC=EF=BC=8C=E4=BD=86=E4=B8=8D=E5=8C=85?= =?UTF-8?q?=E6=8B=AC1=E4=BB=A3=E7=A0=81=E4=B8=AD=E7=9A=84=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC=E5=92=8C2=E7=BB=93=E6=9E=84=E6=80=A7=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC(=E5=A6=82=E6=A0=87=E9=A2=98=E5=86=85)=E7=AD=89?= =?UTF-8?q?=E5=90=88=E7=90=86=E7=A9=BA=E6=A0=BC=202.=20=E5=B0=86=E6=80=BB?= =?UTF-8?q?=E7=BB=93=E9=83=A8=E5=88=86=E5=80=92=E6=95=B0=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E5=8F=A5=E4=B8=AD"=E5=A6=82=E4=BD=95=E6=B1=82=E8=81=8C?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=94=9F=E6=88=90=E6=A0=91=E7=9A=84=E6=AF=8F?= =?UTF-8?q?=E4=B8=80=E6=9D=A1=E8=BE=B9"=E7=9A=84"=E6=B1=82=E8=81=8C"?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA"=E8=8E=B7=E5=BE=97"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0053.\345\257\273\345\256\235-prim.md" | 280 +++++++++--------- 1 file changed, 140 insertions(+), 140 deletions(-) diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index a8dad4cbfc..d2c9578a89 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -9,17 +9,17 @@ 在世界的某个区域,有一些分散的神秘岛屿,每个岛屿上都有一种珍稀的资源或者宝藏。国王打算在这些岛屿上建公路,方便运输。 -不同岛屿之间,路途距离不同,国王希望你可以规划建公路的方案,如何可以以最短的总公路距离将 所有岛屿联通起来。 +不同岛屿之间,路途距离不同,国王希望你可以规划建公路的方案,如何可以以最短的总公路距离将所有岛屿联通起来。 给定一张地图,其中包括了所有的岛屿,以及它们之间的距离。以最小化公路建设长度,确保可以链接到所有岛屿。 输入描述: -第一行包含两个整数V 和 E,V代表顶点数,E代表边数 。顶点编号是从1到V。例如:V=2,一个有两个顶点,分别是1和2。 +第一行包含两个整数V和E,V代表顶点数,E代表边数。顶点编号是从1到V。例如:V=2,一个有两个顶点,分别是1和2。 -接下来共有 E 行,每行三个整数 v1,v2 和 val,v1 和 v2 为边的起点和终点,val代表边的权值。 +接下来共有E行,每行三个整数v1,v2和val,v1和v2为边的起点和终点,val代表边的权值。 -输出描述: +输出描述: 输出联通所有岛屿的最小路径总距离 @@ -38,65 +38,65 @@ 5 6 2 5 7 1 6 7 1 -``` +``` -输出示例: +输出示例: 6 -## 解题思路 +## 解题思路 -本题是最小生成树的模板题,那么我们来讲一讲最小生成树。 +本题是最小生成树的模板题,那么我们来讲一讲最小生成树。 -最小生成树 可以使用 prim算法 也可以使用 kruskal算法计算出来。 +最小生成树可以使用prim算法也可以使用kruskal算法计算出来。 -本篇我们先讲解 prim算法。 +本篇我们先讲解prim算法。 -最小生成树是所有节点的最小连通子图, 即:以最小的成本(边的权值)将图中所有节点链接到一起。 +最小生成树是所有节点的最小连通子图,即:以最小的成本(边的权值)将图中所有节点链接到一起。 -图中有n个节点,那么一定可以用 n - 1 条边将所有节点连接到一起。 +图中有n个节点,那么一定可以用n-1条边将所有节点连接到一起。 -那么如何选择 这 n-1 条边 就是 最小生成树算法的任务所在。 +那么如何选择这n-1条边就是最小生成树算法的任务所在。 -例如本题示例中的无向有权图为: +例如本题示例中的无向有权图为: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231206164306.png) -那么在这个图中,如何选取 n-1 条边 使得 图中所有节点连接到一起,并且边的权值和最小呢? +那么在这个图中,如何选取n-1条边使得图中所有节点连接到一起,并且边的权值和最小呢? -(图中为n为7,即7个节点,那么只需要 n-1 即 6条边就可以讲所有顶点连接到一起) +(图中为n为7,即7个节点,那么只需要n-1即6条边就可以讲所有顶点连接到一起) -prim算法 是从节点的角度 采用贪心的策略 每次寻找距离 最小生成树最近的节点 并加入到最小生成树中。 +prim算法是从节点的角度采用贪心的策略每次寻找距离最小生成树最近的节点并加入到最小生成树中。 -prim算法核心就是三步,我称为**prim三部曲**,大家一定要熟悉这三步,代码相对会好些很多: +prim算法核心就是三步,我称为**prim三部曲**,大家一定要熟悉这三步,代码相对会好些很多: 1. 第一步,选距离生成树最近节点 -2. 第二步,最近节点加入生成树 +2. 第二步,最近节点加入生成树 3. 第三步,更新非生成树节点到生成树的距离(即更新minDist数组) -现在录友们会对这三步很陌生,不知道这是干啥的,没关系,下面将会画图举例来带大家把这**prim三部曲**理解到位。 +现在录友们会对这三步很陌生,不知道这是干啥的,没关系,下面将会画图举例来带大家把这**prim三部曲**理解到位。 在prim算法中,有一个数组特别重要,这里我起名为:minDist。 -刚刚我有讲过 “每次寻找距离 最小生成树最近的节点 并加入到最小生成树中”,那么如何寻找距离最小生成树最近的节点呢? +刚刚我有讲过“每次寻找距离最小生成树最近的节点并加入到最小生成树中”,那么如何寻找距离最小生成树最近的节点呢? -这就用到了 minDist 数组, 它用来作什么呢? +这就用到了minDist数组,它用来作什么呢? -**minDist数组 用来记录 每一个节点距离最小生成树的最近距离**。 理解这一点非常重要,这也是 prim算法最核心要点所在,很多录友看不懂prim算法的代码,都是因为没有理解透 这个数组的含义。 +**minDist数组用来记录每一个节点距离最小生成树的最近距离**。理解这一点非常重要,这也是prim算法最核心要点所在,很多录友看不懂prim算法的代码,都是因为没有理解透这个数组的含义。 -接下来,我们来通过一步一步画图,来带大家巩固 **prim三部曲** 以及 minDist数组 的作用。 +接下来,我们来通过一步一步画图,来带大家巩固**prim三部曲**以及minDist数组的作用。 -(**示例中节点编号是从1开始,所以为了让大家看的不晕,minDist数组下标我也从 1 开始计数,下标0 就不使用了,这样 下标和节点标号就可以对应上了,避免大家搞混**) +(**示例中节点编号是从1开始,所以为了让大家看的不晕,minDist数组下标我也从1开始计数,下标0就不使用了,这样下标和节点标号就可以对应上了,避免大家搞混**) -### 1 初始状态 +### 1 初始状态 -minDist 数组 里的数值初始化为 最大数,因为本题 节点距离不会超过 10000,所以 初始化最大数为 10001就可以。 +minDist数组里的数值初始化为最大数,因为本题节点距离不会超过10000,所以初始化最大数为10001就可以。 -相信这里录友就要问了,为什么这么做? +相信这里录友就要问了,为什么这么做? -现在 还没有最小生成树,默认每个节点距离最小生成树是最大的,这样后面我们在比较的时候,发现更近的距离,才能更新到 minDist 数组上。 +现在还没有最小生成树,默认每个节点距离最小生成树是最大的,这样后面我们在比较的时候,发现更近的距离,才能更新到minDist数组上。 如图: @@ -108,125 +108,125 @@ minDist 数组 里的数值初始化为 最大数,因为本题 节点距离不 1、prim三部曲,第一步:选距离生成树最近节点 -选择距离最小生成树最近的节点,加入到最小生成树,刚开始还没有最小生成树,所以随便选一个节点加入就好(因为每一个节点一定会在最小生成树里,所以随便选一个就好),那我们选择节点1 (符合遍历数组的习惯,第一个遍历的也是节点1) +选择距离最小生成树最近的节点,加入到最小生成树,刚开始还没有最小生成树,所以随便选一个节点加入就好(因为每一个节点一定会在最小生成树里,所以随便选一个就好),那我们选择节点1(符合遍历数组的习惯,第一个遍历的也是节点1) -2、prim三部曲,第二步:最近节点加入生成树 +2、prim三部曲,第二步:最近节点加入生成树 -此时 节点1 已经算最小生成树的节点。 +此时节点1已经算最小生成树的节点。 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) -接下来,我们要更新所有节点距离最小生成树的距离,如图: +接下来,我们要更新所有节点距离最小生成树的距离,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102048.png) -注意下标0,我们就不管它了,下标 1 与节点 1 对应,这样可以避免大家把节点搞混。 +注意下标0,我们就不管它了,下标1与节点1对应,这样可以避免大家把节点搞混。 -此时所有非生成树的节点距离 最小生成树(节点1)的距离都已经跟新了 。 +此时所有非生成树的节点距离最小生成树(节点1)的距离都已经跟新了。 -* 节点2 与 节点1 的距离为1,比原先的 距离值10001小,所以更新minDist[2]。 -* 节点3 和 节点1 的距离为1,比原先的 距离值10001小,所以更新minDist[3]。 -* 节点5 和 节点1 的距离为2,比原先的 距离值10001小,所以更新minDist[5]。 +* 节点2与节点1的距离为1,比原先的距离值10001小,所以更新minDist[2]。 +* 节点3和节点1的距离为1,比原先的距离值10001小,所以更新minDist[3]。 +* 节点5和节点1的距离为2,比原先的距离值10001小,所以更新minDist[5]。 -**注意图中我标记了 minDist数组里更新的权值**,是哪两个节点之间的权值,例如 minDist[2] =1 ,这个 1 是 节点1 与 节点2 之间的连线,清楚这一点对最后我们记录 最小生成树的权值总和很重要。 +**注意图中我标记了minDist数组里更新的权值**,是哪两个节点之间的权值,例如minDist[2]=1,这个1是节点1与节点2之间的连线,清楚这一点对最后我们记录最小生成树的权值总和很重要。 -(我在后面依然会不断重复 prim三部曲,可能基础好的录友会感觉有点啰嗦,但也是让大家感觉这三部曲求解的过程) +(我在后面依然会不断重复prim三部曲,可能基础好的录友会感觉有点啰嗦,但也是让大家感觉这三部曲求解的过程) -### 3 +### 3 1、prim三部曲,第一步:选距离生成树最近节点 -选取一个距离 最小生成树(节点1) 最近的非生成树里的节点,节点2,3,5 距离 最小生成树(节点1) 最近,选节点 2(其实选 节点3或者节点2都可以,距离一样的)加入最小生成树。 +选取一个距离最小生成树(节点1)最近的非生成树里的节点,节点2,3,5距离最小生成树(节点1)最近,选节点2(其实选节点3或者节点2都可以,距离一样的)加入最小生成树。 -2、prim三部曲,第二步:最近节点加入生成树 +2、prim三部曲,第二步:最近节点加入生成树 -此时 节点1 和 节点2,已经算最小生成树的节点。 +此时节点1和节点2,已经算最小生成树的节点。 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) -接下来,我们要更新节点距离最小生成树的距离,如图: +接下来,我们要更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102431.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102431.png) -此时所有非生成树的节点距离 最小生成树(节点1、节点2)的距离都已经跟新了 。 +此时所有非生成树的节点距离最小生成树(节点1、节点2)的距离都已经跟新了。 -* 节点3 和 节点2 的距离为2,和原先的距离值1 小,所以不用更新。 -* 节点4 和 节点2 的距离为2,比原先的距离值10001小,所以更新minDist[4]。 -* 节点5 和 节点2 的距离为10001(不连接),所以不用更新。 -* 节点6 和 节点2 的距离为1,比原先的距离值10001小,所以更新minDist[6]。 +* 节点3和节点2的距离为2,和原先的距离值1小,所以不用更新。 +* 节点4和节点2的距离为2,比原先的距离值10001小,所以更新minDist[4]。 +* 节点5和节点2的距离为10001(不连接),所以不用更新。 +* 节点6和节点2的距离为1,比原先的距离值10001小,所以更新minDist[6]。 -### 4 +### 4 1、prim三部曲,第一步:选距离生成树最近节点 -选择一个距离 最小生成树(节点1、节点2) 最近的非生成树里的节点,节点3,6 距离 最小生成树(节点1、节点2) 最近,选节点3 (选节点6也可以,距离一样)加入最小生成树。 +选择一个距离最小生成树(节点1、节点2)最近的非生成树里的节点,节点3,6距离最小生成树(节点1、节点2)最近,选节点3(选节点6也可以,距离一样)加入最小生成树。 -2、prim三部曲,第二步:最近节点加入生成树 +2、prim三部曲,第二步:最近节点加入生成树 -此时 节点1 、节点2 、节点3 算是最小生成树的节点。 +此时节点1、节点2、节点3算是最小生成树的节点。 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) -接下来更新节点距离最小生成树的距离,如图: +接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102457.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102457.png) -所有非生成树的节点距离 最小生成树(节点1、节点2、节点3 )的距离都已经跟新了 。 +所有非生成树的节点距离最小生成树(节点1、节点2、节点3)的距离都已经跟新了。 -* 节点 4 和 节点 3的距离为 1,和原先的距离值 2 小,所以更新minDist[4]为1。 +* 节点4和节点3的距离为1,和原先的距离值2小,所以更新minDist[4]为1。 -上面为什么我们只比较 节点4 和 节点3 的距离呢? +上面为什么我们只比较节点4和节点3的距离呢? -因为节点3加入 最小生成树后,非 生成树节点 只有 节点 4 和 节点3是链接的,所以需要重新更新一下 节点4距离最小生成树的距离,其他节点距离最小生成树的距离 都不变。 +因为节点3加入最小生成树后,非生成树节点只有节点4和节点3是链接的,所以需要重新更新一下节点4距离最小生成树的距离,其他节点距离最小生成树的距离都不变。 -### 5 +### 5 1、prim三部曲,第一步:选距离生成树最近节点 -继续选择一个距离 最小生成树(节点1、节点2、节点3) 最近的非生成树里的节点,为了巩固大家对 minDist数组的理解,这里我再啰嗦一遍: +继续选择一个距离最小生成树(节点1、节点2、节点3)最近的非生成树里的节点,为了巩固大家对minDist数组的理解,这里我再啰嗦一遍: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231217213516.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231217213516.png) -**minDist数组 是记录了 所有非生成树节点距离生成树的最小距离**,所以 从数组里我们能看出来,非生成树节点 4 和 节点 6 距离 生成树最近。 +**minDist数组是记录了所有非生成树节点距离生成树的最小距离**,所以从数组里我们能看出来,非生成树节点4和节点6距离生成树最近。 -任选一个加入生成树,我们选 节点4(选节点6也行) 。 +任选一个加入生成树,我们选节点4(选节点6也行)。 -**注意**,我们根据 minDist数组,选取距离 生成树 最近的节点 加入生成树,那么 **minDist数组里记录的其实也是 最小生成树的边的权值**(我在图中把权值对应的是哪两个节点也标记出来了)。 +**注意**,我们根据minDist数组,选取距离生成树最近的节点加入生成树,那么**minDist数组里记录的其实也是最小生成树的边的权值**(我在图中把权值对应的是哪两个节点也标记出来了)。 -如果大家不理解,可以跟着我们下面的讲解,看 minDist数组的变化, minDist数组 里记录的权值对应的哪条边。 +如果大家不理解,可以跟着我们下面的讲解,看minDist数组的变化,minDist数组里记录的权值对应的哪条边。 -理解这一点很重要,因为 最后我们要求 最小生成树里所有边的权值和。 +理解这一点很重要,因为最后我们要求最小生成树里所有边的权值和。 -2、prim三部曲,第二步:最近节点加入生成树 +2、prim三部曲,第二步:最近节点加入生成树 -此时 节点1、节点2、节点3、节点4 算是 最小生成树的节点。 +此时节点1、节点2、节点3、节点4算是最小生成树的节点。 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) -接下来更新节点距离最小生成树的距离,如图: +接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102618.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102618.png) -minDist数组已经更新了 所有非生成树的节点距离 最小生成树(节点1、节点2、节点3、节点4 )的距离 。 +minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4)的距离。 -* 节点 5 和 节点 4的距离为 1,和原先的距离值 2 小,所以更新minDist[5]为1。 +* 节点5和节点4的距离为1,和原先的距离值2小,所以更新minDist[5]为1。 -### 6 +### 6 1、prim三部曲,第一步:选距离生成树最近节点 -继续选距离 最小生成树(节点1、节点2、节点3、节点4 )最近的非生成树里的节点,只有 节点 5 和 节点6。 +继续选距离最小生成树(节点1、节点2、节点3、节点4)最近的非生成树里的节点,只有节点5和节点6。 -选节点5 (选节点6也可以)加入 生成树。 +选节点5(选节点6也可以)加入生成树。 -2、prim三部曲,第二步:最近节点加入生成树 +2、prim三部曲,第二步:最近节点加入生成树 -节点1、节点2、节点3、节点4、节点5 算是 最小生成树的节点。 +节点1、节点2、节点3、节点4、节点5算是最小生成树的节点。 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) @@ -234,44 +234,44 @@ minDist数组已经更新了 所有非生成树的节点距离 最小生成树 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102646.png) -minDist数组已经更新了 所有非生成树的节点距离 最小生成树(节点1、节点2、节点3、节点4 、节点5)的距离 。 +minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4、节点5)的距离。 -* 节点 6 和 节点 5 距离为 2,比原先的距离值 1 大,所以不更新 -* 节点 7 和 节点 5 距离为 1,比原先的距离值 10001小,更新 minDist[7] +* 节点6和节点5距离为2,比原先的距离值1大,所以不更新 +* 节点7和节点5距离为1,比原先的距离值10001小,更新minDist[7] -### 7 +### 7 1、prim三部曲,第一步:选距离生成树最近节点 -继续选距离 最小生成树(节点1、节点2、节点3、节点4 、节点5)最近的非生成树里的节点,只有 节点 6 和 节点7。 +继续选距离最小生成树(节点1、节点2、节点3、节点4、节点5)最近的非生成树里的节点,只有节点6和节点7。 -2、prim三部曲,第二步:最近节点加入生成树 +2、prim三部曲,第二步:最近节点加入生成树 -选节点6 (选节点7也行,距离一样的)加入生成树。 +选节点6(选节点7也行,距离一样的)加入生成树。 3、prim三部曲,第三步:更新非生成树节点到生成树的距离(即更新minDist数组) -节点1、节点2、节点3、节点4、节点5、节点6 算是 最小生成树的节点 ,接下来更新节点距离最小生成树的距离,如图: +节点1、节点2、节点3、节点4、节点5、节点6算是最小生成树的节点,接下来更新节点距离最小生成树的距离,如图: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102732.png) -这里就不在重复描述了,大家类推,最后,节点7加入生成树,如图: +这里就不在重复描述了,大家类推,最后,节点7加入生成树,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102820.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102820.png) -### 最后 +### 最后 -最后我们就生成了一个 最小生成树, 绿色的边将所有节点链接到一起,并且 保证权值是最小的,因为我们在更新 minDist 数组的时候,都是选距离 最小生成树最近的点 加入到树中。 +最后我们就生成了一个最小生成树,绿色的边将所有节点链接到一起,并且保证权值是最小的,因为我们在更新minDist数组的时候,都是选距离最小生成树最近的点加入到树中。 -讲解上面的模拟过程的时候,我已经强调多次 minDist数组 是记录了 所有非生成树节点距离生成树的最小距离。 +讲解上面的模拟过程的时候,我已经强调多次minDist数组是记录了所有非生成树节点距离生成树的最小距离。 -最后,minDist数组 也就是记录的是最小生成树所有边的权值。 +最后,minDist数组也就是记录的是最小生成树所有边的权值。 -我在图中,特别把 每条边的权值对应的是哪两个节点 标记出来(例如minDist[7] = 1,对应的是节点5 和 节点7之间的边,而不是 节点6 和 节点7),为了就是让大家清楚, minDist里的每一个值 对应的是哪条边。 +我在图中,特别把每条边的权值对应的是哪两个节点标记出来(例如minDist[7]=1,对应的是节点5和节点7之间的边,而不是节点6和节点7),为了就是让大家清楚,minDist里的每一个值对应的是哪条边。 -那么我们要求最小生成树里边的权值总和 就是 把 最后的 minDist 数组 累加一起。 +那么我们要求最小生成树里边的权值总和就是把最后的minDist数组累加一起。 -以下代码,我对 prim三部曲,做了重点注释,大家根据这三步,就可以 透彻理解prim。 +以下代码,我对prim三部曲,做了重点注释,大家根据这三步,就可以透彻理解prim。 ```CPP #include @@ -338,52 +338,52 @@ int main() { } -``` +``` -时间复杂度为 O(n^2),其中 n 为节点数量。 +时间复杂度为O(n^2),其中n为节点数量。 ## 拓展 -上面讲解的是记录了最小生成树 所有边的权值,如果让打印出来 最小生成树的每条边呢? 或者说 要把这个最小生成树画出来呢? +上面讲解的是记录了最小生成树所有边的权值,如果让打印出来最小生成树的每条边呢?或者说要把这个最小生成树画出来呢? -此时我们就需要把 最小生成树里每一条边记录下来。 +此时我们就需要把最小生成树里每一条边记录下来。 -此时有两个问题: +此时有两个问题: -* 1、用什么结构来记录 -* 2、如何记录 +* 1、用什么结构来记录 +* 2、如何记录 -如果记录边,其实就是记录两个节点就可以,两个节点连成一条边。 +如果记录边,其实就是记录两个节点就可以,两个节点连成一条边。 如何记录两个节点呢? -我们使用一维数组就可以记录。 parent[节点编号] = 节点编号, 这样就把一条边记录下来了。(当然如果节点编号非常大,可以考虑使用map) +我们使用一维数组就可以记录。parent[节点编号] = 节点编号,这样就把一条边记录下来了。(当然如果节点编号非常大,可以考虑使用map) -使用一维数组记录是有向边,不过我们这里不需要记录方向,所以只关注两条边是连接的就行。 +使用一维数组记录是有向边,不过我们这里不需要记录方向,所以只关注两条边是连接的就行。 -parent数组初始化代码: +parent数组初始化代码: ```CPP vector parent(v + 1, -1); ``` -接下来就是第二个问题,如何记录? +接下来就是第二个问题,如何记录? -我们再来回顾一下 prim三部曲, +我们再来回顾一下prim三部曲, 1. 第一步,选距离生成树最近节点 -2. 第二步,最近节点加入生成树 +2. 第二步,最近节点加入生成树 3. 第三步,更新非生成树节点到生成树的距离(即更新minDist数组) -大家先思考一下,我们是在第几步,可以记录 最小生成树的边呢? +大家先思考一下,我们是在第几步,可以记录最小生成树的边呢? -在本面上半篇 我们讲解过:“我们根据 minDist数组,选组距离 生成树 最近的节点 加入生成树,那么 **minDist数组里记录的其实也是 最小生成树的边的权值**。” +在本面上半篇我们讲解过:“我们根据minDist数组,选组距离生成树最近的节点加入生成树,那么**minDist数组里记录的其实也是最小生成树的边的权值**。” -既然 minDist数组 记录了 最小生成树的边,是不是就是在更新 minDist数组 的时候,去更新parent数组来记录一下对应的边呢。 +既然minDist数组记录了最小生成树的边,是不是就是在更新minDist数组的时候,去更新parent数组来记录一下对应的边呢。 -所以 在 prim三部曲中的第三步,更新 parent数组,代码如下: +所以在prim三部曲中的第三步,更新parent数组,代码如下: ```CPP for (int j = 1; j <= v; j++) { @@ -394,23 +394,23 @@ for (int j = 1; j <= v; j++) { } ``` -代码中注释中,我强调了 数组指向的顺序很重要。 因为不少录友在这里会写成这样: `parent[cur] = j` 。 +代码中注释中,我强调了数组指向的顺序很重要。因为不少录友在这里会写成这样: `parent[cur] = j` 。 -这里估计大家会疑惑了,parent[节点编号A] = 节点编号B, 就表示A 和 B 相连,我们这里就不用在意方向,代码中 为什么 只能 `parent[j] = cur` 而不能 `parent[cur] = j` 这么写呢? +这里估计大家会疑惑了,parent[节点编号A] = 节点编号B,就表示A和B相连,我们这里就不用在意方向,代码中为什么只能 `parent[j] = cur` 而不能 `parent[cur] = j` 这么写呢? -如果写成 `parent[cur] = j`,在 for 循环中,有多个 j 满足要求, 那么 parent[cur] 就会被反复覆盖,因为 cur 是一个固定值。 +如果写成 `parent[cur] = j`,在for循环中,有多个j满足要求,那么 parent[cur] 就会被反复覆盖,因为cur是一个固定值。 -举个例子,cur = 1, 在 for循环中,可能 就 j = 2, j = 3,j =4 都符合条件,那么本来应该记录 节点1 与 节点 2、节点3、节点4相连的。 +举个例子,cur=1,在for循环中,可能就j=2,j=3,j=4都符合条件,那么本来应该记录节点1与节点2、节点3、节点4相连的。 -如果 `parent[cur] = j` 这么写,最后更新的逻辑是 parent[1] = 2, parent[1] = 3, parent[1] = 4, 最后只能记录 节点1 与节点 4 相连,其他相连情况都被覆盖了。 +如果 `parent[cur] = j` 这么写,最后更新的逻辑是 parent[1] = 2, parent[1] = 3, parent[1] = 4,最后只能记录节点1与节点4相连,其他相连情况都被覆盖了。 -如果这么写 `parent[j] = cur`, 那就是 parent[2] = 1, parent[3] = 1, parent[4] = 1 ,这样 才能完整表示出 节点1 与 其他节点都是链接的,才没有被覆盖。 +如果这么写 `parent[j] = cur`,那就是 parent[2] = 1, parent[3] = 1, parent[4] = 1 ,这样才能完整表示出节点1与其他节点都是链接的,才没有被覆盖。 主要问题也是我们使用了一维数组来记录。 -如果是二维数组,来记录两个点链接,例如 parent[节点编号A][节点编号B] = 1 ,parent[节点编号B][节点编号A] = 1,来表示 节点A 与 节点B 相连,那就没有上面说的这个注意事项了,当然这么做的话,就是多开辟的内存空间。 +如果是二维数组,来记录两个点链接,例如 parent[节点编号A][节点编号B] = 1 ,parent[节点编号B][节点编号A] = 1,来表示节点A与节点B相连,那就没有上面说的这个注意事项了,当然这么做的话,就是多开辟的内存空间。 -以下是输出最小生成树边的代码,不算最后输出, 就额外添加了两行代码,我都注释标记了: +以下是输出最小生成树边的代码,不算最后输出,就额外添加了两行代码,我都注释标记了: ```CPP #include @@ -460,7 +460,7 @@ int main() { } } -``` +``` 按照本题示例,代码输入如下: @@ -476,40 +476,40 @@ int main() { 注意,这里是无向图,我在输出上添加了箭头仅仅是为了方便大家看出是边的意思。 -大家可以和我们本题最后生成的最小生成树的图 去对比一下 边的链接情况: +大家可以和我们本题最后生成的最小生成树的图去对比一下边的链接情况: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231229115714.png) -绿色的边 是最小生成树,和我们的 输出完全一致。 +绿色的边是最小生成树,和我们的输出完全一致。 -## 总结 +## 总结 -此时我就把prim算法讲解完毕了,我们再来回顾一下。 +此时我就把prim算法讲解完毕了,我们再来回顾一下。 -关于 prim算法,我自创了三部曲,来帮助大家理解: +关于prim算法,我自创了三部曲,来帮助大家理解: 1. 第一步,选距离生成树最近节点 -2. 第二步,最近节点加入生成树 +2. 第二步,最近节点加入生成树 3. 第三步,更新非生成树节点到生成树的距离(即更新minDist数组) -大家只要理解这三部曲, prim算法 至少是可以写出一个框架出来,然后在慢慢补充细节,这样不至于 自己在写prim的时候 两眼一抹黑 完全凭感觉去写。 -这也为什么很多录友感觉 prim算法比较难,而且每次学会来,隔一段时间 又不会写了,主要是 没有一个纲领。 +大家只要理解这三部曲,prim算法至少是可以写出一个框架出来,然后在慢慢补充细节,这样不至于自己在写prim的时候两眼一抹黑完全凭感觉去写。 +这也为什么很多录友感觉prim算法比较难,而且每次学会来,隔一段时间又不会写了,主要是没有一个纲领。 -理解这三部曲之后,更重要的 就是理解 minDist数组。 +理解这三部曲之后,更重要的就是理解minDist数组。 -**minDist数组 是prim算法的灵魂,它帮助 prim算法完成最重要的一步,就是如何找到 距离最小生成树最近的点**。 +**minDist数组是prim算法的灵魂,它帮助prim算法完成最重要的一步,就是如何找到距离最小生成树最近的点**。 -再来帮大家回顾 minDist数组 的含义:记录 每一个节点距离最小生成树的最近距离。 +再来帮大家回顾minDist数组的含义:记录每一个节点距离最小生成树的最近距离。 -理解 minDist数组 ,至少大家看prim算法的代码不会懵。 +理解minDist数组,至少大家看prim算法的代码不会懵。 -也正是 因为 minDist数组 的作用,我们根据 minDist数组,选取距离 生成树 最近的节点 加入生成树,那么 **minDist数组里记录的其实也是 最小生成树的边的权值**。 +也正是因为minDist数组的作用,我们根据minDist数组,选取距离生成树最近的节点加入生成树,那么**minDist数组里记录的其实也是最小生成树的边的权值**。 -所以我们求 最小生成树的权值和 就是 计算后的 minDist数组 数值总和。 +所以我们求最小生成树的权值和就是计算后的minDist数组数值总和。 -最后我们拓展了如何求职 最小生成树 的每一条边,其实 添加的代码很简单,主要是理解 为什么使用 parent数组 来记录边 以及 在哪里 更新parent数组。 +最后我们拓展了如何获得最小生成树的每一条边,其实添加的代码很简单,主要是理解为什么使用parent数组来记录边以及在哪里更新parent数组。 -同时,因为使用一维数组,数组的下标和数组 如何赋值很重要,不要搞反,导致结果被覆盖。 +同时,因为使用一维数组,数组的下标和数组如何赋值很重要,不要搞反,导致结果被覆盖。 好了,以上为总结,录友们学习愉快。 From 419db364a32ee28abcdd03a0aa1f7823a5125552 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Tue, 12 Nov 2024 10:06:56 +0800 Subject: [PATCH 1415/1533] Update --- README.md | 5 +-- ...\345\216\237IP\345\234\260\345\235\200.md" | 3 +- ...11\345\222\214\345\255\220\351\233\206.md" | 44 +++++++++++-------- ...74\232dijkstra\346\234\264\347\264\240.md" | 2 +- ...347\211\251\350\277\220\350\276\223III.md" | 4 +- ...7\232\204\346\224\273\345\207\273astar.md" | 2 +- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 3bb575d1a6..8255bd5a10 100644 --- a/README.md +++ b/README.md @@ -106,8 +106,8 @@ 4. [数组:977.有序数组的平方](./problems/0977.有序数组的平方.md) 5. [数组:209.长度最小的子数组](./problems/0209.长度最小的子数组.md) 6. [数组:区间和](./problems/kamacoder/0058.区间和.md) -6. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md) -8. [数组:开发商购买土地](./problems/kamacoder/0044.开发商购买土地.md) +7. [数组:开发商购买土地](./problems/kamacoder/0044.开发商购买土地.md) +8. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md) 9. [数组:总结篇](./problems/数组总结篇.md) ## 链表 @@ -196,7 +196,6 @@ 12. [二叉树:110.平衡二叉树](./problems/0110.平衡二叉树.md) 13. [二叉树:257.二叉树的所有路径](./problems/0257.二叉树的所有路径.md) 14. [本周总结!(二叉树)](./problems/周总结/20201003二叉树周末总结.md) -15. [二叉树:二叉树中递归带着回溯](./problems/二叉树中递归带着回溯.md) 16. [二叉树:404.左叶子之和](./problems/0404.左叶子之和.md) 17. [二叉树:513.找树左下角的值](./problems/0513.找树左下角的值.md) 18. [二叉树:112.路径总和](./problems/0112.路径总和.md) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index eb81f4b662..a03a0e3b15 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -376,9 +376,8 @@ class Solution { // 剪枝:ip段的长度最大是3,并且ip段处于[0,255] for (int i = start; i < s.length() && i - start < 3 && Integer.parseInt(s.substring(start, i + 1)) >= 0 && Integer.parseInt(s.substring(start, i + 1)) <= 255; i++) { - // 如果ip段的长度大于1,并且第一位为0的话,continue if (i + 1 - start > 1 && s.charAt(start) - '0' == 0) { - continue; + break; } stringBuilder.append(s.substring(start, i + 1)); // 当stringBuilder里的网段数量小于3时,才会加点;如果等于3,说明已经有3段了,最后一段不需要再加点 diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 5bd7ff6c81..55ed7ad2ff 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -47,7 +47,13 @@ 那么只要找到集合里能够出现 sum / 2 的子集总和,就算是可以分割成两个相同元素和子集了。 -本题是可以用回溯暴力搜索出所有答案的,但最后超时了,也不想再优化了,放弃回溯,直接上01背包吧。 +本题是可以用回溯暴力搜索出所有答案的,但最后超时了,也不想再优化了,放弃回溯。 + +是否有其他解法可以解决此题。 + +本题的本质是,能否把容量为 sum / 2的背包装满。 + +**这是 背包算法可以解决的经典类型题目**。 如果对01背包不够了解,建议仔细看完如下两篇: @@ -56,7 +62,7 @@ ### 01背包问题 -背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 +01背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 **背包问题有多种背包方式,常见的有:01背包、完全背包、多重背包、分组背包和混合背包等等。** @@ -64,32 +70,33 @@ **即一个商品如果可以重复多次放入是完全背包,而只能放入一次是01背包,写法还是不一样的。** -**要明确本题中我们要使用的是01背包,因为元素我们只能用一次。** +**元素我们只能用一次,如果使用背包,那么也是01背包** 回归主题:首先,本题要求集合里能否出现总和为 sum / 2 的子集。 -那么来一一对应一下本题,看看背包问题如何来解决。 +既有一个 只能装重量为 sum / 2 的背包,商品为数字,这些数字能不能把 这个背包装满。 -**只有确定了如下四点,才能把01背包问题套到本题上来。** +那每一件商品是数字的话,对应的重量 和 价值是多少呢? -* 背包的体积为sum / 2 -* 背包要放入的商品(集合里的元素)重量为 元素的数值,价值也为元素的数值 -* 背包如果正好装满,说明找到了总和为 sum / 2 的子集。 -* 背包中每一个元素是不可重复放入。 +一个数字只有一个维度,即 重量等于价值。 -以上分析完,我们就可以套用01背包,来解决这个问题了。 +当数字 可以装满 承载重量为 sum / 2 的背包的背包时,这个背包的价值也是 sum / 2。 -动规五部曲分析如下: +那么这道题就是 装满 承载重量为 sum / 2 的背包,价值最大是多少? -1. 确定dp数组以及下标的含义 +如果最大价值是 sum / 2,说明正好被商品装满了。 -01背包中,dp[j] 表示: 容量为j的背包,所背的物品价值最大可以为dp[j]。 +因为商品是数字,重量和对应的价值是相同的。 -本题中每一个元素的数值既是重量,也是价值。 +以上分析完,我们就可以直接用01背包 来解决这个问题了。 -**套到本题,dp[j]表示 背包总容量(所能装的总重量)是j,放进物品后,背的最大重量为dp[j]**。 +动规五部曲分析如下: + +1. 确定dp数组以及下标的含义 -那么如果背包容量为target, dp[target]就是装满 背包之后的重量,所以 当 dp[target] == target 的时候,背包就装满了。 +01背包中,dp[j] 表示: 容量(所能装的重量)为j的背包,所背的物品价值最大可以为dp[j]。 + +如果背包所载重量为target, dp[target]就是装满 背包之后的总价值,因为 本题中每一个元素的数值既是重量,也是价值,所以,当 dp[target] == target 的时候,背包就装满了。 有录友可能想,那还有装不满的时候? @@ -192,12 +199,11 @@ public: ## 总结 -这道题目就是一道01背包应用类的题目,需要我们拆解题目,然后套入01背包的场景。 +这道题目就是一道01背包经典应用类的题目,需要我们拆解题目,然后才能发现可以使用01背包。 01背包相对于本题,主要要理解,题目中物品是nums[i],重量是nums[i],价值也是nums[i],背包体积是sum/2。 -看代码的话,就可以发现,基本就是按照01背包的写法来的。 - +做完本题后,需要大家清晰:背包问题,不仅可以求 背包能被的最大价值,还可以求这个背包是否可以装满。 ## 其他语言版本 diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index c0a490b343..eb2d51cff9 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -578,7 +578,7 @@ int main() { 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 * 源点到节点2的最短距离为100,小于原minDist[2]的数值max,更新minDist[2] = 100 -* 源点到节点3的最短距离为1,小于原minDist[3]的数值max,更新minDist[4] = 1 +* 源点到节点3的最短距离为1,小于原minDist[3]的数值max,更新minDist[3] = 1 ------------------- diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index dacd23d11d..60c997a5d8 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -215,9 +215,9 @@ int main() { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111849.png) -边:节点3 -> 节点4,权值为1 ,minDist[4] > minDist[3] + 1,更新 minDist[4] = 0 + (-1) = -1 ,如图: +边:节点3 -> 节点4,权值为1 ,minDist[4] > minDist[3] + 1,更新 minDist[4] = 0 + 1 = 1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111837.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241018192042.png) 以上是对所有边进行的第一次松弛,最后 minDist数组为 :-1 -1 0 1 ,(从下标1算起) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 2d0481ecec..8d53276c20 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -173,7 +173,7 @@ int n=q.front();q.pop(); G:起点达到目前遍历节点的距离 -F:目前遍历的节点到达终点的距离 +H:目前遍历的节点到达终点的距离 起点达到目前遍历节点的距离 + 目前遍历的节点到达终点的距离 就是起点到达终点的距离。 From 4ddbb265e47e79919ad501c087919f99a4615612 Mon Sep 17 00:00:00 2001 From: swjtuhjf Date: Tue, 12 Nov 2024 10:38:41 +0800 Subject: [PATCH 1416/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200095.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93II=20pyth?= =?UTF-8?q?on3=20SPFA=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\211\251\350\277\220\350\276\223II.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index ac6ccf3c0d..a88e462ad6 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -333,6 +333,8 @@ public class Main { ### Python +Bellman-Ford方法求解含有负回路的最短路问题 + ```python import sys @@ -388,6 +390,52 @@ if __name__ == "__main__": ``` +SPFA方法求解含有负回路的最短路问题 + +```python +from collections import deque +from math import inf + +def main(): + n, m = [int(i) for i in input().split()] + graph = [[] for _ in range(n+1)] + min_dist = [inf for _ in range(n+1)] + count = [0 for _ in range(n+1)] # 记录节点加入队列的次数 + for _ in range(m): + s, t, v = [int(i) for i in input().split()] + graph[s].append([t, v]) + + min_dist[1] = 0 # 初始化 + count[1] = 1 + d = deque([1]) + flag = False + + while d: # 主循环 + cur_node = d.popleft() + for next_node, val in graph[cur_node]: + if min_dist[next_node] > min_dist[cur_node] + val: + min_dist[next_node] = min_dist[cur_node] + val + count[next_node] += 1 + if next_node not in d: + d.append(next_node) + if count[next_node] == n: # 如果某个点松弛了n次,说明有负回路 + flag = True + if flag: + break + + if flag: + print("circle") + else: + if min_dist[-1] == inf: + print("unconnected") + else: + print(min_dist[-1]) + + +if __name__ == "__main__": + main() +``` + ### Go ### Rust From 947424d311d767dec59f5820b9a83f51da5c2bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E7=AC=9B?= <128707187+catherinexrk@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:52:30 +0800 Subject: [PATCH 1417/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200103.=E6=B0=B4?= =?UTF-8?q?=E6=B5=81=E9=97=AE=E9=A2=98.=20Go=E8=AF=AD=E8=A8=80=20DFS?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 9a34bf094f..65d36e480a 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -413,6 +413,81 @@ if __name__ == "__main__": ``` ### Go +```go +package main + +import ( + "os" + "fmt" + "strings" + "strconv" + "bufio" +) + +var directions = [][]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}} // 四个方向的偏移量 + +func main() { + scanner := bufio.NewScanner(os.Stdin) + + scanner.Scan() + lineList := strings.Fields(scanner.Text()) + N, _ := strconv.Atoi(lineList[0]) + M, _ := strconv.Atoi(lineList[1]) + + grid := make([][]int, N) + visited := make([][]bool, N) // 用于标记是否访问过 + for i := 0; i < N; i++ { + grid[i] = make([]int, M) + visited[i] = make([]bool, M) + scanner.Scan() + lineList = strings.Fields(scanner.Text()) + + for j := 0; j < M; j++ { + grid[i][j], _ = strconv.Atoi(lineList[j]) + } + } + + // 遍历每个单元格,使用DFS检查是否可达两组边界 + for i := 0; i < N; i++ { + for j := 0; j < M; j++ { + canReachFirst, canReachSecond := dfs(grid, visited, i, j) + if canReachFirst && canReachSecond { + fmt.Println(strconv.Itoa(i) + " " + strconv.Itoa(j)) + } + } + } +} + +func dfs(grid [][]int, visited [][]bool, startx int, starty int) (bool, bool) { + visited[startx][starty] = true + canReachFirst := startx == 0 || starty == 0 || startx == len(grid)-1 || starty == len(grid[0])-1 + canReachSecond := startx == len(grid)-1 || starty == len(grid[0])-1 || startx == 0 || starty == 0 + + if canReachFirst && canReachSecond { + return true, true + } + + for _, direction := range directions { + nextx := startx + direction[0] + nexty := starty + direction[1] + + if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) { + continue + } + + if grid[nextx][nexty] <= grid[startx][starty] && !visited[nextx][nexty] { + hasReachFirst, hasReachSecond := dfs(grid, visited, nextx, nexty) + if !canReachFirst { + canReachFirst = hasReachFirst + } + if !canReachSecond { + canReachSecond = hasReachSecond + } + } + } + return canReachFirst, canReachSecond +} +``` ### Rust From 6c497c692b53da154458e82d719cc9c25268e132 Mon Sep 17 00:00:00 2001 From: swjtuhjf Date: Tue, 12 Nov 2024 14:04:38 +0800 Subject: [PATCH 1418/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200096.=E5=9F=8E?= =?UTF-8?q?=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93III=20pyt?= =?UTF-8?q?hon3=20SPFA=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...347\211\251\350\277\220\350\276\223III.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index a41332cf5c..109027b102 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -703,6 +703,9 @@ public class Main { ``` ### Python + +Bellman-Ford方法求解单源有限最短路 + ```python def main(): # 輸入 @@ -736,6 +739,48 @@ def main(): +if __name__ == "__main__": + main() +``` + +SPFA方法求解单源有限最短路 + +```python +from collections import deque +from math import inf + + +def main(): + n, m = [int(i) for i in input().split()] + graph = [[] for _ in range(n+1)] + for _ in range(m): + v1, v2, val = [int(i) for i in input().split()] + graph[v1].append([v2, val]) + src, dst, k = [int(i) for i in input().split()] + min_dist = [inf for _ in range(n+1)] + min_dist[src] = 0 # 初始化起点的距离 + que = deque([src]) + + while k != -1 and que: + visited = [False for _ in range(n+1)] # 用于保证每次松弛时一个节点最多加入队列一次 + que_size = len(que) + temp_dist = min_dist.copy() # 用于记录上一次遍历的结果 + for _ in range(que_size): + cur_node = que.popleft() + for next_node, val in graph[cur_node]: + if min_dist[next_node] > temp_dist[cur_node] + val: + min_dist[next_node] = temp_dist[cur_node] + val + if not visited[next_node]: + que.append(next_node) + visited[next_node] = True + k -= 1 + + if min_dist[dst] == inf: + print("unreachable") + else: + print(min_dist[dst]) + + if __name__ == "__main__": main() ``` From 50472c381cd423ac12ff4c6eb8bb6126a4f5a2d1 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 12 Nov 2024 14:31:02 +0800 Subject: [PATCH 1419/1533] =?UTF-8?q?143.=E9=87=8D=E6=8E=92=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=A2=9E=E5=8A=A0Go=E6=96=B9=E6=B3=95=E4=B8=80?= =?UTF-8?q?=E5=92=8C=E6=96=B9=E6=B3=95=E4=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\346\216\222\351\223\276\350\241\250.md" | 83 ++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" index 8707543190..ccddef5bf0 100644 --- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" +++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" @@ -38,7 +38,7 @@ public: cur = head; int i = 1; int j = vec.size() - 1; // i j为之前前后的双指针 - int count = 0; // 计数,偶数去后面,奇数取前面 + int count = 0; // 计数,偶数取后面,奇数取前面 while (i <= j) { if (count % 2 == 0) { cur->next = vec[j]; @@ -73,7 +73,7 @@ public: } cur = head; - int count = 0; // 计数,偶数去后面,奇数取前面 + int count = 0; // 计数,偶数取后面,奇数取前面 ListNode* node; while(que.size()) { if (count % 2 == 0) { @@ -338,8 +338,85 @@ class Solution: return pre ``` ### Go + +```go +// 方法一 数组模拟 +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func reorderList(head *ListNode) { + vec := make([]*ListNode, 0) + cur := head + if cur == nil { + return + } + for cur != nil { + vec = append(vec, cur) + cur = cur.Next + } + cur = head + i := 1 + j := len(vec) - 1 // i j为前后的双指针 + count := 0 // 计数,偶数取后面,奇数取前面 + for i <= j { + if count % 2 == 0 { + cur.Next = vec[j] + j-- + } else { + cur.Next = vec[i] + i++ + } + cur = cur.Next + count++ + } + cur.Next = nil // 注意结尾 +} +``` + +```go +// 方法二 双向队列模拟 +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func reorderList(head *ListNode) { + que := make([]*ListNode, 0) + cur := head + if cur == nil { + return + } + + for cur.Next != nil { + que = append(que, cur.Next) + cur = cur.Next + } + + cur = head + count := 0 // 计数,偶数取后面,奇数取前面 + for len(que) > 0 { + if count % 2 == 0 { + cur.Next = que[len(que)-1] + que = que[:len(que)-1] + } else { + cur.Next = que[0] + que = que[1:] + } + count++ + cur = cur.Next + } + cur.Next = nil // 注意结尾 +} +``` + ```go -# 方法三 分割链表 +// 方法三 分割链表 func reorderList(head *ListNode) { var slow=head var fast=head From 0f152be27b34a82fa3882ffb5c86c55572550de1 Mon Sep 17 00:00:00 2001 From: lm10 Date: Tue, 12 Nov 2024 17:05:27 +0800 Subject: [PATCH 1420/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0:0105.=E6=9C=89?= =?UTF-8?q?=E5=90=91=E5=9B=BE=E7=9A=84=E5=AE=8C=E5=85=A8=E5=8F=AF=E8=BE=BE?= =?UTF-8?q?=E6=80=A7Javascript=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\217\257\350\276\276\346\200\247.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 838b021245..923fb6e4e2 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -491,6 +491,54 @@ func main() { ### Javascript +```javascript +const rl = require('readline').createInterface({ + input:process.stdin, + output:process.stdout +}) + +let inputLines = [] + +rl.on('line' , (line)=>{ + inputLines.push(line) +}) + +rl.on('close',()=>{ + let [n , edgesCount]= inputLines[0].trim().split(' ').map(Number) + + let graph = Array.from({length:n+1} , ()=>{return[]}) + + for(let i = 1 ; i < inputLines.length ; i++ ){ + let [from , to] = inputLines[i].trim().split(' ').map(Number) + graph[from].push(to) + } + + let visited = new Array(n + 1).fill(false) + + let dfs = (graph , key , visited)=>{ + if(visited[key]){ + return + } + + visited[key] = true + for(let nextKey of graph[key]){ + dfs(graph,nextKey , visited) + } + } + + dfs(graph , 1 , visited) + + for(let i = 1 ; i <= n;i++){ + if(visited[i] === false){ + console.log(-1) + return + } + } + console.log(1) + +}) +``` + ### TypeScript ### PhP From a0690ff730c6569aa0d39cb3b001e4068a6fc89a Mon Sep 17 00:00:00 2001 From: namewyf Date: Tue, 12 Nov 2024 23:03:01 +0800 Subject: [PATCH 1421/1533] =?UTF-8?q?=E5=BC=80=E5=8F=91=E5=95=86=E8=B4=AD?= =?UTF-8?q?=E4=B9=B0=E5=9C=9F=E5=9C=B0=E8=A7=A3=E6=B3=95=20JavaScript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\344\271\260\345\234\237\345\234\260.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" index ea2c696ef6..739e2cadcb 100644 --- "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -388,6 +388,62 @@ if __name__ == "__main__": main() ``` + +### JavaScript + +前缀和 +```js +function func() { + const readline = require('readline') + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }) + let inputLines = [] + rl.on('line', function (line) { + inputLines.push(line.trim()) + }) + + rl.on('close', function () { + let [n, m] = inputLines[0].split(" ").map(Number) + let c = new Array(n).fill(0) + let r = new Array(m).fill(0) + let arr = new Array(n) + let sum = 0//数组总和 + let min = Infinity//设置最小值的初始值为无限大 + //定义数组 + for (let s = 0; s < n; s++) { + arr[s] = inputLines[s + 1].split(" ").map(Number) + } + //每一行的和 + for (let i = 0; i < n; i++) { + for (let j = 0; j < m; j++) { + c[i] += arr[i][j] + sum += arr[i][j] + } + } + //每一列的和 + for (let i = 0; i < n; i++) { + for (let j = 0; j < m; j++) { + r[j] += arr[i][j] + } + } + let sum1 = 0, sum2 = 0 + //横向切割 + for (let i = 0; i < n; i++) { + sum1 += c[i] + min = min < Math.abs(sum - 2 * sum1) ? min : Math.abs(sum - 2 * sum1) + } + //纵向切割 + for (let j = 0; j < m; j++) { + sum2 += r[j] + min = min < Math.abs(sum - 2 * sum2) ? min : Math.abs(sum - 2 * sum2) + } + console.log(min); + }) +} +``` + ### C 前缀和 From 2d266b576653720ff16b0a3dfa635b72f5ffa57d Mon Sep 17 00:00:00 2001 From: liao junwu Date: Thu, 14 Nov 2024 23:24:17 +0800 Subject: [PATCH 1422/1533] [0739 temperature] add C version add C version for 0739: temperature Signed-off-by: liao junwu --- ...17\346\227\245\346\270\251\345\272\246.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index 45af52868f..dd633aed9a 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -215,6 +215,38 @@ public: ## 其他语言版本 +### C: + +```C +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize) { + int len = temperaturesSize; + *returnSize = len; + + int *result = (int *)malloc(sizeof(int) * len); + memset(result, 0x00, sizeof(int) * len); + + int stack[len]; + memset(stack, 0x00, sizeof(stack)); + int top = 0; + + for (int i = 1; i < len; i++) { + if (temperatures[i] <= temperatures[stack[top]]) { /* push */ + stack[++top] = i; + } else { + while (top >= 0 && temperatures[i] > temperatures[stack[top]]) { /* stack not empty */ + result[stack[top]] = i - stack[top]; + top--; /* pop */ + } + stack[++top] = i; /* push */ + } + } + return result; +} +``` + ### Java: ```java From da88f5e4c1407d99b46f087a6d5d10f6e685844e Mon Sep 17 00:00:00 2001 From: liao junwu Date: Sat, 16 Nov 2024 15:42:44 +0800 Subject: [PATCH 1423/1533] [0496 the next bigger element] add C version add C version for 0496: the next bigger element Signed-off-by: liao junwu --- ...4\345\244\247\345\205\203\347\264\240I.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 54182d3018..02e73a588d 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -195,6 +195,62 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 + +### C + +``` C +/* 先用单调栈的方法计算出结果,再根据nums1中的元素去查找对应的结果 */ +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) { + + /* stcak */ + int top = -1; + int stack_len = nums2Size; + int stack[stack_len]; + //memset(stack, 0x00, sizeof(stack)); + + /* nums2 result */ + int* result_nums2 = (int *)malloc(sizeof(int) * nums2Size); + //memset(result_nums2, 0x00, sizeof(int) * nums2Size); + + /* result */ + int* result = (int *)malloc(sizeof(int) * nums1Size); + //memset(result, 0x00, sizeof(int) * nums1Size); + *returnSize = nums1Size; + + /* init */ + stack[++top] = 0; /* stack loaded with array subscripts */ + + for (int i = 0; i < nums2Size; i++) { + result_nums2[i] = -1; + } + + /* get the result_nums2 */ + for (int i = 1; i < nums2Size; i++) { + if (nums2[i] <= nums2[stack[top]]) { + stack[++top] = i; /* push */ + } else { + while ((top >= 0) && (nums2[i] > nums2[stack[top]])) { + result_nums2[stack[top]] = nums2[i]; + top--; /* pop */ + } + stack[++top] = i; + } + } + + /* get the result */ + for (int i = 0; i < nums1Size; i++) { + for (int j = 0; j < nums2Size; j++) { + if (nums1[i] == nums2[j]) { + result[i] = result_nums2[j]; + } + } + } + return result; +} +``` ### Java ```java From 6901cb345efc05009c029ee121592e3f66ec182a Mon Sep 17 00:00:00 2001 From: lllyt8 Date: Sun, 17 Nov 2024 23:30:11 -0800 Subject: [PATCH 1424/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E5=8D=95=E8=AF=8D=E5=86=97=E4=BD=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 89e4ad1166..c1f29a2552 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -102,7 +102,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 4. 确定遍历顺序 -遍历顺序可有有点讲究了。 +遍历顺序可就有点讲究了。 首先从递推公式中可以看出,情况三是根据dp[i + 1][j - 1]是否为true,在对dp[i][j]进行赋值true的。 From 47605060055da15ad606684f92ebde27b5bd213d Mon Sep 17 00:00:00 2001 From: xi-mad <1171866049@qq.com> Date: Mon, 18 Nov 2024 16:50:05 +0800 Subject: [PATCH 1425/1533] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正错误 --- ...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 2747f179d1..a956013a88 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -87,7 +87,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, i 来表示物品、j表示背包容量。 -(如果想用j 表示物品,j表示背包容量 行不行? 都可以的,个人习惯而已) +(如果想用j 表示物品,i 表示背包容量 行不行? 都可以的,个人习惯而已) 我们来尝试把上面的 二维表格填写一下。 From 5e0ab494756cb48b11450f997f56c2677f51dcc4 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Fri, 22 Nov 2024 10:57:51 +0800 Subject: [PATCH 1426/1533] =?UTF-8?q?0123,=200188,=200309=20=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=B1=BB=E9=97=AE=E9=A2=98=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=98=93=E7=90=86=E8=A7=A3=E7=9A=84=E4=B8=80=E7=BB=B4=20dp=20P?= =?UTF-8?q?ython=20=E5=92=8C=20Go=20=E7=89=88=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...344\275\263\346\227\266\346\234\272III.md" | 29 +++++++++- ...\344\275\263\346\227\266\346\234\272IV.md" | 56 +++++++++++++++++-- ...53\345\206\267\345\206\273\346\234\237.md" | 35 +++++++++++- 3 files changed, 110 insertions(+), 10 deletions(-) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 8e224a898d..1b7c09d214 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -316,8 +316,9 @@ class Solution: ### Go: +> 版本一 + ```go -// 版本一 func maxProfit(prices []int) int { dp := make([][]int, len(prices)) for i := 0; i < len(prices); i++ { @@ -345,8 +346,9 @@ func max(a, b int) int { } ``` +> 版本二 + ```go -// 版本二 func maxProfit(prices []int) int { if len(prices) == 0 { return 0 @@ -371,8 +373,9 @@ func max(x, y int) int { } ``` +> 版本三 + ```go -// 版本三 func maxProfit(prices []int) int { if len(prices) == 0 { return 0 @@ -397,6 +400,26 @@ func max(x, y int) int { } ``` +> 版本四:一维 dp 易懂版本 + +```go +func maxProfit(prices []int) int { + dp := make([]int, 4) + dp[0] = -prices[0] + dp[2] = -prices[0] + + for _, price := range prices[1:] { + dc := slices.Clone(dp) // 这句话是关键,把前一天的 dp 状态保存下来,防止被覆盖掉,后面只用它,不用 dp,逻辑简单易懂 + dp[0] = max(dc[0], -price) + dp[1] = max(dc[1], dc[0] + price) + dp[2] = max(dc[2], dc[1] - price) + dp[3] = max(dc[3], dc[2] + price) + } + + return dp[3] +} +``` + ### JavaScript: > 版本一: diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 0b1622ac5a..cbba12c9d1 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -297,8 +297,7 @@ class Solution { ### Python: -版本一 - +> 版本一 ```python class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: @@ -313,7 +312,8 @@ class Solution: dp[i][j+2] = max(dp[i-1][j+2], dp[i-1][j+1] + prices[i]) return dp[-1][2*k] ``` -版本二 + +> 版本二 ```python class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: @@ -329,9 +329,31 @@ class Solution: dp[j] = max(dp[j],dp[j-1]+prices[i]) return dp[2*k] ``` + +> 版本三: 一维 dp 数组(易理解版本) +```python +class Solution: + def maxProfit(self, k: int, prices: List[int]) -> int: + dp = [0] * k * 2 + for i in range(k): + dp[i * 2] = -prices[0] + + for price in prices[1:]: + dc = dp.copy() # 这句话是关键,把前一天的 dp 状态保存下来,防止被覆盖掉,后面只用它,不用 dp,逻辑简单易懂 + + for i in range(2 * k): + if i % 2 == 1: + dp[i] = max(dc[i], dc[i - 1] + price) + else: + pre = 0 if i == 0 else dc[i - 1] + dp[i] = max(dc[i], pre - price) + + return dp[-1] +``` + ### Go: -版本一: +> 版本一: ```go // 买卖股票的最佳时机IV 动态规划 @@ -368,7 +390,7 @@ func max(a, b int) int { } ``` -版本二: 三维 dp数组 +> 版本二: 三维 dp数组 ```go func maxProfit(k int, prices []int) int { length := len(prices) @@ -443,7 +465,31 @@ func max(a, b int) int { } ``` +> 版本四:一维 dp 数组(易理解版本) +```go +func maxProfit(k int, prices []int) int { + dp := make([]int, 2 * k) + for i := range k { + dp[i * 2] = -prices[0] + } + + for j := 1; j < len(prices); j++ { + dc := slices.Clone(dp) // 这句话是关键,把前一天的 dp 状态保存下来,防止被覆盖掉,后面只用它,不用 dp,逻辑简单易懂 + + for i := range k * 2 { + if i % 2 == 1 { + dp[i] = max(dc[i], dc[i - 1] + prices[j]) + } else { + pre := 0; if i >= 1 { pre = dc[i - 1] } + dp[i] = max(dc[i], pre - prices[j]) + } + } + } + + return dp[2 * k - 1] +} +``` ### JavaScript: diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 4913b8bd57..707b153235 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -274,7 +274,7 @@ class Solution { ``` ### Python: -版本一 +> 版本一 ```python from typing import List @@ -294,7 +294,8 @@ class Solution: return max(dp[n-1][3], dp[n-1][1], dp[n-1][2]) # 返回最后一天不持有股票的最大利润 ``` -版本二 + +> 版本二 ```python class Solution: def maxProfit(self, prices: List[int]) -> int: @@ -320,6 +321,36 @@ class Solution: return max(dp[-1][1], dp[-1][2]) ``` + +> 版本三 +```python +class Solution: + def maxProfit(self, prices: List[int]) -> int: + # 0: holding stocks + # (1) keep holding stocks: dp[i][0] = dp[i - 1][0] + # (2) buy stocks: dp[i][0] = dp[i - 1][1] - price, or dp[i - 1][3] - price + # 1: keep no stocks: dp[i][1] = dp[i - 1][1] + # 2: sell stocks: dp[i][2] = dp[i - 1][0] + price + # 3: cooldown day: dp[i][3] = dp[i - 1][2] + dp = [-prices[0], 0, 0, 0] + + for price in prices[1:]: + dc = dp.copy() # 这句话是关键,把前一天的 dp 状态保存下来,防止被覆盖掉,后面只用它,不用 dp,逻辑简单易懂 + dp[0] = max( + dc[0], + dc[1] - price, + dc[3] - price + ) + dp[1] = max( + dc[1], + dc[3] + ) + dp[2] = dc[0] + price + dp[3] = dc[2] + + return max(dp) +``` + ### Go: ```go From e40ddd5cbf46ae62a0b1aee4b683fb899b6fd6c8 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Fri, 22 Nov 2024 10:59:16 +0800 Subject: [PATCH 1427/1533] =?UTF-8?q?0337.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8DIII.md=20=E4=BD=BF=E7=94=A8=20`slices.Max`=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=E6=89=8B=E5=B7=A5max=20func=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...211\223\345\256\266\345\212\253\350\210\215III.md" | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 12e31ababd..a3130df7ee 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -477,14 +477,7 @@ func max(x, y int) int { ```go func rob(root *TreeNode) int { res := robTree(root) - return max(res[0], res[1]) -} - -func max(a, b int) int { - if a > b { - return a - } - return b + return slices.Max(res) } func robTree(cur *TreeNode) []int { @@ -498,7 +491,7 @@ func robTree(cur *TreeNode) []int { // 考虑去偷当前的屋子 robCur := cur.Val + left[0] + right[0] // 考虑不去偷当前的屋子 - notRobCur := max(left[0], left[1]) + max(right[0], right[1]) + notRobCur := slices.Max(left) + slices.Max(right) // 注意顺序:0:不偷,1:去偷 return []int{notRobCur, robCur} From 332302b998306ca16e253deeff637075ee49e555 Mon Sep 17 00:00:00 2001 From: Lane Zhang Date: Fri, 22 Nov 2024 10:59:39 +0800 Subject: [PATCH 1428/1533] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E9=94=99=E5=AD=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" index c9420d24eb..9ffb453377 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -106,7 +106,7 @@ **如果这灵魂三问自己都做到了,基本上这道题目也就解决了**,或者更清晰的知道自己究竟是哪一点不明白,是状态转移不明白,还是实现代码不知道该怎么写,还是不理解遍历dp数组的顺序。 -然后在问问题,目的性就很强了,群里的小伙伴也可以快速知道提问者的疑惑了。 +然后再问问题,目的性就很强了,群里的小伙伴也可以快速知道提问者的疑惑了。 **注意这里不是说不让大家问问题哈, 而是说问问题之前要有自己的思考,问题要问到点子上!** From 7d1cb13e748e6e265438ec7b772835eee412cb42 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Sun, 24 Nov 2024 21:30:18 +0800 Subject: [PATCH 1429/1533] =?UTF-8?q?Update=200108.=E5=86=97=E4=BD=99?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 18a86ad6d7..6c393b4f1a 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -44,7 +44,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527110320.png) -图中的 1 2,2 3,1 3 等三条边在删除后都能使原图变为一棵合法的树。但是 1 3 由于是标准输出里最后出现的那条边,所以输出结果为 1 3 +图中的 1 2,2 3,1 3 等三条边在删除后都能使原图变为一棵合法的树。但是 1 3 由于是标准输入里最后出现的那条边,所以输出结果为 1 3 数据范围: From 72df413ffcf9dbecf7a0101e82360317645a4881 Mon Sep 17 00:00:00 2001 From: ethaiyi9 Date: Tue, 26 Nov 2024 00:20:14 +0800 Subject: [PATCH 1430/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20=E9=94=99=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index cd7096ac17..708e0532f8 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -40,7 +40,7 @@ 本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。 * 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始) -* 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始) +* 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始) 那么使用后序遍历,其实求的是根节点到叶子节点的最小距离,就是求高度的过程,不过这个最小距离 也同样是最小深度。 From 455520bde6ba29d01eb9b43d3aba88b8f7d2dead Mon Sep 17 00:00:00 2001 From: Po1vre Date: Thu, 28 Nov 2024 15:54:24 +0800 Subject: [PATCH 1431/1533] =?UTF-8?q?fix:=2070=20=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=E5=88=A0=E5=8E=BB=E5=A4=8D=E6=9D=82=E5=BA=A6=E7=9A=84?= =?UTF-8?q?=E5=86=85=E8=81=94=E5=85=AC=E5=BC=8F=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 仅有单对的‘$’,与其他文档同步,故删去 --- "problems/0070.\347\210\254\346\245\274\346\242\257.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index a2f664a495..6a13a21cdb 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -130,8 +130,8 @@ public: }; ``` -* 时间复杂度:$O(n)$ -* 空间复杂度:$O(n)$ +* 时间复杂度:O(n) +* 空间复杂度:O(n) 当然依然也可以,优化一下空间复杂度,代码如下: @@ -154,8 +154,8 @@ public: }; ``` -* 时间复杂度:$O(n)$ -* 空间复杂度:$O(1)$ +* 时间复杂度:O(n) +* 空间复杂度:O(1) 后面将讲解的很多动规的题目其实都是当前状态依赖前两个,或者前三个状态,都可以做空间上的优化,**但我个人认为面试中能写出版本一就够了哈,清晰明了,如果面试官要求进一步优化空间的话,我们再去优化**。 @@ -524,3 +524,4 @@ impl Solution { + From 94bdecfc2eeab67d6657fb6c688478638a5b86fc Mon Sep 17 00:00:00 2001 From: dam <1782067308@qq.com> Date: Fri, 29 Nov 2024 15:56:34 +0800 Subject: [PATCH 1432/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0474.=20=E4=B8=80?= =?UTF-8?q?=E5=92=8C=E9=9B=B6=20java=20=E4=B8=89=E7=BB=B4DP=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=AE=9E=E7=8E=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\344\270\200\345\222\214\351\233\266.md" | 65 ++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index e514e7292b..9d24f01434 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -261,8 +261,70 @@ public: ## 其他语言版本 - ### Java + +三维DP数组实现 + +```java +class Solution { + public int findMaxForm(String[] strs, int m, int n) { + /// 数组有三个维度 + // 第一个维度:取前面的几个字符串 + // 第二个维度:0的数量限制(背包维度 1 容量) + // 第三个维度:1的数量限制(背包维度 2 容量) + int[][][] dpArr = new int[strs.length][m + 1][n + 1]; + + /// 初始化dpArr数组 + // 计算第一个字符串的零数量和1数量 + int zeroNum = 0; + int oneNum = 0; + for (char c : strs[0].toCharArray()) { + if (c == '0') { + zeroNum++; + } else { + oneNum++; + } + } + // 当0数量、1数量都容得下第一个字符串时,将DP数组的相应位置初始化为1,因为当前的子集数量为1 + for (int j = zeroNum; j <= m; j++) { + for (int k = oneNum; k <= n; k++) { + dpArr[0][j][k] = 1; + } + } + /// 依次填充加入第i个字符串之后的DP数组 + for (int i = 1; i < strs.length; i++) { + zeroNum = 0; + oneNum = 0; + for (char c : strs[i].toCharArray()) { + if (c == '0') { + zeroNum++; + } else { + oneNum++; + } + } + for (int j = 0; j <= m; j++) { + for (int k = 0; k <= n; k++) { + if (j >= zeroNum && k >= oneNum) { + // --if-- 当0数量维度和1数量维度的容量都大于等于当前字符串的0数量和1数量时,才考虑是否将当前字符串放入背包 + // 不放入第i个字符串,子集数量仍为 dpArr[i - 1][j][k] + // 放入第i个字符串,需要在0维度腾出 zeroNum 个容量,1维度腾出 oneNum 个容量,然后放入当前字符串,即 dpArr[i - 1][j - zeroNum][k - oneNum] + 1) + dpArr[i][j][k] = Math.max(dpArr[i - 1][j][k], dpArr[i - 1][j - zeroNum][k - oneNum] + 1); + } else { + // --if-- 无法放入第i个字符串,子集数量仍为 dpArr[i - 1][j][k] + dpArr[i][j][k] = dpArr[i - 1][j][k]; + } + } + } + } + return dpArr[dpArr.length - 1][m][n]; + } +} +``` + + + +二维DP数组实现 + ```Java class Solution { public int findMaxForm(String[] strs, int m, int n) { @@ -682,3 +744,4 @@ public class Solution + From 4d382794537f44bba8028b8e005df25fb29a43b6 Mon Sep 17 00:00:00 2001 From: ASGPIPO Date: Mon, 2 Dec 2024 16:48:06 +0800 Subject: [PATCH 1433/1533] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0README?= =?UTF-8?q?=E4=B8=AD=20=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=8E=E5=8F=8C?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=9D=BF=E5=9D=97=E9=A2=98=E7=9B=AE=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E9=94=99=E8=AF=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3e3b67861e..b326046ede 100644 --- a/README.md +++ b/README.md @@ -141,9 +141,9 @@ 1. [字符串:344.反转字符串](./problems/0344.反转字符串.md) 2. [字符串:541.反转字符串II](./problems/0541.反转字符串II.md) -3. [字符串:替换数字](./problems/kama54.替换数字.md) +3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md) 4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) -5. [字符串:右旋字符串](./problems/kama55.右旋字符串.md) +5. [字符串:右旋字符串](./problems/kamacoder/0055.右旋字符串.md) 6. [帮你把KMP算法学个通透](./problems/0028.实现strStr.md) 8. [字符串:459.重复的子字符串](./problems/0459.重复的子字符串.md) 9. [字符串:总结篇!](./problems/字符串总结.md) @@ -154,7 +154,7 @@ 1. [数组:27.移除元素](./problems/0027.移除元素.md) 2. [字符串:344.反转字符串](./problems/0344.反转字符串.md) -3. [字符串:替换数字](./problems/kama54.替换数字.md) +3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md) 4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) 5. [链表:206.翻转链表](./problems/0206.翻转链表.md) 6. [链表:19.删除链表的倒数第 N 个结点](./problems/0019.删除链表的倒数第N个节点.md) From 5b94b448646e15507c07aa5c95337b2f1bd0f620 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Wed, 4 Dec 2024 12:29:18 -0600 Subject: [PATCH 1434/1533] =?UTF-8?q?feat:=20Updated=E9=A2=98=E7=9B=AE1365?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E4=BE=9B=E4=BA=86=E4=BD=BF=E7=94=A8=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E8=BF=9B=E8=A1=8C=E5=93=88=E5=B8=8C=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\347\232\204\346\225\260\345\255\227.md" | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 64f610968b..22dd322632 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -138,7 +138,9 @@ public int[] smallerNumbersThanCurrent(int[] nums) { ### Python: -```python +> (版本一)使用字典 + +```python3 class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: res = nums[:] @@ -152,6 +154,23 @@ class Solution: return res ``` +> (版本二)使用数组 + +```python3 +class Solution: + def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: + # 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销 + sort_nums = sorted(nums) + # 题意中 0 <= nums[i] <= 100,故range的参数设为101 + hash_lst = [0 for _ in range(101)] + # 从后向前遍历,这样hash里存放的就是相同元素最左面的数值和下标了 + for i in range(len(sort_nums)-1,-1,-1): + hash_lst[sort_nums[i]] = i + for i in range(len(nums)): + nums[i] = hash_lst[nums[i]] + return nums +``` + ### Go: ```go From 14223a2fa6c4a93d9e782a6b4e32e73682c5e051 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Wed, 4 Dec 2024 12:33:40 -0600 Subject: [PATCH 1435/1533] =?UTF-8?q?fix:=20Update=201365=EF=BC=8C?= =?UTF-8?q?=E5=BB=BA=E8=AE=AE=E4=B8=8D=E4=BD=BF=E7=94=A8built-in=20func?= =?UTF-8?q?=E4=BD=9C=E4=B8=BAvar=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题解中使用`hash`作为变量名,而hash本身也是python3的built-in函数,故建议更改变量名 --- ...60\345\255\227\347\232\204\346\225\260\345\255\227.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 22dd322632..95a270ffda 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -144,13 +144,13 @@ public int[] smallerNumbersThanCurrent(int[] nums) { class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: res = nums[:] - hash = dict() + hash_dict = dict() res.sort() # 从小到大排序之后,元素下标就是小于当前数字的数字 for i, num in enumerate(res): - if num not in hash.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况 - hash[num] = i + if num not in hash_dict.keys(): # 遇到了相同的数字,那么不需要更新该 number 的情况 + hash_dict[num] = i for i, num in enumerate(nums): - res[i] = hash[num] + res[i] = hash_dict[num] return res ``` From e1034946d5bc1f5a74341fa82c81413117bbbbc8 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Wed, 4 Dec 2024 12:35:06 -0600 Subject: [PATCH 1436/1533] =?UTF-8?q?typo:=20Update=201365=EF=BC=8CRust?= =?UTF-8?q?=E5=BB=BA=E8=AE=AE=E9=A6=96=E5=AD=97=E6=AF=8D=E5=A4=A7=E5=86=99?= =?UTF-8?q?=EF=BC=8C=E4=B8=8E=E5=85=B6=E4=BB=96=E8=AF=AD=E8=A8=80=E7=9B=B8?= =?UTF-8?q?=E5=90=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 95a270ffda..d7de450b90 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -279,7 +279,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] { }; ``` -### rust +### Rust ```rust use std::collections::HashMap; impl Solution { From 66caa02935bf372ac1df3e812e18aa7059fc1561 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Wed, 4 Dec 2024 12:43:06 -0600 Subject: [PATCH 1437/1533] =?UTF-8?q?feat:=20Update=201365=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0python=E7=9A=84=E6=9A=B4=E5=8A=9B=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E5=B0=86=E5=93=88=E5=B8=8C=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E4=B8=A4=E7=A7=8D=E6=96=B9=E6=B3=95=E5=90=88=E5=B9=B6=E5=86=99?= =?UTF-8?q?=E5=9C=A8=E4=B8=80=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\347\232\204\346\225\260\345\255\227.md" | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index d7de450b90..b34ba3da97 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -138,10 +138,28 @@ public int[] smallerNumbersThanCurrent(int[] nums) { ### Python: -> (版本一)使用字典 +> 暴力法 ```python3 class Solution: + def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: + res = [0 for _ in range(len(nums))] + for i in range(len(nums)): + cnt = 0 + for j in range(len(nums)): + if j == i: + continue + if nums[i] > nums[j]: + cnt += 1 + res[i] = cnt + return res +``` + +> 排序+hash + +```python3 +class Solution: + # 方法一:使用字典 def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: res = nums[:] hash_dict = dict() @@ -152,12 +170,8 @@ class Solution: for i, num in enumerate(nums): res[i] = hash_dict[num] return res -``` -> (版本二)使用数组 - -```python3 -class Solution: + # 方法二:使用数组 def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: # 同步进行排序和创建新数组的操作,这样可以减少一次冗余的数组复制操作,以减少一次O(n) 的复制时间开销 sort_nums = sorted(nums) From 53397d452ecc3d5b4612eb605dc577609edccb7b Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Wed, 4 Dec 2024 12:45:00 -0600 Subject: [PATCH 1438/1533] =?UTF-8?q?docs:=20Update=201365=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=86=92=E5=8F=B7=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\260\345\255\227\347\232\204\346\225\260\345\255\227.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index b34ba3da97..722fcb1790 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -138,7 +138,7 @@ public int[] smallerNumbersThanCurrent(int[] nums) { ### Python: -> 暴力法 +> 暴力法: ```python3 class Solution: @@ -155,7 +155,7 @@ class Solution: return res ``` -> 排序+hash +> 排序+hash: ```python3 class Solution: @@ -274,7 +274,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] { }; ``` -> 排序+hash +> 排序+hash: ```typescript function smallerNumbersThanCurrent(nums: number[]): number[] { From 7ead61625d0f85890034bb43c0f4a255a8820b19 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Wed, 4 Dec 2024 12:47:57 -0600 Subject: [PATCH 1439/1533] =?UTF-8?q?docs:=20Update=201365=EF=BC=8C?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=B8=BA=E4=B8=AD=E6=96=87=E5=86=92=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\260\345\255\227\347\232\204\346\225\260\345\255\227.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 722fcb1790..f0a77f5587 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -115,7 +115,7 @@ public: ## 其他语言版本 -### Java: +### Java: ```Java public int[] smallerNumbersThanCurrent(int[] nums) { @@ -253,7 +253,7 @@ var smallerNumbersThanCurrent = function(nums) { }; ``` -### TypeScript: +### TypeScript: > 暴力法: @@ -293,7 +293,7 @@ function smallerNumbersThanCurrent(nums: number[]): number[] { }; ``` -### Rust +### Rust: ```rust use std::collections::HashMap; impl Solution { From 6776ecc172511158d6f516721afd8bfb91429014 Mon Sep 17 00:00:00 2001 From: Po1vre Date: Fri, 6 Dec 2024 14:37:45 +0800 Subject: [PATCH 1440/1533] =?UTF-8?q?docs:=200518=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2=E2=85=A1=20=E4=BF=AE=E6=94=B9=E6=A0=87?= =?UTF-8?q?=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 0d35fb7cdf..1921866e5e 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -397,7 +397,7 @@ object Solution { } } ``` -## C +### C ```c int change(int amount, int* coins, int coinsSize) { @@ -444,3 +444,4 @@ public class Solution + From 4a9cfb56048e10e688745605289cbe76de84a0bf Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 6 Dec 2024 17:43:53 +0800 Subject: [PATCH 1441/1533] =?UTF-8?q?=E7=8E=B0=E6=9C=89=E8=A7=A3=E6=B3=95p?= =?UTF-8?q?ython=E8=B6=85=E6=97=B6=EF=BC=8C=E9=80=9A=E8=BF=87=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E9=80=92=E5=BD=92=E6=AC=A1=E6=95=B0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\350\247\243\346\225\260\347\213\254.md" | 84 +++++++++++-------- 1 file changed, 50 insertions(+), 34 deletions(-) diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index 6a9f69bd07..5f3f881cf1 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -366,40 +366,56 @@ class Solution: """ Do not return anything, modify board in-place instead. """ - self.backtracking(board) - - def backtracking(self, board: List[List[str]]) -> bool: - # 若有解,返回True;若无解,返回False - for i in range(len(board)): # 遍历行 - for j in range(len(board[0])): # 遍历列 - # 若空格内已有数字,跳过 - if board[i][j] != '.': continue - for k in range(1, 10): - if self.is_valid(i, j, k, board): - board[i][j] = str(k) - if self.backtracking(board): return True - board[i][j] = '.' - # 若数字1-9都不能成功填入空格,返回False无解 - return False - return True # 有解 - - def is_valid(self, row: int, col: int, val: int, board: List[List[str]]) -> bool: - # 判断同一行是否冲突 - for i in range(9): - if board[row][i] == str(val): - return False - # 判断同一列是否冲突 - for j in range(9): - if board[j][col] == str(val): - return False - # 判断同一九宫格是否有冲突 - start_row = (row // 3) * 3 - start_col = (col // 3) * 3 - for i in range(start_row, start_row + 3): - for j in range(start_col, start_col + 3): - if board[i][j] == str(val): - return False - return True + row_used = [set() for _ in range(9)] + col_used = [set() for _ in range(9)] + box_used = [set() for _ in range(9)] + for row in range(9): + for col in range(9): + num = board[row][col] + if num == ".": + continue + row_used[row].add(num) + col_used[col].add(num) + box_used[(row // 3) * 3 + col // 3].add(num) + self.backtracking(0, 0, board, row_used, col_used, box_used) + + def backtracking( + self, + row: int, + col: int, + board: List[List[str]], + row_used: List[List[int]], + col_used: List[List[int]], + box_used: List[List[int]], + ) -> bool: + if row == 9: + return True + + next_row, next_col = (row, col + 1) if col < 8 else (row + 1, 0) + if board[row][col] != ".": + return self.backtracking( + next_row, next_col, board, row_used, col_used, box_used + ) + + for num in map(str, range(1, 10)): + if ( + num not in row_used[row] + and num not in col_used[col] + and num not in box_used[(row // 3) * 3 + col // 3] + ): + board[row][col] = num + row_used[row].add(num) + col_used[col].add(num) + box_used[(row // 3) * 3 + col // 3].add(num) + if self.backtracking( + next_row, next_col, board, row_used, col_used, box_used + ): + return True + board[row][col] = "." + row_used[row].remove(num) + col_used[col].remove(num) + box_used[(row // 3) * 3 + col // 3].remove(num) + return False ``` ### Go From 34d28a94245baaa9744d2a06d84c050f643aeb24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Sun, 8 Dec 2024 16:17:59 +0800 Subject: [PATCH 1442/1533] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\270\211\346\225\260\344\271\213\345\222\214.md" | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 0d47bf4815..349335d0a6 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -36,7 +36,7 @@ ### 哈希解法 -两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。 +两层for循环就可以确定 两个数值,可以使用哈希法来确定 第三个数 0-(a+b) 或者 0 - (a + c) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。 把符合条件的三元组放进vector中,然后再去重,这样是非常费时的,很容易超时,也是这道题目通过率如此之低的根源所在。 @@ -51,10 +51,8 @@ class Solution { public: // 在一个数组中找到3个数形成的三元组,它们的和为0,不能重复使用(三数下标互不相同),且三元组不能重复。 - // 理论解法:a+b+c(存储)==0(检索) <=> c(存储)==0-(a+b)(检索) - // 实际解法:a+b+c(存储)==0(检索) <=> b(存储)==0-(a+c)(检索) + // b(存储)== 0-(a+c)(检索) vector> threeSum(vector& nums) { - // 本解法的内层循环一边存储一边检索,所以被存储的应该是b,而不是c vector> result; sort(nums.begin(), nums.end()); @@ -71,11 +69,7 @@ public: unordered_set set; for (int k = i + 1; k < nums.size(); k++) { - // [(-2x), ..., (x), (x), x, x, x, ...] - // eg. [0, 0, 0] - // eg. [-4, 2, 2] - // eg. [(-4), -1, 0, 0, 1, (2), (2), {2}, {2}, 3, 3] - // 去重b=c时的b和c,即第三个x到最后一个x需要被跳过 + // 去重b=c时的b和c if (k > i + 2 && nums[k] == nums[k - 1] && nums[k - 1] == nums[k - 2]) continue; @@ -83,7 +77,6 @@ public: int target = 0 - (nums[i] + nums[k]); if (set.find(target) != set.end()) { result.push_back({nums[i], target, nums[k]}); // nums[k]成为c - // 内层循环中,a固定,如果find到了和上轮一样的b,那么c也就和上轮一样,所以去重b set.erase(target); } else { From 451b02dd5d6986df59483ee9f2444acab5d6abe2 Mon Sep 17 00:00:00 2001 From: Alvinn <138186772+fubugun@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:56:47 +0800 Subject: [PATCH 1443/1533] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 8572ec2d5f..8cd505a829 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -45,7 +45,7 @@ 那么二叉树如何可以自底向上查找呢? -回溯啊,二叉树回溯的过程就是从低到上。 +回溯啊,二叉树回溯的过程就是从底到上。 后序遍历(左右中)就是天然的回溯过程,可以根据左右子树的返回值,来处理中节点的逻辑。 From 0763f4b2fe5be95c9105c844e818759d44bfc745 Mon Sep 17 00:00:00 2001 From: Alvinn <138186772+fubugun@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:58:02 +0800 Subject: [PATCH 1444/1533] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index f0ef2366ad..fafef1f2d5 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -58,7 +58,7 @@ * fast和slow同时移动,直到fast指向末尾,如题: - +//图片中有错别词:应该将“只到”改为“直到” * 删除slow指向的下一个节点,如图: From d3dc9f51b7569329d2688eec3d0c23f457c18e02 Mon Sep 17 00:00:00 2001 From: Alvinn <138186772+fubugun@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:01:20 +0800 Subject: [PATCH 1445/1533] =?UTF-8?q?Update=20=E8=B4=AA=E5=BF=83=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index f042c0acf3..6fde2dbbe6 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -78,7 +78,7 @@ * 求解每一个子问题的最优解 * 将局部最优解堆叠成全局最优解 -这个四步其实过于理论化了,我们平时在做贪心类的题目 很难去按照这四步去思考,真是有点“鸡肋”。 +这个四步其实过于理论化了,我们平时在做贪心类的题目时,如果按照这四步去思考,真是有点“鸡肋”。 做题的时候,只要想清楚 局部最优 是什么,如果推导出全局最优,其实就够了。 From a28c40a8bcbb068c9d2f9f4b902322ce04e888ef Mon Sep 17 00:00:00 2001 From: Alvinn <138186772+fubugun@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:06:28 +0800 Subject: [PATCH 1446/1533] =?UTF-8?q?Update=2020201107=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 2d20a19702..7e333c7672 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -75,7 +75,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 除了这些难点,**本题还有细节,例如:切割过的地方不能重复切割所以递归函数需要传入i + 1**。 -所以本题应该是一个道hard题目了。 +所以本题应该是一道hard题目了。 **本题的树形结构中,和代码的逻辑有一个小出入,已经判断不是回文的子串就不会进入递归了,纠正如下:** From c6f44808a37f88bdb5164170042505ceb1d228a0 Mon Sep 17 00:00:00 2001 From: Alvinn <138186772+fubugun@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:15:25 +0800 Subject: [PATCH 1447/1533] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index ed8149d08e..78e1407419 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -172,7 +172,7 @@ if (result.size() == ticketNum + 1) { 回溯的过程中,如何遍历一个机场所对应的所有机场呢? -这里刚刚说过,在选择映射函数的时候,不能选择`unordered_map> targets`, 因为一旦有元素增删multiset的迭代器就会失效,当然可能有牛逼的容器删除元素迭代器不会失效,这里就不在讨论了。 +这里刚刚说过,在选择映射函数的时候,不能选择`unordered_map> targets`, 因为一旦有元素增删multiset的迭代器就会失效,当然可能有牛逼的容器删除元素迭代器不会失效,这里就不再讨论了。 **可以说本题既要找到一个对数据进行排序的容器,而且还要容易增删元素,迭代器还不能失效**。 From b4ebda4fc94e4505ff268ec0e08bd951911e26c5 Mon Sep 17 00:00:00 2001 From: C_W Date: Tue, 10 Dec 2024 16:52:12 +1100 Subject: [PATCH 1448/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00028=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0strStr=20C=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index e0cb123ef1..63a08d960d 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -1456,6 +1456,70 @@ public int[] GetNext(string needle) } ``` +### C: + +> 前缀表统一右移和减一 + +```c + +int *build_next(char* needle, int len) { + + int *next = (int *)malloc(len * sizeof(int)); + assert(next); // 确保分配成功 + + // 初始化next数组 + next[0] = -1; // next[0] 设置为 -1,表示没有有效前缀匹配 + if (len <= 1) { // 如果模式串长度小于等于 1,直接返回 + return next; + } + next[1] = 0; // next[1] 设置为 0,表示第一个字符没有公共前后缀 + + // 构建next数组, i 从模式串的第三个字符开始, j 指向当前匹配的最长前缀长度 + int i = 2, j = 0; + while (i < len) { + if (needle[i - 1] == needle[j]) { + j++; + next[i] = j; + i++; + } else if (j > 0) { + // 如果不匹配且 j > 0, 回退到次长匹配前缀的长度 + j = next[j]; + } else { + next[i] = 0; + i++; + } + } + return next; +} + +int strStr(char* haystack, char* needle) { + + int needle_len = strlen(needle); + int haystack_len = strlen(haystack); + + int *next = build_next(needle, needle_len); + + int i = 0, j = 0; // i 指向主串的当前起始位置, j 指向模式串的当前匹配位置 + while (i <= haystack_len - needle_len) { + if (haystack[i + j] == needle[j]) { + j++; + if (j == needle_len) { + free(next); + next = NULL + return i; + } + } else { + i += j - next[j]; // 调整主串的起始位置 + j = j > 0 ? next[j] : 0; + } + } + + free(next); + next = NULL; + return -1; +} +``` +

From 9768c91ee2d09ffa8aa594fc4160d3bfdf4034dd Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 11 Dec 2024 11:38:38 +0800 Subject: [PATCH 1449/1533] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 0872460907..75e095384b 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -151,13 +151,13 @@ if (abs(target) > sum) return 0; // 此时没有方案 本题则是装满有几种方法。其实这就是一个组合问题了。 -1. 确定dp数组以及下标的含义 +#### 1. 确定dp数组以及下标的含义 先用 二维 dp数组求解本题,dp[i][j]:使用 下标为[0, i]的nums[i]能够凑满j(包括j)这么大容量的包,有dp[i][j]种方法。 01背包为什么这么定义dp数组,我在[0-1背包理论基础](https://www.programmercarl.com/%E8%83%8C%E5%8C%85%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%8001%E8%83%8C%E5%8C%85-1.html)中 确定dp数组的含义里讲解过。 -2. 确定递推公式 +#### 2. 确定递推公式 我们先手动推导一下,这个二维数组里面的数值。 @@ -264,7 +264,7 @@ if (nums[i] > j) dp[i][j] = dp[i - 1][j]; else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; ``` -3. dp数组如何初始化 +#### 3. dp数组如何初始化 先明确递推的方向,如图,求解 dp[2][2] 是由 上方和左上方推出。 @@ -315,7 +315,7 @@ for (int i = 0; i < nums.size(); i++) { } ``` -4. 确定遍历顺序 +#### 4. 确定遍历顺序 在明确递推方向时,我们知道 当前值 是由上方和左上方推出。 @@ -360,7 +360,7 @@ for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 这里大家可以看出,无论是以上哪种遍历,都不影响 dp[2][2]的求值,用来 推导 dp[2][2] 的数值都在。 -5. 举例推导dp数组 +#### 5. 举例推导dp数组 输入:nums: [1, 1, 1, 1, 1], target: 3 @@ -421,7 +421,7 @@ public: dp[i][j] 去掉 行的维度,即 dp[j],表示:填满j(包括j)这么大容积的包,有dp[j]种方法。 -2. 确定递推公式 +#### 2. 确定递推公式 二维DP数组递推公式: `dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]];` @@ -429,17 +429,17 @@ dp[i][j] 去掉 行的维度,即 dp[j],表示:填满j(包括j)这么 **这个公式在后面在讲解背包解决排列组合问题的时候还会用到!** -3. dp数组如何初始化 +#### 3. dp数组如何初始化 在上面 二维dp数组中,我们讲解过 dp[0][0] 初始为1,这里dp[0] 同样初始为1 ,即装满背包为0的方法有一种,放0件物品。 -4. 确定遍历顺序 +#### 4. 确定遍历顺序 在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中,我们系统讲过对于01背包问题一维dp的遍历。 遍历物品放在外循环,遍历背包在内循环,且内循环倒序(为了保证物品只使用一次)。 -5. 举例推导dp数组 +#### 5. 举例推导dp数组 输入:nums: [1, 1, 1, 1, 1], target: 3 @@ -526,7 +526,6 @@ dp[j] += dp[j - nums[i]]; ## 其他语言版本 - ### Java ```java class Solution { From 1a47aea273788b30ab56efc5b58e3404c826cd71 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 11 Dec 2024 11:46:56 +0800 Subject: [PATCH 1450/1533] =?UTF-8?q?=E8=AE=B2=E6=B8=85=E6=A5=9A=E5=A6=82?= =?UTF-8?q?=E4=BD=95=E8=BD=AC=E5=8C=9601=E8=83=8C=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\347\232\204\351\207\215\351\207\217II.md" | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index b40ed114eb..0d445a71f0 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -42,40 +42,41 @@ ## 思路 -如果对背包问题不都熟悉先看这两篇: +如果对背包问题不熟悉的话先看这两篇: -* [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) -* [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) +* [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) +* [01背包理论基础(一维数组)](https://programmercarl.com/背包理论基础01背包-2.html) -本题其实就是尽量让石头分成重量相同的两堆,相撞之后剩下的石头最小,**这样就化解成01背包问题了**。 +本题其实是尽量让石头分成重量相同的两堆(尽可能相同),相撞之后剩下的石头就是最小的。 -是不是感觉和昨天讲解的[416. 分割等和子集](https://programmercarl.com/0416.分割等和子集.html)非常像了。 +一堆的石头重量是sum,那么我们就尽可能拼成 重量为 sum / 2 的石头堆。 这样剩下的石头堆也是 尽可能接近 sum/2 的重量。 +那么此时问题就是有一堆石头,每个石头都有自己的重量,是否可以 装满 最大重量为 sum / 2的背包。 -本题物品的重量为stones[i],物品的价值也为stones[i]。 +看到这里,大家是否感觉和昨天讲解的 [416. 分割等和子集](https://programmercarl.com/0416.分割等和子集.html)非常像了,简直就是同一道题。 -对应着01背包里的物品重量weight[i]和 物品价值value[i]。 +本题**这样就化解成01背包问题了**。 + +**[416. 分割等和子集](https://programmercarl.com/0416.分割等和子集.html) 是求背包是否正好装满,而本题是求背包最多能装多少**。 + +物品就是石头,物品的重量为stones[i],物品的价值也为stones[i]。 接下来进行动规五步曲: -1. 确定dp数组以及下标的含义 +### 1. 确定dp数组以及下标的含义 **dp[j]表示容量(这里说容量更形象,其实就是重量)为j的背包,最多可以背最大重量为dp[j]**。 -可以回忆一下01背包中,dp[j]的含义,容量为j的背包,最多可以装的价值为 dp[j]。 +相对于 01背包,本题中,石头的重量是 stones[i],石头的价值也是 stones[i] 。 -相对于 01背包,本题中,石头的重量是 stones[i],石头的价值也是 stones[i] ,可以 “最多可以装的价值为 dp[j]” == “最多可以背的重量为dp[j]” +“最多可以装的价值为 dp[j]” 等同于 “最多可以背的重量为dp[j]” -2. 确定递推公式 +### 2. 确定递推公式 01背包的递推公式为:dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); 本题则是:**dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]);** -一些同学可能看到这dp[j - stones[i]] + stones[i]中 又有- stones[i] 又有+stones[i],看着有点晕乎。 - -大家可以再去看 dp[j]的含义。 - -3. dp数组如何初始化 +### 3. dp数组如何初始化 既然 dp[j]中的j表示容量,那么最大容量(重量)是多少呢,就是所有石头的重量和。 @@ -95,7 +96,7 @@ vector dp(15001, 0); ``` -4. 确定遍历顺序 +### 4. 确定遍历顺序 在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历! @@ -111,7 +112,7 @@ for (int i = 0; i < stones.size(); i++) { // 遍历物品 ``` -5. 举例推导dp数组 +### 5. 举例推导dp数组 举例,输入:[2,4,1,1],此时target = (2 + 4 + 1 + 1)/2 = 4 ,dp数组状态图如下: @@ -154,10 +155,7 @@ public: 本题其实和[416. 分割等和子集](https://programmercarl.com/0416.分割等和子集.html)几乎是一样的,只是最后对dp[target]的处理方式不同。 -[416. 分割等和子集](https://programmercarl.com/0416.分割等和子集.html)相当于是求背包是否正好装满,而本题是求背包最多能装多少。 - - - +**[416. 分割等和子集](https://programmercarl.com/0416.分割等和子集.html)相当于是求背包是否正好装满,而本题是求背包最多能装多少**。 ## 其他语言版本 From ac6dfbcd204d0b92a6ef394d69743bef4ca4b74a Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 11 Dec 2024 11:53:03 +0800 Subject: [PATCH 1451/1533] =?UTF-8?q?python=E4=BB=A3=E7=A0=81=E4=B8=8D?= =?UTF-8?q?=E5=90=88=E9=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\233\277\346\215\242\346\225\260\345\255\227.md" | 10 ---------- 1 file changed, 10 deletions(-) diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 443b8bfbda..de0ab1a37a 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -288,16 +288,6 @@ func main(){ -### python: -```Python -class Solution: - def change(self, s): - lst = list(s) # Python里面的string也是不可改的,所以也是需要额外空间的。空间复杂度:O(n)。 - for i in range(len(lst)): - if lst[i].isdigit(): - lst[i] = "number" - return ''.join(lst) -``` ### JavaScript: ```js const readline = require("readline"); From e7f96785d45c8858e68df5cae3851a7738a25635 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 11 Dec 2024 11:53:47 +0800 Subject: [PATCH 1452/1533] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A1=A8=E8=BE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index e8d92cc29f..9c813b3ae9 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -100,7 +100,8 @@ Floyd算法核心思想是动态规划。 这里我们用 grid数组来存图,那就把dp数组命名为 grid。 -grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点的最短距离为m。 +grid[i][j][k] = m,表示 **节点i 到 节点j 以[1...k] 集合中的一个节点为中间节点的最短距离为m**。 + 可能有录友会想,凭什么就这么定义呢? From f3ddf8fc16a5703097d6786ad9274a08ce320ddc Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 11 Dec 2024 11:54:47 +0800 Subject: [PATCH 1453/1533] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A1=A8=E8=BE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...347\241\20001\350\203\214\345\214\205-1.md" | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index 2747f179d1..c164461637 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -41,8 +41,6 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 -![动态规划-背包问题](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117175428387.jpg) - 这是标准的背包问题,以至于很多同学看了这个自然就会想到背包,甚至都不知道暴力的解法应该怎么解了。 这样其实是没有从底向上去思考,而是习惯性想到了背包,那么暴力的解法应该是怎么样的呢? @@ -73,7 +71,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 依然动规五部曲分析一波。 -1. 确定dp数组以及下标的含义 +#### 1. 确定dp数组以及下标的含义 我们需要使用二维数组,为什么呢? @@ -87,7 +85,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, i 来表示物品、j表示背包容量。 -(如果想用j 表示物品,j表示背包容量 行不行? 都可以的,个人习惯而已) +(如果想用j 表示物品,i表示背包容量 行不行? 都可以的,个人习惯而已) 我们来尝试把上面的 二维表格填写一下。 @@ -131,7 +129,7 @@ i 来表示物品、j表示背包容量。 **要时刻记着这个dp数组的含义,下面的一些步骤都围绕这dp数组的含义进行的**,如果哪里看懵了,就来回顾一下i代表什么,j又代表什么。 -2. 确定递推公式 +#### 2. 确定递推公式 这里在把基本信息给出来: @@ -176,7 +174,7 @@ i 来表示物品、j表示背包容量。 递归公式: `dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);` -3. dp数组如何初始化 +#### 3. dp数组如何初始化 **关于初始化,一定要和dp数组的定义吻合,否则到递推公式的时候就会越来越乱**。 @@ -197,8 +195,8 @@ dp[0][j],即:i为0,存放编号0的物品的时候,各个容量的背包 代码初始化如下: ```CPP -for (int j = 0 ; j < weight[0]; j++) { // 当然这一步,如果把dp数组预先初始化为0了,这一步就可以省略,但很多同学应该没有想清楚这一点。 - dp[0][j] = 0; +for (int i = 1; i < weight.size(); i++) { // 当然这一步,如果把dp数组预先初始化为0了,这一步就可以省略,但很多同学应该没有想清楚这一点。 + dp[i][0] = 0; } // 正序遍历 for (int j = weight[0]; j <= bagweight; j++) { @@ -236,7 +234,7 @@ for (int j = weight[0]; j <= bagweight; j++) { **费了这么大的功夫,才把如何初始化讲清楚,相信不少同学平时初始化dp数组是凭感觉来的,但有时候感觉是不靠谱的**。 -4. 确定遍历顺序 +#### 4. 确定遍历顺序 在如下图中,可以看出,有两个遍历的维度:物品与背包重量 @@ -293,7 +291,7 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 **其实背包问题里,两个for循环的先后循序是非常有讲究的,理解遍历顺序其实比理解推导公式难多了**。 -5. 举例推导dp数组 +#### 5. 举例推导dp数组 来看一下对应的dp数组的数值,如图: From c11b546119f8679f7bafd9d8865929bc06272f1c Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 11 Dec 2024 11:55:32 +0800 Subject: [PATCH 1454/1533] =?UTF-8?q?=E4=BB=8E=E4=BA=8C=E7=BB=B4=E5=88=B0?= =?UTF-8?q?=E4=B8=80=E7=BB=B4=20=E9=87=8D=E8=AE=B2=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\214\205\344\270\200\347\273\264.md" | 211 +++++++ ...14\345\205\250\350\203\214\345\214\205.md" | 564 ++++++------------ 2 files changed, 399 insertions(+), 376 deletions(-) create mode 100644 "problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" new file mode 100644 index 0000000000..a8e241c3ba --- /dev/null +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" @@ -0,0 +1,211 @@ + +# 完全背包-一维数组 + +本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problempage.php?pid=1052)去练习。 + +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +## 思路 + +本篇我们不再做五部曲分析,核心内容 在 01背包二维 、01背包一维 和 完全背包二维 的讲解中都讲过了。 + +上一篇我们刚刚讲了完全背包二维DP数组的写法: + +```CPP +for (int i = 1; i < n; i++) { // 遍历物品 + for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + if (j < weight[i]) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); + } +} +``` + +压缩成一维DP数组,也就是将上一层拷贝到当前层。 + +将上一层dp[i-1] 的那一层拷贝到 当前层 dp[i] ,那么 递推公式由:`dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i])` 变成: `dp[i][j] = max(dp[i][j], dp[i][j - weight[i]] + value[i])` + +这里有录友想,这样拷贝的话, dp[i - 1][j] 的数值会不会 覆盖了 dp[i][j] 的数值呢? + +并不会,因为 当前层 dp[i][j] 是空的,是没有计算过的。 + +变成 `dp[i][j] = max(dp[i][j], dp[i][j - weight[i]] + value[i])` 我们压缩成一维dp数组,去掉 i 层数维度。 + +即:`dp[j] = max(dp[j], dp[j - weight[i]] + value[i])` + + +接下来我们重点讲一下遍历顺序。 + +看过这两篇的话: + +* [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) +* [01背包理论基础(一维数组)](https://programmercarl.com/背包理论基础01背包-2.html) + +就知道了,01背包中二维dp数组的两个for遍历的先后循序是可以颠倒了,一维dp数组的两个for循环先后循序一定是先遍历物品,再遍历背包容量。 + +**在完全背包中,对于一维dp数组来说,其实两个for循环嵌套顺序是无所谓的**! + +因为dp[j] 是根据 下标j之前所对应的dp[j]计算出来的。 只要保证下标j之前的dp[j]都是经过计算的就可以了。 + +遍历物品在外层循环,遍历背包容量在内层循环,状态如图: + +![动态规划-完全背包1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210126104529605.jpg) + +遍历背包容量在外层循环,遍历物品在内层循环,状态如图: + +![动态规划-完全背包2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210729234011.png) + +看了这两个图,大家就会理解,完全背包中,两个for循环的先后循序,都不影响计算dp[j]所需要的值(这个值就是下标j之前所对应的dp[j])。 + +先遍历背包再遍历物品,代码如下: + +```CPP +for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + for(int i = 0; i < weight.size(); i++) { // 遍历物品 + if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); + } + cout << endl; +} +``` + +先遍历物品再遍历背包: + +```CPP +for(int i = 0; i < weight.size(); i++) { // 遍历物品 + for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); + } +} +``` + +整体代码如下: + +```cpp +#include +#include +using namespace std; + +int main() { + int N, bagWeight; + cin >> N >> bagWeight; + vector weight(N, 0); + vector value(N, 0); + for (int i = 0; i < N; i++) { + int w; + int v; + cin >> w >> v; + weight[i] = w; + value[i] = v; + } + + vector dp(bagWeight + 1, 0); + for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + for(int i = 0; i < weight.size(); i++) { // 遍历物品 + if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); + } + } + cout << dp[bagWeight] << endl; + + return 0; +} +``` + + + +## 总结 + +细心的同学可能发现,**全文我说的都是对于纯完全背包问题,其for循环的先后循环是可以颠倒的!** + +但如果题目稍稍有点变化,就会体现在遍历顺序上。 + +如果问装满背包有几种方式的话? 那么两个for循环的先后顺序就有很大区别了,而leetcode上的题目都是这种稍有变化的类型。 + +这个区别,我将在后面讲解具体leetcode题目中给大家介绍,因为这块如果不结合具题目,单纯的介绍原理估计很多同学会越看越懵! + +别急,下一篇就是了! + +最后,**又可以出一道面试题了,就是纯完全背包,要求先用二维dp数组实现,然后再用一维dp数组实现,最后再问,两个for循环的先后是否可以颠倒?为什么?** + +这个简单的完全背包问题,估计就可以难住不少候选人了。 + + +## 其他语言版本 + +### Java: + +```java +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int N = scanner.nextInt(); + int bagWeight = scanner.nextInt(); + + int[] weight = new int[N]; + int[] value = new int[N]; + for (int i = 0; i < N; i++) { + weight[i] = scanner.nextInt(); + value[i] = scanner.nextInt(); + } + + int[] dp = new int[bagWeight + 1]; + + for (int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + for (int i = 0; i < weight.length; i++) { // 遍历物品 + if (j >= weight[i]) { + dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); + } + } + } + + System.out.println(dp[bagWeight]); + scanner.close(); + } +} + +``` + + + +### Python: + +```python +def complete_knapsack(N, bag_weight, weight, value): + dp = [0] * (bag_weight + 1) + + for j in range(bag_weight + 1): # 遍历背包容量 + for i in range(len(weight)): # 遍历物品 + if j >= weight[i]: + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + + return dp[bag_weight] + +# 输入 +N, bag_weight = map(int, input().split()) +weight = [] +value = [] +for _ in range(N): + w, v = map(int, input().split()) + weight.append(w) + value.append(v) + +# 输出结果 +print(complete_knapsack(N, bag_weight, weight, value)) + + +``` + + +### Go: + +```go + +``` +### Javascript: + +```Javascript +``` + diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 3a50ee7bbf..1e270555d1 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -5,18 +5,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

-# 动态规划:完全背包理论基础 +# 完全背包理论基础-二维DP数组 本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problempage.php?pid=1052)去练习,题意是一样的。 -## 算法公开课 - -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - -## 思路 - -### 完全背包 - +## 完全背包 有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品都有无限个(也就是可以放入背包多次)**,求解将哪些物品装入背包里物品价值总和最大。 @@ -24,14 +17,12 @@ 同样leetcode上没有纯完全背包问题,都是需要完全背包的各种应用,需要转化成完全背包问题,所以我这里还是以纯完全背包问题进行讲解理论和原理。 -在下面的讲解中,我依然举这个例子: +在下面的讲解中,我拿下面数据举例子: -背包最大重量为4。 - -物品为: +背包最大重量为4,物品为: | | 重量 | 价值 | -| --- | --- | --- | +| ----- | ---- | ---- | | 物品0 | 1 | 15 | | 物品1 | 3 | 20 | | 物品2 | 4 | 30 | @@ -40,471 +31,292 @@ 问背包能背的物品最大价值是多少? -01背包和完全背包唯一不同就是体现在遍历顺序上,所以本文就不去做动规五部曲了,我们直接针对遍历顺序经行分析! +**如果没看到之前的01背包讲解,已经要先仔细看如下两篇,01背包是基础,本篇在讲解完全背包,之前的背包基础我将不会重复讲解**。 -关于01背包我如下两篇已经进行深入分析了: +* [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) +* [01背包理论基础(一维数组)](https://programmercarl.com/背包理论基础01背包-2.html) -* [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) -* [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) +动规五部曲分析完全背包,为了从原理上讲清楚,我们先从二维dp数组分析: -首先再回顾一下01背包的核心代码 -```cpp -for(int i = 0; i < weight.size(); i++) { // 遍历物品 - for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); - } -} -``` +### 1. 确定dp数组以及下标的含义 -我们知道01背包内嵌的循环是从大到小遍历,为了保证每个物品仅被添加一次。 +**dp[i][j] 表示从下标为[0-i]的物品,每个物品可以取无限次,放进容量为j的背包,价值总和最大是多少**。 -而完全背包的物品是可以添加多次的,所以要从小到大去遍历,即: +很多录友也会疑惑,凭什么上来就定义 dp数组,思考过程是什么样的, 这个思考过程我在 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的 “确定dp数组以及下标的含义” 有详细讲解。 -```CPP -// 先遍历物品,再遍历背包 -for(int i = 0; i < weight.size(); i++) { // 遍历物品 - for(int j = weight[i]; j <= bagWeight ; j++) { // 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); - } -} -``` +### 2. 确定递推公式 -至于为什么,我在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中也做了讲解。 +这里在把基本信息给出来: -dp状态图如下: +| | 重量 | 价值 | +| ----- | ---- | ---- | +| 物品0 | 1 | 15 | +| 物品1 | 3 | 20 | +| 物品2 | 4 | 30 | +对于递推公式,首先我们要明确有哪些方向可以推导出 dp[i][j]。 -![动态规划-完全背包](https://code-thinking-1253855093.file.myqcloud.com/pics/20210126104510106.jpg) +这里依然拿dp[1][4]的状态来举例: ([01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中也是这个例子,要注意下面的不同之处) -相信很多同学看网上的文章,关于完全背包介绍基本就到为止了。 +求取 dp[1][4] 有两种情况: -**其实还有一个很重要的问题,为什么遍历物品在外层循环,遍历背包容量在内层循环?** +1. 放物品1 +2. 还是不放物品1 -这个问题很多题解关于这里都是轻描淡写就略过了,大家都默认 遍历物品在外层,遍历背包容量在内层,好像本应该如此一样,那么为什么呢? +如果不放物品1, 那么背包的价值应该是 dp[0][4] 即 容量为4的背包,只放物品0的情况。 -难道就不能遍历背包容量在外层,遍历物品在内层? +推导方向如图: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126112952.png) -看过这两篇的话: -* [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) -* [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) +如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 -就知道了,01背包中二维dp数组的两个for遍历的先后循序是可以颠倒了,一维dp数组的两个for循环先后循序一定是先遍历物品,再遍历背包容量。 +容量为1,只考虑放物品0 和物品1 的最大价值是 dp[1][1], **注意 这里和 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 有所不同了**! -**在完全背包中,对于一维dp数组来说,其实两个for循环嵌套顺序是无所谓的!** +在 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中,背包先空留出物品1的容量,此时容量为1,只考虑放物品0的最大价值是 dp[0][1],**因为01背包每个物品只有一个,既然空出物品1,那背包中也不会再有物品1**! -因为dp[j] 是根据 下标j之前所对应的dp[j]计算出来的。 只要保证下标j之前的dp[j]都是经过计算的就可以了。 +而在完全背包中,物品是可以放无限个,所以 即使空出物品1空间重量,那背包中也可能还有物品1,所以此时我们依然考虑放 物品0 和 物品1 的最大价值即: **dp[1][1], 而不是 dp[0][1]** -遍历物品在外层循环,遍历背包容量在内层循环,状态如图: +所以 放物品1 的情况 = dp[1][1] + 物品1 的价值,推导方向如图: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126113104.png) -![动态规划-完全背包1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210126104529605.jpg) -遍历背包容量在外层循环,遍历物品在内层循环,状态如图: +(**注意上图和 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的区别**,对于理解完全背包很重要) -![动态规划-完全背包2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210729234011.png) +两种情况,分别是放物品1 和 不放物品1,我们要取最大值(毕竟求的是最大价值) -看了这两个图,大家就会理解,完全背包中,两个for循环的先后循序,都不影响计算dp[j]所需要的值(这个值就是下标j之前所对应的dp[j])。 +`dp[1][4] = max(dp[0][4], dp[1][1] + 物品1 的价值) ` -先遍历背包在遍历物品,代码如下: +以上过程,抽象化如下: -```CPP -// 先遍历背包,再遍历物品 -for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 - for(int i = 0; i < weight.size(); i++) { // 遍历物品 - if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); - } - cout << endl; -} -``` +* **不放物品i**:背包容量为j,里面不放物品i的最大价值是dp[i - 1][j]。 -完整的C++测试代码如下: +* **放物品i**:背包空出物品i的容量后,背包容量为j - weight[i],dp[i][j - weight[i]] 为背包容量为j - weight[i]且不放物品i的最大价值,那么dp[i][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 -```CPP -// 先遍历物品,在遍历背包 -void test_CompletePack() { - vector weight = {1, 3, 4}; - vector value = {15, 20, 30}; - int bagWeight = 4; - vector dp(bagWeight + 1, 0); - for(int i = 0; i < weight.size(); i++) { // 遍历物品 - for(int j = weight[i]; j <= bagWeight; j++) { // 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); - } - } - cout << dp[bagWeight] << endl; -} -int main() { - test_CompletePack(); -} +递归公式: `dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]);` -``` +(注意,完全背包二维dp数组 和 01背包二维dp数组 递推公式的区别,01背包中是 `dp[i - 1][j - weight[i]] + value[i])`) -```CPP +### 3. dp数组如何初始化 -// 先遍历背包,再遍历物品 -void test_CompletePack() { - vector weight = {1, 3, 4}; - vector value = {15, 20, 30}; - int bagWeight = 4; +**关于初始化,一定要和dp数组的定义吻合,否则到递推公式的时候就会越来越乱**。 - vector dp(bagWeight + 1, 0); +首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: - for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 - for(int i = 0; i < weight.size(); i++) { // 遍历物品 - if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); - } - } - cout << dp[bagWeight] << endl; -} -int main() { - test_CompletePack(); -} +![动态规划-背包问题2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010304192.png) -``` +在看其他情况。 -本题力扣上没有原题,大家可以去[卡码网第52题](https://kamacoder.com/problempage.php?pid=1052)去练习,题意是一样的,C++代码如下: +状态转移方程 `dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]);` 可以看出有一个方向 i 是由 i-1 推导出来,那么i为0的时候就一定要初始化。 -```cpp -#include -#include -using namespace std; +dp[0][j],即:存放编号0的物品的时候,各个容量的背包所能存放的最大价值。 -// 先遍历背包,再遍历物品 -void test_CompletePack(vector weight, vector value, int bagWeight) { +那么很明显当 `j < weight[0]`的时候,dp[0][j] 应该是 0,因为背包容量比编号0的物品重量还小。 - vector dp(bagWeight + 1, 0); +当`j >= weight[0]`时,**dp[0][j] 如果能放下weight[0]的话,就一直装,每一种物品有无限个**。 - for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 - for(int i = 0; i < weight.size(); i++) { // 遍历物品 - if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); - } - } - cout << dp[bagWeight] << endl; -} -int main() { - int N, V; - cin >> N >> V; - vector weight; - vector value; - for (int i = 0; i < N; i++) { - int w; - int v; - cin >> w >> v; - weight.push_back(w); - value.push_back(v); - } - test_CompletePack(weight, value, V); - return 0; +代码初始化如下: + +```CPP +for (int i = 1; i < weight.size(); i++) { // 当然这一步,如果把dp数组预先初始化为0了,这一步就可以省略,但很多同学应该没有想清楚这一点。 + dp[i][0] = 0; } + +// 正序遍历,如果能放下就一直装物品0 +for (int j = weight[0]; j <= bagWeight; j++) + dp[0][j] = dp[0][j - weight[0]] + value[0]; ``` +(注意上面初始化和 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html)的区别在于物品有无限个) -## 总结 +此时dp数组初始化情况如图所示: -细心的同学可能发现,**全文我说的都是对于纯完全背包问题,其for循环的先后循环是可以颠倒的!** +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241114161608.png) -但如果题目稍稍有点变化,就会体现在遍历顺序上。 +dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? -如果问装满背包有几种方式的话? 那么两个for循环的先后顺序就有很大区别了,而leetcode上的题目都是这种稍有变化的类型。 +其实从递归公式: dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); 可以看出dp[i][j] 是由上方和左方数值推导出来了,那么 其他下标初始为什么数值都可以,因为都会被覆盖。 -这个区别,我将在后面讲解具体leetcode题目中给大家介绍,因为这块如果不结合具题目,单纯的介绍原理估计很多同学会越看越懵! +但只不过一开始就统一把dp数组统一初始为0,更方便一些。 -别急,下一篇就是了! +最后初始化代码如下: -最后,**又可以出一道面试题了,就是纯完全背包,要求先用二维dp数组实现,然后再用一维dp数组实现,最后再问,两个for循环的先后是否可以颠倒?为什么?** -这个简单的完全背包问题,估计就可以难住不少候选人了。 +```CPP +// 初始化 dp +vector> dp(weight.size(), vector(bagweight + 1, 0)); +for (int j = weight[0]; j <= bagWeight; j++) { + dp[0][j] = dp[0][j - weight[0]] + value[0]; +} +``` +### 4. 确定遍历顺序 +[01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中我们讲过,01背包二维DP数组,先遍历物品还是先遍历背包都是可以的。 -## 其他语言版本 +因为两种遍历顺序,对于二维dp数组来说,递推公式所需要的值,二维dp数组里对应的位置都有。 -### Java: - -```java -//先遍历物品,再遍历背包 -private static void testCompletePack(){ - int[] weight = {1, 3, 4}; - int[] value = {15, 20, 30}; - int bagWeight = 4; - int[] dp = new int[bagWeight + 1]; - for (int i = 0; i < weight.length; i++){ // 遍历物品 - for (int j = weight[i]; j <= bagWeight; j++){ // 遍历背包容量 - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); - } - } - for (int maxValue : dp){ - System.out.println(maxValue + " "); +详细可以看 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的 【遍历顺序】的讲解 + +所以既可以 先遍历物品再遍历背包: + +```CPP +for (int i = 1; i < n; i++) { // 遍历物品 + for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + if (j < weight[i]) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); } } +``` -//先遍历背包,再遍历物品 -private static void testCompletePackAnotherWay(){ - int[] weight = {1, 3, 4}; - int[] value = {15, 20, 30}; - int bagWeight = 4; - int[] dp = new int[bagWeight + 1]; - for (int i = 1; i <= bagWeight; i++){ // 遍历背包容量 - for (int j = 0; j < weight.length; j++){ // 遍历物品 - if (i - weight[j] >= 0){ - dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]); - } - } - } - for (int maxValue : dp){ - System.out.println(maxValue + " "); +也可以 先遍历背包再遍历物品: + +```CPP +for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + for (int i = 1; i < n; i++) { // 遍历物品 + if (j < weight[i]) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); } } ``` +### 5. 举例推导dp数组 +以本篇举例数据为例,填满了dp二维数组如图: -### Python: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126113752.png) -先遍历物品,再遍历背包(无参版) -```python -def test_CompletePack(): - weight = [1, 3, 4] - value = [15, 20, 30] - bagWeight = 4 - dp = [0] * (bagWeight + 1) - for i in range(len(weight)): # 遍历物品 - for j in range(weight[i], bagWeight + 1): # 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - print(dp[bagWeight]) +因为 物品0 的性价比是最高的,而且 在完全背包中,每一类物品都有无限个,所以有无限个物品0,既然物品0 性价比最高,当然是优先放物品0。 -test_CompletePack() -``` +### 本题代码: -先遍历物品,再遍历背包(有参版) -```python -def test_CompletePack(weight, value, bagWeight): - dp = [0] * (bagWeight + 1) - for i in range(len(weight)): # 遍历物品 - for j in range(weight[i], bagWeight + 1): # 遍历背包容量 - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - return dp[bagWeight] - -if __name__ == "__main__": - weight = [1, 3, 4] - value = [15, 20, 30] - bagWeight = 4 - result = test_CompletePack(weight, value, bagWeight) - print(result) -``` -先遍历背包,再遍历物品(无参版) -```python -def test_CompletePack(): - weight = [1, 3, 4] - value = [15, 20, 30] - bagWeight = 4 +```CPP +#include +#include +using namespace std; - dp = [0] * (bagWeight + 1) +int main() { + int n, bagWeight; + int w, v; + cin >> n >> bagWeight; + vector weight(n); + vector value(n); + for (int i = 0; i < n; i++) { + cin >> weight[i] >> value[i]; + } - for j in range(bagWeight + 1): # 遍历背包容量 - for i in range(len(weight)): # 遍历物品 - if j - weight[i] >= 0: - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + vector> dp(n, vector(bagWeight + 1, 0)); - print(dp[bagWeight]) + // 初始化 + for (int j = weight[0]; j <= bagWeight; j++) + dp[0][j] = dp[0][j - weight[0]] + value[0]; -test_CompletePack() + for (int i = 1; i < n; i++) { // 遍历物品 + for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + if (j < weight[i]) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); + } + } + cout << dp[n - 1][bagWeight] << endl; + + return 0; +} ``` -先遍历背包,再遍历物品(有参版) -```python -def test_CompletePack(weight, value, bagWeight): - dp = [0] * (bagWeight + 1) - for j in range(bagWeight + 1): # 遍历背包容量 - for i in range(len(weight)): # 遍历物品 - if j - weight[i] >= 0: - dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) - return dp[bagWeight] +关于一维dp数组,大家看这里:[完全背包一维dp数组讲解](./背包问题完全背包一维.md) +## 其他语言版本 -if __name__ == "__main__": - weight = [1, 3, 4] - value = [15, 20, 30] - bagWeight = 4 - result = test_CompletePack(weight, value, bagWeight) - print(result) +### Java -``` +```Java +import java.util.Scanner; -### Go: - -```go - -// test_CompletePack1 先遍历物品, 在遍历背包 -func test_CompletePack1(weight, value []int, bagWeight int) int { - // 定义dp数组 和初始化 - dp := make([]int, bagWeight+1) - // 遍历顺序 - for i := 0; i < len(weight); i++ { - // 正序会多次添加 value[i] - for j := weight[i]; j <= bagWeight; j++ { - // 推导公式 - dp[j] = max(dp[j], dp[j-weight[i]]+value[i]) - // debug - //fmt.Println(dp) - } - } - return dp[bagWeight] -} +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int bagWeight = scanner.nextInt(); -// test_CompletePack2 先遍历背包, 在遍历物品 -func test_CompletePack2(weight, value []int, bagWeight int) int { - // 定义dp数组 和初始化 - dp := make([]int, bagWeight+1) - // 遍历顺序 - // j从0 开始 - for j := 0; j <= bagWeight; j++ { - for i := 0; i < len(weight); i++ { - if j >= weight[i] { - // 推导公式 - dp[j] = max(dp[j], dp[j-weight[i]]+value[i]) - } - // debug - //fmt.Println(dp) - } - } - return dp[bagWeight] -} + int[] weight = new int[n]; + int[] value = new int[n]; -func max(a, b int) int { - if a > b { - return a - } - return b -} + for (int i = 0; i < n; i++) { + weight[i] = scanner.nextInt(); + value[i] = scanner.nextInt(); + } -func main() { - weight := []int{1, 3, 4} - price := []int{15, 20, 30} - fmt.Println(test_CompletePack1(weight, price, 4)) - fmt.Println(test_CompletePack2(weight, price, 4)) -} -``` -### Javascript: - -```Javascript -// 先遍历物品,再遍历背包容量 -function test_completePack1() { - let weight = [1, 3, 5] - let value = [15, 20, 30] - let bagWeight = 4 - let dp = new Array(bagWeight + 1).fill(0) - for(let i = 0; i <= weight.length; i++) { - for(let j = weight[i]; j <= bagWeight; j++) { - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]) + int[][] dp = new int[n][bagWeight + 1]; + + // 初始化 + for (int j = weight[0]; j <= bagWeight; j++) { + dp[0][j] = dp[0][j - weight[0]] + value[0]; } - } - console.log(dp) -} -// 先遍历背包容量,再遍历物品 -function test_completePack2() { - let weight = [1, 3, 5] - let value = [15, 20, 30] - let bagWeight = 4 - let dp = new Array(bagWeight + 1).fill(0) - for(let j = 0; j <= bagWeight; j++) { - for(let i = 0; i < weight.length; i++) { - if (j >= weight[i]) { - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]) + // 动态规划 + for (int i = 1; i < n; i++) { + for (int j = 0; j <= bagWeight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); + } } } - } - console.log(2, dp); -} -``` -### TypeScript: - -```typescript -// 先遍历物品,再遍历背包容量 -function test_CompletePack(): void { - const weight: number[] = [1, 3, 4]; - const value: number[] = [15, 20, 30]; - const bagSize: number = 4; - const dp: number[] = new Array(bagSize + 1).fill(0); - for (let i = 0; i < weight.length; i++) { - for (let j = weight[i]; j <= bagSize; j++) { - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); + System.out.println(dp[n - 1][bagWeight]); + scanner.close(); } - } - console.log(dp); } -test_CompletePack(); + ``` -### Scala: +### Go -```scala -// 先遍历物品,再遍历背包容量 -object Solution { - def test_CompletePack() { - var weight = Array[Int](1, 3, 4) - var value = Array[Int](15, 20, 30) - var baseweight = 4 +### Python - var dp = new Array[Int](baseweight + 1) +```python +def knapsack(n, bag_weight, weight, value): + dp = [[0] * (bag_weight + 1) for _ in range(n)] - for (i <- 0 until weight.length) { - for (j <- weight(i) to baseweight) { - dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i)) - } - } - dp(baseweight) - } -} -``` + # 初始化 + for j in range(weight[0], bag_weight + 1): + dp[0][j] = dp[0][j - weight[0]] + value[0] -### Rust: - -```rust -impl Solution { - // 先遍历物品 - fn complete_pack() { - let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4); - let mut dp = vec![0; bag_size + 1]; - for (weight, value) in goods { - for j in weight..=bag_size { - dp[j] = dp[j].max(dp[j - weight] + value); - } - } - println!("先遍历物品:{}", dp[bag_size]); - } + # 动态规划 + for i in range(1, n): + for j in range(bag_weight + 1): + if j < weight[i]: + dp[i][j] = dp[i - 1][j] + else: + dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]) - // 先遍历背包 - fn complete_pack_after() { - let (goods, bag_size) = (vec![(1, 15), (3, 20), (4, 30)], 4); - let mut dp = vec![0; bag_size + 1]; - for i in 0..=bag_size { - for (weight, value) in &goods { - if i >= *weight { - dp[i] = dp[i].max(dp[i - weight] + value); - } - } - } - println!("先遍历背包:{}", dp[bag_size]); - } -} + return dp[n - 1][bag_weight] + +# 输入 +n, bag_weight = map(int, input().split()) +weight = [] +value = [] +for _ in range(n): + w, v = map(int, input().split()) + weight.append(w) + value.append(v) + +# 输出结果 +print(knapsack(n, bag_weight, weight, value)) -#[test] -fn test_complete_pack() { - Solution::complete_pack(); - Solution::complete_pack_after(); -} ``` +### JavaScript + +

From 47d32df226427e9e58639557da54f364a2cfe7ad Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 11 Dec 2024 11:56:17 +0800 Subject: [PATCH 1455/1533] Update --- README.md | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 3e3b67861e..a50dcd72d1 100644 --- a/README.md +++ b/README.md @@ -299,24 +299,25 @@ 背包问题大纲 -11. [动态规划:01背包理论基础](./problems/背包理论基础01背包-1.md) -12. [动态规划:01背包理论基础(滚动数组)](./problems/背包理论基础01背包-2.md) +11. [动态规划:01背包理论基础(二维dp数组)](./problems/背包理论基础01背包-1.md) +12. [动态规划:01背包理论基础(一维dp数组)](./problems/背包理论基础01背包-2.md) 13. [动态规划:416.分割等和子集](./problems/0416.分割等和子集.md) 14. [动态规划:1049.最后一块石头的重量II](./problems/1049.最后一块石头的重量II.md) 15. [本周小结!(动态规划系列三)](./problems/周总结/20210121动规周末总结.md) 16. [动态规划:494.目标和](./problems/0494.目标和.md) 17. [动态规划:474.一和零](./problems/0474.一和零.md) -18. [动态规划:完全背包总结篇](./problems/背包问题理论基础完全背包.md) -19. [动态规划:518.零钱兑换II](./problems/0518.零钱兑换II.md) -20. [本周小结!(动态规划系列四)](./problems/周总结/20210128动规周末总结.md) -21. [动态规划:377.组合总和Ⅳ](./problems/0377.组合总和Ⅳ.md) -22. [动态规划:70.爬楼梯(完全背包版本)](./problems/0070.爬楼梯完全背包版本.md) -23. [动态规划:322.零钱兑换](./problems/0322.零钱兑换.md) -24. [动态规划:279.完全平方数](./problems/0279.完全平方数.md) -25. [本周小结!(动态规划系列五)](./problems/周总结/20210204动规周末总结.md) -26. [动态规划:139.单词拆分](./problems/0139.单词拆分.md) -27. [动态规划:多重背包理论基础](./problems/背包问题理论基础多重背包.md) -28. [背包问题总结篇](./problems/背包总结篇.md) +18. [动态规划:完全背包理论基础(二维dp数组)](./problems/背包问题理论基础完全背包.md) +19. [动态规划:完全背包理论基础(一维dp数组)](./problems/背包问题完全背包一维.md) +20. [动态规划:518.零钱兑换II](./problems/0518.零钱兑换II.md) +21. [本周小结!(动态规划系列四)](./problems/周总结/20210128动规周末总结.md) +22. [动态规划:377.组合总和Ⅳ](./problems/0377.组合总和Ⅳ.md) +23. [动态规划:70.爬楼梯(完全背包版本)](./problems/0070.爬楼梯完全背包版本.md) +24. [动态规划:322.零钱兑换](./problems/0322.零钱兑换.md) +25. [动态规划:279.完全平方数](./problems/0279.完全平方数.md) +26. [本周小结!(动态规划系列五)](./problems/周总结/20210204动规周末总结.md) +27. [动态规划:139.单词拆分](./problems/0139.单词拆分.md) +28. [动态规划:多重背包理论基础](./problems/背包问题理论基础多重背包.md) +29. [背包问题总结篇](./problems/背包总结篇.md) 打家劫舍系列: @@ -408,21 +409,6 @@ (持续更新中....) - -## 十大排序 - -## 数论 - -## 高级数据结构经典题目 - -* 并查集 -* 最小生成树 -* 线段树 -* 树状数组 -* 字典树 - -## 海量数据处理 - # 补充题目 以上题目是重中之重,大家至少要刷两遍以上才能彻底理解,如果熟练以上题目之后还在找其他题目练手,可以再刷以下题目: From fb9186fc79c233a9cf06f9e5ecc3070f1d1c3332 Mon Sep 17 00:00:00 2001 From: C_W Date: Thu, 12 Dec 2024 12:15:00 +1100 Subject: [PATCH 1456/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00459=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=20C=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\345\255\227\347\254\246\344\270\262.md" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 2be8922b9d..de0e6e4def 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -879,6 +879,52 @@ public int[] GetNext(string s) } ``` +### C + +```c +// 前缀表不减一 +int *build_next(char* s, int len) { + + int *next = (int *)malloc(len * sizeof(int)); + assert(next); + + // 初始化前缀表 + next[0] = 0; + + // 构建前缀表表 + int i = 1, j = 0; + while (i < len) { + if (s[i] == s[j]) { + j++; + next[i] = j; + i++; + } else if (j > 0) { + j = next[j - 1]; + } else { + next[i] = 0; + i++; + } + } + return next; +} + +bool repeatedSubstringPattern(char* s) { + + int len = strlen(s); + int *next = build_next(s, len); + bool result = false; + + // 检查最小重复片段能否被长度整除 + if (next[len - 1]) { + result = len % (len - next[len - 1]) == 0; + } + + free(next); + return result; +} + +``` +

From 96bb3d1bfeb507859ad3fcc00bd809f913dbce7c Mon Sep 17 00:00:00 2001 From: holic-x <1301996554@qq.com> Date: Thu, 12 Dec 2024 10:33:08 +0800 Subject: [PATCH 1457/1533] =?UTF-8?q?docs=EF=BC=9A=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E3=80=900096-=E5=9F=8E=E5=B8=82=E9=97=B4=E8=B4=A7=E7=89=A9?= =?UTF-8?q?=E8=BF=90=E8=BE=93III=E3=80=91JAVA=E7=89=88=E6=9C=AC=E7=9A=84SP?= =?UTF-8?q?FA=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充【0096-城市间货物运输III】JAVA版本的SPFA思路 --- ...347\211\251\350\277\220\350\276\223III.md" | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index a3e9e840f3..08fd010cd8 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -702,6 +702,125 @@ public class Main { ``` +```java +class Edge { + public int u; // 边的端点1 + public int v; // 边的端点2 + public int val; // 边的权值 + + public Edge() { + } + + public Edge(int u, int v) { + this.u = u; + this.v = v; + this.val = 0; + } + + public Edge(int u, int v, int val) { + this.u = u; + this.v = v; + this.val = val; + } +} + +/** + * SPFA算法(版本3):处理含【负权回路】的有向图的最短路径问题 + * bellman_ford(版本3) 的队列优化算法版本 + * 限定起点、终点、至多途径k个节点 + */ +public class SPFAForSSSP { + + /** + * SPFA算法 + * + * @param n 节点个数[1,n] + * @param graph 邻接表 + * @param startIdx 开始节点(源点) + */ + public static int[] spfa(int n, List> graph, int startIdx, int k) { + // 定义最大范围 + int maxVal = Integer.MAX_VALUE; + // minDist[i] 源点到节点i的最短距离 + int[] minDist = new int[n + 1]; // 有效节点编号范围:[1,n] + Arrays.fill(minDist, maxVal); // 初始化为maxVal + minDist[startIdx] = 0; // 设置源点到源点的最短路径为0 + + // 定义queue记录每一次松弛更新的节点 + Queue queue = new LinkedList<>(); + queue.offer(startIdx); // 初始化:源点开始(queue和minDist的更新是同步的) + + + // SPFA算法核心:只对上一次松弛的时候更新过的节点关联的边进行松弛操作 + while (k + 1 > 0 && !queue.isEmpty()) { // 限定松弛 k+1 次 + int curSize = queue.size(); // 记录当前队列节点个数(上一次松弛更新的节点个数,用作分层统计) + while (curSize-- > 0) { //分层控制,限定本次松弛只针对上一次松弛更新的节点,不对新增的节点做处理 + // 记录当前minDist状态,作为本次松弛的基础 + int[] minDist_copy = Arrays.copyOfRange(minDist, 0, minDist.length); + + // 取出节点 + int cur = queue.poll(); + // 获取cur节点关联的边,进行松弛操作 + List relateEdges = graph.get(cur); + for (Edge edge : relateEdges) { + int u = edge.u; // 与`cur`对照 + int v = edge.v; + int weight = edge.val; + if (minDist_copy[u] + weight < minDist[v]) { + minDist[v] = minDist_copy[u] + weight; // 更新 + // 队列同步更新(此处有一个针对队列的优化:就是如果已经存在于队列的元素不需要重复添加) + if (!queue.contains(v)) { + queue.offer(v); // 与minDist[i]同步更新,将本次更新的节点加入队列,用做下一个松弛的参考基础 + } + } + } + } + // 当次松弛结束,次数-1 + k--; + } + + // 返回minDist + return minDist; + } + + public static void main(String[] args) { + // 输入控制 + Scanner sc = new Scanner(System.in); + System.out.println("1.输入N个节点、M条边(u v weight)"); + int n = sc.nextInt(); + int m = sc.nextInt(); + + System.out.println("2.输入M条边"); + List> graph = new ArrayList<>(); // 构建邻接表 + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + while (m-- > 0) { + int u = sc.nextInt(); + int v = sc.nextInt(); + int weight = sc.nextInt(); + graph.get(u).add(new Edge(u, v, weight)); + } + + System.out.println("3.输入src dst k(起点、终点、至多途径k个点)"); + int src = sc.nextInt(); + int dst = sc.nextInt(); + int k = sc.nextInt(); + + // 调用算法 + int[] minDist = SPFAForSSSP.spfa(n, graph, src, k); + // 校验起点->终点 + if (minDist[dst] == Integer.MAX_VALUE) { + System.out.println("unreachable"); + } else { + System.out.println("最短路径:" + minDist[n]); + } + } +} +``` + + + ### Python ```python def main(): From b5cbc15bcfe13b3e8a6e45cf0f4e584eddd434d9 Mon Sep 17 00:00:00 2001 From: holic-x <1301996554@qq.com> Date: Thu, 12 Dec 2024 16:53:50 +0800 Subject: [PATCH 1458/1533] =?UTF-8?q?docs=EF=BC=9A=E3=80=900097-=E5=B0=8F?= =?UTF-8?q?=E6=98=8E=E9=80=9B=E5=85=AC=E5=9B=AD=E3=80=91=E8=A1=A5=E5=85=85?= =?UTF-8?q?=20JAVA=E7=89=88=E6=9C=AC=E3=80=90=E5=9F=BA=E4=BA=8E=E4=B8=89?= =?UTF-8?q?=E7=BB=B4=E6=95=B0=E7=BB=84=E7=9A=84Floyd=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit docs:【0097-小明逛公园】补充 JAVA版本【基于三维数组的Floyd算法】 --- ...16\351\200\233\345\205\254\345\233\255.md" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index 8b3078fc01..27ad0eb9fa 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -424,6 +424,71 @@ floyd算法的时间复杂度相对较高,适合 稠密图且源点较多的 ### Java +- 基于三维数组的Floyd算法 + +```java +public class FloydBase { + + // public static int MAX_VAL = Integer.MAX_VALUE; + public static int MAX_VAL = 10005; // 边的最大距离是10^4(不选用Integer.MAX_VALUE是为了避免相加导致数值溢出) + + public static void main(String[] args) { + // 输入控制 + Scanner sc = new Scanner(System.in); + System.out.println("1.输入N M"); + int n = sc.nextInt(); + int m = sc.nextInt(); + + System.out.println("2.输入M条边"); + + // ① dp定义(grid[i][j][k] 节点i到节点j 可能经过节点K(k∈[1,n]))的最短路径 + int[][][] grid = new int[n + 1][n + 1][n + 1]; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + for (int k = 0; k <= n; k++) { + grid[i][j][k] = grid[j][i][k] = MAX_VAL; // 其余设置为最大值 + } + } + } + + // ② dp 推导:grid[i][j][k] = min{grid[i][k][k-1] + grid[k][j][k-1], grid[i][j][k-1]} + while (m-- > 0) { + int u = sc.nextInt(); + int v = sc.nextInt(); + int weight = sc.nextInt(); + grid[u][v][0] = grid[v][u][0] = weight; // 初始化(处理k=0的情况) ③ dp初始化 + } + + // ④ dp推导:floyd 推导 + for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + grid[i][j][k] = Math.min(grid[i][k][k - 1] + grid[k][j][k - 1], grid[i][j][k - 1]); + } + } + } + + System.out.println("3.输入[起点-终点]计划个数"); + int x = sc.nextInt(); + + System.out.println("4.输入每个起点src 终点dst"); + + while (x-- > 0) { + int src = sc.nextInt(); + int dst = sc.nextInt(); + // 根据floyd推导结果输出计划路径的最小距离 + if (grid[src][dst][n] == MAX_VAL) { + System.out.println("-1"); + } else { + System.out.println(grid[src][dst][n]); + } + } + } +} +``` + + + ### Python 基于三维数组的Floyd From d6f7f3adbcd2532fafe6ffc06efc4e3d01f8d1ee Mon Sep 17 00:00:00 2001 From: C_W Date: Thu, 12 Dec 2024 22:32:25 +1100 Subject: [PATCH 1459/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20C=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\345\256\236\347\216\260\346\240\210.md" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index f0fe3a3c30..73d9db1b16 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -1277,6 +1277,95 @@ impl MyStack { } ``` +### C: + +> C:单队列 + +```c +typedef struct Node { + int val; + struct Node *next; +} Node_t; + +// 用单向链表实现queue +typedef struct { + Node_t *head; + Node_t *foot; + int size; +} MyStack; + +MyStack* myStackCreate() { + MyStack *obj = (MyStack *)malloc(sizeof(MyStack)); + assert(obj); + obj->head = NULL; + obj->foot = NULL; + obj->size = 0; + return obj; +} + +void myStackPush(MyStack* obj, int x) { + + Node_t *temp = (Node_t *)malloc(sizeof(Node_t)); + assert(temp); + temp->val = x; + temp->next = NULL; + + // 添加至queue末尾 + if (obj->foot) { + obj->foot->next = temp; + } else { + obj->head = temp; + } + obj->foot = temp; + obj->size++; +} + +int myStackPop(MyStack* obj) { + + // 获取末尾元素 + int target = obj->foot->val; + + if (obj->head == obj->foot) { + free(obj->foot); + obj->head = NULL; + obj->foot = NULL; + } else { + + Node_t *prev = obj->head; + // 移动至queue尾部节点前一个节点 + while (prev->next != obj->foot) { + prev = prev->next; + } + + free(obj->foot); + obj->foot = prev; + obj->foot->next = NULL; + } + + obj->size--; + return target; +} + +int myStackTop(MyStack* obj) { + return obj->foot->val; +} + +bool myStackEmpty(MyStack* obj) { + return obj->size == 0; +} + +void myStackFree(MyStack* obj) { + Node_t *curr = obj->head; + while (curr != NULL) { + Node_t *temp = curr->next; + free(curr); + curr = temp; + } + free(obj); +} + +``` +

From d66e733d6c66b37baec8acd0d42f8161a07bf8fc Mon Sep 17 00:00:00 2001 From: C_W Date: Thu, 12 Dec 2024 22:33:13 +1100 Subject: [PATCH 1460/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200150.=E9=80=86?= =?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?= =?UTF-8?q?.md=20C=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\345\274\217\346\261\202\345\200\274.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 7d4031d7da..0f1e9c23ef 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -502,6 +502,67 @@ impl Solution { } ``` +### C: + +```c +int str_to_int(char *str) { + // string转integer + int num = 0, tens = 1; + for (int i = strlen(str) - 1; i >= 0; i--) { + if (str[i] == '-') { + num *= -1; + break; + } + num += (str[i] - '0') * tens; + tens *= 10; + } + return num; +} + +int evalRPN(char** tokens, int tokensSize) { + + int *stack = (int *)malloc(tokensSize * sizeof(int)); + assert(stack); + int stackTop = 0; + + for (int i = 0; i < tokensSize; i++) { + char symbol = (tokens[i])[0]; + if (symbol < '0' && (tokens[i])[1] == '\0') { + + // pop两个数字 + int num1 = stack[--stackTop]; + int num2 = stack[--stackTop]; + + // 计算结果 + int result; + if (symbol == '+') { + result = num1 + num2; + } else if (symbol == '-') { + result = num2 - num1; + } else if (symbol == '/') { + result = num2 / num1; + } else { + result = num1 * num2; + } + + // push回stack + stack[stackTop++] = result; + + } else { + + // push数字进stack + int num = str_to_int(tokens[i]); + stack[stackTop++] = num; + + } + } + + int result = stack[0]; + free(stack); + return result; +} +``` +

From 739e3f891ce199d7258fa4a192b7fe3dd10a9251 Mon Sep 17 00:00:00 2001 From: C_W Date: Fri, 13 Dec 2024 11:43:42 +1100 Subject: [PATCH 1461/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20C?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...43\346\234\200\345\244\247\345\200\274.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index caa24d8de3..9bb3494db4 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -890,6 +890,38 @@ public: }; ``` +### C + +```c +int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) { + *returnSize = numsSize - k + 1; + int *res = (int*)malloc((*returnSize) * sizeof(int)); + assert(res); + int *deque = (int*)malloc(numsSize * sizeof(int)); + assert(deque); + int front = 0, rear = 0, idx = 0; + + for (int i = 0 ; i < numsSize ; i++) { + while (front < rear && deque[front] <= i - k) { + front++; + } + + while (front < rear && nums[deque[rear - 1]] <= nums[i]) { + rear--; + } + + deque[rear++] = i; + + if (i >= k - 1) { + res[idx++] = nums[deque[front]]; + } + } + + return res; +} + +``` +

From 3ce802897f9cf19ef158cb197f031c1cf3cb8baf Mon Sep 17 00:00:00 2001 From: Murphy Tian Date: Mon, 16 Dec 2024 15:38:43 +0800 Subject: [PATCH 1462/1533] [Fix][DP][Target Sum] python 2D version align with the dp equation --- .../0494.\347\233\256\346\240\207\345\222\214.md" | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index dda3ad7528..eef5ceb616 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -671,18 +671,26 @@ class Solution: # 创建二维动态规划数组,行表示选取的元素数量,列表示累加和 dp = [[0] * (target_sum + 1) for _ in range(len(nums) + 1)] + dp = [[0] * (target_sum + 1) for _ in range(len(nums))] # 初始化状态 dp[0][0] = 1 + if nums[0] <= target_sum: + dp[0][nums[0]] = 1 + numZero = 0 + for i in range(len(nums)): + if nums[i] == 0: + numZero += 1 + dp[i][0] = int(math.pow(2, numZero)) # 动态规划过程 - for i in range(1, len(nums) + 1): + for i in range(1, len(nums)): for j in range(target_sum + 1): dp[i][j] = dp[i - 1][j] # 不选取当前元素 if j >= nums[i - 1]: - dp[i][j] += dp[i - 1][j - nums[i - 1]] # 选取当前元素 + dp[i][j] += dp[i - 1][j - nums[i]] # 选取当前元素 - return dp[len(nums)][target_sum] # 返回达到目标和的方案数 + return dp[len(nums)-1][target_sum] # 返回达到目标和的方案数 ``` From e93f00aef737dad56bf4656a02c17cb66e214fcc Mon Sep 17 00:00:00 2001 From: ForsakenDelusion <144082461+ForsakenDelusion@users.noreply.github.com> Date: Sat, 21 Dec 2024 18:06:53 +0800 Subject: [PATCH 1463/1533] =?UTF-8?q?0235.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88=E4=B8=80=E5=A4=84=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 192bb031dc..3911261a53 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -99,7 +99,7 @@ if (cur == NULL) return cur; * 确定单层递归的逻辑 -在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭又闭) +在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭右闭) 那么如果 cur->val 大于 p->val,同时 cur->val 大于q->val,那么就应该向左遍历(说明目标区间在左子树上)。 From 173ebe75726cf41260ccc8824c73fcb78dfdcd8f Mon Sep 17 00:00:00 2001 From: "Zhen (Evan) Wang" <273509239@qq.com> Date: Wed, 25 Dec 2024 11:14:51 +0800 Subject: [PATCH 1464/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B00059.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II=E7=9A=84C#=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BF=9D=E8=AF=81=E4=B8=8EC++=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=9D=E6=8C=81=E4=B8=80=E8=87=B4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\227\213\347\237\251\351\230\265II.md" | 75 ++++++++++++++----- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index c59ec0333f..94966126b9 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -715,26 +715,65 @@ object Solution { ### C#: ```csharp -public class Solution { - public int[][] GenerateMatrix(int n) { - int[][] answer = new int[n][]; - for(int i = 0; i < n; i++) - answer[i] = new int[n]; - int start = 0; - int end = n - 1; - int tmp = 1; - while(tmp < n * n) +public int[][] GenerateMatrix(int n) +{ + // 参考Carl的代码随想录里面C++的思路 + // https://www.programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html#%E6%80%9D%E8%B7%AF + int startX = 0, startY = 0; // 定义每循环一个圈的起始位置 + int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理 + int count = 1; // 用来给矩阵每个空格赋值 + int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2) + int offset = 1;// 需要控制每一条边遍历的长度,每次循环右边界收缩一位 + + // 构建result二维数组 + int[][] result = new int[n][]; + for (int k = 0; k < n; k++) + { + result[k] = new int[n]; + } + + int i = 0, j = 0; // [i,j] + while (loop > 0) + { + i = startX; + j = startY; + // 四个For循环模拟转一圈 + // 第一排,从左往右遍历,不取最右侧的值(左闭右开) + for (; j < n - offset; j++) + { + result[i][j] = count++; + } + // 右侧的第一列,从上往下遍历,不取最下面的值(左闭右开) + for (; i < n - offset; i++) + { + result[i][j] = count++; + } + + // 最下面的第一行,从右往左遍历,不取最左侧的值(左闭右开) + for (; j > startY; j--) + { + result[i][j] = count++; + } + + // 左侧第一列,从下往上遍历,不取最左侧的值(左闭右开) + for (; i > startX; i--) { - for(int i = start; i < end; i++) answer[start][i] = tmp++; - for(int i = start; i < end; i++) answer[i][end] = tmp++; - for(int i = end; i > start; i--) answer[end][i] = tmp++; - for(int i = end; i > start; i--) answer[i][start] = tmp++; - start++; - end--; - } - if(n % 2 == 1) answer[n / 2][n / 2] = tmp; - return answer; + result[i][j] = count++; + } + // 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1) + startX++; + startY++; + + // offset 控制每一圈里每一条边遍历的长度 + offset++; + loop--; + } + if (n % 2 == 1) + { + // n 为奇数 + result[mid][mid] = count; } + return result; } ``` From 7a1d070c257c16bf01aaf4b95bded65393bc3286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=AE=A3=E9=AD=81?= <13963268+cheng-xuankui@user.noreply.gitee.com> Date: Thu, 26 Dec 2024 20:49:16 +0800 Subject: [PATCH 1465/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A019.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9,=E6=B7=BB=E5=8A=A0=E4=BA=86java?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BD=BF=E7=94=A8=E9=80=92=E5=BD=92=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=88=A0=E9=99=A4=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index fafef1f2d5..16312d0fc4 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -129,6 +129,36 @@ class Solution { } ``` + +```java +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + // 创建一个新的哑节点,指向原链表头 + ListNode s = new ListNode(-1, head); + // 递归调用remove方法,从哑节点开始进行删除操作 + remove(s, n); + // 返回新链表的头(去掉可能的哑节点) + return s.next; + } + + public int remove(ListNode p, int n) { + // 递归结束条件:如果当前节点为空,返回0 + if (p == null) { + return 0; + } + // 递归深入到下一个节点 + int net = remove(p.next, n); + // 如果当前节点是倒数第n个节点,进行删除操作 + if (net == n) { + p.next = p.next.next; + } + // 返回当前节点的总深度 + return net + 1; + } +} +``` + + ### Python: ```python From 0fa443ce1a234704898630c188640f4512e9f7d1 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Thu, 26 Dec 2024 14:03:38 -0600 Subject: [PATCH 1466/1533] =?UTF-8?q?fix:=20150=E9=A2=98=E6=9B=B4=E6=AD=A3?= =?UTF-8?q?Python=E8=A7=A3=E6=B3=95=E4=B8=AD=E4=BD=BF=E7=94=A8eval()?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此处原本提供的两个python解法是一样的,并无区别;更正为实际上真正使用eval()的方法。 --- ...76\345\274\217\346\261\202\345\200\274.md" | 32 ++++++------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 7d4031d7da..4ffe950b5b 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -188,34 +188,20 @@ class Solution(object): return stack.pop() ``` -另一种可行,但因为使用eval相对较慢的方法: +另一种可行,但因为使用eval()相对较慢的方法: ```python -from operator import add, sub, mul - -def div(x, y): - # 使用整数除法的向零取整方式 - return int(x / y) if x * y > 0 else -(abs(x) // abs(y)) - class Solution(object): - op_map = {'+': add, '-': sub, '*': mul, '/': div} - - def evalRPN(self, tokens): - """ - :type tokens: List[str] - :rtype: int - """ + def evalRPN(self, tokens: List[str]) -> int: stack = [] for token in tokens: - if token in self.op_map: - op1 = stack.pop() - op2 = stack.pop() - operation = self.op_map[token] - stack.append(operation(op2, op1)) + # 判断是否为数字,因为isdigit()不识别负数,故需要排除第一位的符号 + if token.isdigit() or (len(token)>1 and token[1].isdigit()): + stack.append(token) else: - stack.append(int(token)) - return stack.pop() - - + op2 = stack.pop() + op1 = stack.pop() + stack.append(str(int(eval(op1 + token + op2)))) + return int(stack.pop()) ``` ### Go: From 0638ba5ad88909b1463ccb43f55cb06f9bc88d09 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: Thu, 26 Dec 2024 14:09:06 -0600 Subject: [PATCH 1467/1533] =?UTF-8?q?docs:=20150=E9=A2=98=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 4ffe950b5b..5fb28c29d4 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -200,6 +200,7 @@ class Solution(object): else: op2 = stack.pop() op1 = stack.pop() + # 由题意"The division always truncates toward zero",所以使用int()可以天然取整 stack.append(str(int(eval(op1 + token + op2)))) return int(stack.pop()) ``` From 5da4a7ec0edb78b465f3f00fd4ef4412bb9d5b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?= Date: Fri, 27 Dec 2024 14:48:22 +0800 Subject: [PATCH 1468/1533] =?UTF-8?q?docs=EF=BC=9A=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E3=80=900459.=E9=87=8D=E5=A4=8D=E7=9A=84=E5=AD=90=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2.md=E3=80=91=E7=9A=84=20JavaScript=20?= =?UTF-8?q?=E3=80=81C#=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\345\255\227\347\254\246\344\270\262.md" | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 2be8922b9d..1820056245 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -648,6 +648,29 @@ var repeatedSubstringPattern = function (s) { }; ``` +> 正则匹配 +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var repeatedSubstringPattern = function(s) { + let reg = /^(\w+)\1+$/ + return reg.test(s) +}; +``` +> 移动匹配 +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var repeatedSubstringPattern = function (s) { + let ss = s + s; + return ss.substring(1, ss.length - 1).includes(s); +}; +``` + ### TypeScript: > 前缀表统一减一 @@ -853,8 +876,10 @@ impl Solution { } ``` ### C# + +> 前缀表不减一 + ```csharp -// 前缀表不减一 public bool RepeatedSubstringPattern(string s) { if (s.Length == 0) @@ -879,6 +904,13 @@ public int[] GetNext(string s) } ``` +> 移动匹配 +```csharp +public bool RepeatedSubstringPattern(string s) { + string ss = (s + s).Substring(1, (s + s).Length - 2); + return ss.Contains(s); +} +```

From aa2450222d48ae248a31b15bbcab6484a5fc3967 Mon Sep 17 00:00:00 2001 From: 1ltwo Date: Fri, 3 Jan 2025 10:11:06 +0800 Subject: [PATCH 1469/1533] =?UTF-8?q?go=E7=89=88=E6=9C=AC=E6=96=B0?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=EF=BC=9A=E5=8F=8C=E6=8C=87=E9=92=88=E9=80=86?= =?UTF-8?q?=E5=BA=8F=E9=81=8D=E5=8E=86=EF=BC=8C=E6=97=B6=E9=97=B4=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6O(n)=EF=BC=8C=E7=A9=BA=E9=97=B4=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6O(n)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\347\232\204\345\215\225\350\257\215.md" | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 7c0b7cb182..3dbd59b916 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -639,7 +639,46 @@ func reverse(b []byte) { } } ``` - +```go +//双指针解法。指针逆序遍历,将遍历后得到的单词(间隔为空格,用以区分)顺序放置在额外空间 +//时间复杂度O(n),空间复杂度O(n) +func reverseWords(s string) string { + strBytes := []byte(s) + n := len(strBytes) + // 记录有效字符范围的起始和结束位置 + start, end := 0, n-1 + // 去除开头空格 + for start < n && strBytes[start] == 32 { + start++ + } + // 处理全是空格或空字符串情况 + if start == n { + return "" + } + // 去除结尾空格 + for end >= 0 && strBytes[end] == 32 { + end-- + } + // 结果切片,预分配容量 + res := make([]byte, 0, end-start+1)//这里挺重要的,本人之前没有预分配容量,每次循环都添加单词,导致内存超限(也可能就是我之前的思路有问题) + // 从后往前遍历有效字符范围 + for i := end; i >= start; { + // 找单词起始位置,直接通过循环条件判断定位 + for ; i >= start && strBytes[i] == 32; i-- { + } + j := i + for ; j >= start && strBytes[j]!= 32; j-- { + } + res = append(res, strBytes[j+1:i+1]...) + // 只在不是最后一个单词时添加空格 + if j > start { + res = append(res, 32) + } + i = j + } + return string(res) +} +``` ### JavaScript: From 4e77de7fc55231493ef96d54a4d6ad88ed7120a0 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 3 Jan 2025 15:21:58 +0800 Subject: [PATCH 1470/1533] =?UTF-8?q?=E9=87=8D=E5=86=99=E9=9B=B6=E9=92=B1?= =?UTF-8?q?=E5=85=91=E6=8D=A2II=EF=BC=8C=E4=BB=8E=E4=BA=8C=E7=BB=B4?= =?UTF-8?q?=E8=A7=92=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\351\222\261\345\205\221\346\215\242II.md" | 239 ++++++++++++++++-- 1 file changed, 211 insertions(+), 28 deletions(-) diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 7023121292..2c82027fd6 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -4,8 +4,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

- - # 518.零钱兑换II [力扣题目链接](https://leetcode.cn/problems/coin-change-ii/) @@ -45,15 +43,19 @@ **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 二维dp讲解 +如果大家认真做完:[分割等和子集](https://www.programmercarl.com/0416.%E5%88%86%E5%89%B2%E7%AD%89%E5%92%8C%E5%AD%90%E9%9B%86.html) , [最后一块石头的重量II](https://www.programmercarl.com/1049.%E6%9C%80%E5%90%8E%E4%B8%80%E5%9D%97%E7%9F%B3%E5%A4%B4%E7%9A%84%E9%87%8D%E9%87%8FII.html) 和 [目标和](https://www.programmercarl.com/0494.%E7%9B%AE%E6%A0%87%E5%92%8C.html) +应该会知道类似这种题目:给出一个总数,一些物品,问能否凑成这个总数。 -## 思路 +这是典型的背包问题! -这是一道典型的背包问题,一看到钱币数量不限,就知道这是一个完全背包。 +本题求的是装满这个背包的物品组合数是多少。 +因为每一种面额的硬币有无限个,所以这是完全背包。 -对完全背包还不了解的同学,可以看这篇:[动态规划:关于完全背包,你该了解这些!](https://programmercarl.com/背包问题理论基础完全背包.html) +对完全背包还不了解的同学,可以看这篇:[完全背包理论基础](https://programmercarl.com/背包问题理论基础完全背包.html) 但本题和纯完全背包不一样,**纯完全背包是凑成背包最大价值是多少,而本题是要求凑成总金额的物品组合个数!** @@ -69,44 +71,182 @@ 如果问的是排列数,那么上面就是两种排列了。 -**组合不强调元素之间的顺序,排列强调元素之间的顺序**。 其实这一点我们在讲解回溯算法专题的时候就讲过了哈。 +**组合不强调元素之间的顺序,排列强调元素之间的顺序**。 其实这一点我们在讲解回溯算法专题的时候就讲过。 那我为什么要介绍这些呢,因为这和下文讲解遍历顺序息息相关! -回归本题,动规五步曲来分析如下: +本题其实与我们讲过 [494. 目标和](https://programmercarl.com/0494.目标和.html) 十分类似。 -1. 确定dp数组以及下标的含义 +[494. 目标和](https://programmercarl.com/0494.目标和.html) 求的是装满背包有多少种方法,而本题是求装满背包有多少种组合。 -dp[j]:凑成总金额j的货币组合数为dp[j] +这有啥区别? -2. 确定递推公式 +**求装满背包有几种方法其实就是求组合数**。 不过 [494. 目标和](https://programmercarl.com/0494.目标和.html) 是 01背包,即每一类物品只有一个。 -dp[j] 就是所有的dp[j - coins[i]](考虑coins[i]的情况)相加。 +以下动规五部曲: -所以递推公式:dp[j] += dp[j - coins[i]]; +### 1、确定dp数组以及下标的含义 -**这个递推公式大家应该不陌生了,我在讲解01背包题目的时候在这篇[494. 目标和](https://programmercarl.com/0494.目标和.html)中就讲解了,求装满背包有几种方法,公式都是:dp[j] += dp[j - nums[i]];** +定义二维dp数值 dp[i][j]:使用 下标为[0, i]的coins[i]能够凑满j(包括j)这么大容量的包,有dp[i][j]种组合方法。 -3. dp数组如何初始化 +很多录友也会疑惑,凭什么上来就定义 dp数组,思考过程是什么样的, 这个思考过程我在 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的 “确定dp数组以及下标的含义” 有详细讲解。 -首先dp[0]一定要为1,dp[0] = 1是 递归公式的基础。如果dp[0] = 0 的话,后面所有推导出来的值都是0了。 +(**强烈建议按照代码随想录的顺序学习,否则可能看不懂我的讲解**) -那么 dp[0] = 1 有没有含义,其实既可以说 凑成总金额0的货币组合数为1,也可以说 凑成总金额0的货币组合数为0,好像都没有毛病。 -但题目描述中,也没明确说 amount = 0 的情况,结果应该是多少。 +### 2、确定递推公式 -这里我认为题目描述还是要说明一下,因为后台测试数据是默认,amount = 0 的情况,组合数为1的。 +> **注意**: 这里的公式推导,与之前讲解过的 [494. 目标和](https://programmercarl.com/0494.目标和.html) 、[完全背包理论基础](https://programmercarl.com/背包问题理论基础完全背包.html) 有极大重复,所以我不在重复讲解原理,而是只讲解区别。 -下标非0的dp[j]初始化为0,这样累计加dp[j - coins[i]]的时候才不会影响真正的dp[j] +我们再回顾一下,[01背包理论基础](https://programmercarl.com/背包理论基础01背包-1.html),中二维DP数组的递推公式为: -dp[0]=1还说明了一种情况:如果正好选了coins[i]后,也就是j-coins[i] == 0的情况表示这个硬币刚好能选,此时dp[0]为1表示只选coins[i]存在这样的一种选法。 +`dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i])` -4. 确定遍历顺序 +在 [完全背包理论基础](https://programmercarl.com/背包问题理论基础完全背包.html) 详细讲解了完全背包二维DP数组的递推公式为: -本题中我们是外层for循环遍历物品(钱币),内层for遍历背包(金钱总额),还是外层for遍历背包(金钱总额),内层for循环遍历物品(钱币)呢? +`dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i])` + + +看去完全背包 和 01背包的差别在哪里? + +在于01背包是 `dp[i - 1][j - weight[i]] + value[i]` ,完全背包是 `dp[i][j - weight[i]] + value[i])` + +主要原因就是 完全背包单类物品有无限个。 + +具体原因我在 [完全背包理论基础(二维)](https://programmercarl.com/背包问题理论基础完全背包.html) 的 「确定递推公式」有详细讲解,如果大家忘了,再回顾一下。 + +我上面有说过,本题和 [494. 目标和](https://programmercarl.com/0494.目标和.html) 是一样的,唯一区别就是 [494. 目标和](https://programmercarl.com/0494.目标和.html) 是 01背包,本题是完全背包。 + + +在[494. 目标和](https://programmercarl.com/0494.目标和.html)中详解讲解了装满背包有几种方法,二维DP数组的递推公式: +`dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]` + +所以本题递推公式:`dp[i][j] = dp[i - 1][j] + dp[i][j - nums[i]]` ,区别依然是 ` dp[i - 1][j - nums[i]]` 和 `dp[i][j - nums[i]]` + +这个 ‘所以’ 我省略了很多推导的内容,因为这些内容在 [494. 目标和](https://programmercarl.com/0494.目标和.html) 和 [完全背包理论基础](https://programmercarl.com/背包问题理论基础完全背包.html) 都详细讲过。 + +这里不再重复讲解。 + +大家主要疑惑点 + +1、 `dp[i][j] = dp[i - 1][j] + dp[i][j - nums[i]]` 这个递归公式框架怎么来的,在 [494. 目标和](https://programmercarl.com/0494.目标和.html) 有详细讲解。 + +2、为什么是 ` dp[i][j - nums[i]]` 而不是 ` dp[i - 1][j - nums[i]]` ,在[完全背包理论基础(二维)](https://programmercarl.com/背包问题理论基础完全背包.html) 有详细讲解 + + +### 3. dp数组如何初始化 + +那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827103507.png) + + +这里首先要关注的就是 dp[0][0] 应该是多少? + +背包空间为0,装满「物品0」 的组合数有多少呢? + +应该是 0 个, 但如果 「物品0」 的 数值就是0呢? 岂不是可以有无限个0 组合 和为0! + +题目描述中说了`1 <= coins.length <= 300` ,所以不用考虑 物品数值为0的情况。 + +那么最上行dp[0][j] 如何初始化呢? + +dp[0][j]的含义:用「物品0」(即coins[0]) 装满 背包容量为j的背包,有几种组合方法。 (如果看不懂dp数组的含义,建议先学习[494. 目标和](https://programmercarl.com/0494.目标和.html)) + +如果 j 可以整除 物品0,那么装满背包就有1种组合方法。 + +初始化代码: + +```CPP +for (int j = 0; j <= bagSize; j++) { + if (j % coins[0] == 0) dp[0][j] = 1; +} +``` + +最左列如何初始化呢? + +dp[i][0] 的含义:用物品i(即coins[i]) 装满容量为0的背包 有几种组合方法。 + +都有一种方法,即不装。 + +所以 dp[i][0] 都初始化为1 + +### 4. 确定遍历顺序 + +二维DP数组的完全背包的两个for循环先后顺序是无所谓的。 + +先遍历背包,还是先遍历物品都是可以的。 + +原理和 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的 「遍历顺序」是一样的,都是因为 两个for循环的先后顺序不影响 递推公式 所需要的数值。 + +具体分析过程看 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的 「遍历顺序」 + +### 5. 打印DP数组 + +以amount为5,coins为:[2,3,5] 为例: + +dp数组应该是这样的: + +``` +1 0 1 0 1 0 +1 0 1 1 1 1 +1 0 1 1 1 2 +``` + +### 代码实现: + +```CPP +class Solution { +public: + int change(int amount, vector& coins) { + int bagSize = amount; + + vector> dp(coins.size(), vector(bagSize + 1, 0)); + + // 初始化最上行 + for (int j = 0; j <= bagSize; j++) { + if (j % coins[0] == 0) dp[0][j] = 1; + } + // 初始化最左列 + for (int i = 0; i < coins.size(); i++) { + dp[i][0] = 1; + } + // 以下遍历顺序行列可以颠倒 + for (int i = 1; i < coins.size(); i++) { // 行,遍历物品 + for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 + if (coins[i] > j) dp[i][j] = dp[i - 1][j]; + else dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i]]; + } + } + return dp[coins.size() - 1][bagSize]; + } +}; +``` + +## 一维dp讲解 + +### 1、确定dp数组以及下标的含义 + +dp[j]:凑成总金额j的货币组合数为dp[j] + +### 2、确定递推公式 + +本题 二维dp 递推公式: `dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i]]` + +压缩成一维:`dp[j] += dp[j - coins[i]]` + +这个递推公式大家应该不陌生了,我在讲解01背包题目的时候在这篇[494. 目标和](https://programmercarl.com/0494.目标和.html)中就讲解了,求装满背包有几种方法,公式都是:`dp[j] += dp[j - nums[i]]` + +### 3. dp数组如何初始化 + +装满背包容量为0 的方法是1,即不放任何物品,`dp[0] = 1` + +### 4. 确定遍历顺序 -我在[动态规划:关于完全背包,你该了解这些!](https://programmercarl.com/背包问题理论基础完全背包.html)中讲解了完全背包的两个for循环的先后顺序都是可以的。 +本题中我们是外层for循环遍历物品(钱币),内层for遍历背包(金钱总额),还是外层for遍历背包(金钱总额),内层for循环遍历物品(钱币)呢? + +我在[完全背包(一维DP)](./背包问题完全背包一维.md)中讲解了完全背包的两个for循环的先后顺序都是可以的。 **但本题就不行了!** @@ -116,7 +256,7 @@ dp[0]=1还说明了一种情况:如果正好选了coins[i]后,也就是j-coi 所以纯完全背包是能凑成总和就行,不用管怎么凑的。 -本题是求凑出来的方案个数,且每个方案个数是为组合数。 +本题是求凑出来的方案个数,且每个方案个数是组合数。 那么本题,两个for循环的先后顺序可就有说法了。 @@ -154,7 +294,7 @@ for (int j = 0; j <= amount; j++) { // 遍历背包容量 可能这里很多同学还不是很理解,**建议动手把这两种方案的dp数组数值变化打印出来,对比看一看!(实践出真知)** -5. 举例推导dp数组 +### 5. 举例推导dp数组 输入: amount = 5, coins = [1, 2, 5] ,dp状态图如下: @@ -208,7 +348,17 @@ public: ## 总结 -本题的递推公式,其实我们在[494. 目标和](https://programmercarl.com/0494.目标和.html)中就已经讲过了,**而难点在于遍历顺序!** +本题我们从 二维 分析到 一维。 + +大家在刚开始学习的时候,从二维开始学习 容易理解。 + +之后,推荐大家直接掌握一维的写法,熟练后更容易写出来。 + +本题中,二维dp主要是就要 想清楚和我们之前讲解的 [01背包理论基础](https://programmercarl.com/背包理论基础01背包-1.html)、[494. 目标和](https://programmercarl.com/0494.目标和.html)、 [完全背包理论基础](https://programmercarl.com/背包问题理论基础完全背包.html) 联系与区别。 + +这也是代码随想录安排刷题顺序的精髓所在。 + +本题的一维dp中,难点在于理解便利顺序。 在求装满背包有几种方案的时候,认清遍历顺序是非常关键的。 @@ -216,8 +366,7 @@ public: **如果求排列数就是外层for遍历背包,内层for循环遍历物品**。 -可能说到排列数录友们已经有点懵了,后面Carl还会安排求排列数的题目,到时候在对比一下,大家就会发现神奇所在! - +可能说到排列数录友们已经有点懵了,后面我还会安排求排列数的题目,到时候在对比一下,大家就会发现神奇所在! ## 其他语言版本 @@ -444,3 +593,37 @@ public class Solution + +---------- + + + +回归本题,动规五步曲来分析如下: + +1. 确定dp数组以及下标的含义 + +dp[j]:凑成总金额j的货币组合数为dp[j] + +2. 确定递推公式 + +dp[j] 就是所有的dp[j - coins[i]](考虑coins[i]的情况)相加。 + +所以递推公式:dp[j] += dp[j - coins[i]]; + +**这个递推公式大家应该不陌生了,我在讲解01背包题目的时候在这篇[494. 目标和](https://programmercarl.com/0494.目标和.html)中就讲解了,求装满背包有几种方法,公式都是:dp[j] += dp[j - nums[i]];** + +3. dp数组如何初始化 + +首先dp[0]一定要为1,dp[0] = 1是 递归公式的基础。如果dp[0] = 0 的话,后面所有推导出来的值都是0了。 + +那么 dp[0] = 1 有没有含义,其实既可以说 凑成总金额0的货币组合数为1,也可以说 凑成总金额0的货币组合数为0,好像都没有毛病。 + +但题目描述中,也没明确说 amount = 0 的情况,结果应该是多少。 + +这里我认为题目描述还是要说明一下,因为后台测试数据是默认,amount = 0 的情况,组合数为1的。 + +下标非0的dp[j]初始化为0,这样累计加dp[j - coins[i]]的时候才不会影响真正的dp[j] + +dp[0]=1还说明了一种情况:如果正好选了coins[i]后,也就是j-coins[i] == 0的情况表示这个硬币刚好能选,此时dp[0]为1表示只选coins[i]存在这样的一种选法。 + +---------------- From aa6d892eaa9143312d8529d0b95ac7d418985c55 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 3 Jan 2025 15:22:41 +0800 Subject: [PATCH 1471/1533] =?UTF-8?q?=E7=AC=94=E8=AF=AF=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- ...\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 4cab00ccbd..da2ebd3051 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -99,7 +99,7 @@ public: 这道绝佳的面试题我没有用过,如果录友们有面试别人的需求,就把这个套路拿去吧。 -我在[通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html)中,以我自己面试别人的真实经历,通过求x的n次方 这么简单的题目,就可以考察候选人对算法性能以及递归的理解深度,录友们可以看看,绝对有收获! +我在[通过一道面试题目,讲一讲递归算法的时间复杂度!](../前序/递归算法的时间复杂度.md)中,以我自己面试别人的真实经历,通过求x的n次方 这么简单的题目,就可以考察候选人对算法性能以及递归的理解深度,录友们可以看看,绝对有收获! ## 周四 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 1e270555d1..0cc6e91592 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -95,7 +95,7 @@ * **放物品i**:背包空出物品i的容量后,背包容量为j - weight[i],dp[i][j - weight[i]] 为背包容量为j - weight[i]且不放物品i的最大价值,那么dp[i][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 -递归公式: `dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]);` +递推公式: `dp[i][j] = max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]);` (注意,完全背包二维dp数组 和 01背包二维dp数组 递推公式的区别,01背包中是 `dp[i - 1][j - weight[i]] + value[i])`) From b72609d65ed2c41212ba39bba57f21615be6077e Mon Sep 17 00:00:00 2001 From: Jian Date: Sat, 4 Jan 2025 01:59:28 +0800 Subject: [PATCH 1472/1533] =?UTF-8?q?0018=E5=9B=9B=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=20=E4=BF=AE=E6=94=B9=E6=80=9D=E8=B7=AF=E7=AC=AC?= =?UTF-8?q?=E4=BA=8C=E6=AE=B5=E6=9C=AB=E5=B0=BE=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 64923e41fc..fb8557fab9 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -35,7 +35,7 @@ 四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。 -但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target`是`-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[i] > target && (nums[i] >=0 || target >= 0)`就可以了。 +但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target`是`-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[k] > target && (nums[k] >=0 || target >= 0)`就可以了。 [15.三数之和](https://programmercarl.com/0015.三数之和.html)的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。 @@ -802,3 +802,4 @@ end + From 69bee02cf12f11cf33fbc1f86b6ae2132dde1afb Mon Sep 17 00:00:00 2001 From: aPurpleBerry <2454196228@qq.com> Date: Sun, 5 Jan 2025 15:57:57 +0800 Subject: [PATCH 1473/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C?= =?UTF-8?q?=E5=8C=85=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85.md=20JavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\205\250\350\203\214\345\214\205.md" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 0cc6e91592..ea658f7e51 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -316,7 +316,51 @@ print(knapsack(n, bag_weight, weight, value)) ### JavaScript +```js +const readline = require('readline').createInterface({ + input: process.stdin, + output: process.stdout +}); + +let input = []; +readline.on('line', (line) => { + input.push(line.trim()); +}); + +readline.on('close', () => { + // 第一行解析 n 和 v + const [n, bagweight] = input[0].split(' ').map(Number); + + /// 剩余 n 行解析重量和价值 + const weight = []; + const value = []; + for (let i = 1; i <= n; i++) { + const [wi, vi] = input[i].split(' ').map(Number); + weight.push(wi); + value.push(vi); + } + + + let dp = Array.from({ length: n }, () => Array(bagweight + 1).fill(0)); + + for (let j = weight[0]; j <= bagweight; j++) { + dp[0][j] = dp[0][j-weight[0]] + value[0]; + } + + for (let i = 1; i < n; i++) { + for (let j = 0; j <= bagweight; j++) { + if (j < weight[i]) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - weight[i]] + value[i]); + } + } + } + console.log(dp[n - 1][bagweight]); +}); + +```

From ba5e1b443f8c5b80ccf5f7a34d3ee2b89cd34d47 Mon Sep 17 00:00:00 2001 From: 1ltwo Date: Tue, 7 Jan 2025 22:30:25 +0800 Subject: [PATCH 1474/1533] =?UTF-8?q?go=E7=89=88=E6=9C=AC=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\344\271\260\345\234\237\345\234\260.md" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" index 739e2cadcb..6ef46f2109 100644 --- "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -526,3 +526,89 @@ int main() } ``` + +### Go + +前缀和 + +```go +package main + +import ( + "fmt" + "os" + "bufio" + "strings" + "strconv" + "math" +) + +func main() { + var n, m int + + reader := bufio.NewReader(os.Stdin) + + line, _ := reader.ReadString('\n') + line = strings.TrimSpace(line) + params := strings.Split(line, " ") + + n, _ = strconv.Atoi(params[0]) + m, _ = strconv.Atoi(params[1])//n和m读取完成 + + land := make([][]int, n)//土地矩阵初始化 + + for i := 0; i < n; i++ { + line, _ := reader.ReadString('\n') + line = strings.TrimSpace(line) + values := strings.Split(line, " ") + land[i] = make([]int, m) + for j := 0; j < m; j++ { + value, _ := strconv.Atoi(values[j]) + land[i][j] = value + } + }//所有读取完成 + + //初始化前缀和矩阵 + preMatrix := make([][]int, n+1) + for i := 0; i <= n; i++ { + preMatrix[i] = make([]int, m+1) + } + + for a := 1; a < n+1; a++ { + for b := 1; b < m+1; b++ { + preMatrix[a][b] = land[a-1][b-1] + preMatrix[a-1][b] + preMatrix[a][b-1] - preMatrix[a-1][b-1] + } + } + + totalSum := preMatrix[n][m] + + minDiff := math.MaxInt32//初始化极大数,用于比较 + + //按行分割 + for i := 1; i < n; i++ { + topSum := preMatrix[i][m] + + bottomSum := totalSum - topSum + + diff := int(math.Abs(float64(topSum - bottomSum))) + if diff < minDiff { + minDiff = diff + } + } + + //按列分割 + for j := 1; j < m; j++ { + topSum := preMatrix[n][j] + + bottomSum := totalSum - topSum + + diff := int(math.Abs(float64(topSum - bottomSum))) + if diff < minDiff { + minDiff = diff + } + } + + fmt.Println(minDiff) +} +``` + From d5e0827abeabbcc97b341e99231966bfbafaf7cd Mon Sep 17 00:00:00 2001 From: Jian Date: Wed, 8 Jan 2025 01:04:07 +0800 Subject: [PATCH 1475/1533] =?UTF-8?q?0020=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7=20Java=E6=9B=B4=E7=AE=80=E5=8D=95=E6=98=93?= =?UTF-8?q?=E6=87=82=E7=9A=84=E6=96=B0=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\347\232\204\346\213\254\345\217\267.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index f2f5cdd13f..493e2871ae 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -172,6 +172,29 @@ class Solution { } ``` +```java +// 解法二 +// 对应的另一半一定在栈顶 +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + for(char c : s.toCharArray()){ + // 有对应的另一半就直接消消乐 + if(c == ')' && !stack.isEmpty() && stack.peek() == '(') + stack.pop(); + else if(c == '}' && !stack.isEmpty() && stack.peek() == '{') + stack.pop(); + else if(c == ']' && !stack.isEmpty() && stack.peek() == '[') + stack.pop(); + else + stack.push(c);// 没有匹配的就放进去 + } + + return stack.isEmpty(); + } +} +``` + ### Python: ```python From a7ad0cd812649e0cf1928d9c4e54bc4369588a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sat, 11 Jan 2025 19:18:46 +0800 Subject: [PATCH 1476/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9leetcode-master\pro?= =?UTF-8?q?blems\kamacoder\0044.=E5=BC=80=E5=8F=91=E5=95=86=E8=B4=AD?= =?UTF-8?q?=E4=B9=B0=E5=9C=9F=E5=9C=B0.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\206\350\264\255\344\271\260\345\234\237\345\234\260.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" index 739e2cadcb..efad56dac5 100644 --- "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -212,13 +212,14 @@ public class Main { int horizontalCut = 0; for (int i = 0; i < n; i++) { horizontalCut += horizontal[i]; - result = Math.min(result, Math.abs(sum - 2 * horizontalCut)); + result = Math.min(result, Math.abs((sum - horizontalCut) - horizontalCut)); + // 更新result。其中,horizontalCut表示前i行的和,sum - horizontalCut表示剩下的和,作差、取绝对值,得到题目需要的“A和B各自的子区域内的土地总价值之差”。下同。 } int verticalCut = 0; for (int j = 0; j < m; j++) { verticalCut += vertical[j]; - result = Math.min(result, Math.abs(sum - 2 * verticalCut)); + result = Math.min(result, Math.abs((sum - verticalCut) - verticalCut)); } System.out.println(result); From cb5b6e524197e1e2f66bcd3b157b37bfbc726803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sun, 12 Jan 2025 11:54:23 +0800 Subject: [PATCH 1477/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9leetcode-master\pro?= =?UTF-8?q?blems\0349.=E4=B8=A4=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84?= =?UTF-8?q?=E4=BA=A4=E9=9B=86.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\204\347\232\204\344\272\244\351\233\206.md" | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 77dfc50a61..93fa09318a 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -123,6 +123,9 @@ public: ### Java: 版本一:使用HashSet ```Java +// 时间复杂度O(n+m+k) 空间复杂度O(n+k) +// 其中n是数组nums1的长度,m是数组nums2的长度,k是交集元素的个数 + import java.util.HashSet; import java.util.Set; @@ -145,8 +148,15 @@ class Solution { } //方法1:将结果集合转为数组 - - return resSet.stream().mapToInt(x -> x).toArray(); + return res.stream().mapToInt(Integer::intValue).toArray(); + /** + * 将 Set 转换为 int[] 数组: + * 1. stream() : Collection 接口的方法,将集合转换为 Stream + * 2. mapToInt(Integer::intValue) : + * - 中间操作,将 Stream 转换为 IntStream + * - 使用方法引用 Integer::intValue,将 Integer 对象拆箱为 int 基本类型 + * 3. toArray() : 终端操作,将 IntStream 转换为 int[] 数组。 + */ //方法2:另外申请一个数组存放setRes中的元素,最后返回数组 int[] arr = new int[resSet.size()]; @@ -538,3 +548,4 @@ end + From 61c04cdc433050e09f8f67aeb1fc45454a7f1e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 13 Jan 2025 15:47:55 +0800 Subject: [PATCH 1478/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1.md=E7=9A=84=E6=97=B6=E9=97=B4=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index eb83d3ece9..1d7391732f 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -104,7 +104,7 @@ public: }; ``` -* 时间复杂度: O(n) +* 时间复杂度: O(m+n),其中m表示ransomNote的长度,n表示magazine的长度 * 空间复杂度: O(1) @@ -470,3 +470,4 @@ bool canConstruct(char* ransomNote, char* magazine) { + From 13cb15cad6d548a99b4f4b427a3d57f91d5ce3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 13 Jan 2025 16:33:28 +0800 Subject: [PATCH 1479/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00018.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 64923e41fc..acf8263c09 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -253,7 +253,7 @@ public class Solution { for (int k = 0; k < nums.length; k++) { // 剪枝处理 if (nums[k] > target && nums[k] >= 0) { - break; + break; // 此处的break可以等价于return result; } // 对nums[k]去重 if (k > 0 && nums[k] == nums[k - 1]) { @@ -262,7 +262,7 @@ public class Solution { for (int i = k + 1; i < nums.length; i++) { // 第二级剪枝 if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) { - break; + break; // 注意是break到上一级for循环,如果直接return result;会有遗漏 } // 对nums[i]去重 if (i > k + 1 && nums[i] == nums[i - 1]) { @@ -802,3 +802,4 @@ end + From 059b6464c0b8968bd139764988ecbc652eebdabe Mon Sep 17 00:00:00 2001 From: "Zhen (Evan) Wang" <273509239@qq.com> Date: Tue, 14 Jan 2025 10:36:56 +0800 Subject: [PATCH 1480/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?--199.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=8F=B3=E8=A7=86?= =?UTF-8?q?=E5=9B=BE=20=20C#=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0102.二叉树的层序遍历中的199.二叉树的右视图 C# 代码 --- ...02\345\272\217\351\201\215\345\216\206.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 98e1e98a6e..0c3b0f8c5e 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1231,6 +1231,47 @@ impl Solution { } ``` +#### C#: + +```C# 199.二叉树的右视图 +public class Solution +{ + public IList RightSideView(TreeNode root) + { + var result = new List(); + Queue queue = new(); + + if (root != null) + { + queue.Enqueue(root); + } + while (queue.Count > 0) + { + int count = queue.Count; + int lastValue = count - 1; + for (int i = 0; i < count; i++) + { + var currentNode = queue.Dequeue(); + if (i == lastValue) + { + result.Add(currentNode.val); + } + + // lastValue == i == count -1 : left 先于 right 进入Queue + if (currentNode.left != null) queue.Enqueue(currentNode.left); + if (currentNode.right != null) queue.Enqueue(currentNode.right); + + //// lastValue == i == 0: right 先于 left 进入Queue + // if(currentNode.right !=null ) queue.Enqueue(currentNode.right); + // if(currentNode.left !=null ) queue.Enqueue(currentNode.left); + } + } + + return result; + } +} +``` + ## 637.二叉树的层平均值 [力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) From 2bcd88a096d694b295a855062339579458c0769f Mon Sep 17 00:00:00 2001 From: "Zhen (Evan) Wang" <273509239@qq.com> Date: Tue, 14 Jan 2025 11:18:41 +0800 Subject: [PATCH 1481/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?-=20637.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=B9=B3?= =?UTF-8?q?=E5=9D=87=E5=80=BC=20C#=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 637.二叉树的层平均值 C# 版本 --- ...02\345\272\217\351\201\215\345\216\206.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 0c3b0f8c5e..ce53e49af9 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1599,6 +1599,35 @@ impl Solution { } ``` +#### C#: + +```C# 二叉树的层平均值 +public class Solution { + public IList AverageOfLevels(TreeNode root) { + var result= new List(); + Queue queue = new(); + if(root !=null) queue.Enqueue(root); + + while (queue.Count > 0) + { + int count = queue.Count; + double value=0; + for (int i = 0; i < count; i++) + { + var curentNode=queue.Dequeue(); + value += curentNode.val; + if (curentNode.left!=null) queue.Enqueue(curentNode.left); + if (curentNode.right!=null) queue.Enqueue(curentNode.right); + } + result.Add(value/count); + } + + return result; + } +} + +``` + ## 429.N叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) From 275efa0c70ecdb36c2c77eb2e26059b7b8b29d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Wed, 15 Jan 2025 15:52:42 +0800 Subject: [PATCH 1482/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A00459.=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= =?UTF-8?q?=E7=9A=84Java=E7=89=88=E6=9C=AC=E4=BA=8C=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E8=A1=A8=E4=B8=8D=E5=87=8F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\345\255\227\347\254\246\344\270\262.md" | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index de0e6e4def..bdced0ef60 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -390,6 +390,8 @@ public: ### Java: +(版本一) 前缀表 减一 + ```java class Solution { public boolean repeatedSubstringPattern(String s) { @@ -420,6 +422,45 @@ class Solution { } ``` +(版本二) 前缀表 不减一 + +```java +/* + * 充分条件:如果字符串s是由重复子串组成的,那么它的最长相等前后缀不包含的子串一定是s的最小重复子串。 + * 必要条件:如果字符串s的最长相等前后缀不包含的子串是s的最小重复子串,那么s必然是由重复子串组成的。 + * 推得:当字符串s的长度可以被其最长相等前后缀不包含的子串的长度整除时,不包含的子串就是s的最小重复子串。 + * + * 时间复杂度:O(n) + * 空间复杂度:O(n) + */ +class Solution { + public boolean repeatedSubstringPattern(String s) { + // if (s.equals("")) return false; + // 边界判断(可以去掉,因为题目给定范围是1 <= s.length <= 10^4) + int n = s.length(); + + // Step 1.构建KMP算法的前缀表 + int[] next = new int[n]; // 前缀表的值表示 以该位置结尾的字符串的最长相等前后缀的长度 + int j = 0; + next[0] = 0; + for (int i = 1; i < n; i++) { + while (j > 0 && s.charAt(i) != s.charAt(j)) // 只要前缀后缀还不一致,就根据前缀表回退j直到起点为止 + j = next[j - 1]; + if (s.charAt(i) == s.charAt(j)) + j++; + next[i] = j; + } + + // Step 2.判断重复子字符串 + if (next[n - 1] > 0 && n % (n - next[n - 1]) == 0) { // 当字符串s的长度可以被其最长相等前后缀不包含的子串的长度整除时 + return true; // 不包含的子串就是s的最小重复子串 + } else { + return false; + } + } +} +``` + ### Python: (版本一) 前缀表 减一 @@ -930,4 +971,3 @@ bool repeatedSubstringPattern(char* s) { - From 1d0333f59634bc68b9c4f253f95a2ef8f7a5009e Mon Sep 17 00:00:00 2001 From: Anqi Li <103280095+iqna126@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:16:40 -0800 Subject: [PATCH 1483/1533] =?UTF-8?q?=E7=BB=990054.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md=20=E5=8A=A0=E5=85=A5python=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\346\225\260\345\255\227.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index de0ab1a37a..e4b5c43fef 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -215,6 +215,46 @@ public class Main { } ``` +### Python: +```python +class Solution(object): + def subsitute_numbers(self, s): + """ + :type s: str + :rtype: str + """ + + count = sum(1 for char in s if char.isdigit()) # 统计数字的个数 + expand_len = len(s) + (count * 5) # 计算扩充后字符串的大小, x->number, 每有一个数字就要增加五个长度 + res = [''] * expand_len + + new_index = expand_len - 1 # 指向扩充后字符串末尾 + old_index = len(s) - 1 # 指向原字符串末尾 + + while old_index >= 0: # 从后往前, 遇到数字替换成“number” + if s[old_index].isdigit(): + res[new_index-5:new_index+1] = "number" + new_index -= 6 + else: + res[new_index] = s[old_index] + new_index -= 1 + old_index -= 1 + + return "".join(res) + +if __name__ == "__main__": + solution = Solution() + + while True: + try: + s = input() + result = solution.subsitute_numbers(s) + print(result) + except EOFError: + break + +``` + ### Go: ````go package main From 20385f114619bd5a1c90f0f10ebd4ed7a76b9b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Fri, 17 Jan 2025 10:29:24 +0800 Subject: [PATCH 1484/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md=E7=9A=84Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=B8=80=E5=A4=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index f2f5cdd13f..9d9168e31e 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -166,7 +166,7 @@ class Solution { deque.pop(); } } - //最后判断栈中元素是否匹配 + //遍历结束,如果栈为空,则括号全部匹配 return deque.isEmpty(); } } @@ -555,3 +555,4 @@ impl Solution { + From a66142cbc57e9f3fbe3d5b72689d5cab38fbc690 Mon Sep 17 00:00:00 2001 From: Jian Date: Fri, 17 Jan 2025 19:37:21 +0800 Subject: [PATCH 1485/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9239=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BCjava?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A=E4=B8=A5=E9=87=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\227\345\217\243\346\234\200\345\244\247\345\200\274.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index caa24d8de3..63ee3daedc 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -267,7 +267,7 @@ class Solution { //利用双端队列手动实现单调队列 /** * 用一个单调队列来存储对应的下标,每当窗口滑动的时候,直接取队列的头部指针对应的值放入结果集即可 - * 单调队列类似 (tail -->) 3 --> 2 --> 1 --> 0 (--> head) (右边为头结点,元素存的是下标) + * 单调递减队列类似 (head -->) 3 --> 2 --> 1 --> 0 (--> tail) (左边为头结点,元素存的是下标) */ class Solution { public int[] maxSlidingWindow(int[] nums, int k) { @@ -281,7 +281,7 @@ class Solution { while(!deque.isEmpty() && deque.peek() < i - k + 1){ deque.poll(); } - // 2.既然是单调,就要保证每次放进去的数字要比末尾的都大,否则也弹出 + // 2.维护单调递减队列:新元素若大于队尾元素,则弹出队尾元素,直到满足单调性 while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) { deque.pollLast(); } @@ -894,3 +894,4 @@ public: + From b764a124b71350bd8d008cddaddae3136f84c5f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 20 Jan 2025 22:59:33 +0800 Subject: [PATCH 1486/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?= =?UTF-8?q?.md=E7=9A=84Java=E7=89=88=E6=9C=AC=E6=B3=A8=E9=87=8A=E7=AC=94?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...37\344\270\200\350\277\255\344\273\243\346\263\225.md" | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 037cf1109b..a6d4e3ffc3 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -238,7 +238,7 @@ class Solution { while (!st.empty()) { TreeNode node = st.peek(); if (node != null) { - st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 + st.pop(); // 将该节点弹出,避免重复操作,下面再将右左中节点添加到栈中(前序遍历-中左右,入栈顺序右左中) if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) st.push(node); // 添加中节点 @@ -266,11 +266,10 @@ public List inorderTraversal(TreeNode root) { while (!st.empty()) { TreeNode node = st.peek(); if (node != null) { - st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 + st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中(中序遍历-左中右,入栈顺序右中左) if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) st.push(node); // 添加中节点 st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 - if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) } else { // 只有遇到空节点的时候,才将下一个节点放进结果集 st.pop(); // 将空节点弹出 @@ -294,7 +293,7 @@ class Solution { while (!st.empty()) { TreeNode node = st.peek(); if (node != null) { - st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 + st.pop(); // 将该节点弹出,避免重复操作,下面再将中右左节点添加到栈中(后序遍历-左右中,入栈顺序中右左) st.push(node); // 添加中节点 st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) @@ -975,3 +974,4 @@ public IList PostorderTraversal(TreeNode root) + From da742feaa3e324c9489bedaf867004cae583cc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Thu, 23 Jan 2025 11:47:27 +0800 Subject: [PATCH 1487/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C.md=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index b97013e67a..141967f593 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -309,25 +309,25 @@ public: 0112.路径总和 ```java -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if (root == null) { return false; } - targetsum -= root.val; + targetSum -= root.val; // 叶子结点 if (root.left == null && root.right == null) { - return targetsum == 0; + return targetSum == 0; } if (root.left != null) { - boolean left = haspathsum(root.left, targetsum); - if (left) { // 已经找到 + boolean left = hasPathSum(root.left, targetSum); + if (left) { // 已经找到,提前返回 return true; } } if (root.right != null) { - boolean right = haspathsum(root.right, targetsum); - if (right) { // 已经找到 + boolean right = hasPathSum(root.right, targetSum); + if (right) { // 已经找到,提前返回 return true; } } @@ -336,16 +336,16 @@ class solution { } // lc112 简洁方法 -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if (root == null) return false; // 为空退出 // 叶子节点判断是否符合 - if (root.left == null && root.right == null) return root.val == targetsum; + if (root.left == null && root.right == null) return root.val == targetSum; // 求两侧分支的路径和 - return haspathsum(root.left, targetsum - root.val) || haspathsum(root.right, targetsum - root.val); + return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } } ``` @@ -353,22 +353,22 @@ class solution { 迭代 ```java -class solution { - public boolean haspathsum(treenode root, int targetsum) { +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { if(root == null) return false; - stack stack1 = new stack<>(); - stack stack2 = new stack<>(); + Stack stack1 = new Stack<>(); + Stack stack2 = new Stack<>(); stack1.push(root); stack2.push(root.val); - while(!stack1.isempty()) { + while(!stack1.isEmpty()) { int size = stack1.size(); for(int i = 0; i < size; i++) { - treenode node = stack1.pop(); + TreeNode node = stack1.pop(); int sum = stack2.pop(); // 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true - if(node.left == null && node.right == null && sum == targetsum) { + if(node.left == null && node.right == null && sum == targetSum) { return true; } // 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来 @@ -387,8 +387,9 @@ class solution { } } ``` -```Java 統一迭代法 - public boolean hasPathSum(TreeNode root, int targetSum) { +```Java +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { Stack treeNodeStack = new Stack<>(); Stack sumStack = new Stack<>(); @@ -422,38 +423,39 @@ class solution { } return false; } +} ``` 0113.路径总和-ii ```java -class solution { - public List> pathsum(TreeNode root, int targetsum) { +class Solution { + public List> pathSum(TreeNode root, int targetSum) { List> res = new ArrayList<>(); if (root == null) return res; // 非空判断 List path = new LinkedList<>(); - preorderdfs(root, targetsum, res, path); + preOrderDfs(root, targetSum, res, path); return res; } - public void preorderdfs(TreeNode root, int targetsum, List> res, List path) { + public void preOrderDfs(TreeNode root, int targetSum, List> res, List path) { path.add(root.val); // 遇到了叶子节点 if (root.left == null && root.right == null) { // 找到了和为 targetsum 的路径 - if (targetsum - root.val == 0) { + if (targetSum - root.val == 0) { res.add(new ArrayList<>(path)); } return; // 如果和不为 targetsum,返回 } if (root.left != null) { - preorderdfs(root.left, targetsum - root.val, res, path); + preOrderDfs(root.left, targetSum - root.val, res, path); path.remove(path.size() - 1); // 回溯 } if (root.right != null) { - preorderdfs(root.right, targetsum - root.val, res, path); + preOrderDfs(root.right, targetSum - root.val, res, path); path.remove(path.size() - 1); // 回溯 } } @@ -1626,3 +1628,4 @@ public class Solution { + From 163c3f33d8d24735230b25ddb697d726f0d5c242 Mon Sep 17 00:00:00 2001 From: Anqi Li <103280095+iqna126@users.noreply.github.com> Date: Thu, 23 Jan 2025 08:16:44 -0800 Subject: [PATCH 1488/1533] =?UTF-8?q?0054.=E6=9B=BF=E6=8D=A2=E6=95=B0?= =?UTF-8?q?=E5=AD=97.md=20=E5=9C=A8=E8=AE=B2=E8=A7=A3=E4=B8=AD=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E4=BA=86python=20string=E4=B9=9F=E4=B8=8D=E5=8F=AF?= =?UTF-8?q?=E5=8F=98=E7=9A=84=E8=A7=A3=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index e4b5c43fef..f788d65ba7 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -21,7 +21,7 @@ ## 思路 -如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java刷题的录友,一定要使用辅助空间,因为Java里的string不能修改) +如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java和Python刷题的录友,一定要使用辅助空间,因为Java和Python里的string不能修改) 首先扩充数组到每个数字字符替换成 "number" 之后的大小。 From 27718a4dfca99b25ceda3ee2946130c29261be71 Mon Sep 17 00:00:00 2001 From: asnpro <920569392@qq.com> Date: Fri, 24 Jan 2025 22:51:46 +0800 Subject: [PATCH 1489/1533] =?UTF-8?q?docs:=20=E4=B8=BA=200494.=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=92=8C.md=20=E5=AE=8C=E5=96=84=20JavaDoc=20?= =?UTF-8?q?=E6=B3=A8=E9=87=8A,=20=E6=B7=BB=E5=8A=A0=E5=8A=A8=E8=A7=84?= =?UTF-8?q?=E4=BA=94=E9=83=A8=E6=9B=B2=E6=B3=A8=E9=87=8A,=20=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?,=20=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D=E4=BB=A5=E5=A2=9E=E5=BC=BA=E4=BB=A3=E7=A0=81=E8=AF=AD?= =?UTF-8?q?=E4=B9=89=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4.\347\233\256\346\240\207\345\222\214.md" | 61 +++++++++++++++---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 4a1fc6ab9a..c38ba7e43c 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -825,30 +825,69 @@ func abs(x int) int { ### JavaScript ```javascript +/** + * 题目来源: {@link https://leetcode.cn/problems/target-sum/} + * + * 题解来源: {@link https://programmercarl.com/0494.%E7%9B%AE%E6%A0%87%E5%92%8C.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE} + * + * 时间复杂度: O(n * C), C 为数组元素总和与目标值之和的一半 + * + * 空间复杂度: O(C) + * + * @param { number[] } nums + * @param { number } target + * @return { number } + */ const findTargetSumWays = (nums, target) => { - - const sum = nums.reduce((a, b) => a+b); + // 原题目可转化为: + // + // 将所有元素划分为 2 个集合, + // 一个集合中包含所有要添加 "+" 号的元素, 一个集合中包含所有要添加 "-" 号的元素 + // + // 设两个集合的元素和分别为 positive 和 negative, 所有元素总和为 sum, 那么有如下等式: + // positive + negative = sum (1) + // positive - negative = target (2) + // (1) 与 (2) 联立可得: positive = (sum + target) / 2, + // 所以如果能从原数组中取出若干个元素形成 1 个元素总和为 (sum + target) / 2 的集合, + // 就算得到了 1 种满足题意的组合方法 + // + // 因此, 所求变为: 有多少种取法, 可使得容量为 (sum + target) / 2 的背包被装满? + + const sum = nums.reduce((a, b) => a + b); - if(Math.abs(target) > sum) { + if (Math.abs(target) > sum) { return 0; } - if((target + sum) % 2) { + if ((target + sum) % 2) { return 0; } - const halfSum = (target + sum) / 2; - - let dp = new Array(halfSum+1).fill(0); + const bagWeight = (target + sum) / 2; + + // 1. dp 数组的含义 + // dp[j]: 装满容量为 j 的背包, 有 dp[j] 种方法 + let dp = new Array(bagWeight + 1).fill(0); + + // 2. 递推公式 + // dp[j] = Σ(dp[j - nums[j]]), (j ∈ [0, j] 且 j >= nums[j]) + // 因为 dp[j - nums[j]] 表示: 装满容量为 j - nums[j] 背包有 dp[j - nums[j]] 种方法 + // 而容量为 j - nums[j] 的背包只需要再将 nums[j] 放入背包就能使得背包容量达到 j + // 因此, 让背包容量达到 j 有 Σ(dp[j - nums[j]]) 种方法 + + // 3. dp 数组如何初始化 + // dp[0] = 1, dp[1 ~ bagWeight] = 0 dp[0] = 1; - - for(let i = 0; i < nums.length; i++) { - for(let j = halfSum; j >= nums[i]; j--) { + + // 4. 遍历顺序 + // 先物品后背包, 物品从前往后遍历, 背包容量从后往前遍历 + for (let i = 0; i < nums.length; i++) { + for (let j = bagWeight; j >= nums[i]; j--) { dp[j] += dp[j - nums[i]]; } } - return dp[halfSum]; + return dp[bagWeight]; }; ``` From e6698cbac457714427f4a9cba6ea2a3ea9eed94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Sat, 25 Jan 2025 11:52:09 +0800 Subject: [PATCH 1490/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE.md=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=20=E8=BF=9B=E8=A1=8C=E4=BA=86=E4=BB=A3=E7=A0=81=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E5=B9=B6=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\347\273\235\345\257\271\345\267\256.md" | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 2533a618ed..b6d08dbeaa 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -153,23 +153,27 @@ public: 递归 ```java class Solution { - TreeNode pre;// 记录上一个遍历的结点 + TreeNode pre; // 记录上一个遍历的结点 int result = Integer.MAX_VALUE; + public int getMinimumDifference(TreeNode root) { - if(root==null)return 0; - traversal(root); - return result; + if (root == null) + return 0; + traversal(root); + return result; } - public void traversal(TreeNode root){ - if(root==null)return; - //左 + + public void traversal(TreeNode root) { + if (root == null) + return; + // 左 traversal(root.left); - //中 - if(pre!=null){ - result = Math.min(result,root.val-pre.val); + // 中 + if (pre != null) { + result = Math.min(result, root.val - pre.val); } pre = root; - //右 + // 右 traversal(root.right); } } @@ -182,22 +186,27 @@ class Solution { TreeNode pre = null; int result = Integer.MAX_VALUE; - if(root != null) + if (root != null) stack.add(root); - while(!stack.isEmpty()){ + + // 中序遍历(左中右),由于栈先入后出,反序(右中左) + while (!stack.isEmpty()) { TreeNode curr = stack.peek(); - if(curr != null){ + if (curr != null) { stack.pop(); - if(curr.right != null) + // 右 + if (curr.right != null) stack.add(curr.right); + // 中(先用null标记) stack.add(curr); stack.add(null); - if(curr.left != null) + // 左 + if (curr.left != null) stack.add(curr.left); - }else{ + } else { // 中(遇到null再处理) stack.pop(); TreeNode temp = stack.pop(); - if(pre != null) + if (pre != null) result = Math.min(result, temp.val - pre.val); pre = temp; } @@ -674,3 +683,4 @@ public class Solution + From 7f5ce0ac41f35e97d02ecfdf85c940f91c32d026 Mon Sep 17 00:00:00 2001 From: lewiseng <65698183+lewiseng@users.noreply.github.com> Date: Sat, 25 Jan 2025 15:56:17 -0500 Subject: [PATCH 1491/1533] =?UTF-8?q?Update=200216.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index 3d7f2d0c03..04805c93f5 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -367,7 +367,7 @@ class Solution: def backtracking(self, targetSum, k, currentSum, startIndex, path, result): if currentSum > targetSum: # 剪枝操作 - return # 如果path的长度等于k但currentSum不等于targetSum,则直接返回 + return # 如果currentSum已经超过targetSum,则直接返回 if len(path) == k: if currentSum == targetSum: result.append(path[:]) From c2e95f6c25b2c3f9ff61dc0891cc08360b676bb1 Mon Sep 17 00:00:00 2001 From: dam <1782067308@qq.com> Date: Sun, 26 Jan 2025 12:08:15 +0800 Subject: [PATCH 1492/1533] =?UTF-8?q?108=E5=86=97=E4=BD=99=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=20java=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\344\275\231\350\277\236\346\216\245.md" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index efbbb6d203..df3dd4deed 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -176,6 +176,81 @@ int main() { ### Java +```java +import java.util.Scanner; + +public class Main { + private static int[] father; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int pointNum = scanner.nextInt(); + father = new int[pointNum + 1]; + init(); + for (int i = 0; i < pointNum; i++) { + join(scanner.nextInt(), scanner.nextInt()); + } + } + + /** + * 并查集初始化 + */ + private static void init() { + for (int i = 1; i < father.length; i++) { + // 让每个元素指向自己 + father[i] = i; + } + } + + /** + * 并查集寻根 + * + * @param u + * @return + */ + private static int find(int u) { + // 判断 u 是否等于自己,如果是的话,直接返回自己 + // 如果不等于自己,就寻找根,寻找的时候,反复进行路径压缩 + return u == father[u] ? u : (father[u] = find(father[u])); + } + + /** + * 判断 u 和 v 是否同根 + * + * @param u + * @param v + * @return + */ + private static boolean isSame(int u, int v) { + return find(u) == find(v); + } + + /** + * 添加 边 到并查集,v 指向 u + * + * @param u + * @param v + */ + private static void join(int u, int v) { + // --if-- 如果两个点已经同根,说明他们的信息已经存储到并查集中了,直接返回即可 + // 寻找u的根 + int uRoot = find(u); + // 寻找v的根 + int vRoot = find(v); + if (uRoot == vRoot) { + // --if-- 如果u,v的根相同,说明两者已经连接了,直接输出 + System.out.println(u + " " + v); + return; + } + // --if-- 将信息添加到并查集 + father[vRoot] = uRoot; + } + +} +``` + + + ### Python ```python From 8bf4e2cdf1f69ab9a8f733e0c5693236335cff5c Mon Sep 17 00:00:00 2001 From: 1ltwo Date: Mon, 27 Jan 2025 16:08:27 +0800 Subject: [PATCH 1493/1533] =?UTF-8?q?738go=E7=89=88=E6=9C=AC=E7=AD=94?= =?UTF-8?q?=E6=A1=88=E6=9B=B4=E6=AD=A3=EF=BC=88=E5=8E=9F=E7=AD=94=E6=A1=88?= =?UTF-8?q?=E5=8A=9B=E6=89=A3=E6=97=A0=E6=B3=95=E9=80=9A=E8=BF=87=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\347\232\204\346\225\260\345\255\227.md" | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index ff438e981c..f2cfee04c9 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -273,22 +273,20 @@ class Solution: ### Go ```go func monotoneIncreasingDigits(n int) int { - s := strconv.Itoa(N)//将数字转为字符串,方便使用下标 - ss := []byte(s)//将字符串转为byte数组,方便更改。 - n := len(ss) - if n <= 1 { - return n - } - for i := n-1; i > 0; i-- { - if ss[i-1] > ss[i] { //前一个大于后一位,前一位减1,后面的全部置为9 - ss[i-1] -= 1 - for j := i; j < n; j++ { //后面的全部置为9 - ss[j] = '9' - } - } - } - res, _ := strconv.Atoi(string(ss)) - return res + s := strconv.Itoa(n) + // 从左到右遍历字符串,找到第一个不满足单调递增的位置 + for i := len(s) - 2; i >= 0; i-- { + if s[i] > s[i+1] { + // 将该位置的数字减1 + s = s[:i] + string(s[i]-1) + s[i+1:] + // 将该位置之后的所有数字置为9 + for j := i + 1; j < len(s); j++ { + s = s[:j] + "9" + s[j+1:] + } + } + } + result, _ := strconv.Atoi(s) + return result } ``` @@ -447,3 +445,4 @@ public class Solution + From 333099a1268544306e303fcb779ffd801c0d0c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 3 Feb 2025 20:13:41 +0800 Subject: [PATCH 1494/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E6=80=BB=E7=BB=93=E7=AF=87.md=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=BA=A7=E5=88=AB=E7=9A=84=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\211\346\240\221\346\200\273\347\273\223\347\257\207.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" index 8db40d6560..4794233ab5 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -92,10 +92,9 @@ * 递归:中序,双指针操作 * 迭代:模拟中序,逻辑相同 * [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html) - + * 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合 - * [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) - +* [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) * 递归:中序,双指针操作累加 * 迭代:模拟中序,逻辑相同 @@ -163,3 +162,4 @@ + From 3b2373d35f1f551826b473d019fa1f1e58966f79 Mon Sep 17 00:00:00 2001 From: buptz2b <155848478+buptz2b@users.noreply.github.com> Date: Tue, 4 Feb 2025 22:00:18 +0800 Subject: [PATCH 1495/1533] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\270\244\346\225\260\344\271\213\345\222\214.md" | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index e982ae129b..3c92e4dfeb 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -287,17 +287,16 @@ func twoSum(nums []int, target int) []int { ``` ```go -// 使用map方式解题,降低时间复杂度 func twoSum(nums []int, target int) []int { m := make(map[int]int) - for index, val := range nums { - if preIndex, ok := m[target-val]; ok { - return []int{preIndex, index} - } else { - m[val] = index + for i, num := range nums { + complement := target - num + if index, found := m[complement]; found { + return []int{index, i} } + m[num] = i } - return []int{} + return nil // 返回空数组 nil 代替空切片 } ``` From 4f07f351a870de2b4fb3ba79e15ebbd090669c46 Mon Sep 17 00:00:00 2001 From: "e-peirong.li" Date: Mon, 10 Feb 2025 15:22:19 +0800 Subject: [PATCH 1496/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=20=E6=A0=88?= =?UTF-8?q?=E4=B8=8E=E9=98=9F=E5=88=97=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?= =?UTF-8?q?.md=20=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index 21c61a4c8b..3ceb8690e4 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -65,7 +65,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 **我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。** -deque是一个双向队列,只要封住一段,只开通另一端就可以实现栈的逻辑了。 +deque是一个双向队列,只要封住一端,只开通另一端就可以实现栈的逻辑了。 **SGI STL中 队列底层实现缺省情况下一样使用deque实现的。** From 3176a0c828f162d7776e3e2204129780e547f421 Mon Sep 17 00:00:00 2001 From: "e-peirong.li" Date: Mon, 10 Feb 2025 15:45:42 +0800 Subject: [PATCH 1497/1533] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=20README.md?= =?UTF-8?q?=20=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b1db51c903..b64bcea684 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ 按照先面的排列顺序,从数组开始刷起就可以了,顺序都安排好了,按顺序刷就好。 -在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,最这个专题的归纳总结。 +在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,是这个专题的归纳总结。 如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 From 2c218d7c5a6709c25009bf2237c1c518589cf9c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=BF=94?= Date: Thu, 13 Feb 2025 10:48:11 +0800 Subject: [PATCH 1498/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B90209.=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?= =?UTF-8?q?.md=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index c6d89976d0..e163623afd 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -68,7 +68,7 @@ public: 接下来就开始介绍数组操作中另一个重要的方法:**滑动窗口**。 -所谓滑动窗口,**就是不断的调节子序列的起始位置和终止位置,从而得出我们要想的结果**。 +所谓滑动窗口,**就是不断的调节子序列的起始位置和终止位置,从而得出我们想要的结果**。 在暴力解法中,是一个for循环滑动窗口的起始位置,一个for循环为滑动窗口的终止位置,用两个for循环 完成了一个不断搜索区间的过程。 From add956a0e1581d1470bfcc18e01c2a175d7d358d Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sun, 16 Feb 2025 14:36:05 +0800 Subject: [PATCH 1499/1533] Update --- ...11\345\222\214\345\255\220\351\233\206.md" | 12 +++++----- ...00\345\244\247\351\235\242\347\247\257.md" | 4 ++-- ...57\350\276\276\350\267\257\345\276\204.md" | 2 +- ...60\351\207\217\345\271\277\346\220\234.md" | 2 +- ...00\345\244\247\351\235\242\347\247\257.md" | 2 +- ...04\346\200\273\351\235\242\347\247\257.md" | 15 +++++------- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 23 +++++++++++++++++++ 8 files changed, 41 insertions(+), 21 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 55ed7ad2ff..902c022ab3 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -60,7 +60,7 @@ * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -### 01背包问题 +## 01背包问题 01背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 @@ -92,7 +92,7 @@ 动规五部曲分析如下: -1. 确定dp数组以及下标的含义 +### 1. 确定dp数组以及下标的含义 01背包中,dp[j] 表示: 容量(所能装的重量)为j的背包,所背的物品价值最大可以为dp[j]。 @@ -104,7 +104,7 @@ 而dp[6] 就可以等于6了,放进1 和 5,那么dp[6] == 6,说明背包装满了。 -2. 确定递推公式 +### 2. 确定递推公式 01背包的递推公式为:dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); @@ -113,7 +113,7 @@ 所以递推公式:dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]); -3. dp数组如何初始化 +### 3. dp数组如何初始化 在01背包,一维dp如何初始化,已经讲过, @@ -133,7 +133,7 @@ vector dp(10001, 0); ``` -4. 确定遍历顺序 +### 4. 确定遍历顺序 在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历! @@ -148,7 +148,7 @@ for(int i = 0; i < nums.size(); i++) { } ``` -5. 举例推导dp数组 +### 5. 举例推导dp数组 dp[j]的数值一定是小于等于j的。 diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 11b638d4d6..ca70420687 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -44,7 +44,7 @@ 这里其实涉及到dfs的两种写法。 -写法一,dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 +写法一,dfs处理当前节点的相邻节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 ```CPP // 版本一 @@ -87,7 +87,7 @@ public: }; ``` -写法二,dfs处理当前节点,即即在主函数遇到岛屿就计数为0,dfs处理接下来的全部陆地 +写法二,dfs处理当前节点,即在主函数遇到岛屿就计数为0,dfs处理接下来的全部陆地 dfs ```CPP diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 7f56f9f4dd..4df53b448f 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -7,7 +7,7 @@ 【题目描述】 -给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个函数,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。 +给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个程序,找出并返回所有从节点 1 到节点 n 的路径。每条路径应以节点编号的列表形式表示。 【输入描述】 diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index d654e23604..f8c36a00a8 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -72,7 +72,7 @@ 如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250124094043.png) 超时写法 (从队列中取出节点再标记,注意代码注释的地方) diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 4f2f9d6732..170c0917aa 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -63,7 +63,7 @@ 这里其实涉及到dfs的两种写法。 -写法一,dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 +写法一,dfs处理当前节点的相邻节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地 ```CPP // 版本一 diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 123e36cec5..43ac8ec96d 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -72,10 +72,8 @@ #include using namespace std; int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // 保存四个方向 -int count; // 统计符合题目要求的陆地空格数量 void dfs(vector>& grid, int x, int y) { grid[x][y] = 0; - count++; for (int i = 0; i < 4; i++) { // 向四个方向遍历 int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; @@ -109,16 +107,17 @@ int main() { if (grid[0][j] == 1) dfs(grid, 0, j); if (grid[n - 1][j] == 1) dfs(grid, n - 1, j); } - count = 0; + int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { - if (grid[i][j] == 1) dfs(grid, i, j); + if (grid[i][j] == 1) count++; } } cout << count << endl; } ``` + 采用广度优先搜索的代码如下: ```CPP @@ -126,13 +125,11 @@ int main() { #include #include using namespace std; -int count = 0; int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 void bfs(vector>& grid, int x, int y) { queue> que; que.push({x, y}); grid[x][y] = 0; // 只要加入队列,立刻标记 - count++; while(!que.empty()) { pair cur = que.front(); que.pop(); int curx = cur.first; @@ -143,7 +140,6 @@ void bfs(vector>& grid, int x, int y) { if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 if (grid[nextx][nexty] == 1) { que.push({nextx, nexty}); - count++; grid[nextx][nexty] = 0; // 只要加入队列立刻标记 } } @@ -169,15 +165,16 @@ int main() { if (grid[0][j] == 1) bfs(grid, 0, j); if (grid[n - 1][j] == 1) bfs(grid, n - 1, j); } - count = 0; + int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { - if (grid[i][j] == 1) bfs(grid, i, j); + if (grid[i][j] == 1) count++; } } cout << count << endl; } + ``` diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index efe833a7e1..ce3dbbdbe7 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -162,7 +162,7 @@ if (终止条件) { 终止添加不仅是结束本层递归,同时也是我们收获结果的时候。 -另外,其实很多dfs写法,没有写终止条件,其实终止条件写在了, 下面dfs递归的逻辑里了,也就是不符合条件,直接不会向下递归。这里如果大家不理解的话,没关系,后面会有具体题目来讲解。 +另外,其实很多dfs写法,没有写终止条件,其实终止条件写在了, 隐藏在下面dfs递归的逻辑里了,也就是不符合条件,直接不会向下递归。这里如果大家不理解的话,没关系,后面会有具体题目来讲解。 3. 处理目前搜索节点出发的路径 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" index 50c3615733..84f693a00b 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -128,6 +128,29 @@ 主要是 朴素存储、邻接表和邻接矩阵。 +关于朴素存储,这是我自创的名字,因为这种存储方式,就是将所有边存下来。 + +例如图: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511112951.png) + +图中有8条边,我们就定义 8 * 2的数组,即有n条边就申请n * 2,这么大的数组: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250110114348.png) + +数组第一行:6 7,就表示节点6 指向 节点7,以此类推。 + +当然可以不用数组,用map,或者用 类 到可以表示出 这种边的关系。 + +这种表示方式的好处就是直观,把节点与节点之间关系很容易展现出来。 + +但如果我们想知道 节点1 和 节点6 是否相连,我们就需要把存储空间都枚举一遍才行。 + +这是明显的缺点,同时,我们在深搜和广搜的时候,都不会使用这种存储方式。 + +因为 搜索中,需要知道 节点与其他节点的链接情况,而这种朴素存储,都需要全部枚举才知道链接情况。 + +在图论章节的后面文章讲解中,我会举例说明的。大家先有个印象。 ### 邻接矩阵 From da821733ff03cefe9a2bc150e2f9f1821793e5f8 Mon Sep 17 00:00:00 2001 From: HuangLM03 <2306725926@qq.com> Date: Sun, 16 Feb 2025 14:48:02 +0800 Subject: [PATCH 1500/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0kamacoder/0058=E5=8C=BA=E9=97=B4=E5=92=8C=E7=9A=84Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...8.\345\214\272\351\227\264\345\222\214.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index 23e7189a15..6f400ee433 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -357,3 +357,54 @@ int main(int argc, char *argv[]) ``` +### Go + +```go +package main + +import ( + "fmt" + "bufio" + "strconv" + "os" +) + +func main() { + // bufio中读取数据的接口,因为数据卡的比较严,导致使用fmt.Scan会超时 + scanner := bufio.NewScanner(os.Stdin) + + // 获取数组大小 + scanner.Scan() + n, _ := strconv.Atoi(scanner.Text()) + + // 获取数组元素的同时计算前缀和,一般建议切片开大一点防止各种越界问题 + arr := make([]int, n + 1) + for i := 0; i < n; i++ { + scanner.Scan() + arr[i], _ = strconv.Atoi(scanner.Text()) + if i != 0 { + arr[i] += arr[i - 1] + } + } + + /* + 区间[l, r]的和可以使用区间[0, r]和[0, l - 1]相减得到, + 在代码中即为arr[r]-arr[l-1]。这里需要注意l-1是否越界 + */ + for { + var l, r int + scanner.Scan() + _, err := fmt.Sscanf(scanner.Text(), "%d %d", &l, &r) + if err != nil { + return + } + + if l > 0 { + fmt.Println(arr[r] - arr[l - 1]) + } else { + fmt.Println(arr[r]) + } + } +} +``` + From f802fc7e999210c70bba081ba79f3be21b4b3a81 Mon Sep 17 00:00:00 2001 From: HuangLM03 <2306725926@qq.com> Date: Mon, 17 Feb 2025 02:13:03 +0800 Subject: [PATCH 1501/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0kamacoder/0047.?= =?UTF-8?q?=E5=8F=82=E4=BC=9Adijkstra=E6=9C=B4=E7=B4=A0=E7=9A=84Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...74\232dijkstra\346\234\264\347\264\240.md" | 120 +++++++++++++++++- 1 file changed, 114 insertions(+), 6 deletions(-) diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index e71e9d5374..5fd57965ad 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -114,7 +114,7 @@ dijkstra 算法 同样是贪心的思路,不断寻找距离 源点最近的没 ### 模拟过程 ------------ +----------- 0、初始化 @@ -130,7 +130,7 @@ minDist数组数值初始化为int最大值。 此时所有节点都没有被访问过,所以 visited数组都为0 ---------------- +--------------- 以下为dijkstra 三部曲 @@ -555,13 +555,13 @@ int main() { 那我们来看dijkstra 求解的路径是什么样的,继续dijkstra 三部曲来模拟 :(dijkstra模拟过程上面已经详细讲过,以下只模拟重要过程,例如如何初始化就省略讲解了) ------------ +----------- 初始化: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104801.png) ---------------- +--------------- 1、选源点到哪个节点近且该节点未被访问过 @@ -632,7 +632,7 @@ int main() { 节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 ------------- +------------ 1、选源点到哪个节点近且该节点未被访问过 @@ -646,7 +646,7 @@ int main() { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110711.png) --------------- +-------------- 至此dijkstra的模拟过程就结束了,根据最后的minDist数组,我们求 节点1 到 节点5 的最短路径的权值总和为 3,路径: 节点1 -> 节点3 -> 节点4 -> 节点5 @@ -865,6 +865,114 @@ if __name__ == "__main__": ### Go +```go +package main + +import( + "fmt" + "os" + "bufio" + "strconv" + "strings" + "math" +) + +func main() { + // 创建Reader从标准输入中读取数据 + reader := bufio.NewReader(os.Stdin) + + // 以字符串的形式读取一行 + line, _ := reader.ReadString('\n') + // 去掉字符串前后可能存在的空格 + line = strings.TrimSpace(line) + // 以空格作为分隔符分割字符串,得到数字的字符串形式 + params := strings.Split(line, " ") + // 字符串转化为数字,得到n和m,其中n为汽车站数,m为公路数 + n, _ := strconv.Atoi(params[0]) + m, _ := strconv.Atoi(params[1]) + + // 存储从源点到每个节点的最短距离 + minDist := initSliceInt(math.MaxInt32, n + 1) + minDist[1] = 0 + // 记录顶点是否被访问过 + visited := initSliceBool(false, n + 1) + + // 存储每个车站之间的距离 + grid := make([][]int, n + 1) + for i := 1; i <= n; i++ { + grid[i] = initSliceInt(math.MaxInt32, n + 1) + grid[i][i] = 0 + } + for i := 1; i <= m; i++ { + line, _ = reader.ReadString('\n') + line = strings.TrimSpace(line) + params = strings.Split(line, " ") + a, _ := strconv.Atoi(params[0]) + b, _ := strconv.Atoi(params[1]) + c, _ := strconv.Atoi(params[2]) + + grid[a][b] = c + } + + // Dijkstra算法 + for i := 1; i <= n; i++ { + cur := -1 + // 1、选距离源点最近且未访问过的节点 + for j := 1; j <= n; j++ { + if visited[j] == false && (cur == -1 || minDist[cur] > minDist[j]) { + cur = j; + } + } + + // 2、标记该节点已被访问 + visited[cur] = true + + /* + 3、更新非访问节点到源点的距离(即更新minDist数组)。实际更新时无需判断 + 节点是否被访问过,因为1.的限制,即使更新被访问过的点也没有任何影响。 + */ + for j := 1; j <= n; j++ { + minDist[j] = min(minDist[j], minDist[cur] + grid[cur][j]) + } + + } + + if minDist[n] == math.MaxInt32 { + // 不能到达终点 + fmt.Print(-1) + } else { + // 到达终点最短路径 + fmt.Print(minDist[n]) + } +} + +// 创建int类型的切片 +func initSliceInt(value int, count int) []int { + result := make([]int, count) + for i := range result { + result[i] = value + } + return result +} + +// 创建bool类型的切片 +func initSliceBool(value bool, count int) []bool { + result := make([]bool, count) + for i := range result { + result[i] = value + } + return result +} + +// 比较两个int类型的大小,返回较小的一个 +func min(a, b int) int { + if a > b { + return b + } + return a +} +``` + ### Rust ### JavaScript From 52cbec304ec4c4530551612f4a26b7fb2a5108cd Mon Sep 17 00:00:00 2001 From: Jerry Zeng <87971517+Zzz212zzZ@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:31:03 -0500 Subject: [PATCH 1502/1533] =?UTF-8?q?update:=20707=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E9=A2=98=E8=A7=A3=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?Javascript=E5=8F=8C=E5=A4=B4=E9=93=BE=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\351\223\276\350\241\250.md" | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index ed1726d942..5c72b05a29 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -1191,6 +1191,160 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { */ ``` +```js +/** + 定义双头节点的结构:同时包含前指针`prev`和后指针next` +*/ +class Node { + constructor(val, prev, next) { + this.val = val + this.prev = prev + this.next = next + } +} + +/** + 双链表:维护 `head` 和 `tail` 两个哨兵节点,这样可以简化对于中间节点的操作 + 并且维护 `size`,使得能够以O(1)时间判断操作是否合法 +*/ +var MyLinkedList = function () { + this.tail = new Node(-1) + this.head = new Node(-1) + this.tail.prev = this.head + this.head.next = this.tail + this.size = 0 +}; + +/** + * 获取在index处节点的值 + * + * @param {number} index + * @return {number} + * + * 时间复杂度: O(n) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.get = function (index) { + // 当索引超出范围时,返回-1 + if (index > this.size) { + return -1 + } + + let cur = this.head + for (let i = 0; i <= index; i++) { + cur = cur.next + } + + return cur.val +}; + +/** + * 在链表头部添加一个新节点 + * + * @param {number} val + * @return {void} + * + * 时间复杂度: O(1) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.addAtHead = function (val) { + /** + head <-> [newNode] <-> originNode + */ + this.size++ + const originNode = this.head.next + // 创建新节点,并建立连接 + const newNode = new Node(val, this.head, originNode) + + // 取消原前后结点的连接 + this.head.next = newNode + originNode.prev = newNode +}; + +/** + * 在链表尾部添加一个新节点 + * + * @param {number} val + * @return {void} + * + * 时间复杂度: O(1) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.addAtTail = function (val) { + /** + originNode <-> [newNode] <-> tail + */ + this.size++ + const originNode = this.tail.prev + + // 创建新节点,并建立连接 + const newNode = new Node(val, originNode, this.tail) + + // 取消原前后结点的连接 + this.tail.prev = newNode + originNode.next = newNode +}; + +/** + * 在指定索引位置前添加一个新节点 + * + * @param {number} index + * @param {number} val + * @return {void} + * + * 时间复杂度: O(n) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.addAtIndex = function (index, val) { + // 当索引超出范围时,直接返回 + if (index > this.size) { + return + } + this.size++ + + let cur = this.head + for (let i = 0; i < index; i++) { + cur = cur.next + } + + const new_next = cur.next + + // 创建新节点,并建立连接 + const node = new Node(val, cur, new_next) + + // 取消原前后结点的连接 + cur.next = node + new_next.prev = node +}; + +/** + * 删除指定索引位置的节点 + * + * @param {number} index + * @return {void} + * + * 时间复杂度: O(n) + * 空间复杂度: O(1) + */ +MyLinkedList.prototype.deleteAtIndex = function (index) { + // 当索引超出范围时,直接返回 + if (index >= this.size) { + return + } + + this.size-- + let cur = this.head + for (let i = 0; i < index; i++) { + cur = cur.next + } + + const new_next = cur.next.next + // 取消原前后结点的连接 + new_next.prev = cur + cur.next = new_next +}; +``` + ### TypeScript: ```TypeScript From a01972daad0997d57c48c28b67ff44ebf6028227 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 3 Mar 2025 20:27:16 +0800 Subject: [PATCH 1503/1533] Update --- .../0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index a7e28df52f..5e211cd09c 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -61,7 +61,7 @@ 思路依然是从地图周边出发,将周边空格相邻的陆地都做上标记,然后在遍历一遍地图,遇到 陆地 且没做过标记的,那么都是地图中间的 陆地 ,全部改成水域就行。 -有的录友可能想,我在定义一个 visited 二维数组,单独标记周边的陆地,然后遍历地图的时候同时对 数组board 和 数组visited 进行判断,决定 陆地是否变成水域。 +有的录友可能想,我再定义一个 visited 二维数组,单独标记周边的陆地,然后遍历地图的时候同时对 地图数组 和 数组visited 进行判断,决定 陆地是否变成水域。 这样做其实就有点麻烦了,不用额外定义空间了,标记周边的陆地,可以直接改陆地为其他特殊值作为标记。 From f0767204bb09f1e95642af36a624a558a8321e5c Mon Sep 17 00:00:00 2001 From: RelishCoding <122661763+RelishCoding@users.noreply.github.com> Date: Tue, 4 Mar 2025 16:58:38 +0800 Subject: [PATCH 1504/1533] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.为Java解法中添加迭代法的boolean标记法 2.为原有的Java解法中添加空指针标记法说明,并调整代码格式 --- ...00\350\277\255\344\273\243\346\263\225.md" | 199 +++++++++++++++--- 1 file changed, 171 insertions(+), 28 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index a6d4e3ffc3..25756a9146 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -227,7 +227,8 @@ public: ## 其他语言版本 ### Java: -迭代法前序遍历代码如下: + +迭代法前序遍历(空指针标记法)代码如下: ```java class Solution { @@ -239,11 +240,10 @@ class Solution { TreeNode node = st.peek(); if (node != null) { st.pop(); // 将该节点弹出,避免重复操作,下面再将右左中节点添加到栈中(前序遍历-中左右,入栈顺序右左中) - if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) - if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) - st.push(node); // 添加中节点 + if (node.right != null) st.push(node.right); // 添加右节点(空节点不入栈) + if (node.left != null) st.push(node.left); // 添加左节点(空节点不入栈) + st.push(node); // 添加中节点 st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 - } else { // 只有遇到空节点的时候,才将下一个节点放进结果集 st.pop(); // 将空节点弹出 node = st.peek(); // 重新取出栈中元素 @@ -256,34 +256,34 @@ class Solution { } ``` -迭代法中序遍历代码如下: +迭代法中序遍历(空指针标记法)代码如下: ```java class Solution { -public List inorderTraversal(TreeNode root) { + public List inorderTraversal(TreeNode root) { List result = new LinkedList<>(); - Stack st = new Stack<>(); - if (root != null) st.push(root); - while (!st.empty()) { - TreeNode node = st.peek(); - if (node != null) { - st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中(中序遍历-左中右,入栈顺序右中左) - if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) - st.push(node); // 添加中节点 - st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 - if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) - } else { // 只有遇到空节点的时候,才将下一个节点放进结果集 - st.pop(); // 将空节点弹出 - node = st.peek(); // 重新取出栈中元素 - st.pop(); - result.add(node.val); // 加入到结果集 + Stack st = new Stack<>(); + if (root != null) st.push(root); + while (!st.empty()) { + TreeNode node = st.peek(); + if (node != null) { + st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中(中序遍历-左中右,入栈顺序右中左) + if (node.right != null) st.push(node.right); // 添加右节点(空节点不入栈) + st.push(node); // 添加中节点 + st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 + if (node.left != null) st.push(node.left); // 添加左节点(空节点不入栈) + } else { // 只有遇到空节点的时候,才将下一个节点放进结果集 + st.pop(); // 将空节点弹出 + node = st.peek(); // 重新取出栈中元素 + st.pop(); + result.add(node.val); // 加入到结果集 + } } + return result; } - return result; -} } ``` -迭代法后序遍历代码如下: +迭代法后序遍历(空指针标记法)代码如下: ```java class Solution { public List postorderTraversal(TreeNode root) { @@ -294,11 +294,10 @@ class Solution { TreeNode node = st.peek(); if (node != null) { st.pop(); // 将该节点弹出,避免重复操作,下面再将中右左节点添加到栈中(后序遍历-左右中,入栈顺序中右左) - st.push(node); // 添加中节点 + st.push(node); // 添加中节点 st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 if (node.right!=null) st.push(node.right); // 添加右节点(空节点不入栈) - if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) - + if (node.left!=null) st.push(node.left); // 添加左节点(空节点不入栈) } else { // 只有遇到空节点的时候,才将下一个节点放进结果集 st.pop(); // 将空节点弹出 node = st.peek(); // 重新取出栈中元素 @@ -311,6 +310,150 @@ class Solution { } ``` +迭代法前序遍历(boolean 标记法)代码如下: + +```java +// LeetCode提交时注意添加导包语句 +import java.util.AbstractMap.SimpleEntry; + +class Solution { + public List preorderTraversal(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) + return result; + Stack> stack = new Stack<>(); + stack.push(new SimpleEntry<>(root, false)); + + while (!stack.isEmpty()) { + TreeNode node = stack.peek().getKey(); + // 多加一个visited参数,使“迭代统一写法”成为一件简单的事 + boolean visited = stack.peek().getValue(); + stack.pop(); + + // visited为True,表示该节点和两个儿子位次之前已经安排过了,现在可以收割节点了 + if (visited) { + result.add(node.val); + continue; + } + + // visited当前为false, 表示初次访问本节点,此次访问的目的是“把自己和两个儿子在栈中安排好位次” + + // 前序遍历是'中左右',右儿子最先入栈,最后出栈 + if (node.right != null) { + stack.push(new SimpleEntry<>(node.right, false)); + } + + // 左儿子位置居中 + if (node.left != null) { + stack.push(new SimpleEntry<>(node.left, false)); + } + + // 节点自己最后入栈,最先出栈 + // 同时,设置 visited 为 true,表示下次再访问本节点时,允许收割 + stack.push(new SimpleEntry<>(node, true)); + } + + return result; + } +} +``` + +迭代法中序遍历(boolean 标记法)代码如下: + +```java +// LeetCode提交时注意添加导包语句 +import java.util.AbstractMap.SimpleEntry; + +class Solution { + public List inorderTraversal(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) + return result; + Stack> stack = new Stack<>(); + stack.push(new SimpleEntry<>(root, false)); + + while (!stack.isEmpty()) { + TreeNode node = stack.peek().getKey(); + // 多加一个visited参数,使“迭代统一写法”成为一件简单的事 + boolean visited = stack.peek().getValue(); + stack.pop(); + + // visited为True,表示该节点和两个儿子位次之前已经安排过了,现在可以收割节点了 + if (visited) { + result.add(node.val); + continue; + } + + // visited当前为false, 表示初次访问本节点,此次访问的目的是“把自己和两个儿子在栈中安排好位次” + + // 中序遍历是'左中右',右儿子最先入栈,最后出栈 + if (node.right != null) { + stack.push(new SimpleEntry<>(node.right, false)); + } + + // 把自己加回到栈中,位置居中 + // 同时,设置 visited 为 true,表示下次再访问本节点时,允许收割 + stack.push(new SimpleEntry<>(node, true)); + + // 左儿子最后入栈,最先出栈 + if (node.left != null) { + stack.push(new SimpleEntry<>(node.left, false)); + } + } + + return result; + } +} +``` + +迭代法后序遍历(boolean 标记法)代码如下: + +```java +// LeetCode提交时注意添加导包语句 +import java.util.AbstractMap.SimpleEntry; + +class Solution { + public List postorderTraversal(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) + return result; + Stack> stack = new Stack<>(); + stack.push(new SimpleEntry<>(root, false)); + + while (!stack.isEmpty()) { + TreeNode node = stack.peek().getKey(); + // 多加一个visited参数,使“迭代统一写法”成为一件简单的事 + boolean visited = stack.peek().getValue(); + stack.pop(); + + // visited为True,表示该节点和两个儿子位次之前已经安排过了,现在可以收割节点了 + if (visited) { + result.add(node.val); + continue; + } + + // visited当前为false, 表示初次访问本节点,此次访问的目的是“把自己和两个儿子在栈中安排好位次” + + // 后序遍历是'左右中',节点自己最先入栈,最后出栈 + // 同时,设置 visited 为 true,表示下次再访问本节点时,允许收割 + stack.push(new SimpleEntry<>(node, true)); + + // 右儿子位置居中 + if (node.right != null) { + stack.push(new SimpleEntry<>(node.right, false)); + } + + // 左儿子最后入栈,最先出栈 + if (node.left != null) { + stack.push(new SimpleEntry<>(node.left, false)); + } + } + + return result; + } +} +``` + ### Python: > 迭代法前序遍历(空指针标记法): From d6e680498b4375a06bff36a37ed274ecd5cf3373 Mon Sep 17 00:00:00 2001 From: zen <43899716+zenwangzy@users.noreply.github.com> Date: Wed, 5 Mar 2025 00:00:17 +0800 Subject: [PATCH 1505/1533] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0454-四数相加,力扣函数参数更新,故更新对应题解代码 --- ...5\233\233\346\225\260\347\233\270\345\212\240II.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index af19f5f7d8..2138590c31 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -62,18 +62,18 @@ C++代码: ```CPP class Solution { public: - int fourSumCount(vector& A, vector& B, vector& C, vector& D) { + int fourSumCount(vector& nums1, vector& nums2, vector& nums3, vector& nums4) { unordered_map umap; //key:a+b的数值,value:a+b数值出现的次数 // 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中 - for (int a : A) { - for (int b : B) { + for (int a : nums1) { + for (int b : nums2) { umap[a + b]++; } } int count = 0; // 统计a+b+c+d = 0 出现的次数 // 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。 - for (int c : C) { - for (int d : D) { + for (int c : nums3) { + for (int d : nums4) { if (umap.find(0 - (c + d)) != umap.end()) { count += umap[0 - (c + d)]; } From 792c9dfbaa1624ce97ba8ce74d0f29b7dc44f7ff Mon Sep 17 00:00:00 2001 From: wsmh <1362384538@qq.com> Date: Mon, 10 Mar 2025 09:41:08 +0800 Subject: [PATCH 1506/1533] fix sentence error --- .../0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index f788d65ba7..16b45e85a8 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -39,7 +39,7 @@ 从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素整体向后移动。 -**其实很多数组填充类的问题,其做法都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** +**其实很多数组填充类的问题,其做法都是先预先给数组扩容到填充后的大小,然后在从后向前进行操作。** 这么做有两个好处: From 1378685575df9a8ff76bd1cb56ecb731b00739eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98windscape=E2=80=99?= <2462269317@qq.com> Date: Mon, 10 Mar 2025 20:32:41 +0800 Subject: [PATCH 1507/1533] =?UTF-8?q?0126.=E9=AA=91=E5=A3=AB=E7=9A=84?= =?UTF-8?q?=E6=94=BB=E5=87=BBastar.md=E6=B7=BB=E5=8A=A0Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\232\204\346\224\273\345\207\273astar.md" | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 8ad3264433..d3aa8a114a 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -335,6 +335,86 @@ IDA * 算法 对这一空间增长问题进行了优化,关于 IDA * 算法, ### Java +```java +import java.util.PriorityQueue; +import java.util.Scanner; + +class Knight implements Comparable { + int x, y; + int g, h, f; + + public Knight(int x, int y, int g, int h, int f) { + this.x = x; + this.y = y; + this.g = g; + this.h = h; + this.f = f; + } + + @Override + public int compareTo(Knight k) { + return Integer.compare(this.f, k.f); + } +} + +public class Main { + static int[][] moves = new int[1001][1001]; + static int[][] dir = { {-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2} }; + static int b1, b2; + + static int Heuristic(Knight k) { + return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2); + } + + static void astar(Knight k) { + PriorityQueue que = new PriorityQueue<>(); + que.add(k); + while (!que.isEmpty()) { + Knight cur = que.poll(); + if (cur.x == b1 && cur.y == b2) { + break; + } + for (int i = 0; i < 8; i++) { + int nextX = cur.x + dir[i][0]; + int nextY = cur.y + dir[i][1]; + if (nextX < 1 || nextX > 1000 || nextY < 1 || nextY > 1000) { + continue; + } + if (moves[nextX][nextY] == 0) { + moves[nextX][nextY] = moves[cur.x][cur.y] + 1; + Knight next = new Knight(nextX, nextY, cur.g + 5, Heuristic(new Knight(nextX, nextY, 0, 0, 0)), 0); + next.f = next.g + next.h; + que.add(next); + } + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + while (n-- > 0) { + int a1 = scanner.nextInt(); + int a2 = scanner.nextInt(); + b1 = scanner.nextInt(); + b2 = scanner.nextInt(); + for (int i = 0; i < 1001; i++) { + for (int j = 0; j < 1001; j++) { + moves[i][j] = 0; + } + } + Knight start = new Knight(a1, a2, 0, Heuristic(new Knight(a1, a2, 0, 0, 0)), 0); + start.f = start.g + start.h; + astar(start); + System.out.println(moves[b1][b2]); + } + scanner.close(); + } +} +``` + + + ### Python ```Python @@ -683,4 +763,3 @@ int main() { - From 82c7f09b00ca0cd030f9776229b3a30df1ff5bb0 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 12 Mar 2025 10:51:49 +0800 Subject: [PATCH 1508/1533] Update --- ...46\211\200\346\234\211\350\267\257\345\276\204.md" | 6 +++--- ...46\263\242\351\202\243\345\245\221\346\225\260.md" | 2 +- ...46\260\264\346\265\201\351\227\256\351\242\230.md" | 11 ++++++----- ...46\234\200\345\244\247\345\262\233\345\261\277.md" | 2 +- problems/pics/test | 0 5 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 problems/pics/test diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 2d9292bc49..fdaa87f896 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -69,15 +69,15 @@ if (cur->left == NULL && cur->right == NULL) { 再来看一下终止处理的逻辑。 -这里使用vector 结构path来记录路径,所以要把vector 结构的path转为string格式,再把这个string 放进 result里。 +这里使用`vector` 结构path来记录路径,所以要把`vector` 结构的path转为string格式,再把这个string 放进 result里。 -**那么为什么使用了vector 结构来记录路径呢?** 因为在下面处理单层递归逻辑的时候,要做回溯,使用vector方便来做回溯。 +**那么为什么使用了`vector` 结构来记录路径呢?** 因为在下面处理单层递归逻辑的时候,要做回溯,使用vector方便来做回溯。 可能有的同学问了,我看有些人的代码也没有回溯啊。 **其实是有回溯的,只不过隐藏在函数调用时的参数赋值里**,下文我还会提到。 -这里我们先使用vector结构的path容器来记录路径,那么终止处理逻辑如下: +这里我们先使用`vector`结构的path容器来记录路径,那么终止处理逻辑如下: ```CPP if (cur->left == NULL && cur->right == NULL) { // 遇到叶子节点 diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 1c4127fcd2..ac173dbddc 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -151,7 +151,7 @@ public: * 时间复杂度:O(2^n) * 空间复杂度:O(n),算上了编程语言中实现递归的系统栈所占空间 -这个递归的时间复杂度大家画一下树形图就知道了,如果不清晰的同学,可以看这篇:[通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html) +这个递归的时间复杂度大家画一下树形图就知道了,如果不清晰的同学,可以看这篇:[通过一道面试题目,讲一讲递归算法的时间复杂度!](./前序/递归算法的时间复杂度.md) ## 总结 diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 3066c99f28..31f5f1d9d4 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -162,16 +162,17 @@ int main() { 同样从第二组边界的边上节点 逆流而上,将遍历过的节点也标记上。 -然后**两方都标记过的节点就是既可以流太平洋也可以流大西洋的节点**。 +然后**两方都标记过的节点就是既可以流向第一组边界也可以流向第二组边界的节点**。 -从第一组边界边上节点出发,如图: +从第一组边界边上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522120036.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250304174747.png) +从第二组边界上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -从第二组边界上节点出发,如图: +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250304174801.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522120122.png) +最后,我们得到两个方向交界的这些节点,就是我们最后要求的节点。 按照这样的逻辑,就可以写出如下遍历代码:(详细注释) diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index ffb6cae34d..5f091779d1 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -72,7 +72,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829104834.png) -第一步,则遍历题目,并将岛屿到编号和面积上的统计,过程如图所示: +第一步,则遍历地图,并将岛屿的编号和面积都统计好,过程如图所示: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105644.png) diff --git a/problems/pics/test b/problems/pics/test new file mode 100644 index 0000000000..e69de29bb2 From 1cfdefa3fd34abd401f9a9edc5da267b4de8ce25 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 12 Mar 2025 11:17:46 +0800 Subject: [PATCH 1509/1533] Update --- .../\351\230\277\351\207\214\344\272\221.png" | Bin 341267 -> 0 bytes problems/{pics => images}/test | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "pics/\351\230\277\351\207\214\344\272\221.png" rename problems/{pics => images}/test (100%) diff --git "a/pics/\351\230\277\351\207\214\344\272\221.png" "b/pics/\351\230\277\351\207\214\344\272\221.png" deleted file mode 100644 index 79f41d6beb9983c5945aec7cfd65bb98e1e448d6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 341267 zcmeFZbyQUEzb{USAc{yzqm+c8qzoYbeF_{(mm4M(%sFF(%lR*b2sPy z&iQ`u{MNc>-GA@bwT9Ww9v=2Q`~AG%?^iq%^hR0c!99w5XlQ5;*w{=WKZlkrx$yuD1B4%@EP2bqW?b0DA@p$Gu=6uC;;esXXQ$7&sT6I)>A<8^NQ2DR)8eO)$kVU9BD`TzTUZRfXu{1{SDt>a zJAy(&u-{8u0di8JU7fr? zAqyG^I}-EGheiU2r>l=HFV!`K$VxHG6T}yA6CsyzgF8)^gH%U@>t+v2Kv4820rJG9 z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%r;_LDnc!tRQ5fqE6htsGelXWE z7djTK7mvR3&1(dSj1z8ejro!^67b+_11=WavpTf`YpL?Es@ki?s*$IWgOB7LFU#EW zB*vBterL4>&9j5AOGSV|q#yaY!_(LClSUgr(O0f?WLT`c^^48EVoJ*Cg}{Y2#bX(C zk7_{vPJdd0`{9Td4^;daGg@t+_Re7eD|Y2Sj=a=$o@|Ts*G2R5^=ARgji1xcL)Og3 z(v~mqNvi>lyyN_js$I3(RoFSaA9alQs_S=IVwtK(0atN-;Y%W=V_oAlC35FK3T8Jb zE$Mhp$Z?{hhi|m~HRknNb09s9UEmQG;KJ!^B@t$rvNA5jZzj%4-G7yGhL7wafik7gPca|t|3 z)pP6_K<%3far~ySaa%F{Cm~LpXhT2N-f`DT^4++h7Zd6J_k|5`J!YBNN1kuv)dA04 z@_Pf3tcNac-K=iH7M8y0Dpo^){4)uKjz-7TCt$8`v(@3479oi^43bmokSBJ z@6okMjkO$O4^m_x+-;hyOz+ZQ6#bswy@=*iB|dr{S!^ zw;>{K#mWu-kTA6JzS|y{a{L!~zkav4m~FI6Z%-WubbJ0bWxyT-8lV6OI%TXWf2LzQ zMJ(l6v=V;9t^Vo-2|V{;%O2SgXmC)W=}&*>Bm5-rr}sYtBOS(}B6SfDHPSang|SZ$ zM+Holi)R+6W@}{s>oAXgSXZ3(2N-7k?_q*~qkvh?{)S>~7Tv?|hY==At)ymwXw)IT zsOs3nf3MAc;+tM+Lw?xN40+Z+mnc5%PHM-}ln7slXIA>i$Ek&-zT}dWA;o~%Dy8i5 zqa>G(>L-ms7rHKQnQnIJg-rGUzUCuptf3@! zf&q_}fBJ2ivEiaKT)kXfm$e^x@T=j--t7rDwwD51X^jVMM`n~x`MOmd39rfh&xsP^*BJFWiFHd?;a;X bJpkE!5ejt0UUOjqK8E^tu3@h}3i*EkiC|i& diff --git a/problems/pics/test b/problems/images/test similarity index 100% rename from problems/pics/test rename to problems/images/test From ee41ca792faf0db3156bfac3eb340386d5c930c7 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 12 Mar 2025 12:25:15 +0800 Subject: [PATCH 1510/1533] Update --- problems/qita/kstar.md | 131 -------------- problems/qita/xunlianying.md | 337 ----------------------------------- 2 files changed, 468 deletions(-) delete mode 100644 problems/qita/kstar.md delete mode 100644 problems/qita/xunlianying.md diff --git a/problems/qita/kstar.md b/problems/qita/kstar.md deleted file mode 100644 index 7d1b045c8e..0000000000 --- a/problems/qita/kstar.md +++ /dev/null @@ -1,131 +0,0 @@ -# 代码随想录知识星球 - -前一阵知识星球刚刚发布了[星球精华-大厂八股文(第三版)](https://programmercarl.com/other/kstar_baguwen.html) - -这份八股文,就有30w字,将近400张思维导图,表格,分析图,整个PDF将近900页的篇幅。 - -这些其实都是星球录友们,每日打卡的内容,但这我也仅仅是整理了一部分,因为信息量确实巨大。 - -目前星球里已经有将近1000 个精华帖: - -

- -同时还有[计算机2023届求职薪资PDF](https://programmercarl.com/other/2022salary.html)等一些列独家资料,都在星期置顶帖里: - -
- -星球里的录友都可以得到我1V1的指导,**我已经详细回答了7000+个问题**: (这个回答问题数量,可以看出我有劳模的潜质 ) - -
- -有的时候,大家还是需要过来人,给指点一点,甚至是“踹一脚” 就会想清楚很多。 - -
- -不仅我回答问题,我还会邀请星球里各个方向的录友来和大家一起交流具体技术问题,这个就是星球导师计划: (如果想提问的话,也在星球置顶1可以找到链接) - -
- -
- -可以看看星球导师计划里具体的问答: - -
- -同时我还给录友们至少修改了上千份的简历,我也总结了很多大家写简历上问题。在 「写简历」这个tab上,可以找到我总结的所有问题和简历模板 - -
- -【专业技能】【项目经验】【自我评价】都应该怎么写,面试时候 自我介绍,应该怎么说,我都给出了我的建议: - -
- - -如果你还在犹豫要不要加入的话,**可以进来体验三天,三天内点击知识星球APP右上角,可以自助全额退款**。 绝对不会坑大家! -
- - -一些录友当初也是进来 白嫖一波资料,就退款跑了,不过后面又加回来,例如这位录友: - -
- -**星球里的资料仅仅是辅助,更重要的是星球里的这一圈人,你会发现 这个圈子的质量非常高!** - -不仅仅是 **211、985录友非常之多**,关键是大家都非常努力上进! - -这是知识星球APP里可以看到,录友们的日常打卡: - -
- -刷星球上的内容,要刷朋友圈,刷抖音,有意义的多。 - -
- -
- -星球网页版是这样的: - -
- -加入星球,是很多录友当年做的最有意义的一件事情 - -
-
-
-
- -可以看看星球里的交流氛围: - -
- -
- -
- -
- -
- -大家的很多疑问在星球置顶3,我都做了详细的整理,录友都说我是“整理狂魔”,不过大家懒,我就得勤劳一些。 - -
- -星球置顶3的信息量非常大,不仅仅是整理各个求职方向的学习路线,还有大家的常见疑惑,我之前回答过的内容,都做了整理。 - -大家看完之后,其实对自己就会有明确的规划了。 - -
- -给大家看看星球置顶帖3的部分内容,以下仅仅是部分截图: - -
- -
- -
- -
- - -大家加入星球后,一定要看星球置顶帖和精华帖的内容,你会发现这里很有优秀录友的帖子,包括:各种资料,学习路线,学习心得,规划,职场发展等等。 - -
- -很多录友看完之后都更加明确了自己的方向。 - -
- - -相对于其他星球,「代码随想录」知识星球到底怎么样,可以看看录友们是怎么说的。 - -
- -最后也欢迎大家加入代码随想录[知识星球](https://mp.weixin.qq.com/s/wPaJumc8afuzWLo72yRlIw),**这里有很多优秀的人,有很多精彩的事!** - -
- -这里依然给出10元代金券,微信扫领代金券加入,如果感觉不值得,**三天内知识星球APP右上角直接全额退款!** 无任何套路。 - -
- - diff --git a/problems/qita/xunlianying.md b/problems/qita/xunlianying.md deleted file mode 100644 index f13290f302..0000000000 --- a/problems/qita/xunlianying.md +++ /dev/null @@ -1,337 +0,0 @@ - -# 代码随想录算法训练营 - -::: tip 通知 -训练营35期,将于 4月3日开营,目前可以报名,报名后提前拉群,在群里等着开营就好。 -::: - -大家可以百度搜索:代码随想录算法训练营, 看看往期录友们在训练营里打卡总结的博客。 - -
- -这是训练营里录友坚持到最后一天的打卡,大家可以看看他们的博客是每天都有记录的: - -* [这种方式,有效逼我坚持下来(C++-小飞-嘉院大三)(精华)](https://blog.csdn.net/weixin_60353640/article/details/133797799) -* [完成比完美重要(Java-小姜-已工作/南京)(精华)](https://xie.infoq.cn/article/3d07b4040ceab0f546d66e3e1) -* [已经刷了500题的基础,参加训练营依然收获满满(Java-怪懒懒-求职)(精华)](https://blog.csdn.net/2301_78266314/article/details/132144046) - -* [算法超级弱,最后坚持下来了(Java-信任呢-上大研二)(精华)](https://blog.csdn.net/xinrenne/article/details/133267089) -* [第一次比较完整的刷题训练经历,群里氛围超级好(JAVA-雷贯三十三重天-北航研二)(精华)](https://blog.csdn.net/qq_44120129/article/details/133230372) -* [我全程坚持下来,还是很有成就感的(python-wj-待业)(精华)](https://blog.csdn.net/u013441272/article/details/133229421) - -* [一点基础都没有,坚持下来了(C++ 润 大二)(精华)](https://blog.csdn.net/m0_74583479/article/details/132776719) -* [这个钱花的很值得(C++-GMZ-研一)(精华)](https://blog.csdn.net/weixin_43303286/article/details/132796571) -* [看着名单里录友都在坚持,自己也要坚持(C++-凯-湖工大研三)(精华)](https://blog.csdn.net/weixin_62453859/article/details/132788830) - -* [一刷心得(Java-小何同学-广财大二)(精华)](https://juejin.cn/post/7272250890597531684) -* [花钱买服务、买环境、买时间(Java-古今-大工研二)(精华)](https://blog.csdn.net/dannky_Z/article/details/132532049) -* [一刷心得(java-唔哩凹-大三)(精华)](https://blog.csdn.net/iwtup/article/details/132545456) - -* [训练营结束有点不舍,坚持最久的一件事(C++-徐一-中科院研二)(精华)](https://blog.csdn.net/weixin_46108098/article/details/132158352) -* [同学推荐,报名训练营,坚持下来了(c++-刘浩-沈自所-研二)(精华)](https://blog.csdn.net/qq1156148707/article/details/132155446) - -* [每日的刷题训练真的艰难,但坚持下来了(C++-五-已工作福建)(精华)](https://blog.csdn.net/weixin_44952586/article/details/131909720) -* [加入训练营,就是因为这个气氛,只靠自己很难坚持(cpp-Lord HouSton-cqu研二)(精华)](https://blog.csdn.net/HSL13594379250/article/details/131889934) -* [很幸运,我坚持下来了,感觉收货满满(java-李-UCAS研0)(精华)](https://blog.csdn.net/ResNet156/article/details/131920163) -* [谈谈自己的收获,养成了写博客的习惯(java-翌-研二)(精华)](https://blog.csdn.net/weixin_47460244/article/details/131912316) - -* [养成了刷题的习惯(C++-热心市民C先生-南理工研一)(精华)](https://blog.csdn.net/qqq1521902442/article/details/131614999) -* [工作也坚持下来(Python-Hongying-已工作杭州)(精华)](https://blog.csdn.net/weixin_42286468/article/details/131628069) -* [入营不亏(C++-小叶子-云财研二)(精华)](https://blog.csdn.net/dream_aleaf/article/details/131613667) - -* [训练营一刷总结(Java-HQH-研二)](https://blog.csdn.net/weixin_43821876/article/details/128991822) -* [训练营总结,一群人才能走的更远(Java-Lixy-已工作南京)](https://blog.csdn.net/weixin_45368277/article/details/128997823) -* [训练营总结,中途🐑了,也坚持下来(C++-Jane-科大研二)](https://blog.csdn.net/Jane_10358/article/details/128977424) -* [这两个月有很多不可控因素,但依然坚持下来(java-hha-南工大二)](https://blog.csdn.net/qerwtrt4t/article/details/128975401) -* [训练营总结,最后坚持下来(C++ - 阿舟 - 已工作武汉)](https://blog.csdn.net/m0_74360161/article/details/129000723) - - -博客链接:[https://blog.csdn.net/m0_61724447/article/details/128443084](https://blog.csdn.net/m0_61724447/article/details/128443084) -
- -博客链接:[https://juejin.cn/post/7170304080504586254](https://juejin.cn/post/7170304080504586254) -
- -博客链接:[https://blog.csdn.net/weixin_44047621/article/details/128430623](https://blog.csdn.net/weixin_44047621/article/details/128430623) -
- -博客链接:[https://blog.csdn.net/weixin_47467016/article/details/128460565](https://blog.csdn.net/weixin_47467016/article/details/128460565v) -
- -也有一些录友,把总结发在训练营内部打卡表里,例如: - -昵称:java-低调-已工作 - -通过两个月的时间系统性的学习了算法,然后按照不同的题目去做分类,设计的刷题进度也很好,让自己有了一个质的提升,贵在坚持,好在自己也是坚持了下来,**通过自己的坚持,让自己养成了一个刷题的好习惯,这才是最难能可贵的**。 - -但是时间跨度有点大,还是要继续坚持之后自己去二刷,这样才能更好的巩固,把算法知识学习的更好。 - ---------- - -昵称:java-岂几岂几-毕业 - -收获真的很大,这是第一次刷算法题,清楚了面试高频题的题型,**巩固了之前摇摇欲坠的自学算法基础**。接下来计划是重刷随想录,并且补充上一亩三分地刷题区置顶贴里列出的题型,在巩固一刷的基础上增加做题量。 - ------------- - -昵称:python/go-ds-研三 - -跟着卡哥的训练营最大的收获就是把代码随想录都通读了一遍,因为进营之前就已经刷过不少力扣题了,但很多都是当时自己捣鼓出来或者看官方题解的。 - -而这一次的60天刷题,不管题目做没做过,都看过卡哥的代码随想录了,**这其中的区别也是最大的收获就是知识体系建立起来了**,越往后做题,条理越清晰。 - -即使有些题一刷还是做不太出来,但不再像之前自己做那样做题前后都是懵逼状态了,而是有一个清晰明了的判断了。 - -但coding能力还是有待改进,接下来要进行二刷,同时也祝卡哥的事业蒸蒸日上,代码随想录越办越好! - ------------- - -昵称:Python-ukn-研二 - -完美收官,有点小遗憾的是后面dp做得有点赶,没有沉下心来消化,接下来重点把自己不擅长的专题和重点专题二刷甚至三刷。 - -**跟着训练营练下来最大的感受是很有信心,有节奏有计划**,每过完一个专题,就多一分成就感,题感也越来越好,期待自己的规律二刷,谢谢一路坚持的小伙伴们!谢谢大佬助手和卡哥! - - ------------ - - -## 训练营的目的是什么? - -对于刷题,学算法,[《代码随想录》](https://programmercarl.com/other/publish.html)(programmercarl.com)已经把刷题顺序给大家列好了,大家跟着刷就行。 - -但即使这样,其实不少录友还会有很多疑问,不知道怎么用代码随想录,例如: - -* 卡哥,**有没有一起从0开始刷代码随想录的录友,想一起组个队** -* 卡哥您好,我是985准研一非科班,自学java, 然后现在在刷代码随想录,**请问需要每个题目的所有解法都掌握吗**?请教下卡哥正确的刷题姿势🙏 -* **我大概多久才能刷完代码随想录**? -* 二叉树,我只掌握 递归够用么? -* 很多解法,我是不是只用暴力就可以,**时间比较紧,我还要去掌握优化方法吗**? -* 卡哥,**请问跟着代码随想录刷题有答疑的服务吗**? 因为有的题目 自己写的怎么都不对,浪费很多时间,可能过来人指点一下立刻就知道。 -* 卡哥,我KMP太难了,我跳过可以吗? -* 卡哥,我进了刷题群,可是**大家刷题进度不尽相同,所以讨论起来经常不在一个频道上**。 -* 刚开始还看了一周代码随想录,后来又..摆烂了... **最近又重新再看代码随想录,然后卡住了又摆烂了好几天了**...... -* 卡哥,**我刷题很容易囫囵吞枣,虽然说代码随想录一刷,但很多内容根本没消化,在进度上欺骗自己**,好像一刷完了,但感觉自己理解的,不到30%。 -* 卡哥,**感觉之前刷的都忘了,能力没有什么提升,现在还是一道都不会做**。我一刷每道题都得先看看题解然后忘了再去看边看边写。 - -**以上这些是不是有戳中某些录友们的痛处**。 - -其实对于很多算法基础不太好的录友,即使资料已经很齐全,但还是需要一些规划和答疑。 - -而且在时间规划上,因为刚开始刷的录友,不知道 前方题目 是多大难度,所以 一开始计划 一天刷三道,往往因为遇到了一道难题,一天也解决不了,耽误了整体进度,甚至直接开始摆烂,下次再开始刷题可能就很久以后了...... - -所以 **代码随想录算法训练营** 帮助大家在规划时间内,有质量的完成代码随想录一刷。 - -我亲自给大家规划节奏,大家一起按照我的节奏来,规定时间内,一刷一定能把代码随想录所有内容吃透,然后大家自己去二刷,三刷就好了,师傅领进门修行在个人。 - -## 训练营提供一些什么呢? - -1.具体内容 - -针对代码随想录上,**195篇算法文章,主要题目150道**,手把手带大家刷完,帮大家做好详细刷题规划,每天布置刷题任务,监督博客记录总结。 - -任务布置 -
- -每日规划: -
- -训练营周期内,每天应该做哪些题目,同时我根据题目的难度,适当调整每天学习安排,不会是 每天固定3题的这种,而是根据难度而定。 - -我会告诉大家,哪些解法是一刷的时候必须掌握的,哪些解法可以二刷再去学习,哪些总结是必看的。 - -每日打卡: -
- -关于如果debug自己的代码,训练营会给具体建议: -
- -训练营群中每日讨论的重点内容都会做整理,在分享给大家训练营成员: -
- -**同时每天做针对大家的疑问做详细答疑,保证大家消化当天的学习内容**。 - -2.**气氛气氛还是气氛** - -训练营中,**大家都是同一个基础,同一个进度刷题,每天刷题题目都是一样的**,这样的一个学习群,大家讨论起来更有意义。 -**还有会监督机制**,训练营的成员要注册一个自己的博客(自己搭建或者使用博客网站都可以),每天要去写今日刷题心得和总结,来进行打卡。 - -
- -3.带大家写博客 - -很多录友平时刷题,或者学习技术,没有写博客的习惯,或者因为懒,就不写了。 - -但大家学了很多技术之后,发现 好像都忘了。。。 - -所以训练营会带着大家写博客,每天都要写博客,博客的标题,格式,我都帮大家规划好,倒逼自己养生记录的习惯。 - -因为训练营很多录友开始有了写博客的习惯,以下是一些录友博客的结尾部分: - -
- -
- -每天训练营群里会每天统计大家的博客情况。这样不仅可以监督自己总结,针对大家写的比较好的博客,会给予曝光,增加自己写博客的动力。 - -训练营里的录友们可以相互参考对方的博客,看谁总结的更好。 - -
- - -4.关于答疑 - -很多录友可能担心自己的问题,得不到解决,或者在群里和大家讨论,也没人回复 导致自己因为小问题卡了很久,甚至直接摆烂好一阵子。 - -所以训练营里大家的问题,我都会做答疑。 - -估计训练营里的问题会比较多,我也可能回答不过来。所以我会找了算法能力很强的助手协助我给大家答疑,也就是说,**大家刷题遇到问题,不会有后顾之忧,当天的问题,当天一定会得到解决**。 - -
- -当然训练营题目答疑,**仅限于 每天规划的题目**,并不会大家刷的其他算法题都做答疑,那样的话工作量很容易不可控(这里我也不会夸大承诺,欺骗大家报名之类的),如果是其他算法题可以在群里和大家交流。 - -### 训练营的资料是什么呢? - -**强调一下:训练营里所有的资料,都是我独立制作而且是开源免费的:即代码随想录网站(programmercarl.com),Github:https://github.com/youngyangyang04/leetcode-master和[代码随想录算法公开课](https://mp.weixin.qq.com/s/xncn6IHJGs45sJOChN6V_g)** - -训练营提供给大家的服务是**规划,监督,指导和答疑**。 - -至于代码随想录算法内容的质量如何,这个已经是有口皆碑了,基本是面试求职必刷的资料。 - -
- -
- -而且代码随想录开源的内容要比市面上 大家付费几百,上千元报的算法训练营的资料都要好的多。 - -毕竟内容是开源的,质量如何 大家自己去看就好。 - -## 训练营的学习方式 - -组织方式:一个学习微信群(180人左右),大家进群之后,等群公告就好,我会通知开始时间和每日刷题计划。 - -所需时间:训练营为期60天(两个月),群里每天会布置学习任务,只要大家跟上节奏,60天一定可以刷完代码随想录。 - -每日任务:需要花费3-4个小时左右的时间来完成。这是针对一般算法水平的学习速度来规划的时间,不同水平会有差异。 - -每周周日会休息一天,没跟上进度的录友,可以跟进度,跟上进度的录友可以复习或者适当放松一下。 - -监督机制:训练营里,每天会针对大家每天所刷的题目做答疑,同时也会有监督打卡机制,在群公告里会详细描述。 - -所需语言:**所有语种都可以**,毕竟代码随想录几乎支持所有主流语言,**也会针对大家所用的语言做针对性答疑**。 - -## 开营时间 - -**训练营开始常态化报名,即一直可以报名,当人满180人的时候,就开始新的一期**。 最新的一期可以看文章评论区,或者文章开头。 - -## 训练营的价格 - -大家应该最关心的是价格了,**定价依然是268元**,注意这是两个月训练营的费用,而且是全程规划,指导,监督和答疑。 - -(对于[知识星球](https://programmercarl.com/other/kstar.html)里的录友的话,训练营会立减30元,也就是238元,后面如果推出其他服务,星球录友都相当于VIP,都会有优惠。当然如果你已经报了训练营,再去报知识星球,并不给再给大家优惠了,一定要先是星球成员,再报训练营才有优惠) - -大家能在市面上找到算法训练营都价格不菲,基本都是上千的单价,**而且内容和质量并没有 代码随想录 优质**。 - -后面一定会涨价的,**如果你确实需要有人带,有监督,给规划,有答疑,能花两个月时间更下来的话,还是早报早学习**。 - -## 我适合报名吗? - -符合一下特点的录友可以报名: - -* 基础比较差,没刷过代码随想录或者刚开始刷 -* 刷过一些代码随想录的题目了,感觉掌握不扎实,想用2个月时间系统重刷一遍 -* 自己刷题,**很容易遇到各种代码问题,需要有人答疑** -* 以前一刷过,但基本都忘了,想高质量二刷 -* **想找队友,一起从0刷代码随想录** -* 自控能力差,遇到点问题就容易躺好一阵子,需要别人监督学习 -* **想有一个规划时间,来刷完代码随想录** -* 不知道代码随想录中哪些解法是必备的,哪些解法是可以简单了解的 -* 刷题总会忘,感觉刷了和没刷差不多,**不擅长做总结,不擅长写博客记录心得**,自己也懒得写博客写总结 - -以下录友不合适报名: - -* 自学能力强,代码随想录资料都是开源的,刷题顺序也列好了,自学能力强的录友自己学就行 -* 有算法和代码基础,基本算法题遇到的问题,都能通过自己debug解决 -* 没有两个月时间,每天也不能抽出那么多时间学习算法 -* 算法0基础,基本的数据结构都没听说过,例如数组,链表。 -* 编程0基础,基本的编程语言还不会,因为训练营还是默认大家会熟悉所用编程语言里的各种容器的使用 - -**训练营不限编程语言**,任何语言都可以报名,都会答疑。 - - -## 常见疑问 - -**海外录友有时差可以报名吗**? - -可以的,一期就有很多海外的录友,有疑问在群里也会回复,而且群里讨论的重点内容,都会有总结,不用担心错过了精彩内容。 - -**已经工作的录友适合报名吗**? - -适合报名。对于工作的录友,每天未必说一定挤出3-4个小时来刷题。 - -对于时间充足的录友,要刷拓展题,要写博客作总结。 如果时间紧张,任务上是可以适当精简。 - -所以每日任务弹性还是比较大的,至少跟上进度保证每天的题目代码提交通过了,看看群里的讨论,自己理解加深了就可以。 - -工作的录友要学会挤时间,训练营一期录友有不少是工作的,他们是这么搞的: - -训练营每日晚上提前发布明天的任务, 他们第二天通勤 时候 可以先看题想思路,白天抽空看文章解析 看看思路是否一致,看看群里讨论内容,晚上下班可以一口气把当天的题目刷完。 - -加入训练营,每日对自己有一个压迫感,挤一挤 时间就有了。 - -对于工作的录友,我之前本来计划是安排一期 工作日题量小 休息日题量多一些的训练营,但通过一期发现,包括已经工作的录友,**大家休息日真的没有心思学习,甚至“比工作日更忙”**,所以理想很丰满,现实很骨感。 - -**要不要搞三个月四个月半年的训练营**? - -目前来看四个月以上的时间有点太长了,时间长价格也会高,毕竟要服务的时间长了。 - -而且刷题在于一鼓作气,把时间拉的太长,很多录友都是前期 动力十足,后面无论是 如何@ 如何公告 如果催大家 赶进度,大家都会无动于衷,从最终效果来看 战线不能太长。 - -所以没有逼自己一把 跟上进度的决心,就算搞一年时间的训练营,该放弃的还是会放弃。 - -至于三个月的训练营,是可以考虑的,不过安排时间还要待定。 - -## 报名方式 - -* 正常支付,价格:268 (支付成功后,支付记录发给客服 -* 知识星球录友支付,价格:238 (支付成功后,[代码随想录知识星球](https://programmercarl.com/other/kstar.html)截图 和 支付记录发给客服 -* 往期算法训练营录友再次报名,价格:130 (支付成功后,往期训练营群或者支付记录 和 本次支付记录发给客服 - -(**注意一定要是[代码随想录知识星球](https://programmercarl.com/other/kstar.html)成员才会有优惠**) - -支付宝支付如下: - -
- -[微信支付点击这里](https://www.programmercarl.com/other/weixinzhifu.html) - -付款后,将付款截图发给客服,客服会在24h内统一回复,**所以大家发给客服信息不要急,当天一定会回复的**。 - -
- -关于训练营的任何问题,可以在客服这里咨询! - - - -## 最后 - -训练营其实算是代码随想录的一个补充,其内容都是免费开放的,有学习能力的录友自己学习就好。 - -单就从我的 [代码随想录算法公开课](https://mp.weixin.qq.com/s/xncn6IHJGs45sJOChN6V_g) 来说,质量如何,大家可以去看评论区,我完全可以把它做成付费的视频课,但我还是选择免费开放给大家,目前一周会更新四个算法视频,已经快把二叉树系列更完了。 - -之所以做训练营,是因为大家太多的问题,不是视频或者文章教程可以解决的,需要的是规划,组织,监督和答疑。 - -所以我才组织训练营,搞成付费的也是为了质量更高一些,同时也是因为需要一些门槛,要不然就和普通刷题群没什么区别了。 - -等大家跟着代码随想录训练营一路走下来之后,大家再回顾自己两个月学习的内容和总结的博客,**一定会发现 这个价格 物超所值**! - -关于训练营的任何疑问都可以扫码联系客服 - -
- - - - - - From 4b01a7d5e412f393f81a66212360f16fd14ddbbf Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 14 Mar 2025 17:11:14 +0800 Subject: [PATCH 1511/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\265\201\351\227\256\351\242\230.md" | 4 ++-- ...46\344\270\262\346\216\245\351\276\231.md" | 21 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 31f5f1d9d4..1c646b1c03 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -111,14 +111,14 @@ bool isResult(vector>& grid, int x, int y) { break; } } - // 第二边界右边 + // 第二边界下边 for (int j = 0; j < m; j++) { if (visited[n - 1][j]) { isSecond = true; break; } } - // 第二边界下边 + // 第二边界右边 for (int i = 0; i < n; i++) { if (visited[i][m - 1]) { isSecond = true; diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 3b6c20ca08..5f11e4fbfa 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -13,10 +13,14 @@ 2. 序列中最后一个字符串是 endStr。 -3. 每次转换只能改变一个字符。 +3. **每次转换只能改变一个位置的字符**(例如 ftr 可以转化 fty ,但 ftr 不能转化 frx)。 4. 转换过程中的中间字符串必须是字典 strList 中的字符串。 +5. beginStr 和 endStr 不在 字典 strList 中 + +6. 字符串中只有小写的26个字母 + 给你两个字符串 beginStr 和 endStr 和一个字典 strList,找到从 beginStr 到 endStr 的最短转换序列中的字符串数目。如果不存在这样的转换序列,返回 0。 输入描述 @@ -67,16 +71,23 @@ yhn 所以这道题要解决两个问题: -* 图中的线是如何连在一起的 -* 起点和终点的最短路径长度 +1、图中的线是如何连在一起的 + +在搜索的过程中,我们可以枚举,用26个字母替换当前字符串的每一个字符,在看替换后 是否在 strList里出现过,就可以判断 两个字符串 是否是链接的。 + +2、起点和终点的最短路径长度 首先题目中并没有给出点与点之间的连线,而是要我们自己去连,条件是字符只能差一个。 所以判断点与点之间的关系,需要判断是不是差一个字符,**如果差一个字符,那就是有链接**。 -然后就是求起点和终点的最短路径长度,**这里无向图求最短路,广搜最为合适,广搜只要搜到了终点,那么一定是最短的路径**。因为广搜就是以起点中心向四周扩散的搜索。 +然后就是求起点和终点的最短路径长度,在无权图中,求最短路,用深搜或者广搜就行,没必要用最短路算法。 + +**在无权图中,用广搜求最短路最为合适,广搜只要搜到了终点,那么一定是最短的路径**。因为广搜就是以起点中心向四周扩散的搜索。 + +**本题如果用深搜,会比较麻烦,要在到达终点的不同路径中选则一条最短路**。 -**本题如果用深搜,会比较麻烦,要在到达终点的不同路径中选则一条最短路**。 而广搜只要达到终点,一定是最短路。 +而广搜只要达到终点,一定是最短路。 另外需要有一个注意点: From ca2cffabd0ac5e2c6efab2be56743265fbe521c4 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 14 Mar 2025 17:35:33 +0800 Subject: [PATCH 1512/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b1db51c903..acd07c28d0 100644 --- a/README.md +++ b/README.md @@ -69,13 +69,14 @@ ## 前序 -* [「代码随想录」学习社区](https://programmercarl.com/other/kstar.html) +* [做项目(多个C++、Java、Go、前端、测开项目)](https://programmercarl.com/other/kstar.html) * 编程语言 * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) * [编程语言基础课](https://kamacoder.com/courseshop.php) * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) + * [大厂算法笔试题](https://kamacoder.com/company.php) * 工具 * [一站式vim配置](https://github.com/youngyangyang04/PowerVim) @@ -384,10 +385,10 @@ 8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md) 9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md) 10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md) -11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) -12. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) -13. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) -14. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) +11. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) +12. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) +13. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) +14. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) 15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md) 16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md) 17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md) From 0881fb5e81154b1c55cd663d86bf0d89381be935 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 14 Mar 2025 18:05:33 +0800 Subject: [PATCH 1513/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...227\347\254\246\344\270\262\346\216\245\351\276\231.md" | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 5f11e4fbfa..f6d97866da 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -57,7 +57,11 @@ yhn 2 <= N <= 500

+<<<<<<< HEAD +======= + +>>>>>>> d0bd2dc5 (更新图)

@@ -65,7 +69,8 @@ yhn 以示例1为例,从这个图中可以看出 abc 到 def的路线 不止一条,但最短的一条路径上是4个节点。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240529121038.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250317105155.png) + 本题只需要求出最短路径的长度就可以了,不用找出具体路径。 From f3c481e80b57b77fc338bc0ee7c2d6514f74ef17 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Fri, 14 Mar 2025 18:05:59 +0800 Subject: [PATCH 1514/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E5=A4=B4=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\346\225\260\344\271\213\345\222\214.md" | 12 +++------ ...36\346\226\207\345\255\220\344\270\262.md" | 12 +++------ ...11\346\225\260\344\271\213\345\222\214.md" | 12 +++------ ...27\346\257\215\347\273\204\345\220\210.md" | 12 +++------ ...33\346\225\260\344\271\213\345\222\214.md" | 12 +++------ ...4N\344\270\252\350\212\202\347\202\271.md" | 12 +++------ ...10\347\232\204\346\213\254\345\217\267.md" | 12 +++------ ...55\347\232\204\350\212\202\347\202\271.md" | 12 +++------ ...73\351\231\244\345\205\203\347\264\240.md" | 12 +++------ .../0028.\345\256\236\347\216\260strStr.md" | 12 +++------ ...00\344\270\252\346\216\222\345\210\227.md" | 12 +++------ ...00\344\270\252\344\275\215\347\275\256.md" | 12 +++------ ...22\345\205\245\344\275\215\347\275\256.md" | 12 +++------ ...7.\350\247\243\346\225\260\347\213\254.md" | 12 +++------ ...04\345\220\210\346\200\273\345\222\214.md" | 12 +++------ ...\345\220\210\346\200\273\345\222\214II.md" | 12 +++------ ...2.\346\216\245\351\233\250\346\260\264.md" | 12 +++------ ...\350\267\203\346\270\270\346\210\217II.md" | 12 +++------ ...6.\345\205\250\346\216\222\345\210\227.md" | 12 +++------ ...\345\205\250\346\216\222\345\210\227II.md" | 12 +++------ "problems/0051.N\347\232\207\345\220\216.md" | 12 +++------ .../0052.N\347\232\207\345\220\216II.md" | 12 +++------ ...47\345\255\220\345\272\217\345\222\214.md" | 12 +++------ ...01\350\247\204\345\210\222\357\274\211.md" | 12 +++------ ...72\346\227\213\347\237\251\351\230\265.md" | 12 +++------ ...63\350\267\203\346\270\270\346\210\217.md" | 12 +++------ ...10\345\271\266\345\214\272\351\227\264.md" | 12 +++------ ...\346\227\213\347\237\251\351\230\265II.md" | 12 +++------ ...15\345\220\214\350\267\257\345\276\204.md" | 12 +++------ ...\345\220\214\350\267\257\345\276\204II.md" | 12 +++------ ...0.\347\210\254\346\245\274\346\242\257.md" | 12 +++------ ...14\345\214\205\347\211\210\346\234\254.md" | 12 +++------ ...26\350\276\221\350\267\235\347\246\273.md" | 12 +++------ "problems/0077.\347\273\204\345\220\210.md" | 12 +++------ ...04\345\220\210\344\274\230\345\214\226.md" | 12 +++------ "problems/0078.\345\255\220\351\233\206.md" | 12 +++------ ...47\347\232\204\347\237\251\345\275\242.md" | 12 +++------ "problems/0090.\345\255\220\351\233\206II.md" | 12 +++------ ...\345\216\237IP\345\234\260\345\235\200.md" | 12 +++------ ...11\346\220\234\347\264\242\346\240\221.md" | 12 +++------ ...11\346\220\234\347\264\242\346\240\221.md" | 12 +++------ ...70\345\220\214\347\232\204\346\240\221.md" | 12 +++------ ...60\344\272\214\345\217\211\346\240\221.md" | 12 +++------ ...02\345\272\217\351\201\215\345\216\206.md" | 12 +++------ ...00\345\244\247\346\267\261\345\272\246.md" | 12 +++------ ...40\344\272\214\345\217\211\346\240\221.md" | 12 +++------ ...11\346\220\234\347\264\242\346\240\221.md" | 12 +++------ ...41\344\272\214\345\217\211\346\240\221.md" | 12 +++------ ...00\345\260\217\346\267\261\345\272\246.md" | 12 +++------ ...57\345\276\204\346\200\273\345\222\214.md" | 12 +++------ ...04\345\255\220\345\272\217\345\210\227.md" | 12 +++------ ...02\347\202\271\346\214\207\351\222\210.md" | 12 +++------ ...00\344\275\263\346\227\266\346\234\272.md" | 12 +++------ ...\344\275\263\346\227\266\346\234\272II.md" | 12 +++------ ...01\350\247\204\345\210\222\357\274\211.md" | 12 +++------ ...344\275\263\346\227\266\346\234\272III.md" | 12 +++------ ...25\350\257\215\346\216\245\351\276\231.md" | 12 +++------ ...60\345\255\227\344\271\213\345\222\214.md" | 12 +++------ ...25\347\232\204\345\214\272\345\237\237.md" | 12 +++------ ...62\345\233\236\346\226\207\344\270\262.md" | 12 +++------ ...\345\233\236\346\226\207\344\270\262II.md" | 12 +++------ ...4.\345\212\240\346\262\271\347\253\231.md" | 12 +++------ ...06\345\217\221\347\263\226\346\236\234.md" | 12 +++------ ...25\350\257\215\346\213\206\345\210\206.md" | 12 +++------ ...57\345\275\242\351\223\276\350\241\250.md" | 12 +++------ ...\345\275\242\351\223\276\350\241\250II.md" | 12 +++------ ...15\346\216\222\351\223\276\350\241\250.md" | 12 +++------ ...76\345\274\217\346\261\202\345\200\274.md" | 12 +++------ ...14\347\232\204\345\215\225\350\257\215.md" | 12 +++------ ...70\344\272\244\351\223\276\350\241\250.md" | 12 +++------ ...\344\275\263\346\227\266\346\234\272IV.md" | 12 +++------ ...13\350\275\254\346\225\260\347\273\204.md" | 12 +++------ ...23\345\256\266\345\212\253\350\210\215.md" | 12 +++------ ...7.\345\271\277\346\220\234\347\211\210.md" | 12 +++------ ...7.\346\267\261\346\220\234\347\211\210.md" | 12 +++------ ...2.\345\277\253\344\271\220\346\225\260.md" | 12 +++------ ...76\350\241\250\345\205\203\347\264\240.md" | 12 +++------ ...04\345\255\227\347\254\246\344\270\262.md" | 12 +++------ ...73\350\275\254\351\223\276\350\241\250.md" | 12 +++------ ...7.\350\257\276\347\250\213\350\241\250.md" | 12 +++------ ...04\345\255\220\346\225\260\347\273\204.md" | 12 +++------ ...\350\257\276\347\250\213\350\241\250II.md" | 3 +++ ...\345\256\266\345\212\253\350\210\215II.md" | 12 +++------ ...345\220\210\346\200\273\345\222\214III.md" | 12 +++------ ...02\347\202\271\344\270\252\346\225\260.md" | 12 +++------ ...27\345\256\236\347\216\260\346\240\210.md" | 12 +++------ ...54\344\272\214\345\217\211\346\240\221.md" | 12 +++------ ...36\347\216\260\351\230\237\345\210\227.md" | 12 +++------ ...36\346\226\207\351\223\276\350\241\250.md" | 12 +++------ ...54\345\205\261\347\245\226\345\205\210.md" | 12 +++------ ...54\345\205\261\347\245\226\345\205\210.md" | 12 +++------ ...43\346\234\200\345\244\247\345\200\274.md" | 12 +++------ ...15\345\274\202\344\275\215\350\257\215.md" | 12 +++------ ...00\346\234\211\350\267\257\345\276\204.md" | 12 +++------ ...50\345\271\263\346\226\271\346\225\260.md" | 12 +++------ ...3.\347\247\273\345\212\250\351\233\266.md" | 12 +++------ ...07\345\255\220\345\272\217\345\210\227.md" | 12 +++------ ...53\345\206\267\345\206\273\346\234\237.md" | 12 +++------ ...66\351\222\261\345\205\221\346\215\242.md" | 12 +++------ ...11\346\216\222\350\241\214\347\250\213.md" | 12 +++------ ...345\256\266\345\212\253\350\210\215III.md" | 12 +++------ ...64\346\225\260\346\213\206\345\210\206.md" | 12 +++------ ...54\345\255\227\347\254\246\344\270\262.md" | 12 +++------ ...30\351\242\221\345\205\203\347\264\240.md" | 12 +++------ ...04\347\232\204\344\272\244\351\233\206.md" | 12 +++------ ...06\345\212\250\345\272\217\345\210\227.md" | 12 +++------ ...10\346\200\273\345\222\214\342\205\243.md" | 12 +++------ ...3.\350\265\216\351\207\221\344\277\241.md" | 12 +++------ ...55\345\255\220\345\272\217\345\210\227.md" | 12 +++------ ...66\345\255\220\344\271\213\345\222\214.md" | 12 +++------ ...15\345\273\272\351\230\237\345\210\227.md" | 12 +++------ ...11\345\222\214\345\255\220\351\233\206.md" | 12 +++------ ...64\346\265\201\351\227\256\351\242\230.md" | 12 +++------ ...15\345\217\240\345\214\272\351\227\264.md" | 12 +++------ ...55\347\232\204\350\212\202\347\202\271.md" | 12 +++------ ...25\347\210\206\346\260\224\347\220\203.md" | 12 +++------ ...\346\225\260\347\233\270\345\212\240II.md" | 12 +++------ ...06\345\217\221\351\245\274\345\271\262.md" | 12 +++------ ...20\345\255\227\347\254\246\344\270\262.md" | 12 +++------ ...77\347\232\204\345\221\250\351\225\277.md" | 12 +++------ ...4.\344\270\200\345\222\214\351\233\266.md" | 12 +++------ ...36\345\255\220\345\272\217\345\210\227.md" | 12 +++------ ...4.\347\233\256\346\240\207\345\222\214.md" | 12 +++------ ...4\345\244\247\345\205\203\347\264\240I.md" | 12 +++------ ...55\347\232\204\344\274\227\346\225\260.md" | 12 +++------ ...\345\244\247\345\205\203\347\264\240II.md" | 12 +++------ ...42\351\202\243\345\245\221\346\225\260.md" | 12 +++------ ...13\350\247\222\347\232\204\345\200\274.md" | 12 +++------ ...07\345\255\220\345\272\217\345\210\227.md" | 12 +++------ ...\351\222\261\345\205\221\346\215\242II.md" | 12 +++------ ...17\347\273\235\345\257\271\345\267\256.md" | 12 +++------ ...72\347\264\257\345\212\240\346\240\221.md" | 12 +++------ ...\345\255\227\347\254\246\344\270\262II.md" | 12 +++------ ...40\351\231\244\346\223\215\344\275\234.md" | 12 +++------ ...66\344\272\214\345\217\211\346\240\221.md" | 12 +++------ ...36\346\226\207\345\255\220\344\270\262.md" | 12 +++------ ...a2\345\217\202\350\256\256\351\231\242.md" | 12 +++------ ...47\344\272\214\345\217\211\346\240\221.md" | 12 +++------ ...24\345\233\236\345\216\237\347\202\271.md" | 12 +++------ ...11\346\220\234\347\264\242\346\240\221.md" | 12 +++------ ...27\347\232\204\344\270\252\346\225\260.md" | 12 +++------ ...22\345\242\236\345\272\217\345\210\227.md" | 12 +++------ ...27\344\275\231\350\277\236\346\216\245.md" | 12 +++------ ...\344\275\231\350\277\236\346\216\245II.md" | 12 +++------ ...00\345\244\247\351\235\242\347\247\257.md" | 12 +++------ ...55\347\232\204\346\220\234\347\264\242.md" | 12 +++------ ...22\345\205\245\346\223\215\344\275\234.md" | 12 +++------ ...14\345\210\206\346\237\245\346\211\276.md" | 12 +++------ ...76\350\256\241\351\223\276\350\241\250.md" | 12 +++------ ...53\346\211\213\347\273\255\350\264\271.md" | 12 +++------ ...01\350\247\204\345\210\222\357\274\211.md" | 12 +++------ ...15\345\255\220\346\225\260\347\273\204.md" | 12 +++------ ...55\345\277\203\347\264\242\345\274\225.md" | 12 +++------ ...36\347\232\204\346\225\260\345\255\227.md" | 12 +++------ ...17\346\227\245\346\270\251\345\272\246.md" | 12 +++------ ...66\350\277\237\346\227\266\351\227\264.md" | 3 +++ ...71\347\210\254\346\245\274\346\242\257.md" | 12 +++------ ...27\346\257\215\345\214\272\351\227\264.md" | 12 +++------ ...34\347\232\204\350\210\252\347\217\255.md" | 12 +++------ ...75\347\232\204\350\267\257\345\276\204.md" | 12 +++------ ...47\344\272\272\345\267\245\345\262\233.md" | 12 +++------ ...31\345\222\214\346\210\277\351\227\264.md" | 12 +++------ ...04\345\255\227\347\254\246\344\270\262.md" | 12 +++------ ...54\346\260\264\346\211\276\351\233\266.md" | 12 +++------ ...\345\272\217\346\225\260\347\273\204II.md" | 12 +++------ ...77\346\214\211\351\224\256\345\205\245.md" | 12 +++------ ...61\350\204\211\346\225\260\347\273\204.md" | 12 +++------ ...47\344\272\214\345\217\211\346\240\221.md" | 12 +++------ ...04\347\232\204\345\271\263\346\226\271.md" | 12 +++------ ...70\347\224\250\345\255\227\347\254\246.md" | 12 +++------ ...04\346\225\260\347\273\204\345\222\214.md" | 12 +++------ ...60\347\232\204\346\225\260\351\207\217.md" | 12 +++------ ...70\344\272\244\347\232\204\347\272\277.md" | 12 +++------ ...73\351\207\215\345\244\215\351\241\271.md" | 12 +++------ ...\347\232\204\351\207\215\351\207\217II.md" | 12 +++------ ...61\345\255\220\345\272\217\345\210\227.md" | 12 +++------ ...72\347\216\260\346\254\241\346\225\260.md" | 12 +++------ ...41\345\255\227\347\254\246\344\270\262.md" | 12 +++------ ...77\347\232\204\346\225\260\347\233\256.md" | 12 +++------ ...21\347\232\204\345\237\216\345\270\202.md" | 12 +++------ ...60\347\233\256\346\216\222\345\272\217.md" | 12 +++------ ...27\347\232\204\346\225\260\345\255\227.md" | 12 +++------ ...21\345\217\230\345\271\263\350\241\241.md" | 12 +++------ ...55\345\277\203\350\212\202\347\202\271.md" | 12 +++------ ...30\345\234\250\350\267\257\345\276\204.md" | 12 +++------ ...57\345\244\232\345\244\247\357\274\237.md" | 12 +++------ ...44\270\262\346\216\245\351\276\231-03.png" | Bin 0 -> 56769 bytes problems/toolgithub.sh | 24 +++++++++++------- ...11\346\255\245\351\223\272\345\236\253.md" | 12 +++------ ...46\347\235\200\345\233\236\346\272\257.md" | 12 +++------ ...21\346\200\273\347\273\223\347\257\207.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...00\350\277\255\344\273\243\346\263\225.md" | 12 +++------ ...55\344\273\243\351\201\215\345\216\206.md" | 12 +++------ ...22\345\275\222\351\201\215\345\216\206.md" | 12 +++------ ...77\346\215\242\347\251\272\346\240\274.md" | 12 +++------ ...54\345\255\227\347\254\246\344\270\262.md" | 12 +++------ ...30\346\200\273\347\273\223\347\257\207.md" | 12 +++------ ...22\346\200\273\347\273\223\347\257\207.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...07\351\222\210\346\200\273\347\273\223.md" | 12 +++------ ...14\350\241\250\346\200\273\347\273\223.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...36\346\272\257\346\200\273\347\273\223.md" | 12 +++------ ...00\347\247\215\345\206\231\346\263\225.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...46\344\270\262\346\200\273\347\273\223.md" | 12 +++------ ...04\346\200\273\347\273\223\347\257\207.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...37\345\210\227\346\200\273\347\273\223.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...06\350\256\262\350\247\243\357\274\211.md" | 12 +++------ ...27\346\263\225\346\250\241\346\235\277.md" | 12 +++------ ...05\346\200\273\347\273\223\347\257\207.md" | 12 +++------ ...47\241\20001\350\203\214\345\214\205-1.md" | 12 +++------ ...47\241\20001\350\203\214\345\214\205-2.md" | 12 +++------ ...14\345\214\205\344\270\200\347\273\264.md" | 3 +++ ...32\351\207\215\350\203\214\345\214\205.md" | 12 +++------ ...14\345\205\250\350\203\214\345\214\205.md" | 12 +++------ ...25\346\200\273\347\273\223\347\257\207.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...50\346\200\273\347\273\223\347\257\207.md" | 12 +++------ ...06\350\256\272\345\237\272\347\241\200.md" | 12 +++------ ...76\350\241\250\347\233\270\344\272\244.md" | 12 +++------ 224 files changed, 681 insertions(+), 1980 deletions(-) create mode 100644 "problems/images/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231-03.png" diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index e982ae129b..6be92fa81b 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1. 两数之和 @@ -557,7 +555,3 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){ } ``` -

- - - diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index b3d3b93896..4ce49810b3 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -731,8 +729,4 @@ public class Solution { ``` -

- - - diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 1685db7452..52dbdab7b5 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -980,7 +978,3 @@ object Solution { } ``` -

- - - diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index 9a320ca0e6..93f41e0f74 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 17.电话号码的字母组合 @@ -766,7 +764,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index f3188b0f13..bf7d3bd4ee 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 一样的道理,能解决四数之和 @@ -798,8 +796,4 @@ def four_sum(nums, target) end ``` -

- - - diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 16312d0fc4..53b59039e5 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -479,7 +477,3 @@ public class Solution { } } ``` -

- - - diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index 2475138ebb..c642fb4ecd 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -574,8 +572,4 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index 305bb7ccba..b9494297e4 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 24. 两两交换链表中的节点 @@ -527,7 +525,3 @@ public ListNode SwapPairs(ListNode head) } ``` -

- - - diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index 10817ba6b7..d01765ff66 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 27. 移除元素 @@ -519,7 +517,3 @@ int removeElement(List nums, int val) { ``` -

- - - diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 63a08d960d..b25cb301f9 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 在一个串中查找是否出现过另一个串,这是KMP的看家本领。 @@ -1520,7 +1518,3 @@ int strStr(char* haystack, char* needle) { } ``` -

- - - diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" index 48af8d0da1..95bb1d899e 100644 --- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" +++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -269,7 +267,3 @@ var nextPermutation = function(nums) { -

- - - diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index 16adcdf19c..37248e4819 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 34. 在排序数组中查找元素的第一个和最后一个位置 @@ -855,7 +853,3 @@ int* searchRange(int* nums, int numsSize, int target, int* returnSize){ ``` -

- - - diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index b5be9a5f8b..e0b065cd5a 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -549,7 +547,3 @@ int searchInsert(int* nums, int numsSize, int target){ ``` -

- - - diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index 5f3f881cf1..b26bf53308 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -893,7 +891,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 92c68562da..455bd697cd 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -661,7 +659,3 @@ public class Solution -

- - - diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 22cf726d8c..281db4dc86 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 这篇可以说是全网把组合问题如何去重,讲的最清晰的了! @@ -807,7 +805,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 0484f830f6..8a424e3ea2 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1095,7 +1093,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 8919d39367..a20eb2a6a9 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心理准备! @@ -542,7 +540,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index ca465efd6f..611a4cb1e0 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 46.全排列 @@ -521,8 +519,4 @@ public class Solution ``` -

- - - diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index 08e3c616a2..1e51a7bcd7 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -555,7 +553,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 38fc07e790..b201b55fe4 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 51. N皇后 @@ -921,7 +919,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index 271484a4ee..11c257b073 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -307,7 +305,3 @@ class Solution { } ``` -

- - - diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index 705a00d7a8..6f8c2a6e7e 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 53. 最大子序和 @@ -492,7 +490,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 38a3a11818..568626dc6d 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 53. 最大子序和 @@ -244,7 +242,3 @@ function maxSubArray(nums: number[]): number { -

- - - diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 3b7afb9099..7d7f460f01 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -485,7 +483,3 @@ func min(x, y int) int { ``` -

- - - diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index 896dc998d1..3ab004b2aa 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 55. 跳跃游戏 @@ -293,7 +291,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 538be69336..76792dbae1 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 56. 合并区间 @@ -405,7 +403,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 94966126b9..9961c0e70b 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -829,7 +827,3 @@ def generate_matrix(n) end ``` -

- - - diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 7025135ade..23cd8060fa 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -616,7 +614,3 @@ public class Solution -

- - - diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 13923abeca..61d9329d4b 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -781,7 +779,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 6a13a21cdb..92c3858698 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 70. 爬楼梯 @@ -520,8 +518,4 @@ impl Solution { ``` -

- - - diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index c51a590baf..a5435ddd71 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 70. 爬楼梯(进阶版) @@ -252,7 +250,3 @@ var climbStairs = function (n: number): number { ### Rust: -

- - - diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 0da3bf5093..408999d873 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 72. 编辑距离 @@ -462,7 +460,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index c4be5a3804..60900d76a6 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 第77题. 组合 @@ -876,7 +874,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index 0fa568afe9..e2ca3d7d0a 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -412,7 +410,3 @@ object Solution { ``` -

- - - diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 0c368b41ed..73eb385bc1 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 78.子集 @@ -492,7 +490,3 @@ public class Solution { -

- - - diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 5c6f407321..6577cf542d 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 84.柱状图中最大的矩形 @@ -863,7 +861,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 811d3cc005..3bda02bc9e 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 90.子集II @@ -697,7 +695,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index a03a0e3b15..1a89827897 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -876,7 +874,3 @@ public class Solution -

- - - diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 25d79aff74..f4e0e456b7 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -348,7 +346,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index fb4ca7d88d..22a47f9632 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 98.验证二叉搜索树 @@ -807,7 +805,3 @@ public bool IsValidBST(TreeNode root) ``` -

- - - diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" index 7268b9f083..52c9fcf2e7 100644 --- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" +++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -340,7 +338,3 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { -

- - - diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 31c24fc5aa..f066408408 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 101. 对称二叉树 @@ -946,8 +944,4 @@ public bool IsSymmetric(TreeNode root) ``` -

- - - diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index ce53e49af9..da2d85c99d 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -3601,7 +3599,3 @@ impl Solution { **致敬叶师傅!** -

- - - diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index fdc9009603..6b9994ed93 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1195,7 +1193,3 @@ public int MaxDepth(TreeNode root) -

- - - diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index bde61a7551..d0af8fef27 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1351,7 +1349,3 @@ public TreeNode BuildTree(int[] inorder, int[] postorder) ``` -

- - - diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 4804ccd3b3..adb2a06082 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 构造二叉搜索树,一不小心就平衡了 @@ -562,7 +560,3 @@ public TreeNode Traversal(int[] nums, int left, int right) -

- - - diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index a4339ac3d7..c3da728077 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -998,7 +996,3 @@ public int GetHeight(TreeNode root) ``` -

- - - diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 708e0532f8..a77594b28f 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 和求最大深度一个套路? @@ -752,7 +750,3 @@ public int MinDepth(TreeNode root) } ``` -

- - - diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 141967f593..22ed777fba 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1624,8 +1622,4 @@ public class Solution { ``` -

- - - diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index cf24c4c180..832b64d183 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 115.不同的子序列 @@ -376,7 +374,3 @@ impl Solution { -

- - - diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" index 98bd4e41c1..234929f3d1 100644 --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 116. 填充每个节点的下一个右侧节点指针 @@ -489,8 +487,4 @@ public class Solution -

- - - diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index f8092503e3..b9df47a491 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 121. 买卖股票的最佳时机 @@ -627,7 +625,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index 6663a66d12..b268040771 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 122.买卖股票的最佳时机 II @@ -423,7 +421,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 0dced9efd4..d8cb308b7e 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 122.买卖股票的最佳时机II @@ -477,7 +475,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 1b7c09d214..75f7cb3f20 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 123.买卖股票的最佳时机III @@ -565,7 +563,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 00d7d4cfc4..556613e587 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 127. 单词接龙 @@ -360,7 +358,3 @@ function diffonechar(word1: string, word2: string): boolean { ``` -

- - - diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" index 90dfd0618f..923bc63807 100644 --- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" +++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -383,7 +381,3 @@ int sumNumbers(struct TreeNode* root){ ``` -

- - - diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index 8ef8d5b280..4eeb57143e 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 130. 被围绕的区域 @@ -793,7 +791,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 4eca0ddf53..f9b5d244c5 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 切割问题其实是一种组合问题! @@ -1007,7 +1005,3 @@ public class Solution -

- - - diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" index 85e047f2b7..2117a44801 100644 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -373,7 +371,3 @@ var minCut = function(s) { -

- - - diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index 0248760da6..fdf3e0d3db 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 134. 加油站 @@ -709,7 +707,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index eb2081fe3f..75cce157c6 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 135. 分发糖果 @@ -401,7 +399,3 @@ public class Solution -

- - - diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 29748e2780..b74d2cdfa0 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -565,7 +563,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" index ac6565763f..4957e9fbd5 100644 --- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" +++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 141. 环形链表 @@ -160,7 +158,3 @@ function hasCycle(head: ListNode | null): boolean { -

- - - diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index 7cda58c396..fb8b875f7d 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -465,7 +463,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" index ccddef5bf0..c61eb4b403 100644 --- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" +++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 143.重排链表 @@ -689,7 +687,3 @@ void reorderList(struct ListNode* head){ ``` -

- - - diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index bc73f6da0d..6d21452d1d 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 这不仅仅是一道好题,也展现出计算机的思考方式 @@ -550,7 +548,3 @@ int evalRPN(char** tokens, int tokensSize) { } ``` -

- - - diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 3dbd59b916..b5246a7dbb 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1086,8 +1084,4 @@ public string ReverseWords(string s) { } ``` -

- - - diff --git "a/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" "b/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" index d4422bd8ba..cdc58912fe 100644 --- "a/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" +++ "b/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" @@ -1,11 +1,5 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) 同:[链表:链表相交](https://programmercarl.com/面试题02.07.链表相交.html) -

- - - diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index cbba12c9d1..b182d4d03d 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 188.买卖股票的最佳时机IV @@ -641,8 +639,4 @@ impl Solution { -

- - - diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" index e91109c692..976cbed4d1 100644 --- "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" +++ "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 189. 旋转数组 @@ -212,7 +210,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 032204bbce..3d06c95241 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 198.打家劫舍 @@ -361,7 +359,3 @@ impl Solution { -

- - - diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 00e4efd894..4901934bfe 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 200. 岛屿数量 @@ -410,7 +408,3 @@ impl Solution { ``` ``` -

- - - diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 4657920334..a3f6f48c76 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 200. 岛屿数量 @@ -463,7 +461,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 39cb39fa31..fdcadee97c 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -558,7 +556,3 @@ def next_value(n) end ``` -

- - - diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 5a4bbb7423..9a38aaa152 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -808,7 +806,3 @@ end ``` -

- - - diff --git "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" index e416d9ceda..ba255e0685 100644 --- "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 205. 同构字符串 @@ -180,7 +178,3 @@ function isIsomorphic(s: string, t: string): boolean { -

- - - diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index 430bebe59b..7509882f08 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。 @@ -739,7 +737,3 @@ public ListNode reverseList(ListNode head) { > 采用这种方法需要注意一点。就是当整个出栈循环结束以后,cur正好指向原来链表的第一个结点,而此时结点1中的next指向的是结点2,因此最后还需要`cur.next = null` ![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png) -

- - - diff --git "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" index dff0b18eb0..f992c72b89 100644 --- "a/problems/0207.\350\257\276\347\250\213\350\241\250.md" +++ "b/problems/0207.\350\257\276\347\250\213\350\241\250.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) 拓扑排序指的是一种 解决问题的大体思路, 而具体算法,可能是 广搜 可能是深搜。 @@ -59,7 +57,3 @@ public: } }; ``` -

- - - diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index c6d89976d0..43a3cb6a2b 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 209.长度最小的子数组 @@ -558,7 +556,3 @@ public class Solution { } } ``` -

- - - diff --git "a/problems/0210.\350\257\276\347\250\213\350\241\250II.md" "b/problems/0210.\350\257\276\347\250\213\350\241\250II.md" index 2d2e242941..b0d9fe8a9e 100644 --- "a/problems/0210.\350\257\276\347\250\213\350\241\250II.md" +++ "b/problems/0210.\350\257\276\347\250\213\350\241\250II.md" @@ -1,3 +1,6 @@ +* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) ```CPP class Solution { diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index 05ebd1ad09..536e1e89b8 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 213.打家劫舍II @@ -367,7 +365,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index 3d7f2d0c03..3dbd676a8d 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -739,7 +737,3 @@ public class Solution -

- - - diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index 8d7779f989..9b649d7be8 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 222.完全二叉树的节点个数 @@ -894,7 +892,3 @@ public int CountNodes(TreeNode root) ``` -

- - - diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 73d9db1b16..2396858056 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1367,7 +1365,3 @@ void myStackFree(MyStack* obj) { ``` -

- - - diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index c34ca4bfcb..0980e6004c 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 226.翻转二叉树 @@ -1022,7 +1020,3 @@ public TreeNode InvertTree(TreeNode root) { ``` -

- - - diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 657567cfdf..6775a37265 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 工作上一定没人这么搞,但是考察对栈、队列理解程度的好题 @@ -691,7 +689,3 @@ impl MyQueue { } ``` -

- - - diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" index 1356b7da3b..f493383967 100644 --- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" +++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 234.回文链表 @@ -429,7 +427,3 @@ function reverseList(head: ListNode | null): ListNode | null { -

- - - diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 3911261a53..c5eb603a0d 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 235. 二叉搜索树的最近公共祖先 @@ -548,7 +546,3 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) -

- - - diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 8cd505a829..f15d1cff60 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 本来是打算将二叉树和二叉搜索树的公共祖先问题一起讲,后来发现篇幅过长了,只能先说一说二叉树的公共祖先问题。 @@ -491,7 +489,3 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) ``` -

- - - diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index 651e4da40c..875f1bd193 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -922,8 +920,4 @@ int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) { ``` -

- - - diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 61488f03fd..9a783e5b11 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 数组就是简单的哈希表,但是数组的大小可不是无限开辟的 @@ -416,7 +414,3 @@ bool isAnagram(char* s, char* t) { -

- - - diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index fdaa87f896..287db20937 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 以为只用了递归,其实还用了回溯 @@ -938,7 +936,3 @@ public void Traversal(TreeNode node, List path, List res) } ``` -

- - - diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index dc5a7e9ec9..c1077bd43d 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 279.完全平方数 @@ -479,7 +477,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index cbce029576..d7911054e4 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 283. 移动零:动态规划:一样的套路,再求一次完全平方数 @@ -186,7 +184,3 @@ impl Solution { -

- - - diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 442938c06f..7d2e488623 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 300.最长递增子序列 @@ -361,7 +359,3 @@ func lengthOfLIS(nums: Array): Int64 { ``` -

- - - diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index b98a416cc1..6a81933505 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 309.最佳买卖股票时机含冷冻期 @@ -603,8 +601,4 @@ impl Solution { ``` -

- - - 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 e55e20bedf..dea77a3d10 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" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 322. 零钱兑换 @@ -499,7 +497,3 @@ function coinChange(coins: number[], amount: number): number { ``` -

- - - diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 78e1407419..f1df25229d 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 这也可以用回溯法? 其实深搜和回溯也是相辅相成的,毕竟都用递归。 @@ -941,7 +939,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index a3130df7ee..08728e4faf 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 337.打家劫舍 III @@ -623,7 +621,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 5d0110f67e..06549185a3 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 343. 整数拆分 @@ -564,7 +562,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 793c9af362..c88d008c82 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -429,7 +427,3 @@ object Solution { } } ``` -

- - - diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index cca9b0edce..b6575c5fd4 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 前K个大数问题,老生常谈,不得不谈 @@ -609,8 +607,4 @@ impl Solution { ``` -

- - - diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 93fa09318a..5066bff1b9 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -544,8 +542,4 @@ end * [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/) -

- - - diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index e2ea99046b..886d86aefe 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 376. 摆动序列 @@ -714,7 +712,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index ba8546c662..d2feb0c5d6 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 377. 组合总和 Ⅳ @@ -357,7 +355,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index 1d7391732f..8a2f52ae42 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -466,8 +464,4 @@ bool canConstruct(char* ransomNote, char* magazine) { ``` -

- - - diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 92246e4f9b..2a5be51c59 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 392.判断子序列 @@ -405,7 +403,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 66aff68f2e..0efdb6f663 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 404.左叶子之和 @@ -685,7 +683,3 @@ public int SumOfLeftLeaves(TreeNode root) ``` -

- - - diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index d6fc415b68..11853e1170 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 406.根据身高重建队列 @@ -422,7 +420,3 @@ public class Solution -

- - - diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 902c022ab3..9cc6db0e4b 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 416. 分割等和子集 @@ -801,7 +799,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index 5156ce2289..ec87eb9595 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -837,7 +835,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index d6321315d7..a37d1cadac 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 435. 无重叠区间 @@ -495,7 +493,3 @@ public class Solution -

- - - diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 831655e8e1..7280918460 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 二叉搜索树删除节点就涉及到结构调整了 @@ -836,7 +834,3 @@ def delete_node(root, key) end ``` -

- - - diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 14456f92bf..854498829b 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 452. 用最少数量的箭引爆气球 @@ -357,7 +355,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index af19f5f7d8..a26071a1fa 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 需要哈希的地方都能找到map的身影 @@ -526,7 +524,3 @@ def two_sum_mapping(nums1, nums2) end ``` -

- - - diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 5e8fb73094..a2a1b1f339 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 455.分发饼干 @@ -434,7 +432,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 988b2abf11..78aad3e786 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > KMP算法还能干这个 @@ -999,7 +997,3 @@ bool repeatedSubstringPattern(char* s) { ``` -

- - - diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 5261d6c2ae..bff619ccde 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -433,7 +431,3 @@ function islandPerimeter(grid: number[][]): number { ``` -

- - - diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 9d24f01434..ca525ab2e3 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 474.一和零 @@ -740,8 +738,4 @@ public class Solution ``` -

- - - diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 7832095a10..1b927dd3d2 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 和子集问题有点像,但又处处是陷阱 @@ -640,7 +638,3 @@ public class Solution { } ``` -

- - - diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index c38ba7e43c..bde843ead3 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -1024,8 +1022,4 @@ public class Solution ``` -

- - - diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 02e73a588d..628149b75d 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 496.下一个更大元素 I @@ -507,7 +505,3 @@ impl Solution { -

- - - diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index c89f8031e8..32a89e859a 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 二叉树上应该怎么求,二叉搜索树上又应该怎么求? @@ -1052,7 +1050,3 @@ public class Solution -

- - - diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index b466337d6e..93924483f3 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 503.下一个更大元素II @@ -358,7 +356,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index ac173dbddc..b2e56a613c 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 509. 斐波那契数 @@ -476,7 +474,3 @@ public class Solution -

- - - diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index c7446726f8..da37360374 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 513.找树左下角的值 @@ -764,7 +762,3 @@ public class Solution // @lc code=end ``` -

- - - diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index 166310aaff..f0ef2f53d0 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -299,7 +297,3 @@ impl Solution { -

- - - diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 835df85212..1698db9887 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 518.零钱兑换II @@ -589,7 +587,3 @@ public class Solution ``` -

- - - diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index b6d08dbeaa..d7b0e056db 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 利用二叉搜索树的特性搞起! @@ -679,8 +677,4 @@ public class Solution ``` -

- - - diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index b95b585485..1bbbdac76d 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 538.把二叉搜索树转换为累加树 @@ -549,7 +547,3 @@ public class Solution -

- - - diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index b3e7b02229..2bbe6cffae 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -516,8 +514,4 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index b9f9ad9625..a86dfad195 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 583. 两个字符串的删除操作 @@ -470,7 +468,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 530350ac69..f180c4f3b9 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 617.合并二叉树 @@ -804,7 +802,3 @@ public TreeNode MergeTrees(TreeNode root1, TreeNode root2) ``` -

- - - diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index cf32d7ed24..e2783027aa 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 647. 回文子串 @@ -613,7 +611,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" index 1540a60173..e77070fcf8 100644 --- "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" +++ "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -284,7 +282,3 @@ function predictPartyVictory(senate: string): string { -

- - - diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index fed9b2b991..9f897a7502 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 654.最大二叉树 @@ -599,7 +597,3 @@ public TreeNode ConstructMaximumBinaryTree(int[] nums) ``` -

- - - diff --git "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" index eccfef3a21..89993b6ff6 100644 --- "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" +++ "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 657. 机器人能否返回原点 @@ -182,7 +180,3 @@ var judgeCircle = function (moves) { -

- - - diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 325733862c..0a05360bf1 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -587,7 +585,3 @@ public TreeNode TrimBST(TreeNode root, int low, int high) -

- - - diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" index 0366ee8063..9bfa91cc94 100644 --- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" +++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 673.最长递增子序列的个数 @@ -361,7 +359,3 @@ var findNumberOfLIS = function(nums) { -

- - - diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 8b967092bc..2c490c0c5b 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 674. 最长连续递增序列 @@ -514,7 +512,3 @@ func findLengthOfLCIS(nums: Array): Int64 { -

- - - diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index 7808549036..e6d2d8e502 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 684.冗余连接 @@ -379,7 +377,3 @@ var findRedundantConnection = function(edges) { -

- - - diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 3f489d82b7..7b0e320c6f 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 685.冗余连接II @@ -620,7 +618,3 @@ var findRedundantDirectedConnection = function(edges) { -

- - - diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index ca70420687..0b84e651c1 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 695. 岛屿的最大面积 @@ -709,7 +707,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 9ec51524b8..4225b3fe25 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 700.二叉搜索树中的搜索 @@ -508,7 +506,3 @@ public TreeNode SearchBST(TreeNode root, int val) ``` -

- - - diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 25d39486f3..ef383faa86 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 701.二叉搜索树中的插入操作 @@ -724,7 +722,3 @@ public TreeNode InsertIntoBST(TreeNode root, int val) { ``` -

- - - diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index d86146d63a..405018745e 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 704. 二分查找 @@ -837,7 +835,3 @@ class Solution { ``` -

- - - diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 5c72b05a29..7023bd902a 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 听说这道题目把链表常见的五个操作都覆盖了? @@ -1845,8 +1843,4 @@ public class MyLinkedList } ``` -

- - - diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" index e742b8c89b..fb095d7518 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 714. 买卖股票的最佳时机含手续费 @@ -361,7 +359,3 @@ object Solution { ``` -

- - - diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 17997b6208..ebed4a0b30 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 714.买卖股票的最佳时机含手续费 @@ -337,7 +335,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 1391926a76..0e4b346d82 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 718. 最长重复子数组 @@ -602,7 +600,3 @@ func findLength(nums1: Array, nums2: Array): Int64 { ``` -

- - - diff --git "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" index a66a445083..bccca4f2d4 100644 --- "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" +++ "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 724.寻找数组的中心下标 @@ -159,7 +157,3 @@ function pivotIndex(nums: number[]): number { -

- - - diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index f2cfee04c9..17182778ae 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 738.单调递增的数字 @@ -441,8 +439,4 @@ public class Solution } ``` -

- - - diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index dd633aed9a..542aad29b8 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -515,7 +513,3 @@ impl Solution { -

- - - diff --git "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" index e631951a9e..6533a240bf 100644 --- "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" +++ "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" @@ -1,3 +1,6 @@ +* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 743.网络延迟时间 diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 753a104d97..9145c7ed1d 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -538,7 +536,3 @@ public class Solution -

- - - diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 19d862db14..70ebfe4ff7 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 763.划分字母区间 @@ -462,7 +460,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" index 9c0a8e7f27..68d8421502 100644 --- "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" +++ "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 787. K 站中转内最便宜的航班 @@ -180,7 +178,3 @@ public: -

- - - diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index 40e1bbe73e..a37e5c3f8c 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 797.所有可能的路径 @@ -294,7 +292,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index d24eaacc50..0ebda2524a 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 827.最大人工岛 @@ -504,7 +502,3 @@ return res; ``` -

- - - diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index b78693b446..4076fce513 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -484,7 +482,3 @@ function canVisitAllRooms(rooms: number[][]): boolean { ``` -

- - - diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" index c32cdd339b..f229447384 100644 --- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 844.比较含退格的字符串 @@ -588,7 +586,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index b7887d456e..aeb470fe5a 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 860.柠檬水找零 @@ -440,7 +438,3 @@ public class Solution -

- - - diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" index 28680dbf0f..484099f89f 100644 --- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" +++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -410,7 +408,3 @@ function sortArrayByParityII(nums: number[]): number[] { ``` -

- - - diff --git "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" index f4a8fa8e52..47465199a8 100644 --- "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" +++ "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 925.长按键入 @@ -227,7 +225,3 @@ function isLongPressedName(name: string, typed: string): boolean { -

- - - diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" index 77167df041..383f6aa5b1 100644 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 941.有效的山脉数组 @@ -213,7 +211,3 @@ impl Solution { } ``` -

- - - diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index 327c54f72a..0df2cc5b60 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -789,7 +787,3 @@ public class Solution } ``` -

- - - diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index b8488e10de..6e58be1af6 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 双指针风骚起来,也是无敌 @@ -588,8 +586,4 @@ public class Solution { } ``` -

- - - diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index f938c2b734..3d7d8e0770 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -582,7 +580,3 @@ end ``` -

- - - diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 1a7817775a..6e908d5af6 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1005.K次取反后最大化的数组和 @@ -377,7 +375,3 @@ public class Solution -

- - - diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index f708e4a368..030d56a024 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1020. 飞地的数量 @@ -753,8 +751,4 @@ impl Solution { -

- - - diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 5164e1f7ff..53e0f370e7 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1035.不相交的线 @@ -278,7 +276,3 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { -

- - - diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 51ec4e62c6..01d33fbff7 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 匹配问题都是栈的强项 @@ -523,7 +521,3 @@ end ``` -

- - - diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 0d445a71f0..62e7d9c590 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1049.最后一块石头的重量II @@ -535,7 +533,3 @@ public class Solution ``` -

- - - diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 6d05ccf3f8..821f3c42a1 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1143.最长公共子序列 @@ -420,7 +418,3 @@ func longestCommonSubsequence(text1: String, text2: String): Int64 { ``` -

- - - diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index 781badf549..fbb19af773 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1207.独一无二的出现次数 @@ -247,7 +245,3 @@ impl Solution { -

- - - diff --git "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" index a32ca98ffc..a9e275d92d 100644 --- "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1221. 分割平衡字符串 @@ -172,7 +170,3 @@ function balancedStringSplit(s: string): number { }; ``` -

- - - diff --git "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" index 3d7b9fe96c..5d99670950 100644 --- "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" +++ "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1254. 统计封闭岛屿的数目 @@ -136,7 +134,3 @@ var closedIsland = function(grid) { -

- - - diff --git "a/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" "b/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" index d8d8861f47..bea47a2e63 100644 --- "a/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" +++ "b/problems/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) floyd @@ -49,7 +47,3 @@ public: return result; } }; -

- - - diff --git "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" index 9cfb674328..0ae1603494 100644 --- "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" +++ "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -217,7 +215,3 @@ var sortByBits = function(arr) { -

- - - diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index f0a77f5587..2cb73f728a 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -311,7 +309,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" index 120cafffd3..7a1a7f3cb9 100644 --- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" +++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1382.将二叉搜索树变平衡 @@ -218,7 +216,3 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null -

- - - diff --git "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" index e3db794706..e9ea5f4463 100644 --- "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" +++ "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1791.找出星型图的中心节点 @@ -77,7 +75,3 @@ public: ``` -

- - - diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 93e9b66365..acb544155a 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 1971. 寻找图中是否存在路径 @@ -335,7 +333,3 @@ func validPath(n int, edges [][]int, source int, destination int) bool { ``` -

- - - diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index a5dab942c8..7276af53b3 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 程序提交之后为什么会超时?O(n)的算法会超时,n究竟是多大? @@ -224,7 +222,3 @@ int main() { -

- - - diff --git "a/problems/images/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231-03.png" "b/problems/images/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231-03.png" new file mode 100644 index 0000000000000000000000000000000000000000..7ad2ced6903b82f6c6db2d8d85746df9097fc01a GIT binary patch literal 56769 zcma(31yodR_dbr(AxcU}3lD;XG)SYQNOuoNgMf4m3!;saNpZDiJhDl5q~phT^_*1cD@( z8S+Bl?X!XCo3v-zDCiD{vtfeVIfc(@`XqXI^5f-c#~OUEAwMMMt=eaO6~fEeJRdJ2 zSD(c&+D9)3;HIdp%{c}ZzpyCpz`Zr8vA{f{MbN9O_qTs=B;0PK=21_+<(QktJJQUQ zJsxL}e(>rso_e-$yGCwu%df zSI!zqaTqJ#_=vGcL+Du5E011xLo_PszvF^_I6}f#=DOu(BYF{2no>yD6(gu3_H>2Y z31dThL|gpab_v-G2*kfo<5+-o3^KAGkZzf$ZDpFLUHn|yU2J`R2}h_>wS;%?Y>lwJ z&bp;N9L{S!Fju3kf9U0Ap6@h_Y5IY7oc#^!n{U1s(kiF$2G<8~xxDIxn;FM0RSb2=r10WS4NVPM^+rwF=<|EpXav_u zs;AUVIBYxqV@7Jp;@CYio@76C+X~v8ECFruYCnXddm%%1X4R{>cewi1A9&W<>apFM8z0)jbueU-o#Xg-5NACqu-+@73F5$-_tsi`!ECx3!?H-NWZrZ{Rn?vu>Ft4k>6aVe|s$ z+CTVkmI{!Eug&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SazVaDR|E$LCfxRZYTpwT(=;%cY7P0Z-QkZBwsjJ;J9IyeMI<=29?A1n^*{WHo z>hp`mW=WsD{@*<3;{gj#iytfHqsC=?Oy`MgNY7nGwUjKm#5wQ#990*vv<%#W196*! ziv-d-4SL1S?ll%rK|6@Hs*^%~9xUoQUTV5eXj0fMwEw;ycpGz$KMv^M%Kjte?bn@;_M+wgT~5;C)=IFSvbTNH#5H5dYve)mTTN-D+aZPKjTM{3bu$k9M{yPk$65^ zvFPfE&q0y1LVa4qJJ-*l?^0Pt2Qx?o+iWQARxg(N_>NkfJUdYBn&DIBh%`Np{2%KA ziW+Dxqr=x<^>K!v?#usD_bOF<^UUT~(aNo+=UJN)oTy}cBge8Fy?@6022Z(wR9SPBejXg_Ou>Z92WFfv zBexQmZaVvREB?9 zulv)y%dW%pR;>ueLi2wFQQ*J%HCID6$r;nNEGL!d(C+LpfVom9peBkJMc>1Qj8wM~ z(!TS(BDM9t()rI>6L#Wg)~~IdygY@th?;OWyZ8dkjjNdp)y0g+H;Sd&>^!Bt6nS;a zpxVjzeC~po2pJ4WMF!3Cw9z`_>bg;xMP2sB`+Qs58Th2`JgfIH;Z#GK{^u6O zaFR}l%<|Oh1TMOuc9yf32Jl9K91W6UEevA@&$lKpF?sn$iS znf&j@abACZU5s{i+2x~iQm52TjreuS`s%u)*mL*MY^baliL+4a{yF02W3~6`p4{i5 zzl=;bv;^Y|oF`hOubF0~%&5BaXNoce5}%E(Lne~F{%Ml3IZqWBuJUktS-n?@Wj>{h z>{0p2*cH9_=cA-*_gAZWY`KvyeRcgR=thZPm_2SSlyk(C3jBA参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

' -cx $i - ex -sc '1i|' -cx $i - ex -sc '1i| ' -cx $i - ex -sc '1i|' -cx $i - ex -sc '1i|

' -cx $i + # ex -sc '1i|

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

' -cx $i + # ex -sc '1i|' -cx $i + # ex -sc '1i| ' -cx $i + # ex -sc '1i|' -cx $i + # ex -sc '1i|

' -cx $i + + ex -sc '1i|* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)' -cx $i + ex -sc '1i|* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)' -cx $i + ex -sc '1i|* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)' -cx $i + + # echo '## 其他语言版本' >> $i # echo '\n' >> $i # echo 'Java:' >> $i @@ -48,10 +54,10 @@ do # 添加结尾 - echo '

' >> $i - echo '' >> $i - echo ' ' >> $i - echo '' >> $i + # echo '

' >> $i + # echo '' >> $i + # echo ' ' >> $i + # echo '' >> $i # echo '-----------------------' >> $i diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" index c2f5efd96d..69d6aa9c45 100644 --- "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" +++ "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划之编辑距离总结篇 @@ -196,7 +194,3 @@ class Solution { -

- - - diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" index 42d78ae39f..7fa2b6ec94 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 二叉树:以为使用了递归,其实还隐藏着回溯 @@ -687,7 +685,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" index 4794233ab5..f4d093d638 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 二叉树:总结篇!(需要掌握的二叉树技能都在这里了) @@ -158,8 +156,4 @@ -

- - - diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index c665827791..9a63b66cfd 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -314,7 +312,3 @@ public class TreeNode } ``` -

- - - diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index a6d4e3ffc3..d001e0f7a7 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 统一写法是一种什么感觉 @@ -970,8 +968,4 @@ public IList PostorderTraversal(TreeNode root) ``` -

- - - diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index a3c5b38fa0..289c651b60 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 听说还可以用非递归的方式 @@ -797,7 +795,3 @@ public IList PostorderTraversal(TreeNode root) } ``` -

- - - diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 8f61b8c69a..ffa3ff6cf8 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 一看就会,一写就废! @@ -728,7 +726,3 @@ function traversal($root, array &$output) { ``` -

- - - diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index f5803cb452..fd2d16f47a 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 替换数字 @@ -171,7 +169,3 @@ for (int i = 0; i < a.size(); i++) { -

- - - diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index e32f4ce15d..6fe12e3d51 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 右旋字符串 @@ -176,7 +174,3 @@ int main() { -

- - - diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index 7927deb7d7..314fb471b1 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # Leetcode股票问题总结篇! @@ -474,7 +472,3 @@ public: -

- - - diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index dd1646d6d8..faca5ecb55 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划最强总结篇! @@ -132,7 +130,3 @@ 所以Carl花费的这么大精力,把自己对动规算法理解 一五一十的全部分享给了录友们,帮助大家少走弯路,加油! -

- - - diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" index 9ffb453377..634f710f9a 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划理论基础 @@ -132,7 +130,3 @@ -

- - - diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" index 409e80ab98..9c92e3d6c3 100644 --- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" +++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 又是一波总结 @@ -95,7 +93,3 @@ for (int i = 0; i < array.size(); i++) { 建议大家可以把文中涉及到的题目在好好做一做,琢磨琢磨,基本对双指针法就不在话下了。 -

- - - diff --git "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" index cde23ad138..55caffe4f0 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 哈希表总结篇如约而至 @@ -126,7 +124,3 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 -

- - - diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 825c465719..b7d10671e1 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -132,7 +130,3 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 -

- - - diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index 8d9b78c4b9..b8d96193ee 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -448,7 +446,3 @@ N皇后问题分析: -

- - - diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 96dfeffec3..f1e1570a61 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 回溯算法去重问题的另一种写法 @@ -709,7 +707,3 @@ impl Solution { ``` -

- - - diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index 862fb101a1..474fb8f749 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 回溯算法理论基础 @@ -176,7 +174,3 @@ void backtracking(参数) { -

- - - diff --git "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" index 7da9791400..460944c5d2 100644 --- "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" +++ "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 字符串:总结篇 @@ -124,7 +122,3 @@ KMP算法是字符串查找最重要的算法,但彻底理解KMP并不容易 -

- - - diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index 7c2fd94724..e45165a625 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 数组总结篇 @@ -135,7 +133,3 @@ 推荐的题目即使大家之前做过了,再读一遍文章,也会帮助你提炼出解题的精髓所在。 -

- - - diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index e6d25c15e6..c1ac287d0f 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -119,7 +117,3 @@ public static void test_arr() { -

- - - diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index df022c779b..2d09daeb94 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 栈与队列总结篇 @@ -160,7 +158,3 @@ cd a/b/c/../../ -

- - - diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index 21c61a4c8b..bff0ec634b 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) > 来看看栈和队列不为人知的一面 @@ -93,7 +91,3 @@ std::queue> third; // 定义以list为底层容器的队列 -

- - - diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 70a9a97a19..a2350835d6 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -209,7 +207,3 @@ Go中slice的`append`操作和C++中vector的扩容机制基本相同。 -

- - - diff --git "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" index 0d32cebb2d..068806d622 100644 --- "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" +++ "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 算法模板 ## 算法模板 @@ -853,7 +851,3 @@ Go: -

- - - diff --git "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" index 651a92a804..3c587b6d2e 100644 --- "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 听说背包问题很难? 这篇总结篇来拯救你了 @@ -103,7 +101,3 @@ -

- - - diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index d9b953c0b3..c2598ec99a 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -593,7 +591,3 @@ int main() { -

- - - diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index b5862bb52b..6caa4f6327 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划:01背包理论基础(滚动数组) @@ -461,7 +459,3 @@ int main() { ``` -

- - - diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" index a8e241c3ba..5a23b67c73 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" @@ -1,3 +1,6 @@ +* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 完全背包-一维数组 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index 878efc1297..39e7ebe378 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 动态规划:关于多重背包,你该了解这些! @@ -237,7 +235,3 @@ print(dp[-1]) -

- - - diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index ea658f7e51..d76ff196b1 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 完全背包理论基础-二维DP数组 @@ -362,7 +360,3 @@ readline.on('close', () => { ``` -

- - - diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" index 14d82151c9..7d4c57e877 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 贪心算法总结篇 @@ -151,7 +149,3 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 -

- - - diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index 6fde2dbbe6..14f397296f 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 关于贪心算法,你该了解这些! @@ -92,7 +90,3 @@ 最后给出贪心的一般解题步骤,大家可以发现这个解题步骤也是比较抽象的,不像是二叉树,回溯算法,给出了那么具体的解题套路和模板。 -

- - - diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index 7da0d2de3c..e29ba268a3 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 链表总结篇 @@ -98,7 +96,3 @@ -

- - - diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index d131380728..3637d05f5a 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) @@ -275,7 +273,3 @@ public class Node -

- - - diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 48944b5e4e..f8d9039a48 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -1,8 +1,6 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

+* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html) +* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html) +* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) # 面试题 02.07. 链表相交 @@ -575,7 +573,3 @@ func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? { ``` -

- - - From 18ba181db38a7b619062df6122c7081bf9d7af79 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Sun, 16 Mar 2025 17:28:55 +0800 Subject: [PATCH 1515/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A1=A8=E8=BE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" | 2 -- "problems/0042.\346\216\245\351\233\250\346\260\264.md" | 1 - 2 files changed, 3 deletions(-) diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 281db4dc86..4a9d59cc23 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -3,8 +3,6 @@ * [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html) -> 这篇可以说是全网把组合问题如何去重,讲的最清晰的了! - # 40.组合总和II [力扣题目链接](https://leetcode.cn/problems/combination-sum-ii/) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 8a424e3ea2..8c43ae6051 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -5,7 +5,6 @@ -> 这个图就是大厂面试经典题目,接雨水! 最常青藤的一道题,面试官百出不厌! # 42. 接雨水 From 73a3cb532fd01f0abacb317b084630d46c640446 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 17 Mar 2025 10:57:51 +0800 Subject: [PATCH 1516/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- ...\227\347\254\246\344\270\262\346\216\245\351\276\231.md" | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index acd07c28d0..2d40e2b99d 100644 --- a/README.md +++ b/README.md @@ -385,8 +385,8 @@ 8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md) 9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md) 10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md) -11. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) -12. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) +11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) +12. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) 13. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) 14. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) 15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index f6d97866da..8bae6280bf 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -57,11 +57,7 @@ yhn 2 <= N <= 500

-<<<<<<< HEAD - -======= - ->>>>>>> d0bd2dc5 (更新图) +

From 91e7dab72c2053502928607738575d9b1002ad7f Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 17 Mar 2025 11:18:46 +0800 Subject: [PATCH 1517/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=A1=A8=E8=BE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" | 2 -- 1 file changed, 2 deletions(-) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index 0e13846d60..eb80e048f6 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -387,8 +387,6 @@ int main() { * 本题可以有负权回路,说明只要多做松弛,结果是会变的。 * 本题要求最多经过k个节点,对松弛次数是有限制的。 -如果本题中 没有负权回路的测试用例, 那版本一的代码就可以过了,也就不用我费这么大口舌去讲解的这个坑了。 - ## 拓展三(SPFA) 本题也可以用 SPFA来做,关于 SPFA ,已经在这里 [0094.城市间货物运输I-SPFA](./0094.城市间货物运输I-SPFA.md) 有详细讲解。 From 86a0208485d7379a4e4f0e4dda4545eec34ac9da Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Mon, 17 Mar 2025 15:52:23 +0800 Subject: [PATCH 1518/1533] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++-- ...44\346\225\260\344\271\213\345\222\214.md" | 4 +- ...36\346\226\207\345\255\220\344\270\262.md" | 4 +- ...27\346\257\215\347\273\204\345\220\210.md" | 4 +- ...4N\344\270\252\350\212\202\347\202\271.md" | 2 +- ...10\347\232\204\346\213\254\345\217\267.md" | 6 +-- ...22\345\205\245\344\275\215\347\275\256.md" | 10 ++-- ...7.\350\247\243\346\225\260\347\213\254.md" | 8 ++-- ...04\345\220\210\346\200\273\345\222\214.md" | 8 ++-- ...\345\220\210\346\200\273\345\222\214II.md" | 6 +-- ...2.\346\216\245\351\233\250\346\260\264.md" | 14 +++--- ...\350\267\203\346\270\270\346\210\217II.md" | 6 +-- ...6.\345\205\250\346\216\222\345\210\227.md" | 6 +-- ...\345\205\250\346\216\222\345\210\227II.md" | 6 +-- "problems/0051.N\347\232\207\345\220\216.md" | 6 +-- .../0052.N\347\232\207\345\220\216II.md" | 2 +- ...01\350\247\204\345\210\222\357\274\211.md" | 2 +- ...72\346\227\213\347\237\251\351\230\265.md" | 2 +- ...63\350\267\203\346\270\270\346\210\217.md" | 2 +- ...10\345\271\266\345\214\272\351\227\264.md" | 2 +- ...\346\227\213\347\237\251\351\230\265II.md" | 2 +- ...15\345\220\214\350\267\257\345\276\204.md" | 10 ++-- ...\345\220\214\350\267\257\345\276\204II.md" | 12 ++--- ...0.\347\210\254\346\245\274\346\242\257.md" | 2 +- ...26\350\276\221\350\267\235\347\246\273.md" | 4 +- "problems/0077.\347\273\204\345\220\210.md" | 10 ++-- ...04\345\220\210\344\274\230\345\214\226.md" | 2 +- ...47\347\232\204\347\237\251\345\275\242.md" | 10 ++-- "problems/0090.\345\255\220\351\233\206II.md" | 2 +- ...\345\216\237IP\345\234\260\345\235\200.md" | 4 +- ...11\346\220\234\347\264\242\346\240\221.md" | 10 ++-- ...11\346\220\234\347\264\242\346\240\221.md" | 4 +- ...70\345\220\214\347\232\204\346\240\221.md" | 4 +- ...60\344\272\214\345\217\211\346\240\221.md" | 4 +- ...02\345\272\217\351\201\215\345\216\206.md" | 18 +++---- ...00\345\244\247\346\267\261\345\272\246.md" | 6 +-- ...40\344\272\214\345\217\211\346\240\221.md" | 8 ++-- ...11\346\220\234\347\264\242\346\240\221.md" | 4 +- ...41\344\272\214\345\217\211\346\240\221.md" | 6 +-- ...00\345\260\217\346\267\261\345\272\246.md" | 2 +- ...57\345\276\204\346\200\273\345\222\214.md" | 8 ++-- ...04\345\255\220\345\272\217\345\210\227.md" | 4 +- ...02\347\202\271\346\214\207\351\222\210.md" | 2 +- ...00\344\275\263\346\227\266\346\234\272.md" | 2 +- ...\344\275\263\346\227\266\346\234\272II.md" | 2 +- ...344\275\263\346\227\266\346\234\272III.md" | 2 +- ...25\350\257\215\346\216\245\351\276\231.md" | 2 +- ...25\347\232\204\345\214\272\345\237\237.md" | 6 +-- ...\345\233\236\346\226\207\344\270\262II.md" | 2 +- ...4.\345\212\240\346\262\271\347\253\231.md" | 4 +- ...06\345\217\221\347\263\226\346\236\234.md" | 6 +-- ...25\350\257\215\346\213\206\345\210\206.md" | 4 +- ...57\345\275\242\351\223\276\350\241\250.md" | 2 +- ...\345\275\242\351\223\276\350\241\250II.md" | 12 ++--- ...15\346\216\222\351\223\276\350\241\250.md" | 2 +- ...\344\275\263\346\227\266\346\234\272IV.md" | 2 +- ...23\345\256\266\345\212\253\350\210\215.md" | 2 +- ...7.\345\271\277\346\220\234\347\211\210.md" | 6 +-- ...7.\346\267\261\346\220\234\347\211\210.md" | 4 +- ...76\350\241\250\345\205\203\347\264\240.md" | 12 ++--- ...73\350\275\254\351\223\276\350\241\250.md" | 2 +- ...04\345\255\220\346\225\260\347\273\204.md" | 2 +- ...\345\256\266\345\212\253\350\210\215II.md" | 6 +-- ...345\220\210\346\200\273\345\222\214III.md" | 6 +-- ...02\347\202\271\344\270\252\346\225\260.md" | 12 ++--- ...54\344\272\214\345\217\211\346\240\221.md" | 4 +- ...54\345\205\261\347\245\226\345\205\210.md" | 6 +-- ...54\345\205\261\347\245\226\345\205\210.md" | 12 ++--- ...00\346\234\211\350\267\257\345\276\204.md" | 6 +-- ...50\345\271\263\346\226\271\346\225\260.md" | 2 +- ...07\345\255\220\345\272\217\345\210\227.md" | 2 +- ...53\345\206\267\345\206\273\346\234\237.md" | 4 +- ...66\351\222\261\345\205\221\346\215\242.md" | 2 +- ...11\346\216\222\350\241\214\347\250\213.md" | 6 +-- ...345\256\266\345\212\253\350\210\215III.md" | 4 +- ...64\346\225\260\346\213\206\345\210\206.md" | 2 +- ...04\347\232\204\344\272\244\351\233\206.md" | 4 +- ...06\345\212\250\345\272\217\345\210\227.md" | 12 ++--- ...10\346\200\273\345\222\214\342\205\243.md" | 2 +- ...55\345\255\220\345\272\217\345\210\227.md" | 6 +-- ...66\345\255\220\344\271\213\345\222\214.md" | 6 +-- ...15\345\273\272\351\230\237\345\210\227.md" | 2 +- ...11\345\222\214\345\255\220\351\233\206.md" | 2 +- ...64\346\265\201\351\227\256\351\242\230.md" | 6 +-- ...15\345\217\240\345\214\272\351\227\264.md" | 2 +- ...55\347\232\204\350\212\202\347\202\271.md" | 2 +- ...25\347\210\206\346\260\224\347\220\203.md" | 2 +- ...06\345\217\221\351\245\274\345\271\262.md" | 4 +- ...20\345\255\227\347\254\246\344\270\262.md" | 30 ++++++------ ...77\347\232\204\345\221\250\351\225\277.md" | 2 +- ...4.\344\270\200\345\222\214\351\233\266.md" | 4 +- ...36\345\255\220\345\272\217\345\210\227.md" | 4 +- ...4.\347\233\256\346\240\207\345\222\214.md" | 30 ++++++------ ...55\347\232\204\344\274\227\346\225\260.md" | 4 +- ...13\350\247\222\347\232\204\345\200\274.md" | 4 +- ...07\345\255\220\345\272\217\345\210\227.md" | 8 ++-- ...\351\222\261\345\205\221\346\215\242II.md" | 4 +- ...17\347\273\235\345\257\271\345\267\256.md" | 4 +- ...72\347\264\257\345\212\240\346\240\221.md" | 4 +- ...40\351\231\244\346\223\215\344\275\234.md" | 2 +- ...66\344\272\214\345\217\211\346\240\221.md" | 2 +- ...36\346\226\207\345\255\220\344\270\262.md" | 6 +-- ...47\344\272\214\345\217\211\346\240\221.md" | 2 +- ...11\346\220\234\347\264\242\346\240\221.md" | 10 ++-- ...27\347\232\204\344\270\252\346\225\260.md" | 2 +- ...22\345\242\236\345\272\217\345\210\227.md" | 2 +- ...27\344\275\231\350\277\236\346\216\245.md" | 6 +-- ...\344\275\231\350\277\236\346\216\245II.md" | 4 +- ...00\345\244\247\351\235\242\347\247\257.md" | 4 +- ...55\347\232\204\346\220\234\347\264\242.md" | 4 +- ...22\345\205\245\346\223\215\344\275\234.md" | 2 +- ...14\345\210\206\346\237\245\346\211\276.md" | 4 +- ...76\350\256\241\351\223\276\350\241\250.md" | 6 +-- ...15\345\255\220\346\225\260\347\273\204.md" | 4 +- ...17\346\227\245\346\270\251\345\272\246.md" | 24 +++++----- ...66\350\277\237\346\227\266\351\227\264.md" | 46 +++++++++--------- ...71\347\210\254\346\245\274\346\242\257.md" | 4 +- ...27\346\257\215\345\214\272\351\227\264.md" | 2 +- ...34\347\232\204\350\210\252\347\217\255.md" | 6 +-- ...75\347\232\204\350\267\257\345\276\204.md" | 4 +- ...47\344\272\272\345\267\245\345\262\233.md" | 6 +-- ...31\345\222\214\346\210\277\351\227\264.md" | 4 +- ...61\350\204\211\346\225\260\347\273\204.md" | 2 +- ...47\344\272\214\345\217\211\346\240\221.md" | 10 ++-- ...60\347\232\204\346\225\260\351\207\217.md" | 8 ++-- ...70\344\272\244\347\232\204\347\272\277.md" | 4 +- ...\347\232\204\351\207\215\351\207\217II.md" | 2 +- ...61\345\255\220\345\272\217\345\210\227.md" | 4 +- ...77\347\232\204\346\225\260\347\233\256.md" | 4 +- ...21\345\217\230\345\271\263\350\241\241.md" | 2 +- ...55\345\277\203\350\212\202\347\202\271.md" | 2 +- ...30\345\234\250\350\267\257\345\276\204.md" | 2 +- ...57\345\244\232\345\244\247\357\274\237.md" | 10 ++-- ...17\202\344\274\232dijkstra\345\240\206.md" | 14 +++--- ...74\232dijkstra\346\234\264\347\264\240.md" | 40 ++++++++-------- .../0053.\345\257\273\345\256\235-Kruskal.md" | 20 ++++---- .../0053.\345\257\273\345\256\235-prim.md" | 22 ++++----- ...77\346\215\242\346\225\260\345\255\227.md" | 4 +- ...13\345\255\227\347\254\246\344\270\262.md" | 8 ++-- ...8.\345\214\272\351\227\264\345\222\214.md" | 4 +- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 20 ++++---- ...7\347\211\251\350\277\220\350\276\223I.md" | 20 ++++---- ...\347\211\251\350\277\220\350\276\223II.md" | 4 +- ...347\211\251\350\277\220\350\276\223III.md" | 40 ++++++++-------- ...16\351\200\233\345\205\254\345\233\255.md" | 12 ++--- ...57\350\276\276\350\267\257\345\276\204.md" | 4 +- ...60\351\207\217\345\271\277\346\220\234.md" | 6 +-- ...60\351\207\217\346\267\261\346\220\234.md" | 4 +- ...00\345\244\247\351\235\242\347\247\257.md" | 4 +- ...04\346\200\273\351\235\242\347\247\257.md" | 6 +-- ...11\346\262\241\345\255\244\345\262\233.md" | 6 +-- ...64\346\265\201\351\227\256\351\242\230.md" | 6 +-- ...00\345\244\247\345\262\233\345\261\277.md" | 10 ++-- ...50\345\217\257\350\276\276\346\200\247.md" | 4 +- ...77\347\232\204\345\221\250\351\225\277.md" | 8 ++-- ...50\347\232\204\350\267\257\345\276\204.md" | 2 +- ...27\344\275\231\350\277\236\346\216\245.md" | 12 ++--- ...\344\275\231\350\277\236\346\216\245II.md" | 12 ++--- ...46\344\270\262\346\216\245\351\276\231.md" | 4 +- ...57\344\273\266\346\236\204\345\273\272.md" | 22 ++++----- ...7\232\204\346\224\273\345\207\273astar.md" | 8 ++-- ...06\350\256\272\345\237\272\347\241\200.md" | 24 +++++----- ...06\350\256\272\345\237\272\347\241\200.md" | 6 +-- ...06\350\256\272\345\237\272\347\241\200.md" | 14 +++--- ...06\350\256\272\345\237\272\347\241\200.md" | 30 ++++++------ ...30\346\200\273\347\273\223\347\257\207.md" | 2 +- problems/qita/acm.md | 20 ++++---- problems/qita/join.md | 48 +++++++++---------- problems/qita/server.md | 6 +-- problems/qita/shejimoshi.md | 14 +++--- problems/qita/tulunfabu.md | 34 ++++++------- ...21\346\200\273\347\273\223\347\257\207.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 16 +++---- ...55\344\273\243\351\201\215\345\216\206.md" | 2 +- .../ACM\346\250\241\345\274\217.md" | 18 +++---- ...72\344\272\214\345\217\211\346\240\221.md" | 16 +++---- "problems/\345\211\215\345\272\217/vim.md" | 2 +- ...43\347\240\201\351\243\216\346\240\274.md" | 2 +- ...05\345\255\230\346\266\210\350\200\227.md" | 8 ++-- ...64\345\244\215\346\235\202\345\272\246.md" | 6 +-- ...17\345\221\230\347\256\200\345\216\206.md" | 4 +- ...27\346\263\225\350\266\205\346\227\266.md" | 10 ++-- ...02\345\272\246\345\210\206\346\236\220.md" | 6 +-- ...64\345\244\215\346\235\202\345\272\246.md" | 4 +- ...77\346\215\242\347\251\272\346\240\274.md" | 4 +- ...54\345\255\227\347\254\246\344\270\262.md" | 8 ++-- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 10 ++-- ...50\346\234\253\346\200\273\347\273\223.md" | 12 ++--- ...50\346\234\253\346\200\273\347\273\223.md" | 10 ++-- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 4 +- ...50\346\234\253\346\200\273\347\273\223.md" | 8 ++-- ...50\346\234\253\346\200\273\347\273\223.md" | 12 ++--- ...50\346\234\253\346\200\273\347\273\223.md" | 6 +-- ...50\346\234\253\346\200\273\347\273\223.md" | 4 +- ...50\346\234\253\346\200\273\347\273\223.md" | 10 ++-- ...50\346\234\253\346\200\273\347\273\223.md" | 6 +-- ...06\350\256\272\345\237\272\347\241\200.md" | 12 ++--- ...36\346\272\257\346\200\273\347\273\223.md" | 42 ++++++++-------- ...00\347\247\215\345\206\231\346\263\225.md" | 4 +- ...06\350\256\272\345\237\272\347\241\200.md" | 4 +- ...04\346\200\273\347\273\223\347\257\207.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 6 +-- ...06\350\256\272\345\237\272\347\241\200.md" | 6 +-- ...06\350\256\262\350\247\243\357\274\211.md" | 8 ++-- ...05\346\200\273\347\273\223\347\257\207.md" | 4 +- ...47\241\20001\350\203\214\345\214\205-1.md" | 26 +++++----- ...47\241\20001\350\203\214\345\214\205-2.md" | 2 +- ...14\345\214\205\344\270\200\347\273\264.md" | 4 +- ...14\345\205\250\350\203\214\345\214\205.md" | 10 ++-- ...25\346\200\273\347\273\223\347\257\207.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- ...50\346\200\273\347\273\223\347\257\207.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 14 +++--- ...76\350\241\250\347\233\270\344\272\244.md" | 8 ++-- 216 files changed, 813 insertions(+), 813 deletions(-) diff --git a/README.md b/README.md index 2d40e2b99d..06de2f5d0e 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ 题目分类大纲如下: -二叉树大纲 +二叉树大纲 1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md) 2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md) @@ -222,7 +222,7 @@ 题目分类大纲如下: -回溯算法大纲 +回溯算法大纲 1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md) 2. [回溯算法:77.组合](./problems/0077.组合.md) @@ -252,7 +252,7 @@ 题目分类大纲如下: -贪心算法大纲 +贪心算法大纲 1. [关于贪心算法,你该了解这些!](./problems/贪心算法理论基础.md) 2. [贪心算法:455.分发饼干](./problems/0455.分发饼干.md) @@ -503,5 +503,5 @@ 添加微信记得备注,如果是已工作,备注:姓名-城市-岗位。如果学生,备注:姓名-学校-年级。**备注没有自我介绍不通过哦** -
+
diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 6be92fa81b..f9bea8289e 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -83,10 +83,10 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候, 过程如下: -![过程一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220711202638.png) +![过程一](https://file.kamacoder.com/pics/20220711202638.png) -![过程二](https://code-thinking-1253855093.file.myqcloud.com/pics/20230220223536.png) +![过程二](https://file.kamacoder.com/pics/20230220223536.png) C++代码: diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index 4ce49810b3..1e0667e575 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -106,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473.jpg) +![647.回文子串](https://file.kamacoder.com/pics/20210121171032473.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -140,7 +140,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951.jpg) +![647.回文子串1](https://file.kamacoder.com/pics/20210121171059951.jpg) **注意因为dp[i][j]的定义,所以j一定是大于等于i的,那么在填充dp[i][j]的时候一定是只填充右上半部分**。 diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index 93f41e0f74..a35fd4e2bc 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -11,7 +11,7 @@ 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 -![17.电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/2020102916424043.png) +![17.电话号码的字母组合](https://file.kamacoder.com/pics/2020102916424043.png) 示例: * 输入:"23" @@ -64,7 +64,7 @@ const string letterMap[10] = { 例如:输入:"23",抽象为树形结构,如图所示: -![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123200304469.png) +![17. 电话号码的字母组合](https://file.kamacoder.com/pics/20201123200304469.png) 图中可以看出遍历的深度,就是输入"23"的长度,而叶子节点就是我们要收集的结果,输出["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。 diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 53b59039e5..9b2ba88ef8 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -16,7 +16,7 @@ 示例 1: -![19.删除链表的倒数第N个节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20210510085957392.png) +![19.删除链表的倒数第N个节点](https://file.kamacoder.com/pics/20210510085957392.png) 输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index c642fb4ecd..7282471285 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -81,13 +81,13 @@ cd a/b/c/../../ 1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。 -![括号匹配1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020080915505387.png) +![括号匹配1](https://file.kamacoder.com/pics/2020080915505387.png) 2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。 -![括号匹配2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155107397.png) +![括号匹配2](https://file.kamacoder.com/pics/20200809155107397.png) 3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。 -![括号匹配3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155115779.png) +![括号匹配3](https://file.kamacoder.com/pics/20200809155115779.png) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index e0b065cd5a..c9826fa205 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -41,7 +41,7 @@ 这道题目,要在数组中插入目标值,无非是这四种情况。 -![35_搜索插入位置3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232148471.png) +![35_搜索插入位置3](https://file.kamacoder.com/pics/20201216232148471.png) * 目标值在数组所有元素之前 * 目标值等于数组中某一个元素 @@ -82,14 +82,14 @@ public: 效率如下: -![35_搜索插入位置](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232127268.png) +![35_搜索插入位置](https://file.kamacoder.com/pics/20201216232127268.png) ### 二分法 既然暴力解法的时间复杂度是O(n),就要尝试一下使用二分查找法。 -![35_搜索插入位置4](https://code-thinking-1253855093.file.myqcloud.com/pics/202012162326354.png) +![35_搜索插入位置4](https://file.kamacoder.com/pics/202012162326354.png) 大家注意这道题目的前提是数组是有序数组,这也是使用二分查找的基础条件。 @@ -99,7 +99,7 @@ public: 大体讲解一下二分法的思路,这里来举一个例子,例如在这个数组中,使用二分法寻找元素为5的位置,并返回其下标。 -![35_搜索插入位置5](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232659199.png) +![35_搜索插入位置5](https://file.kamacoder.com/pics/20201216232659199.png) 二分查找涉及的很多的边界条件,逻辑比较简单,就是写不好。 @@ -150,7 +150,7 @@ public: * 空间复杂度:O(1) 效率如下: -![35_搜索插入位置2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020121623272877.png) +![35_搜索插入位置2](https://file.kamacoder.com/pics/2020121623272877.png) ### 二分法第二种写法 diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index b26bf53308..5d2adb4d9d 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -18,11 +18,11 @@ 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 空白格用 '.' 表示。 -![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/202011171912586.png) +![解数独](https://file.kamacoder.com/pics/202011171912586.png) 一个数独。 -![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117191340669.png) +![解数独](https://file.kamacoder.com/pics/20201117191340669.png) 答案被标成红色。 @@ -52,7 +52,7 @@ 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131816104.png) +![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131816104.png) ### 回溯三部曲 @@ -83,7 +83,7 @@ bool backtracking(vector>& board) * 递归单层搜索逻辑 -![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131822254.png) +![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131822254.png) 在树形图中可以看出我们需要的是一个二维的递归 (一行一列) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 455bd697cd..8467277115 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -50,7 +50,7 @@ candidates 中的数字可以无限制重复被选取。 本题搜索的过程抽象成树形结构如下: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) 注意图中叶子节点的返回条件,因为本题没有组合数量要求,仅仅是总和的限制,所以递归没有层数的限制,只要选取的元素总和超过target,就返回! 而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。 @@ -85,7 +85,7 @@ void backtracking(vector& candidates, int target, int sum, int startIndex) 在如下树形结构中: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135337214.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135337214.png) 从叶子节点可以清晰看到,终止只有两种情况,sum大于target和sum等于target。 @@ -158,7 +158,7 @@ public: 在这个树形结构中: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135342472.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135342472.png) 以及上面的版本一的代码大家可以看到,对于sum已经大于target的情况,其实是依然进入了下一层递归,只是下一层递归结束判断的时候,会判断sum > target的话就返回。 @@ -171,7 +171,7 @@ public: 如图: -![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170809182.png) +![39.组合总和1](https://file.kamacoder.com/pics/20201223170809182.png) for循环剪枝代码如下: diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 4a9d59cc23..f0cbc2200f 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -76,7 +76,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 选择过程树形结构如图所示: -![40.组合总和II](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000918.png) +![40.组合总和II](https://file.kamacoder.com/pics/20230310000918.png) 可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。 @@ -126,7 +126,7 @@ if (sum == target) { 这块比较抽象,如图: -![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000954.png) +![40.组合总和II1](https://file.kamacoder.com/pics/20230310000954.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: @@ -137,7 +137,7 @@ if (sum == target) { 而 used[i - 1] == true,说明是进入下一层递归,去下一个数,所以是树枝上,如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221021163812.png) +![](https://file.kamacoder.com/pics/20221021163812.png) **这块去重的逻辑很抽象,网上搜的题解基本没有能讲清楚的,如果大家之前思考过这个问题或者刷过这道题目,看到这里一定会感觉通透了很多!** diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 8c43ae6051..1e6ec11bc5 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -47,10 +47,10 @@ 首先要明确,要按照行来计算,还是按照列来计算。 按照行来计算如图: -![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091118927.png) +![42.接雨水2](https://file.kamacoder.com/pics/20210402091118927.png) 按照列来计算如图: -![42.接雨水1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091208445.png) +![42.接雨水1](https://file.kamacoder.com/pics/20210402091208445.png) 一些同学在实现的时候,很容易一会按照行来计算一会按照列来计算,这样就会越写越乱。 @@ -62,7 +62,7 @@ 这句话可以有点绕,来举一个理解,例如求列4的雨水高度,如图: -![42.接雨水3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092732301.png) +![42.接雨水3](https://file.kamacoder.com/pics/20210223092732301.png) 列4 左侧最高的柱子是列3,高度为2(以下用lHeight表示)。 @@ -201,7 +201,7 @@ public: 1. 首先单调栈是按照行方向来计算雨水,如图: -![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092629946.png) +![42.接雨水2](https://file.kamacoder.com/pics/20210223092629946.png) 知道这一点,后面的就可以理解了。 @@ -215,7 +215,7 @@ public: 如图: -![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229.png) +![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229.png) 关于单调栈的顺序给大家一个总结: [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 中求一个元素右边第一个更大元素,单调栈就是递增的,[84.柱状图中最大的矩形](https://programmercarl.com/0084.柱状图中最大的矩形.html)求一个元素右边第一个更小元素,单调栈就是递减的。 @@ -229,7 +229,7 @@ public: 如图所示: -![42.接雨水5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223094619398.png) +![42.接雨水5](https://file.kamacoder.com/pics/20210223094619398.png) 4. 栈里要保存什么数值 @@ -284,7 +284,7 @@ if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况 如果当前遍历的元素(柱子)高度大于栈顶元素的高度,此时就出现凹槽了,如图所示: -![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229-20230310123027977.png) +![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229-20230310123027977.png) 取栈顶元素,将栈顶元素弹出,这个就是凹槽的底部,也就是中间位置,下标记为mid,对应的高度为height[mid](就是图中的高度1)。 diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index a20eb2a6a9..dd51384d77 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -47,7 +47,7 @@ 如图: -![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103.png) +![45.跳跃游戏II](https://file.kamacoder.com/pics/20201201232309103.png) **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** @@ -99,11 +99,11 @@ public: 因为当移动下标指向 nums.size - 2 时: - 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即 ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图: - ![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png) + ![45.跳跃游戏II2](https://file.kamacoder.com/pics/20201201232445286.png) - 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图: -![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693.png) +![45.跳跃游戏II1](https://file.kamacoder.com/pics/20201201232338693.png) 代码如下: diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index 611a4cb1e0..5a190242e3 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -41,7 +41,7 @@ 我以[1,2,3]为例,抽象成树形结构如下: -![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) +![全排列](https://file.kamacoder.com/pics/20240803180318.png) ### 回溯三部曲 @@ -53,7 +53,7 @@ 但排列问题需要一个used数组,标记已经选择的元素,如图橘黄色部分所示: -![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) +![全排列](https://file.kamacoder.com/pics/20240803180318.png) 代码如下: @@ -65,7 +65,7 @@ void backtracking (vector& nums, vector& used) * 递归终止条件 -![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png) +![全排列](https://file.kamacoder.com/pics/20240803180318.png) 可以看出叶子节点,就是收割结果的地方。 diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index 1e51a7bcd7..6ed794aaaf 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -48,7 +48,7 @@ 我以示例中的 [1,1,2]为例 (为了方便举例,已经排序)抽象为一棵树,去重过程如图: -![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201331223.png) +![47.全排列II1](https://file.kamacoder.com/pics/20201124201331223.png) 图中我们对同一树层,前一位(也就是nums[i-1])如果使用过,那么就进行去重。 @@ -130,11 +130,11 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201406192.png) +![47.全排列II2](https://file.kamacoder.com/pics/20201124201406192.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201431571.png) +![47.全排列II3](https://file.kamacoder.com/pics/20201124201431571.png) 大家应该很清晰的看到,树层上对前一位去重非常彻底,效率很高,树枝上对前一位去重虽然最后可以得到答案,但是做了很多无用搜索。 diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index b201b55fe4..2a90a023e6 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -15,7 +15,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211020232201.png) +![](https://file.kamacoder.com/pics/20211020232201.png) * 输入:n = 4 * 输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] @@ -45,7 +45,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, 下面我用一个 3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303.jpg) +![51.N皇后](https://file.kamacoder.com/pics/20210130182532303.jpg) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 @@ -85,7 +85,7 @@ void backtracking(int n, int row, vector& chessboard) { * 递归终止条件 在如下树形结构中: -![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303-20230310122134167.jpg) +![51.N皇后](https://file.kamacoder.com/pics/20210130182532303-20230310122134167.jpg) 可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。 diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index 11c257b073..489ab1f756 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -13,7 +13,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并 上图为 8 皇后问题的一种解法。 -![51n皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20200821152118456.png) +![51n皇后](https://file.kamacoder.com/pics/20200821152118456.png) 给定一个整数 n,返回 n 皇后不同的解决方案的数量。 diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 568626dc6d..174f55e848 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -54,7 +54,7 @@ dp[0]应该是多少呢? 5. 举例推导dp数组 以示例一为例,输入:nums = [-2,1,-3,4,-1,2,1,-5,4],对应的dp状态如下: -![53.最大子序和(动态规划)](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303104129101.png) +![53.最大子序和(动态规划)](https://file.kamacoder.com/pics/20210303104129101.png) **注意最后的结果可不是dp[nums.size() - 1]!** ,而是dp[6]。 diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index 7d7f460f01..a852b6740d 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -36,7 +36,7 @@ 由外向内一圈一圈这么画下去,如下所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922102236.png) +![](https://file.kamacoder.com/pics/20220922102236.png) 这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。 diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index 3ab004b2aa..0ebbcb595c 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -48,7 +48,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203105634.png) +![](https://file.kamacoder.com/pics/20230203105634.png) i 每次移动只能在 cover 的范围内移动,每移动一个元素,cover 得到该元素数值(新的覆盖范围)的补充,让 i 继续移动下去。 diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 76792dbae1..cb06fcab3d 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -38,7 +38,7 @@ 这么说有点抽象,看图:(**注意图中区间都是按照左边界排序之后了**) -![56.合并区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223200632791.png) +![56.合并区间](https://file.kamacoder.com/pics/20201223200632791.png) 知道如何判断重复之后,剩下的就是合并了,如何去模拟合并区间呢? diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 9961c0e70b..d7aea257a5 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -54,7 +54,7 @@ 那么我按照左闭右开的原则,来画一圈,大家看一下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922102236.png) +![](https://file.kamacoder.com/pics/20220922102236.png) 这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。 diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 23cd8060fa..20bd56ba9f 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -16,7 +16,7 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110174033215.png) +![](https://file.kamacoder.com/pics/20210110174033215.png) * 输入:m = 3, n = 7 * 输出:28 @@ -62,7 +62,7 @@ 如图举例: -![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700.png) +![62.不同路径](https://file.kamacoder.com/pics/20201209113602700.png) 此时问题就可以转化为求二叉树叶子节点的个数,代码如下: @@ -131,7 +131,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图所示: -![62.不同路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113631392.png) +![62.不同路径1](https://file.kamacoder.com/pics/20201209113631392.png) 以上动规五部曲分析完毕,C++代码如下: @@ -180,7 +180,7 @@ public: 在这个图中,可以看出一共m,n的话,无论怎么走,走到终点都需要 m + n - 2 步。 -![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700-20230310120944078.png) +![62.不同路径](https://file.kamacoder.com/pics/20201209113602700-20230310120944078.png) 在这m + n - 2 步中,一定有 m - 1 步是要向下走的,不用管什么时候向下走。 @@ -190,7 +190,7 @@ public: 那么答案,如图所示: -![62.不同路径2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113725324.png) +![62.不同路径2](https://file.kamacoder.com/pics/20201209113725324.png) **求组合的时候,要防止两个int相乘溢出!** 所以不能把算式的分子都算出来,分母都算出来再做除法。 diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 61d9329d4b..d39036ba3a 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -14,13 +14,13 @@ 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径? -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204901338.png) +![](https://file.kamacoder.com/pics/20210111204901338.png) 网格中的障碍物和空位置分别用 1 和 0 来表示。 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204939971.png) +![](https://file.kamacoder.com/pics/20210111204939971.png) * 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] * 输出:2 @@ -32,7 +32,7 @@ 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111205857918.png) +![](https://file.kamacoder.com/pics/20210111205857918.png) * 输入:obstacleGrid = [[0,1],[0,0]] * 输出:1 @@ -93,7 +93,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图: -![63.不同路径II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114513928.png) +![63.不同路径II](https://file.kamacoder.com/pics/20210104114513928.png) 下标(0, j)的初始化情况同理。 @@ -127,11 +127,11 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114548983.png) +![63.不同路径II1](https://file.kamacoder.com/pics/20210104114548983.png) 对应的dp table 如图: -![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256.png) +![63.不同路径II2](https://file.kamacoder.com/pics/20210104114610256.png) 如果这个图看不懂,建议再理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下! diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 92c3858698..17bf3ee760 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -101,7 +101,7 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 举例当n为5的时候,dp table(dp数组)应该是这样的 -![70.爬楼梯](https://code-thinking-1253855093.file.myqcloud.com/pics/20210105202546299.png) +![70.爬楼梯](https://file.kamacoder.com/pics/20210105202546299.png) 如果代码出问题了,就把dp table 打印出来,看看究竟是不是和自己推导的一样。 diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 408999d873..192ea47002 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -170,7 +170,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 可以看出dp[i][j]是依赖左方,上方和左上方元素的,如图: -![72.编辑距离](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162113131.jpg) +![72.编辑距离](https://file.kamacoder.com/pics/20210114162113131.jpg) 所以在dp矩阵中一定是从左到右从上到下去遍历。 @@ -194,7 +194,7 @@ for (int i = 1; i <= word1.size(); i++) { 以示例1为例,输入:`word1 = "horse", word2 = "ros"`为例,dp矩阵状态图如下: -![72.编辑距离1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162132300.jpg) +![72.编辑距离1](https://file.kamacoder.com/pics/20210114162132300.jpg) 以上动规五部分析完毕,C++代码如下: diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 60900d76a6..c523c01c17 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -82,7 +82,7 @@ for (int i = 1; i <= n; i++) { 那么我把组合问题抽象为如下树形结构: -![77.组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195223940.png) +![77.组合](https://file.kamacoder.com/pics/20201123195223940.png) 可以看出这棵树,一开始集合是 1,2,3,4, 从左向右取数,取过的数,不再重复取。 @@ -126,7 +126,7 @@ vector path; // 用来存放符合条件结果 从下图中红线部分可以看出,在集合[1,2,3,4]取1之后,下一层递归,就要在[2,3,4]中取数了,那么下一层递归如何知道从[2,3,4]中取数呢,靠的就是startIndex。 -![77.组合2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195328976.png) +![77.组合2](https://file.kamacoder.com/pics/20201123195328976.png) 所以需要startIndex来记录下一层递归,搜索的起始位置。 @@ -146,7 +146,7 @@ path这个数组的大小如果达到k,说明我们找到了一个子集大小 如图红色部分: -![77.组合3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195407907.png) +![77.组合3](https://file.kamacoder.com/pics/20201123195407907.png) 此时用result二维数组,把path保存起来,并终止本层递归。 @@ -163,7 +163,7 @@ if (path.size() == k) { 回溯法的搜索过程就是一个树型结构的遍历过程,在如下图中,可以看出for循环用来横向遍历,递归的过程是纵向遍历。 -![77.组合1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195242899.png) +![77.组合1](https://file.kamacoder.com/pics/20201123195242899.png) 如此我们才遍历完图中的这棵树。 @@ -267,7 +267,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207-20230310134409532.png) +![77.组合4](https://file.kamacoder.com/pics/20210130194335207-20230310134409532.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index e2ca3d7d0a..c5e26e7706 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -67,7 +67,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207.png) +![77.组合4](https://file.kamacoder.com/pics/20210130194335207.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index 6577cf542d..e1a6671e73 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -11,9 +11,9 @@ 求在该柱状图中,能够勾勒出来的矩形的最大面积。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210803220437.png) +![](https://file.kamacoder.com/pics/20210803220437.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210803220506.png) +![](https://file.kamacoder.com/pics/20210803220506.png) * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 @@ -114,7 +114,7 @@ public: 我来举一个例子,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221165730.png) +![](https://file.kamacoder.com/pics/20230221165730.png) 只有栈里从大到小的顺序,才能保证栈顶元素找到左右两边第一个小于栈顶元素的柱子。 @@ -179,7 +179,7 @@ public: 如果数组本身就是升序的,例如[2,4,6,8],那么入栈之后 都是单调递减,一直都没有走 情况三 计算结果的哪一步,所以最后输出的就是0了。 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221163936.png) +![](https://file.kamacoder.com/pics/20230221163936.png) 那么结尾加一个0,就会让栈里的所有元素,走到情况三的逻辑。 @@ -194,7 +194,7 @@ public: 之后又将6 加入栈(此时8已经弹出了),然后 就是 4 与 栈口元素 6 进行比较,周而复始,那么计算的最后结果result就是0。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221164533.png) +![](https://file.kamacoder.com/pics/20230221164533.png) 所以我们需要在 height数组前后各加一个元素0。 diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 3bda02bc9e..2f26e6068f 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -39,7 +39,7 @@ 用示例中的[1, 2, 2] 来举例,如图所示: (**注意去重需要先对集合排序**) -![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124195411977.png) +![90.子集II](https://file.kamacoder.com/pics/20201124195411977.png) 从图中可以看出,同一树层上重复取2 就要过滤掉,同一树枝上就可以重复取2,因为同一树枝上元素的集合才是唯一子集! diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 1a89827897..5ef2162898 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -54,7 +54,7 @@ 切割问题可以抽象为树型结构,如图: -![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933.png) +![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933.png) ### 回溯三部曲 @@ -106,7 +106,7 @@ if (pointNum == 3) { // 逗点数量为3时,分隔结束 如果不合法就结束本层循环,如图中剪掉的分支: -![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933-20230310132314109.png) +![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933-20230310132314109.png) 然后就是递归和回溯的过程: diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index f4e0e456b7..ca99a46695 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -12,7 +12,7 @@ 示例: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png) +![](https://file.kamacoder.com/pics/20210113161941835.png) ## 算法公开课 @@ -27,11 +27,11 @@ 了解了二叉搜索树之后,我们应该先举几个例子,画画图,看看有没有什么规律,如图: -![96.不同的二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093106367.png) +![96.不同的二叉搜索树](https://file.kamacoder.com/pics/20210107093106367.png) n为1的时候有一棵树,n为2有两棵树,这个是很直观的。 -![96.不同的二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093129889.png) +![96.不同的二叉搜索树1](https://file.kamacoder.com/pics/20210107093129889.png) 来看看n为3的时候,有哪几种情况。 @@ -65,7 +65,7 @@ dp[3],就是 元素1为头结点搜索树的数量 + 元素2为头结点搜索 如图所示: -![96.不同的二叉搜索树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093226241.png) +![96.不同的二叉搜索树2](https://file.kamacoder.com/pics/20210107093226241.png) 此时我们已经找到递推关系了,那么可以用动规五部曲再系统分析一遍。 @@ -118,7 +118,7 @@ for (int i = 1; i <= n; i++) { n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093253987.png) +![96.不同的二叉搜索树3](https://file.kamacoder.com/pics/20210107093253987.png) 当然如果自己画图举例的话,基本举例到n为3就可以了,n为4的时候,画图已经比较麻烦了。 diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 22a47f9632..9569cbddf1 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -16,7 +16,7 @@ * 节点的右子树只包含大于当前节点的数。 * 所有左子树和右子树自身必须也是二叉搜索树。 -![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png) +![98.验证二叉搜索树](https://file.kamacoder.com/pics/20230310000750.png) ## 算法公开课 @@ -102,7 +102,7 @@ if (root->val > root->left->val && root->val < root->right->val) { 例如: [10,5,15,null,null,6,20] 这个case: -![二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000824.png) +![二叉搜索树](https://file.kamacoder.com/pics/20230310000824.png) 节点10大于左节点5,小于右节点15,但右子树里出现了一个6 这就不符合了! diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" index 52c9fcf2e7..e5f610009e 100644 --- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" +++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" @@ -12,9 +12,9 @@ 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726172932.png) +![](https://file.kamacoder.com/pics/20210726172932.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png) +![](https://file.kamacoder.com/pics/20210726173011.png) ## 思路 diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index f066408408..205597b068 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -9,7 +9,7 @@ 给定一个二叉树,检查它是否是镜像对称的。 -![101. 对称二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144607387.png) +![101. 对称二叉树](https://file.kamacoder.com/pics/20210203144607387.png) ## 算法公开课 @@ -25,7 +25,7 @@ 比较的是两个子树的里侧和外侧的元素是否相等。如图所示: -![101. 对称二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144624414.png) +![101. 对称二叉树1](https://file.kamacoder.com/pics/20210203144624414.png) 那么遍历的顺序应该是什么样的呢? diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index da2d85c99d..6725d72cc2 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -37,7 +37,7 @@ 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。 -![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png) +![102.二叉树的层序遍历](https://file.kamacoder.com/pics/20210203144842988.png) ### 思路 @@ -532,7 +532,7 @@ public IList> LevelOrder(TreeNode root) 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) -![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png) +![107.二叉树的层次遍历II](https://file.kamacoder.com/pics/20210203151058308.png) ### 思路 @@ -926,7 +926,7 @@ public IList> LevelOrderBottom(TreeNode root) 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 -![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png) +![199.二叉树的右视图](https://file.kamacoder.com/pics/20210203151307377.png) ### 思路 @@ -1276,7 +1276,7 @@ public class Solution 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。 -![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png) +![637.二叉树的层平均值](https://file.kamacoder.com/pics/20210203151350500.png) ### 思路 @@ -1634,7 +1634,7 @@ public class Solution { 例如,给定一个 3叉树 : -![429. N叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151439168.png) +![429. N叉树的层序遍历](https://file.kamacoder.com/pics/20210203151439168.png) 返回其层序遍历: @@ -2006,7 +2006,7 @@ impl Solution { 您需要在二叉树的每一行中找到最大的值。 -![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png) +![515.在每个树行中找最大值](https://file.kamacoder.com/pics/20210203151532153.png) ### 思路 @@ -2337,7 +2337,7 @@ struct Node { 初始状态下,所有 next 指针都被设置为 NULL。 -![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg) +![116.填充每个节点的下一个右侧节点指针](https://file.kamacoder.com/pics/20210203152044855.jpg) ### 思路 @@ -2971,7 +2971,7 @@ object Solution { 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310134849764.png) +![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310134849764.png) 返回它的最大深度 3 。 @@ -2981,7 +2981,7 @@ object Solution { 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585-20230310134854803.png) +![层序遍历](https://file.kamacoder.com/pics/20200810193056585-20230310134854803.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 6b9994ed93..2eb22ae5a3 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -18,7 +18,7 @@ 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310121809902.png) +![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310121809902.png) 返回它的最大深度 3 。 @@ -172,7 +172,7 @@ public: 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585.png) +![层序遍历](https://file.kamacoder.com/pics/20200810193056585.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 @@ -217,7 +217,7 @@ public: 例如,给定一个 3叉树 : -![559.n叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315313214.png) +![559.n叉树的最大深度](https://file.kamacoder.com/pics/2021020315313214.png) 我们应返回其最大深度,3。 diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index d0af8fef27..2f8e5eefb6 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -25,7 +25,7 @@ * 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: -![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png) +![106. 从中序与后序遍历序列构造二叉树1](https://file.kamacoder.com/pics/20210203154316774.png) ## 算法公开课 @@ -40,7 +40,7 @@ 流程如图: -![106.从中序与后序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154249860.png) +![106.从中序与后序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154249860.png) 那么代码应该怎么写呢? @@ -411,7 +411,7 @@ public: 中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: -![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png) +![105. 从前序与中序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154626672.png) ### 思路 @@ -554,7 +554,7 @@ public: 举一个例子: -![106.从中序与后序遍历序列构造二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154720326.png) +![106.从中序与后序遍历序列构造二叉树2](https://file.kamacoder.com/pics/20210203154720326.png) tree1 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index adb2a06082..5829e2d220 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -16,7 +16,7 @@ 示例: -![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png) +![108.将有序数组转换为二叉搜索树](https://file.kamacoder.com/pics/20201022164420763.png) ## 算法公开课 @@ -40,7 +40,7 @@ 例如 有序数组[-10,-3,0,5,9] 就可以构造成这样的二叉搜索树,如图。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220930173553.png) +![](https://file.kamacoder.com/pics/20220930173553.png) 上图中,是符合二叉搜索树的特性吧,如果要这么做的话,是不是本题意义就不大了,所以才强调是平衡二叉搜索树。 diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index c3da728077..ff84ad8471 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -19,7 +19,7 @@ 给定二叉树 [3,9,20,null,null,15,7] -![110.平衡二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315542230.png) +![110.平衡二叉树](https://file.kamacoder.com/pics/2021020315542230.png) 返回 true 。 @@ -27,7 +27,7 @@ 给定二叉树 [1,2,2,3,3,null,null,4,4] -![110.平衡二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155447919.png) +![110.平衡二叉树1](https://file.kamacoder.com/pics/20210203155447919.png) 返回 false 。 @@ -46,7 +46,7 @@ 但leetcode中强调的深度和高度很明显是按照节点来计算的,如图: -![110.平衡二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155515650.png) +![110.平衡二叉树2](https://file.kamacoder.com/pics/20210203155515650.png) 关于根节点的深度究竟是1 还是 0,不同的地方有不一样的标准,leetcode的题目中都是以节点为一度,即根节点深度是1。但维基百科上定义用边为一度,即根节点的深度是0,我们暂时以leetcode为准(毕竟要在这上面刷题)。 diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index a77594b28f..bd4ea29d6c 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -20,7 +20,7 @@ 给定二叉树 [3,9,20,null,null,15,7], -![111.二叉树的最小深度1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315582586.png) +![111.二叉树的最小深度1](https://file.kamacoder.com/pics/2021020315582586.png) 返回它的最小深度 2. diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 22ed777fba..24891acee9 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -15,7 +15,7 @@ 示例: 给定如下二叉树,以及目标和 sum = 22, -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230407210247.png) +![](https://file.kamacoder.com/pics/20230407210247.png) 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 @@ -53,7 +53,7 @@ 如图所示: -![112.路径总和](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020316051216.png) +![112.路径总和](https://file.kamacoder.com/pics/2021020316051216.png) 图中可以看出,遍历的路线,并不要遍历整棵树,所以递归函数需要返回值,可以用bool类型表示。 @@ -230,7 +230,7 @@ public: 给定如下二叉树,以及目标和 sum = 22, -![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png) +![113.路径总和ii1.png](https://file.kamacoder.com/pics/20210203160854654.png) ### 思路 @@ -239,7 +239,7 @@ public: 如图: -![113.路径总和ii](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160922745.png) +![113.路径总和ii](https://file.kamacoder.com/pics/20210203160922745.png) 为了尽可能的把细节体现出来,我写出如下代码(**这份代码并不简洁,但是逻辑非常清晰**) diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 832b64d183..1df3d899a8 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -70,7 +70,7 @@ dp[i][j]:以i-1为结尾的s子序列中出现以j-1为结尾的t的个数为d 从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j] 是从上方和左上方推导而来,如图:,那么 dp[i][0] 和dp[0][j]是一定要初始化的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221222165412.png) +![](https://file.kamacoder.com/pics/20221222165412.png) 每次当初始化的时候,都要回顾一下dp[i][j]的定义,不要凭感觉初始化。 @@ -101,7 +101,7 @@ for (int j = 1; j <= t.size(); j++) dp[0][j] = 0; // 其实这行代码可以和 从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j]都是根据左上方和正上方推出来的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221222165412.png) +![](https://file.kamacoder.com/pics/20221222165412.png) 所以遍历的时候一定是从上到下,从左到右,这样保证dp[i][j]可以根据之前计算出来的数值进行计算。 diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" index 234929f3d1..9de1de1ee5 100644 --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -26,7 +26,7 @@ struct Node { * 你只能使用常量级额外空间。 * 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png) +![](https://file.kamacoder.com/pics/20210727143202.png) ## 思路 diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index b9df47a491..f82ed962fa 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -129,7 +129,7 @@ dp[0][1]表示第0天不持有股票,不持有股票那么现金就是0,所 以示例1,输入:[7,1,5,3,6,4]为例,dp数组状态如下: -![121.买卖股票的最佳时机](https://code-thinking-1253855093.file.myqcloud.com/pics/20210224225642465.png) +![121.买卖股票的最佳时机](https://file.kamacoder.com/pics/20210224225642465.png) dp[5][1]就是最终结果。 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index b268040771..4ccb17bbfd 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -66,7 +66,7 @@ 如图: -![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858-20230310134659477.png) +![122.买卖股票的最佳时机II](https://file.kamacoder.com/pics/2020112917480858-20230310134659477.png) 一些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。 diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 75f7cb3f20..c4ff89a0c5 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -120,7 +120,7 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]); 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201228181724295-20230310134201291.png) +![123.买卖股票的最佳时机III](https://file.kamacoder.com/pics/20201228181724295-20230310134201291.png) 大家可以看到红色框为最后两次卖出的状态。 diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 556613e587..1ce0bc11a7 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -31,7 +31,7 @@ 以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210827175432.png) +![](https://file.kamacoder.com/pics/20210827175432.png) 本题只需要求出最短路径的长度就可以了,不用找出路径。 diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" index 4eeb57143e..278c12eccc 100644 --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -8,7 +8,7 @@ 给你一个 m x n 的矩阵 board ,由若干字符 'X' 和 'O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220901104745.png) +![](https://file.kamacoder.com/pics/20220901104745.png) * 输入:board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]] * 输出:[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]] @@ -28,11 +28,11 @@ 步骤一:深搜或者广搜将地图周边的'O'全部改成'A',如图所示: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902102337.png) +![图一](https://file.kamacoder.com/pics/20220902102337.png) 步骤二:在遍历地图,将'O'全部改成'X'(地图中间的'O'改成了'X'),将'A'改回'O'(保留的地图周边的'O'),如图所示: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902102831.png) +![图二](https://file.kamacoder.com/pics/20220902102831.png) 整体C++代码如下,以下使用dfs实现,其实遍历方式dfs,bfs都是可以的。 diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" index 2117a44801..089dd52c78 100644 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -161,7 +161,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 以输入:"aabc" 为例: -![132.分割回文串II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210124182218844.jpg) +![132.分割回文串II](https://file.kamacoder.com/pics/20210124182218844.jpg) 以上分析完毕,代码如下: diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index fdf3e0d3db..354f642448 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -144,7 +144,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230117165628.png) +![](https://file.kamacoder.com/pics/20230117165628.png) 那么为什么一旦[0,i] 区间和为负数,起始位置就可以是i+1呢,i+1后面就不会出现更大的负数? @@ -152,7 +152,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i 那有没有可能 [0,i] 区间 选某一个作为起点,累加到 i这里 curSum是不会小于零呢? 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230117170703.png) +![](https://file.kamacoder.com/pics/20230117170703.png) 如果 curSum<0 说明 区间和1 + 区间和2 < 0, 那么 假设从上图中的位置开始计数curSum不会小于0的话,就是 区间和2>0。 diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index 75cce157c6..30df21495e 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -56,7 +56,7 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117114916878.png) +![135.分发糖果](https://file.kamacoder.com/pics/20201117114916878.png) 再确定左孩子大于右孩子的情况(从后向前遍历) @@ -66,7 +66,7 @@ for (int i = 1; i < ratings.size(); i++) { 如果从前向后遍历,rating[5]与rating[4]的比较 就不能用上 rating[5]与rating[6]的比较结果了 。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230202102044.png) +![](https://file.kamacoder.com/pics/20230202102044.png) **所以确定左孩子大于右孩子的情况一定要从后向前遍历!** @@ -82,7 +82,7 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117115658791.png) +![135.分发糖果1](https://file.kamacoder.com/pics/20201117115658791.png) 所以该过程代码如下: diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index b74d2cdfa0..513d327ba1 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -180,7 +180,7 @@ dp[0]表示如果字符串为空的话,说明出现在字典里。 以输入: s = "leetcode", wordDict = ["leet", "code"]为例,dp状态如图: -![139.单词拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202162652727.jpg) +![139.单词拆分](https://file.kamacoder.com/pics/20210202162652727.jpg) dp[s.size()]就是最终结果。 @@ -241,7 +241,7 @@ public: 使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240809155103.png) +![](https://file.kamacoder.com/pics/20240809155103.png) 最后dp[s.size()] = 0 即 dp[13] = 0 ,而不是1,因为先用 "apple" 去遍历的时候,dp[8]并没有被赋值为1 (还没用"pen"),所以 dp[13]也不能变成1。 diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" index 4957e9fbd5..685a92d529 100644 --- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" +++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" @@ -13,7 +13,7 @@ 如果链表中存在环,则返回 true 。 否则,返回 false 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727173600.png) +![](https://file.kamacoder.com/pics/20210727173600.png) ## 思路 diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index fb8b875f7d..6cfabc60f6 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -20,7 +20,7 @@ **说明**:不允许修改给定的链表。 -![循环链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20200816110112704.png) +![循环链表](https://file.kamacoder.com/pics/20200816110112704.png) ## 算法公开课 @@ -50,7 +50,7 @@ 会发现最终都是这种情况, 如下图: -![142环形链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318162236720.png) +![142环形链表1](https://file.kamacoder.com/pics/20210318162236720.png) fast和slow各自再走一步, fast和slow就相遇了 @@ -70,7 +70,7 @@ fast和slow各自再走一步, fast和slow就相遇了 环形入口节点到 fast指针与slow指针相遇节点 节点数为y。 从相遇节点 再到环形入口节点节点数为 z。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220925103433.png) +![](https://file.kamacoder.com/pics/20220925103433.png) 那么相遇时: slow指针走过的节点数为: `x + y`, @@ -154,20 +154,20 @@ public: 即文章[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)中如下的地方: -![142环形链表5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318165123581.png) +![142环形链表5](https://file.kamacoder.com/pics/20210318165123581.png) 首先slow进环的时候,fast一定是先进环来了。 如果slow进环入口,fast也在环入口,那么把这个环展开成直线,就是如下图的样子: -![142环形链表3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816503266.png) +![142环形链表3](https://file.kamacoder.com/pics/2021031816503266.png) 可以看出如果slow 和 fast同时在环入口开始走,一定会在环入口3相遇,slow走了一圈,fast走了两圈。 重点来了,slow进环的时候,fast一定是在环的任意一个位置,如图: -![142环形链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816515727.png) +![142环形链表4](https://file.kamacoder.com/pics/2021031816515727.png) 那么fast指针走到环入口3的时候,已经走了k + n 个节点,slow相应的应该走了(k + n) / 2 个节点。 diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" index c61eb4b403..98488bc11d 100644 --- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" +++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" @@ -6,7 +6,7 @@ [力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726160122.png) +![](https://file.kamacoder.com/pics/20210726160122.png) ## 思路 diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index b182d4d03d..a3fc7ef126 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -132,7 +132,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229100358221.png) +![188.买卖股票的最佳时机IV](https://file.kamacoder.com/pics/20201229100358221.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 3d06c95241..0bee40f7cd 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -87,7 +87,7 @@ for (int i = 2; i < nums.size(); i++) { 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://code-thinking-1253855093.file.myqcloud.com/pics/20210221170954115.jpg) +![198.打家劫舍](https://file.kamacoder.com/pics/20210221170954115.jpg) 红框dp[nums.size() - 1]为结果。 diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 4901934bfe..9ea47329bb 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -13,7 +13,7 @@ 此外,你可以假设该网格的四条边均被水包围。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726093256.png) +![](https://file.kamacoder.com/pics/20220726093256.png) 提示: @@ -28,7 +28,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -48,7 +48,7 @@ 如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png) +![图二](https://file.kamacoder.com/pics/20220727100846.png) 超时写法 (从队列中取出节点再标记) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index a3f6f48c76..a015399859 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -12,7 +12,7 @@ 此外,你可以假设该网格的四条边均被水包围。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726093256.png) +![](https://file.kamacoder.com/pics/20220726093256.png) 提示: @@ -27,7 +27,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 9a38aaa152..5ecf89bf19 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -34,11 +34,11 @@ 这里以链表 1 4 2 4 来举例,移除元素4。 -![203_链表删除元素1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095351161.png) +![203_链表删除元素1](https://file.kamacoder.com/pics/20210316095351161.png) 如果使用C,C++编程语言的话,不要忘了还要从内存中删除这两个移除的节点, 清理节点内存之后如图: -![203_链表删除元素2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095418280.png) +![203_链表删除元素2](https://file.kamacoder.com/pics/20210316095418280.png) **当然如果使用java ,python的话就不用手动管理内存了。** @@ -56,16 +56,16 @@ 来看第一种操作:直接使用原来的链表来进行移除。 -![203_链表删除元素3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031609544922.png) +![203_链表删除元素3](https://file.kamacoder.com/pics/2021031609544922.png) 移除头结点和移除其他节点的操作是不一样的,因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点。 所以头结点如何移除呢,其实只要将头结点向后移动一位就可以,这样就从链表中移除了一个头结点。 -![203_链表删除元素4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095512470.png) +![203_链表删除元素4](https://file.kamacoder.com/pics/20210316095512470.png) 依然别忘将原头结点从内存中删掉。 -![203_链表删除元素5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095543775.png) +![203_链表删除元素5](https://file.kamacoder.com/pics/20210316095543775.png) 这样移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。 @@ -76,7 +76,7 @@ 来看看如何设置一个虚拟头。依然还是在这个链表中,移除元素1。 -![203_链表删除元素6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095619221.png) +![203_链表删除元素6](https://file.kamacoder.com/pics/20210316095619221.png) 这里来给链表添加一个虚拟头结点为新的头结点,此时要移除这个旧头结点元素1。 diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index 7509882f08..4e33342a67 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -27,7 +27,7 @@ 其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: -![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png) +![206_反转链表](https://file.kamacoder.com/pics/20210218090901207.png) 之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 43a3cb6a2b..ca24bc4234 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -104,7 +104,7 @@ public: 解题的关键在于 窗口的起始位置如何移动,如图所示: -![leetcode_209](https://code-thinking-1253855093.file.myqcloud.com/pics/20210312160441942.png) +![leetcode_209](https://file.kamacoder.com/pics/20210312160441942.png) 可以发现**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)暴力解法降为O(n)。** diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index 536e1e89b8..8fceb0a91e 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -42,15 +42,15 @@ * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160748643-20230310134000692.jpg) +![213.打家劫舍II](https://file.kamacoder.com/pics/20210129160748643-20230310134000692.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160821374-20230310134003961.jpg) +![213.打家劫舍II1](https://file.kamacoder.com/pics/20210129160821374-20230310134003961.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160842491-20230310134008133.jpg) +![213.打家劫舍II2](https://file.kamacoder.com/pics/20210129160842491-20230310134008133.jpg) **注意我这里用的是"考虑"**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素! 对于情况三,取nums[1] 和 nums[3]就是最大的。 diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index 3dbd676a8d..e23be78d5e 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -45,7 +45,7 @@ 选取过程如图: -![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975.png) +![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975.png) 图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。 @@ -108,7 +108,7 @@ if (path.size() == k) { 本题和[77. 组合](https://programmercarl.com/0077.组合.html)区别之一就是集合固定的就是9个数[1,...,9],所以for循环固定i<=9 如图: -![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975-20230310113546003.png) +![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975-20230310113546003.png) 处理过程就是 path收集每次选取的元素,相当于树型结构里的边,sum来统计path里元素的总和。 @@ -166,7 +166,7 @@ public: 这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。 如图: -![216.组合总和III1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112319580476.png) +![216.组合总和III1](https://file.kamacoder.com/pics/2020112319580476.png) 已选元素总和如果已经大于n(图中数值为4)了,那么往后遍历就没有意义了,直接剪掉。 diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index 9b649d7be8..37ae7819aa 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -153,7 +153,7 @@ public: 我来举一个典型的例子如题: - + 完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。 @@ -162,10 +162,10 @@ public: 对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。 完全二叉树(一)如图: -![222.完全二叉树的节点个数](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092543662.png) +![222.完全二叉树的节点个数](https://file.kamacoder.com/pics/20201124092543662.png) 完全二叉树(二)如图: -![222.完全二叉树的节点个数1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092634138.png) +![222.完全二叉树的节点个数1](https://file.kamacoder.com/pics/20201124092634138.png) 可以看出如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量。 @@ -173,15 +173,15 @@ public: 在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163554.png) +![](https://file.kamacoder.com/pics/20220829163554.png) 在完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163709.png) +![](https://file.kamacoder.com/pics/20220829163709.png) 那有录友说了,这种情况,递归向左遍历的深度等于递归向右遍历的深度,但也不是满二叉树,如题: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163811.png) +![](https://file.kamacoder.com/pics/20220829163811.png) 如果这么想,大家就是对 完全二叉树理解有误区了,**以上这棵二叉树,它根本就不是一个完全二叉树**! diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index 0980e6004c..248a28a4d5 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -10,7 +10,7 @@ 翻转一棵二叉树。 -![226.翻转二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192644329.png) +![226.翻转二叉树](https://file.kamacoder.com/pics/20210203192644329.png) 这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,全当一个乐子哈) @@ -35,7 +35,7 @@ 如果要从整个树来看,翻转还真的挺复杂,整个树以中间分割线进行翻转,如图: -![226.翻转二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192724351.png) +![226.翻转二叉树1](https://file.kamacoder.com/pics/20210203192724351.png) 可以发现想要翻转它,其实就把每一个节点的左右孩子交换一下就可以了。 diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index c5eb603a0d..98cc5b7da8 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -14,7 +14,7 @@ 例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5] -![235. 二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201018172243602.png) +![235. 二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20201018172243602.png) 示例 1: @@ -52,7 +52,7 @@ 如图,我们从根节点搜索,第一次遇到 cur节点是数值在[q, p]区间中,即 节点5,此时可以说明 q 和 p 一定分别存在于 节点 5的左子树,和右子树中。 -![235.二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926164214.png) +![235.二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20220926164214.png) 此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为p的祖先, 如果从节点5继续向右遍历则错过成为q的祖先。 @@ -64,7 +64,7 @@ 如图所示:p为节点6,q为节点9 -![235.二叉搜索树的最近公共祖先2](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926165141.png) +![235.二叉搜索树的最近公共祖先2](https://file.kamacoder.com/pics/20220926165141.png) 可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回! diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index f15d1cff60..537d624084 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -16,7 +16,7 @@ 例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4] -![236. 二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201016173414722.png) +![236. 二叉树的最近公共祖先](https://file.kamacoder.com/pics/20201016173414722.png) 示例 1: 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 @@ -51,7 +51,7 @@ **首先最容易想到的一个情况:如果找到一个节点,发现左子树出现结点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。** 即情况一: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922173502.png) +![](https://file.kamacoder.com/pics/20220922173502.png) 判断逻辑是 如果递归遍历遇到q,就将q返回,遇到p 就将p返回,那么如果 左右子树的返回值都不为空,说明此时的中节点,一定是q 和p 的最近祖先。 @@ -61,7 +61,7 @@ **但是很多人容易忽略一个情况,就是节点本身p(q),它拥有一个子孙节点q(p)。** 情况二: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922173530.png) +![](https://file.kamacoder.com/pics/20220922173530.png) 其实情况一 和 情况二 代码实现过程都是一样的,也可以说,实现情况一的逻辑,顺便包含了情况二。 @@ -129,7 +129,7 @@ left与right的逻辑处理; // 中 如图: -![236.二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415105872.png) +![236.二叉树的最近公共祖先](https://file.kamacoder.com/pics/2021020415105872.png) 就像图中一样直接返回7。 @@ -162,7 +162,7 @@ TreeNode* right = lowestCommonAncestor(root->right, p, q); 如图: -![236.二叉树的最近公共祖先1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151125844.png) +![236.二叉树的最近公共祖先1](https://file.kamacoder.com/pics/20210204151125844.png) 图中节点10的左子树返回null,右子树返回目标值7,那么此时节点10的处理逻辑就是把右子树的返回值(最近公共祖先7)返回上去! @@ -183,7 +183,7 @@ else { // (left == NULL && right == NULL) 那么寻找最小公共祖先,完整流程图如下: -![236.二叉树的最近公共祖先2](https://code-thinking-1253855093.file.myqcloud.com/pics/202102041512582.png) +![236.二叉树的最近公共祖先2](https://file.kamacoder.com/pics/202102041512582.png) **从图中,大家可以看到,我们是如何回溯遍历整棵二叉树,将结果返回给头结点的!** diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 287db20937..5d71351172 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -14,7 +14,7 @@ 说明: 叶子节点是指没有子节点的节点。 示例: -![257.二叉树的所有路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415161576.png) +![257.二叉树的所有路径1](https://file.kamacoder.com/pics/2021020415161576.png) ## 算法公开课 @@ -28,7 +28,7 @@ 前序遍历以及回溯的过程如图: -![257.二叉树的所有路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151702443.png) +![257.二叉树的所有路径](https://file.kamacoder.com/pics/20210204151702443.png) 我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。** @@ -315,7 +315,7 @@ public: 其实关键还在于 参数,使用的是 `string path`,这里并没有加上引用`&` ,即本层递归中,path + 该节点数值,但该层递归结束,上一层path的数值并不会受到任何影响。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220831173322.png) +![](https://file.kamacoder.com/pics/20220831173322.png) 节点4 的path,在遍历到节点3,path+3,遍历节点3的递归结束之后,返回节点4(回溯的过程),path并不会把3加上。 diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index c1077bd43d..8171a409a0 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -93,7 +93,7 @@ for (int i = 0; i <= n; i++) { // 遍历背包 已输入n为5例,dp状态图如下: -![279.完全平方数](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202112617341.jpg) +![279.完全平方数](https://file.kamacoder.com/pics/20210202112617341.jpg) dp[0] = 0 dp[1] = min(dp[0] + 1) = 1 diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 7d2e488623..de37ed5cc8 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -85,7 +85,7 @@ for (int i = 1; i < nums.size(); i++) { 输入:[0,1,0,3,2],dp数组的变化如下: -![300.最长上升子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110170945618.jpg) +![300.最长上升子序列](https://file.kamacoder.com/pics/20210110170945618.jpg) 如果代码写出来,但一直AC不了,那么就把dp数组打印出来,看看对不对! diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 6a81933505..599a1f42e1 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -47,7 +47,7 @@ dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]。 * 状态三:今天卖出股票 * 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天! -![](https://code-thinking-1253855093.file.myqcloud.com/pics/518d5baaf33f4b2698064f8efb42edbf.png) +![](https://file.kamacoder.com/pics/518d5baaf33f4b2698064f8efb42edbf.png) j的状态为: @@ -136,7 +136,7 @@ dp[i][3] = dp[i - 1][2]; 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032317451040.png) +![309.最佳买卖股票时机含冷冻期](https://file.kamacoder.com/pics/2021032317451040.png) 最后结果是取 状态二,状态三,和状态四的最大值,不少同学会把状态四忘了,状态四是冷冻期,最后一天如果是冷冻期也可能是最大值。 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 dea77a3d10..7f3bc1e4b1 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" @@ -104,7 +104,7 @@ dp[0] = 0; 以输入:coins = [1, 2, 5], amount = 5为例 -![322.零钱兑换](https://code-thinking-1253855093.file.myqcloud.com/pics/20210201111833906.jpg) +![322.零钱兑换](https://file.kamacoder.com/pics/20210201111833906.jpg) dp[amount]为最终结果。 diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index f1df25229d..fcdb6a33ed 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -57,7 +57,7 @@ 对于死循环,我来举一个有重复机场的例子: -![332.重新安排行程](https://code-thinking-1253855093.file.myqcloud.com/pics/20201115180537865.png) +![332.重新安排行程](https://file.kamacoder.com/pics/20201115180537865.png) 为什么要举这个例子呢,就是告诉大家,出发机场和到达机场也会重复的,**如果在解题的过程中没有对集合元素处理好,就会死循环。** @@ -111,7 +111,7 @@ void backtracking(参数) { 本题以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![332.重新安排行程1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555-20230310121223600.png) +![332.重新安排行程1](https://file.kamacoder.com/pics/2020111518065555-20230310121223600.png) 开始回溯三部曲讲解: @@ -137,7 +137,7 @@ bool backtracking(int ticketNum, vector& result) { 因为我们只需要找到一个行程,就是在树形结构中唯一的一条通向叶子节点的路线,如图: -![332.重新安排行程1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555-20230310121240991.png) +![332.重新安排行程1](https://file.kamacoder.com/pics/2020111518065555-20230310121240991.png) 所以找到了这个叶子节点了直接返回,这个递归函数的返回值问题我们在讲解二叉树的系列的时候,在这篇[二叉树:递归函数究竟什么时候需要返回值,什么时候不要返回值?](https://programmercarl.com/0112.路径总和.html)详细介绍过。 diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 08728e4faf..4916af4c26 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -12,7 +12,7 @@ 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 -![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png) +![337.打家劫舍III](https://file.kamacoder.com/pics/20210223173849619.png) ## 算法公开课 @@ -177,7 +177,7 @@ return {val2, val1}; 以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203110031.png) +![](https://file.kamacoder.com/pics/20230203110031.png) **最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。 diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 06549185a3..203c422879 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -127,7 +127,7 @@ for (int i = 3; i <= n ; i++) { 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104173021581.png) +![343.整数拆分](https://file.kamacoder.com/pics/20210104173021581.png) 以上动规五部曲分析完毕,C++代码如下: diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 5066bff1b9..65d22a809c 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -14,7 +14,7 @@ 题意:给定两个数组,编写一个函数来计算它们的交集。 -![349. 两个数组的交集](https://code-thinking-1253855093.file.myqcloud.com/pics/20200818193523911.png) +![349. 两个数组的交集](https://file.kamacoder.com/pics/20200818193523911.png) **说明:** 输出结果中的每个元素一定是唯一的。 @@ -51,7 +51,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 思路如图所示: -![set哈希法](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707173513.png) +![set哈希法](https://file.kamacoder.com/pics/20220707173513.png) C++代码如下: diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 886d86aefe..50934981a6 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -46,7 +46,7 @@ 用示例二来举例,如图所示: -![376.摆动序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124174327597.png) +![376.摆动序列](https://file.kamacoder.com/pics/20201124174327597.png) **局部最优:删除单调坡度上的节点(不包括单调坡度两端的节点),那么这个坡度就可以有两个局部峰值**。 @@ -72,13 +72,13 @@ 例如 [1,2,2,2,2,1]这样的数组,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106170449.png) +![](https://file.kamacoder.com/pics/20230106170449.png) 它的摇摆序列长度是多少呢? **其实是长度是 3**,也就是我们在删除的时候 要不删除左面的三个 2,要不就删除右边的三个 2。 如图,可以统一规则,删除左边的三个 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230106172613.png) +![](https://file.kamacoder.com/pics/20230106172613.png) 在图中,当 i 指向第一个 2 的时候,`prediff > 0 && curdiff = 0` ,当 i 指向最后一个 2 的时候 `prediff = 0 && curdiff < 0`。 @@ -106,7 +106,7 @@ 那么为了规则统一,针对序列[2,5],可以假设为[2,2,5],这样它就有坡度了即 preDiff = 0,如图: -![376.摆动序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124174357612.png) +![376.摆动序列1](https://file.kamacoder.com/pics/20201124174357612.png) 针对以上情形,result 初始为 1(默认最右面有一个峰值),此时 curDiff > 0 && preDiff <= 0,那么 result++(计算了左面的峰值),最后得到的 result 就是 2(峰值个数为 2 即摆动序列长度为 2) @@ -145,7 +145,7 @@ public: 在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108171505.png) +![](https://file.kamacoder.com/pics/20230108171505.png) 图中,我们可以看出,版本一的代码在三个地方记录峰值,但其实结果因为是 2,因为 单调中的平坡 不能算峰值(即摆动)。 @@ -184,7 +184,7 @@ public: **本题异常情况的本质,就是要考虑平坡**, 平坡分两种,一个是 上下中间有平坡,一个是单调有平坡,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108174452.png) +![](https://file.kamacoder.com/pics/20230108174452.png) ### 思路 2(动态规划) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index d2feb0c5d6..20a94331c4 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -103,7 +103,7 @@ dp[i](考虑nums[j])可以由 dp[i - nums[j]](不考虑nums[j]) 推导 我们再来用示例中的例子推导一下: -![377.组合总和Ⅳ](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000625.png) +![377.组合总和Ⅳ](https://file.kamacoder.com/pics/20230310000625.png) 如果代码运行处的结果不是想要的结果,就把dp[i]都打出来,看看和我们推导的一不一样。 diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index 2a5be51c59..d59b7bc121 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -80,7 +80,7 @@ if (s[i - 1] != t[j - 1]),此时相当于t要删除元素,t如果把当前 因为这样的定义在dp二维矩阵中可以留出初始化的区间,如图: -![392.判断子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303173115966.png) +![392.判断子序列](https://file.kamacoder.com/pics/20210303173115966.png) 如果要是定义的dp[i][j]是以下标i为结尾的字符串s和以下标j为结尾的字符串t,初始化就比较麻烦了。 @@ -98,14 +98,14 @@ vector> dp(s.size() + 1, vector(t.size() + 1, 0)); 如图所示: -![392.判断子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303172354155.jpg) +![392.判断子序列1](https://file.kamacoder.com/pics/20210303172354155.jpg) 5. 举例推导dp数组 以示例一为例,输入:s = "abc", t = "ahbgdc",dp状态转移图如下: -![392.判断子序列2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021030317364166.jpg) +![392.判断子序列2](https://file.kamacoder.com/pics/2021030317364166.jpg) dp[i][j]表示以下标i-1为结尾的字符串s和以下标j-1为结尾的字符串t 相同子序列的长度,所以如果dp[s.size()][t.size()] 与 字符串s的长度相同说明:s与t的最长相同子序列就是s,那么s 就是 t 的子序列。 diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 0efdb6f663..69723815a1 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -12,7 +12,7 @@ 示例: -![404.左叶子之和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151927654.png) +![404.左叶子之和1](https://file.kamacoder.com/pics/20210204151927654.png) ## 算法公开课 @@ -26,12 +26,12 @@ 大家思考一下如下图中二叉树,左叶子之和究竟是多少? -![404.左叶子之和](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151949672.png) +![404.左叶子之和](https://file.kamacoder.com/pics/20210204151949672.png) **其实是0,因为这棵树根本没有左叶子!** 但看这个图的左叶子之和是多少? -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902165805.png) +![图二](https://file.kamacoder.com/pics/20220902165805.png) 相信通过这两个图,大家对最左叶子的定义有明确理解了。 diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 11853e1170..0d060ee837 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -61,7 +61,7 @@ 以图中{5,2} 为例: -![406.根据身高重建队列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216201851982.png) +![406.根据身高重建队列](https://file.kamacoder.com/pics/20201216201851982.png) 按照身高排序之后,优先按身高高的people的k来插入,后序插入节点也不会影响前面已经插入的节点,最终按照k的规则完成了队列。 diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 9cc6db0e4b..79b4d4f75a 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -155,7 +155,7 @@ dp[j]的数值一定是小于等于j的。 用例1,输入[1,5,11,5] 为例,如图: -![416.分割等和子集2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110104240545.png) +![416.分割等和子集2](https://file.kamacoder.com/pics/20210110104240545.png) 最后dp[11] == 11,说明可以将这个数组分割成两个子集,使得两个子集的元素和相等。 diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" index ec87eb9595..116cd08e09 100644 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -18,7 +18,7 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230129103212.png) +![](https://file.kamacoder.com/pics/20230129103212.png) * 输入: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] * 输出: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] @@ -130,11 +130,11 @@ public: 从太平洋边上节点出发,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220722103029.png) +![图一](https://file.kamacoder.com/pics/20220722103029.png) 从大西洋边上节点出发,如图: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220722103330.png) +![图二](https://file.kamacoder.com/pics/20220722103330.png) 按照这样的逻辑,就可以写出如下遍历代码:(详细注释) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index a37d1cadac..04845ea7c3 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -44,7 +44,7 @@ 这里记录非交叉区间的个数还是有技巧的,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230201164134.png) +![](https://file.kamacoder.com/pics/20230201164134.png) 区间,1,2,3,4,5,6都按照右边界排好序。 diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 7280918460..406116a388 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -20,7 +20,7 @@ 示例: -![450.删除二叉搜索树中的节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20201020171048265.png) +![450.删除二叉搜索树中的节点](https://file.kamacoder.com/pics/20201020171048265.png) ## 算法公开课 diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index 854498829b..17d21cd1c4 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -76,7 +76,7 @@ 以题目示例: [[10,16],[2,8],[1,6],[7,12]]为例,如图:(方便起见,已经排序) -![452.用最少数量的箭引爆气球](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123101929791.png) +![452.用最少数量的箭引爆气球](https://file.kamacoder.com/pics/20201123101929791.png) 可以看出首先第一组重叠气球,一定是需要一个箭,气球3,的左边界大于了 第一组重叠气球的最小右边界,所以再需要一支箭来射气球3了。 diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index a2a1b1f339..2a6ade1b64 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -46,7 +46,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230405225628.png) +![](https://file.kamacoder.com/pics/20230405225628.png) 这个例子可以看出饼干 9 只有喂给胃口为 7 的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。 @@ -89,7 +89,7 @@ public: 如果 for 控制的是饼干, if 控制胃口,就是出现如下情况 : -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230112102848.png) +![](https://file.kamacoder.com/pics/20230112102848.png) if 里的 index 指向 胃口 10, for 里的 i 指向饼干 9,因为 饼干 9 满足不了 胃口 10,所以 i 持续向前移动,而 index 走不到` s[index] >= g[i]` 的逻辑,所以 index 不会移动,那么当 i 持续向前移动,最后所有的饼干都匹配不上。 diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index 78aad3e786..627a27a48e 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -46,13 +46,13 @@ 当一个字符串s:abcabc,内部由重复的子串组成,那么这个字符串的结构一定是这样的: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728104518.png) +![图一](https://file.kamacoder.com/pics/20220728104518.png) 也就是由前后相同的子串组成。 那么既然前面有相同的子串,后面有相同的子串,用 s + s,这样组成的字符串中,后面的子串做前串,前面的子串做后串,就一定还能组成一个s,如图: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728104931.png) +![图二](https://file.kamacoder.com/pics/20220728104931.png) 当然,我们在判断 s + s 拼接的字符串里是否出现一个s的的时候,**要刨除 s + s 的首字符和尾字符**,这样避免在s+s中搜索出原来的s,我们要搜索的是中间拼接出来的s。 @@ -64,11 +64,11 @@ 如图,字符串s,图中数字为数组下标,在 s + s 拼接后, 不算首尾字符,中间凑成s字符串。 (图中数字为数组下标) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910115555.png) +![](https://file.kamacoder.com/pics/20240910115555.png) 图中,因为中间拼接成了s,根据红色框 可以知道 s[4] = s[0], s[5] = s[1], s[0] = s[2], s[1] = s[3] s[2] = s[4] ,s[3] = s[5] -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910115819.png) +![](https://file.kamacoder.com/pics/20240910115819.png) 以上相等关系我们串联一下: @@ -83,7 +83,7 @@ s[5] = s[1] = s[3] 这里可以有录友想,凭什么就是这样组成的s呢,我换一个方式组成s 行不行,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910120751.png) +![](https://file.kamacoder.com/pics/20240910120751.png) s[3] = s[0],s[4] = s[1] ,s[5] = s[2],s[0] = s[3],s[1] = s[4],s[2] = s[5] @@ -101,7 +101,7 @@ s[0] s[1] s[2] = s[3] s[4] s[5] 如果是这样的呢,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910121236.png) +![](https://file.kamacoder.com/pics/20240910121236.png) s[1] = s[0],s[2] = s[1] ,s[3] = s[2],s[4] = s[3],s[5] = s[4],s[0] = s[5] @@ -165,23 +165,23 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 那么相同前后缀可以是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110257.png) +![](https://file.kamacoder.com/pics/20240913110257.png) 也可以是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110316.png) +![](https://file.kamacoder.com/pics/20240913110316.png) 最长的相等前后缀,也就是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913110841.png) +![](https://file.kamacoder.com/pics/20240913110841.png) 这里有录友就想:如果字符串s 是由最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114348.png) +![](https://file.kamacoder.com/pics/20240913114348.png) 如果这样的话,因为前后缀要相同,所以 p2 = p1,p3 = p2,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913114818.png) +![](https://file.kamacoder.com/pics/20240913114818.png) p2 = p1,p3 = p2 即: p1 = p2 = p3 @@ -203,7 +203,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 情况一, 最长相等前后缀不包含的子串的长度 比 字符串s的一半的长度还大,那一定不是字符串s的重复子串,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240911110236.png) +![](https://file.kamacoder.com/pics/20240911110236.png) 图中:前后缀不包含的子串的长度 大于 字符串s的长度的 二分之一 @@ -211,7 +211,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 情况二,最长相等前后缀不包含的子串的长度 可以被 字符串s的长度整除,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910174249.png) +![](https://file.kamacoder.com/pics/20240910174249.png) 步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,所以 s[0] 一定和 s[2]相同,s[1] 一定和 s[3]相同,即:,s[0]s[1]与s[2]s[3]相同 。 @@ -234,7 +234,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 那么它的最长相同前后缀,就不是上图中的前后缀,而是这样的的前后缀: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240910175053.png) +![](https://file.kamacoder.com/pics/20240910175053.png) 录友可能再问,由一个字符组成的字符串,最长相等前后缀凭什么就是这样的。 @@ -250,7 +250,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 **情况三,最长相等前后缀不包含的子串的长度 不被 字符串s的长度整除得情况**,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240913115854.png) +![](https://file.kamacoder.com/pics/20240913115854.png) 步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,t[2] 与 k[2]相同。 diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index bff619ccde..40ddc57d63 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -15,7 +15,7 @@ 岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230829180848.png) +![](https://file.kamacoder.com/pics/20230829180848.png) * 输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] * 输出:16 diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index ca525ab2e3..8166b39aad 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -51,7 +51,7 @@ 其实本题并不是多重背包,再来看一下这个图,捋清几种背包的关系 -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407-20230310132423205.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407-20230310132423205.png) 多重背包是每个物品,数量不同的情况。 @@ -127,7 +127,7 @@ for (string str : strs) { // 遍历物品 最后dp数组的状态如下所示: -![474.一和零](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120111201512.jpg) +![474.一和零](https://file.kamacoder.com/pics/20210120111201512.jpg) 以上动规五部曲分析完毕,C++代码如下: diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 1b927dd3d2..b3171c8a12 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -45,7 +45,7 @@ 为了有鲜明的对比,我用[4, 7, 6, 7]这个数组来举例,抽象为树形结构如图: -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124200229824.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201124200229824.png) @@ -79,7 +79,7 @@ if (path.size() > 1) { * 单层搜索逻辑 -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124200229824-20230310131640070.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201124200229824-20230310131640070.png) 在图中可以看出,**同一父节点下的同层上使用过的元素就不能再使用了** 那么单层搜索代码如下: diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index bde843ead3..a23e1743cb 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -163,7 +163,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 先只考虑物品0,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808161747.png) +![](https://file.kamacoder.com/pics/20240808161747.png) (这里的所有物品,都是题目中的数字1)。 @@ -177,7 +177,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 接下来 考虑 物品0 和 物品1,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162052.png) +![](https://file.kamacoder.com/pics/20240808162052.png) 装满背包容量为0 的方法个数是1,即 放0件物品。 @@ -191,7 +191,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 接下来 考虑 物品0 、物品1 和 物品2 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808162533.png) +![](https://file.kamacoder.com/pics/20240808162533.png) 装满背包容量为0 的方法个数是1,即 放0件物品。 @@ -207,17 +207,17 @@ if (abs(target) > sum) return 0; // 此时没有方案 如图红色部分: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240808163312.png) +![](https://file.kamacoder.com/pics/20240808163312.png) dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物品1 和 物品2, 如图所示,三种方法: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826111946.png) +![](https://file.kamacoder.com/pics/20240826111946.png) **容量为2 的背包,如果不放 物品2 有几种方法呢**? 有 dp[1][2] 种方法,即 背包容量为2,只考虑物品0 和 物品1 ,有 dp[1][2] 种方法,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826112805.png) +![](https://file.kamacoder.com/pics/20240826112805.png) **容量为2 的背包, 如果放 物品2 有几种方法呢**? @@ -229,7 +229,7 @@ dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826113043.png) +![](https://file.kamacoder.com/pics/20240826113043.png) 有录友可能疑惑,这里计算的是放满 容量为2的背包 有几种方法,那物品2去哪了? @@ -239,7 +239,7 @@ dp[2][2] = 容量为2的背包不放物品2有几种方法 + 容量为2的背包 所以 dp[2][2] = dp[1][2] + dp[1][1] ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826113258.png) +![](https://file.kamacoder.com/pics/20240826113258.png) 以上过程,抽象化如下: @@ -266,11 +266,11 @@ else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; 先明确递推的方向,如图,求解 dp[2][2] 是由 上方和左上方推出。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240826115800.png) +![](https://file.kamacoder.com/pics/20240826115800.png) 那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827103507.png) +![](https://file.kamacoder.com/pics/20240827103507.png) 关于dp[0][0]的值,在上面的递推公式讲解中已经讲过,装满背包容量为0 的方法数量是1,即 放0件物品。 @@ -323,7 +323,7 @@ for (int i = 0; i < nums.size(); i++) { 例如下图,如果上方没数值,左上方没数值,就无法推出 dp[2][2]。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827105427.png) +![](https://file.kamacoder.com/pics/20240827105427.png) 那么是先 从上到下 ,再从左到右遍历,例如这样: @@ -349,11 +349,11 @@ for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 这里我再画图讲一下,以求dp[2][2]为例,当先从上到下,再从左到右遍历,矩阵是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827110933.png) +![](https://file.kamacoder.com/pics/20240827110933.png) 当先从左到右,再从上到下遍历,矩阵是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827111013.png) +![](https://file.kamacoder.com/pics/20240827111013.png) 这里大家可以看出,无论是以上哪种遍历,都不影响 dp[2][2]的求值,用来 推导 dp[2][2] 的数值都在。 @@ -366,7 +366,7 @@ bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827111612.png) +![](https://file.kamacoder.com/pics/20240827111612.png) 这么大的矩阵,我们是可以自己手动模拟出来的。 @@ -445,7 +445,7 @@ bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210125120743274.jpg) +![](https://file.kamacoder.com/pics/20210125120743274.jpg) 大家可以和 二维dp数组的打印结果做一下对比。 diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 32a89e859a..8cca8e65c8 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -23,7 +23,7 @@ 给定 BST [1,null,2,2], -![501. 二叉搜索树中的众数](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014221532206.png) +![501. 二叉搜索树中的众数](https://file.kamacoder.com/pics/20201014221532206.png) 返回[2]. @@ -144,7 +144,7 @@ public: 如图: -![501.二叉搜索树中的众数1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204152758889.png) +![501.二叉搜索树中的众数1](https://file.kamacoder.com/pics/20210204152758889.png) 中序遍历代码如下: diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index da37360374..4098cb7bfb 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -12,11 +12,11 @@ 示例 1: -![513.找树左下角的值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204152956836.png) +![513.找树左下角的值](https://file.kamacoder.com/pics/20210204152956836.png) 示例 2: -![513.找树左下角的值1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153017586.png) +![513.找树左下角的值1](https://file.kamacoder.com/pics/20210204153017586.png) ## 算法公开课 diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index f0ef2f53d0..5e456ac975 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -56,7 +56,7 @@ 如果s[i]与s[j]相同,那么dp[i][j] = dp[i + 1][j - 1] + 2; 如图: -![516.最长回文子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151350563.jpg) +![516.最长回文子序列](https://file.kamacoder.com/pics/20210127151350563.jpg) (如果这里看不懂,回忆一下dp[i][j]的定义) @@ -68,7 +68,7 @@ 那么dp[i][j]一定是取最大的,即:dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); -![516.最长回文子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151420476.jpg) +![516.最长回文子序列1](https://file.kamacoder.com/pics/20210127151420476.jpg) 代码如下: @@ -97,7 +97,7 @@ for (int i = 0; i < s.size(); i++) dp[i][i] = 1; 从递归公式中,可以看出,dp[i][j] 依赖于 dp[i + 1][j - 1] ,dp[i + 1][j] 和 dp[i][j - 1],如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230102172155.png) +![](https://file.kamacoder.com/pics/20230102172155.png) **所以遍历i的时候一定要从下到上遍历,这样才能保证下一行的数据是经过计算的**。 @@ -121,7 +121,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 输入s:"cbbd" 为例,dp数组状态如图: -![516.最长回文子序列3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210127151521432.jpg) +![516.最长回文子序列3](https://file.kamacoder.com/pics/20210127151521432.jpg) 红色框即:dp[0][s.size() - 1]; 为最终结果。 diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 1698db9887..95122a7c95 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -136,7 +136,7 @@ 那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240827103507.png) +![](https://file.kamacoder.com/pics/20240827103507.png) 这里首先要关注的就是 dp[0][0] 应该是多少? @@ -296,7 +296,7 @@ for (int j = 0; j <= amount; j++) { // 遍历背包容量 输入: amount = 5, coins = [1, 2, 5] ,dp状态图如下: -![518.零钱兑换II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120181331461.jpg) +![518.零钱兑换II](https://file.kamacoder.com/pics/20210120181331461.jpg) 最后红色框dp[amount]为最终结果。 diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index d7b0e056db..466bd74479 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -13,7 +13,7 @@ 示例: -![530二叉搜索树的最小绝对差](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014223400123.png) +![530二叉搜索树的最小绝对差](https://file.kamacoder.com/pics/20201014223400123.png) 提示:树中至少有 2 个节点。 @@ -70,7 +70,7 @@ public: 如图: -![530.二叉搜索树的最小绝对差](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153247458.png) +![530.二叉搜索树的最小绝对差](https://file.kamacoder.com/pics/20210204153247458.png) 一些同学不知道在递归中如何记录前一个节点的指针,其实实现起来是很简单的,大家只要看过一次,写过一次,就掌握了。 diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index 1bbbdac76d..45bf1f96ed 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -18,7 +18,7 @@ 示例 1: -![538.把二叉搜索树转换为累加树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201023160751832.png) +![538.把二叉搜索树转换为累加树](https://file.kamacoder.com/pics/20201023160751832.png) * 输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] * 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] @@ -67,7 +67,7 @@ 遍历顺序如图所示: -![538.把二叉搜索树转换为累加树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153440666.png) +![538.把二叉搜索树转换为累加树](https://file.kamacoder.com/pics/20210204153440666.png) 本题依然需要一个pre指针记录当前遍历节点cur的前一个节点,这样才方便做累加。 diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index a86dfad195..7f7d30f6a1 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -81,7 +81,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 以word1:"sea",word2:"eat"为例,推导dp数组状态图如下: -![583.两个字符串的删除操作1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210714101750205.png) +![583.两个字符串的删除操作1](https://file.kamacoder.com/pics/20210714101750205.png) 以上分析完毕,代码如下: diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index f180c4f3b9..755200fe82 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -13,7 +13,7 @@ 示例 1: -![617.合并二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000854.png) +![617.合并二叉树](https://file.kamacoder.com/pics/20230310000854.png) 注意: 合并必须从两个树的根节点开始。 diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index e2783027aa..7282953570 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -48,7 +48,7 @@ dp[i] 和 dp[i-1] ,dp[i + 1] 看上去都没啥关系。 所以我们要看回文串的性质。 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230102170752.png) +![](https://file.kamacoder.com/pics/20230102170752.png) 我们在判断字符串S是否是回文,那么如果我们知道 s[1],s[2],s[3] 这个子串是回文的,那么只需要比较 s[0]和s[4]这两个元素是否相同,如果相同的话,这个字符串s 就是回文串。 @@ -106,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473-20230310132134822.jpg) +![647.回文子串](https://file.kamacoder.com/pics/20210121171032473-20230310132134822.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -136,7 +136,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951-20230310132153163.jpg) +![647.回文子串1](https://file.kamacoder.com/pics/20210121171059951-20230310132153163.jpg) 图中有6个true,所以就是有6个回文子串。 diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 9f897a7502..b8841a8bea 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -17,7 +17,7 @@ 示例 : -![654.最大二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204154534796.png) +![654.最大二叉树](https://file.kamacoder.com/pics/20210204154534796.png) 提示: diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 0a05360bf1..f4ded2c4fb 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -14,9 +14,9 @@ 给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。 -![669.修剪二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173115788.png) +![669.修剪二叉搜索树](https://file.kamacoder.com/pics/20201014173115788.png) -![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173219142.png) +![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20201014173219142.png) ## 算法公开课 @@ -50,7 +50,7 @@ public: 我们在重新关注一下第二个示例,如图: -![669.修剪二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155302751.png) +![669.修剪二叉搜索树](https://file.kamacoder.com/pics/20210204155302751.png) **所以以上的代码是不可行的!** @@ -60,7 +60,7 @@ public: 在上图中我们发现节点0并不符合区间要求,那么将节点0的右孩子 节点2 直接赋给 节点3的左孩子就可以了(就是把节点0从二叉树中移除),如图: -![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155327203.png) +![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20210204155327203.png) 理解了最关键部分了我们再递归三部曲: @@ -127,7 +127,7 @@ return root; 在回顾一下上面的代码,针对下图中二叉树的情况: -![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155327203-20230310120126738.png) +![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20210204155327203-20230310120126738.png) 如下代码相当于把节点0的右孩子(节点2)返回给上一层, diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" index 9bfa91cc94..92009f5b76 100644 --- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" +++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" @@ -178,7 +178,7 @@ for (int i = 0; i < nums.size(); i++) { 输入:[1,3,5,4,7] -![673.最长递增子序列的个数](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000656.png) +![673.最长递增子序列的个数](https://file.kamacoder.com/pics/20230310000656.png) **如果代码写出来了,怎么改都通过不了,那么把dp和count打印出来看看对不对!** diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 2c490c0c5b..16bb2f1887 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -85,7 +85,7 @@ for (int i = 1; i < nums.size(); i++) { 已输入nums = [1,3,5,4,7]为例,dp数组状态如下: -![674.最长连续递增序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204103529742.jpg) +![674.最长连续递增序列](https://file.kamacoder.com/pics/20210204103529742.jpg) **注意这里要取dp[i]里的最大值,所以dp[2]才是结果!** diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index e6d2d8e502..8a7234df52 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -12,7 +12,7 @@ 请找出一条可以删去的边,删除后可使得剩余部分是一个有着 n 个节点的树。如果有多个答案,则返回数组 edges 中最后出现的边。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727150215.png) +![](https://file.kamacoder.com/pics/20210727150215.png) 提示: * n == edges.length @@ -85,7 +85,7 @@ void join(int u, int v) { 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104720.png) +![](https://file.kamacoder.com/pics/20230604104720.png) 节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 @@ -95,7 +95,7 @@ void join(int u, int v) { 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104330.png) +![](https://file.kamacoder.com/pics/20230604104330.png) 已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 7b0e320c6f..66f7bfe1bc 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -16,9 +16,9 @@ 返回一条能删除的边,使得剩下的图是有 n 个节点的有根树。若有多个答案,返回最后出现在给定二维数组的答案。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727151057.png) +![](https://file.kamacoder.com/pics/20210727151057.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727151118.png) +![](https://file.kamacoder.com/pics/20210727151118.png) 提示: diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 0b84e651c1..972a999591 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -14,7 +14,7 @@ 计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220729111528.png) +![](https://file.kamacoder.com/pics/20220729111528.png) * 输入:grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] * 输出:6 @@ -27,7 +27,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上“1”的数量,然后取一个最大的。 diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 4225b3fe25..0c373f615b 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -12,7 +12,7 @@ 例如, -![700.二叉搜索树中的搜索](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204155522476.png) +![700.二叉搜索树中的搜索](https://file.kamacoder.com/pics/20210204155522476.png) 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。 @@ -124,7 +124,7 @@ public: 中间节点如果大于3就向左走,如果小于3就向右走,如图: -![二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20200812190213280.png) +![二叉搜索树](https://file.kamacoder.com/pics/20200812190213280.png) 所以迭代法代码如下: diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index ef383faa86..6ce9ef3371 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -12,7 +12,7 @@ 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。 -![701.二叉搜索树中的插入操作](https://code-thinking-1253855093.file.myqcloud.com/pics/20201019173259554.png) +![701.二叉搜索树中的插入操作](https://file.kamacoder.com/pics/20201019173259554.png) 提示: diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 405018745e..0ce2f3b8a6 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -59,7 +59,7 @@ 例如在数组:1,2,3,4,7,9,10中查找元素2,如图所示: -![704.二分查找](https://code-thinking-1253855093.file.myqcloud.com/pics/20210311153055723.jpg) +![704.二分查找](https://file.kamacoder.com/pics/20210311153055723.jpg) 代码如下:(详细注释) @@ -102,7 +102,7 @@ public: 在数组:1,2,3,4,7,9,10中查找元素2,如图所示:(**注意和方法一的区别**) -![704.二分查找1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210311153123632.jpg) +![704.二分查找1](https://file.kamacoder.com/pics/20210311153123632.jpg) 代码如下:(详细注释) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 7023bd902a..a2b2803b11 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -20,7 +20,7 @@ * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。 -![707示例](https://code-thinking-1253855093.file.myqcloud.com/pics/20200814200558953.png) +![707示例](https://file.kamacoder.com/pics/20200814200558953.png) ## 算法公开课 @@ -35,10 +35,10 @@ 如果对链表的虚拟头结点不清楚,可以看这篇文章:[链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html) 删除链表节点: -![链表-删除节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195114541.png) +![链表-删除节点](https://file.kamacoder.com/pics/20200806195114541.png) 添加链表节点: -![链表-添加节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195134331.png) +![链表-添加节点](https://file.kamacoder.com/pics/20200806195134331.png) 这道题目设计链表的五个接口: * 获取链表第index个节点的数值 diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 0e4b346d82..b371bd857b 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -95,7 +95,7 @@ for (int i = 1; i <= nums1.size(); i++) { 拿示例1中,A: [1,2,3,2,1],B: [3,2,1,4,7]为例,画一个dp数组的状态变化,如下: -![718.最长重复子数组](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011215282060.jpg) +![718.最长重复子数组](https://file.kamacoder.com/pics/2021011215282060.jpg) 以上五部曲分析完毕,C++代码如下: @@ -127,7 +127,7 @@ public: 在如下图中: -![718.最长重复子数组](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011215282060-20230310134554486.jpg) +![718.最长重复子数组](https://file.kamacoder.com/pics/2021011215282060-20230310134554486.jpg) 我们可以看出dp[i][j]都是由dp[i - 1][j - 1]推出。那么压缩为一维数组,也就是dp[j]都是由dp[j - 1]推出。 diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index 542aad29b8..ed43cf141f 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -69,7 +69,7 @@ 首先先将第一个遍历元素加入单调栈 -![739.每日温度1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124434172.jpg) +![739.每日温度1](https://file.kamacoder.com/pics/20210219124434172.jpg) --------- @@ -77,65 +77,65 @@ 我们要保持一个递增单调栈(从栈头到栈底),所以将T[0]弹出,T[1]加入,此时result数组可以记录了,result[0] = 1,即T[0]右面第一个比T[0]大的元素是T[1]。 -![739.每日温度2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124504299.jpg) +![739.每日温度2](https://file.kamacoder.com/pics/20210219124504299.jpg) ----------- 加入T[2],同理,T[1]弹出 -![739.每日温度3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124527361.jpg) +![739.每日温度3](https://file.kamacoder.com/pics/20210219124527361.jpg) ------- 加入T[3],T[3] < T[2] (当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况),加T[3]加入单调栈。 -![739.每日温度4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124610761.jpg) +![739.每日温度4](https://file.kamacoder.com/pics/20210219124610761.jpg) --------- 加入T[4],T[4] == T[3] (当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况),此时依然要加入栈,不用计算距离,因为我们要求的是右面第一个大于本元素的位置,而不是大于等于! -![739.每日温度5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124633444.jpg) +![739.每日温度5](https://file.kamacoder.com/pics/20210219124633444.jpg) --------- 加入T[5],T[5] > T[4] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[4]弹出,同时计算距离,更新result -![739.每日温度6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124700567.jpg) +![739.每日温度6](https://file.kamacoder.com/pics/20210219124700567.jpg) ---------- T[4]弹出之后, T[5] > T[3] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[3]继续弹出,同时计算距离,更新result -![739.每日温度7](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124726613.jpg) +![739.每日温度7](https://file.kamacoder.com/pics/20210219124726613.jpg) ------- 直到发现T[5]小于T[st.top()],终止弹出,将T[5]加入单调栈 -![739.每日温度8](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124807715.jpg) +![739.每日温度8](https://file.kamacoder.com/pics/20210219124807715.jpg) ------- 加入T[6],同理,需要将栈里的T[5],T[2]弹出 -![739.每日温度9](https://code-thinking-1253855093.file.myqcloud.com/pics/2021021912483374.jpg) +![739.每日温度9](https://file.kamacoder.com/pics/2021021912483374.jpg) ------- 同理,继续弹出 -![739.每日温度10](https://code-thinking-1253855093.file.myqcloud.com/pics/2021021912490098.jpg) +![739.每日温度10](https://file.kamacoder.com/pics/2021021912490098.jpg) ------ 此时栈里只剩下了T[6] -![739.每日温度11](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124930156.jpg) +![739.每日温度11](https://file.kamacoder.com/pics/20210219124930156.jpg) ------------ 加入T[7], T[7] < T[6] 直接入栈,这就是最后的情况,result数组也更新完了。 -![739.每日温度12](https://code-thinking-1253855093.file.myqcloud.com/pics/20210219124957216.jpg) +![739.每日温度12](https://file.kamacoder.com/pics/20210219124957216.jpg) 此时有同学可能就疑惑了,那result[6] , result[7]怎么没更新啊,元素也一直在栈里。 diff --git "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" index 6533a240bf..c8a8736151 100644 --- "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" +++ "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" @@ -13,7 +13,7 @@ https://leetcode.cn/problems/network-delay-time/description/ 现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240229104105.png) +![](https://file.kamacoder.com/pics/20240229104105.png) 提示: @@ -42,7 +42,7 @@ dijkstra算法:在有权图(权值非负数)中求从起点到其他节点 如本题示例中的图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240125162647.png) +![](https://file.kamacoder.com/pics/20240125162647.png) 起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 @@ -88,7 +88,7 @@ minDist数组数值初始化为int最大值。 这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) +![](https://file.kamacoder.com/pics/20240130115306.png) (图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) @@ -110,7 +110,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) +![](https://file.kamacoder.com/pics/20240130115421.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -136,7 +136,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) +![](https://file.kamacoder.com/pics/20240130121240.png) 更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 @@ -170,7 +170,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) +![](https://file.kamacoder.com/pics/20240130120434.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -190,7 +190,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) +![](https://file.kamacoder.com/pics/20240201105335.png) 由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: @@ -210,7 +210,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110250.png) +![](https://file.kamacoder.com/pics/20240201110250.png) 由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: @@ -230,7 +230,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110651.png) +![](https://file.kamacoder.com/pics/20240201110651.png) 由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: @@ -248,7 +248,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110920.png) +![](https://file.kamacoder.com/pics/20240201110920.png) 节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 @@ -262,7 +262,7 @@ minDist数组数值初始化为int最大值。 路径如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) +![](https://file.kamacoder.com/pics/20240201111352.png) 在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 @@ -431,7 +431,7 @@ select:4 看一下这个图: (有负权值) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104334.png) +![](https://file.kamacoder.com/pics/20240227104334.png) 节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 @@ -441,7 +441,7 @@ select:4 初始化: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104801.png) +![](https://file.kamacoder.com/pics/20240227104801.png) --------------- @@ -455,7 +455,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110217.png) +![](https://file.kamacoder.com/pics/20240227110217.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -474,7 +474,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110330.png) +![](https://file.kamacoder.com/pics/20240227110330.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -492,7 +492,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110346.png) +![](https://file.kamacoder.com/pics/20240227110346.png) 由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: @@ -510,7 +510,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110405.png) +![](https://file.kamacoder.com/pics/20240227110405.png) 节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 @@ -526,7 +526,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110711.png) +![](https://file.kamacoder.com/pics/20240227110711.png) -------------- @@ -654,7 +654,7 @@ for (int v = 1; v <= n; v++) { 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) +![](https://file.kamacoder.com/pics/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 @@ -678,7 +678,7 @@ for (int v = 1; v <= n; v++) { 邻接表的构造如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: @@ -763,7 +763,7 @@ vector> grid(n + 1); 不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 图中邻接表表示: @@ -784,7 +784,7 @@ vector>> grid(n + 1); 举例来给大家展示 该代码表达的数据 如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103904.png) +![](https://file.kamacoder.com/pics/20240223103904.png) * 节点1 指向 节点3 权值为 1 * 节点1 指向 节点5 权值为 2 @@ -907,7 +907,7 @@ for (int v = 1; v <= n; v++) { 再回顾一下邻接表的构造(数组 + 链表): -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 9145c7ed1d..147c7bfba7 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -52,7 +52,7 @@ 请你计算并返回达到楼梯顶部的最低花费。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221031170131.png) +![](https://file.kamacoder.com/pics/20221031170131.png) ## 思路 @@ -112,7 +112,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。 拿示例2:cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] ,来模拟一下dp数组的状态变化,如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221026175104.png) +![](https://file.kamacoder.com/pics/20221026175104.png) 如果大家代码写出来有问题,就把dp数组打印出来,看看和如上推导的是不是一样的。 diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 70ebfe4ff7..daf52bea55 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -44,7 +44,7 @@ 如图: -![763.划分字母区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201222191924417.png) +![763.划分字母区间](https://file.kamacoder.com/pics/20201222191924417.png) 明白原理之后,代码并不复杂,如下: diff --git "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" index 68d8421502..6133ac7733 100644 --- "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" +++ "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" @@ -9,11 +9,11 @@ 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ,并返回该价格。 如果不存在这样的路线,则输出 -1。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319103900.png) +![](https://file.kamacoder.com/pics/20240319103900.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319103919.png) +![](https://file.kamacoder.com/pics/20240319103919.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240319104026.png) +![](https://file.kamacoder.com/pics/20240319104026.png) ## 思路 diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index a37e5c3f8c..639b6b2b3b 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -11,7 +11,7 @@ graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向边)。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221203135439.png) +![](https://file.kamacoder.com/pics/20221203135439.png) 提示: @@ -96,7 +96,7 @@ path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 一些录友可以疑惑这里如果找到x 链接的节点的,例如如果x目前是节点0,那么目前的过程就是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221204111937.png) +![](https://file.kamacoder.com/pics/20221204111937.png) 二维数组中,graph[x][i] 都是x链接的节点,当前遍历的节点就是 `graph[x][i]` 。 diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 0ebda2524a..e6aa4601dd 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -51,11 +51,11 @@ 拿如下地图的岛屿情况来举例: (1为陆地) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829104834.png) +![](https://file.kamacoder.com/pics/20220829104834.png) 第一步,则遍历题目,并将岛屿到编号和面积上的统计,过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105644.png) +![](https://file.kamacoder.com/pics/20220829105644.png) 本过程代码如下: @@ -102,7 +102,7 @@ int largestIsland(vector>& grid) { 第二步过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105249.png) +![](https://file.kamacoder.com/pics/20220829105249.png) 也就是遍历每一个0的方格,并统计其相邻岛屿面积,最后取一个最大值。 diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index 4076fce513..60180d2736 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -35,7 +35,7 @@ 图中给我的两个示例: `[[1],[2],[3],[]]` `[[1,3],[3,0,1],[2],[0]]`,画成对应的图如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220714101414.png) +![](https://file.kamacoder.com/pics/20220714101414.png) 我们可以看出图1的所有节点都是链接的,而图二中,节点2 是孤立的。 @@ -48,7 +48,7 @@ 图3:[[5], [], [1, 3], [5]] ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220714102201.png) +![](https://file.kamacoder.com/pics/20220714102201.png) 在图3中,大家可以发现,节点0只能到节点5,然后就哪也去不了了。 diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" index 383f6aa5b1..d4165f3672 100644 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" @@ -16,7 +16,7 @@ * arr[0] < arr[1] < ... arr[i-1] < arr[i] * arr[i] > arr[i+1] > ... > arr[arr.length - 1] -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210729103604.png) +![](https://file.kamacoder.com/pics/20210729103604.png) 示例 1: * 输入:arr = [2,1] diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index 0df2cc5b60..d8c31ca998 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -17,7 +17,7 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229175736596.png) +![](https://file.kamacoder.com/pics/20201229175736596.png) * 输入:[0,0,null,0,0] * 输出:1 @@ -25,7 +25,7 @@ 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/2020122917584449.png) +![](https://file.kamacoder.com/pics/2020122917584449.png) * 输入:[0,0,null,0,null,0,null,null,0] * 输出:2 @@ -143,7 +143,7 @@ if (cur == NULL) return 2; 如图: -![968.监控二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229203710729.png) +![968.监控二叉树2](https://file.kamacoder.com/pics/20201229203710729.png) 代码如下: @@ -191,7 +191,7 @@ if (left == 1 || right == 1) return 2; **从这个代码中,可以看出,如果left == 1, right == 0 怎么办?其实这种条件在情况2中已经判断过了**,如图: -![968.监控二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020122920362355.png) +![968.监控二叉树1](https://file.kamacoder.com/pics/2020122920362355.png) 这种情况也是大多数同学容易迷惑的情况。 @@ -199,7 +199,7 @@ if (left == 1 || right == 1) return 2; 以上都处理完了,递归结束之后,可能头结点 还有一个无覆盖的情况,如图: -![968.监控二叉树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229203742446.png) +![968.监控二叉树3](https://file.kamacoder.com/pics/20201229203742446.png) 所以递归结束之后,还要判断根节点,如果没有覆盖,result++,代码如下: diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" index 030d56a024..ae6b3895fa 100644 --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -12,13 +12,13 @@ 返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830100710.png) +![](https://file.kamacoder.com/pics/20220830100710.png) * 输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] * 输出:3 * 解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830100742.png) +![](https://file.kamacoder.com/pics/20220830100742.png) * 输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] * 输出:0 @@ -32,11 +32,11 @@ 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104632.png) +![](https://file.kamacoder.com/pics/20220830104632.png) 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104651.png) +![](https://file.kamacoder.com/pics/20220830104651.png) 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 53e0f370e7..0119df82ea 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -18,7 +18,7 @@ 以这种方法绘制线条,并返回可以绘制的最大连线数。 -![1035.不相交的线](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032116363533.png) +![1035.不相交的线](https://file.kamacoder.com/pics/2021032116363533.png) ## 算法公开课 @@ -36,7 +36,7 @@ 拿示例一nums1 = [1,4,2], nums2 = [1,2,4]为例,相交情况如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914145158.png) +![](https://file.kamacoder.com/pics/20210914145158.png) 其实也就是说nums1和nums2的最长公共子序列是[1,4],长度为2。 这个公共子序列指的是相对顺序不变(即数字4在字符串nums1中数字1的后面,那么数字4也应该在字符串nums2数字1的后面) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 62e7d9c590..6dfba4ed44 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -114,7 +114,7 @@ for (int i = 0; i < stones.size(); i++) { // 遍历物品 举例,输入:[2,4,1,1],此时target = (2 + 4 + 1 + 1)/2 = 4 ,dp数组状态图如下: -![1049.最后一块石头的重量II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121115805904.jpg) +![1049.最后一块石头的重量II](https://file.kamacoder.com/pics/20210121115805904.jpg) 最后dp[target]里是容量为target的背包所能背的最大重量。 diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 821f3c42a1..91c29b8313 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -94,7 +94,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 从递推公式,可以看出,有三个方向可以推出dp[i][j],如图: -![1143.最长公共子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204115139616.jpg) +![1143.最长公共子序列](https://file.kamacoder.com/pics/20210204115139616.jpg) 那么为了在递推的过程中,这三个方向都是经过计算的数值,所以要从前向后,从上到下来遍历这个矩阵。 @@ -103,7 +103,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 以输入:text1 = "abcde", text2 = "ace" 为例,dp状态如图: -![1143.最长公共子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210210150215918.jpg) +![1143.最长公共子序列1](https://file.kamacoder.com/pics/20210210150215918.jpg) 最后红框dp[text1.size()][text2.size()]为最终结果 diff --git "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" index 5d99670950..ebea30e34f 100644 --- "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" +++ "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" @@ -10,13 +10,13 @@ 请返回 封闭岛屿 的数目。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830111533.png) +![](https://file.kamacoder.com/pics/20220830111533.png) * 输入:grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]] * 输出:2 * 解释:灰色区域的岛屿是封闭岛屿,因为这座岛屿完全被水域包围(即被 1 区域包围)。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830111601.png) +![](https://file.kamacoder.com/pics/20220830111601.png) * 输入:grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]] * 输出:1 diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" index 7a1a7f3cb9..7b0d32048a 100644 --- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" +++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" @@ -15,7 +15,7 @@ 示例: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726154512.png) +![](https://file.kamacoder.com/pics/20210726154512.png) * 输入:root = [1,null,2,null,3,null,4,null,null] * 输出:[2,1,3,null,null,null,4] diff --git "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" index e9ea5f4463..5dd56c65d9 100644 --- "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" +++ "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" @@ -10,7 +10,7 @@ 什么是度,可以理解为,链接节点的边的数量。 题目中度如图所示: -![1791.找出星型图的中心节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20220704113207.png) +![1791.找出星型图的中心节点](https://file.kamacoder.com/pics/20220704113207.png) 至于出度和入度,那就是在有向图里的概念了,本题是无向图。 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index acb544155a..33b48698b5 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -12,7 +12,7 @@ 给你数组 edges 和整数 n、start 和 end,如果从 start 到 end 存在 有效路径 ,则返回 true,否则返回 false 。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220705101442.png) +![](https://file.kamacoder.com/pics/20220705101442.png) diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 7276af53b3..830bba7e2a 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -13,7 +13,7 @@ ## 超时是怎么回事 -![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117.png) +![程序超时](https://file.kamacoder.com/pics/20200729112716117.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -129,11 +129,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729200018460.png) +![程序超时2](https://file.kamacoder.com/pics/20200729200018460.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下O(n^2) 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://code-thinking-1253855093.file.myqcloud.com/pics/2020072919590970.png) +![程序超时3](https://file.kamacoder.com/pics/2020072919590970.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -141,7 +141,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 O(n)少一个数量级,因为logn的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729195729407.png) +![程序超时4](https://file.kamacoder.com/pics/20200729195729407.png) O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -149,7 +149,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 **整体测试数据整理如下:** -![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175.png) +![程序超时1](https://file.kamacoder.com/pics/20201208231559175.png) 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index 75c12f8a96..e361e8e0d9 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -46,13 +46,13 @@ 如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101345.png) +![](https://file.kamacoder.com/pics/20240227101345.png) 不能到达的情况: 如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101401.png) +![](https://file.kamacoder.com/pics/20240227101401.png) 数据范围: @@ -101,7 +101,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) +![](https://file.kamacoder.com/pics/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 @@ -125,7 +125,7 @@ 邻接表的构造如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: @@ -210,7 +210,7 @@ vector> grid(n + 1); 不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 图中邻接表表示: @@ -231,7 +231,7 @@ vector>> grid(n + 1); 举例来给大家展示 该代码表达的数据 如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103904.png) +![](https://file.kamacoder.com/pics/20240223103904.png) * 节点1 指向 节点3 权值为 1 * 节点1 指向 节点5 权值为 2 @@ -354,7 +354,7 @@ for (int v = 1; v <= n; v++) { 再回顾一下邻接表的构造(数组 + 链表): -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index e71e9d5374..1ff9f1a874 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -46,13 +46,13 @@ 如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101345.png) +![](https://file.kamacoder.com/pics/20240227101345.png) 不能到达的情况: 如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227101401.png) +![](https://file.kamacoder.com/pics/20240227101401.png) 数据范围: @@ -76,7 +76,7 @@ dijkstra算法:在有权图(权值非负数)中求从起点到其他节点 如本题示例中的图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240125162647.png) +![](https://file.kamacoder.com/pics/20240125162647.png) 起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 @@ -122,7 +122,7 @@ minDist数组数值初始化为int最大值。 这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) +![](https://file.kamacoder.com/pics/20240130115306.png) (图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) @@ -144,7 +144,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) +![](https://file.kamacoder.com/pics/20240130115421.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -170,7 +170,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) +![](https://file.kamacoder.com/pics/20240130121240.png) 更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 @@ -204,7 +204,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) +![](https://file.kamacoder.com/pics/20240130120434.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -224,7 +224,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) +![](https://file.kamacoder.com/pics/20240201105335.png) 由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: @@ -244,7 +244,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110250.png) +![](https://file.kamacoder.com/pics/20240201110250.png) 由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: @@ -264,7 +264,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110651.png) +![](https://file.kamacoder.com/pics/20240201110651.png) 由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: @@ -282,7 +282,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201110920.png) +![](https://file.kamacoder.com/pics/20240201110920.png) 节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 @@ -296,7 +296,7 @@ minDist数组数值初始化为int最大值。 路径如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) +![](https://file.kamacoder.com/pics/20240201111352.png) 在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 @@ -541,7 +541,7 @@ int main() { 对应如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201111352.png) +![](https://file.kamacoder.com/pics/20240201111352.png) ### 出现负数 @@ -549,7 +549,7 @@ int main() { 看一下这个图: (有负权值) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104334.png) +![](https://file.kamacoder.com/pics/20240227104334.png) 节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 @@ -559,7 +559,7 @@ int main() { 初始化: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227104801.png) +![](https://file.kamacoder.com/pics/20240227104801.png) --------------- @@ -573,7 +573,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110217.png) +![](https://file.kamacoder.com/pics/20240227110217.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -592,7 +592,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110330.png) +![](https://file.kamacoder.com/pics/20240227110330.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -610,7 +610,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110346.png) +![](https://file.kamacoder.com/pics/20240227110346.png) 由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: @@ -628,7 +628,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110405.png) +![](https://file.kamacoder.com/pics/20240227110405.png) 节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 @@ -644,7 +644,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240227110711.png) +![](https://file.kamacoder.com/pics/20240227110711.png) -------------- diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 861efe6806..585fa4767e 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -63,7 +63,7 @@ kruscal的思路: 依然以示例中,如下这个图来举例。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111113514.png) +![](https://file.kamacoder.com/pics/20240111113514.png) 将图中的边按照权值有小到大排序,这样从贪心的角度来说,优先选 权值小的边加入到 最小生成树中。 @@ -77,13 +77,13 @@ kruscal的思路: 选边(1,2),节点1 和 节点2 不在同一个集合,所以生成树可以添加边(1,2),并将 节点1,节点2 放在同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111114204.png) +![](https://file.kamacoder.com/pics/20240111114204.png) -------- 选边(4,5),节点4 和 节点 5 不在同一个集合,生成树可以添加边(4,5) ,并将节点4,节点5 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240111120458.png) +![](https://file.kamacoder.com/pics/20240111120458.png) **大家判断两个节点是否在同一个集合,就看图中两个节点是否有绿色的粗线连着就行** @@ -93,25 +93,25 @@ kruscal的思路: 选边(1,3),节点1 和 节点3 不在同一个集合,生成树添加边(1,3),并将节点1,节点3 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112105834.png) +![](https://file.kamacoder.com/pics/20240112105834.png) --------- 选边(2,6),节点2 和 节点6 不在同一个集合,生成树添加边(2,6),并将节点2,节点6 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110214.png) +![](https://file.kamacoder.com/pics/20240112110214.png) -------- 选边(3,4),节点3 和 节点4 不在同一个集合,生成树添加边(3,4),并将节点3,节点4 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110450.png) +![](https://file.kamacoder.com/pics/20240112110450.png) ---------- 选边(6,7),节点6 和 节点7 不在同一个集合,生成树添加边(6,7),并将 节点6,节点7 放到同一个集合。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110637.png) +![](https://file.kamacoder.com/pics/20240112110637.png) ----------- @@ -126,7 +126,7 @@ kruscal的思路: 此时 我们就已经生成了一个最小生成树,即: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240112110637.png) +![](https://file.kamacoder.com/pics/20240112110637.png) 在上面的讲解中,看图的话 大家知道如何判断 两个节点 是否在同一个集合(是否有绿色的线连在一起),以及如何把两个节点加入集合(就在图中把两个节点连上) @@ -346,7 +346,7 @@ int main() { 大家可能发现 怎么和我们 模拟画的图不一样,差别在于 代码生成的最小生成树中 节点5 和 节点7相连的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240116163014.png) +![](https://file.kamacoder.com/pics/20240116163014.png) 其实造成这个差别 是对边排序的时候 权值相同的边先后顺序的问题导致的,无论相同权值边的顺序是什么样的,最后都能得出最小生成树。 @@ -366,7 +366,7 @@ Kruskal 与 prim 的关键区别在于,prim维护的是节点的集合,而 K 节点未必一定要连着边那, 例如 这个图,大家能明显感受到边没有那么多对吧,但节点数量 和 上述我们讲的例子是一样的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240116152211.png) +![](https://file.kamacoder.com/pics/20240116152211.png) 为什么边少的话,使用 Kruskal 更优呢? diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index 8e26bea4bd..a7d3584178 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -61,7 +61,7 @@ 例如本题示例中的无向有权图为: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231206164306.png) +![](https://file.kamacoder.com/pics/20231206164306.png) 那么在这个图中,如何选取n-1条边使得图中所有节点连接到一起,并且边的权值和最小呢? @@ -100,7 +100,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231215105603.png) +![](https://file.kamacoder.com/pics/20231215105603.png) 开始构造最小生成树 @@ -118,7 +118,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来,我们要更新所有节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102048.png) +![](https://file.kamacoder.com/pics/20231222102048.png) 注意下标0,我们就不管它了,下标1与节点1对应,这样可以避免大家把节点搞混。 @@ -148,7 +148,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来,我们要更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102431.png) +![](https://file.kamacoder.com/pics/20231222102431.png) 此时所有非生成树的节点距离最小生成树(节点1、节点2)的距离都已经跟新了。 @@ -172,7 +172,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102457.png) +![](https://file.kamacoder.com/pics/20231222102457.png) 所有非生成树的节点距离最小生成树(节点1、节点2、节点3)的距离都已经跟新了。 @@ -188,7 +188,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 继续选择一个距离最小生成树(节点1、节点2、节点3)最近的非生成树里的节点,为了巩固大家对minDist数组的理解,这里我再啰嗦一遍: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231217213516.png) +![](https://file.kamacoder.com/pics/20231217213516.png) **minDist数组是记录了所有非生成树节点距离生成树的最小距离**,所以从数组里我们能看出来,非生成树节点4和节点6距离生成树最近。 @@ -209,7 +209,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102618.png) +![](https://file.kamacoder.com/pics/20231222102618.png) minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4)的距离。 @@ -232,7 +232,7 @@ minDist数组已经更新了所有非生成树的节点距离最小生成树( 接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102646.png) +![](https://file.kamacoder.com/pics/20231222102646.png) minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4、节点5)的距离。 @@ -253,11 +253,11 @@ minDist数组已经更新了所有非生成树的节点距离最小生成树( 节点1、节点2、节点3、节点4、节点5、节点6算是最小生成树的节点,接下来更新节点距离最小生成树的距离,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102732.png) +![](https://file.kamacoder.com/pics/20231222102732.png) 这里就不在重复描述了,大家类推,最后,节点7加入生成树,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231222102820.png) +![](https://file.kamacoder.com/pics/20231222102820.png) ### 最后 @@ -478,7 +478,7 @@ int main() { 大家可以和我们本题最后生成的最小生成树的图去对比一下边的链接情况: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231229115714.png) +![](https://file.kamacoder.com/pics/20231229115714.png) 绿色的边是最小生成树,和我们的输出完全一致。 diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index f788d65ba7..665e8ecbae 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -29,11 +29,11 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030165201.png) +![](https://file.kamacoder.com/pics/20231030165201.png) 然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030173058.png) +![](https://file.kamacoder.com/pics/20231030173058.png) 有同学问了,为什么要从后向前填充,从前向后填充不行么? diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 2b32cb44d2..48150222a7 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -40,16 +40,16 @@ fgabcde 本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106170143.png) +![](https://file.kamacoder.com/pics/20231106170143.png) 右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106171557.png) +![](https://file.kamacoder.com/pics/20231106171557.png) 此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172058.png) +![](https://file.kamacoder.com/pics/20231106172058.png) 其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 @@ -80,7 +80,7 @@ int main() { 可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172534.png) +![](https://file.kamacoder.com/pics/20231106172534.png) 代码如下: diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index 23e7189a15..a6342ef8c0 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -93,7 +93,7 @@ int main() { 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627110604.png) +![](https://file.kamacoder.com/pics/20240627110604.png) 如果,我们想统计,在vec数组上 下标 2 到下标 5 之间的累加和,那是不是就用 p[5] - p[1] 就可以了。 @@ -109,7 +109,7 @@ int main() { 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240627111319.png) +![](https://file.kamacoder.com/pics/20240627111319.png) `p[5] - p[1]` 就是 红色部分的区间和。 diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 99986aaa2d..b592029276 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -62,7 +62,7 @@ 给大家举一个例子: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240328104119.png) +![](https://file.kamacoder.com/pics/20240328104119.png) 本图中,对所有边进行松弛,真正有效的松弛,只有松弛 边(节点1->节点2) 和 边(节点1->节点3) 。 @@ -97,7 +97,7 @@ 初始化,起点为节点1, 起点到起点的最短距离为0,所以minDist[1] 为 0。 将节点1 加入队列 (下次松弛从节点1开始) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115555.png) +![](https://file.kamacoder.com/pics/20240411115555.png) ------------ @@ -109,7 +109,7 @@ 将节点2、节点3 加入队列,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115544.png) +![](https://file.kamacoder.com/pics/20240411115544.png) ----------------- @@ -124,7 +124,7 @@ 将节点4,节点5 加入队列,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110348.png) +![](https://file.kamacoder.com/pics/20240412110348.png) -------------------- @@ -134,7 +134,7 @@ 因为没有从节点3作为出发点的边,所以这里就从队列里取出节点3就好,不用做其他操作,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110420.png) +![](https://file.kamacoder.com/pics/20240412110420.png) ------------ @@ -147,7 +147,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412110445.png) +![](https://file.kamacoder.com/pics/20240412110445.png) --------------- @@ -160,7 +160,7 @@ 如图,将节点3加入队列,因为节点6已经在队列里,所以不用重复添加 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240729161116.png) +![](https://file.kamacoder.com/pics/20240729161116.png) 所以我们在加入队列的过程可以有一个优化,**用visited数组记录已经在队列里的元素,已经在队列的元素不用重复加入** @@ -174,7 +174,7 @@ 所以直接从队列中取出,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240411115424.png) +![](https://file.kamacoder.com/pics/20240411115424.png) ---------- @@ -264,7 +264,7 @@ int main() { 至于为什么 双向图且每一个节点和所有其他节点都相连的话,每个节点 都有 n-1 条指向该节点的边, 我再来举个例子,如图: -[](https://code-thinking-1253855093.file.myqcloud.com/pics/20240416104138.png) +[](https://file.kamacoder.com/pics/20240416104138.png) 图中 每个节点都与其他所有节点相连,节点数n 为 4,每个节点都有3条指向该节点的边,即入度为3。 @@ -329,7 +329,7 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240412111849.png) +![](https://file.kamacoder.com/pics/20240412111849.png) 正权回路 就是有环,但环的总权值为正数。 diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 2afc014b7e..9edde8ace3 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -46,7 +46,7 @@ 1 3 5 ``` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240509200224.png) +![](https://file.kamacoder.com/pics/20240509200224.png) ## 思路 @@ -78,7 +78,7 @@ 这里我给大家举一个例子,每条边有起点、终点和边的权值。例如一条边,节点A 到 节点B 权值为value,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240327102620.png) +![](https://file.kamacoder.com/pics/20240327102620.png) minDist[B] 表示 到达B节点 最小权值,minDist[B] 有哪些状态可以推出来? @@ -127,7 +127,7 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240328104119.png) +![](https://file.kamacoder.com/pics/20240328104119.png) 其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 @@ -150,36 +150,36 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value 边:节点5 -> 节点6,权值为-2 ,minDist[5] 还是默认数值max,所以不能基于 节点5 去更新节点6,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113537.png) +![](https://file.kamacoder.com/pics/20240329113537.png) (在复习一下,minDist[5] 表示起点到节点5的最短距离) 边:节点1 -> 节点2,权值为1 ,minDist[2] > minDist[1] + 1 ,更新 minDist[2] = minDist[1] + 1 = 0 + 1 = 1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113703.png) +![](https://file.kamacoder.com/pics/20240329113703.png) 边:节点5 -> 节点3,权值为1 ,minDist[5] 还是默认数值max,所以不能基于节点5去更新节点3 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113827.png) +![](https://file.kamacoder.com/pics/20240329113827.png) 边:节点2 -> 节点5,权值为2 ,minDist[5] > minDist[2] + 2 (经过上面的计算minDist[2]已经不是默认值,而是 1),更新 minDist[5] = minDist[2] + 2 = 1 + 2 = 3 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329113927.png) +![](https://file.kamacoder.com/pics/20240329113927.png) 边:节点2 -> 节点4,权值为-3 ,minDist[4] > minDist[2] + (-3),更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114036.png) +![](https://file.kamacoder.com/pics/20240329114036.png) 边:节点4 -> 节点6,权值为4 ,minDist[6] > minDist[4] + 4,更新 minDist[6] = minDist[4] + 4 = -2 + 4 = 2 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114120.png) +![](https://file.kamacoder.com/pics/20240329114120.png) 边:节点1 -> 节点3,权值为5 ,minDist[3] > minDist[1] + 5,更新 minDist[3] = minDist[1] + 5 = 0 + 5 = 5 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240329114324.png) +![](https://file.kamacoder.com/pics/20240329114324.png) -------- diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index a3896b8896..5dddf45013 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -78,7 +78,7 @@ circle 我们拿题目中示例来画一个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240705161426.png) +![](https://file.kamacoder.com/pics/20240705161426.png) 图中 节点1 到 节点4 的最短路径是多少(题目中的最低运输成本) (注意边可以为负数的) @@ -86,7 +86,7 @@ circle 而图中有负权回路: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402103712.png) +![](https://file.kamacoder.com/pics/20240402103712.png) 那么我们在负权回路中多绕一圈,我们的最短路径 是不是就更小了 (也就是更低的运输成本) diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index eb80e048f6..37cfaee0e3 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -63,7 +63,7 @@ 本题是最多经过 k 个城市, 那么是 k + 1条边相连的节点。 这里可能有录友想不懂为什么是k + 1,来看这个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240402115614.png) +![](https://file.kamacoder.com/pics/20240402115614.png) 图中,节点1 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 @@ -195,7 +195,7 @@ int main() { 起点为节点1, 起点到起点的距离为0,所以 minDist[1] 初始化为0 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111940.png) +![](https://file.kamacoder.com/pics/20240409111940.png) 其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 @@ -203,21 +203,21 @@ int main() { 边:节点1 -> 节点2,权值为-1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = minDist[1] + (-1) = 0 - 1 = -1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111914.png) +![](https://file.kamacoder.com/pics/20240409111914.png) 边:节点2 -> 节点3,权值为1 ,minDist[3] > minDist[2] + 1 ,更新 minDist[3] = minDist[2] + 1 = -1 + 1 = 0 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111903.png) +![](https://file.kamacoder.com/pics/20240409111903.png) 边:节点3 -> 节点1,权值为-1 ,minDist[1] > minDist[3] + (-1),更新 minDist[1] = 0 + (-1) = -1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111849.png) +![](https://file.kamacoder.com/pics/20240409111849.png) 边:节点3 -> 节点4,权值为1 ,minDist[4] > minDist[3] + 1,更新 minDist[4] = 0 + 1 = 1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241018192042.png) +![](https://file.kamacoder.com/pics/20241018192042.png) 以上是对所有边进行的第一次松弛,最后 minDist数组为 :-1 -1 0 1 ,(从下标1算起) @@ -244,7 +244,7 @@ int main() { 在上面画图距离中,对所有边进行第一次松弛,在计算 边(节点2 -> 节点3) 的时候,更新了 节点3。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240409111903.png) +![](https://file.kamacoder.com/pics/20240409111903.png) 理论上来说节点3 应该在对所有边第二次松弛的时候才更新。 这因为当时是基于已经计算好的 节点2(minDist[2])来做计算了。 @@ -331,11 +331,11 @@ int main() { 所构成是图是一样的,都是如下的这个图,但给出的边的顺序是不一样的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410154340.png) +![](https://file.kamacoder.com/pics/20240410154340.png) 再用版本一的代码是运行一下,发现结果输出是 1, 是对的。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410154940.png) +![](https://file.kamacoder.com/pics/20240410154940.png) 分明刚刚输出的结果是 -2,是错误的,怎么 一样的图,这次输出的结果就对了呢? @@ -345,7 +345,7 @@ int main() { 初始化: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410155545.png) +![](https://file.kamacoder.com/pics/20240410155545.png) 边:节点3 -> 节点1,权值为-1 ,节点3还没有被计算过,节点1 不更新。 @@ -355,7 +355,7 @@ int main() { 边:节点1 -> 节点2,权值为 -1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = 0 + (-1) = -1 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240410160046.png) +![](https://file.kamacoder.com/pics/20240410160046.png) 以上是对所有边 松弛一次的状态。 @@ -472,7 +472,7 @@ int main() { 但大家会发现,以上代码大家提交后,怎么耗时这么多? -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418113308.png) +![](https://file.kamacoder.com/pics/20240418113308.png) 理论上,SPFA的时间复杂度不是要比 bellman_ford 更优吗? @@ -554,7 +554,7 @@ int main() { 以上代码提交后,耗时情况: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240418113952.png) +![](https://file.kamacoder.com/pics/20240418113952.png) 大家发现 依然远比 bellman_ford 的代码版本 耗时高。 @@ -579,11 +579,11 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 在以下这个图中,求节点1 到 节点7 最多经过2个节点 的最短路是多少呢? -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508112249.png) +![](https://file.kamacoder.com/pics/20240508112249.png) 最短路显然是: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508112416.png) +![](https://file.kamacoder.com/pics/20240508112416.png) 最多经过2个节点,也就是3条边相连的路线:节点1 -> 节点2 -> 节点6-> 节点7 @@ -591,24 +591,24 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 初始化如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115306.png) +![](https://file.kamacoder.com/pics/20240130115306.png) 找距离源点最近且没有被访问过的节点,先找节点1 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130115421.png) +![](https://file.kamacoder.com/pics/20240130115421.png) 距离源点最近且没有被访问过的节点,找节点2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130121240.png) +![](https://file.kamacoder.com/pics/20240130121240.png) 距离源点最近且没有被访问过的节点,找到节点3: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240130120434.png) +![](https://file.kamacoder.com/pics/20240130120434.png) 距离源点最近且没有被访问过的节点,找到节点4: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240201105335.png) +![](https://file.kamacoder.com/pics/20240201105335.png) 此时最多经过2个节点的搜索就完毕了,但结果中minDist[7] (即节点7的结果)并没有被更。 diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index dfbd6aa9a8..97765ebc7b 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -155,7 +155,7 @@ grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点 grid数组是一个三维数组,那么我们初始化的数据在 i 与 j 构成的平层,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240425104247.png) +![](https://file.kamacoder.com/pics/20240425104247.png) 红色的 底部一层是我们初始化好的数据,注意:从三维角度去看初始化的数据很重要,下面我们在聊遍历顺序的时候还会再讲。 @@ -202,7 +202,7 @@ vector>> grid(n + 1, vector>(n + 1, vector(n 所以遍历k 的for循环一定是在最外面,这样才能一层一层去遍历。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120109.png) +![](https://file.kamacoder.com/pics/20240424120109.png) 至于遍历 i 和 j 的话,for 循环的先后顺序无所谓。 @@ -234,7 +234,7 @@ for (int i = 1; i <= n; i++) { 此时就遍历了 j 与 k 形成一个平面,i 则是纵面,那遍历 就是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) +![](https://file.kamacoder.com/pics/20240424115827.png) 而我们初始化的数据 是 k 为0, i 和 j 形成的平面做初始化,如果以 k 和 j 形成的平面去一层一层遍历,就造成了 递推公式 用不上上一轮计算的结果,从而导致结果不对(初始化的部分是 i 与j 形成的平面,在初始部分有讲过)。 @@ -253,7 +253,7 @@ for (int i = 1; i <= n; i++) { 就是图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424120942.png) +![](https://file.kamacoder.com/pics/20240424120942.png) 求节点1 到 节点 2 的最短距离,运行结果是 10 ,但正确的结果很明显是3。 @@ -267,7 +267,7 @@ for (int i = 1; i <= n; i++) { 而遍历k 的for循环如果放在中间呢,同样是 j 与k 行程一个平面,i 是纵面,遍历的也是这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240424115827.png) +![](https://file.kamacoder.com/pics/20240424115827.png) 同样不能完全用上初始化 和 上一层计算的结果。 @@ -283,7 +283,7 @@ for (int i = 1; i <= n; i++) { 图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240425112636.png) +![](https://file.kamacoder.com/pics/20240425112636.png) 求 节点1 到节点3 的最短距离,如果k循环放中间,程序的运行结果是 -1,也就是不能到达节点3。 diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 4df53b448f..2f0dcdcc87 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -43,7 +43,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240514103953.png) +![](https://file.kamacoder.com/pics/20240514103953.png) 用例解释: @@ -141,7 +141,7 @@ while (m--) { 我在 [图论理论基础篇](./图论理论基础.md) 举了一个例子: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index f8c36a00a8..0da2f315a1 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -35,7 +35,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240516111613.png) +![](https://file.kamacoder.com/pics/20240516111613.png) 根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 @@ -50,7 +50,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -72,7 +72,7 @@ 如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250124094043.png) +![](https://file.kamacoder.com/pics/20250124094043.png) 超时写法 (从队列中取出节点再标记,注意代码注释的地方) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 5a21f387fc..06be926874 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -36,7 +36,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240516111613.png) +![](https://file.kamacoder.com/pics/20240516111613.png) 根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 @@ -50,7 +50,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 170c0917aa..f2b9b901d1 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -33,7 +33,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517103410.png) +![](https://file.kamacoder.com/pics/20240517103410.png) 样例输入中,岛屿的最大面积为 4。 @@ -48,7 +48,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png) +![图一](https://file.kamacoder.com/pics/20220726094200.png) 这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上“1”的数量,然后取一个最大的。 diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index 43ac8ec96d..c8fe372cd9 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -37,7 +37,7 @@ 提示信息: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517105557.png) +![](https://file.kamacoder.com/pics/20240517105557.png) 在矩阵中心部分的岛屿,因为没有任何一个单元格接触到矩阵边缘,所以该岛屿属于孤岛,总面积为 1。 @@ -54,11 +54,11 @@ 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104632.png) +![](https://file.kamacoder.com/pics/20220830104632.png) 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104651.png) +![](https://file.kamacoder.com/pics/20220830104651.png) 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 5e211cd09c..265ec31f73 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -43,11 +43,11 @@ 提示信息: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517110932.png) +![](https://file.kamacoder.com/pics/20240517110932.png) 将孤岛沉没: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517110953.png) +![](https://file.kamacoder.com/pics/20240517110953.png) 数据范围: @@ -73,7 +73,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517113813.png) +![](https://file.kamacoder.com/pics/20240517113813.png) 整体C++代码如下,以下使用dfs实现,其实遍历方式dfs,bfs都是可以的。 diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 1c646b1c03..5924cb18a9 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -48,7 +48,7 @@ 提示信息: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240517115816.png) +![](https://file.kamacoder.com/pics/20240517115816.png) 图中的蓝色方块上的雨水既能流向第一组边界,也能流向第二组边界。所以最终答案为所有蓝色方块的坐标。 @@ -166,11 +166,11 @@ int main() { 从第一组边界边上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250304174747.png) +![](https://file.kamacoder.com/pics/20250304174747.png) 从第二组边界上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250304174801.png) +![](https://file.kamacoder.com/pics/20250304174801.png) 最后,我们得到两个方向交界的这些节点,就是我们最后要求的节点。 diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 5f091779d1..483d777275 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -35,12 +35,12 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522154055.png) +![](https://file.kamacoder.com/pics/20240522154055.png) 对于上面的案例,有两个位置可将 0 变成 1,使得岛屿的面积最大,即 6。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522154110.png) +![](https://file.kamacoder.com/pics/20240522154110.png) 数据范围: @@ -70,11 +70,11 @@ 拿如下地图的岛屿情况来举例: (1为陆地) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829104834.png) +![](https://file.kamacoder.com/pics/20220829104834.png) 第一步,则遍历地图,并将岛屿的编号和面积都统计好,过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105644.png) +![](https://file.kamacoder.com/pics/20220829105644.png) 本过程代码如下: @@ -121,7 +121,7 @@ int largestIsland(vector>& grid) { 第二步过程如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829105249.png) +![](https://file.kamacoder.com/pics/20220829105249.png) 也就是遍历每一个0的方格,并统计其相邻岛屿面积,最后取一个最大值。 diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 6901c655e4..cfe77c0d38 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -33,7 +33,7 @@ 【提示信息】 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522174707.png) +![](https://file.kamacoder.com/pics/20240522174707.png) 从 1 号节点可以到达任意节点,输出 1。 @@ -48,7 +48,7 @@ 接下来我们再画一个图,从图里可以直观看出来,节点6 是 不能到达节点1 的 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240522175451.png) +![](https://file.kamacoder.com/pics/20240522175451.png) 这就很容易让我们想起岛屿问题,只要发现独立的岛,就是不可到达的。 diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 91a1a438c6..a1ef2a76eb 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -37,7 +37,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524115244.png) +![](https://file.kamacoder.com/pics/20240524115244.png) 岛屿的周长为 14。 @@ -57,14 +57,14 @@ 如果该陆地上下左右的空格是有水域,则说明是一条边,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524115933.png) +![](https://file.kamacoder.com/pics/20240524115933.png) 陆地的右边空格是水域,则说明找到一条边。 如果该陆地上下左右的空格出界了,则说明是一条边,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120105.png) +![](https://file.kamacoder.com/pics/20240524120105.png) 该陆地的下边空格出界了,则说明找到一条边。 @@ -114,7 +114,7 @@ int main() { 因为有一对相邻两个陆地,边的总数就要减2,如图红线部分,有两个陆地相邻,总边数就要减2 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120855.png) +![](https://file.kamacoder.com/pics/20240524120855.png) 那么只需要在计算出相邻岛屿的数量就可以了,相邻岛屿数量为cover。 diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 779907c8fd..363a188465 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -40,7 +40,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527104432.png) +![](https://file.kamacoder.com/pics/20240527104432.png) 数据范围: diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index ae247ac025..fe641f53c9 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -9,11 +9,11 @@ 有一个图,它是一棵树,他是拥有 n 个节点(节点编号1到n)和 n - 1 条边的连通无环无向图(其实就是一个线形图),如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240905163122.png) +![](https://file.kamacoder.com/pics/20240905163122.png) 现在在这棵树上的基础上,添加一条边(依然是n个节点,但有n条边),使这个图变成了有环图,如图 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240905164721.png) +![](https://file.kamacoder.com/pics/20240905164721.png) 先请你找出冗余边,删除后,使该图可以重新变成一棵树。 @@ -42,7 +42,7 @@ 提示信息 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527110320.png) +![](https://file.kamacoder.com/pics/20240527110320.png) 图中的 1 2,2 3,1 3 等三条边在删除后都能使原图变为一棵合法的树。但是 1 3 由于是标准输入里最后出现的那条边,所以输出结果为 1 3 @@ -69,13 +69,13 @@ 如图所示,节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104720.png) +![](https://file.kamacoder.com/pics/20230604104720.png) 如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了。 如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230604104330.png) +![](https://file.kamacoder.com/pics/20230604104330.png) 已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 @@ -157,7 +157,7 @@ int main() { 图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527110320.png) +![](https://file.kamacoder.com/pics/20240527110320.png) 输出示例 diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 070bc68500..78132a3256 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -9,11 +9,11 @@ 有一种有向树,该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。有向树拥有 n 个节点和 n - 1 条边。如图:  - + 现在有一个有向图,有向图是在有向树中的两个没有直接链接的节点中间添加一条有向边。如图: - + 输入一个有向图,该图由一个有着 n 个节点(节点编号 从 1 到 n),n 条边,请返回一条可以删除的边,使得删除该条边之后该有向图可以被当作一颗有向树。 @@ -42,7 +42,7 @@ 提示信息 - + 在删除 2 3 后有向图可以变为一棵合法的有向树,所以输出 2 3 @@ -64,13 +64,13 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527115807.png) +![](https://file.kamacoder.com/pics/20240527115807.png) 找到了节点3 的入度为2,删 1 -> 3 或者 2 -> 3 。选择删顺序靠后便可。 但 入度为2 还有一种情况,情况二,只能删特定的一条边,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527151456.png) +![](https://file.kamacoder.com/pics/20240527151456.png) 节点3 的入度为 2,但在删除边的时候,只能删 这条边(节点1 -> 节点3),如果删这条边(节点4 -> 节点3),那么删后本图也不是有向树了(因为找不到根节点)。 @@ -81,7 +81,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240527120531.png) +![](https://file.kamacoder.com/pics/20240527120531.png) 对于情况三,删掉构成环的边就可以了。 diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 8bae6280bf..af3436901c 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -57,7 +57,7 @@ yhn 2 <= N <= 500

- +

@@ -65,7 +65,7 @@ yhn 以示例1为例,从这个图中可以看出 abc 到 def的路线 不止一条,但最短的一条路径上是4个节点。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250317105155.png) +![](https://file.kamacoder.com/pics/20250317105155.png) 本题只需要求出最短路径的长度就可以了,不用找出具体路径。 diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index 58c1776304..18802765a9 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -39,7 +39,7 @@ 文件依赖关系如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510192157.png) +![](https://file.kamacoder.com/pics/20240510192157.png) 所以,文件处理的顺序除了示例中的顺序,还存在 @@ -104,7 +104,7 @@ 以题目中示例为例如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510110836.png) +![](https://file.kamacoder.com/pics/20240510110836.png) 做拓扑排序的话,如果肉眼去找开头的节点,一定能找到 节点0 吧,都知道要从节点0 开始。 @@ -135,17 +135,17 @@ 1、找到入度为0 的节点,加入结果集 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113110.png) +![](https://file.kamacoder.com/pics/20240510113110.png) 2、将该节点从图中移除 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113142.png) +![](https://file.kamacoder.com/pics/20240510113142.png) ---------------- 1、找到入度为0 的节点,加入结果集 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113345.png) +![](https://file.kamacoder.com/pics/20240510113345.png) 这里大家会发现,节点1 和 节点2 入度都为0, 选哪个呢? @@ -153,19 +153,19 @@ 2、将该节点从图中移除 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113640.png) +![](https://file.kamacoder.com/pics/20240510113640.png) --------------- 1、找到入度为0 的节点,加入结果集 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113853.png) +![](https://file.kamacoder.com/pics/20240510113853.png) 节点2 和 节点3 入度都为0,选哪个都行,这里选节点2 2、将该节点从图中移除 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510114004.png) +![](https://file.kamacoder.com/pics/20240510114004.png) -------------- @@ -177,7 +177,7 @@ 如果有 有向环怎么办呢?例如这个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510115115.png) +![](https://file.kamacoder.com/pics/20240510115115.png) 这个图,我们只能将入度为0 的节点0 接入结果集。 @@ -252,13 +252,13 @@ while (que.size()) { 如果这里不理解,看上面的模拟过程第一步: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113110.png) +![](https://file.kamacoder.com/pics/20240510113110.png) 这事节点1 和 节点2 的入度为 1。 将节点0删除后,图为这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510113142.png) +![](https://file.kamacoder.com/pics/20240510113142.png) 那么 节点0 作为出发点 所连接的节点的入度 就都做了 减一 的操作。 diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 8ad3264433..7d0096d52d 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -9,7 +9,7 @@ 骑士移动规则如图,红色是起始位置,黄色是骑士可以走的地方。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240626104833.png) +![](https://file.kamacoder.com/pics/20240626104833.png) 棋盘大小 1000 x 1000(棋盘的 x 和 y 坐标均在 [1, 1000] 区间内,包含边界) @@ -108,7 +108,7 @@ int main() 我们来看一下广搜的搜索过程,如图,红色是起点,绿色是终点,黄色是要遍历的点,最后从 起点 找到 达到终点的最短路径是棕色。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611143712.png) +![](https://file.kamacoder.com/pics/20240611143712.png) 可以看出 广搜中,做了很多无用的遍历, 黄色的格子是广搜遍历到的点。 @@ -131,11 +131,11 @@ Astar 是一种 广搜的改良版。 有的是 Astar是 dijkstra 的改良版 在BFS中,我们想搜索,从起点到终点的最短路径,要一层一层去遍历。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611143712.png) +![](https://file.kamacoder.com/pics/20240611143712.png) 如果 使用A * 的话,其搜索过程是这样的,如图,图中着色的都是我们要遍历的点。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240611195223.png) +![](https://file.kamacoder.com/pics/20240611195223.png) (上面两图中 最短路长度都是8,只是走的方式不同而已) diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index e463b95600..5eb5127a93 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -105,13 +105,13 @@ bool isSame(int u, int v) { 搜索过程像是一个多叉树中从叶子到根节点的过程,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602102619.png) +![](https://file.kamacoder.com/pics/20230602102619.png) 如果这棵多叉树高度很深的话,每次find函数 去寻找根的过程就要递归很多次。 我们的目的只需要知道这些节点在同一个根下就可以,所以对这棵多叉树的构造只需要这样就可以了,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602103040.png) +![](https://file.kamacoder.com/pics/20230602103040.png) 除了根节点其他所有节点都挂载根节点下,这样我们在寻根的时候就很快,只需要一步, @@ -226,7 +226,7 @@ join(3, 2); 此时构成的图是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230525111307.png) +![](https://file.kamacoder.com/pics/20230525111307.png) 此时问 1,3是否在同一个集合,我们调用 `join(1, 2); join(3, 2);` 很明显本意要表示 1,3是在同一个集合。 @@ -256,7 +256,7 @@ join(3, 2); 构成的图是这样的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230525112101.png) +![](https://file.kamacoder.com/pics/20230525112101.png) 因为在join函数里,我们有find函数进行寻根的过程,这样就保证元素 1,2,3在这个有向图里是强连通的。 @@ -275,12 +275,12 @@ join(3, 2); 1、`join(1, 8);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122112727.png) +![](https://file.kamacoder.com/pics/20231122112727.png) 2、`join(3, 8);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122113857.png) +![](https://file.kamacoder.com/pics/20231122113857.png) 有录友可能想,`join(3, 8)` 在图中为什么 将 元素1 连向元素 3 而不是将 元素 8 连向 元素 3 呢? @@ -288,12 +288,12 @@ join(3, 2); 3、`join(1, 7);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122114108.png) +![](https://file.kamacoder.com/pics/20231122114108.png) 4、`join(8, 5);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122114847.png) +![](https://file.kamacoder.com/pics/20231122114847.png) 这里8的根是3,那么 5 应该指向 8 的根 3,这里的原因,我们在上面「常见误区」已经讲过了。 但 为什么 图中 8 又直接指向了 3 了呢? @@ -310,11 +310,11 @@ int find(int u) { 5、`join(2, 9);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122115000.png) +![](https://file.kamacoder.com/pics/20231122115000.png) 6、`join(6, 9);` -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231122115404.png) +![](https://file.kamacoder.com/pics/20231122115404.png) 这里为什么是 2 指向了 6,因为 9的根为 2,所以用2指向6。 @@ -347,13 +347,13 @@ rank表示树的高度,即树中结点层次的最大值。 例如两个集合(多叉树)需要合并,如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602172250.png) +![](https://file.kamacoder.com/pics/20230602172250.png) 树1 rank 为2,树2 rank 为 3。那么合并两个集合,是 树1 合入 树2,还是 树2 合入 树1呢? 我们来看两个不同方式合入的效果。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230602172933.png) +![](https://file.kamacoder.com/pics/20230602172933.png) 这里可以看出,树2 合入 树1 会导致整棵树的高度变的更高,而 树1 合入 树2 整棵树的高度 和 树2 保持一致。 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index d791d2c0ff..96e313fc42 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -18,11 +18,11 @@ 我们用一个方格地图,假如每次搜索的方向为 上下左右(不包含斜上方),那么给出一个start起始位置,那么BFS就是从四个方向走出第一步。 -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220825104505.png) +![图一](https://file.kamacoder.com/pics/20220825104505.png) 如果加上一个end终止位置,那么使用BFS的搜索过程如图所示: -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220825102653.png) +![图二](https://file.kamacoder.com/pics/20220825102653.png) 我们从图中可以看出,从start起点开始,是一圈一圈,向外搜索,方格编号1为第一步遍历的节点,方格编号2为第二步遍历的节点,第四步的时候我们找到终止点end。 @@ -30,7 +30,7 @@ 而且地图还可以有障碍,如图所示: -![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220825103900.png) +![图三](https://file.kamacoder.com/pics/20220825103900.png) 在第五步,第六步 我只把关键的节点染色了,其他方向周边没有去染色,大家只要关注关键地方染色的逻辑就可以。 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index ce3dbbdbe7..1611e00aa0 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -28,29 +28,29 @@ 如图一,是一个无向图,我们要搜索从节点1到节点6的所有路径。 -![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707093643.png) +![图一](https://file.kamacoder.com/pics/20220707093643.png) 那么dfs搜索的第一条路径是这样的: (假设第一次延默认方向,就找到了节点6),图二 -![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707093807.png) +![图二](https://file.kamacoder.com/pics/20220707093807.png) 此时我们找到了节点6,(遇到黄河了,是不是应该回头了),那么应该再去搜索其他方向了。 如图三: -![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707094011.png) +![图三](https://file.kamacoder.com/pics/20220707094011.png) 路径2撤销了,改变了方向,走路径3(红色线), 接着也找到终点6。 那么撤销路径2,改为路径3,在dfs中其实就是回溯的过程(这一点很重要,很多录友不理解dfs代码中回溯是用来干什么的) 又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图四中,路径4撤销(回溯的过程),改为路径5。 -![图四](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707094322.png) +![图四](https://file.kamacoder.com/pics/20220707094322.png) 又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图五,路径6撤销(回溯的过程),改为路径7,路径8 和 路径7,路径9, 结果发现死路一条,都走到了自己走过的节点。 -![图五](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707094813.png) +![图五](https://file.kamacoder.com/pics/20220707094813.png) 那么节点2所连接路径和节点3所链接的路径 都走过了,撤销路径只能向上回退,去选择撤销当初节点4的选择,也就是撤销路径5,改为路径10 。 如图图六: -![图六](https://code-thinking-1253855093.file.myqcloud.com/pics/20220707095232.png) +![图六](https://file.kamacoder.com/pics/20220707095232.png) 上图演示中,其实我并没有把 所有的 从节点1 到节点6的dfs(深度优先搜索)的过程都画出来,那样太冗余了,但 已经把dfs 关键的地方都涉及到了,关键就两点: @@ -180,7 +180,7 @@ for (选择:本节点所连接的其他节点) { 如图七所示, 路径2 已经走到了 目的地节点6,那么 路径2 是如何撤销,然后改为 路径3呢? 其实这就是 回溯的过程,撤销路径2,走换下一个方向。 -![图七](https://code-thinking-1253855093.file.myqcloud.com/pics/20220708093544.png) +![图七](https://file.kamacoder.com/pics/20220708093544.png) ## 总结 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" index 84f693a00b..9bec322765 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -17,15 +17,15 @@ 有向图是指 图中边是有方向的: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195737.png) +![](https://file.kamacoder.com/pics/20240510195737.png) 无向图是指 图中边没有方向: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195451.png) +![](https://file.kamacoder.com/pics/20240510195451.png) 加权有向图,就是图中边是有权值的,例如: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240510195821.png) +![](https://file.kamacoder.com/pics/20240510195821.png) 加权无向图也是同理。 @@ -35,7 +35,7 @@ 例如,该无向图中,节点4的度为5,节点6的度为3。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511115029.png) +![](https://file.kamacoder.com/pics/20240511115029.png) 在有向图中,每个节点有出度和入度。 @@ -45,7 +45,7 @@ 例如,该有向图中,节点3的入度为2,出度为1,节点1的入度为0,出度为2。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511115235.png) +![](https://file.kamacoder.com/pics/20240511115235.png) ## 连通性 @@ -56,11 +56,11 @@ 在无向图中,任何两个节点都是可以到达的,我们称之为连通图 ,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511102351.png) +![](https://file.kamacoder.com/pics/20240511102351.png) 如果有节点不能到达其他节点,则为非连通图,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511102449.png) +![](https://file.kamacoder.com/pics/20240511102449.png) 节点1 不能到达节点4。 @@ -72,7 +72,7 @@ 我们来看这个有向图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511104531.png) +![](https://file.kamacoder.com/pics/20240511104531.png) 这个图是强连通图吗? @@ -82,7 +82,7 @@ 下面这个有向图才是强连通图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511113101.png) +![](https://file.kamacoder.com/pics/20240511113101.png) ### 连通分量 @@ -91,7 +91,7 @@ 只看概念大家可能不理解,我来画个图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511111559.png) +![](https://file.kamacoder.com/pics/20240511111559.png) 该无向图中 节点1、节点2、节点5 构成的子图就是 该无向图中的一个连通分量,该子图所有节点都是相互可达到的。 @@ -111,7 +111,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511112951.png) +![](https://file.kamacoder.com/pics/20240511112951.png) 节点1、节点2、节点3、节点4、节点5 构成的子图是强连通分量,因为这是强连通图,也是极大图。 @@ -132,11 +132,11 @@ 例如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240511112951.png) +![](https://file.kamacoder.com/pics/20240511112951.png) 图中有8条边,我们就定义 8 * 2的数组,即有n条边就申请n * 2,这么大的数组: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20250110114348.png) +![](https://file.kamacoder.com/pics/20250110114348.png) 数组第一行:6 7,就表示节点6 指向 节点7,以此类推。 @@ -162,7 +162,7 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240222110025.png) +![](https://file.kamacoder.com/pics/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间。 @@ -188,7 +188,7 @@ 邻接表的构造如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240223103713.png) +![](https://file.kamacoder.com/pics/20240223103713.png) 这里表达的图是: diff --git "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index 54f9153900..e83880dab1 100644 --- "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -17,7 +17,7 @@ 最短路算法比较复杂,而且各自有各自的应用场景,我来用一张表把讲过的最短路算法的使用场景都展现出来: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240508121355.png) +![](https://file.kamacoder.com/pics/20240508121355.png) (因为A * 属于启发式搜索,和上面最短路算法并不是一类,不适合一起对比,所以没有放在一起) diff --git a/problems/qita/acm.md b/problems/qita/acm.md index 999283564a..33dcd8a652 100644 --- a/problems/qita/acm.md +++ b/problems/qita/acm.md @@ -11,15 +11,15 @@ [知识星球](https://programmercarl.com/other/kstar.html)里很多录友的日常打卡中,都表示被 ACM模式折磨过: -
+
-
+
-
+
-
+
-
+
所以我正式推出:**卡码网**([https://kamacoder.com](https://kamacoder.com)),**专门帮助大家练习ACM模式**。 @@ -43,17 +43,17 @@ 来看看这极简的界面,没有烂七八糟的功能,只有刷题! -
+
在「状态」这里可以看到 大家提交的代码和判题记录,目前卡码网([https://kamacoder.com](https://kamacoder.com))几乎无时无刻都有卡友在提交代码。 看看大家周六晚上都在做什么,刷哪些题目。 -
+
提交代码的界面是这样的,**目前支持所有主流刷题语言**。 -
+
## 题解 @@ -63,7 +63,7 @@ [https://github.com/youngyangyang04/kamacoder-solutions](https://github.com/youngyangyang04/kamacoder-solutions) -
+
**欢迎去Github上star,欢迎fork,也欢迎来Github仓库贡献其他语言版本,成为contributor**。 @@ -71,7 +71,7 @@ 目前已经有两位录友贡献C和Java版本了。 -
+
期待在Github(https://github.com/youngyangyang04/kamacoder-solutions) 的contributors上也出现你的头像。 diff --git a/problems/qita/join.md b/problems/qita/join.md index c7e17588bf..027b81726a 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -28,10 +28,10 @@ 点击这里Fetch upstream。 -
+
点击之后,这里就会显示最新的信息了 -
+
注意这时是你的远端仓库为最新版本,本地还不是最新的,本地要git pull一下。 @@ -39,18 +39,18 @@ 如何提交代码呢,首先把自己的代码提交到自己的fork的远端仓库中,然后open pull request,如图: -
+
点击 open pull request之后,就是如下画面,一个pull request有多个commit。 -
+
然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 -
+
此时你就提交成功了,我会在项目中的pull requests 处理列表里看到你的请求。 -
+
然后如果你发现自己的代码没有合入多半是有问题,如果有问题都有会在pull request里给出留言的, @@ -78,27 +78,27 @@ C++代码 \`\`\` 例如这个commit,在添加java代码的时候,就直接添加代码 -
+
正确的格式应该是这样: -
+
一般发现问题,我也会在代码中给出评论: -
+
这样大家也可以学习一些 提交代码的规范方面的知识 有的录友 是添加的代码块语法,但没有标记是哪种语言,这样的话 代码就不会针对某种语言高亮显示了,也比较影响阅读,例如: -
+
提交python代码的话,要注释好,是python2还是python3 例如这样: -
+
当然python2的话,只这么写就行 @@ -113,7 +113,7 @@ python代码 有的录友是一个pull request 里只有一个commit。 -
+
其实如果大家是平时一天写了两三道题目的话,那么分三个commit,一个pull request提交上来就行。 @@ -127,13 +127,13 @@ python代码 例如这位录友,在提交Java代码的时候,按照题解的意思对Java版本的代码进行的注释,这就很棒👍 -
+
-
+
当然如果大家感觉 已有的代码 不符合以上要求的话,例如 代码思路不够清晰不够规范,注释不够友好,依然欢迎提交优化代码,要记得详细注释哦。 -
+
### 说明具体是哪种方法 @@ -141,10 +141,10 @@ python代码 下面这位录友做的就很好 -
+
-
+
有的题解,是一起给出了多道题目的讲解,例如项目中0102.二叉树的层序遍历.md 中有八道题目,那么大家添加代码的时候 应该在代码注释上,或者 直接写上 是哪个题目的代码。 @@ -162,7 +162,7 @@ python代码 有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 -
+
**代码规范从你我做起!** @@ -183,10 +183,10 @@ python代码 在合入的过程中还要处理冲突的代码, 理解大家代码的思路,解决冲突,然后在力扣提交一下,确保是没问题。 例如同一道题目, 一位录友提交了, 我还没处理如何,另一位录友也对这道题也提交了代码,这样就会发生冲突 -
+
大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。 -
+
一天晚上分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] @@ -201,11 +201,11 @@ python代码 确保这种额外文件不要提交。 -
+
还有添加不同方法的时候,直接用正文格式写,哪种方法就可以了,不要添加目录 ,例如这样,这样整篇文章目录结构就有影响了。 -
+
前面不要加 `## 前序遍历(迭代法)`,直接写`前序遍历(迭代法)`就可以了。 @@ -233,11 +233,11 @@ Go语言代码 甚至发现哪里有语病,也欢迎提交PR来修改,例如下面:就是把【下表】 纠正为【下标】 -
+
不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 -
+
## 特别注意 diff --git a/problems/qita/server.md b/problems/qita/server.md index 7e214d7993..72476f5754 100644 --- a/problems/qita/server.md +++ b/problems/qita/server.md @@ -51,7 +51,7 @@ 操作方式这样,我把命令包 包装成一个shell命令,想传那个文件,直接 uploadtomyserver,然后就返回可以下载的链接,这个文件也同时传到了我的服务器上。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211126165643.png) +![](https://file.kamacoder.com/pics/20211126165643.png) 我也把我的项目代码放在了github上: @@ -93,11 +93,11 @@ https://github.com/youngyangyang04/fileHttpServer 就是这样一个非常普通的查询页面。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211126160200.png) +![](https://file.kamacoder.com/pics/20211126160200.png) 查询通过之后,就会展现返现群二维码。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211127160558.png) +![](https://file.kamacoder.com/pics/20211127160558.png) 但要部署在服务器上,因为没有公网IP,别人用不了你的服务。 diff --git a/problems/qita/shejimoshi.md b/problems/qita/shejimoshi.md index d5342c6132..07d5540a25 100644 --- a/problems/qita/shejimoshi.md +++ b/problems/qita/shejimoshi.md @@ -7,11 +7,11 @@ 所以卡码网 针对 23种设计,**推出了 23道编程题目,来帮助大家练习设计模式**。 -
+
这里的23到编程题目对应了 23种这几模式。 例如第一题,小明的购物车,就是单例模式: -
+
区别于网上其他教程,本教程的特点是: @@ -40,18 +40,18 @@ 同时还给全部整理到PDF上,这份PDF,我们写的很用心了,来个大家截个图: -
+
-
+
-
+
-
+
关于设计模式的题目,大家现在就可以去 卡码网(kamacoder)去做了。 关于这23道题目对应 设计模式精讲 PDF,也免费分享给录友们,大家可以加我的企业微信获取: -
+
已经有我企业微信的录友,直接发:设计模式,这四个字就好,我会直接发你。 diff --git a/problems/qita/tulunfabu.md b/problems/qita/tulunfabu.md index 28ee463850..b45b790d81 100644 --- a/problems/qita/tulunfabu.md +++ b/problems/qita/tulunfabu.md @@ -8,7 +8,7 @@ 我知道录友们在等图论等太久了,其实我比大家都着急。 -![大家一直都在催](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105618.png) +![大家一直都在催](https://file.kamacoder.com/pics/20240613105618.png) 图论完整版目前已经开放在代码随想录网站:programmercarl.com @@ -20,7 +20,7 @@ * 拓扑排序 * 最短路算法 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104436.png) +![](https://file.kamacoder.com/pics/20240613104436.png) **耗时一年之久,代码随想录图论 终于面世了**! @@ -32,21 +32,21 @@ 随便截一些图,大家感受一下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104703.png) +![](https://file.kamacoder.com/pics/20240613104703.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104824.png) +![](https://file.kamacoder.com/pics/20240613104824.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104852.png) +![](https://file.kamacoder.com/pics/20240613104852.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613104926.png) +![](https://file.kamacoder.com/pics/20240613104926.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105007.png) +![](https://file.kamacoder.com/pics/20240613105007.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105030.png) +![](https://file.kamacoder.com/pics/20240613105030.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105106.png) +![](https://file.kamacoder.com/pics/20240613105106.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240613105143.png) +![](https://file.kamacoder.com/pics/20240613105143.png) 具体内容,大家可以去代码随想录网站(programmercarl.com)去看看,非常精彩! @@ -203,19 +203,19 @@ cout << result[result.size() - 1]; 当大家通过 代码随想录 提升了编程与算法能力,考上研或者找到好工作的时候,于我来说已经是很幸福的事情: -![对笔试帮助大](https://code-thinking-1253855093.file.myqcloud.com/pics/20230914172536.png) +![对笔试帮助大](https://file.kamacoder.com/pics/20230914172536.png) -![华为od将近满分](https://code-thinking-1253855093.file.myqcloud.com/pics/20230914172607.png) +![华为od将近满分](https://file.kamacoder.com/pics/20230914172607.png) -![研究生复试](https://code-thinking-1253855093.file.myqcloud.com/pics/20240621103130.png) +![研究生复试](https://file.kamacoder.com/pics/20240621103130.png) -![红包感谢代码随想录366](https://code-thinking-1253855093.file.myqcloud.com/pics/20231123151310.png) +![红包感谢代码随想录366](https://file.kamacoder.com/pics/20231123151310.png) -![上岸亚马逊](https://code-thinking-1253855093.file.myqcloud.com/pics/20240206174151.png) +![上岸亚马逊](https://file.kamacoder.com/pics/20240206174151.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220718094112.png) +![](https://file.kamacoder.com/pics/20220718094112.png) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220718094332.png) +![](https://file.kamacoder.com/pics/20220718094332.png) 至此**图论内容 已完全免费开放在代码随想录网站(programmercarl.com),造福广大学习编程的录友们**! diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" index f4d093d638..a67cf5db9d 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -147,7 +147,7 @@ 二叉树专题汇聚为一张图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211030125421.png) +![](https://file.kamacoder.com/pics/20211030125421.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 9a63b66cfd..1fbbc9d934 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -15,7 +15,7 @@ 题目分类大纲如下: -二叉树大纲 +二叉树大纲 说到二叉树,大家对于二叉树其实都很熟悉了,本文呢我也不想教科书式的把二叉树的基础内容再啰嗦一遍,所以以下我讲的都是一些比较重点的内容。 @@ -31,7 +31,7 @@ 如图所示: - + 这棵二叉树为满二叉树,也可以说深度为k,有2^k-1个节点的二叉树。 @@ -46,7 +46,7 @@ 我来举一个典型的例子如题: - + 相信不少同学最后一个二叉树是不是完全二叉树都中招了。 @@ -63,7 +63,7 @@ 下面这两棵树都是搜索树 - + ### 平衡二叉搜索树 @@ -72,7 +72,7 @@ 如图: - + 最后一棵 不是平衡二叉树,因为它的左右两个子树的高度差的绝对值超过了1。 @@ -91,13 +91,13 @@ 链式存储如图: - + 链式存储是大家很熟悉的一种方式,那么我们来看看如何顺序存储呢? 其实就是用数组来存储二叉树,顺序存储的方式如图: - + 用数组来存储二叉树如何遍历的呢? @@ -144,7 +144,7 @@ 大家可以对着如下图,看看自己理解的前后中序有没有问题。 - + 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 289c651b60..e011612ca6 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -117,7 +117,7 @@ public: 再来看后序遍历,先序遍历是中左右,后序遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: -![前序到后序](https://code-thinking-1253855093.file.myqcloud.com/pics/20200808200338924.png) +![前序到后序](https://file.kamacoder.com/pics/20200808200338924.png) **所以后序遍历只需要前序遍历的代码稍作修改就可以了,代码如下:** diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" index 313264fba2..f1ff3a5ffc 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" @@ -5,15 +5,15 @@ 平时大家在力扣上刷题,就是 核心代码模式,即给你一个函数,直接写函数实现,例如这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193631.png) +![](https://file.kamacoder.com/pics/20231109193631.png) 而ACM模式,是程序头文件,main函数,数据的输入输出都要自己处理,例如这样: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193743.png) +![](https://file.kamacoder.com/pics/20231109193743.png) 大家可以发现 右边代码框什么都没有,程序从头到尾都需要自己实现,本题如果写完代码是这样的: (细心的录友可以发现和力扣上刷题是不一样的) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109193931.png) +![](https://file.kamacoder.com/pics/20231109193931.png) **如果大家从一开始学习算法就一直在力扣上的话,突然切到ACM模式会非常不适应**。 @@ -21,15 +21,15 @@ 知识星球里也有很多录友,因为不熟悉ACM模式在面试的过程中吃了不少亏。 -
+
-
+
-
+
-
+
-
+
## 面试究竟怎么考? @@ -53,7 +53,7 @@ 你只要能把卡码网首页的25道题目 都刷了 ,就把所有的ACM输入输出方式都练习到位了,不会有任何盲区。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231109195056.png) +![](https://file.kamacoder.com/pics/20231109195056.png) 而且你不用担心,题目难度太大,直接给自己劝退,**卡码网的前25道题目都是我精心制作的,难度也是循序渐进的**,大家去刷一下就知道了。 diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" index 48781eda88..086e6e0ea0 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" @@ -15,7 +15,7 @@ 其输入用例,就是用一个数组来表述 二叉树,如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914222335.png) +![](https://file.kamacoder.com/pics/20210914222335.png) 一直跟着公众号学算法的录友 应该知道,我在[二叉树:构造二叉树登场!](https://mp.weixin.qq.com/s/Dza-fqjTyGrsRw4PWNKdxA),已经讲过,**只有 中序与后序 和 中序和前序 可以确定一棵唯一的二叉树。 前序和后序是不能确定唯一的二叉树的**。 @@ -24,7 +24,7 @@ 很明显,是后台直接明确了构造规则。 再看一下 这个 输入序列 和 对应的二叉树。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914222335.png) +![](https://file.kamacoder.com/pics/20210914222335.png) 从二叉树 推导到 序列,大家可以发现这就是层序遍历。 @@ -36,7 +36,7 @@ 顺序存储,就是用一个数组来存二叉树,其方式如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914223147.png) +![](https://file.kamacoder.com/pics/20210914223147.png) 那么此时大家是不是应该知道了,数组如何转化成 二叉树了。**如果父节点的数组下标是i,那么它的左孩子下标就是i * 2 + 1,右孩子下标就是 i * 2 + 2**。 @@ -80,7 +80,7 @@ TreeNode* construct_binary_tree(const vector& vec) { 这个函数最后返回的 指针就是 根节点的指针, 这就是 传入二叉树的格式了,也就是 力扣上的用例输入格式,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914224422.png) +![](https://file.kamacoder.com/pics/20210914224422.png) 也有不少同学在做ACM模式的题目,就经常疑惑: @@ -176,7 +176,7 @@ int main() { 和 [538.把二叉搜索树转换为累加树](https://mp.weixin.qq.com/s/rlJUFGCnXsIMX0Lg-fRpIw) 中的输入是一样的 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914222335.png) +![](https://file.kamacoder.com/pics/20210914222335.png) 这里可能又有同学疑惑,你这不一样啊,题目是null,你为啥用-1。 @@ -184,11 +184,11 @@ int main() { 在来看,测试代码输出的效果: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914230045.png) +![](https://file.kamacoder.com/pics/20210914230045.png) 可以看出和 题目中输入用例 这个图 是一样一样的。 只不过题目中图没有把 空节点 画出来而已。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914230118.png) +![](https://file.kamacoder.com/pics/20210914230118.png) 大家可以拿我的代码去测试一下,跑一跑。 @@ -205,7 +205,7 @@ int main() { **[知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)**里有的录友已经开始三刷: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727234031.png) +![](https://file.kamacoder.com/pics/20210727234031.png) 只做过一遍,真的就是懂了一点皮毛, 第二遍刷才有真的对各个题目有较为深入的理解,也会明白 我为什么要这样安排刷题的顺序了。 diff --git "a/problems/\345\211\215\345\272\217/vim.md" "b/problems/\345\211\215\345\272\217/vim.md" index 581019995a..13935b91c6 100644 --- "a/problems/\345\211\215\345\272\217/vim.md" +++ "b/problems/\345\211\215\345\272\217/vim.md" @@ -93,7 +93,7 @@ sh install.sh 当然 还有很多,我还详细写了PowerVim的快捷键,使用方法,插件,配置,等等,都在Github主页的README上。当时我的Github上写的都是英文README,这次为了方便大家阅读,我又翻译成中文README。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211013102249.png) +![](https://file.kamacoder.com/pics/20211013102249.png) Github地址:[https://github.com/youngyangyang04/PowerVim](https://github.com/youngyangyang04/PowerVim) diff --git "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" index 95f4f129da..33891632b8 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" @@ -57,7 +57,7 @@ 我做了一下总结如图: -![编程风格](https://code-thinking-1253855093.file.myqcloud.com/pics/20201119173039835.png) +![编程风格](https://file.kamacoder.com/pics/20201119173039835.png) ### 水平留白(代码空格) diff --git "a/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" "b/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" index f70564e4a2..88fb129ee2 100644 --- "a/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" +++ "b/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" @@ -19,7 +19,7 @@ 如果我们写C++的程序,就要知道栈和堆的概念,程序运行时所需的内存空间分为 固定部分,和可变部分,如下: -![C++内存空间](https://code-thinking-1253855093.file.myqcloud.com/pics/20210309165950660.png) +![C++内存空间](https://file.kamacoder.com/pics/20210309165950660.png) 固定部分的内存消耗 是不会随着代码运行产生变化的, 可变部分则是会产生变化的 @@ -41,7 +41,7 @@ 想要算出自己程序会占用多少内存就一定要了解自己定义的数据类型的大小,如下: -![C++数据类型的大小](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193045440.png) +![C++数据类型的大小](https://file.kamacoder.com/pics/20200804193045440.png) 注意图中有两个不一样的地方,为什么64位的指针就占用了8个字节,而32位的指针占用4个字节呢? @@ -109,7 +109,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第一种就是内存对齐的情况,如图: -![内存对齐](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193307347.png) +![内存对齐](https://file.kamacoder.com/pics/20200804193307347.png) 一字节的char占用了四个字节,空了三个字节的内存地址,int数据从地址4开始。 @@ -117,7 +117,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第二种是没有内存对齐的情况如图: -![非内存对齐](https://code-thinking-1253855093.file.myqcloud.com/pics/20200804193353926.png) +![非内存对齐](https://file.kamacoder.com/pics/20200804193353926.png) char型的数据和int型的数据挨在一起,该int数据从地址1开始,那么CPU想要读这个数据的话来看看需要几步操作: diff --git "a/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" "b/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" index 61f0a7ef6a..045646ff7e 100644 --- "a/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" +++ "b/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" @@ -38,7 +38,7 @@ 同样的同理再看一下快速排序,都知道快速排序是O(nlogn),但是当数据已经有序情况下,快速排序的时间复杂度是O(n^2) 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是O(n^2)**。 **但是我们依然说快速排序是O(nlogn)的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示: -![时间复杂度4,一般情况下的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728185745611-20230310123844306.png) +![时间复杂度4,一般情况下的时间复杂度](https://file.kamacoder.com/pics/20200728185745611-20230310123844306.png) 我们主要关心的还是一般情况下的数据形式。 @@ -49,7 +49,7 @@ 如下图中可以看出不同算法的时间复杂度在不同数据输入规模下的差异。 -![时间复杂度,不同数据规模的差异](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447384-20230310124015324.png) +![时间复杂度,不同数据规模的差异](https://file.kamacoder.com/pics/20200728191447384-20230310124015324.png) 在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。 @@ -115,7 +115,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 为什么可以这么做呢?如下图所示: -![时间复杂度1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728191447349-20230310124032001.png) +![时间复杂度1.png](https://file.kamacoder.com/pics/20200728191447349-20230310124032001.png) 假如有两个算法的时间复杂度,分别是log以2为底n的对数和log以10为底n的对数,那么这里如果还记得高中数学的话,应该不难理解`以2为底n的对数 = 以2为底10的对数 * 以10为底n的对数`。 diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" index 7fbfa1fd2b..1bdcccd586 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" @@ -103,13 +103,13 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 最后福利,把我的简历模板贡献出来!如下图所示。 -![简历模板](https://code-thinking-1253855093.file.myqcloud.com/pics/20200803175538158.png) +![简历模板](https://file.kamacoder.com/pics/20200803175538158.png) 这里是简历模板中Markdown的代码:[https://github.com/youngyangyang04/Markdown-Resume-Template](https://github.com/youngyangyang04/Markdown-Resume-Template) ,可以fork到自己Github仓库上,按照这个模板来修改自己的简历。 **Word版本的简历,添加如下企业微信,通过之后就会发你word版本**。 -
+
如果已经有我的企业微信,直接回复:简历模板,就可以了。 diff --git "a/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" "b/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" index 34ebe6deb1..ca1a422c0b 100644 --- "a/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" +++ "b/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" @@ -8,7 +8,7 @@ ## 超时是怎么回事 -![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117-20230310124308704.png) +![程序超时](https://file.kamacoder.com/pics/20200729112716117-20230310124308704.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -124,11 +124,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729200018460-20230310124315093.png) +![程序超时2](https://file.kamacoder.com/pics/20200729200018460-20230310124315093.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下 $O(n^2)$ 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://code-thinking-1253855093.file.myqcloud.com/pics/2020072919590970-20230310124318532.png) +![程序超时3](https://file.kamacoder.com/pics/2020072919590970-20230310124318532.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -136,7 +136,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 $O(n)$ 少一个数量级,因为 $\log n$ 的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729195729407-20230310124322232.png) +![程序超时4](https://file.kamacoder.com/pics/20200729195729407-20230310124322232.png) $O(n\log n)$ 的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -144,7 +144,7 @@ $O(n\log n)$ 的算法,1s内大概计算机可以运行 2 * (10^7)次计算, **整体测试数据整理如下:** -![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175-20230310124325152.png) +![程序超时1](https://file.kamacoder.com/pics/20201208231559175-20230310124325152.png) 至于 $O(\log n)$ 和 $O(n^3)$ 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" index 39513a9155..035399ce0f 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" @@ -29,7 +29,7 @@ int fibonacci(int i) { 可以看出上面的代码每次递归都是O(1)的操作。再来看递归了多少次,这里将i为5作为输入的递归过程 抽象成一棵递归树,如图: -![递归空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305093200104.png) +![递归空间复杂度分析](https://file.kamacoder.com/pics/20210305093200104.png) 从图中,可以看出f(5)是由f(4)和f(3)相加而来,那么f(4)是由f(3)和f(2)相加而来 以此类推。 @@ -196,7 +196,7 @@ int main() 在看递归的深度是多少呢?如图所示: -![递归空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305094749554.png) +![递归空间复杂度分析](https://file.kamacoder.com/pics/20210305094749554.png) 递归第n个斐波那契数的话,递归调用栈的深度就是n。 @@ -214,7 +214,7 @@ int fibonacci(int i) { 最后对各种求斐波那契数列方法的性能做一下分析,如题: -![递归的空间复杂度分析](https://code-thinking-1253855093.file.myqcloud.com/pics/20210305095227356.png) +![递归的空间复杂度分析](https://file.kamacoder.com/pics/20210305095227356.png) 可以看出,求斐波那契数的时候,使用递归算法并不一定是在性能上是最优的,但递归确实简化的代码层面的复杂度。 diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" index 1ea1e65be8..befe549874 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" @@ -69,7 +69,7 @@ int function3(int x, int n) { 我们来分析一下,首先看递归了多少次呢,可以把递归抽象出一棵满二叉树。刚刚同学写的这个算法,可以用一棵满二叉树来表示(为了方便表示,选择n为偶数16),如图: -![递归算法的时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209193909426.png) +![递归算法的时间复杂度](https://file.kamacoder.com/pics/20201209193909426.png) 当前这棵二叉树就是求x的n次方,n为16的情况,n为16的时候,进行了多少次乘法运算呢? @@ -79,7 +79,7 @@ int function3(int x, int n) { 这么如果是求x的n次方,这个递归树有多少个节点呢,如下图所示:(m为深度,从0开始) -![递归求时间复杂度](https://code-thinking-1253855093.file.myqcloud.com/pics/20200728195531892.png) +![递归求时间复杂度](https://file.kamacoder.com/pics/20200728195531892.png) **时间复杂度忽略掉常数项`-1`之后,这个递归算法的时间复杂度依然是O(n)**。对,你没看错,依然是O(n)的时间复杂度! diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index fd2d16f47a..a88919d4ca 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -35,11 +35,11 @@ 如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030165201.png) +![](https://file.kamacoder.com/pics/20231030165201.png) 然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231030173058.png) +![](https://file.kamacoder.com/pics/20231030173058.png) 有同学问了,为什么要从后向前填充,从前向后填充不行么? diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 6fe12e3d51..4c62312c8b 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -44,16 +44,16 @@ fgabcde 本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106170143.png) +![](https://file.kamacoder.com/pics/20231106170143.png) 右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106171557.png) +![](https://file.kamacoder.com/pics/20231106171557.png) 此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172058.png) +![](https://file.kamacoder.com/pics/20231106172058.png) 其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 @@ -84,7 +84,7 @@ int main() { 可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20231106172534.png) +![](https://file.kamacoder.com/pics/20231106172534.png) 代码如下: diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index ea5082244c..3212ca5603 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -254,5 +254,5 @@ traversal(cur->left, tmp, result); * Github:[leetcode-master](https://github.com/youngyangyang04/leetcode-master) * 知乎:[代码随想录](https://www.zhihu.com/people/sun-xiu-yang-64) -![](https://code-thinking-1253855093.file.myqcloud.com/pics/2021013018121150.png) +![](https://file.kamacoder.com/pics/2021013018121150.png)
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 7e333c7672..d910ce2509 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -31,7 +31,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)第一个树形结构没有画出startIndex的作用,**这里这里纠正一下,准确的树形结构如图所示:** -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) ## 周二 @@ -45,7 +45,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123202817973.png) +![40.组合总和II1](https://file.kamacoder.com/pics/20201123202817973.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: @@ -79,7 +79,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; **本题的树形结构中,和代码的逻辑有一个小出入,已经判断不是回文的子串就不会进入递归了,纠正如下:** -![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203228309.png) +![131.分割回文串](https://file.kamacoder.com/pics/20201123203228309.png) ## 周四 @@ -90,7 +90,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形图如下: -![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933-20230310133532452.png) +![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933-20230310133532452.png) 在本文的树形结构图中,我已经把详细的分析思路都画了出来,相信大家看了之后一定会思路清晰不少! @@ -112,7 +112,7 @@ if (s.size() > 12) return result; // 剪枝 如图: -![78.子集](https://code-thinking-1253855093.file.myqcloud.com/pics/202011232041348.png) +![78.子集](https://file.kamacoder.com/pics/202011232041348.png) 认清这个本质之后,今天的题目就是一道模板题了。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index ec36d1213d..c2e122844d 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -11,14 +11,14 @@ 树形结构如下: -![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449-20230310133150714.png) +![90.子集II](https://file.kamacoder.com/pics/2020111217110449-20230310133150714.png) ## 周二 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170832333-20230310133155209.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201112170832333-20230310133155209.png) [回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)留言区大家有很多疑问,主要还是和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混合在了一起。 @@ -33,7 +33,7 @@ 可以看出元素1在[1,2]中已经使用过了,但是在[2,1]中还要在使用一次1,所以处理排列问题就不用使用startIndex了。 如图: -![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170304979-20230310133201250.png) +![46.全排列](https://file.kamacoder.com/pics/20201112170304979-20230310133201250.png) **大家此时可以感受出排列问题的不同:** @@ -46,7 +46,7 @@ 树形结构如下: -![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112171930470-20230310133206398.png) +![47.全排列II1](https://file.kamacoder.com/pics/20201112171930470-20230310133206398.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -54,11 +54,11 @@ 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172230434-20230310133211392.png) +![47.全排列II2.png](https://file.kamacoder.com/pics/20201112172230434-20230310133211392.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172327967-20230310133216389.png) +![47.全排列II3](https://file.kamacoder.com/pics/20201112172327967-20230310133216389.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 9209efda71..4ab1cddbff 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -15,7 +15,7 @@ 如图: -![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858.png) +![122.买卖股票的最佳时机II](https://file.kamacoder.com/pics/2020112917480858.png) ## 周二 @@ -31,7 +31,7 @@ 如图: -![55.跳跃游戏](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124154758229.png) +![55.跳跃游戏](https://file.kamacoder.com/pics/20201124154758229.png) ## 周三 @@ -44,7 +44,7 @@ 如图: -![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103-20230310133110942.png) +![45.跳跃游戏II](https://file.kamacoder.com/pics/20201201232309103-20230310133110942.png) 注意:**图中的移动下标是到当前这步覆盖的最远距离(下标2的位置),此时没有到终点,只能增加第二步来扩大覆盖范围**。 @@ -55,10 +55,10 @@ 而版本二就比较统一的,超过范围,步数就加一,但在移动下标的范围了做了文章。 即如果覆盖最远距离下标是倒数第二点:直接加一就行,默认一定可以到终点。如图: -![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286-20230310133115650.png) +![45.跳跃游戏II2](https://file.kamacoder.com/pics/20201201232445286-20230310133115650.png) 如果覆盖最远距离下标不是倒数第二点,说明本次覆盖已经到终点了。如图: -![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693-20230310133120115.png) +![45.跳跃游戏II1](https://file.kamacoder.com/pics/20201201232338693-20230310133120115.png) 有的录友认为版本一好理解,有的录友认为版本二好理解,其实掌握一种就可以了,也不用非要比拼一下代码的简洁性,简洁程度都差不多了。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" index 461219f2da..f449995b01 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -77,7 +77,7 @@ 文中从计算机硬件出发,分析计算机的计算性能,然后亲自做实验,整理出数据如下: -![程序超时1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201208231559175-20230310133304038.png) +![程序超时1](https://file.kamacoder.com/pics/20201208231559175-20230310133304038.png) **大家有一个数量级上的概念就可以了!** diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 91f9656f48..19a95615a3 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -38,7 +38,7 @@ 如图: -![135.分发糖果](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117114916878-20230310133332759.png) +![135.分发糖果](https://file.kamacoder.com/pics/20201117114916878-20230310133332759.png) 接着在贪心另一边,左孩子大于右孩子,左孩子的糖果就要比右孩子多。 @@ -50,7 +50,7 @@ 局部最优可以推出全局最优。 如图: -![135.分发糖果1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117115658791-20230310133346127.png) +![135.分发糖果1](https://file.kamacoder.com/pics/20201117115658791-20230310133346127.png) ## 周三 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index b37bba80dc..a8ba7454eb 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -9,7 +9,7 @@ 如图: -![452.用最少数量的箭引爆气球](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123101929791-20230310133845522.png) +![452.用最少数量的箭引爆气球](https://file.kamacoder.com/pics/20201123101929791-20230310133845522.png) 模拟射气球的过程,很多同学真的要去模拟了,实时把气球从数组中移走,这么写的话就复杂了,从前向后遍历重复的只要跳过就可以的。 @@ -21,7 +21,7 @@ 如图: -![435.无重叠区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201221201553618.png) +![435.无重叠区间](https://file.kamacoder.com/pics/20201221201553618.png) 细心的同学就发现了,此题和 [贪心算法:用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像。 @@ -71,7 +71,7 @@ public: 如图: -![763.划分字母区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201222191924417-20230310133855435.png) +![763.划分字母区间](https://file.kamacoder.com/pics/20201222191924417-20230310133855435.png) ## 周四 @@ -86,7 +86,7 @@ public: 如图: -![56.合并区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223200632791-20230310133859587.png) +![56.合并区间](https://file.kamacoder.com/pics/20201223200632791-20230310133859587.png) ## 总结 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index ebc720caa0..1b6bd84bcc 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -31,7 +31,7 @@ for (int i = 1; i < m; i++) { } ``` -![62.不同路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113631392-20230310133703294.png) +![62.不同路径1](https://file.kamacoder.com/pics/20201209113631392-20230310133703294.png) ## 周二 @@ -45,7 +45,7 @@ dp[i][j]定义依然是:表示从(0 ,0)出发,到(i, j) 有dp[i][j]条 如图: -![63.不同路径II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114513928-20230310133707783.png) +![63.不同路径II](https://file.kamacoder.com/pics/20210104114513928-20230310133707783.png) 这里难住了不少同学,代码如下: @@ -70,11 +70,11 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114548983-20230310133711888.png) +![63.不同路径II1](https://file.kamacoder.com/pics/20210104114548983-20230310133711888.png) 对应的dp table 如图: -![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256-20230310133715981.png) +![63.不同路径II2](https://file.kamacoder.com/pics/20210104114610256-20230310133715981.png) ## 周三 @@ -111,7 +111,7 @@ for (int i = 3; i <= n ; i++) { 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104173021581-20230310133720552.png) +![343.整数拆分](https://file.kamacoder.com/pics/20210104173021581-20230310133720552.png) @@ -143,7 +143,7 @@ dp数组如何初始化:只需要初始化dp[0]就可以了,推导的基础 n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093253987-20230310133724531.png) +![96.不同的二叉搜索树3](https://file.kamacoder.com/pics/20210107093253987-20230310133724531.png) ## 总结 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 7bae5ca9ee..dc32891da8 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -17,7 +17,7 @@ 关于其他几种常用的背包,大家看这张图就了然于胸了: -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407-20230310133624872.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407-20230310133624872.png) 本文用动规五部曲详细讲解了01背包的二维dp数组的实现方法,大家其实可以发现最简单的是推导公式了,推导公式估计看一遍就记下来了,但难就难在确定初始化和遍历顺序上。 @@ -70,7 +70,7 @@ for(int i = 1; i < weight.size(); i++) { // 遍历物品 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210118163425129-20230310133630224.jpg) +![动态规划-背包问题4](https://file.kamacoder.com/pics/20210118163425129-20230310133630224.jpg) 最终结果就是dp[2][4]。 @@ -122,7 +122,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103614769-20230310133634873.png) +![动态规划-背包问题9](https://file.kamacoder.com/pics/20210110103614769-20230310133634873.png) ## 周三 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index e785af1228..8598ec69f0 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -35,7 +35,7 @@ bagSize = (S + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![494.目标和](https://code-thinking-1253855093.file.myqcloud.com/pics/20210125120743274-20230310132918821.jpg) +![494.目标和](https://file.kamacoder.com/pics/20210125120743274-20230310132918821.jpg) ## 周二 @@ -73,7 +73,7 @@ dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1); 最后dp数组的状态如下所示: -![474.一和零](https://code-thinking-1253855093.file.myqcloud.com/pics/20210120111201512-20230310132936011.jpg) +![474.一和零](https://file.kamacoder.com/pics/20210120111201512-20230310132936011.jpg) ## 周三 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 26d805bb3a..309f277f6a 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -30,7 +30,7 @@ dp[1] = max(nums[0], nums[1]); 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://code-thinking-1253855093.file.myqcloud.com/pics/20210221170954115-20230310133425353.jpg) +![198.打家劫舍](https://file.kamacoder.com/pics/20210221170954115-20230310133425353.jpg) 红框dp[nums.size() - 1]为结果。 @@ -42,15 +42,15 @@ dp[1] = max(nums[0], nums[1]); * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160748643.jpg) +![213.打家劫舍II](https://file.kamacoder.com/pics/20210129160748643.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160821374.jpg) +![213.打家劫舍II1](https://file.kamacoder.com/pics/20210129160821374.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160842491.jpg) +![213.打家劫舍II2](https://file.kamacoder.com/pics/20210129160842491.jpg) 需要注意的是,**“考虑” 不等于 “偷”**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素!对于情况三,取nums[1] 和 nums[3]就是最大的。 @@ -178,7 +178,7 @@ return {val2, val1}; 以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**) -![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129181331613.jpg) +![337.打家劫舍III](https://file.kamacoder.com/pics/20210129181331613.jpg) **最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index ec442a3979..5d84fb1915 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -77,7 +77,7 @@ dp[0][4] = 0; 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201228181724295.png) +![123.买卖股票的最佳时机III](https://file.kamacoder.com/pics/20201228181724295.png) 可以看到红色框为最后两次卖出的状态。 @@ -144,7 +144,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229100358221-20230310133805763.png) +![188.买卖股票的最佳时机IV](https://file.kamacoder.com/pics/20201229100358221-20230310133805763.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 @@ -197,7 +197,7 @@ vector> dp(n, vector(3, 0)); 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229163725348.png) +![309.最佳买卖股票时机含冷冻期](https://file.kamacoder.com/pics/20201229163725348.png) 最后两个状态 不持有股票(能购买) 和 不持有股票(冷冻期)都有可能最后结果,取最大的。 diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index b7d10671e1..92b590bc89 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -18,7 +18,7 @@ 哈希表中关键码就是数组的索引下标,然后通过下标直接访问数组中的元素,如下图所示: -![哈希表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104234805168.png) +![哈希表1](https://file.kamacoder.com/pics/20210104234805168.png) 那么哈希表能解决什么问题呢,**一般哈希表都是用来快速判断一个元素是否出现集合里。** @@ -36,7 +36,7 @@ 哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。 -![哈希表2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021010423484818.png) +![哈希表2](https://file.kamacoder.com/pics/2021010423484818.png) 如果hashCode得到的数值大于 哈希表的大小了,也就是大于tableSize了,怎么办呢? @@ -52,7 +52,7 @@ 如图所示,小李和小王都映射到了索引下标 1 的位置,**这一现象叫做哈希碰撞**。 -![哈希表3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021010423494884.png) +![哈希表3](https://file.kamacoder.com/pics/2021010423494884.png) 一般哈希碰撞有两种解决方法, 拉链法和线性探测法。 @@ -60,7 +60,7 @@ 刚刚小李和小王在索引1的位置发生了冲突,发生冲突的元素都被存储在链表中。 这样我们就可以通过索引找到小李和小王了 -![哈希表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235015226.png) +![哈希表4](https://file.kamacoder.com/pics/20210104235015226.png) (数据规模是dataSize, 哈希表的大小为tableSize) @@ -72,7 +72,7 @@ 例如冲突的位置,放了小李,那么就向下找一个空位放置小王的信息。所以要求tableSize一定要大于dataSize ,要不然哈希表上就没有空置的位置来存放 冲突的数据了。如图所示: -![哈希表5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235109950.png) +![哈希表5](https://file.kamacoder.com/pics/20210104235109950.png) 其实关于哈希碰撞还有非常多的细节,感兴趣的同学可以再好好研究一下,这里我就不再赘述了。 @@ -117,7 +117,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 实际上功能都是一样一样的, 但是unordered_set在C++11的时候被引入标准库了,而hash_set并没有,所以建议还是使用unordered_set比较好,这就好比一个是官方认证的,hash_set,hash_map 是C++11标准之前民间高手自发造的轮子。 -![哈希表6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235134572.png) +![哈希表6](https://file.kamacoder.com/pics/20210104235134572.png) ## 总结 diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index b8d96193ee..8fd69d518e 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -63,7 +63,7 @@ void backtracking(参数) { 本题我把回溯问题抽象为树形结构,如题: -![77.组合1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118152928844.png) +![77.组合1](https://file.kamacoder.com/pics/20201118152928844.png) 可以直观的看出其搜索的过程:**for循环横向遍历,递归纵向遍历,回溯不断调整结果集**,这个理念贯穿整个回溯法系列,也是我做了很多回溯的题目,不断摸索其规律才总结出来的。 @@ -73,7 +73,7 @@ void backtracking(参数) { 优化回溯算法只有剪枝一种方法,在[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中把回溯法代码做了剪枝优化,树形结构如图: -![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118153133458.png) +![77.组合4](https://file.kamacoder.com/pics/20201118153133458.png) 大家可以一目了然剪的究竟是哪里。 @@ -89,11 +89,11 @@ void backtracking(参数) { 在[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中,相当于 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)加了一个元素总和的限制。 树形结构如图: -![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118201921245.png) +![216.组合总和III](https://file.kamacoder.com/pics/20201118201921245.png) 整体思路还是一样的,本题的剪枝会好想一些,即:**已选元素总和如果已经大于n(题中要求的和)了,那么往后遍历就没有意义了,直接剪掉**,如图: -![216.组合总和III1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202038240.png) +![216.组合总和III1](https://file.kamacoder.com/pics/20201118202038240.png) 在本题中,依然还可以有一个剪枝,就是[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中提到的,对for循环选择的起始范围的剪枝。 @@ -114,7 +114,7 @@ void backtracking(参数) { **注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路**。 树形结构如下: -![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png) +![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) 最后还给出了本题的剪枝优化,如下: @@ -125,7 +125,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 优化后树形结构如下: -![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202115929.png) +![39.组合总和1](https://file.kamacoder.com/pics/20201118202115929.png) #### 组合总和(三) @@ -140,7 +140,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111820220675.png) +![40.组合总和II1](https://file.kamacoder.com/pics/2020111820220675.png) 我在图中将used的变化用橘黄色标注上,**可以看出在candidates[i] == candidates[i - 1]相同的情况下:** @@ -161,7 +161,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202335724.png) +![17. 电话号码的字母组合](https://file.kamacoder.com/pics/20201118202335724.png) 如果大家在现场面试的时候,一定要注意各种输入异常的情况,例如本题输入1 * #按键。 @@ -189,7 +189,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202448642.png) +![131.分割回文串](https://file.kamacoder.com/pics/20201118202448642.png) ## 子集问题 @@ -200,7 +200,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 如图: -![78.子集](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202544339.png) +![78.子集](https://file.kamacoder.com/pics/20201118202544339.png) 认清这个本质之后,今天的题目就是一道模板题了。 @@ -227,14 +227,14 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449.png) +![90.子集II](https://file.kamacoder.com/pics/2020111217110449.png) ### 递增子序列 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170832333.png) +![491. 递增子序列1](https://file.kamacoder.com/pics/20201112170832333.png) 很多同学都会把这道题目和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混在一起。 @@ -243,7 +243,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 我用没有排序的集合{2,1,2,2}来举个例子画一个图,如下: -![90.子集II2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111316440479.png) +![90.子集II2](https://file.kamacoder.com/pics/2020111316440479.png) **相信这个图胜过千言万语的解释了**。 @@ -259,7 +259,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 如图: -![46.全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112170304979.png) +![46.全排列](https://file.kamacoder.com/pics/20201112170304979.png) **大家此时可以感受出排列问题的不同:** @@ -272,7 +272,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112171930470.png) +![47.全排列II1](https://file.kamacoder.com/pics/20201112171930470.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -280,11 +280,11 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172230434.png) +![47.全排列II2.png](https://file.kamacoder.com/pics/20201112172230434.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201112172327967.png) +![47.全排列II3](https://file.kamacoder.com/pics/20201112172327967.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** @@ -318,7 +318,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111518065555.png) +![](https://file.kamacoder.com/pics/2020111518065555.png) 本题可以算是一道hard的题目了,关于本题的难点我在文中已经详细列出。 @@ -335,7 +335,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 下面我用一个3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118225433127.png) +![51.N皇后](https://file.kamacoder.com/pics/20201118225433127.png) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 @@ -363,7 +363,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790.png) +![37.解数独](https://file.kamacoder.com/pics/2020111720451790.png) 解数独可以说是非常难的题目了,如果还一直停留在一维递归的逻辑中,这道题目可以让大家瞬间崩溃。 @@ -438,7 +438,7 @@ N皇后问题分析: 回溯专题汇聚为一张图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211030124742.png) +![](https://file.kamacoder.com/pics/20211030124742.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[莫非毛](https://wx.zsxq.com/dweb2/index/footprint/828844212542),所画,总结的非常好,分享给大家。 diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index f1e1570a61..5c20f562b6 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -15,7 +15,7 @@ 我用没有排序的集合{2,1,2,2}来举例子画一个图,如图: -![90.子集II2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111316440479-20230310121930316.png) +![90.子集II2](https://file.kamacoder.com/pics/2020111316440479-20230310121930316.png) 图中,大家就很明显的看到,子集重复了。 @@ -95,7 +95,7 @@ private: 如图: -![90.子集II1](https://code-thinking-1253855093.file.myqcloud.com/pics/202011131625054.png) +![90.子集II1](https://file.kamacoder.com/pics/202011131625054.png) 可以看出一旦把unordered_set uset放在类成员位置,它控制的就是整棵树,包括树枝。 diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index 474fb8f749..d31e9651b8 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,7 +6,7 @@ ## 题目分类 -回溯算法大纲 +回溯算法大纲 ## 算法公开课 @@ -114,7 +114,7 @@ if (终止条件) { 如图: -![回溯算法理论基础](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130173631174.png) +![回溯算法理论基础](https://file.kamacoder.com/pics/20210130173631174.png) 注意图中,我特意举例集合大小和孩子的数量是相等的! diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index e45165a625..e29a7bd379 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -125,7 +125,7 @@ ## 总结 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/数组总结.png) +![](https://file.kamacoder.com/pics/数组总结.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。 diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index c1ac287d0f..4000208a9b 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -40,7 +40,7 @@ 那么二维数组直接上图,大家应该就知道怎么回事了 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240606105522.png) +![](https://file.kamacoder.com/pics/20240606105522.png) **那么二维数组在内存的空间地址是连续的么?** @@ -80,7 +80,7 @@ int main() { 如图: -![数组内存](https://code-thinking-1253855093.file.myqcloud.com/pics/20210310150641186.png) +![数组内存](https://file.kamacoder.com/pics/20210310150641186.png) **所以可以看出在C++中二维数组在地址空间上是连续的**。 @@ -111,7 +111,7 @@ public static void test_arr() { 所以Java的二维数组可能是如下排列的方式: -![算法通关数组3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201214111631844.png) +![算法通关数组3](https://file.kamacoder.com/pics/20201214111631844.png) 这里面试中数组相关的理论知识就介绍完了。 diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index bff0ec634b..912bfe1d70 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -11,7 +11,7 @@ 如图所示: -![栈与队列理论1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235346563.png) +![栈与队列理论1](https://file.kamacoder.com/pics/20210104235346563.png) 那么我这里再列出四个关于栈的问题,大家可以思考一下。以下是以C++为例,使用其他编程语言的同学也对应思考一下,自己使用的编程语言里栈和队列是什么样的。 @@ -46,7 +46,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 来说一说栈,栈先进后出,如图所示: -![栈与队列理论2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235434905.png) +![栈与队列理论2](https://file.kamacoder.com/pics/20210104235434905.png) 栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则,所以栈不提供走访功能,也不提供迭代器(iterator)。 不像是set 或者map 提供迭代器iterator来遍历所有元素。 @@ -59,7 +59,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 从下图中可以看出,栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。 -![栈与队列理论3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104235459376.png) +![栈与队列理论3](https://file.kamacoder.com/pics/20210104235459376.png) **我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。** diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index a2350835d6..162ee273e3 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -35,7 +35,7 @@ public: ``` 耗时如下: -![vectorinsert](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218203611181.png) +![vectorinsert](https://file.kamacoder.com/pics/20201218203611181.png) 其直观上来看数组的insert操作是O(n)的,整体代码的时间复杂度是O(n^2)。 @@ -68,7 +68,7 @@ public: 耗时如下: -![使用链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218200756257.png) +![使用链表](https://file.kamacoder.com/pics/20201218200756257.png) 大家都知道对于普通数组,一旦定义了大小就不能改变,例如int a[10];,这个数组a至多只能放10个元素,改不了的。 @@ -95,7 +95,7 @@ for (int i = 0; i < vec.size(); i++) { 就是重新申请一个二倍于原数组大小的数组,然后把数据都拷贝过去,并释放原数组内存。(对,就是这么原始粗暴的方法!) 举一个例子,如图: -![vector原理](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218185902217.png) +![vector原理](https://file.kamacoder.com/pics/20201218185902217.png) 原vector中的size和capicity相同都是3,初始化为1 2 3,此时要push_back一个元素4。 @@ -138,7 +138,7 @@ public: 耗时如下: -![vector手动模拟insert](https://code-thinking-1253855093.file.myqcloud.com/pics/20201218200626718.png) +![vector手动模拟insert](https://file.kamacoder.com/pics/20201218200626718.png) 这份代码就是不让vector动态扩容,全程我们自己模拟insert的操作,大家也可以直观的看出是一个O(n^2)的方法了。 diff --git "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" index 3c587b6d2e..9ce3fdda98 100644 --- "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" @@ -11,7 +11,7 @@ 关于这几种常见的背包,其关系如下: -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000726.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20230310000726.png) 通过这个图,可以很清晰分清这几种常见背包之间的关系。 @@ -93,7 +93,7 @@ 背包问题总结: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/背包问题1.jpeg) +![](https://file.kamacoder.com/pics/背包问题1.jpeg) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画结的非常好,分享给大家。 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index c2598ec99a..79751c89e9 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -21,7 +21,7 @@ 如果这几种背包,分不清,我这里画了一个图,如下: -![416.分割等和子集1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210117171307407.png) +![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407.png) 除此以外其他类型的背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 @@ -77,7 +77,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 如图,二维数组为 dp[i][j]。 -![动态规划-背包问题1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103003361.png) +![动态规划-背包问题1](https://file.kamacoder.com/pics/20210110103003361.png) 那么这里 i 、j、dp[i][j] 分别表示什么呢? @@ -91,7 +91,7 @@ i 来表示物品、j表示背包容量。 我们先看把物品0 放入背包的情况: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730113455.png) +![](https://file.kamacoder.com/pics/20240730113455.png) 背包容量为0,放不下物品0,此时背包里的价值为0。 @@ -103,7 +103,7 @@ i 来表示物品、j表示背包容量。 再看把物品1 放入背包: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730114228.png) +![](https://file.kamacoder.com/pics/20240730114228.png) 背包容量为 0,放不下物品0 或者物品1,此时背包里的价值为0。 @@ -150,7 +150,7 @@ i 来表示物品、j表示背包容量。 推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174246.png) +![](https://file.kamacoder.com/pics/20240730174246.png) 如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 @@ -158,7 +158,7 @@ i 来表示物品、j表示背包容量。 所以 放物品1 的情况 = dp[0][1] + 物品1 的价值,推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240730174436.png) +![](https://file.kamacoder.com/pics/20240730174436.png) 两种情况,分别是放物品1 和 不放物品1,我们要取最大值(毕竟求的是最大价值) @@ -178,7 +178,7 @@ i 来表示物品、j表示背包容量。 首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: -![动态规划-背包问题2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010304192.png) +![动态规划-背包问题2](https://file.kamacoder.com/pics/2021011010304192.png) 在看其他情况。 @@ -205,7 +205,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 此时dp数组初始化情况如图所示: -![动态规划-背包问题7](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103109140.png) +![动态规划-背包问题7](https://file.kamacoder.com/pics/20210110103109140.png) dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? @@ -217,7 +217,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 如图: -![动态规划-背包问题10](https://code-thinking-1253855093.file.myqcloud.com/pics/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%9810.jpg) +![动态规划-背包问题10](https://file.kamacoder.com/pics/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%9810.jpg) 最后初始化代码如下: @@ -236,7 +236,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 在如下图中,可以看出,有两个遍历的维度:物品与背包重量 -![动态规划-背包问题3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010314055.png) +![动态规划-背包问题3](https://file.kamacoder.com/pics/2021011010314055.png) 那么问题来了,**先遍历 物品还是先遍历背包重量呢?** @@ -277,11 +277,11 @@ for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括正上方向),那么先遍历物品,再遍历背包的过程如图所示: -![动态规划-背包问题5](https://code-thinking-1253855093.file.myqcloud.com/pics/202101101032124.png) +![动态规划-背包问题5](https://file.kamacoder.com/pics/202101101032124.png) 再来看看先遍历背包,再遍历物品呢,如图: -![动态规划-背包问题6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103244701.png) +![动态规划-背包问题6](https://file.kamacoder.com/pics/20210110103244701.png) **大家可以看出,虽然两个for循环遍历的次序不同,但是dp[i][j]所需要的数据就是左上角,根本不影响dp[i][j]公式的推导!** @@ -293,7 +293,7 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210118163425129.jpg) +![动态规划-背包问题4](https://file.kamacoder.com/pics/20210118163425129.jpg) 最终结果就是dp[2][4]。 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 6caa4f6327..b6a68960a9 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -163,7 +163,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110103614769.png) +![动态规划-背包问题9](https://file.kamacoder.com/pics/20210110103614769.png) 本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的,代码如下: diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" index 5a23b67c73..be7a5d54f9 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" @@ -54,11 +54,11 @@ for (int i = 1; i < n; i++) { // 遍历物品 遍历物品在外层循环,遍历背包容量在内层循环,状态如图: -![动态规划-完全背包1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210126104529605.jpg) +![动态规划-完全背包1](https://file.kamacoder.com/pics/20210126104529605.jpg) 遍历背包容量在外层循环,遍历物品在内层循环,状态如图: -![动态规划-完全背包2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210729234011.png) +![动态规划-完全背包2](https://file.kamacoder.com/pics/20210729234011.png) 看了这两个图,大家就会理解,完全背包中,两个for循环的先后循序,都不影响计算dp[j]所需要的值(这个值就是下标j之前所对应的dp[j])。 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index d76ff196b1..cb8db1e0e3 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -66,7 +66,7 @@ 推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126112952.png) +![](https://file.kamacoder.com/pics/20241126112952.png) 如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 @@ -78,7 +78,7 @@ 所以 放物品1 的情况 = dp[1][1] + 物品1 的价值,推导方向如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126113104.png) +![](https://file.kamacoder.com/pics/20241126113104.png) (**注意上图和 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的区别**,对于理解完全背包很重要) @@ -103,7 +103,7 @@ 首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: -![动态规划-背包问题2](https://code-thinking-1253855093.file.myqcloud.com/pics/2021011010304192.png) +![动态规划-背包问题2](https://file.kamacoder.com/pics/2021011010304192.png) 在看其他情况。 @@ -132,7 +132,7 @@ for (int j = weight[0]; j <= bagWeight; j++) 此时dp数组初始化情况如图所示: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241114161608.png) +![](https://file.kamacoder.com/pics/20241114161608.png) dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? @@ -185,7 +185,7 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 以本篇举例数据为例,填满了dp二维数组如图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20241126113752.png) +![](https://file.kamacoder.com/pics/20241126113752.png) 因为 物品0 的性价比是最高的,而且 在完全背包中,每一类物品都有无限个,所以有无限个物品0,既然物品0 性价比最高,当然是优先放物品0。 diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" index 7d4c57e877..4c67fb401a 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" @@ -128,7 +128,7 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 贪心专题汇聚为一张图: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/贪心总结water.png) +![](https://file.kamacoder.com/pics/贪心总结water.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412)所画,总结的非常好,分享给大家。 diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index 14f397296f..2d0af8791a 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -7,7 +7,7 @@ 题目分类大纲如下: -贪心算法大纲 +贪心算法大纲 ## 算法公开课 diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index e29ba268a3..99bb2abcd0 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -75,7 +75,7 @@ ## 总结 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/链表总结.png) +![](https://file.kamacoder.com/pics/链表总结.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。 diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 3637d05f5a..5305c9a9dc 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -12,7 +12,7 @@ 链表的入口节点称为链表的头结点也就是head。 如图所示: -![链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194529815.png) +![链表1](https://file.kamacoder.com/pics/20200806194529815.png) ## 链表的类型 @@ -31,7 +31,7 @@ 双链表 既可以向前查询也可以向后查询。 如图所示: -![链表2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194559317.png) +![链表2](https://file.kamacoder.com/pics/20200806194559317.png) ### 循环链表 @@ -39,7 +39,7 @@ 循环链表可以用来解决约瑟夫环问题。 -![链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194629603.png) +![链表4](https://file.kamacoder.com/pics/20200806194629603.png) ## 链表的存储方式 @@ -54,7 +54,7 @@ 如图所示: -![链表3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194613920.png) +![链表3](https://file.kamacoder.com/pics/20200806194613920.png) 这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。 @@ -104,7 +104,7 @@ head->val = 5; 删除D节点,如图所示: -![链表-删除节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195114541-20230310121459257.png) +![链表-删除节点](https://file.kamacoder.com/pics/20200806195114541-20230310121459257.png) 只要将C节点的next指针 指向E节点就可以了。 @@ -118,7 +118,7 @@ head->val = 5; 如图所示: -![链表-添加节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195134331-20230310121503147.png) +![链表-添加节点](https://file.kamacoder.com/pics/20200806195134331-20230310121503147.png) 可以看出链表的增添和删除都是O(1)操作,也不会影响到其他节点。 @@ -128,7 +128,7 @@ head->val = 5; 再把链表的特性和数组的特性进行一个对比,如图所示: -![链表-链表与数据性能对比](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806195200276.png) +![链表-链表与数据性能对比](https://file.kamacoder.com/pics/20200806195200276.png) 数组在定义的时候,长度就是固定的,如果想改动数组的长度,就需要重新定义一个新的数组。 diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index f8d9039a48..0207b71eac 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -13,7 +13,7 @@ 图示两个链表在节点 c1 开始相交: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) +![](https://file.kamacoder.com/pics/20211219221657.png) 题目数据 保证 整个链式结构中不存在环。 @@ -21,15 +21,15 @@ 示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) +![](https://file.kamacoder.com/pics/20211219221723.png) 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) +![](https://file.kamacoder.com/pics/20211219221749.png) 示例 3: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png) +![](https://file.kamacoder.com/pics/20211219221812.png) From 6d50f4735a4e6fc2f900f93eaaa52117dcf6e89c Mon Sep 17 00:00:00 2001 From: lylain Date: Tue, 18 Mar 2025 12:10:20 +0800 Subject: [PATCH 1519/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E6=B7=B1=E6=90=9Cgo=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\351\207\217\346\267\261\346\220\234.md" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 06be926874..eed21ecacc 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -326,6 +326,75 @@ if __name__ == '__main__': ### Go +我们使用一个visited数组,记录下那些位置被遍历过。分为两层遍历。初始化一个visited数组和原始的grid一样大,用来记录哪些陆地被遍历过 + +第一层遍历遍历整个grid数组的元素,遇到陆地,就在对应的visited数组里标记,并且执行深度搜索,深搜的逻辑是把当前位置的4个方向上的位置,全部判断一遍看是不是陆地,如果是,则在这个位置上再执行深搜,达到递归深搜的效果。所以深搜函数里是递归的。 + +```go +package main + +import ( + "fmt" +) + +func visitIsland(grid [][]int) int { + row := len(grid) + if row == 0 { + return 0 + } + visited := make([][]bool, row) + //go的这种初始化方式真的丑陋 + for i := 0; i < row; i++ { + visited[i] = make([]bool, len(grid[0])) + } + ans := 0 + for i := 0; i < row; i++ { + for j := 0; j < len(grid[0]); j++ { + if grid[i][j] == 1 && !visited[i][j] { + visited[i][j] = true + ans++ + visitGrid(grid, visited, i, j) + } + } + } + return ans +} + +func visitGrid(grid [][]int, visited [][]bool, x int, y int) { + diff := [4][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}} + for _, arr := range diff { + nextX := x + arr[0] + nextY := y + arr[1] + if nextX < 0 || nextX >= len(grid) || nextY < 0 || nextY >= len(grid[0]) { + continue + } + if !visited[nextX][nextY] && grid[nextX][nextY] == 1 { + visited[nextX][nextY] = true + visitGrid(grid, visited, nextX, nextY) + } + } +} + +func main() { + var row, col int + fmt.Scan(&row, &col) + if row <=0 || col <=0 { + return + } + grid := make([][]int, row) + for i := 0; i < row; i++ { + grid[i] = make([]int, col) + } + for i := 0; i < row; i++ { + for j := 0; j < col; j++ { + fmt.Scan(&grid[i][j]) + } + } + //这里必须要打印,不然报错会显示潜在的数组越界 + fmt.Println(visitIsland(grid)) +} +``` + ### Rust ### JavaScript From 1ff8e636f898a0a2fe22fb23cfb7f9496bb879e1 Mon Sep 17 00:00:00 2001 From: lylain Date: Sat, 22 Mar 2025 13:58:11 +0800 Subject: [PATCH 1520/1533] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BB=BA=E9=80=A0?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=B2=9B=E5=B1=BF=E4=BB=A3=E7=A0=81go?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\345\262\233\345\261\277.md" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 483d777275..dc5b12e945 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -528,6 +528,104 @@ if __name__ == "__main__": ### Go +```go +package main + +import "fmt" + +func main() { + var m, n int + fmt.Scan(&m, &n) + + grid := make([][]int, m) + for i := range grid { + grid[i] = make([]int, n) + for j := range grid[i] { + fmt.Scan(&grid[i][j]) + } + } + + sum := buildMaxIsland(grid) + fmt.Println(sum) +} + + +func buildMaxIsland(grid [][]int) int { + result := 0 + if grid == nil || len(grid) == 0 { + return result + } + hashMap := make(map[int]int) + islandArea := 0 + gridMark := 2 + for i := 0; i < len(grid); i++ { + for j := 0; j < len(grid[0]); j++ { + if grid[i][j] == 1 { + islandArea = 0 + visitGrid4(grid, i, j, &islandArea, gridMark) + hashMap[gridMark] = islandArea + if islandArea > result { + result = islandArea + } + gridMark++ + } + } + } + + for i := 0; i < len(grid); i++ { + for j := 0; j < len(grid[0]); j++ { + if grid[i][j] == 0 { + sum := findNearByIsland(grid, i, j, hashMap) + if sum > result { + result = sum + } + } + } + } + + return result +} + +func visitGrid4(grid [][]int, x int, y int, preValue *int, gridMark int) { + if x < 0 || y < 0 || x >= len(grid) || y >= len(grid[0]) { + return + } + //可以省略掉visited的原因是因为grid的每个位置如果被遍历过就会被标记为gridMark,能识别出来是不是被遍历过 + if grid[x][y] != 1 { + return + } + // visited[x][y] = true + grid[x][y] = gridMark + *preValue++ + visitGrid4(grid, x+1, y, preValue, gridMark) + visitGrid4(grid, x-1, y, preValue, gridMark) + visitGrid4(grid, x, y+1, preValue, gridMark) + visitGrid4(grid, x, y-1, preValue, gridMark) +} + +func findNearByIsland(grid [][]int, x, y int, hashMap map[int]int) int { + markSet := make(map[int]bool) + sum := 1 + coordinate := [][]int{{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}} + for _, arr := range coordinate { + if arr[0] < 0 || arr[1] < 0 || arr[0] >= len(grid) || arr[1] >= len(grid[0]) { + continue + } + if grid[arr[0]][arr[1]] == 0 { + continue + } + gridMark := grid[arr[0]][arr[1]] + if !markSet[gridMark] { + markSet[gridMark] = true + sum += hashMap[gridMark] + } + } + return sum +} +``` + + + ### Rust ### JavaScript From 7df04d99299c154b66c9680f9212e8ccf6b7fe61 Mon Sep 17 00:00:00 2001 From: lylain Date: Sun, 30 Mar 2025 09:58:39 +0800 Subject: [PATCH 1521/1533] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E6=8E=A5=E9=BE=99go=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\344\270\262\346\216\245\351\276\231.md" | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index af3436901c..c803a9220c 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -268,6 +268,82 @@ if __name__=='__main__': ### Go +```go +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func connectWord(words []string, startWord string, endWord string) int { + if words == nil || len(words) == 0 { + return 0 + } + var allWord = make(map[string]bool) + for _, word := range words { + allWord[word] = true + } + visitedWord := make(map[string]bool) + queue := make([]string, 0) + result := 1 + queue = append(queue, startWord) + queue = append(queue, "") + for len(queue) > 0 { + word := queue[0] + queue = queue[1:] + if word == "" { + if len(queue) > 0 { + result++ + queue = append(queue, "") + } + continue + } + arr := []rune(word) + for i := 0; i < len(arr); i++ { + old := arr[i] + for j := 'a'; j <= 'z'; j++ { + arr[i] = j + newWord := string(arr) + if allWord[newWord] && !visitedWord[newWord] { + visitedWord[newWord] = true + queue = append(queue, newWord) + if newWord == endWord { + return result + 1 + } + } + } + arr[i] = old + } + } + return 0 +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + n, _ := strconv.Atoi(scanner.Text()) + scanner.Scan() + input := strings.Split(scanner.Text(), " ") + beginStr := input[0] + endStr := input[1] + + wordList := []string{beginStr, endStr} + for i := 0; i < n; i++ { + scanner.Scan() + wordList = append(wordList, scanner.Text()) + } + count := connectWord(wordList, beginStr, endStr) + fmt.Println(count) +} + +``` + + + ### Rust ### JavaScript From ce6e658d14c5644c907fab6dfac58d88353a2ec3 Mon Sep 17 00:00:00 2001 From: kama Date: Mon, 19 May 2025 17:11:04 +0800 Subject: [PATCH 1522/1533] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 +- ...44\346\225\260\344\271\213\345\222\214.md" | 4 +- ...36\346\226\207\345\255\220\344\270\262.md" | 4 +- ...11\346\225\260\344\271\213\345\222\214.md" | 2 +- ...27\346\257\215\347\273\204\345\220\210.md" | 4 +- ...33\346\225\260\344\271\213\345\222\214.md" | 0 ...4N\344\270\252\350\212\202\347\202\271.md" | 10 +- ...10\347\232\204\346\213\254\345\217\267.md" | 8 +- ...55\347\232\204\350\212\202\347\202\271.md" | 10 +- ...73\351\231\244\345\205\203\347\264\240.md" | 4 +- .../0028.\345\256\236\347\216\260strStr.md" | 20 +- ...00\344\270\252\346\216\222\345\210\227.md" | 2 +- ...00\344\270\252\344\275\215\347\275\256.md" | 0 ...22\345\205\245\344\275\215\347\275\256.md" | 10 +- ...7.\350\247\243\346\225\260\347\213\254.md" | 8 +- ...04\345\220\210\346\200\273\345\222\214.md" | 8 +- ...\345\220\210\346\200\273\345\222\214II.md" | 6 +- ...2.\346\216\245\351\233\250\346\260\264.md" | 14 +- ...\350\267\203\346\270\270\346\210\217II.md" | 6 +- ...6.\345\205\250\346\216\222\345\210\227.md" | 6 +- ...\345\205\250\346\216\222\345\210\227II.md" | 6 +- "problems/0051.N\347\232\207\345\220\216.md" | 6 +- .../0052.N\347\232\207\345\220\216II.md" | 2 +- ...47\345\255\220\345\272\217\345\222\214.md" | 2 +- ...01\350\247\204\345\210\222\357\274\211.md" | 2 +- ...72\346\227\213\347\237\251\351\230\265.md" | 2 +- ...63\350\267\203\346\270\270\346\210\217.md" | 2 +- ...10\345\271\266\345\214\272\351\227\264.md" | 2 +- ...\346\227\213\347\237\251\351\230\265II.md" | 2 +- ...15\345\220\214\350\267\257\345\276\204.md" | 10 +- ...\345\220\214\350\267\257\345\276\204II.md" | 12 +- ...0.\347\210\254\346\245\274\346\242\257.md" | 2 +- ...14\345\214\205\347\211\210\346\234\254.md" | 0 ...26\350\276\221\350\267\235\347\246\273.md" | 4 +- "problems/0077.\347\273\204\345\220\210.md" | 10 +- ...04\345\220\210\344\274\230\345\214\226.md" | 2 +- "problems/0078.\345\255\220\351\233\206.md" | 4 +- ...47\347\232\204\347\237\251\345\275\242.md" | 10 +- "problems/0090.\345\255\220\351\233\206II.md" | 2 +- ...\345\216\237IP\345\234\260\345\235\200.md" | 4 +- ...11\346\220\234\347\264\242\346\240\221.md" | 10 +- ...11\346\220\234\347\264\242\346\240\221.md" | 4 +- ...70\345\220\214\347\232\204\346\240\221.md" | 4 +- ...60\344\272\214\345\217\211\346\240\221.md" | 6 +- ...02\345\272\217\351\201\215\345\216\206.md" | 22 +- ...00\345\244\247\346\267\261\345\272\246.md" | 6 +- ...40\344\272\214\345\217\211\346\240\221.md" | 8 +- ...11\346\220\234\347\264\242\346\240\221.md" | 6 +- ...41\344\272\214\345\217\211\346\240\221.md" | 6 +- ...00\345\260\217\346\267\261\345\272\246.md" | 6 +- ...57\345\276\204\346\200\273\345\222\214.md" | 8 +- ...04\345\255\220\345\272\217\345\210\227.md" | 8 +- ...02\347\202\271\346\214\207\351\222\210.md" | 4 +- ...00\344\275\263\346\227\266\346\234\272.md" | 2 +- ...\344\275\263\346\227\266\346\234\272II.md" | 2 +- ...01\350\247\204\345\210\222\357\274\211.md" | 0 ...344\275\263\346\227\266\346\234\272III.md" | 2 +- ...25\350\257\215\346\216\245\351\276\231.md" | 2 +- ...60\345\255\227\344\271\213\345\222\214.md" | 2 +- ...25\347\232\204\345\214\272\345\237\237.md" | 6 +- ...62\345\233\236\346\226\207\344\270\262.md" | 4 +- ...\345\233\236\346\226\207\344\270\262II.md" | 2 +- ...4.\345\212\240\346\262\271\347\253\231.md" | 4 +- ...06\345\217\221\347\263\226\346\236\234.md" | 6 +- ...25\350\257\215\346\213\206\345\210\206.md" | 4 +- ...57\345\275\242\351\223\276\350\241\250.md" | 6 +- ...\345\275\242\351\223\276\350\241\250II.md" | 16 +- ...15\346\216\222\351\223\276\350\241\250.md" | 4 +- ...76\345\274\217\346\261\202\345\200\274.md" | 2 +- ...14\347\232\204\345\215\225\350\257\215.md" | 0 ...70\344\272\244\351\223\276\350\241\250.md" | 0 ...\344\275\263\346\227\266\346\234\272IV.md" | 2 +- ...13\350\275\254\346\225\260\347\273\204.md" | 0 ...23\345\256\266\345\212\253\350\210\215.md" | 2 +- ...7.\345\271\277\346\220\234\347\211\210.md" | 6 +- ...7.\346\267\261\346\220\234\347\211\210.md" | 4 +- ...2.\345\277\253\344\271\220\346\225\260.md" | 0 ...76\350\241\250\345\205\203\347\264\240.md" | 12 +- ...04\345\255\227\347\254\246\344\270\262.md" | 0 ...73\350\275\254\351\223\276\350\241\250.md" | 4 +- ...04\345\255\220\346\225\260\347\273\204.md" | 4 +- ...\345\256\266\345\212\253\350\210\215II.md" | 6 +- ...345\220\210\346\200\273\345\222\214III.md" | 6 +- ...02\347\202\271\344\270\252\346\225\260.md" | 12 +- ...27\345\256\236\347\216\260\346\240\210.md" | 2 +- ...54\344\272\214\345\217\211\346\240\221.md" | 6 +- ...36\347\216\260\351\230\237\345\210\227.md" | 2 +- ...36\346\226\207\351\223\276\350\241\250.md" | 2 +- ...54\345\205\261\347\245\226\345\205\210.md" | 6 +- ...54\345\205\261\347\245\226\345\205\210.md" | 12 +- ...43\346\234\200\345\244\247\345\200\274.md" | 6 +- ...15\345\274\202\344\275\215\350\257\215.md" | 2 +- ...00\346\234\211\350\267\257\345\276\204.md" | 6 +- ...50\345\271\263\346\226\271\346\225\260.md" | 2 +- ...3.\347\247\273\345\212\250\351\233\266.md" | 2 +- ...07\345\255\220\345\272\217\345\210\227.md" | 2 +- ...53\345\206\267\345\206\273\346\234\237.md" | 4 +- ...66\351\222\261\345\205\221\346\215\242.md" | 2 +- ...11\346\216\222\350\241\214\347\250\213.md" | 6 +- ...345\256\266\345\212\253\350\210\215III.md" | 4 +- ...64\346\225\260\346\213\206\345\210\206.md" | 2 +- ...54\345\255\227\347\254\246\344\270\262.md" | 2 +- ...30\351\242\221\345\205\203\347\264\240.md" | 2 +- ...04\347\232\204\344\272\244\351\233\206.md" | 4 +- ...06\345\212\250\345\272\217\345\210\227.md" | 12 +- ...10\346\200\273\345\222\214\342\205\243.md" | 2 +- ...3.\350\265\216\351\207\221\344\277\241.md" | 0 ...55\345\255\220\345\272\217\345\210\227.md" | 6 +- ...66\345\255\220\344\271\213\345\222\214.md" | 6 +- ...15\345\273\272\351\230\237\345\210\227.md" | 2 +- ...11\345\222\214\345\255\220\351\233\206.md" | 2 +- ...64\346\265\201\351\227\256\351\242\230.md" | 6 +- ...15\345\217\240\345\214\272\351\227\264.md" | 2 +- ...55\347\232\204\350\212\202\347\202\271.md" | 4 +- ...25\347\210\206\346\260\224\347\220\203.md" | 2 +- ...\346\225\260\347\233\270\345\212\240II.md" | 0 ...06\345\217\221\351\245\274\345\271\262.md" | 4 +- ...20\345\255\227\347\254\246\344\270\262.md" | 32 +- ...77\347\232\204\345\221\250\351\225\277.md" | 6 +- ...4.\344\270\200\345\222\214\351\233\266.md" | 4 +- ...36\345\255\220\345\272\217\345\210\227.md" | 4 +- ...4.\347\233\256\346\240\207\345\222\214.md" | 30 +- ...4\345\244\247\345\205\203\347\264\240I.md" | 0 ...55\347\232\204\344\274\227\346\225\260.md" | 4 +- ...\345\244\247\345\205\203\347\264\240II.md" | 0 ...42\351\202\243\345\245\221\346\225\260.md" | 0 ...13\350\247\222\347\232\204\345\200\274.md" | 4 +- ...07\345\255\220\345\272\217\345\210\227.md" | 8 +- ...\351\222\261\345\205\221\346\215\242II.md" | 4 +- ...17\347\273\235\345\257\271\345\267\256.md" | 4 +- ...72\347\264\257\345\212\240\346\240\221.md" | 4 +- ...\345\255\227\347\254\246\344\270\262II.md" | 2 +- ...40\351\231\244\346\223\215\344\275\234.md" | 2 +- ...66\344\272\214\345\217\211\346\240\221.md" | 4 +- ...36\346\226\207\345\255\220\344\270\262.md" | 6 +- ...a2\345\217\202\350\256\256\351\231\242.md" | 0 ...47\344\272\214\345\217\211\346\240\221.md" | 4 +- ...24\345\233\236\345\216\237\347\202\271.md" | 2 +- ...11\346\220\234\347\264\242\346\240\221.md" | 10 +- ...27\347\232\204\344\270\252\346\225\260.md" | 2 +- ...22\345\242\236\345\272\217\345\210\227.md" | 2 +- ...27\344\275\231\350\277\236\346\216\245.md" | 6 +- ...\344\275\231\350\277\236\346\216\245II.md" | 8 +- ...00\345\244\247\351\235\242\347\247\257.md" | 4 +- ...55\347\232\204\346\220\234\347\264\242.md" | 4 +- ...22\345\205\245\346\223\215\344\275\234.md" | 4 +- ...14\345\210\206\346\237\245\346\211\276.md" | 4 +- ...76\350\256\241\351\223\276\350\241\250.md" | 6 +- ...53\346\211\213\347\273\255\350\264\271.md" | 0 ...01\350\247\204\345\210\222\357\274\211.md" | 0 ...15\345\255\220\346\225\260\347\273\204.md" | 4 +- ...55\345\277\203\347\264\242\345\274\225.md" | 0 ...36\347\232\204\346\225\260\345\255\227.md" | 0 ...17\346\227\245\346\270\251\345\272\246.md" | 24 +- ...66\350\277\237\346\227\266\351\227\264.md" | 46 +-- ...71\347\210\254\346\245\274\346\242\257.md" | 4 +- ...27\346\257\215\345\214\272\351\227\264.md" | 2 +- ...34\347\232\204\350\210\252\347\217\255.md" | 6 +- ...75\347\232\204\350\267\257\345\276\204.md" | 4 +- ...47\344\272\272\345\267\245\345\262\233.md" | 6 +- ...31\345\222\214\346\210\277\351\227\264.md" | 4 +- ...04\345\255\227\347\254\246\344\270\262.md" | 2 +- ...54\346\260\264\346\211\276\351\233\266.md" | 0 ...\345\272\217\346\225\260\347\273\204II.md" | 0 ...77\346\214\211\351\224\256\345\205\245.md" | 2 +- ...61\350\204\211\346\225\260\347\273\204.md" | 4 +- ...47\344\272\214\345\217\211\346\240\221.md" | 10 +- ...04\347\232\204\345\271\263\346\226\271.md" | 2 +- ...70\347\224\250\345\255\227\347\254\246.md" | 2 +- ...04\346\225\260\347\273\204\345\222\214.md" | 0 ...60\347\232\204\346\225\260\351\207\217.md" | 8 +- ...70\344\272\244\347\232\204\347\272\277.md" | 4 +- ...73\351\207\215\345\244\215\351\241\271.md" | 2 +- ...\347\232\204\351\207\215\351\207\217II.md" | 2 +- ...61\345\255\220\345\272\217\345\210\227.md" | 4 +- ...72\347\216\260\346\254\241\346\225\260.md" | 2 +- ...41\345\255\227\347\254\246\344\270\262.md" | 0 ...77\347\232\204\346\225\260\347\233\256.md" | 4 +- ...60\347\233\256\346\216\222\345\272\217.md" | 2 +- ...27\347\232\204\346\225\260\345\255\227.md" | 2 +- ...21\345\217\230\345\271\263\350\241\241.md" | 2 +- ...55\345\277\203\350\212\202\347\202\271.md" | 2 +- ...30\345\234\250\350\267\257\345\276\204.md" | 2 +- ...57\345\244\232\345\244\247\357\274\237.md" | 10 +- ...55\344\271\260\345\234\237\345\234\260.md" | 1 + ...17\202\344\274\232dijkstra\345\240\206.md" | 15 +- ...74\232dijkstra\346\234\264\347\264\240.md" | 41 +-- .../0053.\345\257\273\345\256\235-Kruskal.md" | 21 +- .../0053.\345\257\273\345\256\235-prim.md" | 23 +- ...77\346\215\242\346\225\260\345\255\227.md" | 5 +- ...13\345\255\227\347\254\246\344\270\262.md" | 9 +- ...8.\345\214\272\351\227\264\345\222\214.md" | 5 +- ...\211\251\350\277\220\350\276\223I-SPFA.md" | 21 +- ...7\347\211\251\350\277\220\350\276\223I.md" | 21 +- ...\347\211\251\350\277\220\350\276\223II.md" | 5 +- ...347\211\251\350\277\220\350\276\223III.md" | 41 +-- ...16\351\200\233\345\205\254\345\233\255.md" | 13 +- ...57\350\276\276\350\267\257\345\276\204.md" | 5 +- ...60\351\207\217\345\271\277\346\220\234.md" | 7 +- ...60\351\207\217\346\267\261\346\220\234.md" | 5 +- ...00\345\244\247\351\235\242\347\247\257.md" | 5 +- ...04\346\200\273\351\235\242\347\247\257.md" | 7 +- ...11\346\262\241\345\255\244\345\262\233.md" | 7 +- ...64\346\265\201\351\227\256\351\242\230.md" | 7 +- ...00\345\244\247\345\262\233\345\261\277.md" | 11 +- ...50\345\217\257\350\276\276\346\200\247.md" | 5 +- ...77\347\232\204\345\221\250\351\225\277.md" | 9 +- ...50\347\232\204\350\267\257\345\276\204.md" | 3 +- ...27\344\275\231\350\277\236\346\216\245.md" | 13 +- ...\344\275\231\350\277\236\346\216\245II.md" | 13 +- ...46\344\270\262\346\216\245\351\276\231.md" | 5 +- ...57\344\273\266\346\236\204\345\273\272.md" | 23 +- ...7\232\204\346\224\273\345\207\273astar.md" | 9 +- ...347\224\250ACM\346\250\241\345\274\217.md" | 1 + ...06\350\256\272\345\237\272\347\241\200.md" | 25 +- ...06\350\256\272\345\237\272\347\241\200.md" | 7 +- ...72\346\200\273\347\273\223\347\257\207.md" | 1 + ...06\350\256\272\345\237\272\347\241\200.md" | 15 +- ...06\350\256\272\345\237\272\347\241\200.md" | 31 +- ...30\346\200\273\347\273\223\347\257\207.md" | 3 +- problems/qita/acm.md | 20 +- problems/qita/acm_backup.md | 89 +++++ problems/qita/algo_pdf.md | 70 ++++ problems/qita/ewaishuoming.md | 16 + problems/qita/gongkaike.md | 162 +++++++++ problems/qita/join.md | 48 +-- problems/qita/language.md | 22 ++ problems/qita/publish.md | 200 +++++++++++ problems/qita/say_feel.md | 14 + problems/qita/server.md | 6 +- problems/qita/shejimoshi.md | 14 +- problems/qita/tulunfabu.md | 34 +- problems/qita/tulunshuoming.md | 44 +++ problems/qita/update.md | 56 ++++ ...11\346\255\245\351\223\272\345\236\253.md" | 0 ...46\347\235\200\345\233\236\346\272\257.md" | 0 ...21\346\200\273\347\273\223\347\257\207.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 16 +- ...00\350\277\255\344\273\243\346\263\225.md" | 2 +- ...55\344\273\243\351\201\215\345\216\206.md" | 6 +- ...22\345\275\222\351\201\215\345\216\206.md" | 0 .../ACM\346\250\241\345\274\217.md" | 18 +- ...72\344\272\214\345\217\211\346\240\221.md" | 18 +- ...50\350\277\231\351\207\214\344\272\206.md" | 2 +- .../\345\211\215\345\272\217/gitserver.md" | 312 ++++++++++++++++++ .../\345\211\215\345\272\217/kvstore.md" | 124 +++++++ "problems/\345\211\215\345\272\217/server.md" | 129 ++++++++ "problems/\345\211\215\345\272\217/vim.md" | 4 +- ...54\345\217\270\346\200\273\347\273\223.md" | 125 ------- ...24\345\217\221\346\265\201\347\250\213.md" | 240 -------------- ...43\347\240\201\351\243\216\346\240\274.md" | 4 +- ...05\345\255\230\346\266\210\350\200\227.md" | 10 +- ...26\350\257\221\350\277\220\350\241\214.md" | 2 +- ...54\345\217\270\346\200\273\347\273\223.md" | 109 ------ ...54\345\217\270\346\200\273\347\273\223.md" | 77 ----- ...54\345\217\270\346\200\273\347\273\223.md" | 72 ---- ...64\345\244\215\346\235\202\345\272\246.md" | 8 +- ...54\345\217\270\346\200\273\347\273\223.md" | 83 ----- ...54\345\217\270\346\200\273\347\273\223.md" | 79 ----- ...07\346\241\243\345\267\245\345\205\267.md" | 2 +- ...17\345\221\230\347\256\200\345\216\206.md" | 6 +- ...64\345\244\215\346\235\202\345\272\246.md" | 2 +- ...27\346\263\225\350\266\205\346\227\266.md" | 12 +- ...02\345\272\246\345\210\206\346\236\220.md" | 8 +- ...64\345\244\215\346\235\202\345\272\246.md" | 6 +- ...77\346\215\242\347\251\272\346\240\274.md" | 4 +- ...54\345\255\227\347\254\246\344\270\262.md" | 8 +- ...30\346\200\273\347\273\223\347\257\207.md" | 2 +- ...22\346\200\273\347\273\223\347\257\207.md" | 6 +- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- ...07\351\222\210\346\200\273\347\273\223.md" | 0 ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 4 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 12 +- ...50\346\234\253\346\200\273\347\273\223.md" | 14 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 12 +- ...50\346\234\253\346\200\273\347\273\223.md" | 4 +- ...50\346\234\253\346\200\273\347\273\223.md" | 6 +- ...50\346\234\253\346\200\273\347\273\223.md" | 10 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 14 +- ...50\346\234\253\346\200\273\347\273\223.md" | 8 +- ...50\346\234\253\346\200\273\347\273\223.md" | 6 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 +- ...50\346\234\253\346\200\273\347\273\223.md" | 12 +- ...50\346\234\253\346\200\273\347\273\223.md" | 8 +- ...23\347\263\273\345\210\227\344\270\200.md" | 2 +- ...14\350\241\250\346\200\273\347\273\223.md" | 0 ...06\350\256\272\345\237\272\347\241\200.md" | 12 +- ...36\346\272\257\346\200\273\347\273\223.md" | 42 +-- ...00\347\247\215\345\206\231\346\263\225.md" | 4 +- ...06\350\256\272\345\237\272\347\241\200.md" | 4 +- ...46\344\270\262\346\200\273\347\273\223.md" | 0 ...04\346\200\273\347\273\223\347\257\207.md" | 10 +- ...06\350\256\272\345\237\272\347\241\200.md" | 10 +- ...37\345\210\227\346\200\273\347\273\223.md" | 0 ...06\350\256\272\345\237\272\347\241\200.md" | 6 +- ...06\350\256\262\350\247\243\357\274\211.md" | 8 +- ...27\346\263\225\346\250\241\346\235\277.md" | 0 ...05\346\200\273\347\273\223\347\257\207.md" | 4 +- ...47\241\20001\350\203\214\345\214\205-1.md" | 26 +- ...47\241\20001\350\203\214\345\214\205-2.md" | 2 +- ...14\345\214\205\344\270\200\347\273\264.md" | 4 +- ...32\351\207\215\350\203\214\345\214\205.md" | 0 ...14\345\205\250\350\203\214\345\214\205.md" | 10 +- ...25\346\200\273\347\273\223\347\257\207.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- ...02\345\272\246\345\210\206\346\236\220.md" | 281 ++++++++++++++++ ...50\346\200\273\347\273\223\347\257\207.md" | 2 +- ...06\350\256\272\345\237\272\347\241\200.md" | 14 +- ...76\350\241\250\347\233\270\344\272\244.md" | 12 +- 315 files changed, 2499 insertions(+), 1729 deletions(-) mode change 100644 => 100755 "problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" mode change 100644 => 100755 "problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" mode change 100644 => 100755 "problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" mode change 100644 => 100755 "problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" mode change 100644 => 100755 "problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" mode change 100644 => 100755 "problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" mode change 100644 => 100755 "problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" mode change 100644 => 100755 "problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" mode change 100644 => 100755 "problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" mode change 100644 => 100755 "problems/0028.\345\256\236\347\216\260strStr.md" mode change 100644 => 100755 "problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" mode change 100644 => 100755 "problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" mode change 100644 => 100755 "problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" mode change 100644 => 100755 "problems/0037.\350\247\243\346\225\260\347\213\254.md" mode change 100644 => 100755 "problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" mode change 100644 => 100755 "problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" mode change 100644 => 100755 "problems/0042.\346\216\245\351\233\250\346\260\264.md" mode change 100644 => 100755 "problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" mode change 100644 => 100755 "problems/0046.\345\205\250\346\216\222\345\210\227.md" mode change 100644 => 100755 "problems/0047.\345\205\250\346\216\222\345\210\227II.md" mode change 100644 => 100755 "problems/0051.N\347\232\207\345\220\216.md" mode change 100644 => 100755 "problems/0052.N\347\232\207\345\220\216II.md" mode change 100644 => 100755 "problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" mode change 100644 => 100755 "problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" mode change 100644 => 100755 "problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" mode change 100644 => 100755 "problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" mode change 100644 => 100755 "problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" mode change 100644 => 100755 "problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" mode change 100644 => 100755 "problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" mode change 100644 => 100755 "problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" mode change 100644 => 100755 "problems/0070.\347\210\254\346\245\274\346\242\257.md" mode change 100644 => 100755 "problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" mode change 100644 => 100755 "problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" mode change 100644 => 100755 "problems/0077.\347\273\204\345\220\210.md" mode change 100644 => 100755 "problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" mode change 100644 => 100755 "problems/0078.\345\255\220\351\233\206.md" mode change 100644 => 100755 "problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" mode change 100644 => 100755 "problems/0090.\345\255\220\351\233\206II.md" mode change 100644 => 100755 "problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" mode change 100644 => 100755 "problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" mode change 100644 => 100755 "problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" mode change 100644 => 100755 "problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" mode change 100644 => 100755 "problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" mode change 100644 => 100755 "problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" mode change 100644 => 100755 "problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" mode change 100644 => 100755 "problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" mode change 100644 => 100755 "problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" mode change 100644 => 100755 "problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" mode change 100644 => 100755 "problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" mode change 100644 => 100755 "problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" mode change 100644 => 100755 "problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" mode change 100644 => 100755 "problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" mode change 100644 => 100755 "problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" mode change 100644 => 100755 "problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" mode change 100644 => 100755 "problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" mode change 100644 => 100755 "problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" mode change 100644 => 100755 "problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" mode change 100644 => 100755 "problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" mode change 100644 => 100755 "problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" mode change 100644 => 100755 "problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" mode change 100644 => 100755 "problems/0134.\345\212\240\346\262\271\347\253\231.md" mode change 100644 => 100755 "problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" mode change 100644 => 100755 "problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" mode change 100644 => 100755 "problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" mode change 100644 => 100755 "problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" mode change 100644 => 100755 "problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" mode change 100644 => 100755 "problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" mode change 100644 => 100755 "problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" mode change 100644 => 100755 "problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" mode change 100644 => 100755 "problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" mode change 100644 => 100755 "problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" mode change 100644 => 100755 "problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" mode change 100644 => 100755 "problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" mode change 100644 => 100755 "problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" mode change 100644 => 100755 "problems/0202.\345\277\253\344\271\220\346\225\260.md" mode change 100644 => 100755 "problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" mode change 100644 => 100755 "problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" mode change 100644 => 100755 "problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" mode change 100644 => 100755 "problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" mode change 100644 => 100755 "problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" mode change 100644 => 100755 "problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" mode change 100644 => 100755 "problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" mode change 100644 => 100755 "problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" mode change 100644 => 100755 "problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" mode change 100644 => 100755 "problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" mode change 100644 => 100755 "problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" mode change 100644 => 100755 "problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" mode change 100644 => 100755 "problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" mode change 100644 => 100755 "problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" mode change 100644 => 100755 "problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" mode change 100644 => 100755 "problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" mode change 100644 => 100755 "problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" mode change 100644 => 100755 "problems/0283.\347\247\273\345\212\250\351\233\266.md" mode change 100644 => 100755 "problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" mode change 100644 => 100755 "problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" mode change 100644 => 100755 "problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" mode change 100644 => 100755 "problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" mode change 100644 => 100755 "problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" mode change 100644 => 100755 "problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" mode change 100644 => 100755 "problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" mode change 100644 => 100755 "problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" mode change 100644 => 100755 "problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" mode change 100644 => 100755 "problems/0383.\350\265\216\351\207\221\344\277\241.md" mode change 100644 => 100755 "problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" mode change 100644 => 100755 "problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" mode change 100644 => 100755 "problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" mode change 100644 => 100755 "problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" mode change 100644 => 100755 "problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" mode change 100644 => 100755 "problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" mode change 100644 => 100755 "problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" mode change 100644 => 100755 "problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" mode change 100644 => 100755 "problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" mode change 100644 => 100755 "problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" mode change 100644 => 100755 "problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" mode change 100644 => 100755 "problems/0474.\344\270\200\345\222\214\351\233\266.md" mode change 100644 => 100755 "problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/0494.\347\233\256\346\240\207\345\222\214.md" mode change 100644 => 100755 "problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" mode change 100644 => 100755 "problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" mode change 100644 => 100755 "problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" mode change 100644 => 100755 "problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" mode change 100644 => 100755 "problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" mode change 100644 => 100755 "problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" mode change 100644 => 100755 "problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" mode change 100644 => 100755 "problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" mode change 100644 => 100755 "problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" mode change 100644 => 100755 "problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" mode change 100644 => 100755 "problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" mode change 100644 => 100755 "problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" mode change 100644 => 100755 "problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" mode change 100644 => 100755 "problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" mode change 100644 => 100755 "problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" mode change 100644 => 100755 "problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" mode change 100644 => 100755 "problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" mode change 100644 => 100755 "problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" mode change 100644 => 100755 "problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" mode change 100644 => 100755 "problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" mode change 100644 => 100755 "problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" mode change 100644 => 100755 "problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" mode change 100644 => 100755 "problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" mode change 100644 => 100755 "problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" mode change 100644 => 100755 "problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" mode change 100644 => 100755 "problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" mode change 100644 => 100755 "problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" mode change 100644 => 100755 "problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" mode change 100644 => 100755 "problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" mode change 100644 => 100755 "problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" mode change 100644 => 100755 "problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" mode change 100644 => 100755 "problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" mode change 100644 => 100755 "problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" mode change 100644 => 100755 "problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" mode change 100644 => 100755 "problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" mode change 100644 => 100755 "problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" mode change 100644 => 100755 "problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" mode change 100644 => 100755 "problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" mode change 100644 => 100755 "problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" mode change 100644 => 100755 "problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" mode change 100644 => 100755 "problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" mode change 100644 => 100755 "problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" mode change 100644 => 100755 "problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" mode change 100644 => 100755 "problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" mode change 100644 => 100755 "problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" mode change 100644 => 100755 "problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" mode change 100644 => 100755 "problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" mode change 100644 => 100755 "problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" mode change 100644 => 100755 "problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" mode change 100644 => 100755 "problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" mode change 100644 => 100755 "problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" mode change 100644 => 100755 "problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" mode change 100644 => 100755 "problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" mode change 100644 => 100755 "problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" mode change 100644 => 100755 "problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" mode change 100644 => 100755 "problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" mode change 100644 => 100755 "problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" create mode 100755 problems/qita/acm_backup.md create mode 100755 problems/qita/algo_pdf.md create mode 100755 problems/qita/ewaishuoming.md create mode 100755 problems/qita/gongkaike.md create mode 100755 problems/qita/language.md create mode 100755 problems/qita/publish.md create mode 100755 problems/qita/say_feel.md create mode 100755 problems/qita/tulunshuoming.md create mode 100755 problems/qita/update.md mode change 100644 => 100755 "problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" mode change 100644 => 100755 "problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" mode change 100644 => 100755 "problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" mode change 100644 => 100755 "problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" mode change 100644 => 100755 "problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" mode change 100644 => 100755 "problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" mode change 100644 => 100755 "problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" create mode 100755 "problems/\345\211\215\345\272\217/gitserver.md" create mode 100755 "problems/\345\211\215\345\272\217/kvstore.md" create mode 100755 "problems/\345\211\215\345\272\217/server.md" delete mode 100644 "problems/\345\211\215\345\272\217/\344\270\212\346\265\267\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" delete mode 100644 "problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" delete mode 100644 "problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" delete mode 100644 "problems/\345\211\215\345\272\217/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" delete mode 100644 "problems/\345\211\215\345\272\217/\346\210\220\351\203\275\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" delete mode 100644 "problems/\345\211\215\345\272\217/\346\235\255\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" delete mode 100644 "problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" mode change 100644 => 100755 "problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" mode change 100644 => 100755 "problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" mode change 100644 => 100755 "problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" mode change 100644 => 100755 "problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" mode change 100644 => 100755 "problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" mode change 100644 => 100755 "problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" mode change 100644 => 100755 "problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" mode change 100644 => 100755 "problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" mode change 100644 => 100755 "problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" mode change 100644 => 100755 "problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" mode change 100644 => 100755 "problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" mode change 100644 => 100755 "problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" mode change 100644 => 100755 "problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" mode change 100644 => 100755 "problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" mode change 100644 => 100755 "problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" mode change 100644 => 100755 "problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" mode change 100644 => 100755 "problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" mode change 100644 => 100755 "problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" mode change 100644 => 100755 "problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" mode change 100644 => 100755 "problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" mode change 100644 => 100755 "problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" mode change 100644 => 100755 "problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" mode change 100644 => 100755 "problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" mode change 100644 => 100755 "problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" mode change 100644 => 100755 "problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" create mode 100755 "problems/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" mode change 100644 => 100755 "problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" mode change 100644 => 100755 "problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" mode change 100644 => 100755 "problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" diff --git a/README.md b/README.md index 06de2f5d0e..993d7c6df8 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ 题目分类大纲如下: -二叉树大纲 +二叉树大纲 1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md) 2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md) @@ -222,7 +222,7 @@ 题目分类大纲如下: -回溯算法大纲 +回溯算法大纲 1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md) 2. [回溯算法:77.组合](./problems/0077.组合.md) @@ -252,7 +252,7 @@ 题目分类大纲如下: -贪心算法大纲 +贪心算法大纲 1. [关于贪心算法,你该了解这些!](./problems/贪心算法理论基础.md) 2. [贪心算法:455.分发饼干](./problems/0455.分发饼干.md) @@ -283,7 +283,7 @@ 动态规划专题已经开始啦,来不及解释了,小伙伴们上车别掉队! - + 1. [关于动态规划,你该了解这些!](./problems/动态规划理论基础.md) 2. [动态规划:509.斐波那契数](./problems/0509.斐波那契数.md) 3. [动态规划:70.爬楼梯](./problems/0070.爬楼梯.md) @@ -297,7 +297,7 @@ 背包问题系列: -背包问题大纲 +背包问题大纲 11. [动态规划:01背包理论基础(二维dp数组)](./problems/背包理论基础01背包-1.md) @@ -328,7 +328,7 @@ 股票系列: -股票问题总结 +股票问题总结 32. [动态规划:121.买卖股票的最佳时机](./problems/0121.买卖股票的最佳时机.md) @@ -343,7 +343,7 @@ 子序列系列: - + 41. [动态规划:300.最长递增子序列](./problems/0300.最长上升子序列.md) @@ -503,5 +503,5 @@ 添加微信记得备注,如果是已工作,备注:姓名-城市-岗位。如果学生,备注:姓名-学校-年级。**备注没有自我介绍不通过哦** -
+
diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" old mode 100644 new mode 100755 index f9bea8289e..a11527961d --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -83,10 +83,10 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候, 过程如下: -![过程一](https://file.kamacoder.com/pics/20220711202638.png) +![过程一](https://file1.kamacoder.com/i/algo/20220711202638.png) -![过程二](https://file.kamacoder.com/pics/20230220223536.png) +![过程二](https://file1.kamacoder.com/i/algo/20230220223536.png) C++代码: diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" old mode 100644 new mode 100755 index 1e0667e575..05dd610a72 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -106,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://file.kamacoder.com/pics/20210121171032473.jpg) +![647.回文子串](https://file1.kamacoder.com/i/algo/20210121171032473.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -140,7 +140,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://file.kamacoder.com/pics/20210121171059951.jpg) +![647.回文子串1](https://file1.kamacoder.com/i/algo/20210121171059951.jpg) **注意因为dp[i][j]的定义,所以j一定是大于等于i的,那么在填充dp[i][j]的时候一定是只填充右上半部分**。 diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" old mode 100644 new mode 100755 index 52dbdab7b5..e2cb3f4612 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -100,7 +100,7 @@ public: 动画效果如下: -![15.三数之和](https://code-thinking.cdn.bcebos.com/gifs/15.%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.gif) +![15.三数之和](https://file1.kamacoder.com/i/algo/15.%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.gif) 拿这个nums数组来举例,首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。 diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" old mode 100644 new mode 100755 index a35fd4e2bc..6dcf9ee690 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -11,7 +11,7 @@ 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 -![17.电话号码的字母组合](https://file.kamacoder.com/pics/2020102916424043.png) +![17.电话号码的字母组合](https://file1.kamacoder.com/i/algo/2020102916424043.png) 示例: * 输入:"23" @@ -64,7 +64,7 @@ const string letterMap[10] = { 例如:输入:"23",抽象为树形结构,如图所示: -![17. 电话号码的字母组合](https://file.kamacoder.com/pics/20201123200304469.png) +![17. 电话号码的字母组合](https://file1.kamacoder.com/i/algo/20201123200304469.png) 图中可以看出遍历的深度,就是输入"23"的长度,而叶子节点就是我们要收集的结果,输出["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。 diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" old mode 100644 new mode 100755 diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" old mode 100644 new mode 100755 index 9b2ba88ef8..08f602c1c1 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -16,7 +16,7 @@ 示例 1: -![19.删除链表的倒数第N个节点](https://file.kamacoder.com/pics/20210510085957392.png) +![19.删除链表的倒数第N个节点](https://file1.kamacoder.com/i/algo/20210510085957392.png) 输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] @@ -49,16 +49,16 @@ * 定义fast指针和slow指针,初始值为虚拟头结点,如图: - + * fast首先走n + 1步 ,为什么是n+1呢,因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作),如图: - + * fast和slow同时移动,直到fast指向末尾,如题: - + //图片中有错别词:应该将“只到”改为“直到” * 删除slow指向的下一个节点,如图: - + 此时不难写出如下C++代码: diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" old mode 100644 new mode 100755 index 7282471285..09cf997839 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -81,13 +81,13 @@ cd a/b/c/../../ 1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。 -![括号匹配1](https://file.kamacoder.com/pics/2020080915505387.png) +![括号匹配1](https://file1.kamacoder.com/i/algo/2020080915505387.png) 2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。 -![括号匹配2](https://file.kamacoder.com/pics/20200809155107397.png) +![括号匹配2](https://file1.kamacoder.com/i/algo/20200809155107397.png) 3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。 -![括号匹配3](https://file.kamacoder.com/pics/20200809155115779.png) +![括号匹配3](https://file1.kamacoder.com/i/algo/20200809155115779.png) @@ -95,7 +95,7 @@ cd a/b/c/../../ 动画如下: -![20.有效括号](https://code-thinking.cdn.bcebos.com/gifs/20.有效括号.gif) +![20.有效括号](https://file1.kamacoder.com/i/algo/20.有效括号.gif) 第一种情况:已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" old mode 100644 new mode 100755 index b9494297e4..14d2538f45 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -12,7 +12,7 @@ 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 -24.两两交换链表中的节点-题意 +24.两两交换链表中的节点-题意 ## 算法公开课 @@ -31,16 +31,16 @@ 初始时,cur指向虚拟头结点,然后进行如下三步: -![24.两两交换链表中的节点1](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B91.png) +![24.两两交换链表中的节点1](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B91.png) 操作之后,链表如下: -![24.两两交换链表中的节点2](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B92.png) +![24.两两交换链表中的节点2](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B92.png) 看这个可能就更直观一些了: -![24.两两交换链表中的节点3](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B93.png) +![24.两两交换链表中的节点3](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B93.png) 对应的C++代码实现如下: (注释中详细和如上图中的三步做对应) @@ -81,7 +81,7 @@ public: 心想应该没有更好的方法了吧,也就 $O(n)$ 的时间复杂度,重复提交几次,这样了: -![24.两两交换链表中的节点](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.png) +![24.两两交换链表中的节点](https://file1.kamacoder.com/i/algo/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.png) 力扣上的统计如果两份代码是 100ms 和 300ms的耗时,其实是需要注意的。 diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" old mode 100644 new mode 100755 index d01765ff66..47e05eec6a --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -43,7 +43,7 @@ 删除过程如下: -![27.移除元素-暴力解法](https://code-thinking.cdn.bcebos.com/gifs/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E6%9A%B4%E5%8A%9B%E8%A7%A3%E6%B3%95.gif) +![27.移除元素-暴力解法](https://file1.kamacoder.com/i/algo/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E6%9A%B4%E5%8A%9B%E8%A7%A3%E6%B3%95.gif) 很明显暴力解法的时间复杂度是O(n^2),这道题目暴力解法在leetcode上是可以过的。 @@ -87,7 +87,7 @@ public: 删除过程如下: -![27.移除元素-双指针法](https://code-thinking.cdn.bcebos.com/gifs/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E5%8F%8C%E6%8C%87%E9%92%88%E6%B3%95.gif) +![27.移除元素-双指针法](https://file1.kamacoder.com/i/algo/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E5%8F%8C%E6%8C%87%E9%92%88%E6%B3%95.gif) 很多同学不了解 diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" old mode 100644 new mode 100755 index b25cb301f9..ef8a6c58e6 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -106,7 +106,7 @@ next数组就是一个前缀表(prefix table)。 如动画所示: -![KMP详解1](https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B21.gif) +![KMP详解1](https://file1.kamacoder.com/i/algo/KMP%E7%B2%BE%E8%AE%B21.gif) 动画里,我特意把 子串`aa` 标记上了,这是有原因的,大家先注意一下,后面还会说到。 @@ -147,11 +147,11 @@ next数组就是一个前缀表(prefix table)。 这就是前缀表,那为啥就能告诉我们 上次匹配的位置,并跳过去呢? 回顾一下,刚刚匹配的过程在下标5的地方遇到不匹配,模式串是指向f,如图: -KMP精讲1 +KMP精讲1 然后就找到了下标2,指向b,继续匹配:如图: -KMP精讲2 +KMP精讲2 以下这句话,对于理解为什么使用前缀表可以告诉我们匹配失败之后跳到哪里重新匹配 非常重要! @@ -167,15 +167,15 @@ next数组就是一个前缀表(prefix table)。 如图: -KMP精讲5 +KMP精讲5 长度为前1个字符的子串`a`,最长相同前后缀的长度为0。(注意字符串的**前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串**;**后缀是指不包含第一个字符的所有以最后一个字符结尾的连续子串**。) -KMP精讲6 +KMP精讲6 长度为前2个字符的子串`aa`,最长相同前后缀的长度为1。 -KMP精讲7 +KMP精讲7 长度为前3个字符的子串`aab`,最长相同前后缀的长度为0。 @@ -185,13 +185,13 @@ next数组就是一个前缀表(prefix table)。 长度为前6个字符的子串`aabaaf`,最长相同前后缀的长度为0。 那么把求得的最长相同前后缀的长度就是对应前缀表的元素,如图: -KMP精讲8 +KMP精讲8 可以看出模式串与前缀表对应位置的数字表示的就是:**下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。** 再来看一下如何利用 前缀表找到 当字符不匹配的时候应该指针应该移动的位置。如动画所示: -![KMP精讲2](https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B22.gif) +![KMP精讲2](https://file1.kamacoder.com/i/algo/KMP%E7%B2%BE%E8%AE%B22.gif) 找到的不匹配的位置, 那么此时我们要看它的前一个字符的前缀表的数值是多少。 @@ -225,7 +225,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减 匹配过程动画如下: -![KMP精讲4](https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B24.gif) +![KMP精讲4](https://file1.kamacoder.com/i/algo/KMP%E7%B2%BE%E8%AE%B24.gif) ### 时间复杂度分析 @@ -332,7 +332,7 @@ void getNext(int* next, const string& s){ 代码构造next数组的逻辑流程动画如下: -![KMP精讲3](https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B23.gif) +![KMP精讲3](https://file1.kamacoder.com/i/algo/KMP%E7%B2%BE%E8%AE%B23.gif) 得到了next数组之后,就要用这个来做匹配了。 diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" old mode 100644 new mode 100755 index 95bb1d899e..4bbf20fbb8 --- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" +++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" @@ -67,7 +67,7 @@ 以求1243为例,流程如图: - + 对应的C++代码如下: diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" old mode 100644 new mode 100755 diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" old mode 100644 new mode 100755 index c9826fa205..b48910eef7 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -41,7 +41,7 @@ 这道题目,要在数组中插入目标值,无非是这四种情况。 -![35_搜索插入位置3](https://file.kamacoder.com/pics/20201216232148471.png) +![35_搜索插入位置3](https://file1.kamacoder.com/i/algo/20201216232148471.png) * 目标值在数组所有元素之前 * 目标值等于数组中某一个元素 @@ -82,14 +82,14 @@ public: 效率如下: -![35_搜索插入位置](https://file.kamacoder.com/pics/20201216232127268.png) +![35_搜索插入位置](https://file1.kamacoder.com/i/algo/20201216232127268.png) ### 二分法 既然暴力解法的时间复杂度是O(n),就要尝试一下使用二分查找法。 -![35_搜索插入位置4](https://file.kamacoder.com/pics/202012162326354.png) +![35_搜索插入位置4](https://file1.kamacoder.com/i/algo/202012162326354.png) 大家注意这道题目的前提是数组是有序数组,这也是使用二分查找的基础条件。 @@ -99,7 +99,7 @@ public: 大体讲解一下二分法的思路,这里来举一个例子,例如在这个数组中,使用二分法寻找元素为5的位置,并返回其下标。 -![35_搜索插入位置5](https://file.kamacoder.com/pics/20201216232659199.png) +![35_搜索插入位置5](https://file1.kamacoder.com/i/algo/20201216232659199.png) 二分查找涉及的很多的边界条件,逻辑比较简单,就是写不好。 @@ -150,7 +150,7 @@ public: * 空间复杂度:O(1) 效率如下: -![35_搜索插入位置2](https://file.kamacoder.com/pics/2020121623272877.png) +![35_搜索插入位置2](https://file1.kamacoder.com/i/algo/2020121623272877.png) ### 二分法第二种写法 diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" old mode 100644 new mode 100755 index 5d2adb4d9d..204f0cc092 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -18,11 +18,11 @@ 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 空白格用 '.' 表示。 -![解数独](https://file.kamacoder.com/pics/202011171912586.png) +![解数独](https://file1.kamacoder.com/i/algo/202011171912586.png) 一个数独。 -![解数独](https://file.kamacoder.com/pics/20201117191340669.png) +![解数独](https://file1.kamacoder.com/i/algo/20201117191340669.png) 答案被标成红色。 @@ -52,7 +52,7 @@ 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131816104.png) +![37.解数独](https://file1.kamacoder.com/i/algo/2020111720451790-20230310131816104.png) ### 回溯三部曲 @@ -83,7 +83,7 @@ bool backtracking(vector>& board) * 递归单层搜索逻辑 -![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131822254.png) +![37.解数独](https://file1.kamacoder.com/i/algo/2020111720451790-20230310131822254.png) 在树形图中可以看出我们需要的是一个二维的递归 (一行一列) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" old mode 100644 new mode 100755 index 8467277115..d8dac0b45b --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -50,7 +50,7 @@ candidates 中的数字可以无限制重复被选取。 本题搜索的过程抽象成树形结构如下: -![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) +![39.组合总和](https://file1.kamacoder.com/i/algo/20201223170730367.png) 注意图中叶子节点的返回条件,因为本题没有组合数量要求,仅仅是总和的限制,所以递归没有层数的限制,只要选取的元素总和超过target,就返回! 而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。 @@ -85,7 +85,7 @@ void backtracking(vector& candidates, int target, int sum, int startIndex) 在如下树形结构中: -![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135337214.png) +![39.组合总和](https://file1.kamacoder.com/i/algo/20201223170730367-20230310135337214.png) 从叶子节点可以清晰看到,终止只有两种情况,sum大于target和sum等于target。 @@ -158,7 +158,7 @@ public: 在这个树形结构中: -![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135342472.png) +![39.组合总和](https://file1.kamacoder.com/i/algo/20201223170730367-20230310135342472.png) 以及上面的版本一的代码大家可以看到,对于sum已经大于target的情况,其实是依然进入了下一层递归,只是下一层递归结束判断的时候,会判断sum > target的话就返回。 @@ -171,7 +171,7 @@ public: 如图: -![39.组合总和1](https://file.kamacoder.com/pics/20201223170809182.png) +![39.组合总和1](https://file1.kamacoder.com/i/algo/20201223170809182.png) for循环剪枝代码如下: diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" old mode 100644 new mode 100755 index f0cbc2200f..0d3972662f --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -76,7 +76,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 选择过程树形结构如图所示: -![40.组合总和II](https://file.kamacoder.com/pics/20230310000918.png) +![40.组合总和II](https://file1.kamacoder.com/i/algo/20230310000918.png) 可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。 @@ -126,7 +126,7 @@ if (sum == target) { 这块比较抽象,如图: -![40.组合总和II1](https://file.kamacoder.com/pics/20230310000954.png) +![40.组合总和II1](https://file1.kamacoder.com/i/algo/20230310000954.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: @@ -137,7 +137,7 @@ if (sum == target) { 而 used[i - 1] == true,说明是进入下一层递归,去下一个数,所以是树枝上,如图所示: -![](https://file.kamacoder.com/pics/20221021163812.png) +![](https://file1.kamacoder.com/i/algo/20221021163812.png) **这块去重的逻辑很抽象,网上搜的题解基本没有能讲清楚的,如果大家之前思考过这个问题或者刷过这道题目,看到这里一定会感觉通透了很多!** diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" old mode 100644 new mode 100755 index 1e6ec11bc5..c208637b2f --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -47,10 +47,10 @@ 首先要明确,要按照行来计算,还是按照列来计算。 按照行来计算如图: -![42.接雨水2](https://file.kamacoder.com/pics/20210402091118927.png) +![42.接雨水2](https://file1.kamacoder.com/i/algo/20210402091118927.png) 按照列来计算如图: -![42.接雨水1](https://file.kamacoder.com/pics/20210402091208445.png) +![42.接雨水1](https://file1.kamacoder.com/i/algo/20210402091208445.png) 一些同学在实现的时候,很容易一会按照行来计算一会按照列来计算,这样就会越写越乱。 @@ -62,7 +62,7 @@ 这句话可以有点绕,来举一个理解,例如求列4的雨水高度,如图: -![42.接雨水3](https://file.kamacoder.com/pics/20210223092732301.png) +![42.接雨水3](https://file1.kamacoder.com/i/algo/20210223092732301.png) 列4 左侧最高的柱子是列3,高度为2(以下用lHeight表示)。 @@ -201,7 +201,7 @@ public: 1. 首先单调栈是按照行方向来计算雨水,如图: -![42.接雨水2](https://file.kamacoder.com/pics/20210223092629946.png) +![42.接雨水2](https://file1.kamacoder.com/i/algo/20210223092629946.png) 知道这一点,后面的就可以理解了。 @@ -215,7 +215,7 @@ public: 如图: -![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229.png) +![42.接雨水4](https://file1.kamacoder.com/i/algo/2021022309321229.png) 关于单调栈的顺序给大家一个总结: [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 中求一个元素右边第一个更大元素,单调栈就是递增的,[84.柱状图中最大的矩形](https://programmercarl.com/0084.柱状图中最大的矩形.html)求一个元素右边第一个更小元素,单调栈就是递减的。 @@ -229,7 +229,7 @@ public: 如图所示: -![42.接雨水5](https://file.kamacoder.com/pics/20210223094619398.png) +![42.接雨水5](https://file1.kamacoder.com/i/algo/20210223094619398.png) 4. 栈里要保存什么数值 @@ -284,7 +284,7 @@ if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况 如果当前遍历的元素(柱子)高度大于栈顶元素的高度,此时就出现凹槽了,如图所示: -![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229-20230310123027977.png) +![42.接雨水4](https://file1.kamacoder.com/i/algo/2021022309321229-20230310123027977.png) 取栈顶元素,将栈顶元素弹出,这个就是凹槽的底部,也就是中间位置,下标记为mid,对应的高度为height[mid](就是图中的高度1)。 diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" old mode 100644 new mode 100755 index dd51384d77..c20cdc65e6 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -47,7 +47,7 @@ 如图: -![45.跳跃游戏II](https://file.kamacoder.com/pics/20201201232309103.png) +![45.跳跃游戏II](https://file1.kamacoder.com/i/algo/20201201232309103.png) **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** @@ -99,11 +99,11 @@ public: 因为当移动下标指向 nums.size - 2 时: - 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即 ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图: - ![45.跳跃游戏II2](https://file.kamacoder.com/pics/20201201232445286.png) + ![45.跳跃游戏II2](https://file1.kamacoder.com/i/algo/20201201232445286.png) - 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图: -![45.跳跃游戏II1](https://file.kamacoder.com/pics/20201201232338693.png) +![45.跳跃游戏II1](https://file1.kamacoder.com/i/algo/20201201232338693.png) 代码如下: diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" old mode 100644 new mode 100755 index 5a190242e3..356f51b5a8 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -41,7 +41,7 @@ 我以[1,2,3]为例,抽象成树形结构如下: -![全排列](https://file.kamacoder.com/pics/20240803180318.png) +![全排列](https://file1.kamacoder.com/i/algo/20240803180318.png) ### 回溯三部曲 @@ -53,7 +53,7 @@ 但排列问题需要一个used数组,标记已经选择的元素,如图橘黄色部分所示: -![全排列](https://file.kamacoder.com/pics/20240803180318.png) +![全排列](https://file1.kamacoder.com/i/algo/20240803180318.png) 代码如下: @@ -65,7 +65,7 @@ void backtracking (vector& nums, vector& used) * 递归终止条件 -![全排列](https://file.kamacoder.com/pics/20240803180318.png) +![全排列](https://file1.kamacoder.com/i/algo/20240803180318.png) 可以看出叶子节点,就是收割结果的地方。 diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" old mode 100644 new mode 100755 index 6ed794aaaf..5330997a66 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -48,7 +48,7 @@ 我以示例中的 [1,1,2]为例 (为了方便举例,已经排序)抽象为一棵树,去重过程如图: -![47.全排列II1](https://file.kamacoder.com/pics/20201124201331223.png) +![47.全排列II1](https://file1.kamacoder.com/i/algo/20201124201331223.png) 图中我们对同一树层,前一位(也就是nums[i-1])如果使用过,那么就进行去重。 @@ -130,11 +130,11 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2](https://file.kamacoder.com/pics/20201124201406192.png) +![47.全排列II2](https://file1.kamacoder.com/i/algo/20201124201406192.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://file.kamacoder.com/pics/20201124201431571.png) +![47.全排列II3](https://file1.kamacoder.com/i/algo/20201124201431571.png) 大家应该很清晰的看到,树层上对前一位去重非常彻底,效率很高,树枝上对前一位去重虽然最后可以得到答案,但是做了很多无用搜索。 diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" old mode 100644 new mode 100755 index 2a90a023e6..d06d7798e8 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -15,7 +15,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, 示例 1: -![](https://file.kamacoder.com/pics/20211020232201.png) +![](https://file1.kamacoder.com/i/algo/20211020232201.png) * 输入:n = 4 * 输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] @@ -45,7 +45,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, 下面我用一个 3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://file.kamacoder.com/pics/20210130182532303.jpg) +![51.N皇后](https://file1.kamacoder.com/i/algo/20210130182532303.jpg) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 @@ -85,7 +85,7 @@ void backtracking(int n, int row, vector& chessboard) { * 递归终止条件 在如下树形结构中: -![51.N皇后](https://file.kamacoder.com/pics/20210130182532303-20230310122134167.jpg) +![51.N皇后](https://file1.kamacoder.com/i/algo/20210130182532303-20230310122134167.jpg) 可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。 diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" old mode 100644 new mode 100755 index 489ab1f756..6c6650ad00 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -13,7 +13,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并 上图为 8 皇后问题的一种解法。 -![51n皇后](https://file.kamacoder.com/pics/20200821152118456.png) +![51n皇后](https://file1.kamacoder.com/i/algo/20200821152118456.png) 给定一个整数 n,返回 n 皇后不同的解决方案的数量。 diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" old mode 100644 new mode 100755 index 6f8c2a6e7e..84bb5f6663 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -76,7 +76,7 @@ if (count > result) result = count; 如动画所示: -![53.最大子序和](https://code-thinking.cdn.bcebos.com/gifs/53.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.gif) +![53.最大子序和](https://file1.kamacoder.com/i/algo/53.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.gif) 红色的起始位置就是贪心每次取 count 为正数的时候,开始一个区间的统计。 diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" old mode 100644 new mode 100755 index 174f55e848..ba44a36104 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -54,7 +54,7 @@ dp[0]应该是多少呢? 5. 举例推导dp数组 以示例一为例,输入:nums = [-2,1,-3,4,-1,2,1,-5,4],对应的dp状态如下: -![53.最大子序和(动态规划)](https://file.kamacoder.com/pics/20210303104129101.png) +![53.最大子序和(动态规划)](https://file1.kamacoder.com/i/algo/20210303104129101.png) **注意最后的结果可不是dp[nums.size() - 1]!** ,而是dp[6]。 diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" old mode 100644 new mode 100755 index a852b6740d..8b700c1fe8 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -36,7 +36,7 @@ 由外向内一圈一圈这么画下去,如下所示: -![](https://file.kamacoder.com/pics/20220922102236.png) +![](https://file1.kamacoder.com/i/algo/20220922102236.png) 这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。 diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" old mode 100644 new mode 100755 index 0ebbcb595c..513fc2e340 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -48,7 +48,7 @@ 如图: -![](https://file.kamacoder.com/pics/20230203105634.png) +![](https://file1.kamacoder.com/i/algo/20230203105634.png) i 每次移动只能在 cover 的范围内移动,每移动一个元素,cover 得到该元素数值(新的覆盖范围)的补充,让 i 继续移动下去。 diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" old mode 100644 new mode 100755 index cb06fcab3d..24a97f6c5a --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -38,7 +38,7 @@ 这么说有点抽象,看图:(**注意图中区间都是按照左边界排序之后了**) -![56.合并区间](https://file.kamacoder.com/pics/20201223200632791.png) +![56.合并区间](https://file1.kamacoder.com/i/algo/20201223200632791.png) 知道如何判断重复之后,剩下的就是合并了,如何去模拟合并区间呢? diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" old mode 100644 new mode 100755 index d7aea257a5..927df1c6c1 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -54,7 +54,7 @@ 那么我按照左闭右开的原则,来画一圈,大家看一下: -![](https://file.kamacoder.com/pics/20220922102236.png) +![](https://file1.kamacoder.com/i/algo/20220922102236.png) 这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。 diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" old mode 100644 new mode 100755 index 20bd56ba9f..ac60767dce --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -16,7 +16,7 @@ 示例 1: -![](https://file.kamacoder.com/pics/20210110174033215.png) +![](https://file1.kamacoder.com/i/algo/20210110174033215.png) * 输入:m = 3, n = 7 * 输出:28 @@ -62,7 +62,7 @@ 如图举例: -![62.不同路径](https://file.kamacoder.com/pics/20201209113602700.png) +![62.不同路径](https://file1.kamacoder.com/i/algo/20201209113602700.png) 此时问题就可以转化为求二叉树叶子节点的个数,代码如下: @@ -131,7 +131,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图所示: -![62.不同路径1](https://file.kamacoder.com/pics/20201209113631392.png) +![62.不同路径1](https://file1.kamacoder.com/i/algo/20201209113631392.png) 以上动规五部曲分析完毕,C++代码如下: @@ -180,7 +180,7 @@ public: 在这个图中,可以看出一共m,n的话,无论怎么走,走到终点都需要 m + n - 2 步。 -![62.不同路径](https://file.kamacoder.com/pics/20201209113602700-20230310120944078.png) +![62.不同路径](https://file1.kamacoder.com/i/algo/20201209113602700-20230310120944078.png) 在这m + n - 2 步中,一定有 m - 1 步是要向下走的,不用管什么时候向下走。 @@ -190,7 +190,7 @@ public: 那么答案,如图所示: -![62.不同路径2](https://file.kamacoder.com/pics/20201209113725324.png) +![62.不同路径2](https://file1.kamacoder.com/i/algo/20201209113725324.png) **求组合的时候,要防止两个int相乘溢出!** 所以不能把算式的分子都算出来,分母都算出来再做除法。 diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" old mode 100644 new mode 100755 index d39036ba3a..f39afe8455 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -14,13 +14,13 @@ 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径? -![](https://file.kamacoder.com/pics/20210111204901338.png) +![](https://file1.kamacoder.com/i/algo/20210111204901338.png) 网格中的障碍物和空位置分别用 1 和 0 来表示。 示例 1: -![](https://file.kamacoder.com/pics/20210111204939971.png) +![](https://file1.kamacoder.com/i/algo/20210111204939971.png) * 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] * 输出:2 @@ -32,7 +32,7 @@ 示例 2: -![](https://file.kamacoder.com/pics/20210111205857918.png) +![](https://file1.kamacoder.com/i/algo/20210111205857918.png) * 输入:obstacleGrid = [[0,1],[0,0]] * 输出:1 @@ -93,7 +93,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1; 如图: -![63.不同路径II](https://file.kamacoder.com/pics/20210104114513928.png) +![63.不同路径II](https://file1.kamacoder.com/i/algo/20210104114513928.png) 下标(0, j)的初始化情况同理。 @@ -127,11 +127,11 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://file.kamacoder.com/pics/20210104114548983.png) +![63.不同路径II1](https://file1.kamacoder.com/i/algo/20210104114548983.png) 对应的dp table 如图: -![63.不同路径II2](https://file.kamacoder.com/pics/20210104114610256.png) +![63.不同路径II2](https://file1.kamacoder.com/i/algo/20210104114610256.png) 如果这个图看不懂,建议再理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下! diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" old mode 100644 new mode 100755 index 17bf3ee760..316fbd4f39 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -101,7 +101,7 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 举例当n为5的时候,dp table(dp数组)应该是这样的 -![70.爬楼梯](https://file.kamacoder.com/pics/20210105202546299.png) +![70.爬楼梯](https://file1.kamacoder.com/i/algo/20210105202546299.png) 如果代码出问题了,就把dp table 打印出来,看看究竟是不是和自己推导的一样。 diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" old mode 100644 new mode 100755 diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" old mode 100644 new mode 100755 index 192ea47002..c4bcbb4338 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -170,7 +170,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 可以看出dp[i][j]是依赖左方,上方和左上方元素的,如图: -![72.编辑距离](https://file.kamacoder.com/pics/20210114162113131.jpg) +![72.编辑距离](https://file1.kamacoder.com/i/algo/20210114162113131.jpg) 所以在dp矩阵中一定是从左到右从上到下去遍历。 @@ -194,7 +194,7 @@ for (int i = 1; i <= word1.size(); i++) { 以示例1为例,输入:`word1 = "horse", word2 = "ros"`为例,dp矩阵状态图如下: -![72.编辑距离1](https://file.kamacoder.com/pics/20210114162132300.jpg) +![72.编辑距离1](https://file1.kamacoder.com/i/algo/20210114162132300.jpg) 以上动规五部分析完毕,C++代码如下: diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" old mode 100644 new mode 100755 index c523c01c17..4c9e97fd47 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -82,7 +82,7 @@ for (int i = 1; i <= n; i++) { 那么我把组合问题抽象为如下树形结构: -![77.组合](https://file.kamacoder.com/pics/20201123195223940.png) +![77.组合](https://file1.kamacoder.com/i/algo/20201123195223940.png) 可以看出这棵树,一开始集合是 1,2,3,4, 从左向右取数,取过的数,不再重复取。 @@ -126,7 +126,7 @@ vector path; // 用来存放符合条件结果 从下图中红线部分可以看出,在集合[1,2,3,4]取1之后,下一层递归,就要在[2,3,4]中取数了,那么下一层递归如何知道从[2,3,4]中取数呢,靠的就是startIndex。 -![77.组合2](https://file.kamacoder.com/pics/20201123195328976.png) +![77.组合2](https://file1.kamacoder.com/i/algo/20201123195328976.png) 所以需要startIndex来记录下一层递归,搜索的起始位置。 @@ -146,7 +146,7 @@ path这个数组的大小如果达到k,说明我们找到了一个子集大小 如图红色部分: -![77.组合3](https://file.kamacoder.com/pics/20201123195407907.png) +![77.组合3](https://file1.kamacoder.com/i/algo/20201123195407907.png) 此时用result二维数组,把path保存起来,并终止本层递归。 @@ -163,7 +163,7 @@ if (path.size() == k) { 回溯法的搜索过程就是一个树型结构的遍历过程,在如下图中,可以看出for循环用来横向遍历,递归的过程是纵向遍历。 -![77.组合1](https://file.kamacoder.com/pics/20201123195242899.png) +![77.组合1](https://file1.kamacoder.com/i/algo/20201123195242899.png) 如此我们才遍历完图中的这棵树。 @@ -267,7 +267,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://file.kamacoder.com/pics/20210130194335207-20230310134409532.png) +![77.组合4](https://file1.kamacoder.com/i/algo/20210130194335207-20230310134409532.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" old mode 100644 new mode 100755 index c5e26e7706..8ddc4058cc --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -67,7 +67,7 @@ for (int i = startIndex; i <= n; i++) { 这么说有点抽象,如图所示: -![77.组合4](https://file.kamacoder.com/pics/20210130194335207.png) +![77.组合4](https://file1.kamacoder.com/i/algo/20210130194335207.png) 图中每一个节点(图中为矩形),就代表本层的一个for循环,那么每一层的for循环从第二个数开始遍历的话,都没有意义,都是无效遍历。 diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" old mode 100644 new mode 100755 index 73eb385bc1..844b8dc2ca --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -46,7 +46,7 @@ 以示例中nums = [1,2,3]为例把求子集抽象为树型结构,如下: -![78.子集](https://code-thinking.cdn.bcebos.com/pics/78.%E5%AD%90%E9%9B%86.png) +![78.子集](https://file1.kamacoder.com/i/algo/78.%E5%AD%90%E9%9B%86.png) 从图中红线部分,可以看出**遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合**。 @@ -70,7 +70,7 @@ void backtracking(vector& nums, int startIndex) { 从图中可以看出: -![78.子集](https://code-thinking.cdn.bcebos.com/pics/78.%E5%AD%90%E9%9B%86.png) +![78.子集](https://file1.kamacoder.com/i/algo/78.%E5%AD%90%E9%9B%86.png) 剩余集合为空的时候,就是叶子节点。 diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" old mode 100644 new mode 100755 index e1a6671e73..99fb1678e6 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -11,9 +11,9 @@ 求在该柱状图中,能够勾勒出来的矩形的最大面积。 -![](https://file.kamacoder.com/pics/20210803220437.png) +![](https://file1.kamacoder.com/i/algo/20210803220437.png) -![](https://file.kamacoder.com/pics/20210803220506.png) +![](https://file1.kamacoder.com/i/algo/20210803220506.png) * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 @@ -114,7 +114,7 @@ public: 我来举一个例子,如图: -![](https://file.kamacoder.com/pics/20230221165730.png) +![](https://file1.kamacoder.com/i/algo/20230221165730.png) 只有栈里从大到小的顺序,才能保证栈顶元素找到左右两边第一个小于栈顶元素的柱子。 @@ -179,7 +179,7 @@ public: 如果数组本身就是升序的,例如[2,4,6,8],那么入栈之后 都是单调递减,一直都没有走 情况三 计算结果的哪一步,所以最后输出的就是0了。 如图: -![](https://file.kamacoder.com/pics/20230221163936.png) +![](https://file1.kamacoder.com/i/algo/20230221163936.png) 那么结尾加一个0,就会让栈里的所有元素,走到情况三的逻辑。 @@ -194,7 +194,7 @@ public: 之后又将6 加入栈(此时8已经弹出了),然后 就是 4 与 栈口元素 6 进行比较,周而复始,那么计算的最后结果result就是0。 如图所示: -![](https://file.kamacoder.com/pics/20230221164533.png) +![](https://file1.kamacoder.com/i/algo/20230221164533.png) 所以我们需要在 height数组前后各加一个元素0。 diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" old mode 100644 new mode 100755 index 2f26e6068f..2e8945c90f --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -39,7 +39,7 @@ 用示例中的[1, 2, 2] 来举例,如图所示: (**注意去重需要先对集合排序**) -![90.子集II](https://file.kamacoder.com/pics/20201124195411977.png) +![90.子集II](https://file1.kamacoder.com/i/algo/20201124195411977.png) 从图中可以看出,同一树层上重复取2 就要过滤掉,同一树枝上就可以重复取2,因为同一树枝上元素的集合才是唯一子集! diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" old mode 100644 new mode 100755 index 5ef2162898..6fa732d0c1 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -54,7 +54,7 @@ 切割问题可以抽象为树型结构,如图: -![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933.png) +![93.复原IP地址](https://file1.kamacoder.com/i/algo/20201123203735933.png) ### 回溯三部曲 @@ -106,7 +106,7 @@ if (pointNum == 3) { // 逗点数量为3时,分隔结束 如果不合法就结束本层循环,如图中剪掉的分支: -![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933-20230310132314109.png) +![93.复原IP地址](https://file1.kamacoder.com/i/algo/20201123203735933-20230310132314109.png) 然后就是递归和回溯的过程: diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" old mode 100644 new mode 100755 index ca99a46695..e5bc2b6b65 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -12,7 +12,7 @@ 示例: -![](https://file.kamacoder.com/pics/20210113161941835.png) +![](https://file1.kamacoder.com/i/algo/20210113161941835.png) ## 算法公开课 @@ -27,11 +27,11 @@ 了解了二叉搜索树之后,我们应该先举几个例子,画画图,看看有没有什么规律,如图: -![96.不同的二叉搜索树](https://file.kamacoder.com/pics/20210107093106367.png) +![96.不同的二叉搜索树](https://file1.kamacoder.com/i/algo/20210107093106367.png) n为1的时候有一棵树,n为2有两棵树,这个是很直观的。 -![96.不同的二叉搜索树1](https://file.kamacoder.com/pics/20210107093129889.png) +![96.不同的二叉搜索树1](https://file1.kamacoder.com/i/algo/20210107093129889.png) 来看看n为3的时候,有哪几种情况。 @@ -65,7 +65,7 @@ dp[3],就是 元素1为头结点搜索树的数量 + 元素2为头结点搜索 如图所示: -![96.不同的二叉搜索树2](https://file.kamacoder.com/pics/20210107093226241.png) +![96.不同的二叉搜索树2](https://file1.kamacoder.com/i/algo/20210107093226241.png) 此时我们已经找到递推关系了,那么可以用动规五部曲再系统分析一遍。 @@ -118,7 +118,7 @@ for (int i = 1; i <= n; i++) { n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://file.kamacoder.com/pics/20210107093253987.png) +![96.不同的二叉搜索树3](https://file1.kamacoder.com/i/algo/20210107093253987.png) 当然如果自己画图举例的话,基本举例到n为3就可以了,n为4的时候,画图已经比较麻烦了。 diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" old mode 100644 new mode 100755 index 9569cbddf1..990d3c8413 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -16,7 +16,7 @@ * 节点的右子树只包含大于当前节点的数。 * 所有左子树和右子树自身必须也是二叉搜索树。 -![98.验证二叉搜索树](https://file.kamacoder.com/pics/20230310000750.png) +![98.验证二叉搜索树](https://file1.kamacoder.com/i/algo/20230310000750.png) ## 算法公开课 @@ -102,7 +102,7 @@ if (root->val > root->left->val && root->val < root->right->val) { 例如: [10,5,15,null,null,6,20] 这个case: -![二叉搜索树](https://file.kamacoder.com/pics/20230310000824.png) +![二叉搜索树](https://file1.kamacoder.com/i/algo/20230310000824.png) 节点10大于左节点5,小于右节点15,但右子树里出现了一个6 这就不符合了! diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" old mode 100644 new mode 100755 index e5f610009e..df1b55a462 --- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" +++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" @@ -12,9 +12,9 @@ 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 -![](https://file.kamacoder.com/pics/20210726172932.png) +![](https://file1.kamacoder.com/i/algo/20210726172932.png) -![](https://file.kamacoder.com/pics/20210726173011.png) +![](https://file1.kamacoder.com/i/algo/20210726173011.png) ## 思路 diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" old mode 100644 new mode 100755 index 205597b068..24e9e2684e --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -9,7 +9,7 @@ 给定一个二叉树,检查它是否是镜像对称的。 -![101. 对称二叉树](https://file.kamacoder.com/pics/20210203144607387.png) +![101. 对称二叉树](https://file1.kamacoder.com/i/algo/20210203144607387.png) ## 算法公开课 @@ -25,7 +25,7 @@ 比较的是两个子树的里侧和外侧的元素是否相等。如图所示: -![101. 对称二叉树1](https://file.kamacoder.com/pics/20210203144624414.png) +![101. 对称二叉树1](https://file1.kamacoder.com/i/algo/20210203144624414.png) 那么遍历的顺序应该是什么样的呢? @@ -169,7 +169,7 @@ public: 通过队列来判断根节点的左子树和右子树的内侧和外侧是否相等,如动画所示: -![101.对称二叉树](https://code-thinking.cdn.bcebos.com/gifs/101.%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91.gif) +![101.对称二叉树](https://file1.kamacoder.com/i/algo/101.%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91.gif) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" old mode 100644 new mode 100755 index 6725d72cc2..819153be97 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -26,7 +26,7 @@ -![我要打十个](https://code-thinking.cdn.bcebos.com/gifs/%E6%88%91%E8%A6%81%E6%89%93%E5%8D%81%E4%B8%AA.gif) +![我要打十个](https://file1.kamacoder.com/i/algo/%E6%88%91%E8%A6%81%E6%89%93%E5%8D%81%E4%B8%AA.gif) @@ -37,7 +37,7 @@ 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。 -![102.二叉树的层序遍历](https://file.kamacoder.com/pics/20210203144842988.png) +![102.二叉树的层序遍历](https://file1.kamacoder.com/i/algo/20210203144842988.png) ### 思路 @@ -57,7 +57,7 @@ 使用队列实现二叉树广度优先遍历,动画如下: -![102二叉树的层序遍历](https://code-thinking.cdn.bcebos.com/gifs/102%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86.gif) +![102二叉树的层序遍历](https://file1.kamacoder.com/i/algo/102%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86.gif) 这样就实现了层序从左到右遍历二叉树。 @@ -532,7 +532,7 @@ public IList> LevelOrder(TreeNode root) 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) -![107.二叉树的层次遍历II](https://file.kamacoder.com/pics/20210203151058308.png) +![107.二叉树的层次遍历II](https://file1.kamacoder.com/i/algo/20210203151058308.png) ### 思路 @@ -926,7 +926,7 @@ public IList> LevelOrderBottom(TreeNode root) 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 -![199.二叉树的右视图](https://file.kamacoder.com/pics/20210203151307377.png) +![199.二叉树的右视图](https://file1.kamacoder.com/i/algo/20210203151307377.png) ### 思路 @@ -1276,7 +1276,7 @@ public class Solution 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。 -![637.二叉树的层平均值](https://file.kamacoder.com/pics/20210203151350500.png) +![637.二叉树的层平均值](https://file1.kamacoder.com/i/algo/20210203151350500.png) ### 思路 @@ -1634,7 +1634,7 @@ public class Solution { 例如,给定一个 3叉树 : -![429. N叉树的层序遍历](https://file.kamacoder.com/pics/20210203151439168.png) +![429. N叉树的层序遍历](https://file1.kamacoder.com/i/algo/20210203151439168.png) 返回其层序遍历: @@ -2006,7 +2006,7 @@ impl Solution { 您需要在二叉树的每一行中找到最大的值。 -![515.在每个树行中找最大值](https://file.kamacoder.com/pics/20210203151532153.png) +![515.在每个树行中找最大值](https://file1.kamacoder.com/i/algo/20210203151532153.png) ### 思路 @@ -2337,7 +2337,7 @@ struct Node { 初始状态下,所有 next 指针都被设置为 NULL。 -![116.填充每个节点的下一个右侧节点指针](https://file.kamacoder.com/pics/20210203152044855.jpg) +![116.填充每个节点的下一个右侧节点指针](https://file1.kamacoder.com/i/algo/20210203152044855.jpg) ### 思路 @@ -2971,7 +2971,7 @@ object Solution { 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310134849764.png) +![104. 二叉树的最大深度](https://file1.kamacoder.com/i/algo/20210203153031914-20230310134849764.png) 返回它的最大深度 3 。 @@ -2981,7 +2981,7 @@ object Solution { 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://file.kamacoder.com/pics/20200810193056585-20230310134854803.png) +![层序遍历](https://file1.kamacoder.com/i/algo/20200810193056585-20230310134854803.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" old mode 100644 new mode 100755 index 2eb22ae5a3..52d6d0e5fd --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -18,7 +18,7 @@ 给定二叉树 [3,9,20,null,null,15,7], -![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310121809902.png) +![104. 二叉树的最大深度](https://file1.kamacoder.com/i/algo/20210203153031914-20230310121809902.png) 返回它的最大深度 3 。 @@ -172,7 +172,7 @@ public: 在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示: -![层序遍历](https://file.kamacoder.com/pics/20200810193056585.png) +![层序遍历](https://file1.kamacoder.com/i/algo/20200810193056585.png) 所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。 @@ -217,7 +217,7 @@ public: 例如,给定一个 3叉树 : -![559.n叉树的最大深度](https://file.kamacoder.com/pics/2021020315313214.png) +![559.n叉树的最大深度](https://file1.kamacoder.com/i/algo/2021020315313214.png) 我们应返回其最大深度,3。 diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" old mode 100644 new mode 100755 index 2f8e5eefb6..5253325835 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -25,7 +25,7 @@ * 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: -![106. 从中序与后序遍历序列构造二叉树1](https://file.kamacoder.com/pics/20210203154316774.png) +![106. 从中序与后序遍历序列构造二叉树1](https://file1.kamacoder.com/i/algo/20210203154316774.png) ## 算法公开课 @@ -40,7 +40,7 @@ 流程如图: -![106.从中序与后序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154249860.png) +![106.从中序与后序遍历序列构造二叉树](https://file1.kamacoder.com/i/algo/20210203154249860.png) 那么代码应该怎么写呢? @@ -411,7 +411,7 @@ public: 中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: -![105. 从前序与中序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154626672.png) +![105. 从前序与中序遍历序列构造二叉树](https://file1.kamacoder.com/i/algo/20210203154626672.png) ### 思路 @@ -554,7 +554,7 @@ public: 举一个例子: -![106.从中序与后序遍历序列构造二叉树2](https://file.kamacoder.com/pics/20210203154720326.png) +![106.从中序与后序遍历序列构造二叉树2](https://file1.kamacoder.com/i/algo/20210203154720326.png) tree1 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" old mode 100644 new mode 100755 index 5829e2d220..2df1c2615b --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -16,7 +16,7 @@ 示例: -![108.将有序数组转换为二叉搜索树](https://file.kamacoder.com/pics/20201022164420763.png) +![108.将有序数组转换为二叉搜索树](https://file1.kamacoder.com/i/algo/20201022164420763.png) ## 算法公开课 @@ -40,7 +40,7 @@ 例如 有序数组[-10,-3,0,5,9] 就可以构造成这样的二叉搜索树,如图。 -![](https://file.kamacoder.com/pics/20220930173553.png) +![](https://file1.kamacoder.com/i/algo/20220930173553.png) 上图中,是符合二叉搜索树的特性吧,如果要这么做的话,是不是本题意义就不大了,所以才强调是平衡二叉搜索树。 @@ -63,7 +63,7 @@ 如下两棵树,都是这个数组的平衡二叉搜索树: -![108.将有序数组转换为二叉搜索树](https://code-thinking.cdn.bcebos.com/pics/108.将有序数组转换为二叉搜索树.png) +![108.将有序数组转换为二叉搜索树](https://file1.kamacoder.com/i/algo/108.将有序数组转换为二叉搜索树.png) 如果要分割的数组长度为偶数的时候,中间元素为两个,是取左边元素 就是树1,取右边元素就是树2。 diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" old mode 100644 new mode 100755 index ff84ad8471..d5b100ae80 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -19,7 +19,7 @@ 给定二叉树 [3,9,20,null,null,15,7] -![110.平衡二叉树](https://file.kamacoder.com/pics/2021020315542230.png) +![110.平衡二叉树](https://file1.kamacoder.com/i/algo/2021020315542230.png) 返回 true 。 @@ -27,7 +27,7 @@ 给定二叉树 [1,2,2,3,3,null,null,4,4] -![110.平衡二叉树1](https://file.kamacoder.com/pics/20210203155447919.png) +![110.平衡二叉树1](https://file1.kamacoder.com/i/algo/20210203155447919.png) 返回 false 。 @@ -46,7 +46,7 @@ 但leetcode中强调的深度和高度很明显是按照节点来计算的,如图: -![110.平衡二叉树2](https://file.kamacoder.com/pics/20210203155515650.png) +![110.平衡二叉树2](https://file1.kamacoder.com/i/algo/20210203155515650.png) 关于根节点的深度究竟是1 还是 0,不同的地方有不一样的标准,leetcode的题目中都是以节点为一度,即根节点深度是1。但维基百科上定义用边为一度,即根节点的深度是0,我们暂时以leetcode为准(毕竟要在这上面刷题)。 diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" old mode 100644 new mode 100755 index bd4ea29d6c..e1ee42657c --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -20,7 +20,7 @@ 给定二叉树 [3,9,20,null,null,15,7], -![111.二叉树的最小深度1](https://file.kamacoder.com/pics/2021020315582586.png) +![111.二叉树的最小深度1](https://file1.kamacoder.com/i/algo/2021020315582586.png) 返回它的最小深度 2. @@ -46,7 +46,7 @@ 本题还有一个误区,在处理节点的过程中,最大深度很容易理解,最小深度就不那么好理解,如图: -![111.二叉树的最小深度](https://code-thinking.cdn.bcebos.com/pics/111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.png) +![111.二叉树的最小深度](https://file1.kamacoder.com/i/algo/111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.png) 这就重新审题了,题目中说的是:**最小深度是从根节点到最近叶子节点的最短路径上的节点数量。**注意是**叶子节点**。 @@ -88,7 +88,7 @@ return result; 这个代码就犯了此图中的误区: -![111.二叉树的最小深度](https://code-thinking.cdn.bcebos.com/pics/111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.png) +![111.二叉树的最小深度](https://file1.kamacoder.com/i/algo/111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.png) 如果这么求的话,没有左孩子的分支会算为最短深度。 diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" old mode 100644 new mode 100755 index 24891acee9..73795bcfc9 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -15,7 +15,7 @@ 示例: 给定如下二叉树,以及目标和 sum = 22, -![](https://file.kamacoder.com/pics/20230407210247.png) +![](https://file1.kamacoder.com/i/algo/20230407210247.png) 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 @@ -53,7 +53,7 @@ 如图所示: -![112.路径总和](https://file.kamacoder.com/pics/2021020316051216.png) +![112.路径总和](https://file1.kamacoder.com/i/algo/2021020316051216.png) 图中可以看出,遍历的路线,并不要遍历整棵树,所以递归函数需要返回值,可以用bool类型表示。 @@ -230,7 +230,7 @@ public: 给定如下二叉树,以及目标和 sum = 22, -![113.路径总和ii1.png](https://file.kamacoder.com/pics/20210203160854654.png) +![113.路径总和ii1.png](https://file1.kamacoder.com/i/algo/20210203160854654.png) ### 思路 @@ -239,7 +239,7 @@ public: 如图: -![113.路径总和ii](https://file.kamacoder.com/pics/20210203160922745.png) +![113.路径总和ii](https://file1.kamacoder.com/i/algo/20210203160922745.png) 为了尽可能的把细节体现出来,我写出如下代码(**这份代码并不简洁,但是逻辑非常清晰**) diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index 1df3d899a8..499bf100e2 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -12,7 +12,7 @@ 题目数据保证答案符合 32 位带符号整数范围。 -![115.不同的子序列示例](https://code-thinking.cdn.bcebos.com/pics/115.不同的子序列示例.jpg) +![115.不同的子序列示例](https://file1.kamacoder.com/i/algo/115.不同的子序列示例.jpg) 提示: @@ -70,7 +70,7 @@ dp[i][j]:以i-1为结尾的s子序列中出现以j-1为结尾的t的个数为d 从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j] 是从上方和左上方推导而来,如图:,那么 dp[i][0] 和dp[0][j]是一定要初始化的。 -![](https://file.kamacoder.com/pics/20221222165412.png) +![](https://file1.kamacoder.com/i/algo/20221222165412.png) 每次当初始化的时候,都要回顾一下dp[i][j]的定义,不要凭感觉初始化。 @@ -101,7 +101,7 @@ for (int j = 1; j <= t.size(); j++) dp[0][j] = 0; // 其实这行代码可以和 从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j]都是根据左上方和正上方推出来的。 -![](https://file.kamacoder.com/pics/20221222165412.png) +![](https://file1.kamacoder.com/i/algo/20221222165412.png) 所以遍历的时候一定是从上到下,从左到右,这样保证dp[i][j]可以根据之前计算出来的数值进行计算。 @@ -123,7 +123,7 @@ for (int i = 1; i <= s.size(); i++) { 以s:"baegg",t:"bag"为例,推导dp数组状态如下: -![115.不同的子序列](https://code-thinking.cdn.bcebos.com/pics/115.%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97.jpg) +![115.不同的子序列](https://file1.kamacoder.com/i/algo/115.%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97.jpg) 如果写出来的代码怎么改都通过不了,不妨把dp数组打印出来,看一看,是不是这样的。 diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" old mode 100644 new mode 100755 index 9de1de1ee5..88d3abc93e --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -26,7 +26,7 @@ struct Node { * 你只能使用常量级额外空间。 * 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。 -![](https://file.kamacoder.com/pics/20210727143202.png) +![](https://file1.kamacoder.com/i/algo/20210727143202.png) ## 思路 @@ -42,7 +42,7 @@ struct Node { 如图,假如当前操作的节点是cur: - + 最关键的点是可以通过上一层递归 搭出来的线,进行本次搭线。 diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" old mode 100644 new mode 100755 index f82ed962fa..d12cbf2fe2 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -129,7 +129,7 @@ dp[0][1]表示第0天不持有股票,不持有股票那么现金就是0,所 以示例1,输入:[7,1,5,3,6,4]为例,dp数组状态如下: -![121.买卖股票的最佳时机](https://file.kamacoder.com/pics/20210224225642465.png) +![121.买卖股票的最佳时机](https://file1.kamacoder.com/i/algo/20210224225642465.png) dp[5][1]就是最终结果。 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" old mode 100644 new mode 100755 index 4ccb17bbfd..0da4241931 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -66,7 +66,7 @@ 如图: -![122.买卖股票的最佳时机II](https://file.kamacoder.com/pics/2020112917480858-20230310134659477.png) +![122.买卖股票的最佳时机II](https://file1.kamacoder.com/i/algo/2020112917480858-20230310134659477.png) 一些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" old mode 100644 new mode 100755 diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" old mode 100644 new mode 100755 index c4ff89a0c5..063477cb5a --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -120,7 +120,7 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]); 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://file.kamacoder.com/pics/20201228181724295-20230310134201291.png) +![123.买卖股票的最佳时机III](https://file1.kamacoder.com/i/algo/20201228181724295-20230310134201291.png) 大家可以看到红色框为最后两次卖出的状态。 diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" old mode 100644 new mode 100755 index 1ce0bc11a7..0204606056 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -31,7 +31,7 @@ 以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。 -![](https://file.kamacoder.com/pics/20210827175432.png) +![](https://file1.kamacoder.com/i/algo/20210827175432.png) 本题只需要求出最短路径的长度就可以了,不用找出路径。 diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" old mode 100644 new mode 100755 index 923bc63807..1568a49469 --- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" +++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" @@ -81,7 +81,7 @@ int vectorToInt(const vector& vec) { 如图: - + 代码如下: diff --git "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" old mode 100644 new mode 100755 index 278c12eccc..10d6585c4c --- "a/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" +++ "b/problems/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.md" @@ -8,7 +8,7 @@ 给你一个 m x n 的矩阵 board ,由若干字符 'X' 和 'O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。 -![](https://file.kamacoder.com/pics/20220901104745.png) +![](https://file1.kamacoder.com/i/algo/20220901104745.png) * 输入:board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]] * 输出:[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]] @@ -28,11 +28,11 @@ 步骤一:深搜或者广搜将地图周边的'O'全部改成'A',如图所示: -![图一](https://file.kamacoder.com/pics/20220902102337.png) +![图一](https://file1.kamacoder.com/i/algo/20220902102337.png) 步骤二:在遍历地图,将'O'全部改成'X'(地图中间的'O'改成了'X'),将'A'改回'O'(保留的地图周边的'O'),如图所示: -![图二](https://file.kamacoder.com/pics/20220902102831.png) +![图二](https://file1.kamacoder.com/i/algo/20220902102831.png) 整体C++代码如下,以下使用dfs实现,其实遍历方式dfs,bfs都是可以的。 diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" old mode 100644 new mode 100755 index f9b5d244c5..c76f1ce2f1 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -50,7 +50,7 @@ 所以切割问题,也可以抽象为一棵树形结构,如图: -![131.分割回文串](https://code-thinking.cdn.bcebos.com/pics/131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.jpg) +![131.分割回文串](https://file1.kamacoder.com/i/algo/131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.jpg) 递归用来纵向遍历,for循环用来横向遍历,切割线(就是图中的红线)切割到字符串的结尾位置,说明找到了一个切割方法。 @@ -76,7 +76,7 @@ void backtracking (const string& s, int startIndex) { * 递归函数终止条件 -![131.分割回文串](https://code-thinking.cdn.bcebos.com/pics/131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.jpg) +![131.分割回文串](https://file1.kamacoder.com/i/algo/131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.jpg) 从树形结构的图中可以看出:切割线切到了字符串最后面,说明找到了一种切割方法,此时就是本层递归的终止条件。 diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" old mode 100644 new mode 100755 index 089dd52c78..8bbfa4ee10 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -161,7 +161,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 以输入:"aabc" 为例: -![132.分割回文串II](https://file.kamacoder.com/pics/20210124182218844.jpg) +![132.分割回文串II](https://file1.kamacoder.com/i/algo/20210124182218844.jpg) 以上分析完毕,代码如下: diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" old mode 100644 new mode 100755 index 354f642448..5c8b0c3cc8 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -144,7 +144,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i 如图: -![](https://file.kamacoder.com/pics/20230117165628.png) +![](https://file1.kamacoder.com/i/algo/20230117165628.png) 那么为什么一旦[0,i] 区间和为负数,起始位置就可以是i+1呢,i+1后面就不会出现更大的负数? @@ -152,7 +152,7 @@ i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i 那有没有可能 [0,i] 区间 选某一个作为起点,累加到 i这里 curSum是不会小于零呢? 如图: -![](https://file.kamacoder.com/pics/20230117170703.png) +![](https://file1.kamacoder.com/i/algo/20230117170703.png) 如果 curSum<0 说明 区间和1 + 区间和2 < 0, 那么 假设从上图中的位置开始计数curSum不会小于0的话,就是 区间和2>0。 diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" old mode 100644 new mode 100755 index 30df21495e..9701f0f0c1 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -56,7 +56,7 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果](https://file.kamacoder.com/pics/20201117114916878.png) +![135.分发糖果](https://file1.kamacoder.com/i/algo/20201117114916878.png) 再确定左孩子大于右孩子的情况(从后向前遍历) @@ -66,7 +66,7 @@ for (int i = 1; i < ratings.size(); i++) { 如果从前向后遍历,rating[5]与rating[4]的比较 就不能用上 rating[5]与rating[6]的比较结果了 。如图: -![](https://file.kamacoder.com/pics/20230202102044.png) +![](https://file1.kamacoder.com/i/algo/20230202102044.png) **所以确定左孩子大于右孩子的情况一定要从后向前遍历!** @@ -82,7 +82,7 @@ for (int i = 1; i < ratings.size(); i++) { 如图: -![135.分发糖果1](https://file.kamacoder.com/pics/20201117115658791.png) +![135.分发糖果1](https://file1.kamacoder.com/i/algo/20201117115658791.png) 所以该过程代码如下: diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" old mode 100644 new mode 100755 index 513d327ba1..2015cb90c1 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -180,7 +180,7 @@ dp[0]表示如果字符串为空的话,说明出现在字典里。 以输入: s = "leetcode", wordDict = ["leet", "code"]为例,dp状态如图: -![139.单词拆分](https://file.kamacoder.com/pics/20210202162652727.jpg) +![139.单词拆分](https://file1.kamacoder.com/i/algo/20210202162652727.jpg) dp[s.size()]就是最终结果。 @@ -241,7 +241,7 @@ public: 使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下: -![](https://file.kamacoder.com/pics/20240809155103.png) +![](https://file1.kamacoder.com/i/algo/20240809155103.png) 最后dp[s.size()] = 0 即 dp[13] = 0 ,而不是1,因为先用 "apple" 去遍历的时候,dp[8]并没有被赋值为1 (还没用"pen"),所以 dp[13]也不能变成1。 diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" old mode 100644 new mode 100755 index 685a92d529..d3583ba866 --- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" +++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" @@ -13,7 +13,7 @@ 如果链表中存在环,则返回 true 。 否则,返回 false 。 -![](https://file.kamacoder.com/pics/20210727173600.png) +![](https://file1.kamacoder.com/i/algo/20210727173600.png) ## 思路 @@ -29,7 +29,7 @@ 会发现最终都是这种情况, 如下图: - + fast和slow各自再走一步, fast和slow就相遇了 @@ -38,7 +38,7 @@ fast和slow各自再走一步, fast和slow就相遇了 动画如下: -![141.环形链表](https://code-thinking.cdn.bcebos.com/gifs/141.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.gif) +![141.环形链表](https://file1.kamacoder.com/i/algo/141.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.gif) C++代码如下 diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" old mode 100644 new mode 100755 index 6cfabc60f6..4fd81ef0f5 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -20,7 +20,7 @@ **说明**:不允许修改给定的链表。 -![循环链表](https://file.kamacoder.com/pics/20200816110112704.png) +![循环链表](https://file1.kamacoder.com/i/algo/20200816110112704.png) ## 算法公开课 @@ -50,7 +50,7 @@ 会发现最终都是这种情况, 如下图: -![142环形链表1](https://file.kamacoder.com/pics/20210318162236720.png) +![142环形链表1](https://file1.kamacoder.com/i/algo/20210318162236720.png) fast和slow各自再走一步, fast和slow就相遇了 @@ -59,7 +59,7 @@ fast和slow各自再走一步, fast和slow就相遇了 动画如下: -![141.环形链表](https://code-thinking.cdn.bcebos.com/gifs/141.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.gif) +![141.环形链表](https://file1.kamacoder.com/i/algo/141.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.gif) ### 如果有环,如何找到这个环的入口 @@ -70,7 +70,7 @@ fast和slow各自再走一步, fast和slow就相遇了 环形入口节点到 fast指针与slow指针相遇节点 节点数为y。 从相遇节点 再到环形入口节点节点数为 z。 如图所示: -![](https://file.kamacoder.com/pics/20220925103433.png) +![](https://file1.kamacoder.com/i/algo/20220925103433.png) 那么相遇时: slow指针走过的节点数为: `x + y`, @@ -103,7 +103,7 @@ fast指针走过的节点数:` x + y + n (y + z)`,n为fast指针在环内走 动画如下: -![142.环形链表II(求入口)](https://code-thinking.cdn.bcebos.com/gifs/142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II%EF%BC%88%E6%B1%82%E5%85%A5%E5%8F%A3%EF%BC%89.gif) +![142.环形链表II(求入口)](https://file1.kamacoder.com/i/algo/142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II%EF%BC%88%E6%B1%82%E5%85%A5%E5%8F%A3%EF%BC%89.gif) 那么 n如果大于1是什么情况呢,就是fast指针在环形转n圈之后才遇到 slow指针。 @@ -154,20 +154,20 @@ public: 即文章[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)中如下的地方: -![142环形链表5](https://file.kamacoder.com/pics/20210318165123581.png) +![142环形链表5](https://file1.kamacoder.com/i/algo/20210318165123581.png) 首先slow进环的时候,fast一定是先进环来了。 如果slow进环入口,fast也在环入口,那么把这个环展开成直线,就是如下图的样子: -![142环形链表3](https://file.kamacoder.com/pics/2021031816503266.png) +![142环形链表3](https://file1.kamacoder.com/i/algo/2021031816503266.png) 可以看出如果slow 和 fast同时在环入口开始走,一定会在环入口3相遇,slow走了一圈,fast走了两圈。 重点来了,slow进环的时候,fast一定是在环的任意一个位置,如图: -![142环形链表4](https://file.kamacoder.com/pics/2021031816515727.png) +![142环形链表4](https://file1.kamacoder.com/i/algo/2021031816515727.png) 那么fast指针走到环入口3的时候,已经走了k + n 个节点,slow相应的应该走了(k + n) / 2 个节点。 diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" old mode 100644 new mode 100755 index 98488bc11d..e7056913ee --- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" +++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" @@ -6,7 +6,7 @@ [力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/) -![](https://file.kamacoder.com/pics/20210726160122.png) +![](https://file1.kamacoder.com/i/algo/20210726160122.png) ## 思路 @@ -96,7 +96,7 @@ public: 如图: - + 这种方法,比较难,平均切割链表,看上去很简单,真正代码写的时候有很多细节,同时两个链表最后拼装整一个新的链表也有一些细节需要注意! diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" old mode 100644 new mode 100755 index 6d21452d1d..de56c51ff7 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -78,7 +78,7 @@ 在进一步看,本题中每一个子表达式要得出一个结果,然后拿这个结果再进行运算,那么**这岂不就是一个相邻字符串消除的过程,和[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中的对对碰游戏是不是就非常像了。** 如动画所示: -![150.逆波兰表达式求值](https://code-thinking.cdn.bcebos.com/gifs/150.逆波兰表达式求值.gif) +![150.逆波兰表达式求值](https://file1.kamacoder.com/i/algo/150.逆波兰表达式求值.gif) 相信看完动画大家应该知道,这和[1047. 删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)是差不多的,只不过本题不要相邻元素做消除了,而是做运算! diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" old mode 100644 new mode 100755 diff --git "a/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" "b/problems/0160.\347\233\270\344\272\244\351\223\276\350\241\250.md" old mode 100644 new mode 100755 diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" old mode 100644 new mode 100755 index a3fc7ef126..350533d8c8 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -132,7 +132,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://file.kamacoder.com/pics/20201229100358221.png) +![188.买卖股票的最佳时机IV](https://file1.kamacoder.com/i/algo/20201229100358221.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" old mode 100644 new mode 100755 diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" old mode 100644 new mode 100755 index 0bee40f7cd..7c0aab8ec0 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -87,7 +87,7 @@ for (int i = 2; i < nums.size(); i++) { 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://file.kamacoder.com/pics/20210221170954115.jpg) +![198.打家劫舍](https://file1.kamacoder.com/i/algo/20210221170954115.jpg) 红框dp[nums.size() - 1]为结果。 diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" old mode 100644 new mode 100755 index 9ea47329bb..7ae44b5222 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -13,7 +13,7 @@ 此外,你可以假设该网格的四条边均被水包围。 -![](https://file.kamacoder.com/pics/20220726093256.png) +![](https://file1.kamacoder.com/i/algo/20220726093256.png) 提示: @@ -28,7 +28,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://file.kamacoder.com/pics/20220726094200.png) +![图一](https://file1.kamacoder.com/i/algo/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -48,7 +48,7 @@ 如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 -![图二](https://file.kamacoder.com/pics/20220727100846.png) +![图二](https://file1.kamacoder.com/i/algo/20220727100846.png) 超时写法 (从队列中取出节点再标记) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" old mode 100644 new mode 100755 index a015399859..128007bb62 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -12,7 +12,7 @@ 此外,你可以假设该网格的四条边均被水包围。 -![](https://file.kamacoder.com/pics/20220726093256.png) +![](https://file1.kamacoder.com/i/algo/20220726093256.png) 提示: @@ -27,7 +27,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://file.kamacoder.com/pics/20220726094200.png) +![图一](https://file1.kamacoder.com/i/algo/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" old mode 100644 new mode 100755 diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" old mode 100644 new mode 100755 index 5ecf89bf19..ffe04a5b07 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -34,11 +34,11 @@ 这里以链表 1 4 2 4 来举例,移除元素4。 -![203_链表删除元素1](https://file.kamacoder.com/pics/20210316095351161.png) +![203_链表删除元素1](https://file1.kamacoder.com/i/algo/20210316095351161.png) 如果使用C,C++编程语言的话,不要忘了还要从内存中删除这两个移除的节点, 清理节点内存之后如图: -![203_链表删除元素2](https://file.kamacoder.com/pics/20210316095418280.png) +![203_链表删除元素2](https://file1.kamacoder.com/i/algo/20210316095418280.png) **当然如果使用java ,python的话就不用手动管理内存了。** @@ -56,16 +56,16 @@ 来看第一种操作:直接使用原来的链表来进行移除。 -![203_链表删除元素3](https://file.kamacoder.com/pics/2021031609544922.png) +![203_链表删除元素3](https://file1.kamacoder.com/i/algo/2021031609544922.png) 移除头结点和移除其他节点的操作是不一样的,因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点。 所以头结点如何移除呢,其实只要将头结点向后移动一位就可以,这样就从链表中移除了一个头结点。 -![203_链表删除元素4](https://file.kamacoder.com/pics/20210316095512470.png) +![203_链表删除元素4](https://file1.kamacoder.com/i/algo/20210316095512470.png) 依然别忘将原头结点从内存中删掉。 -![203_链表删除元素5](https://file.kamacoder.com/pics/20210316095543775.png) +![203_链表删除元素5](https://file1.kamacoder.com/i/algo/20210316095543775.png) 这样移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。 @@ -76,7 +76,7 @@ 来看看如何设置一个虚拟头。依然还是在这个链表中,移除元素1。 -![203_链表删除元素6](https://file.kamacoder.com/pics/20210316095619221.png) +![203_链表删除元素6](https://file1.kamacoder.com/i/algo/20210316095619221.png) 这里来给链表添加一个虚拟头结点为新的头结点,此时要移除这个旧头结点元素1。 diff --git "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" old mode 100644 new mode 100755 diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" old mode 100644 new mode 100755 index 4e33342a67..e49037dd2b --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -27,7 +27,7 @@ 其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: -![206_反转链表](https://file.kamacoder.com/pics/20210218090901207.png) +![206_反转链表](https://file1.kamacoder.com/i/algo/20210218090901207.png) 之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 @@ -35,7 +35,7 @@ 我们拿有示例中的链表来举例,如动画所示:(纠正:动画应该是先移动pre,在移动cur) -![](https://code-thinking.cdn.bcebos.com/gifs/206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.gif) +![](https://file1.kamacoder.com/i/algo/206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.gif) 首先定义一个cur指针,指向头结点,再定义一个pre指针,初始化为null。 diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" old mode 100644 new mode 100755 index ca24bc4234..40917f9b8e --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -84,7 +84,7 @@ public: 这里还是以题目中的示例来举例,s=7, 数组是 2,3,1,2,4,3,来看一下查找的过程: -![209.长度最小的子数组](https://code-thinking.cdn.bcebos.com/gifs/209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.gif) +![209.长度最小的子数组](https://file1.kamacoder.com/i/algo/209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.gif) 最后找到 4,3 是最短距离。 @@ -104,7 +104,7 @@ public: 解题的关键在于 窗口的起始位置如何移动,如图所示: -![leetcode_209](https://file.kamacoder.com/pics/20210312160441942.png) +![leetcode_209](https://file1.kamacoder.com/i/algo/20210312160441942.png) 可以发现**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)暴力解法降为O(n)。** diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" old mode 100644 new mode 100755 index 8fceb0a91e..6f2fdd0610 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -42,15 +42,15 @@ * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://file.kamacoder.com/pics/20210129160748643-20230310134000692.jpg) +![213.打家劫舍II](https://file1.kamacoder.com/i/algo/20210129160748643-20230310134000692.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://file.kamacoder.com/pics/20210129160821374-20230310134003961.jpg) +![213.打家劫舍II1](https://file1.kamacoder.com/i/algo/20210129160821374-20230310134003961.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://file.kamacoder.com/pics/20210129160842491-20230310134008133.jpg) +![213.打家劫舍II2](https://file1.kamacoder.com/i/algo/20210129160842491-20230310134008133.jpg) **注意我这里用的是"考虑"**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素! 对于情况三,取nums[1] 和 nums[3]就是最大的。 diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" old mode 100644 new mode 100755 index e23be78d5e..2da0372b1a --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -45,7 +45,7 @@ 选取过程如图: -![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975.png) +![216.组合总和III](https://file1.kamacoder.com/i/algo/20201123195717975.png) 图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。 @@ -108,7 +108,7 @@ if (path.size() == k) { 本题和[77. 组合](https://programmercarl.com/0077.组合.html)区别之一就是集合固定的就是9个数[1,...,9],所以for循环固定i<=9 如图: -![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975-20230310113546003.png) +![216.组合总和III](https://file1.kamacoder.com/i/algo/20201123195717975-20230310113546003.png) 处理过程就是 path收集每次选取的元素,相当于树型结构里的边,sum来统计path里元素的总和。 @@ -166,7 +166,7 @@ public: 这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。 如图: -![216.组合总和III1](https://file.kamacoder.com/pics/2020112319580476.png) +![216.组合总和III1](https://file1.kamacoder.com/i/algo/2020112319580476.png) 已选元素总和如果已经大于n(图中数值为4)了,那么往后遍历就没有意义了,直接剪掉。 diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" old mode 100644 new mode 100755 index 37ae7819aa..eaf4eab2c9 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -153,7 +153,7 @@ public: 我来举一个典型的例子如题: - + 完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。 @@ -162,10 +162,10 @@ public: 对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。 完全二叉树(一)如图: -![222.完全二叉树的节点个数](https://file.kamacoder.com/pics/20201124092543662.png) +![222.完全二叉树的节点个数](https://file1.kamacoder.com/i/algo/20201124092543662.png) 完全二叉树(二)如图: -![222.完全二叉树的节点个数1](https://file.kamacoder.com/pics/20201124092634138.png) +![222.完全二叉树的节点个数1](https://file1.kamacoder.com/i/algo/20201124092634138.png) 可以看出如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量。 @@ -173,15 +173,15 @@ public: 在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。如图: -![](https://file.kamacoder.com/pics/20220829163554.png) +![](https://file1.kamacoder.com/i/algo/20220829163554.png) 在完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树,如图: -![](https://file.kamacoder.com/pics/20220829163709.png) +![](https://file1.kamacoder.com/i/algo/20220829163709.png) 那有录友说了,这种情况,递归向左遍历的深度等于递归向右遍历的深度,但也不是满二叉树,如题: -![](https://file.kamacoder.com/pics/20220829163811.png) +![](https://file1.kamacoder.com/i/algo/20220829163811.png) 如果这么想,大家就是对 完全二叉树理解有误区了,**以上这棵二叉树,它根本就不是一个完全二叉树**! diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" old mode 100644 new mode 100755 index 2396858056..72dfd2aacf --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -60,7 +60,7 @@ queue.pop(); queue.empty(); ``` -![225.用队列实现栈](https://code-thinking.cdn.bcebos.com/gifs/225.用队列实现栈.gif) +![225.用队列实现栈](https://file1.kamacoder.com/i/algo/225.用队列实现栈.gif) 详细如代码注释所示: diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" old mode 100644 new mode 100755 index 248a28a4d5..67a1a59338 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -10,7 +10,7 @@ 翻转一棵二叉树。 -![226.翻转二叉树](https://file.kamacoder.com/pics/20210203192644329.png) +![226.翻转二叉树](https://file1.kamacoder.com/i/algo/20210203192644329.png) 这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,全当一个乐子哈) @@ -35,7 +35,7 @@ 如果要从整个树来看,翻转还真的挺复杂,整个树以中间分割线进行翻转,如图: -![226.翻转二叉树1](https://file.kamacoder.com/pics/20210203192724351.png) +![226.翻转二叉树1](https://file1.kamacoder.com/i/algo/20210203192724351.png) 可以发现想要翻转它,其实就把每一个节点的左右孩子交换一下就可以了。 @@ -55,7 +55,7 @@ 我们下文以前序遍历为例,通过动画来看一下翻转的过程: -![翻转二叉树](https://code-thinking.cdn.bcebos.com/gifs/%E7%BF%BB%E8%BD%AC%E4%BA%8C%E5%8F%89%E6%A0%91.gif) +![翻转二叉树](https://file1.kamacoder.com/i/algo/%E7%BF%BB%E8%BD%AC%E4%BA%8C%E5%8F%89%E6%A0%91.gif) 我们来看一下递归三部曲: diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" old mode 100644 new mode 100755 index 6775a37265..56698e023f --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -57,7 +57,7 @@ queue.pop();**注意此时的输出栈的操作** queue.pop(); queue.empty(); -![232.用栈实现队列版本2](https://code-thinking.cdn.bcebos.com/gifs/232.用栈实现队列版本2.gif) +![232.用栈实现队列版本2](https://file1.kamacoder.com/i/algo/232.用栈实现队列版本2.gif) 在push数据的时候,只要数据放进输入栈就好,**但在pop的时候,操作就复杂一些,输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入)**,再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。 diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" old mode 100644 new mode 100755 index f493383967..6248861d94 --- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" +++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" @@ -87,7 +87,7 @@ public: 如图所示: - + 代码如下: diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" old mode 100644 new mode 100755 index 98cc5b7da8..a1fe78d169 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -14,7 +14,7 @@ 例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5] -![235. 二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20201018172243602.png) +![235. 二叉搜索树的最近公共祖先](https://file1.kamacoder.com/i/algo/20201018172243602.png) 示例 1: @@ -52,7 +52,7 @@ 如图,我们从根节点搜索,第一次遇到 cur节点是数值在[q, p]区间中,即 节点5,此时可以说明 q 和 p 一定分别存在于 节点 5的左子树,和右子树中。 -![235.二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20220926164214.png) +![235.二叉搜索树的最近公共祖先](https://file1.kamacoder.com/i/algo/20220926164214.png) 此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为p的祖先, 如果从节点5继续向右遍历则错过成为q的祖先。 @@ -64,7 +64,7 @@ 如图所示:p为节点6,q为节点9 -![235.二叉搜索树的最近公共祖先2](https://file.kamacoder.com/pics/20220926165141.png) +![235.二叉搜索树的最近公共祖先2](https://file1.kamacoder.com/i/algo/20220926165141.png) 可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回! diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" old mode 100644 new mode 100755 index 537d624084..5044e3ba00 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -16,7 +16,7 @@ 例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4] -![236. 二叉树的最近公共祖先](https://file.kamacoder.com/pics/20201016173414722.png) +![236. 二叉树的最近公共祖先](https://file1.kamacoder.com/i/algo/20201016173414722.png) 示例 1: 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 @@ -51,7 +51,7 @@ **首先最容易想到的一个情况:如果找到一个节点,发现左子树出现结点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。** 即情况一: -![](https://file.kamacoder.com/pics/20220922173502.png) +![](https://file1.kamacoder.com/i/algo/20220922173502.png) 判断逻辑是 如果递归遍历遇到q,就将q返回,遇到p 就将p返回,那么如果 左右子树的返回值都不为空,说明此时的中节点,一定是q 和p 的最近祖先。 @@ -61,7 +61,7 @@ **但是很多人容易忽略一个情况,就是节点本身p(q),它拥有一个子孙节点q(p)。** 情况二: -![](https://file.kamacoder.com/pics/20220922173530.png) +![](https://file1.kamacoder.com/i/algo/20220922173530.png) 其实情况一 和 情况二 代码实现过程都是一样的,也可以说,实现情况一的逻辑,顺便包含了情况二。 @@ -129,7 +129,7 @@ left与right的逻辑处理; // 中 如图: -![236.二叉树的最近公共祖先](https://file.kamacoder.com/pics/2021020415105872.png) +![236.二叉树的最近公共祖先](https://file1.kamacoder.com/i/algo/2021020415105872.png) 就像图中一样直接返回7。 @@ -162,7 +162,7 @@ TreeNode* right = lowestCommonAncestor(root->right, p, q); 如图: -![236.二叉树的最近公共祖先1](https://file.kamacoder.com/pics/20210204151125844.png) +![236.二叉树的最近公共祖先1](https://file1.kamacoder.com/i/algo/20210204151125844.png) 图中节点10的左子树返回null,右子树返回目标值7,那么此时节点10的处理逻辑就是把右子树的返回值(最近公共祖先7)返回上去! @@ -183,7 +183,7 @@ else { // (left == NULL && right == NULL) 那么寻找最小公共祖先,完整流程图如下: -![236.二叉树的最近公共祖先2](https://file.kamacoder.com/pics/202102041512582.png) +![236.二叉树的最近公共祖先2](https://file1.kamacoder.com/i/algo/202102041512582.png) **从图中,大家可以看到,我们是如何回溯遍历整棵二叉树,将结果返回给头结点的!** diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" old mode 100644 new mode 100755 index 875f1bd193..5ea810104d --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -18,7 +18,7 @@ 你能在线性时间复杂度内解决此题吗? - + 提示: @@ -82,7 +82,7 @@ public: 动画如下: -![239.滑动窗口最大值](https://code-thinking.cdn.bcebos.com/gifs/239.滑动窗口最大值.gif) +![239.滑动窗口最大值](https://file1.kamacoder.com/i/algo/239.滑动窗口最大值.gif) 对于窗口里的元素{2, 3, 5, 1 ,4},单调队列里只维护{5, 4} 就够了,保持单调队列里单调递减,此时队列出口元素就是窗口里最大元素。 @@ -98,7 +98,7 @@ public: 为了更直观的感受到单调队列的工作过程,以题目示例为例,输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3,动画如下: -![239.滑动窗口最大值-2](https://code-thinking.cdn.bcebos.com/gifs/239.滑动窗口最大值-2.gif) +![239.滑动窗口最大值-2](https://file1.kamacoder.com/i/algo/239.滑动窗口最大值-2.gif) 那么我们用什么数据结构来实现这个单调队列呢? diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" old mode 100644 new mode 100755 index 9a783e5b11..0a37ea26cc --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -42,7 +42,7 @@ 操作动画如下: -![242.有效的字母异位词](https://code-thinking.cdn.bcebos.com/gifs/242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.gif) +![242.有效的字母异位词](https://file1.kamacoder.com/i/algo/242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.gif) 定义一个数组叫做record用来上记录字符串s里字符出现的次数。 diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" old mode 100644 new mode 100755 index 5d71351172..4a66c816bc --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -14,7 +14,7 @@ 说明: 叶子节点是指没有子节点的节点。 示例: -![257.二叉树的所有路径1](https://file.kamacoder.com/pics/2021020415161576.png) +![257.二叉树的所有路径1](https://file1.kamacoder.com/i/algo/2021020415161576.png) ## 算法公开课 @@ -28,7 +28,7 @@ 前序遍历以及回溯的过程如图: -![257.二叉树的所有路径](https://file.kamacoder.com/pics/20210204151702443.png) +![257.二叉树的所有路径](https://file1.kamacoder.com/i/algo/20210204151702443.png) 我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。** @@ -315,7 +315,7 @@ public: 其实关键还在于 参数,使用的是 `string path`,这里并没有加上引用`&` ,即本层递归中,path + 该节点数值,但该层递归结束,上一层path的数值并不会受到任何影响。 如图所示: -![](https://file.kamacoder.com/pics/20220831173322.png) +![](https://file1.kamacoder.com/i/algo/20220831173322.png) 节点4 的path,在遍历到节点3,path+3,遍历节点3的递归结束之后,返回节点4(回溯的过程),path并不会把3加上。 diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" old mode 100644 new mode 100755 index 8171a409a0..7c5d7c9c9f --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -93,7 +93,7 @@ for (int i = 0; i <= n; i++) { // 遍历背包 已输入n为5例,dp状态图如下: -![279.完全平方数](https://file.kamacoder.com/pics/20210202112617341.jpg) +![279.完全平方数](https://file1.kamacoder.com/i/algo/20210202112617341.jpg) dp[0] = 0 dp[1] = min(dp[0] + 1) = 1 diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" old mode 100644 new mode 100755 index d7911054e4..e25525684e --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -32,7 +32,7 @@ 如动画所示: -![移动零](https://code-thinking.cdn.bcebos.com/gifs/283.%E7%A7%BB%E5%8A%A8%E9%9B%B6.gif) +![移动零](https://file1.kamacoder.com/i/algo/283.%E7%A7%BB%E5%8A%A8%E9%9B%B6.gif) C++代码如下: diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index de37ed5cc8..06adfd950d --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -85,7 +85,7 @@ for (int i = 1; i < nums.size(); i++) { 输入:[0,1,0,3,2],dp数组的变化如下: -![300.最长上升子序列](https://file.kamacoder.com/pics/20210110170945618.jpg) +![300.最长上升子序列](https://file1.kamacoder.com/i/algo/20210110170945618.jpg) 如果代码写出来,但一直AC不了,那么就把dp数组打印出来,看看对不对! diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" old mode 100644 new mode 100755 index 599a1f42e1..d396e521b3 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -47,7 +47,7 @@ dp[i][j],第i天状态为j,所剩的最多现金为dp[i][j]。 * 状态三:今天卖出股票 * 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天! -![](https://file.kamacoder.com/pics/518d5baaf33f4b2698064f8efb42edbf.png) +![](https://file1.kamacoder.com/i/algo/518d5baaf33f4b2698064f8efb42edbf.png) j的状态为: @@ -136,7 +136,7 @@ dp[i][3] = dp[i - 1][2]; 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://file.kamacoder.com/pics/2021032317451040.png) +![309.最佳买卖股票时机含冷冻期](https://file1.kamacoder.com/i/algo/2021032317451040.png) 最后结果是取 状态二,状态三,和状态四的最大值,不少同学会把状态四忘了,状态四是冷冻期,最后一天如果是冷冻期也可能是最大值。 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" old mode 100644 new mode 100755 index 7f3bc1e4b1..f3a0a07dd9 --- "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" @@ -104,7 +104,7 @@ dp[0] = 0; 以输入:coins = [1, 2, 5], amount = 5为例 -![322.零钱兑换](https://file.kamacoder.com/pics/20210201111833906.jpg) +![322.零钱兑换](https://file1.kamacoder.com/i/algo/20210201111833906.jpg) dp[amount]为最终结果。 diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" old mode 100644 new mode 100755 index fcdb6a33ed..1168277a8d --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -57,7 +57,7 @@ 对于死循环,我来举一个有重复机场的例子: -![332.重新安排行程](https://file.kamacoder.com/pics/20201115180537865.png) +![332.重新安排行程](https://file1.kamacoder.com/i/algo/20201115180537865.png) 为什么要举这个例子呢,就是告诉大家,出发机场和到达机场也会重复的,**如果在解题的过程中没有对集合元素处理好,就会死循环。** @@ -111,7 +111,7 @@ void backtracking(参数) { 本题以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![332.重新安排行程1](https://file.kamacoder.com/pics/2020111518065555-20230310121223600.png) +![332.重新安排行程1](https://file1.kamacoder.com/i/algo/2020111518065555-20230310121223600.png) 开始回溯三部曲讲解: @@ -137,7 +137,7 @@ bool backtracking(int ticketNum, vector& result) { 因为我们只需要找到一个行程,就是在树形结构中唯一的一条通向叶子节点的路线,如图: -![332.重新安排行程1](https://file.kamacoder.com/pics/2020111518065555-20230310121240991.png) +![332.重新安排行程1](https://file1.kamacoder.com/i/algo/2020111518065555-20230310121240991.png) 所以找到了这个叶子节点了直接返回,这个递归函数的返回值问题我们在讲解二叉树的系列的时候,在这篇[二叉树:递归函数究竟什么时候需要返回值,什么时候不要返回值?](https://programmercarl.com/0112.路径总和.html)详细介绍过。 diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" old mode 100644 new mode 100755 index 4916af4c26..44af86bb4c --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -12,7 +12,7 @@ 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 -![337.打家劫舍III](https://file.kamacoder.com/pics/20210223173849619.png) +![337.打家劫舍III](https://file1.kamacoder.com/i/algo/20210223173849619.png) ## 算法公开课 @@ -177,7 +177,7 @@ return {val2, val1}; 以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**) -![](https://file.kamacoder.com/pics/20230203110031.png) +![](https://file1.kamacoder.com/i/algo/20230203110031.png) **最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。 diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" old mode 100644 new mode 100755 index 203c422879..c9467e361f --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -127,7 +127,7 @@ for (int i = 3; i <= n ; i++) { 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://file.kamacoder.com/pics/20210104173021581.png) +![343.整数拆分](https://file1.kamacoder.com/i/algo/20210104173021581.png) 以上动规五部曲分析完毕,C++代码如下: diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" old mode 100644 new mode 100755 index c88d008c82..cadb31c97b --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -69,7 +69,7 @@ 以字符串`hello`为例,过程如下: -![344.反转字符串](https://code-thinking.cdn.bcebos.com/gifs/344.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.gif) +![344.反转字符串](https://file1.kamacoder.com/i/algo/344.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.gif) 不难写出如下C++代码: diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" old mode 100644 new mode 100755 index b6575c5fd4..fa7d6155a5 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -70,7 +70,7 @@ 寻找前k个最大元素流程如图所示:(图中的频率只有三个,所以正好构成一个大小为3的小顶堆,如果频率更多一些,则用这个小顶堆进行扫描) -![347.前K个高频元素](https://code-thinking.cdn.bcebos.com/pics/347.前K个高频元素.jpg) +![347.前K个高频元素](https://file1.kamacoder.com/i/algo/347.前K个高频元素.jpg) 我们来看一下C++代码: diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" old mode 100644 new mode 100755 index 65d22a809c..77e895da61 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -14,7 +14,7 @@ 题意:给定两个数组,编写一个函数来计算它们的交集。 -![349. 两个数组的交集](https://file.kamacoder.com/pics/20200818193523911.png) +![349. 两个数组的交集](https://file1.kamacoder.com/i/algo/20200818193523911.png) **说明:** 输出结果中的每个元素一定是唯一的。 @@ -51,7 +51,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 思路如图所示: -![set哈希法](https://file.kamacoder.com/pics/20220707173513.png) +![set哈希法](https://file1.kamacoder.com/i/algo/20220707173513.png) C++代码如下: diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index 50934981a6..1be9cb4178 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -46,7 +46,7 @@ 用示例二来举例,如图所示: -![376.摆动序列](https://file.kamacoder.com/pics/20201124174327597.png) +![376.摆动序列](https://file1.kamacoder.com/i/algo/20201124174327597.png) **局部最优:删除单调坡度上的节点(不包括单调坡度两端的节点),那么这个坡度就可以有两个局部峰值**。 @@ -72,13 +72,13 @@ 例如 [1,2,2,2,2,1]这样的数组,如图: -![](https://file.kamacoder.com/pics/20230106170449.png) +![](https://file1.kamacoder.com/i/algo/20230106170449.png) 它的摇摆序列长度是多少呢? **其实是长度是 3**,也就是我们在删除的时候 要不删除左面的三个 2,要不就删除右边的三个 2。 如图,可以统一规则,删除左边的三个 2: -![](https://file.kamacoder.com/pics/20230106172613.png) +![](https://file1.kamacoder.com/i/algo/20230106172613.png) 在图中,当 i 指向第一个 2 的时候,`prediff > 0 && curdiff = 0` ,当 i 指向最后一个 2 的时候 `prediff = 0 && curdiff < 0`。 @@ -106,7 +106,7 @@ 那么为了规则统一,针对序列[2,5],可以假设为[2,2,5],这样它就有坡度了即 preDiff = 0,如图: -![376.摆动序列1](https://file.kamacoder.com/pics/20201124174357612.png) +![376.摆动序列1](https://file1.kamacoder.com/i/algo/20201124174357612.png) 针对以上情形,result 初始为 1(默认最右面有一个峰值),此时 curDiff > 0 && preDiff <= 0,那么 result++(计算了左面的峰值),最后得到的 result 就是 2(峰值个数为 2 即摆动序列长度为 2) @@ -145,7 +145,7 @@ public: 在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: -![](https://file.kamacoder.com/pics/20230108171505.png) +![](https://file1.kamacoder.com/i/algo/20230108171505.png) 图中,我们可以看出,版本一的代码在三个地方记录峰值,但其实结果因为是 2,因为 单调中的平坡 不能算峰值(即摆动)。 @@ -184,7 +184,7 @@ public: **本题异常情况的本质,就是要考虑平坡**, 平坡分两种,一个是 上下中间有平坡,一个是单调有平坡,如图: -![](https://file.kamacoder.com/pics/20230108174452.png) +![](https://file1.kamacoder.com/i/algo/20230108174452.png) ### 思路 2(动态规划) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" old mode 100644 new mode 100755 index 20a94331c4..ab92f24aef --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -103,7 +103,7 @@ dp[i](考虑nums[j])可以由 dp[i - nums[j]](不考虑nums[j]) 推导 我们再来用示例中的例子推导一下: -![377.组合总和Ⅳ](https://file.kamacoder.com/pics/20230310000625.png) +![377.组合总和Ⅳ](https://file1.kamacoder.com/i/algo/20230310000625.png) 如果代码运行处的结果不是想要的结果,就把dp[i]都打出来,看看和我们推导的一不一样。 diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" old mode 100644 new mode 100755 diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index d59b7bc121..bf2d959682 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -80,7 +80,7 @@ if (s[i - 1] != t[j - 1]),此时相当于t要删除元素,t如果把当前 因为这样的定义在dp二维矩阵中可以留出初始化的区间,如图: -![392.判断子序列](https://file.kamacoder.com/pics/20210303173115966.png) +![392.判断子序列](https://file1.kamacoder.com/i/algo/20210303173115966.png) 如果要是定义的dp[i][j]是以下标i为结尾的字符串s和以下标j为结尾的字符串t,初始化就比较麻烦了。 @@ -98,14 +98,14 @@ vector> dp(s.size() + 1, vector(t.size() + 1, 0)); 如图所示: -![392.判断子序列1](https://file.kamacoder.com/pics/20210303172354155.jpg) +![392.判断子序列1](https://file1.kamacoder.com/i/algo/20210303172354155.jpg) 5. 举例推导dp数组 以示例一为例,输入:s = "abc", t = "ahbgdc",dp状态转移图如下: -![392.判断子序列2](https://file.kamacoder.com/pics/2021030317364166.jpg) +![392.判断子序列2](https://file1.kamacoder.com/i/algo/2021030317364166.jpg) dp[i][j]表示以下标i-1为结尾的字符串s和以下标j-1为结尾的字符串t 相同子序列的长度,所以如果dp[s.size()][t.size()] 与 字符串s的长度相同说明:s与t的最长相同子序列就是s,那么s 就是 t 的子序列。 diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" old mode 100644 new mode 100755 index 69723815a1..10b159b181 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -12,7 +12,7 @@ 示例: -![404.左叶子之和1](https://file.kamacoder.com/pics/20210204151927654.png) +![404.左叶子之和1](https://file1.kamacoder.com/i/algo/20210204151927654.png) ## 算法公开课 @@ -26,12 +26,12 @@ 大家思考一下如下图中二叉树,左叶子之和究竟是多少? -![404.左叶子之和](https://file.kamacoder.com/pics/20210204151949672.png) +![404.左叶子之和](https://file1.kamacoder.com/i/algo/20210204151949672.png) **其实是0,因为这棵树根本没有左叶子!** 但看这个图的左叶子之和是多少? -![图二](https://file.kamacoder.com/pics/20220902165805.png) +![图二](https://file1.kamacoder.com/i/algo/20220902165805.png) 相信通过这两个图,大家对最左叶子的定义有明确理解了。 diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" old mode 100644 new mode 100755 index 0d060ee837..ce9d3bfb4e --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -61,7 +61,7 @@ 以图中{5,2} 为例: -![406.根据身高重建队列](https://file.kamacoder.com/pics/20201216201851982.png) +![406.根据身高重建队列](https://file1.kamacoder.com/i/algo/20201216201851982.png) 按照身高排序之后,优先按身高高的people的k来插入,后序插入节点也不会影响前面已经插入的节点,最终按照k的规则完成了队列。 diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" old mode 100644 new mode 100755 index 79b4d4f75a..75bc5d0e10 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -155,7 +155,7 @@ dp[j]的数值一定是小于等于j的。 用例1,输入[1,5,11,5] 为例,如图: -![416.分割等和子集2](https://file.kamacoder.com/pics/20210110104240545.png) +![416.分割等和子集2](https://file1.kamacoder.com/i/algo/20210110104240545.png) 最后dp[11] == 11,说明可以将这个数组分割成两个子集,使得两个子集的元素和相等。 diff --git "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" old mode 100644 new mode 100755 index 116cd08e09..c9494313a1 --- "a/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/0417.\345\244\252\345\271\263\346\264\213\345\244\247\350\245\277\346\264\213\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -18,7 +18,7 @@ 示例 1: -![](https://file.kamacoder.com/pics/20230129103212.png) +![](https://file1.kamacoder.com/i/algo/20230129103212.png) * 输入: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] * 输出: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] @@ -130,11 +130,11 @@ public: 从太平洋边上节点出发,如图: -![图一](https://file.kamacoder.com/pics/20220722103029.png) +![图一](https://file1.kamacoder.com/i/algo/20220722103029.png) 从大西洋边上节点出发,如图: -![图二](https://file.kamacoder.com/pics/20220722103330.png) +![图二](https://file1.kamacoder.com/i/algo/20220722103330.png) 按照这样的逻辑,就可以写出如下遍历代码:(详细注释) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" old mode 100644 new mode 100755 index 04845ea7c3..4231a8ee90 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -44,7 +44,7 @@ 这里记录非交叉区间的个数还是有技巧的,如图: -![](https://file.kamacoder.com/pics/20230201164134.png) +![](https://file1.kamacoder.com/i/algo/20230201164134.png) 区间,1,2,3,4,5,6都按照右边界排好序。 diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" old mode 100644 new mode 100755 index 406116a388..44575b8aff --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -20,7 +20,7 @@ 示例: -![450.删除二叉搜索树中的节点](https://file.kamacoder.com/pics/20201020171048265.png) +![450.删除二叉搜索树中的节点](https://file1.kamacoder.com/i/algo/20201020171048265.png) ## 算法公开课 @@ -67,7 +67,7 @@ if (root == nullptr) return root; 第五种情况有点难以理解,看下面动画: -![450.删除二叉搜索树中的节点](https://code-thinking.cdn.bcebos.com/gifs/450.%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.gif) +![450.删除二叉搜索树中的节点](https://file1.kamacoder.com/i/algo/450.%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.gif) 动画中的二叉搜索树中,删除元素7, 那么删除节点(元素7)的左孩子就是5,删除节点(元素7)的右子树的最左面节点是元素8。 diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" old mode 100644 new mode 100755 index 17d21cd1c4..76de3f93a2 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -76,7 +76,7 @@ 以题目示例: [[10,16],[2,8],[1,6],[7,12]]为例,如图:(方便起见,已经排序) -![452.用最少数量的箭引爆气球](https://file.kamacoder.com/pics/20201123101929791.png) +![452.用最少数量的箭引爆气球](https://file1.kamacoder.com/i/algo/20201123101929791.png) 可以看出首先第一组重叠气球,一定是需要一个箭,气球3,的左边界大于了 第一组重叠气球的最小右边界,所以再需要一支箭来射气球3了。 diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" old mode 100644 new mode 100755 diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" old mode 100644 new mode 100755 index 2a6ade1b64..2c38ab9ec1 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -46,7 +46,7 @@ 如图: -![](https://file.kamacoder.com/pics/20230405225628.png) +![](https://file1.kamacoder.com/i/algo/20230405225628.png) 这个例子可以看出饼干 9 只有喂给胃口为 7 的小孩,这样才是整体最优解,并想不出反例,那么就可以撸代码了。 @@ -89,7 +89,7 @@ public: 如果 for 控制的是饼干, if 控制胃口,就是出现如下情况 : -![](https://file.kamacoder.com/pics/20230112102848.png) +![](https://file1.kamacoder.com/i/algo/20230112102848.png) if 里的 index 指向 胃口 10, for 里的 i 指向饼干 9,因为 饼干 9 满足不了 胃口 10,所以 i 持续向前移动,而 index 走不到` s[index] >= g[i]` 的逻辑,所以 index 不会移动,那么当 i 持续向前移动,最后所有的饼干都匹配不上。 diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" old mode 100644 new mode 100755 index 627a27a48e..d164277731 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -46,13 +46,13 @@ 当一个字符串s:abcabc,内部由重复的子串组成,那么这个字符串的结构一定是这样的: -![图一](https://file.kamacoder.com/pics/20220728104518.png) +![图一](https://file1.kamacoder.com/i/algo/20220728104518.png) 也就是由前后相同的子串组成。 那么既然前面有相同的子串,后面有相同的子串,用 s + s,这样组成的字符串中,后面的子串做前串,前面的子串做后串,就一定还能组成一个s,如图: -![图二](https://file.kamacoder.com/pics/20220728104931.png) +![图二](https://file1.kamacoder.com/i/algo/20220728104931.png) 当然,我们在判断 s + s 拼接的字符串里是否出现一个s的的时候,**要刨除 s + s 的首字符和尾字符**,这样避免在s+s中搜索出原来的s,我们要搜索的是中间拼接出来的s。 @@ -64,11 +64,11 @@ 如图,字符串s,图中数字为数组下标,在 s + s 拼接后, 不算首尾字符,中间凑成s字符串。 (图中数字为数组下标) -![](https://file.kamacoder.com/pics/20240910115555.png) +![](https://file1.kamacoder.com/i/algo/20240910115555.png) 图中,因为中间拼接成了s,根据红色框 可以知道 s[4] = s[0], s[5] = s[1], s[0] = s[2], s[1] = s[3] s[2] = s[4] ,s[3] = s[5] -![](https://file.kamacoder.com/pics/20240910115819.png) +![](https://file1.kamacoder.com/i/algo/20240910115819.png) 以上相等关系我们串联一下: @@ -83,7 +83,7 @@ s[5] = s[1] = s[3] 这里可以有录友想,凭什么就是这样组成的s呢,我换一个方式组成s 行不行,如图: -![](https://file.kamacoder.com/pics/20240910120751.png) +![](https://file1.kamacoder.com/i/algo/20240910120751.png) s[3] = s[0],s[4] = s[1] ,s[5] = s[2],s[0] = s[3],s[1] = s[4],s[2] = s[5] @@ -101,7 +101,7 @@ s[0] s[1] s[2] = s[3] s[4] s[5] 如果是这样的呢,如图: -![](https://file.kamacoder.com/pics/20240910121236.png) +![](https://file1.kamacoder.com/i/algo/20240910121236.png) s[1] = s[0],s[2] = s[1] ,s[3] = s[2],s[4] = s[3],s[5] = s[4],s[0] = s[5] @@ -165,23 +165,23 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 那么相同前后缀可以是这样: -![](https://file.kamacoder.com/pics/20240913110257.png) +![](https://file1.kamacoder.com/i/algo/20240913110257.png) 也可以是这样: -![](https://file.kamacoder.com/pics/20240913110316.png) +![](https://file1.kamacoder.com/i/algo/20240913110316.png) 最长的相等前后缀,也就是这样: -![](https://file.kamacoder.com/pics/20240913110841.png) +![](https://file1.kamacoder.com/i/algo/20240913110841.png) 这里有录友就想:如果字符串s 是由最小重复子串p组成,最长相等前后缀就不能更长一些? 例如这样: -![](https://file.kamacoder.com/pics/20240913114348.png) +![](https://file1.kamacoder.com/i/algo/20240913114348.png) 如果这样的话,因为前后缀要相同,所以 p2 = p1,p3 = p2,如图: -![](https://file.kamacoder.com/pics/20240913114818.png) +![](https://file1.kamacoder.com/i/algo/20240913114818.png) p2 = p1,p3 = p2 即: p1 = p2 = p3 @@ -203,7 +203,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 情况一, 最长相等前后缀不包含的子串的长度 比 字符串s的一半的长度还大,那一定不是字符串s的重复子串,如图: -![](https://file.kamacoder.com/pics/20240911110236.png) +![](https://file1.kamacoder.com/i/algo/20240911110236.png) 图中:前后缀不包含的子串的长度 大于 字符串s的长度的 二分之一 @@ -211,7 +211,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 情况二,最长相等前后缀不包含的子串的长度 可以被 字符串s的长度整除,如图: -![](https://file.kamacoder.com/pics/20240910174249.png) +![](https://file1.kamacoder.com/i/algo/20240910174249.png) 步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,所以 s[0] 一定和 s[2]相同,s[1] 一定和 s[3]相同,即:,s[0]s[1]与s[2]s[3]相同 。 @@ -234,7 +234,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 那么它的最长相同前后缀,就不是上图中的前后缀,而是这样的的前后缀: -![](https://file.kamacoder.com/pics/20240910175053.png) +![](https://file1.kamacoder.com/i/algo/20240910175053.png) 录友可能再问,由一个字符组成的字符串,最长相等前后缀凭什么就是这样的。 @@ -250,7 +250,7 @@ p2 = p1,p3 = p2 即: p1 = p2 = p3 **情况三,最长相等前后缀不包含的子串的长度 不被 字符串s的长度整除得情况**,如图: -![](https://file.kamacoder.com/pics/20240913115854.png) +![](https://file1.kamacoder.com/i/algo/20240913115854.png) 步骤一:因为 这是相等的前缀和后缀,t[0] 与 k[0]相同, t[1] 与 k[1]相同,t[2] 与 k[2]相同。 @@ -301,7 +301,7 @@ next 数组记录的就是最长相同前后缀( [字符串:KMP算法精讲] 如图: -![459.重复的子字符串_1](https://code-thinking.cdn.bcebos.com/pics/459.重复的子字符串_1.png) +![459.重复的子字符串_1](https://file1.kamacoder.com/i/algo/459.重复的子字符串_1.png) `next[len - 1] = 7`,`next[len - 1] + 1 = 8`,8就是此时字符串asdfasdfasdf的最长相同前后缀的长度。 diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" old mode 100644 new mode 100755 index 40ddc57d63..ba60bc4564 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -15,7 +15,7 @@ 岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。 -![](https://file.kamacoder.com/pics/20230829180848.png) +![](https://file1.kamacoder.com/i/algo/20230829180848.png) * 输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] * 输出:16 @@ -49,7 +49,7 @@ 如图: - + C++代码如下:(详细注释) @@ -89,7 +89,7 @@ result = 岛屿数量 * 4 - cover * 2; 如图: - + C++代码如下:(详细注释) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" old mode 100644 new mode 100755 index 8166b39aad..750917de3e --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -51,7 +51,7 @@ 其实本题并不是多重背包,再来看一下这个图,捋清几种背包的关系 -![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407-20230310132423205.png) +![416.分割等和子集1](https://file1.kamacoder.com/i/algo/20210117171307407-20230310132423205.png) 多重背包是每个物品,数量不同的情况。 @@ -127,7 +127,7 @@ for (string str : strs) { // 遍历物品 最后dp数组的状态如下所示: -![474.一和零](https://file.kamacoder.com/pics/20210120111201512.jpg) +![474.一和零](https://file1.kamacoder.com/i/algo/20210120111201512.jpg) 以上动规五部曲分析完毕,C++代码如下: diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index b3171c8a12..5d37737169 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -45,7 +45,7 @@ 为了有鲜明的对比,我用[4, 7, 6, 7]这个数组来举例,抽象为树形结构如图: -![491. 递增子序列1](https://file.kamacoder.com/pics/20201124200229824.png) +![491. 递增子序列1](https://file1.kamacoder.com/i/algo/20201124200229824.png) @@ -79,7 +79,7 @@ if (path.size() > 1) { * 单层搜索逻辑 -![491. 递增子序列1](https://file.kamacoder.com/pics/20201124200229824-20230310131640070.png) +![491. 递增子序列1](https://file1.kamacoder.com/i/algo/20201124200229824-20230310131640070.png) 在图中可以看出,**同一父节点下的同层上使用过的元素就不能再使用了** 那么单层搜索代码如下: diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" old mode 100644 new mode 100755 index a23e1743cb..b161bc57a8 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -163,7 +163,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 先只考虑物品0,如图: -![](https://file.kamacoder.com/pics/20240808161747.png) +![](https://file1.kamacoder.com/i/algo/20240808161747.png) (这里的所有物品,都是题目中的数字1)。 @@ -177,7 +177,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 接下来 考虑 物品0 和 物品1,如图: -![](https://file.kamacoder.com/pics/20240808162052.png) +![](https://file1.kamacoder.com/i/algo/20240808162052.png) 装满背包容量为0 的方法个数是1,即 放0件物品。 @@ -191,7 +191,7 @@ if (abs(target) > sum) return 0; // 此时没有方案 接下来 考虑 物品0 、物品1 和 物品2 ,如图: -![](https://file.kamacoder.com/pics/20240808162533.png) +![](https://file1.kamacoder.com/i/algo/20240808162533.png) 装满背包容量为0 的方法个数是1,即 放0件物品。 @@ -207,17 +207,17 @@ if (abs(target) > sum) return 0; // 此时没有方案 如图红色部分: -![](https://file.kamacoder.com/pics/20240808163312.png) +![](https://file1.kamacoder.com/i/algo/20240808163312.png) dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物品1 和 物品2, 如图所示,三种方法: -![](https://file.kamacoder.com/pics/20240826111946.png) +![](https://file1.kamacoder.com/i/algo/20240826111946.png) **容量为2 的背包,如果不放 物品2 有几种方法呢**? 有 dp[1][2] 种方法,即 背包容量为2,只考虑物品0 和 物品1 ,有 dp[1][2] 种方法,如图: -![](https://file.kamacoder.com/pics/20240826112805.png) +![](https://file1.kamacoder.com/i/algo/20240826112805.png) **容量为2 的背包, 如果放 物品2 有几种方法呢**? @@ -229,7 +229,7 @@ dp[2][2] = 3,即 放物品0 和 放物品1、放物品0 和 物品 2、放物 如图: -![](https://file.kamacoder.com/pics/20240826113043.png) +![](https://file1.kamacoder.com/i/algo/20240826113043.png) 有录友可能疑惑,这里计算的是放满 容量为2的背包 有几种方法,那物品2去哪了? @@ -239,7 +239,7 @@ dp[2][2] = 容量为2的背包不放物品2有几种方法 + 容量为2的背包 所以 dp[2][2] = dp[1][2] + dp[1][1] ,如图: -![](https://file.kamacoder.com/pics/20240826113258.png) +![](https://file1.kamacoder.com/i/algo/20240826113258.png) 以上过程,抽象化如下: @@ -266,11 +266,11 @@ else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; 先明确递推的方向,如图,求解 dp[2][2] 是由 上方和左上方推出。 -![](https://file.kamacoder.com/pics/20240826115800.png) +![](https://file1.kamacoder.com/i/algo/20240826115800.png) 那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: -![](https://file.kamacoder.com/pics/20240827103507.png) +![](https://file1.kamacoder.com/i/algo/20240827103507.png) 关于dp[0][0]的值,在上面的递推公式讲解中已经讲过,装满背包容量为0 的方法数量是1,即 放0件物品。 @@ -323,7 +323,7 @@ for (int i = 0; i < nums.size(); i++) { 例如下图,如果上方没数值,左上方没数值,就无法推出 dp[2][2]。 -![](https://file.kamacoder.com/pics/20240827105427.png) +![](https://file1.kamacoder.com/i/algo/20240827105427.png) 那么是先 从上到下 ,再从左到右遍历,例如这样: @@ -349,11 +349,11 @@ for (int j = 0; j <= bagSize; j++) { // 列,遍历背包 这里我再画图讲一下,以求dp[2][2]为例,当先从上到下,再从左到右遍历,矩阵是这样: -![](https://file.kamacoder.com/pics/20240827110933.png) +![](https://file1.kamacoder.com/i/algo/20240827110933.png) 当先从左到右,再从上到下遍历,矩阵是这样: -![](https://file.kamacoder.com/pics/20240827111013.png) +![](https://file1.kamacoder.com/i/algo/20240827111013.png) 这里大家可以看出,无论是以上哪种遍历,都不影响 dp[2][2]的求值,用来 推导 dp[2][2] 的数值都在。 @@ -366,7 +366,7 @@ bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![](https://file.kamacoder.com/pics/20240827111612.png) +![](https://file1.kamacoder.com/i/algo/20240827111612.png) 这么大的矩阵,我们是可以自己手动模拟出来的。 @@ -445,7 +445,7 @@ bagSize = (target + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![](https://file.kamacoder.com/pics/20210125120743274.jpg) +![](https://file1.kamacoder.com/i/algo/20210125120743274.jpg) 大家可以和 二维dp数组的打印结果做一下对比。 diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" old mode 100644 new mode 100755 diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" old mode 100644 new mode 100755 index 8cca8e65c8..457fd61d24 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -23,7 +23,7 @@ 给定 BST [1,null,2,2], -![501. 二叉搜索树中的众数](https://file.kamacoder.com/pics/20201014221532206.png) +![501. 二叉搜索树中的众数](https://file1.kamacoder.com/i/algo/20201014221532206.png) 返回[2]. @@ -144,7 +144,7 @@ public: 如图: -![501.二叉搜索树中的众数1](https://file.kamacoder.com/pics/20210204152758889.png) +![501.二叉搜索树中的众数1](https://file1.kamacoder.com/i/algo/20210204152758889.png) 中序遍历代码如下: diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" old mode 100644 new mode 100755 diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" old mode 100644 new mode 100755 diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" old mode 100644 new mode 100755 index 4098cb7bfb..03f076a2c9 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -12,11 +12,11 @@ 示例 1: -![513.找树左下角的值](https://file.kamacoder.com/pics/20210204152956836.png) +![513.找树左下角的值](https://file1.kamacoder.com/i/algo/20210204152956836.png) 示例 2: -![513.找树左下角的值1](https://file.kamacoder.com/pics/20210204153017586.png) +![513.找树左下角的值1](https://file1.kamacoder.com/i/algo/20210204153017586.png) ## 算法公开课 diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index 5e456ac975..882c36bb05 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -56,7 +56,7 @@ 如果s[i]与s[j]相同,那么dp[i][j] = dp[i + 1][j - 1] + 2; 如图: -![516.最长回文子序列](https://file.kamacoder.com/pics/20210127151350563.jpg) +![516.最长回文子序列](https://file1.kamacoder.com/i/algo/20210127151350563.jpg) (如果这里看不懂,回忆一下dp[i][j]的定义) @@ -68,7 +68,7 @@ 那么dp[i][j]一定是取最大的,即:dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); -![516.最长回文子序列1](https://file.kamacoder.com/pics/20210127151420476.jpg) +![516.最长回文子序列1](https://file1.kamacoder.com/i/algo/20210127151420476.jpg) 代码如下: @@ -97,7 +97,7 @@ for (int i = 0; i < s.size(); i++) dp[i][i] = 1; 从递归公式中,可以看出,dp[i][j] 依赖于 dp[i + 1][j - 1] ,dp[i + 1][j] 和 dp[i][j - 1],如图: -![](https://file.kamacoder.com/pics/20230102172155.png) +![](https://file1.kamacoder.com/i/algo/20230102172155.png) **所以遍历i的时候一定要从下到上遍历,这样才能保证下一行的数据是经过计算的**。 @@ -121,7 +121,7 @@ for (int i = s.size() - 1; i >= 0; i--) { 输入s:"cbbd" 为例,dp数组状态如图: -![516.最长回文子序列3](https://file.kamacoder.com/pics/20210127151521432.jpg) +![516.最长回文子序列3](https://file1.kamacoder.com/i/algo/20210127151521432.jpg) 红色框即:dp[0][s.size() - 1]; 为最终结果。 diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" old mode 100644 new mode 100755 index 95122a7c95..7e4bbb9a81 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -136,7 +136,7 @@ 那么二维数组的最上行 和 最左列一定要初始化,这是递推公式推导的基础,如图红色部分: -![](https://file.kamacoder.com/pics/20240827103507.png) +![](https://file1.kamacoder.com/i/algo/20240827103507.png) 这里首先要关注的就是 dp[0][0] 应该是多少? @@ -296,7 +296,7 @@ for (int j = 0; j <= amount; j++) { // 遍历背包容量 输入: amount = 5, coins = [1, 2, 5] ,dp状态图如下: -![518.零钱兑换II](https://file.kamacoder.com/pics/20210120181331461.jpg) +![518.零钱兑换II](https://file1.kamacoder.com/i/algo/20210120181331461.jpg) 最后红色框dp[amount]为最终结果。 diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" old mode 100644 new mode 100755 index 466bd74479..a8eca862ef --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -13,7 +13,7 @@ 示例: -![530二叉搜索树的最小绝对差](https://file.kamacoder.com/pics/20201014223400123.png) +![530二叉搜索树的最小绝对差](https://file1.kamacoder.com/i/algo/20201014223400123.png) 提示:树中至少有 2 个节点。 @@ -70,7 +70,7 @@ public: 如图: -![530.二叉搜索树的最小绝对差](https://file.kamacoder.com/pics/20210204153247458.png) +![530.二叉搜索树的最小绝对差](https://file1.kamacoder.com/i/algo/20210204153247458.png) 一些同学不知道在递归中如何记录前一个节点的指针,其实实现起来是很简单的,大家只要看过一次,写过一次,就掌握了。 diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" old mode 100644 new mode 100755 index 45bf1f96ed..c4bfeae4b8 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -18,7 +18,7 @@ 示例 1: -![538.把二叉搜索树转换为累加树](https://file.kamacoder.com/pics/20201023160751832.png) +![538.把二叉搜索树转换为累加树](https://file1.kamacoder.com/i/algo/20201023160751832.png) * 输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] * 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] @@ -67,7 +67,7 @@ 遍历顺序如图所示: -![538.把二叉搜索树转换为累加树](https://file.kamacoder.com/pics/20210204153440666.png) +![538.把二叉搜索树转换为累加树](https://file1.kamacoder.com/i/algo/20210204153440666.png) 本题依然需要一个pre指针记录当前遍历节点cur的前一个节点,这样才方便做累加。 diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" old mode 100644 new mode 100755 index 2bbe6cffae..d5ad95c112 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -38,7 +38,7 @@ **所以当需要固定规律一段一段去处理字符串的时候,要想想在for循环的表达式上做做文章。** 性能如下: - + 那么这里具体反转的逻辑我们要不要使用库函数呢,其实用不用都可以,使用reverse来实现反转也没毛病,毕竟不是解题关键部分。 diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" old mode 100644 new mode 100755 index 7f7d30f6a1..8208d9a1eb --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -81,7 +81,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j; 以word1:"sea",word2:"eat"为例,推导dp数组状态图如下: -![583.两个字符串的删除操作1](https://file.kamacoder.com/pics/20210714101750205.png) +![583.两个字符串的删除操作1](https://file1.kamacoder.com/i/algo/20210714101750205.png) 以上分析完毕,代码如下: diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" old mode 100644 new mode 100755 index 755200fe82..3ca5feb9da --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -13,7 +13,7 @@ 示例 1: -![617.合并二叉树](https://file.kamacoder.com/pics/20230310000854.png) +![617.合并二叉树](https://file1.kamacoder.com/i/algo/20230310000854.png) 注意: 合并必须从两个树的根节点开始。 @@ -38,7 +38,7 @@ 动画如下: -![617.合并二叉树](https://code-thinking.cdn.bcebos.com/gifs/617.%E5%90%88%E5%B9%B6%E4%BA%8C%E5%8F%89%E6%A0%91.gif) +![617.合并二叉树](https://file1.kamacoder.com/i/algo/617.%E5%90%88%E5%B9%B6%E4%BA%8C%E5%8F%89%E6%A0%91.gif) 那么我们来按照递归三部曲来解决: diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" old mode 100644 new mode 100755 index 7282953570..fd2ae43886 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -48,7 +48,7 @@ dp[i] 和 dp[i-1] ,dp[i + 1] 看上去都没啥关系。 所以我们要看回文串的性质。 如图: -![](https://file.kamacoder.com/pics/20230102170752.png) +![](https://file1.kamacoder.com/i/algo/20230102170752.png) 我们在判断字符串S是否是回文,那么如果我们知道 s[1],s[2],s[3] 这个子串是回文的,那么只需要比较 s[0]和s[4]这两个元素是否相同,如果相同的话,这个字符串s 就是回文串。 @@ -106,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹 dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图: -![647.回文子串](https://file.kamacoder.com/pics/20210121171032473-20230310132134822.jpg) +![647.回文子串](https://file1.kamacoder.com/i/algo/20210121171032473-20230310132134822.jpg) 如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。 @@ -136,7 +136,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序 举例,输入:"aaa",dp[i][j]状态如下: -![647.回文子串1](https://file.kamacoder.com/pics/20210121171059951-20230310132153163.jpg) +![647.回文子串1](https://file1.kamacoder.com/i/algo/20210121171059951-20230310132153163.jpg) 图中有6个true,所以就是有6个回文子串。 diff --git "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" old mode 100644 new mode 100755 diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" old mode 100644 new mode 100755 index b8841a8bea..49ccc9cdea --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -17,7 +17,7 @@ 示例 : -![654.最大二叉树](https://file.kamacoder.com/pics/20210204154534796.png) +![654.最大二叉树](https://file1.kamacoder.com/i/algo/20210204154534796.png) 提示: @@ -32,7 +32,7 @@ 最大二叉树的构建过程如下: -![654.最大二叉树](https://code-thinking.cdn.bcebos.com/gifs/654.%E6%9C%80%E5%A4%A7%E4%BA%8C%E5%8F%89%E6%A0%91.gif) +![654.最大二叉树](https://file1.kamacoder.com/i/algo/654.%E6%9C%80%E5%A4%A7%E4%BA%8C%E5%8F%89%E6%A0%91.gif) 构造树一般采用的是前序遍历,因为先构造中间节点,然后递归构造左子树和右子树。 diff --git "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" old mode 100644 new mode 100755 index 89993b6ff6..c1706df4fa --- "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" +++ "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" @@ -40,7 +40,7 @@ 最后判断一下x,y是否回到了(0, 0)位置就可以了。 如图所示: - + C++代码如下: diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" old mode 100644 new mode 100755 index f4ded2c4fb..dbcc6ed63d --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -14,9 +14,9 @@ 给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。 -![669.修剪二叉搜索树](https://file.kamacoder.com/pics/20201014173115788.png) +![669.修剪二叉搜索树](https://file1.kamacoder.com/i/algo/20201014173115788.png) -![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20201014173219142.png) +![669.修剪二叉搜索树1](https://file1.kamacoder.com/i/algo/20201014173219142.png) ## 算法公开课 @@ -50,7 +50,7 @@ public: 我们在重新关注一下第二个示例,如图: -![669.修剪二叉搜索树](https://file.kamacoder.com/pics/20210204155302751.png) +![669.修剪二叉搜索树](https://file1.kamacoder.com/i/algo/20210204155302751.png) **所以以上的代码是不可行的!** @@ -60,7 +60,7 @@ public: 在上图中我们发现节点0并不符合区间要求,那么将节点0的右孩子 节点2 直接赋给 节点3的左孩子就可以了(就是把节点0从二叉树中移除),如图: -![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20210204155327203.png) +![669.修剪二叉搜索树1](https://file1.kamacoder.com/i/algo/20210204155327203.png) 理解了最关键部分了我们再递归三部曲: @@ -127,7 +127,7 @@ return root; 在回顾一下上面的代码,针对下图中二叉树的情况: -![669.修剪二叉搜索树1](https://file.kamacoder.com/pics/20210204155327203-20230310120126738.png) +![669.修剪二叉搜索树1](https://file1.kamacoder.com/i/algo/20210204155327203-20230310120126738.png) 如下代码相当于把节点0的右孩子(节点2)返回给上一层, diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" old mode 100644 new mode 100755 index 92009f5b76..9e61229abb --- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" +++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" @@ -178,7 +178,7 @@ for (int i = 0; i < nums.size(); i++) { 输入:[1,3,5,4,7] -![673.最长递增子序列的个数](https://file.kamacoder.com/pics/20230310000656.png) +![673.最长递增子序列的个数](https://file1.kamacoder.com/i/algo/20230310000656.png) **如果代码写出来了,怎么改都通过不了,那么把dp和count打印出来看看对不对!** diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index 16bb2f1887..dae64a11ac --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -85,7 +85,7 @@ for (int i = 1; i < nums.size(); i++) { 已输入nums = [1,3,5,4,7]为例,dp数组状态如下: -![674.最长连续递增序列](https://file.kamacoder.com/pics/20210204103529742.jpg) +![674.最长连续递增序列](https://file1.kamacoder.com/i/algo/20210204103529742.jpg) **注意这里要取dp[i]里的最大值,所以dp[2]才是结果!** diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" old mode 100644 new mode 100755 index 8a7234df52..2f939d0827 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -12,7 +12,7 @@ 请找出一条可以删去的边,删除后可使得剩余部分是一个有着 n 个节点的树。如果有多个答案,则返回数组 edges 中最后出现的边。 -![](https://file.kamacoder.com/pics/20210727150215.png) +![](https://file1.kamacoder.com/i/algo/20210727150215.png) 提示: * n == edges.length @@ -85,7 +85,7 @@ void join(int u, int v) { 如图所示: -![](https://file.kamacoder.com/pics/20230604104720.png) +![](https://file1.kamacoder.com/i/algo/20230604104720.png) 节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 @@ -95,7 +95,7 @@ void join(int u, int v) { 如图所示: -![](https://file.kamacoder.com/pics/20230604104330.png) +![](https://file1.kamacoder.com/i/algo/20230604104330.png) 已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" old mode 100644 new mode 100755 index 66f7bfe1bc..27161d174c --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -16,9 +16,9 @@ 返回一条能删除的边,使得剩下的图是有 n 个节点的有根树。若有多个答案,返回最后出现在给定二维数组的答案。 -![](https://file.kamacoder.com/pics/20210727151057.png) +![](https://file1.kamacoder.com/i/algo/20210727151057.png) -![](https://file.kamacoder.com/pics/20210727151118.png) +![](https://file1.kamacoder.com/i/algo/20210727151118.png) 提示: @@ -38,7 +38,7 @@ 那么有如下三种情况,前两种情况是出现入度为2的点,如图: - + 且只有一个节点入度为2,为什么不看出度呢,出度没有意义,一棵树中随便一个父节点就有多个出度。 @@ -46,7 +46,7 @@ 如图: - + 首先先计算节点的入度,这里不少录友在计算入度的时候就搞蒙了,分不清 edges[i][j] 表示的都是什么。 diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" old mode 100644 new mode 100755 index 972a999591..a63d2b0e06 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -14,7 +14,7 @@ 计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。 -![](https://file.kamacoder.com/pics/20220729111528.png) +![](https://file1.kamacoder.com/i/algo/20220729111528.png) * 输入:grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] * 输出:6 @@ -27,7 +27,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://file.kamacoder.com/pics/20220726094200.png) +![图一](https://file1.kamacoder.com/i/algo/20220726094200.png) 这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上“1”的数量,然后取一个最大的。 diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" old mode 100644 new mode 100755 index 0c373f615b..40777a67a2 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -12,7 +12,7 @@ 例如, -![700.二叉搜索树中的搜索](https://file.kamacoder.com/pics/20210204155522476.png) +![700.二叉搜索树中的搜索](https://file1.kamacoder.com/i/algo/20210204155522476.png) 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。 @@ -124,7 +124,7 @@ public: 中间节点如果大于3就向左走,如果小于3就向右走,如图: -![二叉搜索树](https://file.kamacoder.com/pics/20200812190213280.png) +![二叉搜索树](https://file1.kamacoder.com/i/algo/20200812190213280.png) 所以迭代法代码如下: diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" old mode 100644 new mode 100755 index 6ce9ef3371..fec287449c --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -12,7 +12,7 @@ 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。 -![701.二叉搜索树中的插入操作](https://file.kamacoder.com/pics/20201019173259554.png) +![701.二叉搜索树中的插入操作](https://file1.kamacoder.com/i/algo/20201019173259554.png) 提示: @@ -33,7 +33,7 @@ 如下演示视频中可以看出:只要按照二叉搜索树的规则去遍历,遇到空节点就插入节点就可以了。 -![701.二叉搜索树中的插入操作](https://code-thinking.cdn.bcebos.com/gifs/701.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C.gif) +![701.二叉搜索树中的插入操作](https://file1.kamacoder.com/i/algo/701.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C.gif) 例如插入元素10 ,需要找到末尾节点插入便可,一样的道理来插入元素15,插入元素0,插入元素6,**需要调整二叉树的结构么? 并不需要。**。 diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" old mode 100644 new mode 100755 index 0ce2f3b8a6..e529629492 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -59,7 +59,7 @@ 例如在数组:1,2,3,4,7,9,10中查找元素2,如图所示: -![704.二分查找](https://file.kamacoder.com/pics/20210311153055723.jpg) +![704.二分查找](https://file1.kamacoder.com/i/algo/20210311153055723.jpg) 代码如下:(详细注释) @@ -102,7 +102,7 @@ public: 在数组:1,2,3,4,7,9,10中查找元素2,如图所示:(**注意和方法一的区别**) -![704.二分查找1](https://file.kamacoder.com/pics/20210311153123632.jpg) +![704.二分查找1](https://file1.kamacoder.com/i/algo/20210311153123632.jpg) 代码如下:(详细注释) diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" old mode 100644 new mode 100755 index a2b2803b11..72e35f430f --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -20,7 +20,7 @@ * deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。 -![707示例](https://file.kamacoder.com/pics/20200814200558953.png) +![707示例](https://file1.kamacoder.com/i/algo/20200814200558953.png) ## 算法公开课 @@ -35,10 +35,10 @@ 如果对链表的虚拟头结点不清楚,可以看这篇文章:[链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html) 删除链表节点: -![链表-删除节点](https://file.kamacoder.com/pics/20200806195114541.png) +![链表-删除节点](https://file1.kamacoder.com/i/algo/20200806195114541.png) 添加链表节点: -![链表-添加节点](https://file.kamacoder.com/pics/20200806195134331.png) +![链表-添加节点](https://file1.kamacoder.com/i/algo/20200806195134331.png) 这道题目设计链表的五个接口: * 获取链表第index个节点的数值 diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" old mode 100644 new mode 100755 diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" old mode 100644 new mode 100755 diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" old mode 100644 new mode 100755 index b371bd857b..12384a57a7 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -95,7 +95,7 @@ for (int i = 1; i <= nums1.size(); i++) { 拿示例1中,A: [1,2,3,2,1],B: [3,2,1,4,7]为例,画一个dp数组的状态变化,如下: -![718.最长重复子数组](https://file.kamacoder.com/pics/2021011215282060.jpg) +![718.最长重复子数组](https://file1.kamacoder.com/i/algo/2021011215282060.jpg) 以上五部曲分析完毕,C++代码如下: @@ -127,7 +127,7 @@ public: 在如下图中: -![718.最长重复子数组](https://file.kamacoder.com/pics/2021011215282060-20230310134554486.jpg) +![718.最长重复子数组](https://file1.kamacoder.com/i/algo/2021011215282060-20230310134554486.jpg) 我们可以看出dp[i][j]都是由dp[i - 1][j - 1]推出。那么压缩为一维数组,也就是dp[j]都是由dp[j - 1]推出。 diff --git "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" old mode 100644 new mode 100755 diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" old mode 100644 new mode 100755 diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" old mode 100644 new mode 100755 index ed43cf141f..2ad7e6b79b --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -69,7 +69,7 @@ 首先先将第一个遍历元素加入单调栈 -![739.每日温度1](https://file.kamacoder.com/pics/20210219124434172.jpg) +![739.每日温度1](https://file1.kamacoder.com/i/algo/20210219124434172.jpg) --------- @@ -77,65 +77,65 @@ 我们要保持一个递增单调栈(从栈头到栈底),所以将T[0]弹出,T[1]加入,此时result数组可以记录了,result[0] = 1,即T[0]右面第一个比T[0]大的元素是T[1]。 -![739.每日温度2](https://file.kamacoder.com/pics/20210219124504299.jpg) +![739.每日温度2](https://file1.kamacoder.com/i/algo/20210219124504299.jpg) ----------- 加入T[2],同理,T[1]弹出 -![739.每日温度3](https://file.kamacoder.com/pics/20210219124527361.jpg) +![739.每日温度3](https://file1.kamacoder.com/i/algo/20210219124527361.jpg) ------- 加入T[3],T[3] < T[2] (当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况),加T[3]加入单调栈。 -![739.每日温度4](https://file.kamacoder.com/pics/20210219124610761.jpg) +![739.每日温度4](https://file1.kamacoder.com/i/algo/20210219124610761.jpg) --------- 加入T[4],T[4] == T[3] (当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况),此时依然要加入栈,不用计算距离,因为我们要求的是右面第一个大于本元素的位置,而不是大于等于! -![739.每日温度5](https://file.kamacoder.com/pics/20210219124633444.jpg) +![739.每日温度5](https://file1.kamacoder.com/i/algo/20210219124633444.jpg) --------- 加入T[5],T[5] > T[4] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[4]弹出,同时计算距离,更新result -![739.每日温度6](https://file.kamacoder.com/pics/20210219124700567.jpg) +![739.每日温度6](https://file1.kamacoder.com/i/algo/20210219124700567.jpg) ---------- T[4]弹出之后, T[5] > T[3] (当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况),将T[3]继续弹出,同时计算距离,更新result -![739.每日温度7](https://file.kamacoder.com/pics/20210219124726613.jpg) +![739.每日温度7](https://file1.kamacoder.com/i/algo/20210219124726613.jpg) ------- 直到发现T[5]小于T[st.top()],终止弹出,将T[5]加入单调栈 -![739.每日温度8](https://file.kamacoder.com/pics/20210219124807715.jpg) +![739.每日温度8](https://file1.kamacoder.com/i/algo/20210219124807715.jpg) ------- 加入T[6],同理,需要将栈里的T[5],T[2]弹出 -![739.每日温度9](https://file.kamacoder.com/pics/2021021912483374.jpg) +![739.每日温度9](https://file1.kamacoder.com/i/algo/2021021912483374.jpg) ------- 同理,继续弹出 -![739.每日温度10](https://file.kamacoder.com/pics/2021021912490098.jpg) +![739.每日温度10](https://file1.kamacoder.com/i/algo/2021021912490098.jpg) ------ 此时栈里只剩下了T[6] -![739.每日温度11](https://file.kamacoder.com/pics/20210219124930156.jpg) +![739.每日温度11](https://file1.kamacoder.com/i/algo/20210219124930156.jpg) ------------ 加入T[7], T[7] < T[6] 直接入栈,这就是最后的情况,result数组也更新完了。 -![739.每日温度12](https://file.kamacoder.com/pics/20210219124957216.jpg) +![739.每日温度12](https://file1.kamacoder.com/i/algo/20210219124957216.jpg) 此时有同学可能就疑惑了,那result[6] , result[7]怎么没更新啊,元素也一直在栈里。 diff --git "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" index c8a8736151..40b699c18f 100644 --- "a/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" +++ "b/problems/0743.\347\275\221\347\273\234\345\273\266\350\277\237\346\227\266\351\227\264.md" @@ -13,7 +13,7 @@ https://leetcode.cn/problems/network-delay-time/description/ 现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1 。 -![](https://file.kamacoder.com/pics/20240229104105.png) +![](https://file1.kamacoder.com/i/algo/20240229104105.png) 提示: @@ -42,7 +42,7 @@ dijkstra算法:在有权图(权值非负数)中求从起点到其他节点 如本题示例中的图: -![](https://file.kamacoder.com/pics/20240125162647.png) +![](https://file1.kamacoder.com/i/algo/20240125162647.png) 起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 @@ -88,7 +88,7 @@ minDist数组数值初始化为int最大值。 这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 -![](https://file.kamacoder.com/pics/20240130115306.png) +![](https://file1.kamacoder.com/i/algo/20240130115306.png) (图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) @@ -110,7 +110,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240130115421.png) +![](https://file1.kamacoder.com/i/algo/20240130115421.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -136,7 +136,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240130121240.png) +![](https://file1.kamacoder.com/i/algo/20240130121240.png) 更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 @@ -170,7 +170,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240130120434.png) +![](https://file1.kamacoder.com/i/algo/20240130120434.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -190,7 +190,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201105335.png) +![](https://file1.kamacoder.com/i/algo/20240201105335.png) 由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: @@ -210,7 +210,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201110250.png) +![](https://file1.kamacoder.com/i/algo/20240201110250.png) 由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: @@ -230,7 +230,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201110651.png) +![](https://file1.kamacoder.com/i/algo/20240201110651.png) 由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: @@ -248,7 +248,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201110920.png) +![](https://file1.kamacoder.com/i/algo/20240201110920.png) 节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 @@ -262,7 +262,7 @@ minDist数组数值初始化为int最大值。 路径如图: -![](https://file.kamacoder.com/pics/20240201111352.png) +![](https://file1.kamacoder.com/i/algo/20240201111352.png) 在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 @@ -431,7 +431,7 @@ select:4 看一下这个图: (有负权值) -![](https://file.kamacoder.com/pics/20240227104334.png) +![](https://file1.kamacoder.com/i/algo/20240227104334.png) 节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 @@ -441,7 +441,7 @@ select:4 初始化: -![](https://file.kamacoder.com/pics/20240227104801.png) +![](https://file1.kamacoder.com/i/algo/20240227104801.png) --------------- @@ -455,7 +455,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110217.png) +![](https://file1.kamacoder.com/i/algo/20240227110217.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -474,7 +474,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110330.png) +![](https://file1.kamacoder.com/i/algo/20240227110330.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -492,7 +492,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110346.png) +![](https://file1.kamacoder.com/i/algo/20240227110346.png) 由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: @@ -510,7 +510,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110405.png) +![](https://file1.kamacoder.com/i/algo/20240227110405.png) 节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 @@ -526,7 +526,7 @@ select:4 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110711.png) +![](https://file1.kamacoder.com/i/algo/20240227110711.png) -------------- @@ -654,7 +654,7 @@ for (int v = 1; v <= n; v++) { 如图: -![](https://file.kamacoder.com/pics/20240222110025.png) +![](https://file1.kamacoder.com/i/algo/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 @@ -678,7 +678,7 @@ for (int v = 1; v <= n; v++) { 邻接表的构造如图: -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 这里表达的图是: @@ -763,7 +763,7 @@ vector> grid(n + 1); 不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 图中邻接表表示: @@ -784,7 +784,7 @@ vector>> grid(n + 1); 举例来给大家展示 该代码表达的数据 如下: -![](https://file.kamacoder.com/pics/20240223103904.png) +![](https://file1.kamacoder.com/i/algo/20240223103904.png) * 节点1 指向 节点3 权值为 1 * 节点1 指向 节点5 权值为 2 @@ -907,7 +907,7 @@ for (int v = 1; v <= n; v++) { 再回顾一下邻接表的构造(数组 + 链表): -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" old mode 100644 new mode 100755 index 147c7bfba7..952d4d2ab7 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -52,7 +52,7 @@ 请你计算并返回达到楼梯顶部的最低花费。 -![](https://file.kamacoder.com/pics/20221031170131.png) +![](https://file1.kamacoder.com/i/algo/20221031170131.png) ## 思路 @@ -112,7 +112,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。 拿示例2:cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] ,来模拟一下dp数组的状态变化,如下: -![](https://file.kamacoder.com/pics/20221026175104.png) +![](https://file1.kamacoder.com/i/algo/20221026175104.png) 如果大家代码写出来有问题,就把dp数组打印出来,看看和如上推导的是不是一样的。 diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" old mode 100644 new mode 100755 index daf52bea55..d17878381f --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -44,7 +44,7 @@ 如图: -![763.划分字母区间](https://file.kamacoder.com/pics/20201222191924417.png) +![763.划分字母区间](https://file1.kamacoder.com/i/algo/20201222191924417.png) 明白原理之后,代码并不复杂,如下: diff --git "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" index 6133ac7733..fb58c14816 100644 --- "a/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" +++ "b/problems/0787.K\347\253\231\344\270\255\350\275\254\345\206\205\346\234\200\344\276\277\345\256\234\347\232\204\350\210\252\347\217\255.md" @@ -9,11 +9,11 @@ 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ,并返回该价格。 如果不存在这样的路线,则输出 -1。 -![](https://file.kamacoder.com/pics/20240319103900.png) +![](https://file1.kamacoder.com/i/algo/20240319103900.png) -![](https://file.kamacoder.com/pics/20240319103919.png) +![](https://file1.kamacoder.com/i/algo/20240319103919.png) -![](https://file.kamacoder.com/pics/20240319104026.png) +![](https://file1.kamacoder.com/i/algo/20240319104026.png) ## 思路 diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" old mode 100644 new mode 100755 index 639b6b2b3b..db4d249a15 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -11,7 +11,7 @@ graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向边)。 -![](https://file.kamacoder.com/pics/20221203135439.png) +![](https://file1.kamacoder.com/i/algo/20221203135439.png) 提示: @@ -96,7 +96,7 @@ path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来 一些录友可以疑惑这里如果找到x 链接的节点的,例如如果x目前是节点0,那么目前的过程就是这样的: -![](https://file.kamacoder.com/pics/20221204111937.png) +![](https://file1.kamacoder.com/i/algo/20221204111937.png) 二维数组中,graph[x][i] 都是x链接的节点,当前遍历的节点就是 `graph[x][i]` 。 diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" old mode 100644 new mode 100755 index e6aa4601dd..118735e943 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -51,11 +51,11 @@ 拿如下地图的岛屿情况来举例: (1为陆地) -![](https://file.kamacoder.com/pics/20220829104834.png) +![](https://file1.kamacoder.com/i/algo/20220829104834.png) 第一步,则遍历题目,并将岛屿到编号和面积上的统计,过程如图所示: -![](https://file.kamacoder.com/pics/20220829105644.png) +![](https://file1.kamacoder.com/i/algo/20220829105644.png) 本过程代码如下: @@ -102,7 +102,7 @@ int largestIsland(vector>& grid) { 第二步过程如图所示: -![](https://file.kamacoder.com/pics/20220829105249.png) +![](https://file1.kamacoder.com/i/algo/20220829105249.png) 也就是遍历每一个0的方格,并统计其相邻岛屿面积,最后取一个最大值。 diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" old mode 100644 new mode 100755 index 60180d2736..ffcf2fb919 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -35,7 +35,7 @@ 图中给我的两个示例: `[[1],[2],[3],[]]` `[[1,3],[3,0,1],[2],[0]]`,画成对应的图如下: -![](https://file.kamacoder.com/pics/20220714101414.png) +![](https://file1.kamacoder.com/i/algo/20220714101414.png) 我们可以看出图1的所有节点都是链接的,而图二中,节点2 是孤立的。 @@ -48,7 +48,7 @@ 图3:[[5], [], [1, 3], [5]] ,如图: -![](https://file.kamacoder.com/pics/20220714102201.png) +![](https://file1.kamacoder.com/i/algo/20220714102201.png) 在图3中,大家可以发现,节点0只能到节点5,然后就哪也去不了了。 diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" old mode 100644 new mode 100755 index f229447384..6d0cd68578 --- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -106,7 +106,7 @@ public: 动画如下: - + 如果S[i]和S[j]不相同返回false,如果有一个指针(i或者j)先走到的字符串头部位置,也返回false。 diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" old mode 100644 new mode 100755 diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" old mode 100644 new mode 100755 diff --git "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" old mode 100644 new mode 100755 index 47465199a8..f653caef52 --- "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" +++ "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" @@ -52,7 +52,7 @@ 动画如下: - + 上面的逻辑想清楚了,不难写出如下C++代码: diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" old mode 100644 new mode 100755 index d4165f3672..9f000e2bb5 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" @@ -16,7 +16,7 @@ * arr[0] < arr[1] < ... arr[i-1] < arr[i] * arr[i] > arr[i+1] > ... > arr[arr.length - 1] -![](https://file.kamacoder.com/pics/20210729103604.png) +![](https://file1.kamacoder.com/i/algo/20210729103604.png) 示例 1: * 输入:arr = [2,1] @@ -37,7 +37,7 @@ 这样可以使用两个指针,left和right,让其按照如下规则移动,如图: - + **注意这里还是有一些细节,例如如下两点:** diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" old mode 100644 new mode 100755 index d8c31ca998..989993acf5 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -17,7 +17,7 @@ 示例 1: -![](https://file.kamacoder.com/pics/20201229175736596.png) +![](https://file1.kamacoder.com/i/algo/20201229175736596.png) * 输入:[0,0,null,0,0] * 输出:1 @@ -25,7 +25,7 @@ 示例 2: -![](https://file.kamacoder.com/pics/2020122917584449.png) +![](https://file1.kamacoder.com/i/algo/2020122917584449.png) * 输入:[0,0,null,0,null,0,null,null,0] * 输出:2 @@ -143,7 +143,7 @@ if (cur == NULL) return 2; 如图: -![968.监控二叉树2](https://file.kamacoder.com/pics/20201229203710729.png) +![968.监控二叉树2](https://file1.kamacoder.com/i/algo/20201229203710729.png) 代码如下: @@ -191,7 +191,7 @@ if (left == 1 || right == 1) return 2; **从这个代码中,可以看出,如果left == 1, right == 0 怎么办?其实这种条件在情况2中已经判断过了**,如图: -![968.监控二叉树1](https://file.kamacoder.com/pics/2020122920362355.png) +![968.监控二叉树1](https://file1.kamacoder.com/i/algo/2020122920362355.png) 这种情况也是大多数同学容易迷惑的情况。 @@ -199,7 +199,7 @@ if (left == 1 || right == 1) return 2; 以上都处理完了,递归结束之后,可能头结点 还有一个无覆盖的情况,如图: -![968.监控二叉树3](https://file.kamacoder.com/pics/20201229203742446.png) +![968.监控二叉树3](https://file1.kamacoder.com/i/algo/20201229203742446.png) 所以递归结束之后,还要判断根节点,如果没有覆盖,result++,代码如下: diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" old mode 100644 new mode 100755 index 6e58be1af6..1f58fd5198 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -61,7 +61,7 @@ public: 如动画所示: -![](https://code-thinking.cdn.bcebos.com/gifs/977.有序数组的平方.gif) +![](https://file1.kamacoder.com/i/algo/977.有序数组的平方.gif) 不难写出如下代码: diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" old mode 100644 new mode 100755 index 3d7d8e0770..cbf5ecdb78 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -53,7 +53,7 @@ words[i] 由小写英文字母组成 如图: -![1002.查找常用字符](https://code-thinking.cdn.bcebos.com/pics/1002.查找常用字符.png) +![1002.查找常用字符](https://file1.kamacoder.com/i/algo/1002.查找常用字符.png) 先统计第一个字符串所有字符出现的次数,代码如下: diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" old mode 100644 new mode 100755 diff --git "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" old mode 100644 new mode 100755 index ae6b3895fa..396e6c566b --- "a/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" +++ "b/problems/1020.\351\243\236\345\234\260\347\232\204\346\225\260\351\207\217.md" @@ -12,13 +12,13 @@ 返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。 -![](https://file.kamacoder.com/pics/20220830100710.png) +![](https://file1.kamacoder.com/i/algo/20220830100710.png) * 输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] * 输出:3 * 解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。 -![](https://file.kamacoder.com/pics/20220830100742.png) +![](https://file1.kamacoder.com/i/algo/20220830100742.png) * 输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] * 输出:0 @@ -32,11 +32,11 @@ 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, -![](https://file.kamacoder.com/pics/20220830104632.png) +![](https://file1.kamacoder.com/i/algo/20220830104632.png) 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: -![](https://file.kamacoder.com/pics/20220830104651.png) +![](https://file1.kamacoder.com/i/algo/20220830104651.png) 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" old mode 100644 new mode 100755 index 0119df82ea..16bf869ea6 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -18,7 +18,7 @@ 以这种方法绘制线条,并返回可以绘制的最大连线数。 -![1035.不相交的线](https://file.kamacoder.com/pics/2021032116363533.png) +![1035.不相交的线](https://file1.kamacoder.com/i/algo/2021032116363533.png) ## 算法公开课 @@ -36,7 +36,7 @@ 拿示例一nums1 = [1,4,2], nums2 = [1,2,4]为例,相交情况如图: -![](https://file.kamacoder.com/pics/20210914145158.png) +![](https://file1.kamacoder.com/i/algo/20210914145158.png) 其实也就是说nums1和nums2的最长公共子序列是[1,4],长度为2。 这个公共子序列指的是相对顺序不变(即数字4在字符串nums1中数字1的后面,那么数字4也应该在字符串nums2数字1的后面) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" old mode 100644 new mode 100755 index 01d33fbff7..36702194be --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -46,7 +46,7 @@ 然后再去做对应的消除操作。 如动画所示: -![1047.删除字符串中的所有相邻重复项](https://code-thinking.cdn.bcebos.com/gifs/1047.删除字符串中的所有相邻重复项.gif) +![1047.删除字符串中的所有相邻重复项](https://file1.kamacoder.com/i/algo/1047.删除字符串中的所有相邻重复项.gif) 从栈中弹出剩余元素,此时是字符串ac,因为从栈里弹出的元素是倒序的,所以再对字符串进行反转一下,就得到了最终的结果。 diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" old mode 100644 new mode 100755 index 6dfba4ed44..ddc9f313db --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -114,7 +114,7 @@ for (int i = 0; i < stones.size(); i++) { // 遍历物品 举例,输入:[2,4,1,1],此时target = (2 + 4 + 1 + 1)/2 = 4 ,dp数组状态图如下: -![1049.最后一块石头的重量II](https://file.kamacoder.com/pics/20210121115805904.jpg) +![1049.最后一块石头的重量II](https://file1.kamacoder.com/i/algo/20210121115805904.jpg) 最后dp[target]里是容量为target的背包所能背的最大重量。 diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" old mode 100644 new mode 100755 index 91c29b8313..424f403938 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -94,7 +94,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 从递推公式,可以看出,有三个方向可以推出dp[i][j],如图: -![1143.最长公共子序列](https://file.kamacoder.com/pics/20210204115139616.jpg) +![1143.最长公共子序列](https://file1.kamacoder.com/i/algo/20210204115139616.jpg) 那么为了在递推的过程中,这三个方向都是经过计算的数值,所以要从前向后,从上到下来遍历这个矩阵。 @@ -103,7 +103,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0)); 以输入:text1 = "abcde", text2 = "ace" 为例,dp状态如图: -![1143.最长公共子序列1](https://file.kamacoder.com/pics/20210210150215918.jpg) +![1143.最长公共子序列1](https://file1.kamacoder.com/i/algo/20210210150215918.jpg) 最后红框dp[text1.size()][text2.size()]为最终结果 diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" old mode 100644 new mode 100755 index fbb19af773..72462f1119 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -45,7 +45,7 @@ 如图所示: - + C++代码如下: diff --git "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" old mode 100644 new mode 100755 diff --git "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" old mode 100644 new mode 100755 index ebea30e34f..b440c32648 --- "a/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" +++ "b/problems/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.md" @@ -10,13 +10,13 @@ 请返回 封闭岛屿 的数目。 -![](https://file.kamacoder.com/pics/20220830111533.png) +![](https://file1.kamacoder.com/i/algo/20220830111533.png) * 输入:grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]] * 输出:2 * 解释:灰色区域的岛屿是封闭岛屿,因为这座岛屿完全被水域包围(即被 1 区域包围)。 -![](https://file.kamacoder.com/pics/20220830111601.png) +![](https://file1.kamacoder.com/i/algo/20220830111601.png) * 输入:grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]] * 输出:1 diff --git "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" old mode 100644 new mode 100755 index 0ae1603494..11f947a184 --- "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" +++ "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" @@ -81,7 +81,7 @@ int bitCount(int n) { ``` 以计算12的二进制1的数量为例,如图所示: - + 下面我就使用方法二,来做这道题目: diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" old mode 100644 new mode 100755 index 2cb73f728a..61b548abf8 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -85,7 +85,7 @@ for (int i = 0; i < nums.size(); i++) { 流程如图: - + 关键地方讲完了,整体C++代码如下: diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" old mode 100644 new mode 100755 index 7b0d32048a..551766ff0d --- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" +++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" @@ -15,7 +15,7 @@ 示例: -![](https://file.kamacoder.com/pics/20210726154512.png) +![](https://file1.kamacoder.com/i/algo/20210726154512.png) * 输入:root = [1,null,2,null,3,null,4,null,null] * 输出:[2,1,3,null,null,null,4] diff --git "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" old mode 100644 new mode 100755 index 5dd56c65d9..9991249f5a --- "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" +++ "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" @@ -10,7 +10,7 @@ 什么是度,可以理解为,链接节点的边的数量。 题目中度如图所示: -![1791.找出星型图的中心节点](https://file.kamacoder.com/pics/20220704113207.png) +![1791.找出星型图的中心节点](https://file1.kamacoder.com/i/algo/20220704113207.png) 至于出度和入度,那就是在有向图里的概念了,本题是无向图。 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" old mode 100644 new mode 100755 index 33b48698b5..9048b0f6af --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -12,7 +12,7 @@ 给你数组 edges 和整数 n、start 和 end,如果从 start 到 end 存在 有效路径 ,则返回 true,否则返回 false 。 -![](https://file.kamacoder.com/pics/20220705101442.png) +![](https://file1.kamacoder.com/i/algo/20220705101442.png) diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 830bba7e2a..d74f1a01aa 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -13,7 +13,7 @@ ## 超时是怎么回事 -![程序超时](https://file.kamacoder.com/pics/20200729112716117.png) +![程序超时](https://file1.kamacoder.com/i/algo/20200729112716117.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -129,11 +129,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://file.kamacoder.com/pics/20200729200018460.png) +![程序超时2](https://file1.kamacoder.com/i/algo/20200729200018460.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下O(n^2) 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://file.kamacoder.com/pics/2020072919590970.png) +![程序超时3](https://file1.kamacoder.com/i/algo/2020072919590970.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -141,7 +141,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 O(n)少一个数量级,因为logn的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://file.kamacoder.com/pics/20200729195729407.png) +![程序超时4](https://file1.kamacoder.com/i/algo/20200729195729407.png) O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -149,7 +149,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 **整体测试数据整理如下:** -![程序超时1](https://file.kamacoder.com/pics/20201208231559175.png) +![程序超时1](https://file1.kamacoder.com/i/algo/20201208231559175.png) 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" index 64804842f2..cb5fbb7468 100644 --- "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -613,3 +613,4 @@ func main() { } ``` +
diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index e361e8e0d9..80d7851eaf 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -46,13 +46,13 @@ 如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 -![](https://file.kamacoder.com/pics/20240227101345.png) +![](https://file1.kamacoder.com/i/algo/20240227101345.png) 不能到达的情况: 如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 -![](https://file.kamacoder.com/pics/20240227101401.png) +![](https://file1.kamacoder.com/i/algo/20240227101401.png) 数据范围: @@ -101,7 +101,7 @@ 如图: -![](https://file.kamacoder.com/pics/20240222110025.png) +![](https://file1.kamacoder.com/i/algo/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间,有一条双向边,即:grid[2][5] = 6,grid[5][2] = 6 @@ -125,7 +125,7 @@ 邻接表的构造如图: -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 这里表达的图是: @@ -210,7 +210,7 @@ vector> grid(n + 1); 不少录友,不知道 如何定义的数据结构,怎么表示邻接表的,我来给大家画一个图: -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 图中邻接表表示: @@ -231,7 +231,7 @@ vector>> grid(n + 1); 举例来给大家展示 该代码表达的数据 如下: -![](https://file.kamacoder.com/pics/20240223103904.png) +![](https://file1.kamacoder.com/i/algo/20240223103904.png) * 节点1 指向 节点3 权值为 1 * 节点1 指向 节点5 权值为 2 @@ -354,7 +354,7 @@ for (int v = 1; v <= n; v++) { 再回顾一下邻接表的构造(数组 + 链表): -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 假如 加入的cur 是节点 2, 那么 grid[2] 表示的就是图中第二行链表。 (grid数组的构造我们在 上面 「图的存储」中讲过) @@ -927,3 +927,4 @@ func main() { ### C +
diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 1ff9f1a874..42099df92a 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -46,13 +46,13 @@ 如下图所示,起始车站为 1 号车站,终点车站为 7 号车站,绿色路线为最短的路线,路线总长度为 12,则输出 12。 -![](https://file.kamacoder.com/pics/20240227101345.png) +![](https://file1.kamacoder.com/i/algo/20240227101345.png) 不能到达的情况: 如下图所示,当从起始车站不能到达终点车站时,则输出 -1。 -![](https://file.kamacoder.com/pics/20240227101401.png) +![](https://file1.kamacoder.com/i/algo/20240227101401.png) 数据范围: @@ -76,7 +76,7 @@ dijkstra算法:在有权图(权值非负数)中求从起点到其他节点 如本题示例中的图: -![](https://file.kamacoder.com/pics/20240125162647.png) +![](https://file1.kamacoder.com/i/algo/20240125162647.png) 起点(节点1)到终点(节点7) 的最短路径是 图中 标记绿线的部分。 @@ -122,7 +122,7 @@ minDist数组数值初始化为int最大值。 这里在强点一下 **minDist数组的含义:记录所有节点到源点的最短路径**,那么初始化的时候就应该初始为最大值,这样才能在后续出现最短路径的时候及时更新。 -![](https://file.kamacoder.com/pics/20240130115306.png) +![](https://file1.kamacoder.com/i/algo/20240130115306.png) (图中,max 表示默认值,节点0 不做处理,统一从下标1 开始计算,这样下标和节点数值统一, 方便大家理解,避免搞混) @@ -144,7 +144,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240130115421.png) +![](https://file1.kamacoder.com/i/algo/20240130115421.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -170,7 +170,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240130121240.png) +![](https://file1.kamacoder.com/i/algo/20240130121240.png) 更新 minDist数组,即:源点(节点1) 到 节点6 、 节点3 和 节点4的距离。 @@ -204,7 +204,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240130120434.png) +![](https://file1.kamacoder.com/i/algo/20240130120434.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -224,7 +224,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201105335.png) +![](https://file1.kamacoder.com/i/algo/20240201105335.png) 由于节点4的加入,那么源点可以链接到节点5 所以更新minDist数组: @@ -244,7 +244,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201110250.png) +![](https://file1.kamacoder.com/i/algo/20240201110250.png) 由于节点6的加入,那么源点可以链接到节点7 所以 更新minDist数组: @@ -264,7 +264,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201110651.png) +![](https://file1.kamacoder.com/i/algo/20240201110651.png) 由于节点5的加入,那么源点有新的路径可以链接到节点7 所以 更新minDist数组: @@ -282,7 +282,7 @@ minDist数组数值初始化为int最大值。 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240201110920.png) +![](https://file1.kamacoder.com/i/algo/20240201110920.png) 节点7加入,但节点7到节点7的距离为0,所以 不用更新minDist数组 @@ -296,7 +296,7 @@ minDist数组数值初始化为int最大值。 路径如图: -![](https://file.kamacoder.com/pics/20240201111352.png) +![](https://file1.kamacoder.com/i/algo/20240201111352.png) 在上面的讲解中,每一步 我都是按照 dijkstra 三部曲来讲解的,理解了这三部曲,代码也就好懂的。 @@ -541,7 +541,7 @@ int main() { 对应如图: -![](https://file.kamacoder.com/pics/20240201111352.png) +![](https://file1.kamacoder.com/i/algo/20240201111352.png) ### 出现负数 @@ -549,7 +549,7 @@ int main() { 看一下这个图: (有负权值) -![](https://file.kamacoder.com/pics/20240227104334.png) +![](https://file1.kamacoder.com/i/algo/20240227104334.png) 节点1 到 节点5 的最短路径 应该是 节点1 -> 节点2 -> 节点3 -> 节点4 -> 节点5 @@ -559,7 +559,7 @@ int main() { 初始化: -![](https://file.kamacoder.com/pics/20240227104801.png) +![](https://file1.kamacoder.com/i/algo/20240227104801.png) --------------- @@ -573,7 +573,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110217.png) +![](https://file1.kamacoder.com/i/algo/20240227110217.png) 更新 minDist数组,即:源点(节点1) 到 节点2 和 节点3的距离。 @@ -592,7 +592,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110330.png) +![](https://file1.kamacoder.com/i/algo/20240227110330.png) 由于节点3的加入,那么源点可以有新的路径链接到节点4 所以更新minDist数组: @@ -610,7 +610,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110346.png) +![](https://file1.kamacoder.com/i/algo/20240227110346.png) 由于节点4的加入,那么源点可以有新的路径链接到节点5 所以更新minDist数组: @@ -628,7 +628,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110405.png) +![](https://file1.kamacoder.com/i/algo/20240227110405.png) 节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了 @@ -644,7 +644,7 @@ int main() { 3、更新非访问节点到源点的距离(即更新minDist数组) ,如图: -![](https://file.kamacoder.com/pics/20240227110711.png) +![](https://file1.kamacoder.com/i/algo/20240227110711.png) -------------- @@ -942,3 +942,4 @@ main() ### C +
diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 585fa4767e..53da7af9ee 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -63,7 +63,7 @@ kruscal的思路: 依然以示例中,如下这个图来举例。 -![](https://file.kamacoder.com/pics/20240111113514.png) +![](https://file1.kamacoder.com/i/algo/20240111113514.png) 将图中的边按照权值有小到大排序,这样从贪心的角度来说,优先选 权值小的边加入到 最小生成树中。 @@ -77,13 +77,13 @@ kruscal的思路: 选边(1,2),节点1 和 节点2 不在同一个集合,所以生成树可以添加边(1,2),并将 节点1,节点2 放在同一个集合。 -![](https://file.kamacoder.com/pics/20240111114204.png) +![](https://file1.kamacoder.com/i/algo/20240111114204.png) -------- 选边(4,5),节点4 和 节点 5 不在同一个集合,生成树可以添加边(4,5) ,并将节点4,节点5 放到同一个集合。 -![](https://file.kamacoder.com/pics/20240111120458.png) +![](https://file1.kamacoder.com/i/algo/20240111120458.png) **大家判断两个节点是否在同一个集合,就看图中两个节点是否有绿色的粗线连着就行** @@ -93,25 +93,25 @@ kruscal的思路: 选边(1,3),节点1 和 节点3 不在同一个集合,生成树添加边(1,3),并将节点1,节点3 放到同一个集合。 -![](https://file.kamacoder.com/pics/20240112105834.png) +![](https://file1.kamacoder.com/i/algo/20240112105834.png) --------- 选边(2,6),节点2 和 节点6 不在同一个集合,生成树添加边(2,6),并将节点2,节点6 放到同一个集合。 -![](https://file.kamacoder.com/pics/20240112110214.png) +![](https://file1.kamacoder.com/i/algo/20240112110214.png) -------- 选边(3,4),节点3 和 节点4 不在同一个集合,生成树添加边(3,4),并将节点3,节点4 放到同一个集合。 -![](https://file.kamacoder.com/pics/20240112110450.png) +![](https://file1.kamacoder.com/i/algo/20240112110450.png) ---------- 选边(6,7),节点6 和 节点7 不在同一个集合,生成树添加边(6,7),并将 节点6,节点7 放到同一个集合。 -![](https://file.kamacoder.com/pics/20240112110637.png) +![](https://file1.kamacoder.com/i/algo/20240112110637.png) ----------- @@ -126,7 +126,7 @@ kruscal的思路: 此时 我们就已经生成了一个最小生成树,即: -![](https://file.kamacoder.com/pics/20240112110637.png) +![](https://file1.kamacoder.com/i/algo/20240112110637.png) 在上面的讲解中,看图的话 大家知道如何判断 两个节点 是否在同一个集合(是否有绿色的线连在一起),以及如何把两个节点加入集合(就在图中把两个节点连上) @@ -346,7 +346,7 @@ int main() { 大家可能发现 怎么和我们 模拟画的图不一样,差别在于 代码生成的最小生成树中 节点5 和 节点7相连的。 -![](https://file.kamacoder.com/pics/20240116163014.png) +![](https://file1.kamacoder.com/i/algo/20240116163014.png) 其实造成这个差别 是对边排序的时候 权值相同的边先后顺序的问题导致的,无论相同权值边的顺序是什么样的,最后都能得出最小生成树。 @@ -366,7 +366,7 @@ Kruskal 与 prim 的关键区别在于,prim维护的是节点的集合,而 K 节点未必一定要连着边那, 例如 这个图,大家能明显感受到边没有那么多对吧,但节点数量 和 上述我们讲的例子是一样的。 -![](https://file.kamacoder.com/pics/20240116152211.png) +![](https://file1.kamacoder.com/i/algo/20240116152211.png) 为什么边少的话,使用 Kruskal 更优呢? @@ -760,3 +760,4 @@ int main() } ``` +
diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index a7d3584178..df0129ee2a 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -61,7 +61,7 @@ 例如本题示例中的无向有权图为: -![](https://file.kamacoder.com/pics/20231206164306.png) +![](https://file1.kamacoder.com/i/algo/20231206164306.png) 那么在这个图中,如何选取n-1条边使得图中所有节点连接到一起,并且边的权值和最小呢? @@ -100,7 +100,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 如图: -![](https://file.kamacoder.com/pics/20231215105603.png) +![](https://file1.kamacoder.com/i/algo/20231215105603.png) 开始构造最小生成树 @@ -118,7 +118,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来,我们要更新所有节点距离最小生成树的距离,如图: -![](https://file.kamacoder.com/pics/20231222102048.png) +![](https://file1.kamacoder.com/i/algo/20231222102048.png) 注意下标0,我们就不管它了,下标1与节点1对应,这样可以避免大家把节点搞混。 @@ -148,7 +148,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来,我们要更新节点距离最小生成树的距离,如图: -![](https://file.kamacoder.com/pics/20231222102431.png) +![](https://file1.kamacoder.com/i/algo/20231222102431.png) 此时所有非生成树的节点距离最小生成树(节点1、节点2)的距离都已经跟新了。 @@ -172,7 +172,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来更新节点距离最小生成树的距离,如图: -![](https://file.kamacoder.com/pics/20231222102457.png) +![](https://file1.kamacoder.com/i/algo/20231222102457.png) 所有非生成树的节点距离最小生成树(节点1、节点2、节点3)的距离都已经跟新了。 @@ -188,7 +188,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 继续选择一个距离最小生成树(节点1、节点2、节点3)最近的非生成树里的节点,为了巩固大家对minDist数组的理解,这里我再啰嗦一遍: -![](https://file.kamacoder.com/pics/20231217213516.png) +![](https://file1.kamacoder.com/i/algo/20231217213516.png) **minDist数组是记录了所有非生成树节点距离生成树的最小距离**,所以从数组里我们能看出来,非生成树节点4和节点6距离生成树最近。 @@ -209,7 +209,7 @@ minDist数组里的数值初始化为最大数,因为本题节点距离不会 接下来更新节点距离最小生成树的距离,如图: -![](https://file.kamacoder.com/pics/20231222102618.png) +![](https://file1.kamacoder.com/i/algo/20231222102618.png) minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4)的距离。 @@ -232,7 +232,7 @@ minDist数组已经更新了所有非生成树的节点距离最小生成树( 接下来更新节点距离最小生成树的距离,如图: -![](https://file.kamacoder.com/pics/20231222102646.png) +![](https://file1.kamacoder.com/i/algo/20231222102646.png) minDist数组已经更新了所有非生成树的节点距离最小生成树(节点1、节点2、节点3、节点4、节点5)的距离。 @@ -253,11 +253,11 @@ minDist数组已经更新了所有非生成树的节点距离最小生成树( 节点1、节点2、节点3、节点4、节点5、节点6算是最小生成树的节点,接下来更新节点距离最小生成树的距离,如图: -![](https://file.kamacoder.com/pics/20231222102732.png) +![](https://file1.kamacoder.com/i/algo/20231222102732.png) 这里就不在重复描述了,大家类推,最后,节点7加入生成树,如图: -![](https://file.kamacoder.com/pics/20231222102820.png) +![](https://file1.kamacoder.com/i/algo/20231222102820.png) ### 最后 @@ -478,7 +478,7 @@ int main() { 大家可以和我们本题最后生成的最小生成树的图去对比一下边的链接情况: -![](https://file.kamacoder.com/pics/20231229115714.png) +![](https://file1.kamacoder.com/i/algo/20231229115714.png) 绿色的边是最小生成树,和我们的输出完全一致。 @@ -757,3 +757,4 @@ main() ### C +
diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 665e8ecbae..67d31a5564 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -29,11 +29,11 @@ 如图: -![](https://file.kamacoder.com/pics/20231030165201.png) +![](https://file1.kamacoder.com/i/algo/20231030165201.png) 然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 -![](https://file.kamacoder.com/pics/20231030173058.png) +![](https://file1.kamacoder.com/i/algo/20231030173058.png) 有同学问了,为什么要从后向前填充,从前向后填充不行么? @@ -432,3 +432,4 @@ echo $s; ### Rust: +
diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index 48150222a7..be998390c5 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -40,16 +40,16 @@ fgabcde 本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) -![](https://file.kamacoder.com/pics/20231106170143.png) +![](https://file1.kamacoder.com/i/algo/20231106170143.png) 右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: -![](https://file.kamacoder.com/pics/20231106171557.png) +![](https://file1.kamacoder.com/i/algo/20231106171557.png) 此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: -![](https://file.kamacoder.com/pics/20231106172058.png) +![](https://file1.kamacoder.com/i/algo/20231106172058.png) 其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 @@ -80,7 +80,7 @@ int main() { 可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: -![](https://file.kamacoder.com/pics/20231106172534.png) +![](https://file1.kamacoder.com/i/algo/20231106172534.png) 代码如下: @@ -409,3 +409,4 @@ echo $s; ### Rust: +
diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index 8eaad9f0de..894e0383d5 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -93,7 +93,7 @@ int main() { 如图: -![](https://file.kamacoder.com/pics/20240627110604.png) +![](https://file1.kamacoder.com/i/algo/20240627110604.png) 如果,我们想统计,在vec数组上 下标 2 到下标 5 之间的累加和,那是不是就用 p[5] - p[1] 就可以了。 @@ -109,7 +109,7 @@ int main() { 如图所示: -![](https://file.kamacoder.com/pics/20240627111319.png) +![](https://file1.kamacoder.com/i/algo/20240627111319.png) `p[5] - p[1]` 就是 红色部分的区间和。 @@ -408,3 +408,4 @@ func main() { } ``` +
diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index b592029276..9d3fbe839e 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -62,7 +62,7 @@ 给大家举一个例子: -![](https://file.kamacoder.com/pics/20240328104119.png) +![](https://file1.kamacoder.com/i/algo/20240328104119.png) 本图中,对所有边进行松弛,真正有效的松弛,只有松弛 边(节点1->节点2) 和 边(节点1->节点3) 。 @@ -97,7 +97,7 @@ 初始化,起点为节点1, 起点到起点的最短距离为0,所以minDist[1] 为 0。 将节点1 加入队列 (下次松弛从节点1开始) -![](https://file.kamacoder.com/pics/20240411115555.png) +![](https://file1.kamacoder.com/i/algo/20240411115555.png) ------------ @@ -109,7 +109,7 @@ 将节点2、节点3 加入队列,如图: -![](https://file.kamacoder.com/pics/20240411115544.png) +![](https://file1.kamacoder.com/i/algo/20240411115544.png) ----------------- @@ -124,7 +124,7 @@ 将节点4,节点5 加入队列,如图: -![](https://file.kamacoder.com/pics/20240412110348.png) +![](https://file1.kamacoder.com/i/algo/20240412110348.png) -------------------- @@ -134,7 +134,7 @@ 因为没有从节点3作为出发点的边,所以这里就从队列里取出节点3就好,不用做其他操作,如图: -![](https://file.kamacoder.com/pics/20240412110420.png) +![](https://file1.kamacoder.com/i/algo/20240412110420.png) ------------ @@ -147,7 +147,7 @@ 如图: -![](https://file.kamacoder.com/pics/20240412110445.png) +![](https://file1.kamacoder.com/i/algo/20240412110445.png) --------------- @@ -160,7 +160,7 @@ 如图,将节点3加入队列,因为节点6已经在队列里,所以不用重复添加 -![](https://file.kamacoder.com/pics/20240729161116.png) +![](https://file1.kamacoder.com/i/algo/20240729161116.png) 所以我们在加入队列的过程可以有一个优化,**用visited数组记录已经在队列里的元素,已经在队列的元素不用重复加入** @@ -174,7 +174,7 @@ 所以直接从队列中取出,如图: -![](https://file.kamacoder.com/pics/20240411115424.png) +![](https://file1.kamacoder.com/i/algo/20240411115424.png) ---------- @@ -264,7 +264,7 @@ int main() { 至于为什么 双向图且每一个节点和所有其他节点都相连的话,每个节点 都有 n-1 条指向该节点的边, 我再来举个例子,如图: -[](https://file.kamacoder.com/pics/20240416104138.png) +![](https://file1.kamacoder.com/i/algo/20240416104138.png) 图中 每个节点都与其他所有节点相连,节点数n 为 4,每个节点都有3条指向该节点的边,即入度为3。 @@ -329,7 +329,7 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹 如图: -![](https://file.kamacoder.com/pics/20240412111849.png) +![](https://file1.kamacoder.com/i/algo/20240412111849.png) 正权回路 就是有环,但环的总权值为正数。 @@ -536,3 +536,4 @@ main() +
diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 9edde8ace3..63d1be2a30 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -46,7 +46,7 @@ 1 3 5 ``` -![](https://file.kamacoder.com/pics/20240509200224.png) +![](https://file1.kamacoder.com/i/algo/20240509200224.png) ## 思路 @@ -78,7 +78,7 @@ 这里我给大家举一个例子,每条边有起点、终点和边的权值。例如一条边,节点A 到 节点B 权值为value,如图: -![](https://file.kamacoder.com/pics/20240327102620.png) +![](https://file1.kamacoder.com/i/algo/20240327102620.png) minDist[B] 表示 到达B节点 最小权值,minDist[B] 有哪些状态可以推出来? @@ -127,7 +127,7 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value 如图: -![](https://file.kamacoder.com/pics/20240328104119.png) +![](https://file1.kamacoder.com/i/algo/20240328104119.png) 其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 @@ -150,36 +150,36 @@ if (minDist[B] > minDist[A] + value) minDist[B] = minDist[A] + value 边:节点5 -> 节点6,权值为-2 ,minDist[5] 还是默认数值max,所以不能基于 节点5 去更新节点6,如图: -![](https://file.kamacoder.com/pics/20240329113537.png) +![](https://file1.kamacoder.com/i/algo/20240329113537.png) (在复习一下,minDist[5] 表示起点到节点5的最短距离) 边:节点1 -> 节点2,权值为1 ,minDist[2] > minDist[1] + 1 ,更新 minDist[2] = minDist[1] + 1 = 0 + 1 = 1 ,如图: -![](https://file.kamacoder.com/pics/20240329113703.png) +![](https://file1.kamacoder.com/i/algo/20240329113703.png) 边:节点5 -> 节点3,权值为1 ,minDist[5] 还是默认数值max,所以不能基于节点5去更新节点3 如图: -![](https://file.kamacoder.com/pics/20240329113827.png) +![](https://file1.kamacoder.com/i/algo/20240329113827.png) 边:节点2 -> 节点5,权值为2 ,minDist[5] > minDist[2] + 2 (经过上面的计算minDist[2]已经不是默认值,而是 1),更新 minDist[5] = minDist[2] + 2 = 1 + 2 = 3 ,如图: -![](https://file.kamacoder.com/pics/20240329113927.png) +![](https://file1.kamacoder.com/i/algo/20240329113927.png) 边:节点2 -> 节点4,权值为-3 ,minDist[4] > minDist[2] + (-3),更新 minDist[4] = minDist[2] + (-3) = 1 + (-3) = -2 ,如图: -![](https://file.kamacoder.com/pics/20240329114036.png) +![](https://file1.kamacoder.com/i/algo/20240329114036.png) 边:节点4 -> 节点6,权值为4 ,minDist[6] > minDist[4] + 4,更新 minDist[6] = minDist[4] + 4 = -2 + 4 = 2 -![](https://file.kamacoder.com/pics/20240329114120.png) +![](https://file1.kamacoder.com/i/algo/20240329114120.png) 边:节点1 -> 节点3,权值为5 ,minDist[3] > minDist[1] + 5,更新 minDist[3] = minDist[1] + 5 = 0 + 5 = 5 ,如图: -![](https://file.kamacoder.com/pics/20240329114324.png) +![](https://file1.kamacoder.com/i/algo/20240329114324.png) -------- @@ -538,3 +538,4 @@ main() ### C +
diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 5dddf45013..957b8a80bc 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -78,7 +78,7 @@ circle 我们拿题目中示例来画一个图: -![](https://file.kamacoder.com/pics/20240705161426.png) +![](https://file1.kamacoder.com/i/algo/20240705161426.png) 图中 节点1 到 节点4 的最短路径是多少(题目中的最低运输成本) (注意边可以为负数的) @@ -86,7 +86,7 @@ circle 而图中有负权回路: -![](https://file.kamacoder.com/pics/20240402103712.png) +![](https://file1.kamacoder.com/i/algo/20240402103712.png) 那么我们在负权回路中多绕一圈,我们的最短路径 是不是就更小了 (也就是更低的运输成本) @@ -456,3 +456,4 @@ if __name__ == "__main__": ### C +
diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index 37cfaee0e3..0c00ccb668 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -63,7 +63,7 @@ 本题是最多经过 k 个城市, 那么是 k + 1条边相连的节点。 这里可能有录友想不懂为什么是k + 1,来看这个图: -![](https://file.kamacoder.com/pics/20240402115614.png) +![](https://file1.kamacoder.com/i/algo/20240402115614.png) 图中,节点1 最多已经经过2个节点 到达节点4,那么中间是有多少条边呢,是 3 条边对吧。 @@ -195,7 +195,7 @@ int main() { 起点为节点1, 起点到起点的距离为0,所以 minDist[1] 初始化为0 ,如图: -![](https://file.kamacoder.com/pics/20240409111940.png) +![](https://file1.kamacoder.com/i/algo/20240409111940.png) 其他节点对应的minDist初始化为max,因为我们要求最小距离,那么还没有计算过的节点 默认是一个最大数,这样才能更新最小距离。 @@ -203,21 +203,21 @@ int main() { 边:节点1 -> 节点2,权值为-1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = minDist[1] + (-1) = 0 - 1 = -1 ,如图: -![](https://file.kamacoder.com/pics/20240409111914.png) +![](https://file1.kamacoder.com/i/algo/20240409111914.png) 边:节点2 -> 节点3,权值为1 ,minDist[3] > minDist[2] + 1 ,更新 minDist[3] = minDist[2] + 1 = -1 + 1 = 0 ,如图: -![](https://file.kamacoder.com/pics/20240409111903.png) +![](https://file1.kamacoder.com/i/algo/20240409111903.png) 边:节点3 -> 节点1,权值为-1 ,minDist[1] > minDist[3] + (-1),更新 minDist[1] = 0 + (-1) = -1 ,如图: -![](https://file.kamacoder.com/pics/20240409111849.png) +![](https://file1.kamacoder.com/i/algo/20240409111849.png) 边:节点3 -> 节点4,权值为1 ,minDist[4] > minDist[3] + 1,更新 minDist[4] = 0 + 1 = 1 ,如图: -![](https://file.kamacoder.com/pics/20241018192042.png) +![](https://file1.kamacoder.com/i/algo/20241018192042.png) 以上是对所有边进行的第一次松弛,最后 minDist数组为 :-1 -1 0 1 ,(从下标1算起) @@ -244,7 +244,7 @@ int main() { 在上面画图距离中,对所有边进行第一次松弛,在计算 边(节点2 -> 节点3) 的时候,更新了 节点3。 -![](https://file.kamacoder.com/pics/20240409111903.png) +![](https://file1.kamacoder.com/i/algo/20240409111903.png) 理论上来说节点3 应该在对所有边第二次松弛的时候才更新。 这因为当时是基于已经计算好的 节点2(minDist[2])来做计算了。 @@ -331,11 +331,11 @@ int main() { 所构成是图是一样的,都是如下的这个图,但给出的边的顺序是不一样的。 -![](https://file.kamacoder.com/pics/20240410154340.png) +![](https://file1.kamacoder.com/i/algo/20240410154340.png) 再用版本一的代码是运行一下,发现结果输出是 1, 是对的。 -![](https://file.kamacoder.com/pics/20240410154940.png) +![](https://file1.kamacoder.com/i/algo/20240410154940.png) 分明刚刚输出的结果是 -2,是错误的,怎么 一样的图,这次输出的结果就对了呢? @@ -345,7 +345,7 @@ int main() { 初始化: -![](https://file.kamacoder.com/pics/20240410155545.png) +![](https://file1.kamacoder.com/i/algo/20240410155545.png) 边:节点3 -> 节点1,权值为-1 ,节点3还没有被计算过,节点1 不更新。 @@ -355,7 +355,7 @@ int main() { 边:节点1 -> 节点2,权值为 -1 ,minDist[2] > minDist[1] + (-1),更新 minDist[2] = 0 + (-1) = -1 ,如图: -![](https://file.kamacoder.com/pics/20240410160046.png) +![](https://file1.kamacoder.com/i/algo/20240410160046.png) 以上是对所有边 松弛一次的状态。 @@ -472,7 +472,7 @@ int main() { 但大家会发现,以上代码大家提交后,怎么耗时这么多? -![](https://file.kamacoder.com/pics/20240418113308.png) +![](https://file1.kamacoder.com/i/algo/20240418113308.png) 理论上,SPFA的时间复杂度不是要比 bellman_ford 更优吗? @@ -554,7 +554,7 @@ int main() { 以上代码提交后,耗时情况: -![](https://file.kamacoder.com/pics/20240418113952.png) +![](https://file1.kamacoder.com/i/algo/20240418113952.png) 大家发现 依然远比 bellman_ford 的代码版本 耗时高。 @@ -579,11 +579,11 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 在以下这个图中,求节点1 到 节点7 最多经过2个节点 的最短路是多少呢? -![](https://file.kamacoder.com/pics/20240508112249.png) +![](https://file1.kamacoder.com/i/algo/20240508112249.png) 最短路显然是: -![](https://file.kamacoder.com/pics/20240508112416.png) +![](https://file1.kamacoder.com/i/algo/20240508112416.png) 最多经过2个节点,也就是3条边相连的路线:节点1 -> 节点2 -> 节点6-> 节点7 @@ -591,24 +591,24 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 初始化如图所示: -![](https://file.kamacoder.com/pics/20240130115306.png) +![](https://file1.kamacoder.com/i/algo/20240130115306.png) 找距离源点最近且没有被访问过的节点,先找节点1 -![](https://file.kamacoder.com/pics/20240130115421.png) +![](https://file1.kamacoder.com/i/algo/20240130115421.png) 距离源点最近且没有被访问过的节点,找节点2: -![](https://file.kamacoder.com/pics/20240130121240.png) +![](https://file1.kamacoder.com/i/algo/20240130121240.png) 距离源点最近且没有被访问过的节点,找到节点3: -![](https://file.kamacoder.com/pics/20240130120434.png) +![](https://file1.kamacoder.com/i/algo/20240130120434.png) 距离源点最近且没有被访问过的节点,找到节点4: -![](https://file.kamacoder.com/pics/20240201105335.png) +![](https://file1.kamacoder.com/i/algo/20240201105335.png) 此时最多经过2个节点的搜索就完毕了,但结果中minDist[7] (即节点7的结果)并没有被更。 @@ -922,3 +922,4 @@ if __name__ == "__main__": ### C +
diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index 97765ebc7b..53e66ee46f 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -155,7 +155,7 @@ grid[i][j][k] = m,表示 节点i 到 节点j 以[1...k] 集合为中间节点 grid数组是一个三维数组,那么我们初始化的数据在 i 与 j 构成的平层,如图: -![](https://file.kamacoder.com/pics/20240425104247.png) +![](https://file1.kamacoder.com/i/algo/20240425104247.png) 红色的 底部一层是我们初始化好的数据,注意:从三维角度去看初始化的数据很重要,下面我们在聊遍历顺序的时候还会再讲。 @@ -202,7 +202,7 @@ vector>> grid(n + 1, vector>(n + 1, vector(n 所以遍历k 的for循环一定是在最外面,这样才能一层一层去遍历。如图: -![](https://file.kamacoder.com/pics/20240424120109.png) +![](https://file1.kamacoder.com/i/algo/20240424120109.png) 至于遍历 i 和 j 的话,for 循环的先后顺序无所谓。 @@ -234,7 +234,7 @@ for (int i = 1; i <= n; i++) { 此时就遍历了 j 与 k 形成一个平面,i 则是纵面,那遍历 就是这样的: -![](https://file.kamacoder.com/pics/20240424115827.png) +![](https://file1.kamacoder.com/i/algo/20240424115827.png) 而我们初始化的数据 是 k 为0, i 和 j 形成的平面做初始化,如果以 k 和 j 形成的平面去一层一层遍历,就造成了 递推公式 用不上上一轮计算的结果,从而导致结果不对(初始化的部分是 i 与j 形成的平面,在初始部分有讲过)。 @@ -253,7 +253,7 @@ for (int i = 1; i <= n; i++) { 就是图: -![](https://file.kamacoder.com/pics/20240424120942.png) +![](https://file1.kamacoder.com/i/algo/20240424120942.png) 求节点1 到 节点 2 的最短距离,运行结果是 10 ,但正确的结果很明显是3。 @@ -267,7 +267,7 @@ for (int i = 1; i <= n; i++) { 而遍历k 的for循环如果放在中间呢,同样是 j 与k 行程一个平面,i 是纵面,遍历的也是这样: -![](https://file.kamacoder.com/pics/20240424115827.png) +![](https://file1.kamacoder.com/i/algo/20240424115827.png) 同样不能完全用上初始化 和 上一层计算的结果。 @@ -283,7 +283,7 @@ for (int i = 1; i <= n; i++) { 图: -![](https://file.kamacoder.com/pics/20240425112636.png) +![](https://file1.kamacoder.com/i/algo/20240425112636.png) 求 节点1 到节点3 的最短距离,如果k循环放中间,程序的运行结果是 -1,也就是不能到达节点3。 @@ -574,3 +574,4 @@ if __name__ == '__main__': ### C +
diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index 2f0dcdcc87..c71981996b 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -43,7 +43,7 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240514103953.png) +![](https://file1.kamacoder.com/i/algo/20240514103953.png) 用例解释: @@ -141,7 +141,7 @@ while (m--) { 我在 [图论理论基础篇](./图论理论基础.md) 举了一个例子: -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 这里表达的图是: @@ -887,3 +887,4 @@ async function dfs(graph, x, n) { +
diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 0da2f315a1..93c1fe41fa 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -35,7 +35,7 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240516111613.png) +![](https://file1.kamacoder.com/i/algo/20240516111613.png) 根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 @@ -50,7 +50,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://file.kamacoder.com/pics/20220726094200.png) +![图一](https://file1.kamacoder.com/i/algo/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -72,7 +72,7 @@ 如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。 -![](https://file.kamacoder.com/pics/20250124094043.png) +![](https://file1.kamacoder.com/i/algo/20250124094043.png) 超时写法 (从队列中取出节点再标记,注意代码注释的地方) @@ -557,3 +557,4 @@ object Solution { ### C +
diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 06be926874..3c54278af0 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -36,7 +36,7 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240516111613.png) +![](https://file1.kamacoder.com/i/algo/20240516111613.png) 根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。 @@ -50,7 +50,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://file.kamacoder.com/pics/20220726094200.png) +![图一](https://file1.kamacoder.com/i/algo/20220726094200.png) 这道题题目是 DFS,BFS,并查集,基础题目。 @@ -459,3 +459,4 @@ object Solution { ### C +
diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index f2b9b901d1..2ae1f452e0 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -33,7 +33,7 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240517103410.png) +![](https://file1.kamacoder.com/i/algo/20240517103410.png) 样例输入中,岛屿的最大面积为 4。 @@ -48,7 +48,7 @@ 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: -![图一](https://file.kamacoder.com/pics/20220726094200.png) +![图一](https://file1.kamacoder.com/i/algo/20220726094200.png) 这道题目也是 dfs bfs基础类题目,就是搜索每个岛屿上“1”的数量,然后取一个最大的。 @@ -890,3 +890,4 @@ main(); ### C +
diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index c8fe372cd9..c883100724 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -37,7 +37,7 @@ 提示信息: -![](https://file.kamacoder.com/pics/20240517105557.png) +![](https://file1.kamacoder.com/i/algo/20240517105557.png) 在矩阵中心部分的岛屿,因为没有任何一个单元格接触到矩阵边缘,所以该岛屿属于孤岛,总面积为 1。 @@ -54,11 +54,11 @@ 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, -![](https://file.kamacoder.com/pics/20220830104632.png) +![](https://file1.kamacoder.com/i/algo/20220830104632.png) 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: -![](https://file.kamacoder.com/pics/20220830104651.png) +![](https://file1.kamacoder.com/i/algo/20220830104651.png) 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 @@ -700,3 +700,4 @@ const bfs = (graph, x, y) => { ### C +
diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 265ec31f73..1b31676277 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -43,11 +43,11 @@ 提示信息: -![](https://file.kamacoder.com/pics/20240517110932.png) +![](https://file1.kamacoder.com/i/algo/20240517110932.png) 将孤岛沉没: -![](https://file.kamacoder.com/pics/20240517110953.png) +![](https://file1.kamacoder.com/i/algo/20240517110953.png) 数据范围: @@ -73,7 +73,7 @@ 如图: -![](https://file.kamacoder.com/pics/20240517113813.png) +![](https://file1.kamacoder.com/i/algo/20240517113813.png) 整体C++代码如下,以下使用dfs实现,其实遍历方式dfs,bfs都是可以的。 @@ -503,3 +503,4 @@ const bfs = (graph, x, y) => { ### C +
diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index 5924cb18a9..bf6cd40f43 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -48,7 +48,7 @@ 提示信息: -![](https://file.kamacoder.com/pics/20240517115816.png) +![](https://file1.kamacoder.com/i/algo/20240517115816.png) 图中的蓝色方块上的雨水既能流向第一组边界,也能流向第二组边界。所以最终答案为所有蓝色方块的坐标。 @@ -166,11 +166,11 @@ int main() { 从第一组边界边上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -![](https://file.kamacoder.com/pics/20250304174747.png) +![](https://file1.kamacoder.com/i/algo/20250304174747.png) 从第二组边界上节点出发,如图: (图中并没有把所有遍历的方向都画出来,只画关键部分) -![](https://file.kamacoder.com/pics/20250304174801.png) +![](https://file1.kamacoder.com/i/algo/20250304174801.png) 最后,我们得到两个方向交界的这些节点,就是我们最后要求的节点。 @@ -854,3 +854,4 @@ const isResult = (x, y) => { +
diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 483d777275..8c4964a9e4 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -35,12 +35,12 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240522154055.png) +![](https://file1.kamacoder.com/i/algo/20240522154055.png) 对于上面的案例,有两个位置可将 0 变成 1,使得岛屿的面积最大,即 6。 -![](https://file.kamacoder.com/pics/20240522154110.png) +![](https://file1.kamacoder.com/i/algo/20240522154110.png) 数据范围: @@ -70,11 +70,11 @@ 拿如下地图的岛屿情况来举例: (1为陆地) -![](https://file.kamacoder.com/pics/20220829104834.png) +![](https://file1.kamacoder.com/i/algo/20220829104834.png) 第一步,则遍历地图,并将岛屿的编号和面积都统计好,过程如图所示: -![](https://file.kamacoder.com/pics/20220829105644.png) +![](https://file1.kamacoder.com/i/algo/20220829105644.png) 本过程代码如下: @@ -121,7 +121,7 @@ int largestIsland(vector>& grid) { 第二步过程如图所示: -![](https://file.kamacoder.com/pics/20220829105249.png) +![](https://file1.kamacoder.com/i/algo/20220829105249.png) 也就是遍历每一个0的方格,并统计其相邻岛屿面积,最后取一个最大值。 @@ -663,3 +663,4 @@ const dfs = (graph, visited, x, y, mark) => { ### C +
diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index cfe77c0d38..1358f673d5 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -33,7 +33,7 @@ 【提示信息】 -![](https://file.kamacoder.com/pics/20240522174707.png) +![](https://file1.kamacoder.com/i/algo/20240522174707.png) 从 1 号节点可以到达任意节点,输出 1。 @@ -48,7 +48,7 @@ 接下来我们再画一个图,从图里可以直观看出来,节点6 是 不能到达节点1 的 -![](https://file.kamacoder.com/pics/20240522175451.png) +![](https://file1.kamacoder.com/i/algo/20240522175451.png) 这就很容易让我们想起岛屿问题,只要发现独立的岛,就是不可到达的。 @@ -553,3 +553,4 @@ rl.on('close',()=>{ ### C +
diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index a1ef2a76eb..4492d5cd6d 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -37,7 +37,7 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240524115244.png) +![](https://file1.kamacoder.com/i/algo/20240524115244.png) 岛屿的周长为 14。 @@ -57,14 +57,14 @@ 如果该陆地上下左右的空格是有水域,则说明是一条边,如图: -![](https://file.kamacoder.com/pics/20240524115933.png) +![](https://file1.kamacoder.com/i/algo/20240524115933.png) 陆地的右边空格是水域,则说明找到一条边。 如果该陆地上下左右的空格出界了,则说明是一条边,如图: -![](https://file.kamacoder.com/pics/20240524120105.png) +![](https://file1.kamacoder.com/i/algo/20240524120105.png) 该陆地的下边空格出界了,则说明找到一条边。 @@ -114,7 +114,7 @@ int main() { 因为有一对相邻两个陆地,边的总数就要减2,如图红线部分,有两个陆地相邻,总边数就要减2 -![](https://file.kamacoder.com/pics/20240524120855.png) +![](https://file1.kamacoder.com/i/algo/20240524120855.png) 那么只需要在计算出相邻岛屿的数量就可以了,相邻岛屿数量为cover。 @@ -359,3 +359,4 @@ func parseLine(line string, count int) []int { ### C +
diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 363a188465..9ab3388f17 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -40,7 +40,7 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240527104432.png) +![](https://file1.kamacoder.com/i/algo/20240527104432.png) 数据范围: @@ -422,3 +422,4 @@ const isSame = (u, v) => { ### C +
diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index fe641f53c9..de2435073c 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -9,11 +9,11 @@ 有一个图,它是一棵树,他是拥有 n 个节点(节点编号1到n)和 n - 1 条边的连通无环无向图(其实就是一个线形图),如图: -![](https://file.kamacoder.com/pics/20240905163122.png) +![](https://file1.kamacoder.com/i/algo/20240905163122.png) 现在在这棵树上的基础上,添加一条边(依然是n个节点,但有n条边),使这个图变成了有环图,如图 -![](https://file.kamacoder.com/pics/20240905164721.png) +![](https://file1.kamacoder.com/i/algo/20240905164721.png) 先请你找出冗余边,删除后,使该图可以重新变成一棵树。 @@ -42,7 +42,7 @@ 提示信息 -![](https://file.kamacoder.com/pics/20240527110320.png) +![](https://file1.kamacoder.com/i/algo/20240527110320.png) 图中的 1 2,2 3,1 3 等三条边在删除后都能使原图变为一棵合法的树。但是 1 3 由于是标准输入里最后出现的那条边,所以输出结果为 1 3 @@ -69,13 +69,13 @@ 如图所示,节点A 和节点 B 不在同一个集合,那么就可以将两个 节点连在一起。 -![](https://file.kamacoder.com/pics/20230604104720.png) +![](https://file1.kamacoder.com/i/algo/20230604104720.png) 如果边的两个节点已经出现在同一个集合里,说明着边的两个节点已经连在一起了,再加入这条边一定就出现环了。 如图所示: -![](https://file.kamacoder.com/pics/20230604104330.png) +![](https://file1.kamacoder.com/i/algo/20230604104330.png) 已经判断 节点A 和 节点B 在在同一个集合(同一个根),如果将 节点A 和 节点B 连在一起就一定会出现环。 @@ -157,7 +157,7 @@ int main() { 图: -![](https://file.kamacoder.com/pics/20240527110320.png) +![](https://file1.kamacoder.com/i/algo/20240527110320.png) 输出示例 @@ -376,3 +376,4 @@ const isSame = (u, v) => { ### C +
diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 78132a3256..6ad59c4188 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -9,11 +9,11 @@ 有一种有向树,该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。有向树拥有 n 个节点和 n - 1 条边。如图:  - + 现在有一个有向图,有向图是在有向树中的两个没有直接链接的节点中间添加一条有向边。如图: - + 输入一个有向图,该图由一个有着 n 个节点(节点编号 从 1 到 n),n 条边,请返回一条可以删除的边,使得删除该条边之后该有向图可以被当作一颗有向树。 @@ -42,7 +42,7 @@ 提示信息 - + 在删除 2 3 后有向图可以变为一棵合法的有向树,所以输出 2 3 @@ -64,13 +64,13 @@ 如图: -![](https://file.kamacoder.com/pics/20240527115807.png) +![](https://file1.kamacoder.com/i/algo/20240527115807.png) 找到了节点3 的入度为2,删 1 -> 3 或者 2 -> 3 。选择删顺序靠后便可。 但 入度为2 还有一种情况,情况二,只能删特定的一条边,如图: -![](https://file.kamacoder.com/pics/20240527151456.png) +![](https://file1.kamacoder.com/i/algo/20240527151456.png) 节点3 的入度为 2,但在删除边的时候,只能删 这条边(节点1 -> 节点3),如果删这条边(节点4 -> 节点3),那么删后本图也不是有向树了(因为找不到根节点)。 @@ -81,7 +81,7 @@ 如图: -![](https://file.kamacoder.com/pics/20240527120531.png) +![](https://file1.kamacoder.com/i/algo/20240527120531.png) 对于情况三,删掉构成环的边就可以了。 @@ -596,3 +596,4 @@ const getRemoveEdge = (edges) => { ### C +
diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index af3436901c..6cb5886d20 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -57,7 +57,7 @@ yhn 2 <= N <= 500

- +

@@ -65,7 +65,7 @@ yhn 以示例1为例,从这个图中可以看出 abc 到 def的路线 不止一条,但最短的一条路径上是4个节点。 -![](https://file.kamacoder.com/pics/20250317105155.png) +![](https://file1.kamacoder.com/i/algo/20250317105155.png) 本题只需要求出最短路径的长度就可以了,不用找出具体路径。 @@ -360,3 +360,4 @@ const init = async () => { ### C +
diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index 18802765a9..c5650d9708 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -39,7 +39,7 @@ 文件依赖关系如下: -![](https://file.kamacoder.com/pics/20240510192157.png) +![](https://file1.kamacoder.com/i/algo/20240510192157.png) 所以,文件处理的顺序除了示例中的顺序,还存在 @@ -104,7 +104,7 @@ 以题目中示例为例如图: -![](https://file.kamacoder.com/pics/20240510110836.png) +![](https://file1.kamacoder.com/i/algo/20240510110836.png) 做拓扑排序的话,如果肉眼去找开头的节点,一定能找到 节点0 吧,都知道要从节点0 开始。 @@ -135,17 +135,17 @@ 1、找到入度为0 的节点,加入结果集 -![](https://file.kamacoder.com/pics/20240510113110.png) +![](https://file1.kamacoder.com/i/algo/20240510113110.png) 2、将该节点从图中移除 -![](https://file.kamacoder.com/pics/20240510113142.png) +![](https://file1.kamacoder.com/i/algo/20240510113142.png) ---------------- 1、找到入度为0 的节点,加入结果集 -![](https://file.kamacoder.com/pics/20240510113345.png) +![](https://file1.kamacoder.com/i/algo/20240510113345.png) 这里大家会发现,节点1 和 节点2 入度都为0, 选哪个呢? @@ -153,19 +153,19 @@ 2、将该节点从图中移除 -![](https://file.kamacoder.com/pics/20240510113640.png) +![](https://file1.kamacoder.com/i/algo/20240510113640.png) --------------- 1、找到入度为0 的节点,加入结果集 -![](https://file.kamacoder.com/pics/20240510113853.png) +![](https://file1.kamacoder.com/i/algo/20240510113853.png) 节点2 和 节点3 入度都为0,选哪个都行,这里选节点2 2、将该节点从图中移除 -![](https://file.kamacoder.com/pics/20240510114004.png) +![](https://file1.kamacoder.com/i/algo/20240510114004.png) -------------- @@ -177,7 +177,7 @@ 如果有 有向环怎么办呢?例如这个图: -![](https://file.kamacoder.com/pics/20240510115115.png) +![](https://file1.kamacoder.com/i/algo/20240510115115.png) 这个图,我们只能将入度为0 的节点0 接入结果集。 @@ -252,13 +252,13 @@ while (que.size()) { 如果这里不理解,看上面的模拟过程第一步: -![](https://file.kamacoder.com/pics/20240510113110.png) +![](https://file1.kamacoder.com/i/algo/20240510113110.png) 这事节点1 和 节点2 的入度为 1。 将节点0删除后,图为这样: -![](https://file.kamacoder.com/pics/20240510113142.png) +![](https://file1.kamacoder.com/i/algo/20240510113142.png) 那么 节点0 作为出发点 所连接的节点的入度 就都做了 减一 的操作。 @@ -539,3 +539,4 @@ const init = async () => { ### C +
diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 7d0096d52d..6669ce7ba1 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -9,7 +9,7 @@ 骑士移动规则如图,红色是起始位置,黄色是骑士可以走的地方。 -![](https://file.kamacoder.com/pics/20240626104833.png) +![](https://file1.kamacoder.com/i/algo/20240626104833.png) 棋盘大小 1000 x 1000(棋盘的 x 和 y 坐标均在 [1, 1000] 区间内,包含边界) @@ -108,7 +108,7 @@ int main() 我们来看一下广搜的搜索过程,如图,红色是起点,绿色是终点,黄色是要遍历的点,最后从 起点 找到 达到终点的最短路径是棕色。 -![](https://file.kamacoder.com/pics/20240611143712.png) +![](https://file1.kamacoder.com/i/algo/20240611143712.png) 可以看出 广搜中,做了很多无用的遍历, 黄色的格子是广搜遍历到的点。 @@ -131,11 +131,11 @@ Astar 是一种 广搜的改良版。 有的是 Astar是 dijkstra 的改良版 在BFS中,我们想搜索,从起点到终点的最短路径,要一层一层去遍历。 -![](https://file.kamacoder.com/pics/20240611143712.png) +![](https://file1.kamacoder.com/i/algo/20240611143712.png) 如果 使用A * 的话,其搜索过程是这样的,如图,图中着色的都是我们要遍历的点。 -![](https://file.kamacoder.com/pics/20240611195223.png) +![](https://file1.kamacoder.com/i/algo/20240611195223.png) (上面两图中 最短路长度都是8,只是走的方式不同而已) @@ -684,3 +684,4 @@ int main() { +
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" "b/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" index 362ea9714a..c5122b1760 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" @@ -91,3 +91,4 @@ cout << result[result.size() - 1]; 等大家将图论刷完,就会感受到我的良苦用心。加油 +
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 5eb5127a93..61d49b5297 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -105,13 +105,13 @@ bool isSame(int u, int v) { 搜索过程像是一个多叉树中从叶子到根节点的过程,如图: -![](https://file.kamacoder.com/pics/20230602102619.png) +![](https://file1.kamacoder.com/i/algo/20230602102619.png) 如果这棵多叉树高度很深的话,每次find函数 去寻找根的过程就要递归很多次。 我们的目的只需要知道这些节点在同一个根下就可以,所以对这棵多叉树的构造只需要这样就可以了,如图: -![](https://file.kamacoder.com/pics/20230602103040.png) +![](https://file1.kamacoder.com/i/algo/20230602103040.png) 除了根节点其他所有节点都挂载根节点下,这样我们在寻根的时候就很快,只需要一步, @@ -226,7 +226,7 @@ join(3, 2); 此时构成的图是这样的: -![](https://file.kamacoder.com/pics/20230525111307.png) +![](https://file1.kamacoder.com/i/algo/20230525111307.png) 此时问 1,3是否在同一个集合,我们调用 `join(1, 2); join(3, 2);` 很明显本意要表示 1,3是在同一个集合。 @@ -256,7 +256,7 @@ join(3, 2); 构成的图是这样的: -![](https://file.kamacoder.com/pics/20230525112101.png) +![](https://file1.kamacoder.com/i/algo/20230525112101.png) 因为在join函数里,我们有find函数进行寻根的过程,这样就保证元素 1,2,3在这个有向图里是强连通的。 @@ -275,12 +275,12 @@ join(3, 2); 1、`join(1, 8);` -![](https://file.kamacoder.com/pics/20231122112727.png) +![](https://file1.kamacoder.com/i/algo/20231122112727.png) 2、`join(3, 8);` -![](https://file.kamacoder.com/pics/20231122113857.png) +![](https://file1.kamacoder.com/i/algo/20231122113857.png) 有录友可能想,`join(3, 8)` 在图中为什么 将 元素1 连向元素 3 而不是将 元素 8 连向 元素 3 呢? @@ -288,12 +288,12 @@ join(3, 2); 3、`join(1, 7);` -![](https://file.kamacoder.com/pics/20231122114108.png) +![](https://file1.kamacoder.com/i/algo/20231122114108.png) 4、`join(8, 5);` -![](https://file.kamacoder.com/pics/20231122114847.png) +![](https://file1.kamacoder.com/i/algo/20231122114847.png) 这里8的根是3,那么 5 应该指向 8 的根 3,这里的原因,我们在上面「常见误区」已经讲过了。 但 为什么 图中 8 又直接指向了 3 了呢? @@ -310,11 +310,11 @@ int find(int u) { 5、`join(2, 9);` -![](https://file.kamacoder.com/pics/20231122115000.png) +![](https://file1.kamacoder.com/i/algo/20231122115000.png) 6、`join(6, 9);` -![](https://file.kamacoder.com/pics/20231122115404.png) +![](https://file1.kamacoder.com/i/algo/20231122115404.png) 这里为什么是 2 指向了 6,因为 9的根为 2,所以用2指向6。 @@ -347,13 +347,13 @@ rank表示树的高度,即树中结点层次的最大值。 例如两个集合(多叉树)需要合并,如图所示: -![](https://file.kamacoder.com/pics/20230602172250.png) +![](https://file1.kamacoder.com/i/algo/20230602172250.png) 树1 rank 为2,树2 rank 为 3。那么合并两个集合,是 树1 合入 树2,还是 树2 合入 树1呢? 我们来看两个不同方式合入的效果。 -![](https://file.kamacoder.com/pics/20230602172933.png) +![](https://file1.kamacoder.com/i/algo/20230602172933.png) 这里可以看出,树2 合入 树1 会导致整棵树的高度变的更高,而 树1 合入 树2 整棵树的高度 和 树2 保持一致。 @@ -454,3 +454,4 @@ void join(int u, int v) { 敬请期待 并查集题目精讲系列。 +
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 96e313fc42..718f5484a1 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -18,11 +18,11 @@ 我们用一个方格地图,假如每次搜索的方向为 上下左右(不包含斜上方),那么给出一个start起始位置,那么BFS就是从四个方向走出第一步。 -![图一](https://file.kamacoder.com/pics/20220825104505.png) +![图一](https://file1.kamacoder.com/i/algo/20220825104505.png) 如果加上一个end终止位置,那么使用BFS的搜索过程如图所示: -![图二](https://file.kamacoder.com/pics/20220825102653.png) +![图二](https://file1.kamacoder.com/i/algo/20220825102653.png) 我们从图中可以看出,从start起点开始,是一圈一圈,向外搜索,方格编号1为第一步遍历的节点,方格编号2为第二步遍历的节点,第四步的时候我们找到终止点end。 @@ -30,7 +30,7 @@ 而且地图还可以有障碍,如图所示: -![图三](https://file.kamacoder.com/pics/20220825103900.png) +![图三](https://file1.kamacoder.com/i/algo/20220825103900.png) 在第五步,第六步 我只把关键的节点染色了,其他方向周边没有去染色,大家只要关注关键地方染色的逻辑就可以。 @@ -102,3 +102,4 @@ void bfs(vector>& grid, vector>& visited, int x, int y 相信看完本篇,大家会对广搜有一个基础性的认识,后面再来做对应的题目就会得心应手一些。 +
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" index d89d6411e9..d7b8da94cb 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" @@ -143,3 +143,4 @@ kruscal的主要思路: 图论也是我 《代码随想录》所有章节里 所费精力最大的一个章节。 只为了不负录友们的期待。 大家加油💪🏻 +
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 1611e00aa0..50df8aa6df 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -28,29 +28,29 @@ 如图一,是一个无向图,我们要搜索从节点1到节点6的所有路径。 -![图一](https://file.kamacoder.com/pics/20220707093643.png) +![图一](https://file1.kamacoder.com/i/algo/20220707093643.png) 那么dfs搜索的第一条路径是这样的: (假设第一次延默认方向,就找到了节点6),图二 -![图二](https://file.kamacoder.com/pics/20220707093807.png) +![图二](https://file1.kamacoder.com/i/algo/20220707093807.png) 此时我们找到了节点6,(遇到黄河了,是不是应该回头了),那么应该再去搜索其他方向了。 如图三: -![图三](https://file.kamacoder.com/pics/20220707094011.png) +![图三](https://file1.kamacoder.com/i/algo/20220707094011.png) 路径2撤销了,改变了方向,走路径3(红色线), 接着也找到终点6。 那么撤销路径2,改为路径3,在dfs中其实就是回溯的过程(这一点很重要,很多录友不理解dfs代码中回溯是用来干什么的) 又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图四中,路径4撤销(回溯的过程),改为路径5。 -![图四](https://file.kamacoder.com/pics/20220707094322.png) +![图四](https://file1.kamacoder.com/i/algo/20220707094322.png) 又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图五,路径6撤销(回溯的过程),改为路径7,路径8 和 路径7,路径9, 结果发现死路一条,都走到了自己走过的节点。 -![图五](https://file.kamacoder.com/pics/20220707094813.png) +![图五](https://file1.kamacoder.com/i/algo/20220707094813.png) 那么节点2所连接路径和节点3所链接的路径 都走过了,撤销路径只能向上回退,去选择撤销当初节点4的选择,也就是撤销路径5,改为路径10 。 如图图六: -![图六](https://file.kamacoder.com/pics/20220707095232.png) +![图六](https://file1.kamacoder.com/i/algo/20220707095232.png) 上图演示中,其实我并没有把 所有的 从节点1 到节点6的dfs(深度优先搜索)的过程都画出来,那样太冗余了,但 已经把dfs 关键的地方都涉及到了,关键就两点: @@ -180,7 +180,7 @@ for (选择:本节点所连接的其他节点) { 如图七所示, 路径2 已经走到了 目的地节点6,那么 路径2 是如何撤销,然后改为 路径3呢? 其实这就是 回溯的过程,撤销路径2,走换下一个方向。 -![图七](https://file.kamacoder.com/pics/20220708093544.png) +![图七](https://file1.kamacoder.com/i/algo/20220708093544.png) ## 总结 @@ -194,3 +194,4 @@ for (选择:本节点所连接的其他节点) { 后面我也会给大家安排具体练习的题目,依旧是代码随想录的风格,循序渐进由浅入深! +
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" index 9bec322765..fb52c83921 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -17,15 +17,15 @@ 有向图是指 图中边是有方向的: -![](https://file.kamacoder.com/pics/20240510195737.png) +![](https://file1.kamacoder.com/i/algo/20240510195737.png) 无向图是指 图中边没有方向: -![](https://file.kamacoder.com/pics/20240510195451.png) +![](https://file1.kamacoder.com/i/algo/20240510195451.png) 加权有向图,就是图中边是有权值的,例如: -![](https://file.kamacoder.com/pics/20240510195821.png) +![](https://file1.kamacoder.com/i/algo/20240510195821.png) 加权无向图也是同理。 @@ -35,7 +35,7 @@ 例如,该无向图中,节点4的度为5,节点6的度为3。 -![](https://file.kamacoder.com/pics/20240511115029.png) +![](https://file1.kamacoder.com/i/algo/20240511115029.png) 在有向图中,每个节点有出度和入度。 @@ -45,7 +45,7 @@ 例如,该有向图中,节点3的入度为2,出度为1,节点1的入度为0,出度为2。 -![](https://file.kamacoder.com/pics/20240511115235.png) +![](https://file1.kamacoder.com/i/algo/20240511115235.png) ## 连通性 @@ -56,11 +56,11 @@ 在无向图中,任何两个节点都是可以到达的,我们称之为连通图 ,如图: -![](https://file.kamacoder.com/pics/20240511102351.png) +![](https://file1.kamacoder.com/i/algo/20240511102351.png) 如果有节点不能到达其他节点,则为非连通图,如图: -![](https://file.kamacoder.com/pics/20240511102449.png) +![](https://file1.kamacoder.com/i/algo/20240511102449.png) 节点1 不能到达节点4。 @@ -72,7 +72,7 @@ 我们来看这个有向图: -![](https://file.kamacoder.com/pics/20240511104531.png) +![](https://file1.kamacoder.com/i/algo/20240511104531.png) 这个图是强连通图吗? @@ -82,7 +82,7 @@ 下面这个有向图才是强连通图: -![](https://file.kamacoder.com/pics/20240511113101.png) +![](https://file1.kamacoder.com/i/algo/20240511113101.png) ### 连通分量 @@ -91,7 +91,7 @@ 只看概念大家可能不理解,我来画个图: -![](https://file.kamacoder.com/pics/20240511111559.png) +![](https://file1.kamacoder.com/i/algo/20240511111559.png) 该无向图中 节点1、节点2、节点5 构成的子图就是 该无向图中的一个连通分量,该子图所有节点都是相互可达到的。 @@ -111,7 +111,7 @@ 如图: -![](https://file.kamacoder.com/pics/20240511112951.png) +![](https://file1.kamacoder.com/i/algo/20240511112951.png) 节点1、节点2、节点3、节点4、节点5 构成的子图是强连通分量,因为这是强连通图,也是极大图。 @@ -132,11 +132,11 @@ 例如图: -![](https://file.kamacoder.com/pics/20240511112951.png) +![](https://file1.kamacoder.com/i/algo/20240511112951.png) 图中有8条边,我们就定义 8 * 2的数组,即有n条边就申请n * 2,这么大的数组: -![](https://file.kamacoder.com/pics/20250110114348.png) +![](https://file1.kamacoder.com/i/algo/20250110114348.png) 数组第一行:6 7,就表示节点6 指向 节点7,以此类推。 @@ -162,7 +162,7 @@ 如图: -![](https://file.kamacoder.com/pics/20240222110025.png) +![](https://file1.kamacoder.com/i/algo/20240222110025.png) 在一个 n (节点数)为8 的图中,就需要申请 8 * 8 这么大的空间。 @@ -188,7 +188,7 @@ 邻接表的构造如图: -![](https://file.kamacoder.com/pics/20240223103713.png) +![](https://file1.kamacoder.com/i/algo/20240223103713.png) 这里表达的图是: @@ -246,3 +246,4 @@ dfs 和 bfs 一种搜索算法,可以在不同的数据结构上进行搜索 敬请期待! +
diff --git "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index e83880dab1..194f1f5ee2 100644 --- "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -17,7 +17,7 @@ 最短路算法比较复杂,而且各自有各自的应用场景,我来用一张表把讲过的最短路算法的使用场景都展现出来: -![](https://file.kamacoder.com/pics/20240508121355.png) +![](https://file1.kamacoder.com/i/algo/20240508121355.png) (因为A * 属于启发式搜索,和上面最短路算法并不是一类,不适合一起对比,所以没有放在一起) @@ -51,3 +51,4 @@ +
diff --git a/problems/qita/acm.md b/problems/qita/acm.md index 33dcd8a652..d4d942daca 100644 --- a/problems/qita/acm.md +++ b/problems/qita/acm.md @@ -11,15 +11,15 @@ [知识星球](https://programmercarl.com/other/kstar.html)里很多录友的日常打卡中,都表示被 ACM模式折磨过: -
+
-
+
-
+
-
+
-
+
所以我正式推出:**卡码网**([https://kamacoder.com](https://kamacoder.com)),**专门帮助大家练习ACM模式**。 @@ -43,17 +43,17 @@ 来看看这极简的界面,没有烂七八糟的功能,只有刷题! -
+
在「状态」这里可以看到 大家提交的代码和判题记录,目前卡码网([https://kamacoder.com](https://kamacoder.com))几乎无时无刻都有卡友在提交代码。 看看大家周六晚上都在做什么,刷哪些题目。 -
+
提交代码的界面是这样的,**目前支持所有主流刷题语言**。 -
+
## 题解 @@ -63,7 +63,7 @@ [https://github.com/youngyangyang04/kamacoder-solutions](https://github.com/youngyangyang04/kamacoder-solutions) -
+
**欢迎去Github上star,欢迎fork,也欢迎来Github仓库贡献其他语言版本,成为contributor**。 @@ -71,7 +71,7 @@ 目前已经有两位录友贡献C和Java版本了。 -
+
期待在Github(https://github.com/youngyangyang04/kamacoder-solutions) 的contributors上也出现你的头像。 diff --git a/problems/qita/acm_backup.md b/problems/qita/acm_backup.md new file mode 100755 index 0000000000..d4d942daca --- /dev/null +++ b/problems/qita/acm_backup.md @@ -0,0 +1,89 @@ + +# 如何练习ACM模式输入输出模式 | 如何准备笔试 | 卡码网 + +卡码网地址:[https://kamacoder.com](https://kamacoder.com) + +## 为什么卡码网 + +录友们在求职的时候会发现,很多公司的笔试题和面试题都是ACM模式, 而大家习惯去力扣刷题,力扣是核心代码模式。 + +当大家在做ACM模式的算法题的时候,需要自己处理数据的输入输出,**如果没有接触过的话,还是挺难的**。 + +[知识星球](https://programmercarl.com/other/kstar.html)里很多录友的日常打卡中,都表示被 ACM模式折磨过: + +
+ +
+ +
+ +
+ +
+ +所以我正式推出:**卡码网**([https://kamacoder.com](https://kamacoder.com)),**专门帮助大家练习ACM模式**。 + +那么之前大家去哪里练习ACM模式呢? + +去牛客做笔试真题,结果发现 ACM模式没练出来,题目倒是巨难,一点思路都没有,代码更没有写,ACM模式无从练起。 + +去洛谷,POJ上练习? 结果发现 题目超多,不知道从哪里开始刷,也没有一个循序渐进的刷题顺序。 + +**而卡码网上有我精选+制作的25道题目**!我还把25题的后台测试数据制作了一遍,保证大家练习的效果。 + +为什么题目不多,只有25道? + +因为大家练习ACM模式不需要那么多题目,有一个循序渐进的练习过程就好了。 + +这25道题目包含了数组、链表、哈希表、字符串、二叉树、动态规划以及图的的题目,常见的输入输出方式都覆盖了。 + +**这是最精华的25道题目**!。 + +## 卡码网长什么样 + +来看看这极简的界面,没有烂七八糟的功能,只有刷题! + +
+ +在「状态」这里可以看到 大家提交的代码和判题记录,目前卡码网([https://kamacoder.com](https://kamacoder.com))几乎无时无刻都有卡友在提交代码。 +看看大家周六晚上都在做什么,刷哪些题目。 + +
+ + +提交代码的界面是这样的,**目前支持所有主流刷题语言**。 + +
+ +## 题解 + +基本大家来卡码网([https://kamacoder.com](https://kamacoder.com))练习ACM模式,都是对输入输出不够了解的,所以想看现成的题解,看看究竟是怎么处理的。 + +所以我用C++把卡码网上25道题目的题解都写了,并发布到Github上: + +[https://github.com/youngyangyang04/kamacoder-solutions](https://github.com/youngyangyang04/kamacoder-solutions) + +
+ +**欢迎去Github上star,欢迎fork,也欢迎来Github仓库贡献其他语言版本,成为contributor**。 + +如果不懂如何和开源项目提交代码,[可以看这里](https://www.programmercarl.com/qita/join.html) + +目前已经有两位录友贡献C和Java版本了。 + +
+ +期待在Github(https://github.com/youngyangyang04/kamacoder-solutions) 的contributors上也出现你的头像。 + +目前题解只有C++代码吗? + +当然不是,大多数题目已经有了 Java、python、C版本。 **其他语言版本,就给录友们成为contributor的机会了**。 + +## 最后 + +卡码网地址:[https://kamacoder.com](https://kamacoder.com) + +快去体验吧,笔试之前最好 把卡码网25道题目都刷完。 + +期待录友们成为最早一批把卡码网刷爆的coder! + diff --git a/problems/qita/algo_pdf.md b/problems/qita/algo_pdf.md new file mode 100755 index 0000000000..76e2d16af1 --- /dev/null +++ b/problems/qita/algo_pdf.md @@ -0,0 +1,70 @@ +# 代码随想录完整版PDF下载 | 合集下载 | 百度云 | + +代码随想录已经是很多学习算法的小伙伴刷题必备的资料,也得到非常多的好评,这是Carl继续创作的动力。 + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +估计绝大多数录友之前也都下载过代码随想录PDF,但是那是我两年前整理的了。 + +![](https://file1.kamacoder.com/i/algo/20230815200530.png) + +如今,很多题目的讲解都改了上十遍,很多图都重画过。 + +之前的PDF一直都没有全集,而且章节也不全,主要是重点章节:二叉树、回溯算法、贪心、动态规划的整理。 + +也有太多录友和我反馈过:由于XXX原因,自己不能上网,看不了网站,pdf有完整版吗? + +其实录友们的需求我都记下来了,就是工作太多,我只能慢慢一项一项去处理。 + +**虽然慢,但我一直在做**! + +现在代码随想录网站最新的内容以及全集整理完毕。 + +
+ +这份PDF整理的非常精细,并把我的[算法公开课](./gongkaike.md)视频,对应题目的链接都放上去了: + +
+ +这份《代码随想录》PDF 和 《代码随想录》纸质版 和 代码随想录网站基本一致,大家选一个合适自己的阅读方式就好。 + +不过这里我依然建议大家尽量看代码随想录网站(programmercarl.com),因为网站一直都是最新的,也是经常更新的。 + +PDF可以作为辅助,例如不能上网的时候。 + +昨天我在[知识星球](./kstar.md)里第一时间公布这份全版代码随想录PDF下载的消息 + +
+ +同时有我企业微信的录友,都接到了这份PDF的推送: + +
+ +
+ +
+ +
+ +
+ +现在我把它免费分享给录友们,大家可以加我的企业微信,备注:简单自我介绍+pdf ,例如:XX大学研二-pdf 或者 XX城市后端开发-pdf ,通过之后,会直接发给大家的。 + +
+ + + diff --git a/problems/qita/ewaishuoming.md b/problems/qita/ewaishuoming.md new file mode 100755 index 0000000000..7c3f690368 --- /dev/null +++ b/problems/qita/ewaishuoming.md @@ -0,0 +1,16 @@ + + +# 本模块说明 + +本模块题目,暂时没有纳入「代码随想录」算法教程体系之中。 + +* 本模块部分题解还不够完善。 +* 本模块部分题目和「代码随想录」中是相似的。 +* 本模块题解并没有体系化 + +很多录友反馈,除了「代码随想录」还有没有其他题目可以练手,最好也有题解,所以我才把这些题解放出来。本模块题目可以作为大家刷题的一个补充。 + +加油💪 + + + diff --git a/problems/qita/gongkaike.md b/problems/qita/gongkaike.md new file mode 100755 index 0000000000..26c874b0c8 --- /dev/null +++ b/problems/qita/gongkaike.md @@ -0,0 +1,162 @@ + +# 代码随想录算法公开课 | 最强算法公开课 + +和录友们汇报一下,**代码随想录算法公开课**已经更新完毕了。 + +由我亲自录制了140期算法视频,覆盖了 [《代码随想录》纸质版](./publish.md)上全部题目的讲解。 + +视频全部免费开放在[B站:代码随想录](https://www.bilibili.com/video/BV1fA4y1o715) + +目录就在视频播放的右边,完全按照代码随想录的顺序讲解,配合 《代码随想录》或者代码随想录网站一起学习,味道更佳! + +
+ +从在22年的5月份开始决定把《代码随想录》上的算法题都由我亲自讲解一波。 + +当时录了第一期算法视频 「二分查找」: + +
+ +别看现在这期视频有10w的播放量,因为都是后序录友们自己找到我的视频来看的,一年后才到10w播放。 + +当时这个视频发出去,播放量就几百。 + +毕竟这种算法视频和普通娱乐或者范技术类视频没法比,平台也不会推荐的。 + +我的视频播放量虽然低,但只要看过视频的录友,评论都很高。随便找了几个最新的评论: + +![](https://file1.kamacoder.com/i/algo/20221121094718.png) + +![](https://file1.kamacoder.com/i/algo/20230222100337.png) + +![](https://file1.kamacoder.com/i/algo/20230222113111.png) + +![](https://file1.kamacoder.com/i/algo/20230222163023.png) + +![](https://file1.kamacoder.com/i/algo/20230223235500.png) + +![](https://file1.kamacoder.com/i/algo/20230323211739.png) + +![](https://file1.kamacoder.com/i/algo/20230324150536.png) + +![](https://file1.kamacoder.com/i/algo/20230325215454.png) + +![](https://file1.kamacoder.com/i/algo/20230327100147.png) + +![](https://file1.kamacoder.com/i/algo/20230329101702.png) + +![](https://file1.kamacoder.com/i/algo/20230404202808.png) + +当初也是看到大家的评论,我才下决心继续更下去,从 去年5月份,每周坚持更新四期算法视频,雷打不动,一直坚持到现在。 + +![](https://file1.kamacoder.com/i/algo/20230303170120.png) + +一晃 ,大半年过去了,足足更新了 140期算法视频,已经覆盖了 [《代码随想录》纸质版](./publish.md)上全部题目的讲解。 + +**我应该为数不多(至少目前我还没看到)的技术书籍作者能亲自把书中全部内容以视频的方式讲解出来并免费分享给大家的** + +大家可以想一想这些年买过哪些技术书籍,有作者亲自给大家把每章每一节都做视频讲解并免费开放的。 + +**那么看 [《代码随想录》](./publish.md)的录友们就有这个待遇**! + +讲课录视频,其实是很费精力的,大家看视频,可能看我讲的行云流水,其实我都是做了十足的功课。 + +**我平时养成了只要有空的时候就模拟一遍某算法运行过程的习惯,板书更是擦了写写了再擦**,反复尝试那种方式能给大家讲清楚的,然后再开拍视频。 + +可能大家会想,都出书了照着书讲不就好了吗? 应该不难吧? + +如果这么简单的话,可能市面上 很多技术书籍作者们就都亲自讲解一波了。 + +写出来和讲出来还不是一个维度。 + +讲出来需要很综合的能力包括表达力,而且大家看我的算法视频会发现:我是脱稿的,我没有提示词,摄像头开了就开讲 一镜到底。 + +**我是做了非常多练习才达到这个程度**。 + +很多人看到摄像头,就会紧张,没有提示词就不知道自己下句该说什么了,瞬间就会:我是谁,我在哪,我在干什么? + +## 算法公开课质量如何 + +目前国内算法视频的讲解风格,一般是 录屏力扣写代码 或者 ppt演示。 这样其实录制视频难度低了很多。 + +但大家上油管的话,会发现 海外经典算法视频的up ,都是一个小白板直接开讲。 + +
+
+
+ +这种讲课方式 容易走两个极端,**一种就是非常好,成为经典系列,一种就是被喷讲的像垃圾一样**。 + +如果是 录屏或者ppt演示,这样至少有稿件照着读,或者提示词,就算差也不会差到哪去。 + +所以呢,我选择这种白板模拟思路直接手撕代码的讲课方式,也是给自己一个挑战,目前的口碑来看,还有走向了好的那个极端。 + + +关于质量究竟如何,学习效果如何,大家可以去B站上去看(B站同名:[代码随想录](https://www.bilibili.com/video/BV1fA4y1o715)),有口皆碑! + + +## 辛苦录视频为了啥 + +再说说这个辛苦录视频,一忙就忙大半年,投入这么多时间和精力,最后为什么还免费开放。 + +书 + 付费视频讲解 或者 免费网站内容 + 付费视频讲解 或者 免费网站内容 + 部分视频免费 + 部分视频付费,**这些都是非常好的盈利模式**,而且还可持续变现的。 + +以代码随想录目前的影响力来说,我这套140集视频教程,不用很贵,定价99元,每年卖出上万份问题不大。 + +这可是一笔非常可观的收入!(真的很香!)而且还是持续收入,后期还不用什么去维护,不像 [知识星球](./kstar.md) 或者 [算法训练营](./xunlianying.md) 我需要花大量时间给录友们答疑。 + +那我为什么不这么做呢? + +**“代码随想录” 这五个字,我是会用一生去经营的**,凡事要看的长远,不是什么赚钱就立刻要去干什么。 + +这套算法公开课视频,不仅造福录友们,也放大了代码随想录的品牌影响,这是双赢的。 + +**免费硬核的算法内容是 代码随想录的立身之本**,也是 大家为什么学算法学编程首选代码随想录的根本所在。 + +如果有点影响力了,就暗插各种付费,这样不持久! **也会伤了很多录友的心**。 + +所以目前 代码随想录网站(programmercarl.com),代码随想录Github(https://github.com/youngyangyang04/leetcode-master),代码随想录算法公开课(B站:代码随想录),**都是完全免费**,也足够大家学习算法所用。 + +**我的免费算法视频内容,要比绝大多数视频上大家付费的 算法视频课、算法训练营质量要高得多**,视频课程基于《代码随想录》的刷题顺序来录制,会让视频内容非常系统,而不是东一块,西一块的。 + +至于《代码随想录》纸质版的内容其实和代码随想录网站是一样的,很的录友买纸质版是因为习惯看纸质版,有的是仅仅是为了留念和收藏。 + +而且以上开源内容,我还会持续优化迭代的,不会做完了就放着不管,如果一年前刷完代码随想录的录友现在在重看一遍 代码随想录网站,**你会发现很多题解了又多了很多图,又优化了很多讲解内容**。 + +这是我的github提交记录:(https://github.com/youngyangyang04) + +
+ +每天或多或少多要优化一点点。 **每天的量可能不多,但每天都在优化**! + +如果感觉代码随想录网站 和 代码随想录算法公开课对大家确实有帮助,不用买书,欢迎去[豆瓣](https://book.douban.com/subject/35680544/)给一个好评就好,感谢录友们的支持。 + + +## 公开课 + +再回头说说目前算法公开课, 其实直到现在 我新发的视频播放也就两三千的播放量。 + +
+ +(大家现在去B站上去看,可能已经上万播放量,但新发的时候 播放量一直都很惨淡) + +但为什么发视频就两三千播放量,就可以在B站聚集10w录友。 + +
+ +对于B站十万粉的号主来说,好像都得有几个百万播放的爆款视频。 + +**但我的视频从来没有爆款过,也没有被平台推荐过,都是非常稳定的几千播放**。 + +很多录友都是主动搜索找过来的就关注了,或者身边的人推荐来的。 + +只要是真正能给大家带来价值的,真正能让大家学明白算法,就会得到认可。 + +所以,**酒香不怕巷子深,真正有价值的内容,不需要蹭热点,想学习的人一定会找到你**。 + +目前《代码随想录》上的算法讲解视频终于更新完了,后面有就有足够的精力去更图论、排序、高级数据结构了。 + +算法公开课全部发布在B站上,链接直达:[《代码随想录》算法公开课](https://www.bilibili.com/video/BV1fA4y1o715) + +最后,**认准代码随想录,学习算法不迷路**。加油💪🏻 + diff --git a/problems/qita/join.md b/problems/qita/join.md index 027b81726a..232d84ca2f 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -28,10 +28,10 @@ 点击这里Fetch upstream。 -
+
点击之后,这里就会显示最新的信息了 -
+
注意这时是你的远端仓库为最新版本,本地还不是最新的,本地要git pull一下。 @@ -39,18 +39,18 @@ 如何提交代码呢,首先把自己的代码提交到自己的fork的远端仓库中,然后open pull request,如图: -
+
点击 open pull request之后,就是如下画面,一个pull request有多个commit。 -
+
然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 -
+
此时你就提交成功了,我会在项目中的pull requests 处理列表里看到你的请求。 -
+
然后如果你发现自己的代码没有合入多半是有问题,如果有问题都有会在pull request里给出留言的, @@ -78,27 +78,27 @@ C++代码 \`\`\` 例如这个commit,在添加java代码的时候,就直接添加代码 -
+
正确的格式应该是这样: -
+
一般发现问题,我也会在代码中给出评论: -
+
这样大家也可以学习一些 提交代码的规范方面的知识 有的录友 是添加的代码块语法,但没有标记是哪种语言,这样的话 代码就不会针对某种语言高亮显示了,也比较影响阅读,例如: -
+
提交python代码的话,要注释好,是python2还是python3 例如这样: -
+
当然python2的话,只这么写就行 @@ -113,7 +113,7 @@ python代码 有的录友是一个pull request 里只有一个commit。 -
+
其实如果大家是平时一天写了两三道题目的话,那么分三个commit,一个pull request提交上来就行。 @@ -127,13 +127,13 @@ python代码 例如这位录友,在提交Java代码的时候,按照题解的意思对Java版本的代码进行的注释,这就很棒👍 -
+
-
+
当然如果大家感觉 已有的代码 不符合以上要求的话,例如 代码思路不够清晰不够规范,注释不够友好,依然欢迎提交优化代码,要记得详细注释哦。 -
+
### 说明具体是哪种方法 @@ -141,10 +141,10 @@ python代码 下面这位录友做的就很好 -
+
-
+
有的题解,是一起给出了多道题目的讲解,例如项目中0102.二叉树的层序遍历.md 中有八道题目,那么大家添加代码的时候 应该在代码注释上,或者 直接写上 是哪个题目的代码。 @@ -162,7 +162,7 @@ python代码 有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 -
+
**代码规范从你我做起!** @@ -183,10 +183,10 @@ python代码 在合入的过程中还要处理冲突的代码, 理解大家代码的思路,解决冲突,然后在力扣提交一下,确保是没问题。 例如同一道题目, 一位录友提交了, 我还没处理如何,另一位录友也对这道题也提交了代码,这样就会发生冲突 -
+
大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。 -
+
一天晚上分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] @@ -201,11 +201,11 @@ python代码 确保这种额外文件不要提交。 -
+
还有添加不同方法的时候,直接用正文格式写,哪种方法就可以了,不要添加目录 ,例如这样,这样整篇文章目录结构就有影响了。 -
+
前面不要加 `## 前序遍历(迭代法)`,直接写`前序遍历(迭代法)`就可以了。 @@ -233,11 +233,11 @@ Go语言代码 甚至发现哪里有语病,也欢迎提交PR来修改,例如下面:就是把【下表】 纠正为【下标】 -
+
不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 -
+
## 特别注意 diff --git a/problems/qita/language.md b/problems/qita/language.md new file mode 100755 index 0000000000..625154e0a5 --- /dev/null +++ b/problems/qita/language.md @@ -0,0 +1,22 @@ + +# 编程语言基础课 + +「代码随想录」的内容是完全免费的。 + +**不过不少录友是编程零基础**,而刷「代码随想录」至少默认你是会一定的编程语言知识的。 + +如果你是编程零基础,又想快速达到刷算法题(或者说刷代码随想录)所需编程语言的水平,推荐 + +* [C++基础课](../ke/cplus.md) +* [Java基础课](../ke/java.md) +* [Python基础课](../ke/python.md) +* [Go基础课](../ke/go.md) +* [Javascript基础课](../ke/js.md) + +如果你有一定数据结构算法知识,想用数据结构做一个小项目的话,推荐: + +* [手写STL(C++)](../ke/stl.md) +* [kv存储-CPP](../ke/kvcplus.md) +* [kv存储-JAVA](../ke/java.md) + + diff --git a/problems/qita/publish.md b/problems/qita/publish.md new file mode 100755 index 0000000000..5a57ec4e4b --- /dev/null +++ b/problems/qita/publish.md @@ -0,0 +1,200 @@ +# 十年所学,终成《代码随想录》 + +**《代码随想录》终于终于正式出版上市了!** (文末附购买链接,直接五折!) + +[B站介绍](https://www.bilibili.com/video/BV13L4y1E7s4/) + +最近这一年不少录友都问我,代码随想录什么时候出版啊? + +**其实我比大家还期待这一刻的到来!** + +先奉上几张书照片:(封面最终选定为梵高的画作,阿姆斯特丹,圣马迪拉莫,1888,海景) + +
+ +
+ +其实在去年,也就是2020年我就已经将这本书的内容写好了,本以为可以很快出版,但我还是严重低估了写书的工作量。 + +因为自己对质量的追求,一直在不断打磨,所以又是一年快过去了。 + +**《代码随想录》总共将近500页,70w字,200多个插图,真的处处都是心血**。 + +出书是一件浩大工程,比写文章难太多了,**真的字字斟酌,大家看书里可能平平淡淡的一句话、一个词语、一个概念,我可能就查阅很多资料,反复推敲:表达是否准确,用词是否到位,生怕辜负了大家的期待**。 + +这是我自己平时书桌的场景: + +
+ +这两年可以说我没有什么娱乐活动,业余生活极其枯燥,都花费在这本书上了,其中艰辛只有自己知道。 + +而此时当大家都能看到《代码随想录》这部作品的时候,其满足感对我来说已经足以。 + +写这本书用了两年,**而真正消化、理解、研究这些算法知识,我用了整整十年**,十年前我就开始写算法文章,妄图闯进算法的大门,这一写就是十年。 + +
+ +**真的是十年所学,两年打磨,终成《代码随想录》!** + +所以当坚持一件事情的时候,一年、两年,甚至三年、五年,不足以看出其效果,但也许坚持十年的时候,才等到真正收获的时刻。 + +## 代码随想录的故事 + +《代码随想录》不是两年憋大招来个横空出世。 + +而是一点一点打磨出来的,其刷题顺序、题解内容、思考深度 都是经过了上10w录友的共同见证。 + +也正是这些内容,把大家汇聚在一起,一起攻克算法的一座又一座高山。 + +与此同时,也几乎每天都会有录友来专门私信我来表达自己的感激: + +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +这些都是大家与“代码随想录”之间的故事,也欢迎大家在文章留言,说一说自己和 “代码随想录”之间的故事。 + +## 《代码随想录》有何不同? + +大家在学习编程、算法,刷题的时候,**真正的苦恼在于没有一套行之有效的刷题顺序**。 + +从何学起,先学什么,再学什么。力扣(Leetcode)上两千道题目,怎么刷,很多人刷题的效率低,主要体现在如下三点: + +* 找题 +* 找到了不合适现阶段做的题 +* 没有全套的优质题解可以参考 + +而市面上基本找不到真正能解决以上痛点的算法书籍。 + +一些书籍是每个知识点蜻蜓点水,然后就叫大家举一反三。 + +一些书籍是一堆题解堆在一起,让大家学起来感受不到知识的连贯性和系统性。 + +断片式的学习,效率怎么能高呢? + +当初我在学习算法的时候,就深感其中的艰难,当我的题量达到一定数量时候,随着反复的琢磨和深入的思考,我再去回顾这些算法题目,**发现如果要是按照合理的顺序来刷题,那效果一定是 事半功倍!** + +所以我将每一个专题中的**题目按照由易到难的顺序进行编排,每一道题目所涉及的知识,前面都会有相应的题目做知识铺垫,做到环环相扣**。 + +**建议大家按照章节顺序阅读本书**,在阅读的过程中会发现我在题目编排上的良苦用心! + +本书不仅在题目编排上精心设计,而且在针对读者最头痛的算法问题上做了详细且深入的讲解。 + +* 关于动态规划,都知道递推公式的重要性,但dp数组的含义、dp数组的初始化、遍历顺序以及如何打印dp数组来排查Bug,这些都很重要。例如,解决背包问题的时候,遍历顺序才是最关键的,也是最难理解的。 + +* 关于回溯算法,题目要求集合之间不可重复,那么就需要去重,各种资料都说要去重,但没有说清楚是“树层去重”还是“树枝去重”——这是我为了说明去重的过程而创造的两个词汇。 + +* 关于KMP算法,都知道使用前缀表进行回退,可什么是前缀表,为什么一定要用前缀表,根据前缀表回退有几种方式,这些却没有说清楚,导致最后大家看的一头雾水。 + +* 关于二叉树,不同的遍历顺序其递归函数究竟如何安排,递归函数什么时候需要返回值,什么时候不用返回值,什么情况下分别使用前、中、后序遍历,迭代法又要如何实现,这些都决定了对二叉树的理解是否到位。 + +同时我针对每一个专题的特点,整理出其通用的解法套路。 + +例如: + +* 在二叉树专题中,总结了递归“三部曲”来帮助读者掌握二叉树中各种遍历方式的写法。 +* 回溯算法中的回溯“三部曲”可以帮助读者理解回溯算法晦涩难懂的过程。 +* 动态规划中的动规“五部曲”可以帮助读者在一套思考框架下去解决动态规划题目。 + +再来说一说动态规划,在程序员面试中,动态规划是公认的最难掌握的算法,也是出现频率最高的算法。 + +**如果仅仅讲解几道题目,即使再举一反三也远远达不到真正理解动态规划的程度**。 + +**如果把动态规划的题目单纯地堆砌在一起,也只会让人越学越懵,陷入“一看就会,一写就废”的怪圈**。 + +讲清楚一道题容易,讲清楚两道题也容易,但把整个动态规划的各个分支讲清楚,把每道题目讲透彻,并用一套方法论把整个动态规划的题目贯彻始终就有难度了。 + +**而帮助大家解决这个问题,这也是这本书的使命所在**。 + +购买方式,可以扫下方二维码,也可以直接[点击这里,京东直达](https://union-click.jd.com/jdc?e=&p=JF8BAMQJK1olXg8EUVhVCkkWAV8IGV8WVAICU24ZVxNJXF9RXh5UHw0cSgYYXBcIWDoXSQVJQwYAUF1UDEsQHDZNRwYlGFh6NVkPcRdyHWwMZRlLHlQDUj02eEcbM244GFIXWQYAUV5VOHsXBF9edVsUXAcDVVtdDUgnAl8IHFkdXw4BU1lfCkoRM2gIEmtIFVpKAxVtOHsUM184G2sWbURsUVpcCEMVAjgIHAxBWFYAAVdfXE8QBGkBGQsdCQEFVgttCkoWB2Y4) + +
+ + +## 目录 + +
+ +
+ +这里不少录友会问,书的内容和Github:https://github.com/youngyangyang04/leetcode-master,和网站:programmercarl.com 有什么区别呢? + +其实写文章相对来说是随意一些的,但书一定要非常严谨。 + +正如我本篇开头所说,书的内容其实一年前就写好的,但排版、纠错、打磨、重新画图,又花费了一年,所以书一定是更精细的,更严谨的。 + +**《代码随想录》的排版看起来非常舒服,会让你发现 原来学算法 会上瘾!** + +
+ +《代码随想录》的推荐语,我都是颇为用心,不是随随便便找个人写一写推荐语来凑数的。 + +
+ +哈工大计算机王院长,百度杰出架构师猛哥,腾讯专家工程师强哥,王道论坛创始人风华哥,**他们是在我学习工作的不同阶段里对我影响非常大的顶级巨佬**。 + +他们的学习方法,做事风格,都是值得每一位技术人学习。同时他们也是每一位技术人的榜样。 + +特别感谢巨佬们能在百忙之中阅读了本书的书稿,并给本书写了评语。 + +## 最后 + +我希望这本书,不仅仅是可以帮助大家学习编程,循序渐进的去学习算法,高效刷题,进大公司。 + +**同时 当你把这本书放在自己的书桌前,床头前的时候,它也会给你一种乘风破浪的勇气!** + +正如封面(梵高,阿姆斯特丹,圣马迪拉莫,1888,海景),一只帆船在波涛汹涌的大海里扬帆远航! + +《代码随想录》这就要和大家见面了,其实很多录友已经迫不及待: + +
+ +
+ +
+ +
+ +这本书原价还挺贵的(毕竟比较厚),但这里申请到了京东五折优惠,大家可以速度下手了。 + +点击下方链接直接五折购买,全网最低价格了。海外的录友们可以在等几天,广州有货之后,就可以配送的海外了。 + +《代码随想录》使用的语言是C++,使用其他语言的录友可以看本书的讲解思路,刷题顺序,然后配合看网站:programmercarl.com,网站上都对应的Java,Python,Go,Js,C,Swift版本 基本可以满足大家的学习需求。 + +购买方式,可以扫下方二维码,也可以直接[点击这里,京东直达](https://union-click.jd.com/jdc?e=&p=JF8BAMQJK1olXg8EUVhVCkkWAV8IGV8WVAICU24ZVxNJXF9RXh5UHw0cSgYYXBcIWDoXSQVJQwYAUF1UDEsQHDZNRwYlGFh6NVkPcRdyHWwMZRlLHlQDUj02eEcbM244GFIXWQYAUV5VOHsXBF9edVsUXAcDVVtdDUgnAl8IHFkdXw4BU1lfCkoRM2gIEmtIFVpKAxVtOHsUM184G2sWbURsUVpcCEMVAjgIHAxBWFYAAVdfXE8QBGkBGQsdCQEFVgttCkoWB2Y4) + +
+ +最后也感谢录友们的陪伴,真心希望大家都有一个好的前程! + +正如《代码随想录》正式出版一样,**你所期盼,终将到来! 加油💪** + + diff --git a/problems/qita/say_feel.md b/problems/qita/say_feel.md new file mode 100755 index 0000000000..00df8c0bf2 --- /dev/null +++ b/problems/qita/say_feel.md @@ -0,0 +1,14 @@ +恭喜你,已经把代码随想录通关了,欢迎在[卡码笔记](https://notes.kamacoder.com/question/102144)记录一下自己的收获,写一篇小作文。 + +不过一刷代码随想录,理解的一定是不到位的,建议二刷之后,对各个经典类型的题目就有自己的想法了。 + +大家可以在自己的博客写一篇 代码随想录一刷总结,记录这阶段性进步的一刻。 + +如果感觉代码随想录对你确实有帮助,不用买书,欢迎去[豆瓣](https://book.douban.com/subject/35680544/)给一个好评就好,代码随想录在豆瓣上被人恶意抹黑,希望录友们可以去说一说自己刷代码随想录的真实感受,感谢录友们的支持。 + +希望大家都能梦想成真,有好的前程,加油💪 + + + + + diff --git a/problems/qita/server.md b/problems/qita/server.md index 72476f5754..890cf8bcd5 100644 --- a/problems/qita/server.md +++ b/problems/qita/server.md @@ -51,7 +51,7 @@ 操作方式这样,我把命令包 包装成一个shell命令,想传那个文件,直接 uploadtomyserver,然后就返回可以下载的链接,这个文件也同时传到了我的服务器上。 -![](https://file.kamacoder.com/pics/20211126165643.png) +![](https://file1.kamacoder.com/i/algo/20211126165643.png) 我也把我的项目代码放在了github上: @@ -93,11 +93,11 @@ https://github.com/youngyangyang04/fileHttpServer 就是这样一个非常普通的查询页面。 -![](https://file.kamacoder.com/pics/20211126160200.png) +![](https://file1.kamacoder.com/i/algo/20211126160200.png) 查询通过之后,就会展现返现群二维码。 -![](https://file.kamacoder.com/pics/20211127160558.png) +![](https://file1.kamacoder.com/i/algo/20211127160558.png) 但要部署在服务器上,因为没有公网IP,别人用不了你的服务。 diff --git a/problems/qita/shejimoshi.md b/problems/qita/shejimoshi.md index 07d5540a25..959a3fa90a 100644 --- a/problems/qita/shejimoshi.md +++ b/problems/qita/shejimoshi.md @@ -7,11 +7,11 @@ 所以卡码网 针对 23种设计,**推出了 23道编程题目,来帮助大家练习设计模式**。 -
+
这里的23到编程题目对应了 23种这几模式。 例如第一题,小明的购物车,就是单例模式: -
+
区别于网上其他教程,本教程的特点是: @@ -40,18 +40,18 @@ 同时还给全部整理到PDF上,这份PDF,我们写的很用心了,来个大家截个图: -
+
-
+
-
+
-
+
关于设计模式的题目,大家现在就可以去 卡码网(kamacoder)去做了。 关于这23道题目对应 设计模式精讲 PDF,也免费分享给录友们,大家可以加我的企业微信获取: -
+
已经有我企业微信的录友,直接发:设计模式,这四个字就好,我会直接发你。 diff --git a/problems/qita/tulunfabu.md b/problems/qita/tulunfabu.md index b45b790d81..8987709564 100644 --- a/problems/qita/tulunfabu.md +++ b/problems/qita/tulunfabu.md @@ -8,7 +8,7 @@ 我知道录友们在等图论等太久了,其实我比大家都着急。 -![大家一直都在催](https://file.kamacoder.com/pics/20240613105618.png) +![大家一直都在催](https://file1.kamacoder.com/i/algo/20240613105618.png) 图论完整版目前已经开放在代码随想录网站:programmercarl.com @@ -20,7 +20,7 @@ * 拓扑排序 * 最短路算法 -![](https://file.kamacoder.com/pics/20240613104436.png) +![](https://file1.kamacoder.com/i/algo/20240613104436.png) **耗时一年之久,代码随想录图论 终于面世了**! @@ -32,21 +32,21 @@ 随便截一些图,大家感受一下: -![](https://file.kamacoder.com/pics/20240613104703.png) +![](https://file1.kamacoder.com/i/algo/20240613104703.png) -![](https://file.kamacoder.com/pics/20240613104824.png) +![](https://file1.kamacoder.com/i/algo/20240613104824.png) -![](https://file.kamacoder.com/pics/20240613104852.png) +![](https://file1.kamacoder.com/i/algo/20240613104852.png) -![](https://file.kamacoder.com/pics/20240613104926.png) +![](https://file1.kamacoder.com/i/algo/20240613104926.png) -![](https://file.kamacoder.com/pics/20240613105007.png) +![](https://file1.kamacoder.com/i/algo/20240613105007.png) -![](https://file.kamacoder.com/pics/20240613105030.png) +![](https://file1.kamacoder.com/i/algo/20240613105030.png) -![](https://file.kamacoder.com/pics/20240613105106.png) +![](https://file1.kamacoder.com/i/algo/20240613105106.png) -![](https://file.kamacoder.com/pics/20240613105143.png) +![](https://file1.kamacoder.com/i/algo/20240613105143.png) 具体内容,大家可以去代码随想录网站(programmercarl.com)去看看,非常精彩! @@ -203,19 +203,19 @@ cout << result[result.size() - 1]; 当大家通过 代码随想录 提升了编程与算法能力,考上研或者找到好工作的时候,于我来说已经是很幸福的事情: -![对笔试帮助大](https://file.kamacoder.com/pics/20230914172536.png) +![对笔试帮助大](https://file1.kamacoder.com/i/algo/20230914172536.png) -![华为od将近满分](https://file.kamacoder.com/pics/20230914172607.png) +![华为od将近满分](https://file1.kamacoder.com/i/algo/20230914172607.png) -![研究生复试](https://file.kamacoder.com/pics/20240621103130.png) +![研究生复试](https://file1.kamacoder.com/i/algo/20240621103130.png) -![红包感谢代码随想录366](https://file.kamacoder.com/pics/20231123151310.png) +![红包感谢代码随想录366](https://file1.kamacoder.com/i/algo/20231123151310.png) -![上岸亚马逊](https://file.kamacoder.com/pics/20240206174151.png) +![上岸亚马逊](https://file1.kamacoder.com/i/algo/20240206174151.png) -![](https://file.kamacoder.com/pics/20220718094112.png) +![](https://file1.kamacoder.com/i/algo/20220718094112.png) -![](https://file.kamacoder.com/pics/20220718094332.png) +![](https://file1.kamacoder.com/i/algo/20220718094332.png) 至此**图论内容 已完全免费开放在代码随想录网站(programmercarl.com),造福广大学习编程的录友们**! diff --git a/problems/qita/tulunshuoming.md b/problems/qita/tulunshuoming.md new file mode 100755 index 0000000000..9d6e761ca8 --- /dev/null +++ b/problems/qita/tulunshuoming.md @@ -0,0 +1,44 @@ + +# 图论模块说明 + + +非常多录友在催更图论,同时大家也反馈面试中深搜广搜也最近常考的类型。 + +其实在代码随想录中的二叉树和回溯算法章节中已经讲过深搜和广搜,二叉树的遍历就是深搜和广搜在二叉树结构上的应用, 而回溯算法本身就是深搜,只不过利用其回溯的过程。 + +那么在图论中,深搜和广搜就是在图上的遍历,图的存储方式一般是 邻接表和邻接矩阵。 + +我已经在更新图论ing,不过还没有跟更新完,**之前计划是把更新完的部分先分享给[训练营](./xunlianying.html)和 [知识星球](./kstar.md) 录友,等全部更新完之后在完整的分享到网站上**。 + +不过其他录友们也很着急,我也算更新了不少了,就先分享出来给大家吧。 + +**我一直坚持给大家打造最硬核的算法教程而且是免费的!这一点一直都不会变!**。 + +(**注意图论章节还没有更新完,还有更精彩的内容在路上**) + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/problems/qita/update.md b/problems/qita/update.md new file mode 100755 index 0000000000..c562fd4b3c --- /dev/null +++ b/problems/qita/update.md @@ -0,0 +1,56 @@ + +## 2021年 8月11日 + +[代码随想录网站正式上线](https://mp.weixin.qq.com/s/-6rd_g7LrVD1fuKBYk2tXQ) + +## 2021年 10月19日 + +更新Java,Python,JS,Go版本题解 + +## 2022年 1月17日 + +添加评论功能和阅读量统计。 + +为了方便大家阅读,使用无需登录的评论插件 valine。 + +但由于本站访问量太大,leancloud的api调用超过3w次,就只能用付费版本了,本站使用2个小时之后就超过了3w次条用,而付费版本一年要上万块。 + +免费的网站实在承担不起,所以仅在部分页面添加的评论区。 + +例如各个专题中的理论基础和本章总结,都添加的评论区。 + +## 2022年 2月22日 + +升级内存和带宽以应对更大的访问量 + +## 2022年 5月12日 + +更新[星球生活](https://programmercarl.com/other/)专栏,题解支持C、TypeScript 语言版本 + +## 2022年 5月19日 + +补充[额外题目](https://programmercarl.com/other/ewaishuoming.html) + +## 2022年 6月10日 + +添加边框,可以方便调节黑暗模式,开始加入Scala 和 C# 语言版本。 + +## 2023年 5月8日 + +题解都配上了《代码随想录》算法公开课视频讲解 + +## 2023年 9月11日 + +更新部分图论内容,深搜广搜和并查集 + +## 2024年 4月7日 + +由于访问量过大,网站访问速度慢一直被很多录友诟病,特别是海外录友访问更卡。 + +这次网站全部上CDN,全球加速,方便全球录友学习。 + +同时添加github评论区,录友可以在每篇文章下打卡了! + +## 更多精彩,敬请期待 + + diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" old mode 100644 new mode 100755 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" old mode 100644 new mode 100755 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" old mode 100644 new mode 100755 index a67cf5db9d..7d25d81876 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -147,7 +147,7 @@ 二叉树专题汇聚为一张图: -![](https://file.kamacoder.com/pics/20211030125421.png) +![](https://file1.kamacoder.com/i/algo/20211030125421.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index 1fbbc9d934..a68f93a901 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -15,7 +15,7 @@ 题目分类大纲如下: -二叉树大纲 +二叉树大纲 说到二叉树,大家对于二叉树其实都很熟悉了,本文呢我也不想教科书式的把二叉树的基础内容再啰嗦一遍,所以以下我讲的都是一些比较重点的内容。 @@ -31,7 +31,7 @@ 如图所示: - + 这棵二叉树为满二叉树,也可以说深度为k,有2^k-1个节点的二叉树。 @@ -46,7 +46,7 @@ 我来举一个典型的例子如题: - + 相信不少同学最后一个二叉树是不是完全二叉树都中招了。 @@ -63,7 +63,7 @@ 下面这两棵树都是搜索树 - + ### 平衡二叉搜索树 @@ -72,7 +72,7 @@ 如图: - + 最后一棵 不是平衡二叉树,因为它的左右两个子树的高度差的绝对值超过了1。 @@ -91,13 +91,13 @@ 链式存储如图: - + 链式存储是大家很熟悉的一种方式,那么我们来看看如何顺序存储呢? 其实就是用数组来存储二叉树,顺序存储的方式如图: - + 用数组来存储二叉树如何遍历的呢? @@ -144,7 +144,7 @@ 大家可以对着如下图,看看自己理解的前后中序有没有问题。 - + 最后再说一说二叉树中深度优先和广度优先遍历实现方式,我们做二叉树相关题目,经常会使用递归的方式来实现深度优先遍历,也就是实现前中后序遍历,使用递归是比较方便的。 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" old mode 100644 new mode 100755 index d001e0f7a7..803b25ae80 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -67,7 +67,7 @@ public: 看代码有点抽象我们来看一下动画(中序遍历): -![中序遍历迭代(统一写法)](https://code-thinking.cdn.bcebos.com/gifs/%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86%E8%BF%AD%E4%BB%A3%EF%BC%88%E7%BB%9F%E4%B8%80%E5%86%99%E6%B3%95%EF%BC%89.gif) +![中序遍历迭代(统一写法)](https://file1.kamacoder.com/i/algo/%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86%E8%BF%AD%E4%BB%A3%EF%BC%88%E7%BB%9F%E4%B8%80%E5%86%99%E6%B3%95%EF%BC%89.gif) 动画中,result数组就是最终结果集。 diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" old mode 100644 new mode 100755 index e011612ca6..efa07d97d9 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -38,7 +38,7 @@ 动画如下: -![二叉树前序遍历(迭代法)](https://code-thinking.cdn.bcebos.com/gifs/%E4%BA%8C%E5%8F%89%E6%A0%91%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%88%E8%BF%AD%E4%BB%A3%E6%B3%95%EF%BC%89.gif) +![二叉树前序遍历(迭代法)](https://file1.kamacoder.com/i/algo/%E4%BA%8C%E5%8F%89%E6%A0%91%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%88%E8%BF%AD%E4%BB%A3%E6%B3%95%EF%BC%89.gif) 不难写出如下代码: (**注意代码中空节点不入栈**) @@ -85,7 +85,7 @@ public: 动画如下: -![二叉树中序遍历(迭代法)](https://code-thinking.cdn.bcebos.com/gifs/%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%88%E8%BF%AD%E4%BB%A3%E6%B3%95%EF%BC%89.gif) +![二叉树中序遍历(迭代法)](https://file1.kamacoder.com/i/algo/%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86%EF%BC%88%E8%BF%AD%E4%BB%A3%E6%B3%95%EF%BC%89.gif) **中序遍历,可以写出如下代码:** @@ -117,7 +117,7 @@ public: 再来看后序遍历,先序遍历是中左右,后序遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: -![前序到后序](https://file.kamacoder.com/pics/20200808200338924.png) +![前序到后序](https://file1.kamacoder.com/i/algo/20200808200338924.png) **所以后序遍历只需要前序遍历的代码稍作修改就可以了,代码如下:** diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" old mode 100644 new mode 100755 diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" index f1ff3a5ffc..70643b7e49 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217.md" @@ -5,15 +5,15 @@ 平时大家在力扣上刷题,就是 核心代码模式,即给你一个函数,直接写函数实现,例如这样: -![](https://file.kamacoder.com/pics/20231109193631.png) +![](https://file1.kamacoder.com/i/algo/20231109193631.png) 而ACM模式,是程序头文件,main函数,数据的输入输出都要自己处理,例如这样: -![](https://file.kamacoder.com/pics/20231109193743.png) +![](https://file1.kamacoder.com/i/algo/20231109193743.png) 大家可以发现 右边代码框什么都没有,程序从头到尾都需要自己实现,本题如果写完代码是这样的: (细心的录友可以发现和力扣上刷题是不一样的) -![](https://file.kamacoder.com/pics/20231109193931.png) +![](https://file1.kamacoder.com/i/algo/20231109193931.png) **如果大家从一开始学习算法就一直在力扣上的话,突然切到ACM模式会非常不适应**。 @@ -21,15 +21,15 @@ 知识星球里也有很多录友,因为不熟悉ACM模式在面试的过程中吃了不少亏。 -
+
-
+
-
+
-
+
-
+
## 面试究竟怎么考? @@ -53,7 +53,7 @@ 你只要能把卡码网首页的25道题目 都刷了 ,就把所有的ACM输入输出方式都练习到位了,不会有任何盲区。 -![](https://file.kamacoder.com/pics/20231109195056.png) +![](https://file1.kamacoder.com/i/algo/20231109195056.png) 而且你不用担心,题目难度太大,直接给自己劝退,**卡码网的前25道题目都是我精心制作的,难度也是循序渐进的**,大家去刷一下就知道了。 diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" index 086e6e0ea0..2eeb7431bf 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" @@ -15,7 +15,7 @@ 其输入用例,就是用一个数组来表述 二叉树,如下: -![](https://file.kamacoder.com/pics/20210914222335.png) +![](https://file1.kamacoder.com/i/algo/20210914222335.png) 一直跟着公众号学算法的录友 应该知道,我在[二叉树:构造二叉树登场!](https://mp.weixin.qq.com/s/Dza-fqjTyGrsRw4PWNKdxA),已经讲过,**只有 中序与后序 和 中序和前序 可以确定一棵唯一的二叉树。 前序和后序是不能确定唯一的二叉树的**。 @@ -24,7 +24,7 @@ 很明显,是后台直接明确了构造规则。 再看一下 这个 输入序列 和 对应的二叉树。 -![](https://file.kamacoder.com/pics/20210914222335.png) +![](https://file1.kamacoder.com/i/algo/20210914222335.png) 从二叉树 推导到 序列,大家可以发现这就是层序遍历。 @@ -36,7 +36,7 @@ 顺序存储,就是用一个数组来存二叉树,其方式如图所示: -![](https://file.kamacoder.com/pics/20210914223147.png) +![](https://file1.kamacoder.com/i/algo/20210914223147.png) 那么此时大家是不是应该知道了,数组如何转化成 二叉树了。**如果父节点的数组下标是i,那么它的左孩子下标就是i * 2 + 1,右孩子下标就是 i * 2 + 2**。 @@ -80,7 +80,7 @@ TreeNode* construct_binary_tree(const vector& vec) { 这个函数最后返回的 指针就是 根节点的指针, 这就是 传入二叉树的格式了,也就是 力扣上的用例输入格式,如图: -![](https://file.kamacoder.com/pics/20210914224422.png) +![](https://file1.kamacoder.com/i/algo/20210914224422.png) 也有不少同学在做ACM模式的题目,就经常疑惑: @@ -176,7 +176,7 @@ int main() { 和 [538.把二叉搜索树转换为累加树](https://mp.weixin.qq.com/s/rlJUFGCnXsIMX0Lg-fRpIw) 中的输入是一样的 -![](https://file.kamacoder.com/pics/20210914222335.png) +![](https://file1.kamacoder.com/i/algo/20210914222335.png) 这里可能又有同学疑惑,你这不一样啊,题目是null,你为啥用-1。 @@ -184,11 +184,11 @@ int main() { 在来看,测试代码输出的效果: -![](https://file.kamacoder.com/pics/20210914230045.png) +![](https://file1.kamacoder.com/i/algo/20210914230045.png) 可以看出和 题目中输入用例 这个图 是一样一样的。 只不过题目中图没有把 空节点 画出来而已。 -![](https://file.kamacoder.com/pics/20210914230118.png) +![](https://file1.kamacoder.com/i/algo/20210914230118.png) 大家可以拿我的代码去测试一下,跑一跑。 @@ -205,7 +205,7 @@ int main() { **[知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)**里有的录友已经开始三刷: -![](https://file.kamacoder.com/pics/20210727234031.png) +![](https://file1.kamacoder.com/i/algo/20210727234031.png) 只做过一遍,真的就是懂了一点皮毛, 第二遍刷才有真的对各个题目有较为深入的理解,也会明白 我为什么要这样安排刷题的顺序了。 @@ -419,4 +419,4 @@ func main() { ``` ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" "b/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" index 27940f1bd5..7d112a19c8 100644 --- "a/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" +++ "b/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" @@ -211,4 +211,4 @@ leetcode是专门针对算法练习的题库,leetcode现在也推出了中文 ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/gitserver.md" "b/problems/\345\211\215\345\272\217/gitserver.md" new file mode 100755 index 0000000000..caf93ec6ec --- /dev/null +++ "b/problems/\345\211\215\345\272\217/gitserver.md" @@ -0,0 +1,312 @@ + +# 一文手把手教你搭建Git私服 + +## 为什么要搭建Git私服 + +很多同学都问文章,文档,资料怎么备份啊,自己电脑和公司电脑怎么随时同步资料啊等等,这里呢我写一个搭建自己的git私服的详细教程 + +为什么要搭建一个Git私服呢,而不是用Github免费的私有仓库,有以下几点: +* Github 私有仓库真的慢,文件一旦多了,或者有图片文件,git pull 的时候半天拉不下来 +* 自己的文档难免有自己个人信息,放在github心里也是担心的 +* 想建几个库就建几个,想几个人合作开发都可以,不香么? + +**网上可以搜到很多git搭建,但是说的模棱两可**,而且有的直接是在本地搭建git服务,既然是备份,搭建在本地哪有备份的意义,一定要有一个远端服务器, 而且自己的电脑和公司的电脑还是同步自己的文章,文档和资料等等。 + + +适合人群: 想通过git私服来备份自己的文章,Markdown,并做版本管理的同学 +最后,写好每篇 Chat 是对我的责任,也是对你的尊重。谢谢大家~ + +正文如下: + +----------------------------- + +## 如何找到可以外网访问服务器 + +有的同学问了,自己的电脑就不能作为服务器么? + +这里要说一下,安装家庭带宽,运营商默认是不会给我们独立分配公网IP的 + +一般情况下是一片区域公用一个公网IP池,所以外网是不能访问到在家里我们使用的电脑的 + +除非我们自己去做映射,这其实非常麻烦而且公网IP池 是不断变化的 + +辛辛苦苦做了映射,运营商给IP一换,我们的努力就白扯了 + +那我们如何才能找到一个外网可以访问的服务器呢,此时云计算拯救了我们。 + +推荐大家选一家云厂商(阿里云,腾讯云,百度云都可以)在上面上买一台云服务器 + +* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob) +* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu) + +云厂商经常做活动,如果从来没有买过云服务器的账号更便宜,低配一年一百块左右的样子,强烈推荐一起买个三年。 + +买云服务器的时候推荐直接安装centos系统。 + +这里要说一下,有了自己的云服务器之后 不仅仅可以用来做git私服 + +**同时还可以做网站,做程序后台,跑程序,做测试**(这样我们自己的电脑就不会因为自己各种搭建环境下载各种包而搞的的烂糟糟),等等等。 + +有自己云服务器和一个公网IP真的是一件非常非常幸福的事情,能体验到自己的服务随时可以部署上去提供给所有人使用的喜悦。 + +外网可以访问的服务器解决了,接下来就要部署git服务了 + +本文将采用centos系统来部署git私服 + +## 服务器端安装Git + +切换至root账户 + +``` +su root +``` + +看一下服务器有没有安装git,如果出现下面信息就说明是有git的 +``` +[root@instance-5fcyjde7 ~]# git +usage: git [--version] [--help] [-c name=value] + [--exec-path[=]] [--html-path] [--man-path] [--info-path] + [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] + [--git-dir=] [--work-tree=] [--namespace=] + [] + +The most commonly used git commands are: + add Add file contents to the index + bisect Find by binary search the change that introduced a bug + branch List, create, or delete branches + checkout Checkout a branch or paths to the working tree + clone Clone a repository into a new directory + commit Record changes to the repository + diff Show changes between commits, commit and working tree, etc + fetch Download objects and refs from another repository + grep Print lines matching a pattern + init Create an empty Git repository or reinitialize an existing one + log Show commit logs + merge Join two or more development histories together + mv Move or rename a file, a directory, or a symlink + pull Fetch from and merge with another repository or a local branch + push Update remote refs along with associated objects + rebase Forward-port local commits to the updated upstream head + reset Reset current HEAD to the specified state + rm Remove files from the working tree and from the index + show Show various types of objects + status Show the working tree status + tag Create, list, delete or verify a tag object signed with GPG + +'git help -a' and 'git help -g' lists available subcommands and some +concept guides. See 'git help ' or 'git help ' +to read about a specific subcommand or concept. +``` + +如果没有git,就安装一下,yum安装的版本默认是 `1.8.3.1` + +``` +yum install git +``` + +安装成功之后,看一下自己安装的版本 + +``` +git --version +``` + +## 服务器端设置Git账户 + +创建一个git的linux账户,这个账户只做git私服的操作,也是为了安全起见 + +如果不新创建一个linux账户,在自己的常用的linux账户下创建的话,哪天手抖 来一个`rm -rf *` 操作 数据可全没了 + +**这里linux git账户的密码设置的尽量复杂一些,我这里为了演示,就设置成为'gitpassword'** +``` +adduser git +passwd gitpassword +``` + +然后就要切换成git账户,进行后面的操作了 +``` +[root@instance-5fcyjde7 ~]# su - git +``` + +看一下自己所在的目录,是不是在git目录下面 + +``` +[git@instance-5fcyjde7 ~]$ pwd +/home/git +``` + +## 服务器端密钥管理 + +创建`.ssh` 目录,如果`.ssh` 已经存在了,可以忽略这一项 + +为啥用配置ssh公钥呢,同学们记不记得我使用github上传代码的时候也要把自己的公钥配置上传到github上 + +这也是方面每次操作git仓库的时候不用再去输入密码 + +``` +cd ~/ +mkdir .ssh +``` + +进入.ssh 文件下,创建一个 `authorized_keys` 文件,这个文件就是后面就是要放我们客户端的公钥 + +``` +cd ~/.ssh +touch authorized_keys +``` + +别忘了`authorized_keys`给设置权限,很多同学发现自己不能免密登陆,都是因为忘记了给`authorized_keys` 设置权限 + +``` +chmod 700 /home/git/.ssh +chmod 600 /home/git/.ssh/authorized_keys +``` + +接下来我们要把客户端的公钥放在git服务器上,我们在回到客户端,创建一个公钥 + +在我们自己的电脑上,有公钥和私钥 两个文件分别是:`id_rsa` 和 `id_rsa.pub` + +如果是`windows`系统公钥私钥的目录在`C:\Users\用户名\.ssh` 下 + +如果是mac 或者 linux, 公钥和私钥的目录这里 `cd ~/.ssh/`, 如果发现自己的电脑上没有公钥私钥,那就自己创建一个 + +创建密钥的命令 + +``` +ssh-keygen -t rsa +``` + +创建密钥的过程中,一路点击回车就可以了。不需要填任何东西 + +把公钥拷贝到git服务器上,将我们刚刚生成的`id_rsa.pub`,拷贝到git服务器的`/home/git/.ssh/`目录 + +在git服务器上,将公钥添加到`authorized_keys` 文件中 + +``` +cd /home/git/.ssh/ +cat id_rsa.pub >> authorized_keys +``` + +如何看我们配置的密钥是否成功呢, 在客户端直接登录git服务器,看看是否是免密登陆 +``` +ssh git@git服务器ip +``` + +例如: + +``` +ssh git@127.0.0.1 +``` + +如果可以免密登录,那就说明服务器端密钥配置成功了 + +## 服务器端部署Git 仓库 + +我们在登陆到git 服务器端,切换为成 git账户 + +如果是root账户切换成git账户 +``` +su - git +``` + +如果是其他账户切换为git账户 +``` +sudo su - git +``` + +进入git目录下 +``` +cd ~/git +``` + +创建我们的第一个Git私服的仓库,我们叫它为world仓库 + +那么首先创建一个文件夹名为: world.git ,然后进入这个目录 + +有同学问,为什么文件夹名字后面要放`.git`, 其实不这样命名也是可以的 + +但是细心的同学可能注意到,我们平时在github上 `git clone` 其他人的仓库的时候,仓库名字后面,都是加上`.git`的 + +例如下面这个例子,其实就是github对仓库名称的一个命名规则,所以我们也遵守github的命名规则。 + +``` +git clone https://github.com/youngyangyang04/NoSQLAttack.git +``` + +所以我们的操作是 +``` +[git@localhost git]# mkdir world.git +[git@localhost git]# cd world.git +``` + +初始化我们的`world`仓库 + +``` +git init --bare + +``` + +**如果我们想创建多个仓库,就在这里创建多个文件夹并初始化就可以了,和world仓库的操作过程是一样一样的** + +现在我们服务端的git仓库就部署完了,接下来就看看客户端,如何使用这个仓库呢 + +## 客户端连接远程仓库 + +我们在自己的电脑上创建一个文件夹 也叫做`world`吧 + +其实这里命名是随意的,但是我们为了和git服务端的仓库名称保持同步。 这样更直观我们操作的是哪一个仓库。 + +``` +mkdir world +cd world +``` + +进入world文件,并初始化操作 + +``` +cd world +git init +``` + +在world目录上创建一个测试文件,并且将其添加到git版本管理中 + +``` +touch test +git add test +git commit -m "add test file" +``` + +将次仓库和远端仓库同步 + +``` +git remote add origin git@git服务器端的ip:world.git +git push -u origin master +``` + +此时这个test测试文件就已经提交到我们的git远端私服上了 + +## Git私服安全问题 + +这里有两点安全问题 + +### linux git的密码不要泄露出去 + +否则,别人可以通过 ssh git@git服务器IP 来登陆到你的git私服服务器上 + +当然了,这里同学们如果买的是云厂商的云服务器的话 + +如果有人恶意想通过 尝试不同密码链接的方式来链接你的服务器,重试三次以上 + +这个客户端的IP就会被封掉,同时邮件通知我们可以IP来自哪里 + +所以大可放心 密码只要我们不泄露出去,基本上不会有人同时不断尝试密码的方式来登上我们的git私服服务器 + +### 私钥文件`id_rsa` 不要给别人 + +如果有人得到了这个私钥,就可以免密码登陆我们的git私服上了,我相信大家也不至于把自己的私钥主动给别人吧 + +## 总结 + +这里就是整个git私服搭建的全过程,安全问题我也给大家列举了出来,接下来好好享受自己的Git私服吧 + +**enjoy!** + diff --git "a/problems/\345\211\215\345\272\217/kvstore.md" "b/problems/\345\211\215\345\272\217/kvstore.md" new file mode 100755 index 0000000000..268fc018c7 --- /dev/null +++ "b/problems/\345\211\215\345\272\217/kvstore.md" @@ -0,0 +1,124 @@ + +# 手把手带你实现存储引擎 + + +之前在 [刷题攻略登上榜首](https://mp.weixin.qq.com/s/wZRTrA9Rbvgq1yEkSw4vfQ)这篇文章中说过,Carl不仅写了刷题攻略,还写了很多优秀的开源项目。 + +在星球里也有很多小伙伴问我关于一些,项目的选择,**相信如果是C++后台开发路线的话,基本都会去做WebServer 服务器**。 + +我在[知识星球](https://programmercarl.com/other/kstar.html)给小伙伴答疑,包括看了这么多简历,**发现WebServer这个项目是真的多,有点烂大街了**。 + +所以今天我把自己曾经开发的 KV存储引擎 给大家介绍一波,大家可以拿去当做自己的项目经验。 + +**相信只要是搞后端的同学应该都要熟悉非关系型数据库redis吧,那么应该知道redis的存储引擎是跳表实现的**。 + +现在很多云厂商提供的云数据库,其底层都是用了Facebook开源的rocksdb,而rocksdb的底层是Google开源的Levedb,**而Levedb的核心实现也是跳表**。 + +所以大家应该知道跳表的应用有多么的广泛了。 + +那么为什么这个项目非常合适大家用来做自己的项目经验呢? + +如果你是后端开发的话,你在简历上一定会写熟悉或者了解redis吧,那么可以进一步介绍一下自己的项目用跳表实现了redis核心引擎。 + +面试官一定会非常感兴趣的,然后你就可以和面试官侃侃而谈你是如何用跳表实现的这个KV存储引擎的。 + +**瞬间逼格就高了,有木有!** + +我在18年的时候,用跳表实现了一个轻量级KV存储引擎,代码也写的非常规范,熟悉我的录友应该知道,我的代码严格按照Google C++ style来的。 + +因为当时我是想把这个项目国际化的,注释和readme都是英文的,但最近我把这个项目又汉化回来了,方便大家理解。 + +给大家先随意看一段代码,我在注释中其实就已经在讲解跳表的运行原理了。代码使用了C++模板编程,这样接口支持任意类型的数据(包括自己自定义的类) + +![](https://file1.kamacoder.com/i/algo/20221104121454.png) + +项目地址:**https://github.com/youngyangyang04/Skiplist-CPP** + +这个项目中的代码质量是非常高的,如果无论是C++特性的运用,还是代码风格都是绝对拿得出手的! + +好了,牛逼吹完,然后给大家正式介绍一下这个项目 + +## KV存储引擎 + +本项目就是基于跳表实现的轻量级键值型存储引擎,使用C++实现。插入数据、删除数据、查询数据、数据展示、数据落盘、文件加载数据,以及数据库大小显示。 + +在随机写读情况下,该项目每秒可处理啊请求数(QPS): 24.39w,每秒可处理读请求数(QPS): 18.41w + +## 项目展示 + +![](https://file1.kamacoder.com/i/algo/20221104121509.png) + +文件功能: + +* main.cpp 包含skiplist.h使用跳表进行数据操作 +* skiplist.h 跳表核心实现 +* README.md 中文介绍 +* README-en.md 英文介绍 +* bin 生成可执行文件目录 +* makefile 编译脚本 +* store 数据落盘的文件存放在这个文件夹 +* stress_test_start.sh 压力测试脚本 +* LICENSE 使用协议 + + +## 提供接口 + +* insertElement(插入数据) +* deleteElement(删除数据) +* searchElement(查询数据) +* displayList(展示已存数据) +* dumpFile(数据落盘) +* loadFile(文件加载数据) +* size(返回数据规模) + +## 存储引擎数据表现 + +### 插入操作 + +跳表树高:18 + +采用随机插入数据测试: + + +|插入数据规模(万条) |耗时(秒) | +|---|---| +|10 |0.316763 | +|50 |1.86778 | +|100 |4.10648 | + + +每秒可处理写请求数(QPS): 24.39w + +### 取数据操作 + +|取数据规模(万条) |耗时(秒) | +|---|---| +|10|0.47148 |10| +|50|2.56373 |50| +|100|5.43204 |100| + +每秒可处理读请求数(QPS): 18.41w + +## 项目运行方式 + +``` +make // complie demo main.cpp +./bin/main // run +``` + +运行截图:(其中展示了插入数据,删除数据,展示数据等等功能) + +![](https://file1.kamacoder.com/i/algo/20221104121525.png) + +如果想自己写程序使用这个kv存储引擎,只需要在你的CPP文件中include skiplist.h 就可以了。 + +可以运行如下脚本测试kv存储引擎的性能(当然你可以根据自己的需求进行修改) + +``` +sh stress_test_start.sh +``` + +项目地址:**https://github.com/youngyangyang04/Skiplist-CPP** + +**大家白嫖的同时,别忘了给个star,fork,支持一波!** 录友如果最后拿到offer了,也别忘了和我道个喜哦。 + diff --git "a/problems/\345\211\215\345\272\217/server.md" "b/problems/\345\211\215\345\272\217/server.md" new file mode 100755 index 0000000000..890cf8bcd5 --- /dev/null +++ "b/problems/\345\211\215\345\272\217/server.md" @@ -0,0 +1,129 @@ + +# 一台服务器有什么用! + +* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob) +* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu) + +但在组织这场活动的时候,了解到大家都有一个共同的问题: **这个服务器究竟有啥用??** + +这真是一个好问题,而且我一句两句还说不清楚,所以就专门发文来讲一讲。 + +同时我还录制的一期视频,我的视频号,大家可以关注一波。 + + +一说到服务器,可能很多人都说搞分布式,做计算,搞爬虫,做程序后台服务,多人合作等等。 + +其实这些普通人都用不上,我来说一说大家能用上的吧。 + +## 搭建git私服 + +大家平时工作的时候一定有一个自己的工作文件夹,学生的话就是自己的课件,考试,准备面试的资料等等。 + +已经工作的录友,会有一个文件夹放着自己重要的文档,Markdown,图片,简历等等。 + +这么重要的文件夹,而且我们每天都要更新,也担心哪天电脑丢了,或者坏了,突然这些都不见了。 + +所以我们想备份嘛。 + +还有就是我们经常个人电脑和工作电脑要同步一些私人资料,而不是用微信传来传去。 + +这些都是git私服的使用场景,而且很好用。 + +大家也知道 github,gitee也可以搞私人仓库 用来备份,同步文件,但自己的文档可能放着很多重要的信息,包括自己的各种密码,密钥之类的,放到上面未必安全。你就不怕哪些重大bug把你的信息都泄漏了么[机智] + +更关键的是,github 和 gitee都限速的。毕竟人家的功能定位并不是网盘。 + +项目里有大文件(几百M以上),例如pdf,ppt等等 其上传和下载速度会让你窒息。 + +**后面我会发文专门来讲一讲,如何大家git私服!** + +## 搞一个文件存储 + +这个可以用来生成文件的下载链接,也可以把本地文件传到服务器上。 + +相当于自己做一个对象存储,其实云厂商也有对象存储的产品。 + +不过我们自己也可以做一个,不够很多很同学应该都不知道对象存储怎么用吧,其实我们用服务器可以自己做一个类似的公司。 + +我现在就用自己用go写的一个工具,部署在服务器上。 用来和服务器传文件,或者生成一些文件的临时下载链接。 + +这些都是直接命令行操作的, + +操作方式这样,我把命令包 包装成一个shell命令,想传那个文件,直接 uploadtomyserver,然后就返回可以下载的链接,这个文件也同时传到了我的服务器上。 + +![](https://file1.kamacoder.com/i/algo/20211126165643.png) + +我也把我的项目代码放在了github上: + +https://github.com/youngyangyang04/fileHttpServer + +感兴趣的录友可以去学习一波,顺便给个star。 + + +## 网站 + +做网站,例如 大家知道用html 写几行代码,就可以生成一个网页,但怎么给别人展示呢? + +大家如果用自己的电脑做服务器,只能同一个路由器下的设备可以访问你的网站,可能这个设备出了这个屋子 都访问不了你的网站了。 + +因为你的IP不是公网IP。 + +如果有了一台云服务器,都是配公网IP,你的网站就可以让任何人访问了。 + +或者说 你提供的一个服务就可以让任何人使用。 + +例如第二个例子中,我们可以自己开发一个文件存储,这个服务,我只把把命令行给其他人,其他人都可以使用我的服务来生成链接,当然他们的文件也都传到了我的服务器上。 + +再说一个使用场景。 + +我之前在组织免费里服务器的活动的时候,阿里云给我一个excel,让面就是从我这里买服务器录友的名单,我直接把这个名单甩到群里,让大家自己检查,出现在名单里就可以找我返现,这样做是不是也可以。 + +这么做有几个很大的问题: +* 大家都要去下载excel,做对比,会有人改excel的内容然后就说是从你这里买的,我不可能挨个去比较excel有没有改动 +* excel有其他人的个人信息,这是不能暴漏的。 +* 如果每个人自己用excel查询,私信我返现,一个将近两千人找我返现,我微信根本处理不过来,这就变成体力活了。 + +那应该怎么做呢, + +我就简单写一个查询的页面,后端逻辑就是读一个execel表格,大家在查询页面输入自己的阿里云ID,如果在excel里,页面就会返回返现群的二维码,大家就可以自主扫码加群了。 + +这样,我最后就直接在返现群里 发等额红包就好了,是不是极大降低人力成本了 + +当然我是把 17个返现群的二维码都生成好了,按照一定的规则,展现给查询通过的录友。 + +就是这样一个非常普通的查询页面。 + +![](https://file1.kamacoder.com/i/algo/20211126160200.png) + +查询通过之后,就会展现返现群二维码。 + +![](https://file1.kamacoder.com/i/algo/20211127160558.png) + +但要部署在服务器上,因为没有公网IP,别人用不了你的服务。 + + +## 学习linux + +学习linux其实在自己的电脑上搞一台虚拟机,或者安装双系统也可以学习,不过这很考验你的电脑性能如何了。 + +如果你有一个服务器,那就是独立的一台电脑,你怎么霍霍就怎么霍霍,而且一年都不用关机的,可以一直跑你的任务,和你本地电脑也完全隔离。 + +更方便的是,你目前系统假如是CentOS,想做一个实验需要在Ubuntu上,如果是云服务器,更换系统就是在 后台点一下,一键重装,云厂商基本都是支持所有系统一件安装的。 + +我们平时自己玩linux经常是配各种环境,然后这个linux就被自己玩坏了(一般都是毫无节制使用root权限导致的),总之就是环境配不起来了,基本就要重装了。 + +那云服务器重装系统可太方便了。 + +还有就是加入你好不容易配好的环境,如果以后把这个环境玩坏了,你先回退这之前配好的环境而不是重装系统在重新配一遍吧。 + +那么可以用云服务器的镜像保存功能,就是你配好环境的那一刻就可以打一个镜像包,以后如果环境坏了,直接回退到上次镜像包的状态,这是不是就很香了。 + + +## 总结 + +其实云服务器还有很多其他用处,不过我就说一说大家普遍能用的上的。 + + +* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob) +* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu) + diff --git "a/problems/\345\211\215\345\272\217/vim.md" "b/problems/\345\211\215\345\272\217/vim.md" index 13935b91c6..3f1daa53aa 100644 --- "a/problems/\345\211\215\345\272\217/vim.md" +++ "b/problems/\345\211\215\345\272\217/vim.md" @@ -62,7 +62,7 @@ IDE那么很吃内存,打开个IDE卡半天,用VIM就很轻便了,秒开 来感受一下PowerVim的使用体验,看起来很酷吧!注意这些操作都不用鼠标的,一波键盘控制流!所以我平时写代码是不碰鼠标的! -![](https://code-thinking.cdn.bcebos.com/gifs/vim_overview.gif) +![](https://file1.kamacoder.com/i/algo/vim_overview.gif) ## 安装 @@ -93,7 +93,7 @@ sh install.sh 当然 还有很多,我还详细写了PowerVim的快捷键,使用方法,插件,配置,等等,都在Github主页的README上。当时我的Github上写的都是英文README,这次为了方便大家阅读,我又翻译成中文README。 -![](https://file.kamacoder.com/pics/20211013102249.png) +![](https://file1.kamacoder.com/i/algo/20211013102249.png) Github地址:[https://github.com/youngyangyang04/PowerVim](https://github.com/youngyangyang04/PowerVim) diff --git "a/problems/\345\211\215\345\272\217/\344\270\212\346\265\267\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\344\270\212\346\265\267\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" deleted file mode 100644 index 6309ef583e..0000000000 --- "a/problems/\345\211\215\345\272\217/\344\270\212\346\265\267\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ /dev/null @@ -1,125 +0,0 @@ - -# 上海互联网公司总结 - -**个人总结难免有所疏忽,欢迎大家补充,公司好坏没有排名哈!** - -## 一线互联网 - -* 百度(上海) -* 阿里(上海) -* 腾讯(上海) -* 字节跳动(上海) -* 蚂蚁金服(上海) - -## 外企IT/互联网/硬件 - -* 互联网 - * Google(上海) - * 微软(上海) - * LeetCode/力扣(上海) - * unity(上海)游戏引擎 - * SAP(上海)主要产品是ERP - * PayPal(上海)在线支付鼻祖 - * eBay(上海)电子商务公司 -* 偏硬件 - * IBM(上海) - * Tesla(上海)特斯拉 - * Cisco(上海)思科 - * Intel(上海) - * AMD(上海)半导体产品领域 - * EMC(上海)易安信是美国信息存储资讯科技公司 - * NVIDIA(上海)英伟达是GPU(图形处理器)的发明者,人工智能计算的引领者 - -## 二线互联网 - -* 拼多多(总部) -* 饿了么(总部)阿里旗下。 -* 哈啰出行(总部)阿里旗下 -* 盒马(总部)阿里旗下 -* 哔哩哔哩(总部) -* 阅文集团(总部)腾讯旗下 -* 爱奇艺(上海)百度旗下 -* 携程(总部) -* 京东(上海) -* 网易(上海) -* 美团点评(上海) -* 唯品会(上海) - -## 硬件巨头 (有软件/互联网业务) - -华为(上海) - -## 三线互联网 - -* PPTV(总部) -* 微盟(总部)企业云端商业及营销解决方案提供商 -* 喜马拉雅(总部) -* 陆金所(总部)全球领先的线上财富管理平台 -* 口碑(上海)阿里旗下。 -* 三七互娱(上海) -* 趣头条(总部) -* 巨人网络(总部)游戏公司 -* 盛大网络(总部)游戏公司 -* UCloud(总部)云服务提供商 -* 达达集团(总部)本地即时零售与配送平台 -* 众安保险(总部)在线财产保险 -* 触宝(总部)触宝输入法等多款APP -* 平安系列 - -## 明星创业公司 - -* 小红书(总部) -* 叮咚买菜(总部) -* 蔚来汽车(总部) -* 七牛云(总部) -* 得物App(总部)品潮流尖货装备交易、球鞋潮品鉴别查验、互动潮流社区 -* 收钱吧(总部)开创了中国移动支付市场“一站式收款” -* 蜻蜓FM(总部)音频内容聚合平台 -* 流利说(总部)在线教育 -* Soul(总部)社交软件 -* 美味不用等(总部)智慧餐饮服务商 -* 微鲸科技(总部)专注于智能家居领域 -* 途虎养车(总部) -* 米哈游(总部)游戏公司 -* 莉莉丝游戏(总部)游戏公司 -* 樊登读书(总部)在线教育 - -## AI独角兽公司 - -* 依图科技(总部)和旷视,商汤对标,都是做安防视觉 -* 深兰科技(总部)致力于人工智能基础研究和应用开发 - -## 其他行业,涉及互联网 -* 花旗、摩根大通等一些列金融巨头 -* 百姓网 -* 找钢网 -* 安居客 -* 前程无忧 -* 东方财富 -* 三大电信运营商:中国移动、中国电信、中国联通 -* 沪江英语 -* 各大银行 - -通知:很多同学感觉自己基础还比较薄弱,想循序渐进的从头学一遍数据结构与算法,那你来对地方了。在公众号左下角「算法汇总」里已经按照各个系列难易程度排好顺序了,大家跟着文章顺序打卡学习就可以了,留言区有很多录友都在从头打卡!「算法汇总」会持续更新,大家快去看看吧! - -## 总结 - -大家如果看了[北京有这些互联网公司,你都知道么?](https://programmercarl.com/前序/北京互联网公司总结.html)和[深圳原来有这么多互联网公司,你都知道么?](https://programmercarl.com/前序/深圳互联网公司总结.html)就可以看出中国互联网氛围最浓的当然是北京,其次就是上海! - -很多人说深圳才是第二,上海没有产生BAT之类的企业。 - -**那么来看看上海在垂直领域上是如何独领风骚的,视频领域B站,电商领域拼多多小红书,生活周边有饿了么,大众点评(现与美团合并),互联网金融有蚂蚁金服和陆金所,出行领域有行业老大携程,而且BAT在上海都有部门还是很大的团队,再加上上海众多的外企,以及金融公司(有互联网业务)**。 - -此时就能感受出来,上海的互联网氛围要比深圳强很多! - -好了,希望这份list可以帮助到想在上海发展的录友们。 - -相对于北京和上海,深圳互联网公司断层很明显,腾讯一家独大,二线三线垂直行业的公司很少,所以说深圳腾讯的员工流动性相对是较低的,因为基本没得选。 - - - - - - ------------------------ -
diff --git "a/problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" "b/problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" deleted file mode 100644 index ed526897e5..0000000000 --- "a/problems/\345\211\215\345\272\217/\344\272\222\350\201\224\347\275\221\345\244\247\345\216\202\347\240\224\345\217\221\346\265\201\347\250\213.md" +++ /dev/null @@ -1,240 +0,0 @@ -# 揭秘互联网大厂研发流程 - -[B站:揭秘互联网大厂研发流程](https://www.bilibili.com/video/BV1KR4y1H7ST) - -很多录友会很好奇这么个事: - -* 大厂的研发流程应该是什么样的呢 -* 为什么会有那么多繁琐的流程呢 -* 每一步都有什么作用呢 - -这次给大家介绍一波大厂的研发流程,让大家明明白白。 - -其实对于几十人或者上百人一起开发一个项目的话,一个规范的研发流程是很重要的。 - -有的同学可能想,哪有这么多流程啊,就是写完代码,跑一下,没问题,然后就上线了。 - -其实在大厂里研发流程是很重要的。 - -**一个项目从开发到上线到后面的维护,从流程上就保证大家少出错,也方便后面人继续维护**。 - -那么接下来给大家介绍一下详细的流程。 - -## 1.需求文档 - -看需求文档,我们要根据需求文档来确定我们究竟要做什么。 - -一些同学可能感觉 为什么还要用一个需求文档呢,你就告诉我做啥我就做啥不就完事了? - -需求文档一方面是**倒逼产品经理去系统性的思考这个需求究竟有哪些功能**,用来满足哪些用户的需求。 - -另一方面是**保证我们在研发的时候,研发出来的功能是满足需求文档里所描述的**。 - -如果是口头对接的话,很有可能就是你做出来的东西,产品经理看完感觉:这和我说的需求不一样啊!!这和我想的不一样啊!! - -这样就是两个人相互“甩锅”,那这究竟是谁的锅呢。都没有一个证据,对吧。 - -所以说,有一个需求文档很重要。 - -而且每个阶段的需求文档相当于是把这个项目的整个迭代过程都记录下来了。 - -这样也方便后面的人,了解这个项目是如何迭代的。 - -## 2.这个需求包含了哪些功能 - -产品经理在需求文档里描述一个功能,那么我们在实现的时候,可能要改很多模块,或者说我们要增加一些模块。 - -就是我们从代码的角度上来讲,可能要增添很多功能才能满足 用户看起来好像微不足道的小功能。 - -例如点击登录,点击下单,后台都有很多模块协同运行的。 - -我们要把产品经理角度上的这个功能,拆解为我们代码角度上的具体应该开发的那些功能。 - -## 3.确定有哪些难点 - -这里可能有同学疑惑了,我确定这东西干嘛呢。 - -给大家举一个例子,给你一个需求文档。 - -你说你一周的时间就能把它开发完,那为什么是一周呢,为什么不是两天,为什么不是两周呢。 - -其实 和上面的领导汇报你的工作的时候 **都要把自己的工作进行量化**。 - -那么这个功能有哪些难点,我们要克服这个难点,所需要花费的时间,都要有一个大体的量化。 - -这样才能量化我们自己的工作,**领导其实不知道你的工作是简单 还是困难, 领导只在意最终结果**,所以你需要展现给领导你的工作是有难度的是有挑战的。 - -而且**这些也是我们年底用来晋升或者评职称的素材**。 - -如果这些东西你自己都不在乎的话,谁还会帮你在乎呢。 - -而且**确定了自己的工作难点,把这些难点都记录下来,对以后跳槽也很有帮助**。 - -面试官最喜欢问的问题,就是:**你做的项目中有哪些难点?以及你是如何克服的**。 - -所以这一步对自己个人成长来说也是很有重要的。 对于项目组来说也是记录下来,每一个迭代版本有哪些难点,这些难点团队是如何克服的。 - -这也是项目组往上一级去邀功的资料。 - - -## 4.画架构图 - -一般来说,大厂项目的架构都是比较复杂的,特别是后端架构。 - -如果添加一个模块连个文档都没有,连个图也没有,上来就添加的话,后面的人是很难维护的。 - -而且每个模块和每一个模块之间的依赖关系,如果没有画出一个架构图的话,直接看代码很容易直接就看懵了。 - -为什么你可以快速开发一个新的模块,也是因为之前团队有人把这个架构图画清楚了,你只需要看一眼这个架构图,就知道你的模块应该添加在哪里。 - -那么你去添加模块的时候,也应该把这个架构图相应的位置 完善一下。 - -同时呢,在画架构图的过程中,也增添了自己对整个系统架构的掌握程度。 - -这个图也会让你确定,你的模块在整个项目中扮演一个什么样的角色。 - -## 5.定协议 - -后台模块之间进行通讯需要协议,后台和前端通讯也需要协议。 - -所以只要有交互,就要确定协议的数据格式。 - -**定协议要考虑到兼容,要考虑易于维护**。 - -## 6.设计数据结构和算法 - -其实设计数据结构更多一些,因为我们要选择使用什么容器,什么格式来处理我们的数据。 - -至于算法的话,就很少我们亲自设计了。 - -什么快排,二叉树,动态规划,最短路啥的,在实际开发中,都不需要我们自己去写,**直接调包!** - -面试造火箭,工作拧螺丝 就体现在这里。 - -为什么会这样呢? 一个很简单的例子,互联网研发讲究其实就是要快,例如一个功能2天就要开发完,如果算法都要自己去写的话,等都写完了,花都谢了。 - -最关键的是,**你实现的算法 极大概率没有现成的算法接口安全性高**。 - -**开发中要学会才在巨人的肩膀上**。 - -## 7.预估一下容量 - -特别是后端开发,要估计出 我们自己模块大体需要多大磁盘,多大内存,多大带宽,多少核CPU。 - -这也是没有做过研发工作的同学经常忽略的,**因为大家好像默认 磁盘、内存、带宽、cpu是无穷的**。 - -其实我们在设计一个模块的时候,这些都要评估的,不能模块一上线,把机器直接打爆了。 - -例如 直接把带宽打满了,不仅影响自己的模块功能,还影响了机器上其他模块的运行。 - - -## 8.考虑部署 - -要考虑如果一台机器挂了(可能是硬件原因),那么我们的模块还能不能正常提供服务。 - -这就是考虑模块的容灾性,一般都是采用分布式,服务部署在三台机器上,一台如果挂了,还有其他两台提供服务。 - -还有就是要弹性可伸缩,即我们的模块可不可以直接 部署多台机器来提高承载能力。 - -如果用户量突然上来了,或者流量突然上来了,可以通过快速部署多台机器来抗住流量。 - -而不是模块只能在单机上跑,多部署几台就发生各种问题。 - -**这才能说明是有足够强的风险意识的**。 - -## 9.设计评审 - -前八的阶段其实都是设计阶段,那么你的设计需要让组里的同学一起来评审一下,看看有没有什么问题。 - -大家主要也是看看你的模块 会不会给其他模块或者整个系统带来什么问题 以及 设计的是否合理。 - - -## 10.写代码 - -终于到写代码的阶段了,其实到这时候,是最容易的。 - -**写代码就是体力活了,不是脑力活了**。 - -## 11.自测 - -写完代码,我们需要自测,自己的功能会不会有什么问题。 - -这里可能需要自己造一造数据,跑一跑 看看和预想的是不是一样的。 - -## 12.联调 - -自己的模块可能会涉及到其他模块之间的一个交互,或者和前端的一个交互。 - -所以需要其他同学配合一起来测试。 - -这里就有很多沟通工作了,因为其他同学可能手头有自己的活,那么就要协调一个时间来一起测试。 - -这一步也是很费时间的,**其费时关键是要等,要等其他同学有空和你联调或者是别人等你**,而且往往不是联调一次就能解决问题的。 - -所以 在评估开发时间的时候 也要考虑到联调的时间。 - -这也是大厂研发效率低的地方,但上百人开发的项目,**这种沟通上消耗也是在所难免的**。 - -## 13.交给测试 - -自己的代码,自己测 一般都测不出什么问题,需要交给测试同学来给你测一测。 - -这里如果测试同学测出问题,你就要判断确实有问题还是 测试方式不对,如果有问题就要修改,再提给测试,反反复复这么几轮,直到测试同学测试没问题了。 - -**这个过程也是累心的**。 - -## 14.code review - -代码合入主干之前,需要 项目组的同学一起来评审一下你的代码。 - -之前是评审设计,看设计上有没有什么缺失,这次是大家来看看你代码写的怎么样。 - -例如合入主干会不会有什么问题,代码兼容性做的好不好,接口设计的好不好,甚至字段,函数,变量名,命名合不合理。 - -都要经过大家的评审,如果有问题就还是要改。 - -如果没有问题一般 大家会给+2(就是通过的意思),这样就可以合入主干了。 - -## 15.合入主干 - -合入主干为什么会单独列出来呢。 - -其实合入主干是很重要的,经常是自己的代码没问题,但合入主干之后就有问题了。 - -一般就是合入主干的时候有冲突,例如你从主干拉出一个分支,另一个同学从主干拉出一个分支,而且两个分支修改了同一个模块,如果另一个同学提前合入主干,你再合入主干的时候就会有代码冲突。 - -在解决代码冲突的时候 就会修改别人的代码,这个过程很容易产生新的bug。 - - -**一般合入主干之后,测试同学还要重新跑一个全量测试,才能放心发布**。 - -如果跑全量测试没有问题的话,才会松一口气(懂的人都懂)。 - -## 16.发布 - -最后一步就是发布了。 - -发布其实就是把主干的代码更新到线上的服务器上。 - -一些还没有工作的同学,可能不太理解究竟什么是发布。 - -用大白话来讲,就是把 本地的代码或者某台机器的代码,编译成可执行文件,然后更新到 线上的服务器(一个独立的集群,专门处理线上的流量)并运行起来。 - -上线是最重要的一步了,也很容易出问题,因为一个大型项目,其上线的过程都非常复杂(要更新上百台机器的集群),而且**要考虑线上新版和旧版本的兼容问题**。 - -这也是为什么大厂项目都选择深夜上线,**因为深夜在线用户最少,如果出问题,影响的用户会比较少,可以快速修复**。 - -所以大家经常看到 某大厂程序员深夜上线发布版本之类的。 - -## 总结 - -好了,整整讲了十六个步骤!把大厂研发流程中 具体都有哪一步,为什么要这么做,都分析的很清楚了。 - -不过在大厂也不是所有部门都按照这个流程来的,每个部门都有自己玩法,各个部门也不太统一。 - -我这里是介绍的是已经比较规范的流程,**但流程越正规,研发效率就越低,想要提高效率,就是简化流程,简化流程,就会提高项目出错的概率**。 - -**所以这也是一个相互权衡的过程,每一个部门可能根据自己的业务特点,适当简化流程**。 - -好了,讲了这么多,希望对录友们有所启发。 - diff --git "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" index 33891632b8..db54fcb3e7 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" @@ -57,7 +57,7 @@ 我做了一下总结如图: -![编程风格](https://file.kamacoder.com/pics/20201119173039835.png) +![编程风格](https://file1.kamacoder.com/i/algo/20201119173039835.png) ### 水平留白(代码空格) @@ -134,4 +134,4 @@ Google规范是 大括号和 控制语句保持同一行的,我个人也很认 ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" "b/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" index 88fb129ee2..2be0bea5c1 100644 --- "a/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" +++ "b/problems/\345\211\215\345\272\217/\345\206\205\345\255\230\346\266\210\350\200\227.md" @@ -19,7 +19,7 @@ 如果我们写C++的程序,就要知道栈和堆的概念,程序运行时所需的内存空间分为 固定部分,和可变部分,如下: -![C++内存空间](https://file.kamacoder.com/pics/20210309165950660.png) +![C++内存空间](https://file1.kamacoder.com/i/algo/20210309165950660.png) 固定部分的内存消耗 是不会随着代码运行产生变化的, 可变部分则是会产生变化的 @@ -41,7 +41,7 @@ 想要算出自己程序会占用多少内存就一定要了解自己定义的数据类型的大小,如下: -![C++数据类型的大小](https://file.kamacoder.com/pics/20200804193045440.png) +![C++数据类型的大小](https://file1.kamacoder.com/i/algo/20200804193045440.png) 注意图中有两个不一样的地方,为什么64位的指针就占用了8个字节,而32位的指针占用4个字节呢? @@ -109,7 +109,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第一种就是内存对齐的情况,如图: -![内存对齐](https://file.kamacoder.com/pics/20200804193307347.png) +![内存对齐](https://file1.kamacoder.com/i/algo/20200804193307347.png) 一字节的char占用了四个字节,空了三个字节的内存地址,int数据从地址4开始。 @@ -117,7 +117,7 @@ CPU读取内存不是一次读取单个字节,而是一块一块的来读取 第二种是没有内存对齐的情况如图: -![非内存对齐](https://file.kamacoder.com/pics/20200804193353926.png) +![非内存对齐](https://file1.kamacoder.com/i/algo/20200804193353926.png) char型的数据和int型的数据挨在一起,该int数据从地址1开始,那么CPU想要读这个数据的话来看看需要几步操作: @@ -145,4 +145,4 @@ char型的数据和int型的数据挨在一起,该int数据从地址1开始, ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214.md" "b/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214.md" index 5b57d214db..5e91198948 100644 --- "a/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214.md" +++ "b/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214.md" @@ -61,4 +61,4 @@ int main() { ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" deleted file mode 100644 index 7b42868e8f..0000000000 --- "a/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ /dev/null @@ -1,109 +0,0 @@ -# 北京互联网公司总结 - -

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - -**个人总结难免有所疏忽,欢迎大家补充,公司好坏没有排名哈!** - -如果要在北京找工作,这份list可以作为一个大纲,寻找自己合适的公司。 - -## 一线互联网 - -* 百度(总部) -* 阿里(北京) -* 腾讯(北京) -* 字节跳动(总部) - -## 外企 - -* 微软(北京)微软中国主要就是北京和苏州 -* Hulu(北京)美国的视频网站,听说福利待遇超级棒 -* Airbnb(北京)房屋租赁平台 -* Grab(北京)东南亚第一大出行 App -* 印象笔记(北京)evernote在中国的独立品牌 -* FreeWheel(北京)美国最大的视频广告管理和投放平台 -* amazon(北京)全球最大的电商平台 - -## 二线互联网 - -* 美团点评(总部) -* 京东(总部) -* 网易(北京) -* 滴滴出行(总部) -* 新浪(总部) -* 快手(总部) -* 搜狐(总部) -* 搜狗(总部) -* 360(总部) - -## 硬件巨头 (有软件/互联网业务) - -* 华为(北京) -* 联想(总部) -* 小米(总部)后序要搬到武汉,互联网业务也是小米重头 - -## 三线互联网 - -* 爱奇艺(总部) -* 去哪儿网(总部) -* 知乎(总部) -* 豆瓣(总部) -* 当当网(总部) -* 完美世界(总部)游戏公司 -* 昆仑万维(总部)游戏公司 -* 58同城(总部) -* 陌陌(总部) -* 金山软件(北京)包括金山办公软件 -* 用友网络科技(总部)企业服务ERP提供商 -* 映客直播(总部) -* 猎豹移动(总部) -* 一点资讯(总部) -* 国双(总部)企业级大数据和人工智能解决方案提供商 - -## 明星创业公司 - -可以发现北京一堆在线教育的公司,可能教育要紧盯了政策变化,所以都要在北京吧 - -* 好未来(总部)在线教育 -* 猿辅导(总部)在线教育 -* 跟谁学(总部)在线教育 -* 作业帮(总部)在线教育 -* VIPKID(总部)在线教育 -* 雪球(总部)股市资讯 -* 唱吧(总部) -* 每日优鲜(总部)让每个人随时随地享受食物的美好 -* 微店(总部) -* 罗辑思维(总部)得到APP -* 值得买科技(总部)让每一次消费产生幸福感 -* 拉勾网(总部)互联网招聘 - -## AI独角兽公司 - -* 商汤科技(总部)专注于计算机视觉和深度学习 -* 旷视科技(总部)人工智能产品和解决方案公司 -* 第四范式(总部)人工智能技术与服务提供商 -* 地平线机器人(总部)边缘人工智能芯片的全球领导者 -* 寒武纪(总部)全球智能芯片领域的先行者 - -## 互联网媒体 - -* 央视网 -* 搜房网 -* 易车网 -* 链家网 -* 自如网 -* 汽车之家 - - -北京的互联网氛围绝对是最好的(暂不讨论户口和房价问题),大家如果看了[深圳原来有这么多互联网公司,你都知道么?](https://programmercarl.com/前序/深圳互联网公司总结.html)这篇之后,**会发现北京互联网外企和二线互联网公司数量多的优势,在深圳的互联网公司断档比较严重,如果去不了为数不多的一线公司,可选择的余地就非常少了,而北京选择的余地就很多!** - -相对来说,深圳的硬件企业更多一些,因为珠三角制造业配套比较完善。而大多数互联网公司其实就是媒体公司,当然要靠近政治文化中心,这也是有原因的。 - -就酱,我也会陆续整理其他城市的互联网公司,希望对大家有所帮助。 - - - - - ------------------------ -
diff --git "a/problems/\345\211\215\345\272\217/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" deleted file mode 100644 index b8b1641b63..0000000000 --- "a/problems/\345\211\215\345\272\217/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ /dev/null @@ -1,77 +0,0 @@ -# 广州互联网公司总结 - -

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - -**个人总结难免有所疏忽,欢迎大家补充,公司好坏没有排名哈!** - -## 一线互联网 - -* 微信(总部) 有点难进! -* 字节跳动(广州) - -## 二线 -* 网易(总部)主要是游戏 - -## 三线 - -* 唯品会(总部) -* 欢聚时代(总部)旗下YY,虎牙,YY最近被浑水做空,不知百度还要不要收购了 -* 酷狗音乐(总部) -* UC浏览器(总部)现在隶属阿里创始人何小鹏现在搞小鹏汽车 -* 荔枝FM(总部)用户可以在手机上开设自己的电台和录制节目 -* 映客直播(总部)股票已经跌成渣了 -* 爱范儿(总部) -* 三七互娱(总部)游戏公司 -* 君海游戏(总部)游戏公司 -* 4399游戏(总部)游戏公司 -* 多益网络(总部)游戏公司 - -## 硬件巨头 (有软件/互联网业务) -* 小鹏汽车(总部)新能源汽车小霸王 - -## 创业公司 - -* 妈妈网(总部)母婴行业互联网公司 -* 云徙科技(总部)数字商业云服务提供商 -* Fordeal(总部)中东领先跨境电商平台 -* Mobvista(总部)移动数字营销 -* 久邦GOMO(总部)游戏 -* 深海游戏(总部)游戏 - -## 国企 - -* 中国电信广州研发(听说没有996) - - -## 总结 - -同在广东省,难免不了要和深圳对比,大家如果看了这篇:[深圳原来有这么多互联网公司,你都知道么?](https://programmercarl.com/前序/深圳互联网公司总结.html)就能感受到鲜明的对比了。 - -广州大厂高端岗位其实比较少,本土只有微信和网易,微信呢毕竟还是腾讯的分部,而网易被很多人认为是杭州企业,其实网易总部在广州。 - -广州是唯一一个一线城市没有自己本土互联网巨头的城市,所以网易选择在广州扎根还是很正确的,毕竟杭州是阿里的天下,广州也应该扶持一把本土的互联网公司。 - -虽然对于互联网从业人员来说,广州的岗位要比深圳少很多,**但是!!广州的房价整体要比深圳低30%左右,而且广州的教育,医疗,公共资源完全碾压深圳**。 - -教育方面:大学广州有两个985,四个211,深圳这方面就不用说了,大家懂得。 - -基础教育方面深圳的小学初中高中学校数量远远不够用,小孩上学竞争很激烈,我也是经常听同事们说,耳濡目染了。 - -而医疗上基本深圳看不了的病都要往广州跑,深圳的医院数量也不够用。 - -在生活节奏上,广州更慢一些,更有生活的气息,而深圳生存下去的气息更浓烈一些。 - -所以很多在深圳打拼多年的IT从业者选择去广州安家也是有原因的。 - -但也有很多从广州跑到深圳的,深圳发展的机会更多,而广州教育医疗更丰富,房价不高(相对深圳)。 - - - - - - - - ------------------------ -
diff --git "a/problems/\345\211\215\345\272\217/\346\210\220\351\203\275\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\346\210\220\351\203\275\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" deleted file mode 100644 index f6a575f64d..0000000000 --- "a/problems/\345\211\215\345\272\217/\346\210\220\351\203\275\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ /dev/null @@ -1,72 +0,0 @@ - -# 成都互联网公司总结 -

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - -**排名不分先后,个人总结难免有所疏漏,欢迎补充!** - -## 一线互联网 -* 腾讯(成都) 游戏,王者荣耀就在成都! -* 阿里(成都) -* 蚂蚁金服(成都) -* 字节跳动(成都) - -## 硬件巨头 (有软件/互联网业务) - -* 华为(成都) -* OPPO(成都) - -## 二线互联网 - -* 京东(成都) -* 美团(成都) -* 滴滴(成都) - -## 三线互联网 - -* 完美世界 (成都)游戏 -* 聚美优品 (成都) -* 陌陌 (成都) -* 爱奇艺(成都) - -## 外企互联网 - -* NAVER China (成都)搜索引擎公司,主要针对韩国市场 - -## 创业公司 - -* tap4fun(总部)游戏 -* 趣乐多(总部)游戏 -* 天上友嘉(总部)游戏 -* 三七互娱(成都)游戏 -* 咕咚(总部)智能运动 -* 百词斩(总部)在线教育 -* 晓多科技(总部)AI方向 -* 萌想科技(总部)实习僧 -* Camera360(总部)移动影像社区 -* 医联 (总部)医疗解决方案提供商 -* 小明太极 (总部)原创漫画文娱内容网站以及相关APP -* 小鸡叫叫(总部)致力于儿童教育的智慧解决方案 - - -## AI独角兽公司 - -* 科大讯飞(成都) -* 商汤(成都) - -## 总结 - -可以看出成都相对一线城市的互联网氛围确实差了很多。**但是!成都已经是在内陆城市中甚至二线城市中的佼佼者了!** - -从公司的情况上也可以看出:**成都互联网行业目前的名片是“游戏”**,腾讯、完美世界等大厂,还有无数小厂都在成都搞游戏,可能成都的天然属性就是娱乐,这里是游戏的沃土吧。 - -相信大家如果在一些招聘平台上去搜,其实很多公司都在成都,但都是把客服之类的工作安排在成都,而我在列举的时候尽量把研发相关在成都的公司列出来,这样对大家更有帮助。 - - - - - - - ------------------------ -
diff --git "a/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" "b/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" index 045646ff7e..4252fc8779 100644 --- "a/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" +++ "b/problems/\345\211\215\345\272\217/\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" @@ -38,7 +38,7 @@ 同样的同理再看一下快速排序,都知道快速排序是O(nlogn),但是当数据已经有序情况下,快速排序的时间复杂度是O(n^2) 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是O(n^2)**。 **但是我们依然说快速排序是O(nlogn)的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示: -![时间复杂度4,一般情况下的时间复杂度](https://file.kamacoder.com/pics/20200728185745611-20230310123844306.png) +![时间复杂度4,一般情况下的时间复杂度](https://file1.kamacoder.com/i/algo/20200728185745611-20230310123844306.png) 我们主要关心的还是一般情况下的数据形式。 @@ -49,7 +49,7 @@ 如下图中可以看出不同算法的时间复杂度在不同数据输入规模下的差异。 -![时间复杂度,不同数据规模的差异](https://file.kamacoder.com/pics/20200728191447384-20230310124015324.png) +![时间复杂度,不同数据规模的差异](https://file1.kamacoder.com/i/algo/20200728191447384-20230310124015324.png) 在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。 @@ -115,7 +115,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 为什么可以这么做呢?如下图所示: -![时间复杂度1.png](https://file.kamacoder.com/pics/20200728191447349-20230310124032001.png) +![时间复杂度1.png](https://file1.kamacoder.com/i/algo/20200728191447349-20230310124032001.png) 假如有两个算法的时间复杂度,分别是log以2为底n的对数和log以10为底n的对数,那么这里如果还记得高中数学的话,应该不难理解`以2为底n的对数 = 以2为底10的对数 * 以10为底n的对数`。 @@ -164,4 +164,4 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\346\235\255\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\346\235\255\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" deleted file mode 100644 index 6154cfe50f..0000000000 --- "a/problems/\345\211\215\345\272\217/\346\235\255\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ /dev/null @@ -1,83 +0,0 @@ -# 杭州互联网公司总结 - -

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - -**个人总结难免有所疏忽,欢迎大家补充,公司好坏没有排名哈!** - -## 一线互联网 - -* 阿里巴巴(总部) -* 蚂蚁金服(总部)阿里旗下 -* 阿里云(总部)阿里旗下 -* 网易(杭州) 网易云音乐 -* 字节跳动(杭州)抖音分部 - -## 外企 - -* ZOOM (杭州研发中心)全球知名云视频会议服务提供商 -* infosys(杭州)印度公司,据说工资相对不高 -* 思科(杭州) - -## 二线互联网 - -* 滴滴(杭州) -* 快手(杭州) - -## 硬件巨头 (有软件/互联网业务) - -* 海康威视(总部)安防三巨头 -* 浙江大华(总部)安防三巨头 -* 杭州宇视(总部) 安防三巨头 -* 萤石 -* 华为(杭州) -* vivo(杭州) -* oppo(杭州) -* 魅族(杭州) - -## 三线互联网 - -* 蘑菇街(总部)女性消费者的电子商务网站 -* 有赞(总部)帮助商家进行网上开店、社交营销 -* 菜鸟网络(杭州) -* 花瓣网(总部)图片素材领导者 -* 兑吧(总部)用户运营服务平台 -* 同花顺(总部)网上股票证券交易分析软件 -* 51信用卡(总部)信用卡管理 -* 虾米(总部)已被阿里收购 -* 曹操出行(总部) -* 口碑网 (总部) - -## AI独角兽公司 - -* 旷视科技(杭州) -* 商汤(杭州) - -## 创业公司 - -* e签宝(总部)做电子签名 -* 婚礼纪(总部)好多结婚的朋友都用 -* 大搜车(总部)中国领先的汽车交易服务供应商 -* 二更(总部)自媒体 -* 丁香园(总部) - -## 总结 - -杭州距离上海非常近,难免不了和上海做对比,上海是金融之都,如果看了[上海有这些互联网公司,你都知道么?](https://programmercarl.com/前序/上海互联网公司总结.html)就会发现上海互联网也是仅次于北京的。 - -而杭州是阿里的大本营,到处都有阿里的影子,虽然有网易在,但是也基本是盖过去了,很多中小公司也都是阿里某某高管出来创业的。 - -杭州的阿里带动了杭州的电子商务领域热度非常高,如果你想做电商想做直播带货想做互联网营销,杭州都是圣地! - -如果要是写代码的话,每年各种节日促销,加班996应该是常态,电商公司基本都是这样,当然如果赶上一个好领导的话,回报也是很丰厚的。 - -「代码随想录」一直都是干活满满,值得介绍给每一位学习算法的同学! - - - - - - - ------------------------ -
diff --git "a/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" deleted file mode 100644 index 52a8448b51..0000000000 --- "a/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ /dev/null @@ -1,79 +0,0 @@ - -# 深圳互联网公司总结 - -

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -**个人总结难免有所疏忽,欢迎大家补充,公司好坏没有排名哈!** - -## 一线互联网 - -* 腾讯(总部深圳) -* 百度(深圳) -* 阿里(深圳) -* 字节跳动(深圳) - -## 硬件巨头 (有软件/互联网业务) - -* 华为(总部深圳) -* 中兴(总部深圳) -* 海能达(总部深圳) -* oppo(总部深圳) -* vivo(总部深圳) -* 深信服(总部深圳) -* 大疆(总部深圳,无人机巨头) -* 一加手机(总部深圳) -* 柔宇科技(最近口碑急转直下) - -## 二线大厂 - -* 快手(深圳) -* 京东(深圳) -* 顺丰(总部深圳) - -## 三线大厂 - -* 富途证券(2020年成功赴美上市,主要经营港股美股) -* 微众银行(总部深圳) -* 招银科技(总部深圳) -* 平安系列(平安科技、平安寿险、平安产险、平安金融、平安好医生等) -* Shopee(21年有裁员风波) -* 有赞(深圳) -* 迅雷(总部深圳) -* 金蝶(总部深圳) -* 随手记(总部深圳) - -## AI独角兽公司 - -* 商汤科技(人工智能领域的独角兽) -* 追一科技(一家企业级智能服务AI公司) -* 超多维科技 (计算机视觉、裸眼3D) -* 优必选科技 (智能机器人、人脸识别) - -## 明星创业公司 - -* 丰巢科技(让生活更简单) -* 人人都是产品经理(全球领先的产品经理和运营人 学习、交流、分享平台) -* 大丰收(综合农业互联网服务平台) -* 小鹅通(专注新教育的技术服务商) -* 货拉拉(拉货就找货拉拉) -* 编程猫(少儿编程教育头部企业) -* HelloTalk(全球最大的语言学习社交社区) -* 大宇无限( 拥有SnapTube, Lark Player 等多款广受海外新兴市场用户欢迎的产品) -* 知识星球(深圳大成天下公司出品) -* XMind(隶属深圳市爱思软件技术有限公司,思维导图软件) -* 小赢科技(以技术重塑人类的金融体验) - -## 其他行业(有软件/互联网业务) - -* 三大电信运营商:中国移动、中国电信、中国联通 -* 房产企业:恒大(暴雷)、万科 -* 中信深圳 -* 广发证券,深交所 -* 珍爱网(珍爱网是国内知名的婚恋服务网站之一) - - - - - ------------------------ -
diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" index a2f6ee3b5f..c991be1542 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" @@ -130,4 +130,4 @@ Markdown支持部分html,例如这样 ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" index 1bdcccd586..762b55f400 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" @@ -103,13 +103,13 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 最后福利,把我的简历模板贡献出来!如下图所示。 -![简历模板](https://file.kamacoder.com/pics/20200803175538158.png) +![简历模板](https://file1.kamacoder.com/i/algo/20200803175538158.png) 这里是简历模板中Markdown的代码:[https://github.com/youngyangyang04/Markdown-Resume-Template](https://github.com/youngyangyang04/Markdown-Resume-Template) ,可以fork到自己Github仓库上,按照这个模板来修改自己的简历。 **Word版本的简历,添加如下企业微信,通过之后就会发你word版本**。 -
+
如果已经有我的企业微信,直接回复:简历模板,就可以了。 @@ -119,4 +119,4 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246.md" "b/problems/\345\211\215\345\272\217/\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246.md" index b669049293..da2caa24b6 100644 --- "a/problems/\345\211\215\345\272\217/\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246.md" +++ "b/problems/\345\211\215\345\272\217/\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246.md" @@ -65,4 +65,4 @@ for (int i = 0; i < n; i++) { ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" "b/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" index ca1a422c0b..5603412717 100644 --- "a/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" +++ "b/problems/\345\211\215\345\272\217/\347\256\227\346\263\225\350\266\205\346\227\266.md" @@ -8,7 +8,7 @@ ## 超时是怎么回事 -![程序超时](https://file.kamacoder.com/pics/20200729112716117-20230310124308704.png) +![程序超时](https://file1.kamacoder.com/i/algo/20200729112716117-20230310124308704.png) 大家在leetcode上练习算法的时候应该都遇到过一种错误是“超时”。 @@ -124,11 +124,11 @@ int main() { 来看一下运行的效果,如下图: -![程序超时2](https://file.kamacoder.com/pics/20200729200018460-20230310124315093.png) +![程序超时2](https://file1.kamacoder.com/i/algo/20200729200018460-20230310124315093.png) O(n)的算法,1s内大概计算机可以运行 5 * (10^8)次计算,可以推测一下 $O(n^2)$ 的算法应该1s可以处理的数量级的规模是 5 * (10^8)开根号,实验数据如下。 -![程序超时3](https://file.kamacoder.com/pics/2020072919590970-20230310124318532.png) +![程序超时3](https://file1.kamacoder.com/i/algo/2020072919590970-20230310124318532.png) O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚刚的推测。 @@ -136,7 +136,7 @@ O(n^2)的算法,1s内大概计算机可以运行 22500次计算,验证了刚 理论上应该是比 $O(n)$ 少一个数量级,因为 $\log n$ 的复杂度 其实是很快,看一下实验数据。 -![程序超时4](https://file.kamacoder.com/pics/20200729195729407-20230310124322232.png) +![程序超时4](https://file1.kamacoder.com/i/algo/20200729195729407-20230310124322232.png) $O(n\log n)$ 的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符合预期。 @@ -144,7 +144,7 @@ $O(n\log n)$ 的算法,1s内大概计算机可以运行 2 * (10^7)次计算, **整体测试数据整理如下:** -![程序超时1](https://file.kamacoder.com/pics/20201208231559175-20230310124325152.png) +![程序超时1](https://file1.kamacoder.com/i/algo/20201208231559175-20230310124325152.png) 至于 $O(\log n)$ 和 $O(n^3)$ 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 @@ -278,4 +278,4 @@ public class TimeComplexity { ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" index 035399ce0f..01c07a5c00 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" @@ -29,7 +29,7 @@ int fibonacci(int i) { 可以看出上面的代码每次递归都是O(1)的操作。再来看递归了多少次,这里将i为5作为输入的递归过程 抽象成一棵递归树,如图: -![递归空间复杂度分析](https://file.kamacoder.com/pics/20210305093200104.png) +![递归空间复杂度分析](https://file1.kamacoder.com/i/algo/20210305093200104.png) 从图中,可以看出f(5)是由f(4)和f(3)相加而来,那么f(4)是由f(3)和f(2)相加而来 以此类推。 @@ -196,7 +196,7 @@ int main() 在看递归的深度是多少呢?如图所示: -![递归空间复杂度分析](https://file.kamacoder.com/pics/20210305094749554.png) +![递归空间复杂度分析](https://file1.kamacoder.com/i/algo/20210305094749554.png) 递归第n个斐波那契数的话,递归调用栈的深度就是n。 @@ -214,7 +214,7 @@ int fibonacci(int i) { 最后对各种求斐波那契数列方法的性能做一下分析,如题: -![递归的空间复杂度分析](https://file.kamacoder.com/pics/20210305095227356.png) +![递归的空间复杂度分析](https://file1.kamacoder.com/i/algo/20210305095227356.png) 可以看出,求斐波那契数的时候,使用递归算法并不一定是在性能上是最优的,但递归确实简化的代码层面的复杂度。 @@ -264,4 +264,4 @@ int binary_search( int arr[], int l, int r, int x) { ----------------------- -
+
diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" index befe549874..a02c37f2c9 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246.md" @@ -69,7 +69,7 @@ int function3(int x, int n) { 我们来分析一下,首先看递归了多少次呢,可以把递归抽象出一棵满二叉树。刚刚同学写的这个算法,可以用一棵满二叉树来表示(为了方便表示,选择n为偶数16),如图: -![递归算法的时间复杂度](https://file.kamacoder.com/pics/20201209193909426.png) +![递归算法的时间复杂度](https://file1.kamacoder.com/i/algo/20201209193909426.png) 当前这棵二叉树就是求x的n次方,n为16的情况,n为16的时候,进行了多少次乘法运算呢? @@ -79,7 +79,7 @@ int function3(int x, int n) { 这么如果是求x的n次方,这个递归树有多少个节点呢,如下图所示:(m为深度,从0开始) -![递归求时间复杂度](https://file.kamacoder.com/pics/20200728195531892.png) +![递归求时间复杂度](https://file1.kamacoder.com/i/algo/20200728195531892.png) **时间复杂度忽略掉常数项`-1`之后,这个递归算法的时间复杂度依然是O(n)**。对,你没看错,依然是O(n)的时间复杂度! @@ -140,4 +140,4 @@ int function3(int x, int n) { ----------------------- -
+
diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" old mode 100644 new mode 100755 index a88919d4ca..547cae8a30 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -35,11 +35,11 @@ 如图: -![](https://file.kamacoder.com/pics/20231030165201.png) +![](https://file1.kamacoder.com/i/algo/20231030165201.png) 然后从后向前替换数字字符,也就是双指针法,过程如下:i指向新长度的末尾,j指向旧长度的末尾。 -![](https://file.kamacoder.com/pics/20231030173058.png) +![](https://file1.kamacoder.com/i/algo/20231030173058.png) 有同学问了,为什么要从后向前填充,从前向后填充不行么? diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" old mode 100644 new mode 100755 index 4c62312c8b..025073220a --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -44,16 +44,16 @@ fgabcde 本题中,我们需要将字符串右移n位,字符串相当于分成了两个部分,如果n为2,符串相当于分成了两个部分,如图: (length为字符串长度) -![](https://file.kamacoder.com/pics/20231106170143.png) +![](https://file1.kamacoder.com/i/algo/20231106170143.png) 右移n位, 就是将第二段放在前面,第一段放在后面,先不考虑里面字符的顺序,是不是整体倒叙不就行了。如图: -![](https://file.kamacoder.com/pics/20231106171557.png) +![](https://file1.kamacoder.com/i/algo/20231106171557.png) 此时第一段和第二段的顺序是我们想要的,但里面的字符位置被我们倒叙,那么此时我们在把 第一段和第二段里面的字符再倒叙一把,这样字符顺序不就正确了。 如果: -![](https://file.kamacoder.com/pics/20231106172058.png) +![](https://file1.kamacoder.com/i/algo/20231106172058.png) 其实,思路就是 通过 整体倒叙,把两段子串顺序颠倒,两个段子串里的的字符在倒叙一把,**负负得正**,这样就不影响子串里面字符的顺序了。 @@ -84,7 +84,7 @@ int main() { 可以的,不过,要记得 控制好 局部反转的长度,如果先局部反转,那么先反转的子串长度就是 len - n,如图: -![](https://file.kamacoder.com/pics/20231106172534.png) +![](https://file1.kamacoder.com/i/algo/20231106172534.png) 代码如下: diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" old mode 100644 new mode 100755 index 314fb471b1..ff73cd9606 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -6,7 +6,7 @@ 之前我们已经把力扣上股票系列的题目都讲过的,但没有来一篇股票总结,来帮大家高屋建瓴,所以总结篇这就来了! -![股票问题总结](https://code-thinking.cdn.bcebos.com/pics/%E8%82%A1%E7%A5%A8%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93.jpg) +![股票问题总结](https://file1.kamacoder.com/i/algo/%E8%82%A1%E7%A5%A8%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93.jpg) * [动态规划:121.买卖股票的最佳时机](https://programmercarl.com/0121.买卖股票的最佳时机.html) * [动态规划:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II(动态规划).html) diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" old mode 100644 new mode 100755 index faca5ecb55..32df8af41c --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -51,7 +51,7 @@ ## 背包问题系列 -背包问题大纲 +背包问题大纲 * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) @@ -77,7 +77,7 @@ ## 股票系列 -股票问题总结 +股票问题总结 * [动态规划:买卖股票的最佳时机](https://programmercarl.com/0121.买卖股票的最佳时机.html) * [动态规划:本周我们都讲了这些(系列六)](https://programmercarl.com/周总结/20210225动规周末总结.html) @@ -91,7 +91,7 @@ ## 子序列系列 - + * [动态规划:最长递增子序列](https://programmercarl.com/0300.最长上升子序列.html) * [动态规划:最长连续递增序列](https://programmercarl.com/0674.最长连续递增序列.html) diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index 634f710f9a..63f059798e --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,7 +6,7 @@ 动态规划刷题大纲 - + ## 算法公开课 diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" old mode 100644 new mode 100755 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 594656ca23..11dd298292 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -203,4 +203,4 @@ public: **本周我们都是讲解了二叉树,从理论基础到遍历方式,从递归到迭代,从深度遍历到广度遍历,最后再用了一个翻转二叉树的题目把我们之前讲过的遍历方式都串了起来。** -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 3212ca5603..5b25e9c076 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -254,5 +254,5 @@ traversal(cur->left, tmp, result); * Github:[leetcode-master](https://github.com/youngyangyang04/leetcode-master) * 知乎:[代码随想录](https://www.zhihu.com/people/sun-xiu-yang-64) -![](https://file.kamacoder.com/pics/2021013018121150.png) -
+![](https://file1.kamacoder.com/i/algo/2021013018121150.png) +
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 5f5f688a13..94d95efd38 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201010\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -87,4 +87,4 @@ **如果大家一路跟下来,一定收获满满,如果周末不做这个总结,大家可能都不知道自己收获满满,啊哈!** -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 03148b1575..5276cdde6b 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201017\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -116,4 +116,4 @@ 大家如果每天坚持跟下来,会发现又是充实的一周![机智] -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 77db9708c0..e8e2948791 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -114,4 +114,4 @@ * B站:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index d910ce2509..9da360c2c6 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201107\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -31,7 +31,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)第一个树形结构没有画出startIndex的作用,**这里这里纠正一下,准确的树形结构如图所示:** -![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) +![39.组合总和](https://file1.kamacoder.com/i/algo/20201223170730367.png) ## 周二 @@ -45,7 +45,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://file.kamacoder.com/pics/20201123202817973.png) +![40.组合总和II1](https://file1.kamacoder.com/i/algo/20201123202817973.png) 我在图中将used的变化用橘黄色标注上,可以看出在candidates[i] == candidates[i - 1]相同的情况下: @@ -79,7 +79,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; **本题的树形结构中,和代码的逻辑有一个小出入,已经判断不是回文的子串就不会进入递归了,纠正如下:** -![131.分割回文串](https://file.kamacoder.com/pics/20201123203228309.png) +![131.分割回文串](https://file1.kamacoder.com/i/algo/20201123203228309.png) ## 周四 @@ -90,7 +90,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形图如下: -![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933-20230310133532452.png) +![93.复原IP地址](https://file1.kamacoder.com/i/algo/20201123203735933-20230310133532452.png) 在本文的树形结构图中,我已经把详细的分析思路都画了出来,相信大家看了之后一定会思路清晰不少! @@ -112,7 +112,7 @@ if (s.size() > 12) return result; // 剪枝 如图: -![78.子集](https://file.kamacoder.com/pics/202011232041348.png) +![78.子集](https://file1.kamacoder.com/i/algo/202011232041348.png) 认清这个本质之后,今天的题目就是一道模板题了。 @@ -166,4 +166,4 @@ leetcode上的计时应该是以4ms为单位,有的多提交几次,多个4ms -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index c2e122844d..031ddc0250 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -11,14 +11,14 @@ 树形结构如下: -![90.子集II](https://file.kamacoder.com/pics/2020111217110449-20230310133150714.png) +![90.子集II](https://file1.kamacoder.com/i/algo/2020111217110449-20230310133150714.png) ## 周二 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://file.kamacoder.com/pics/20201112170832333-20230310133155209.png) +![491. 递增子序列1](https://file1.kamacoder.com/i/algo/20201112170832333-20230310133155209.png) [回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)留言区大家有很多疑问,主要还是和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混合在了一起。 @@ -33,7 +33,7 @@ 可以看出元素1在[1,2]中已经使用过了,但是在[2,1]中还要在使用一次1,所以处理排列问题就不用使用startIndex了。 如图: -![46.全排列](https://file.kamacoder.com/pics/20201112170304979-20230310133201250.png) +![46.全排列](https://file1.kamacoder.com/i/algo/20201112170304979-20230310133201250.png) **大家此时可以感受出排列问题的不同:** @@ -46,7 +46,7 @@ 树形结构如下: -![47.全排列II1](https://file.kamacoder.com/pics/20201112171930470-20230310133206398.png) +![47.全排列II1](https://file1.kamacoder.com/i/algo/20201112171930470-20230310133206398.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -54,11 +54,11 @@ 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://file.kamacoder.com/pics/20201112172230434-20230310133211392.png) +![47.全排列II2.png](https://file1.kamacoder.com/i/algo/20201112172230434-20230310133211392.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://file.kamacoder.com/pics/20201112172327967-20230310133216389.png) +![47.全排列II3](https://file1.kamacoder.com/i/algo/20201112172327967-20230310133216389.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** @@ -97,4 +97,4 @@ -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 3494a32074..d934270a27 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -112,4 +112,4 @@ public: -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 4ab1cddbff..70081e82e2 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201203\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -15,7 +15,7 @@ 如图: -![122.买卖股票的最佳时机II](https://file.kamacoder.com/pics/2020112917480858.png) +![122.买卖股票的最佳时机II](https://file1.kamacoder.com/i/algo/2020112917480858.png) ## 周二 @@ -31,7 +31,7 @@ 如图: -![55.跳跃游戏](https://file.kamacoder.com/pics/20201124154758229.png) +![55.跳跃游戏](https://file1.kamacoder.com/i/algo/20201124154758229.png) ## 周三 @@ -44,7 +44,7 @@ 如图: -![45.跳跃游戏II](https://file.kamacoder.com/pics/20201201232309103-20230310133110942.png) +![45.跳跃游戏II](https://file1.kamacoder.com/i/algo/20201201232309103-20230310133110942.png) 注意:**图中的移动下标是到当前这步覆盖的最远距离(下标2的位置),此时没有到终点,只能增加第二步来扩大覆盖范围**。 @@ -55,10 +55,10 @@ 而版本二就比较统一的,超过范围,步数就加一,但在移动下标的范围了做了文章。 即如果覆盖最远距离下标是倒数第二点:直接加一就行,默认一定可以到终点。如图: -![45.跳跃游戏II2](https://file.kamacoder.com/pics/20201201232445286-20230310133115650.png) +![45.跳跃游戏II2](https://file1.kamacoder.com/i/algo/20201201232445286-20230310133115650.png) 如果覆盖最远距离下标不是倒数第二点,说明本次覆盖已经到终点了。如图: -![45.跳跃游戏II1](https://file.kamacoder.com/pics/20201201232338693-20230310133120115.png) +![45.跳跃游戏II1](https://file1.kamacoder.com/i/algo/20201201232338693-20230310133120115.png) 有的录友认为版本一好理解,有的录友认为版本二好理解,其实掌握一种就可以了,也不用非要比拼一下代码的简洁性,简洁程度都差不多了。 @@ -92,4 +92,4 @@ -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" index f449995b01..dec7511c21 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -77,7 +77,7 @@ 文中从计算机硬件出发,分析计算机的计算性能,然后亲自做实验,整理出数据如下: -![程序超时1](https://file.kamacoder.com/pics/20201208231559175-20230310133304038.png) +![程序超时1](https://file1.kamacoder.com/i/algo/20201208231559175-20230310133304038.png) **大家有一个数量级上的概念就可以了!** @@ -120,4 +120,4 @@ 就酱,「代码随想录」是技术公众号里的一抹清流,值得推荐给身边的朋友同学们! -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index 19a95615a3..cface1c9d3 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -38,7 +38,7 @@ 如图: -![135.分发糖果](https://file.kamacoder.com/pics/20201117114916878-20230310133332759.png) +![135.分发糖果](https://file1.kamacoder.com/i/algo/20201117114916878-20230310133332759.png) 接着在贪心另一边,左孩子大于右孩子,左孩子的糖果就要比右孩子多。 @@ -50,7 +50,7 @@ 局部最优可以推出全局最优。 如图: -![135.分发糖果1](https://file.kamacoder.com/pics/20201117115658791-20230310133346127.png) +![135.分发糖果1](https://file1.kamacoder.com/i/algo/20201117115658791-20230310133346127.png) ## 周三 @@ -98,4 +98,4 @@ 而且大家也会发现,贪心并没有想象中的那么简单,贪心往往妙的出其不意,触不及防! -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index a8ba7454eb..71abb155c6 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201224\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -9,7 +9,7 @@ 如图: -![452.用最少数量的箭引爆气球](https://file.kamacoder.com/pics/20201123101929791-20230310133845522.png) +![452.用最少数量的箭引爆气球](https://file1.kamacoder.com/i/algo/20201123101929791-20230310133845522.png) 模拟射气球的过程,很多同学真的要去模拟了,实时把气球从数组中移走,这么写的话就复杂了,从前向后遍历重复的只要跳过就可以的。 @@ -21,7 +21,7 @@ 如图: -![435.无重叠区间](https://file.kamacoder.com/pics/20201221201553618.png) +![435.无重叠区间](https://file1.kamacoder.com/i/algo/20201221201553618.png) 细心的同学就发现了,此题和 [贪心算法:用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像。 @@ -71,7 +71,7 @@ public: 如图: -![763.划分字母区间](https://file.kamacoder.com/pics/20201222191924417-20230310133855435.png) +![763.划分字母区间](https://file1.kamacoder.com/i/algo/20201222191924417-20230310133855435.png) ## 周四 @@ -86,7 +86,7 @@ public: 如图: -![56.合并区间](https://file.kamacoder.com/pics/20201223200632791-20230310133859587.png) +![56.合并区间](https://file1.kamacoder.com/i/algo/20201223200632791-20230310133859587.png) ## 总结 @@ -102,4 +102,4 @@ public: **「代码随想录」里总结的都是经典题目,大家跟着练就节省了不少选择题目的时间了**。 -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index da2ebd3051..e74907013c 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -150,4 +150,4 @@ public: -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 1b6bd84bcc..7cedf63919 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -31,7 +31,7 @@ for (int i = 1; i < m; i++) { } ``` -![62.不同路径1](https://file.kamacoder.com/pics/20201209113631392-20230310133703294.png) +![62.不同路径1](https://file1.kamacoder.com/i/algo/20201209113631392-20230310133703294.png) ## 周二 @@ -45,7 +45,7 @@ dp[i][j]定义依然是:表示从(0 ,0)出发,到(i, j) 有dp[i][j]条 如图: -![63.不同路径II](https://file.kamacoder.com/pics/20210104114513928-20230310133707783.png) +![63.不同路径II](https://file1.kamacoder.com/i/algo/20210104114513928-20230310133707783.png) 这里难住了不少同学,代码如下: @@ -70,11 +70,11 @@ for (int i = 1; i < m; i++) { 拿示例1来举例如题: -![63.不同路径II1](https://file.kamacoder.com/pics/20210104114548983-20230310133711888.png) +![63.不同路径II1](https://file1.kamacoder.com/i/algo/20210104114548983-20230310133711888.png) 对应的dp table 如图: -![63.不同路径II2](https://file.kamacoder.com/pics/20210104114610256-20230310133715981.png) +![63.不同路径II2](https://file1.kamacoder.com/i/algo/20210104114610256-20230310133715981.png) ## 周三 @@ -111,7 +111,7 @@ for (int i = 3; i <= n ; i++) { 举例当n为10 的时候,dp数组里的数值,如下: -![343.整数拆分](https://file.kamacoder.com/pics/20210104173021581-20230310133720552.png) +![343.整数拆分](https://file1.kamacoder.com/i/algo/20210104173021581-20230310133720552.png) @@ -143,7 +143,7 @@ dp数组如何初始化:只需要初始化dp[0]就可以了,推导的基础 n为5时候的dp数组状态如图: -![96.不同的二叉搜索树3](https://file.kamacoder.com/pics/20210107093253987-20230310133724531.png) +![96.不同的二叉搜索树3](https://file1.kamacoder.com/i/algo/20210107093253987-20230310133724531.png) ## 总结 @@ -153,4 +153,4 @@ n为5时候的dp数组状态如图: **但我还会坚持规划好的路线,难度循序渐进,并以面试经典题目为准,该简单的时候就是简单,同时也不会因为阅读量低就放弃有难度的题目!**。 -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index dc32891da8..8ae7882e61 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210121\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -17,7 +17,7 @@ 关于其他几种常用的背包,大家看这张图就了然于胸了: -![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407-20230310133624872.png) +![416.分割等和子集1](https://file1.kamacoder.com/i/algo/20210117171307407-20230310133624872.png) 本文用动规五部曲详细讲解了01背包的二维dp数组的实现方法,大家其实可以发现最简单的是推导公式了,推导公式估计看一遍就记下来了,但难就难在确定初始化和遍历顺序上。 @@ -70,7 +70,7 @@ for(int i = 1; i < weight.size(); i++) { // 遍历物品 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://file.kamacoder.com/pics/20210118163425129-20230310133630224.jpg) +![动态规划-背包问题4](https://file1.kamacoder.com/i/algo/20210118163425129-20230310133630224.jpg) 最终结果就是dp[2][4]。 @@ -122,7 +122,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://file.kamacoder.com/pics/20210110103614769-20230310133634873.png) +![动态规划-背包问题9](https://file1.kamacoder.com/i/algo/20210110103614769-20230310133634873.png) ## 周三 @@ -160,4 +160,4 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 就像是我们讲解01背包的时候,花了那么大力气才把每一个细节都讲清楚,这里其实是基础,后面的背包问题怎么变,基础比较牢固自然会有自己的一套思考过程。 -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 8598ec69f0..ff3b771aa8 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210128\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -35,7 +35,7 @@ bagSize = (S + sum) / 2 = (3 + 5) / 2 = 4 dp数组状态变化如下: -![494.目标和](https://file.kamacoder.com/pics/20210125120743274-20230310132918821.jpg) +![494.目标和](https://file1.kamacoder.com/i/algo/20210125120743274-20230310132918821.jpg) ## 周二 @@ -73,7 +73,7 @@ dp[i][j] = max(dp[i][j], dp[i - zeroNum][j - oneNum] + 1); 最后dp数组的状态如下所示: -![474.一和零](https://file.kamacoder.com/pics/20210120111201512-20230310132936011.jpg) +![474.一和零](https://file1.kamacoder.com/i/algo/20210120111201512-20230310132936011.jpg) ## 周三 @@ -140,4 +140,4 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 此时相信大家对动规五部曲也有更深的理解了,同样也验证了Carl之前讲过的:**简单题是用来学习方法论的,而遇到难题才体现出方法论的重要性!** -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index fb570bd402..e1e6baf503 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210204\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -200,4 +200,4 @@ public: 此时我们就已经把完全背包的遍历顺序研究的透透的了! -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 309f277f6a..b17a682972 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -30,7 +30,7 @@ dp[1] = max(nums[0], nums[1]); 以示例二,输入[2,7,9,3,1]为例。 -![198.打家劫舍](https://file.kamacoder.com/pics/20210221170954115-20230310133425353.jpg) +![198.打家劫舍](https://file1.kamacoder.com/i/algo/20210221170954115-20230310133425353.jpg) 红框dp[nums.size() - 1]为结果。 @@ -42,15 +42,15 @@ dp[1] = max(nums[0], nums[1]); * 情况一:考虑不包含首尾元素 -![213.打家劫舍II](https://file.kamacoder.com/pics/20210129160748643.jpg) +![213.打家劫舍II](https://file1.kamacoder.com/i/algo/20210129160748643.jpg) * 情况二:考虑包含首元素,不包含尾元素 -![213.打家劫舍II1](https://file.kamacoder.com/pics/20210129160821374.jpg) +![213.打家劫舍II1](https://file1.kamacoder.com/i/algo/20210129160821374.jpg) * 情况三:考虑包含尾元素,不包含首元素 -![213.打家劫舍II2](https://file.kamacoder.com/pics/20210129160842491.jpg) +![213.打家劫舍II2](https://file1.kamacoder.com/i/algo/20210129160842491.jpg) 需要注意的是,**“考虑” 不等于 “偷”**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素!对于情况三,取nums[1] 和 nums[3]就是最大的。 @@ -178,7 +178,7 @@ return {val2, val1}; 以示例1为例,dp数组状态如下:(**注意用后序遍历的方式推导**) -![337.打家劫舍III](https://file.kamacoder.com/pics/20210129181331613.jpg) +![337.打家劫舍III](https://file1.kamacoder.com/i/algo/20210129181331613.jpg) **最后头结点就是 取下标0 和 下标1的最大值就是偷得的最大金钱**。 @@ -306,4 +306,4 @@ public: **代码随想录温馨提醒:投资有风险,入市需谨慎!** -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 5d84fb1915..0749becbba 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -77,7 +77,7 @@ dp[0][4] = 0; 以输入[1,2,3,4,5]为例 -![123.买卖股票的最佳时机III](https://file.kamacoder.com/pics/20201228181724295.png) +![123.买卖股票的最佳时机III](https://file1.kamacoder.com/i/algo/20201228181724295.png) 可以看到红色框为最后两次卖出的状态。 @@ -144,7 +144,7 @@ for (int j = 1; j < 2 * k; j += 2) { 以输入[1,2,3,4,5],k=2为例。 -![188.买卖股票的最佳时机IV](https://file.kamacoder.com/pics/20201229100358221-20230310133805763.png) +![188.买卖股票的最佳时机IV](https://file1.kamacoder.com/i/algo/20201229100358221-20230310133805763.png) 最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即红色部分就是最后求解。 @@ -197,7 +197,7 @@ vector> dp(n, vector(3, 0)); 以 [1,2,3,0,2] 为例,dp数组如下: -![309.最佳买卖股票时机含冷冻期](https://file.kamacoder.com/pics/20201229163725348.png) +![309.最佳买卖股票时机含冷冻期](https://file1.kamacoder.com/i/algo/20201229163725348.png) 最后两个状态 不持有股票(能购买) 和 不持有股票(冷冻期)都有可能最后结果,取最大的。 @@ -206,4 +206,4 @@ vector> dp(n, vector(3, 0)); 下周还会有一篇股票系列的文章,**股票系列后面我也会单独写一篇总结,来高度概括一下,这样大家会对股票问题就有一个整体性的理解了**。 -
+
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" "b/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" index 52e2fec11f..6cbf6e2a77 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/\344\272\214\345\217\211\346\240\221\351\230\266\346\256\265\346\200\273\347\273\223\347\263\273\345\210\227\344\270\200.md" @@ -206,4 +206,4 @@ public: **本周我们都是讲解了二叉树,从理论基础到遍历方式,从递归到迭代,从深度遍历到广度遍历,最后再用了一个翻转二叉树的题目把我们之前讲过的遍历方式都串了起来。** -
+
diff --git "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" old mode 100644 new mode 100755 diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index 92b590bc89..e3400ad7f0 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -18,7 +18,7 @@ 哈希表中关键码就是数组的索引下标,然后通过下标直接访问数组中的元素,如下图所示: -![哈希表1](https://file.kamacoder.com/pics/20210104234805168.png) +![哈希表1](https://file1.kamacoder.com/i/algo/20210104234805168.png) 那么哈希表能解决什么问题呢,**一般哈希表都是用来快速判断一个元素是否出现集合里。** @@ -36,7 +36,7 @@ 哈希函数如下图所示,通过hashCode把名字转化为数值,一般hashcode是通过特定编码方式,可以将其他数据格式转化为不同的数值,这样就把学生名字映射为哈希表上的索引数字了。 -![哈希表2](https://file.kamacoder.com/pics/2021010423484818.png) +![哈希表2](https://file1.kamacoder.com/i/algo/2021010423484818.png) 如果hashCode得到的数值大于 哈希表的大小了,也就是大于tableSize了,怎么办呢? @@ -52,7 +52,7 @@ 如图所示,小李和小王都映射到了索引下标 1 的位置,**这一现象叫做哈希碰撞**。 -![哈希表3](https://file.kamacoder.com/pics/2021010423494884.png) +![哈希表3](https://file1.kamacoder.com/i/algo/2021010423494884.png) 一般哈希碰撞有两种解决方法, 拉链法和线性探测法。 @@ -60,7 +60,7 @@ 刚刚小李和小王在索引1的位置发生了冲突,发生冲突的元素都被存储在链表中。 这样我们就可以通过索引找到小李和小王了 -![哈希表4](https://file.kamacoder.com/pics/20210104235015226.png) +![哈希表4](https://file1.kamacoder.com/i/algo/20210104235015226.png) (数据规模是dataSize, 哈希表的大小为tableSize) @@ -72,7 +72,7 @@ 例如冲突的位置,放了小李,那么就向下找一个空位放置小王的信息。所以要求tableSize一定要大于dataSize ,要不然哈希表上就没有空置的位置来存放 冲突的数据了。如图所示: -![哈希表5](https://file.kamacoder.com/pics/20210104235109950.png) +![哈希表5](https://file1.kamacoder.com/i/algo/20210104235109950.png) 其实关于哈希碰撞还有非常多的细节,感兴趣的同学可以再好好研究一下,这里我就不再赘述了。 @@ -117,7 +117,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 实际上功能都是一样一样的, 但是unordered_set在C++11的时候被引入标准库了,而hash_set并没有,所以建议还是使用unordered_set比较好,这就好比一个是官方认证的,hash_set,hash_map 是C++11标准之前民间高手自发造的轮子。 -![哈希表6](https://file.kamacoder.com/pics/20210104235134572.png) +![哈希表6](https://file1.kamacoder.com/i/algo/20210104235134572.png) ## 总结 diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" old mode 100644 new mode 100755 index 8fd69d518e..7a7929f41a --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -63,7 +63,7 @@ void backtracking(参数) { 本题我把回溯问题抽象为树形结构,如题: -![77.组合1](https://file.kamacoder.com/pics/20201118152928844.png) +![77.组合1](https://file1.kamacoder.com/i/algo/20201118152928844.png) 可以直观的看出其搜索的过程:**for循环横向遍历,递归纵向遍历,回溯不断调整结果集**,这个理念贯穿整个回溯法系列,也是我做了很多回溯的题目,不断摸索其规律才总结出来的。 @@ -73,7 +73,7 @@ void backtracking(参数) { 优化回溯算法只有剪枝一种方法,在[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中把回溯法代码做了剪枝优化,树形结构如图: -![77.组合4](https://file.kamacoder.com/pics/20201118153133458.png) +![77.组合4](https://file1.kamacoder.com/i/algo/20201118153133458.png) 大家可以一目了然剪的究竟是哪里。 @@ -89,11 +89,11 @@ void backtracking(参数) { 在[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中,相当于 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)加了一个元素总和的限制。 树形结构如图: -![216.组合总和III](https://file.kamacoder.com/pics/20201118201921245.png) +![216.组合总和III](https://file1.kamacoder.com/i/algo/20201118201921245.png) 整体思路还是一样的,本题的剪枝会好想一些,即:**已选元素总和如果已经大于n(题中要求的和)了,那么往后遍历就没有意义了,直接剪掉**,如图: -![216.组合总和III1](https://file.kamacoder.com/pics/20201118202038240.png) +![216.组合总和III1](https://file1.kamacoder.com/i/algo/20201118202038240.png) 在本题中,依然还可以有一个剪枝,就是[回溯算法:组合问题再剪剪枝](https://programmercarl.com/0077.组合优化.html)中提到的,对for循环选择的起始范围的剪枝。 @@ -114,7 +114,7 @@ void backtracking(参数) { **注意以上我只是说求组合的情况,如果是排列问题,又是另一套分析的套路**。 树形结构如下: -![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png) +![39.组合总和](https://file1.kamacoder.com/i/algo/20201223170730367.png) 最后还给出了本题的剪枝优化,如下: @@ -125,7 +125,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 优化后树形结构如下: -![39.组合总和1](https://file.kamacoder.com/pics/20201118202115929.png) +![39.组合总和1](https://file1.kamacoder.com/i/algo/20201118202115929.png) #### 组合总和(三) @@ -140,7 +140,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 都知道组合问题可以抽象为树形结构,那么“使用过”在这个树形结构上是有两个维度的,一个维度是同一树枝上“使用过”,一个维度是同一树层上“使用过”。**没有理解这两个层面上的“使用过” 是造成大家没有彻底理解去重的根本原因**。 -![40.组合总和II1](https://file.kamacoder.com/pics/2020111820220675.png) +![40.组合总和II1](https://file1.kamacoder.com/i/algo/2020111820220675.png) 我在图中将used的变化用橘黄色标注上,**可以看出在candidates[i] == candidates[i - 1]相同的情况下:** @@ -161,7 +161,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![17. 电话号码的字母组合](https://file.kamacoder.com/pics/20201118202335724.png) +![17. 电话号码的字母组合](https://file1.kamacoder.com/i/algo/20201118202335724.png) 如果大家在现场面试的时候,一定要注意各种输入异常的情况,例如本题输入1 * #按键。 @@ -189,7 +189,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 树形结构如下: -![131.分割回文串](https://file.kamacoder.com/pics/20201118202448642.png) +![131.分割回文串](https://file1.kamacoder.com/i/algo/20201118202448642.png) ## 子集问题 @@ -200,7 +200,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 如图: -![78.子集](https://file.kamacoder.com/pics/20201118202544339.png) +![78.子集](https://file1.kamacoder.com/i/algo/20201118202544339.png) 认清这个本质之后,今天的题目就是一道模板题了。 @@ -227,14 +227,14 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![90.子集II](https://file.kamacoder.com/pics/2020111217110449.png) +![90.子集II](https://file1.kamacoder.com/i/algo/2020111217110449.png) ### 递增子序列 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! 树形结构如下: -![491. 递增子序列1](https://file.kamacoder.com/pics/20201112170832333.png) +![491. 递增子序列1](https://file1.kamacoder.com/i/algo/20201112170832333.png) 很多同学都会把这道题目和[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)混在一起。 @@ -243,7 +243,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 我用没有排序的集合{2,1,2,2}来举个例子画一个图,如下: -![90.子集II2](https://file.kamacoder.com/pics/2020111316440479.png) +![90.子集II2](https://file1.kamacoder.com/i/algo/2020111316440479.png) **相信这个图胜过千言万语的解释了**。 @@ -259,7 +259,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 如图: -![46.全排列](https://file.kamacoder.com/pics/20201112170304979.png) +![46.全排列](https://file1.kamacoder.com/i/algo/20201112170304979.png) **大家此时可以感受出排列问题的不同:** @@ -272,7 +272,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树形结构如下: -![47.全排列II1](https://file.kamacoder.com/pics/20201112171930470.png) +![47.全排列II1](https://file1.kamacoder.com/i/algo/20201112171930470.png) **这道题目神奇的地方就是used[i - 1] == false也可以,used[i - 1] == true也可以!** @@ -280,11 +280,11 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 树层上去重(used[i - 1] == false),的树形结构如下: -![47.全排列II2.png](https://file.kamacoder.com/pics/20201112172230434.png) +![47.全排列II2.png](https://file1.kamacoder.com/i/algo/20201112172230434.png) 树枝上去重(used[i - 1] == true)的树型结构如下: -![47.全排列II3](https://file.kamacoder.com/pics/20201112172327967.png) +![47.全排列II3](https://file1.kamacoder.com/i/algo/20201112172327967.png) **可以清晰的看到使用(used[i - 1] == false),即树层去重,效率更高!** @@ -318,7 +318,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 以输入:[["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]为例,抽象为树形结构如下: -![](https://file.kamacoder.com/pics/2020111518065555.png) +![](https://file1.kamacoder.com/i/algo/2020111518065555.png) 本题可以算是一道hard的题目了,关于本题的难点我在文中已经详细列出。 @@ -335,7 +335,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 下面我用一个3 * 3 的棋盘,将搜索过程抽象为一棵树,如图: -![51.N皇后](https://file.kamacoder.com/pics/20201118225433127.png) +![51.N皇后](https://file1.kamacoder.com/i/algo/20201118225433127.png) 从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。 @@ -363,7 +363,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 因为这个树形结构太大了,我抽取一部分,如图所示: -![37.解数独](https://file.kamacoder.com/pics/2020111720451790.png) +![37.解数独](https://file1.kamacoder.com/i/algo/2020111720451790.png) 解数独可以说是非常难的题目了,如果还一直停留在一维递归的逻辑中,这道题目可以让大家瞬间崩溃。 @@ -438,7 +438,7 @@ N皇后问题分析: 回溯专题汇聚为一张图: -![](https://file.kamacoder.com/pics/20211030124742.png) +![](https://file1.kamacoder.com/i/algo/20211030124742.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[莫非毛](https://wx.zsxq.com/dweb2/index/footprint/828844212542),所画,总结的非常好,分享给大家。 diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" old mode 100644 new mode 100755 index 5c20f562b6..5e2c9345c4 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -15,7 +15,7 @@ 我用没有排序的集合{2,1,2,2}来举例子画一个图,如图: -![90.子集II2](https://file.kamacoder.com/pics/2020111316440479-20230310121930316.png) +![90.子集II2](https://file1.kamacoder.com/i/algo/2020111316440479-20230310121930316.png) 图中,大家就很明显的看到,子集重复了。 @@ -95,7 +95,7 @@ private: 如图: -![90.子集II1](https://file.kamacoder.com/pics/202011131625054.png) +![90.子集II1](https://file1.kamacoder.com/i/algo/202011131625054.png) 可以看出一旦把unordered_set uset放在类成员位置,它控制的就是整棵树,包括树枝。 diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index d31e9651b8..c17e0be3f6 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,7 +6,7 @@ ## 题目分类 -回溯算法大纲 +回溯算法大纲 ## 算法公开课 @@ -114,7 +114,7 @@ if (终止条件) { 如图: -![回溯算法理论基础](https://file.kamacoder.com/pics/20210130173631174.png) +![回溯算法理论基础](https://file1.kamacoder.com/i/algo/20210130173631174.png) 注意图中,我特意举例集合大小和孩子的数量是相等的! diff --git "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" old mode 100644 new mode 100755 diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" old mode 100644 new mode 100755 index e29a7bd379..98ba371fdc --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -18,7 +18,7 @@ 举一个字符数组的例子,如图所示: - + 需要两点注意的是 @@ -29,7 +29,7 @@ 例如删除下标为3的元素,需要对下标为3的元素后面的所有元素都要做移动操作,如图所示: - + 而且大家如果使用C++的话,要注意vector 和 array的区别,vector的底层实现是array,严格来讲vector是容器,不是数组。 @@ -37,7 +37,7 @@ 那么二维数组直接上图,大家应该就知道怎么回事了 - + **那么二维数组在内存的空间地址是连续的么?** @@ -45,7 +45,7 @@ 看了下图,就应该明白了: - + 所以**Java的二维数组在内存中不是 `3*4` 的连续地址空间,而是四条连续的地址空间组成!** @@ -125,7 +125,7 @@ ## 总结 -![](https://file.kamacoder.com/pics/数组总结.png) +![](https://file1.kamacoder.com/i/algo/数组总结.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。 diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index 4000208a9b..49c41f5abb --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -18,7 +18,7 @@ 举一个字符数组的例子,如图所示: -![算法通关数组](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%84.png) +![算法通关数组](https://file1.kamacoder.com/i/algo/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%84.png) @@ -31,7 +31,7 @@ 例如删除下标为3的元素,需要对下标为3的元素后面的所有元素都要做移动操作,如图所示: -![算法通关数组1](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%841.png) +![算法通关数组1](https://file1.kamacoder.com/i/algo/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%841.png) 而且大家如果使用C++的话,要注意vector 和 array的区别,vector的底层实现是array,严格来讲vector是容器,不是数组。 @@ -40,7 +40,7 @@ 那么二维数组直接上图,大家应该就知道怎么回事了 -![](https://file.kamacoder.com/pics/20240606105522.png) +![](https://file1.kamacoder.com/i/algo/20240606105522.png) **那么二维数组在内存的空间地址是连续的么?** @@ -80,7 +80,7 @@ int main() { 如图: -![数组内存](https://file.kamacoder.com/pics/20210310150641186.png) +![数组内存](https://file1.kamacoder.com/i/algo/20210310150641186.png) **所以可以看出在C++中二维数组在地址空间上是连续的**。 @@ -111,7 +111,7 @@ public static void test_arr() { 所以Java的二维数组可能是如下排列的方式: -![算法通关数组3](https://file.kamacoder.com/pics/20201214111631844.png) +![算法通关数组3](https://file1.kamacoder.com/i/algo/20201214111631844.png) 这里面试中数组相关的理论知识就介绍完了。 diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" old mode 100644 new mode 100755 diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index 912bfe1d70..0d3cc3a0c7 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -11,7 +11,7 @@ 如图所示: -![栈与队列理论1](https://file.kamacoder.com/pics/20210104235346563.png) +![栈与队列理论1](https://file1.kamacoder.com/i/algo/20210104235346563.png) 那么我这里再列出四个关于栈的问题,大家可以思考一下。以下是以C++为例,使用其他编程语言的同学也对应思考一下,自己使用的编程语言里栈和队列是什么样的。 @@ -46,7 +46,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 来说一说栈,栈先进后出,如图所示: -![栈与队列理论2](https://file.kamacoder.com/pics/20210104235434905.png) +![栈与队列理论2](https://file1.kamacoder.com/i/algo/20210104235434905.png) 栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则,所以栈不提供走访功能,也不提供迭代器(iterator)。 不像是set 或者map 提供迭代器iterator来遍历所有元素。 @@ -59,7 +59,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 从下图中可以看出,栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。 -![栈与队列理论3](https://file.kamacoder.com/pics/20210104235459376.png) +![栈与队列理论3](https://file1.kamacoder.com/i/algo/20210104235459376.png) **我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。** diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" old mode 100644 new mode 100755 index 162ee273e3..a3566268a1 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -35,7 +35,7 @@ public: ``` 耗时如下: -![vectorinsert](https://file.kamacoder.com/pics/20201218203611181.png) +![vectorinsert](https://file1.kamacoder.com/i/algo/20201218203611181.png) 其直观上来看数组的insert操作是O(n)的,整体代码的时间复杂度是O(n^2)。 @@ -68,7 +68,7 @@ public: 耗时如下: -![使用链表](https://file.kamacoder.com/pics/20201218200756257.png) +![使用链表](https://file1.kamacoder.com/i/algo/20201218200756257.png) 大家都知道对于普通数组,一旦定义了大小就不能改变,例如int a[10];,这个数组a至多只能放10个元素,改不了的。 @@ -95,7 +95,7 @@ for (int i = 0; i < vec.size(); i++) { 就是重新申请一个二倍于原数组大小的数组,然后把数据都拷贝过去,并释放原数组内存。(对,就是这么原始粗暴的方法!) 举一个例子,如图: -![vector原理](https://file.kamacoder.com/pics/20201218185902217.png) +![vector原理](https://file1.kamacoder.com/i/algo/20201218185902217.png) 原vector中的size和capicity相同都是3,初始化为1 2 3,此时要push_back一个元素4。 @@ -138,7 +138,7 @@ public: 耗时如下: -![vector手动模拟insert](https://file.kamacoder.com/pics/20201218200626718.png) +![vector手动模拟insert](https://file1.kamacoder.com/i/algo/20201218200626718.png) 这份代码就是不让vector动态扩容,全程我们自己模拟insert的操作,大家也可以直观的看出是一个O(n^2)的方法了。 diff --git "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" old mode 100644 new mode 100755 diff --git "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" old mode 100644 new mode 100755 index 9ce3fdda98..3f3841e1a2 --- "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" @@ -11,7 +11,7 @@ 关于这几种常见的背包,其关系如下: -![416.分割等和子集1](https://file.kamacoder.com/pics/20230310000726.png) +![416.分割等和子集1](https://file1.kamacoder.com/i/algo/20230310000726.png) 通过这个图,可以很清晰分清这几种常见背包之间的关系。 @@ -93,7 +93,7 @@ 背包问题总结: -![](https://file.kamacoder.com/pics/背包问题1.jpeg) +![](https://file1.kamacoder.com/i/algo/背包问题1.jpeg) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画结的非常好,分享给大家。 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" old mode 100644 new mode 100755 index 79751c89e9..d3258c425e --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -21,7 +21,7 @@ 如果这几种背包,分不清,我这里画了一个图,如下: -![416.分割等和子集1](https://file.kamacoder.com/pics/20210117171307407.png) +![416.分割等和子集1](https://file1.kamacoder.com/i/algo/20210117171307407.png) 除此以外其他类型的背包,面试几乎不会问,都是竞赛级别的了,leetcode上连多重背包的题目都没有,所以题库也告诉我们,01背包和完全背包就够用了。 @@ -77,7 +77,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 如图,二维数组为 dp[i][j]。 -![动态规划-背包问题1](https://file.kamacoder.com/pics/20210110103003361.png) +![动态规划-背包问题1](https://file1.kamacoder.com/i/algo/20210110103003361.png) 那么这里 i 、j、dp[i][j] 分别表示什么呢? @@ -91,7 +91,7 @@ i 来表示物品、j表示背包容量。 我们先看把物品0 放入背包的情况: -![](https://file.kamacoder.com/pics/20240730113455.png) +![](https://file1.kamacoder.com/i/algo/20240730113455.png) 背包容量为0,放不下物品0,此时背包里的价值为0。 @@ -103,7 +103,7 @@ i 来表示物品、j表示背包容量。 再看把物品1 放入背包: -![](https://file.kamacoder.com/pics/20240730114228.png) +![](https://file1.kamacoder.com/i/algo/20240730114228.png) 背包容量为 0,放不下物品0 或者物品1,此时背包里的价值为0。 @@ -150,7 +150,7 @@ i 来表示物品、j表示背包容量。 推导方向如图: -![](https://file.kamacoder.com/pics/20240730174246.png) +![](https://file1.kamacoder.com/i/algo/20240730174246.png) 如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 @@ -158,7 +158,7 @@ i 来表示物品、j表示背包容量。 所以 放物品1 的情况 = dp[0][1] + 物品1 的价值,推导方向如图: -![](https://file.kamacoder.com/pics/20240730174436.png) +![](https://file1.kamacoder.com/i/algo/20240730174436.png) 两种情况,分别是放物品1 和 不放物品1,我们要取最大值(毕竟求的是最大价值) @@ -178,7 +178,7 @@ i 来表示物品、j表示背包容量。 首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: -![动态规划-背包问题2](https://file.kamacoder.com/pics/2021011010304192.png) +![动态规划-背包问题2](https://file1.kamacoder.com/i/algo/2021011010304192.png) 在看其他情况。 @@ -205,7 +205,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 此时dp数组初始化情况如图所示: -![动态规划-背包问题7](https://file.kamacoder.com/pics/20210110103109140.png) +![动态规划-背包问题7](https://file1.kamacoder.com/i/algo/20210110103109140.png) dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? @@ -217,7 +217,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 如图: -![动态规划-背包问题10](https://file.kamacoder.com/pics/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%9810.jpg) +![动态规划-背包问题10](https://file1.kamacoder.com/i/algo/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92-%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%9810.jpg) 最后初始化代码如下: @@ -236,7 +236,7 @@ for (int j = weight[0]; j <= bagweight; j++) { 在如下图中,可以看出,有两个遍历的维度:物品与背包重量 -![动态规划-背包问题3](https://file.kamacoder.com/pics/2021011010314055.png) +![动态规划-背包问题3](https://file1.kamacoder.com/i/algo/2021011010314055.png) 那么问题来了,**先遍历 物品还是先遍历背包重量呢?** @@ -277,11 +277,11 @@ for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括正上方向),那么先遍历物品,再遍历背包的过程如图所示: -![动态规划-背包问题5](https://file.kamacoder.com/pics/202101101032124.png) +![动态规划-背包问题5](https://file1.kamacoder.com/i/algo/202101101032124.png) 再来看看先遍历背包,再遍历物品呢,如图: -![动态规划-背包问题6](https://file.kamacoder.com/pics/20210110103244701.png) +![动态规划-背包问题6](https://file1.kamacoder.com/i/algo/20210110103244701.png) **大家可以看出,虽然两个for循环遍历的次序不同,但是dp[i][j]所需要的数据就是左上角,根本不影响dp[i][j]公式的推导!** @@ -293,7 +293,7 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 来看一下对应的dp数组的数值,如图: -![动态规划-背包问题4](https://file.kamacoder.com/pics/20210118163425129.jpg) +![动态规划-背包问题4](https://file1.kamacoder.com/i/algo/20210118163425129.jpg) 最终结果就是dp[2][4]。 diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" old mode 100644 new mode 100755 index b6a68960a9..00dc593417 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -163,7 +163,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 一维dp,分别用物品0,物品1,物品2 来遍历背包,最终得到结果如下: -![动态规划-背包问题9](https://file.kamacoder.com/pics/20210110103614769.png) +![动态规划-背包问题9](https://file1.kamacoder.com/i/algo/20210110103614769.png) 本题力扣上没有原题,大家可以去[卡码网第46题](https://kamacoder.com/problempage.php?pid=1046)去练习,题意是一样的,代码如下: diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" index be7a5d54f9..7dd78302ee 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\345\256\214\345\205\250\350\203\214\345\214\205\344\270\200\347\273\264.md" @@ -54,11 +54,11 @@ for (int i = 1; i < n; i++) { // 遍历物品 遍历物品在外层循环,遍历背包容量在内层循环,状态如图: -![动态规划-完全背包1](https://file.kamacoder.com/pics/20210126104529605.jpg) +![动态规划-完全背包1](https://file1.kamacoder.com/i/algo/20210126104529605.jpg) 遍历背包容量在外层循环,遍历物品在内层循环,状态如图: -![动态规划-完全背包2](https://file.kamacoder.com/pics/20210729234011.png) +![动态规划-完全背包2](https://file1.kamacoder.com/i/algo/20210729234011.png) 看了这两个图,大家就会理解,完全背包中,两个for循环的先后循序,都不影响计算dp[j]所需要的值(这个值就是下标j之前所对应的dp[j])。 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" old mode 100644 new mode 100755 diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" old mode 100644 new mode 100755 index cb8db1e0e3..02b3cdc32d --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -66,7 +66,7 @@ 推导方向如图: -![](https://file.kamacoder.com/pics/20241126112952.png) +![](https://file1.kamacoder.com/i/algo/20241126112952.png) 如果放物品1, **那么背包要先留出物品1的容量**,目前容量是4,物品1 的容量(就是物品1的重量)为3,此时背包剩下容量为1。 @@ -78,7 +78,7 @@ 所以 放物品1 的情况 = dp[1][1] + 物品1 的价值,推导方向如图: -![](https://file.kamacoder.com/pics/20241126113104.png) +![](https://file1.kamacoder.com/i/algo/20241126113104.png) (**注意上图和 [01背包理论基础(二维数组)](https://programmercarl.com/背包理论基础01背包-1.html) 中的区别**,对于理解完全背包很重要) @@ -103,7 +103,7 @@ 首先从dp[i][j]的定义出发,如果背包容量j为0的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为0。如图: -![动态规划-背包问题2](https://file.kamacoder.com/pics/2021011010304192.png) +![动态规划-背包问题2](https://file1.kamacoder.com/i/algo/2021011010304192.png) 在看其他情况。 @@ -132,7 +132,7 @@ for (int j = weight[0]; j <= bagWeight; j++) 此时dp数组初始化情况如图所示: -![](https://file.kamacoder.com/pics/20241114161608.png) +![](https://file1.kamacoder.com/i/algo/20241114161608.png) dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化多少呢? @@ -185,7 +185,7 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 以本篇举例数据为例,填满了dp二维数组如图: -![](https://file.kamacoder.com/pics/20241126113752.png) +![](https://file1.kamacoder.com/i/algo/20241126113752.png) 因为 物品0 的性价比是最高的,而且 在完全背包中,每一类物品都有无限个,所以有无限个物品0,既然物品0 性价比最高,当然是优先放物品0。 diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" old mode 100644 new mode 100755 index 4c67fb401a..7aff85764e --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" @@ -128,7 +128,7 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 贪心专题汇聚为一张图: -![](https://file.kamacoder.com/pics/贪心总结water.png) +![](https://file1.kamacoder.com/i/algo/贪心总结water.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412)所画,总结的非常好,分享给大家。 diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index 2d0af8791a..3bcf307525 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -7,7 +7,7 @@ 题目分类大纲如下: -贪心算法大纲 +贪心算法大纲 ## 算法公开课 diff --git "a/problems/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" "b/problems/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" new file mode 100755 index 0000000000..98aa47a865 --- /dev/null +++ "b/problems/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" @@ -0,0 +1,281 @@ +* [做项目(多个C++、Java、Go、测开、前端项目)](./other/kstar.md) +* [刷算法(两个月高强度学算法)](./xunlian/xunlianying.md) +* [背八股(40天挑战高频面试题)](./xunlian/bagu.md) + + + + + + + + + + + + + + +# 递归算法的时间与空间复杂度分析! + +之前在[通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html)中详细讲解了递归算法的时间复杂度,但没有讲空间复杂度。 + +本篇讲通过求斐波那契数列和二分法再来深入分析一波递归算法的时间和空间复杂度,细心看完,会刷新对递归的认知! + + +## 递归求斐波那契数列的性能分析 + +先来看一下求斐波那契数的递归写法。 + +```CPP +int fibonacci(int i) { + if(i <= 0) return 0; + if(i == 1) return 1; + return fibonacci(i-1) + fibonacci(i-2); +} +``` + +对于递归算法来说,代码一般都比较简短,从算法逻辑上看,所用的存储空间也非常少,但运行时需要内存可不见得会少。 + +### 时间复杂度分析 + +来看看这个求斐波那契的递归算法的时间复杂度是多少呢? + +在讲解递归时间复杂度的时候,我们提到了递归算法的时间复杂度本质上是要看: **递归的次数 * 每次递归的时间复杂度**。 + +可以看出上面的代码每次递归都是O(1)的操作。再来看递归了多少次,这里将i为5作为输入的递归过程 抽象成一棵递归树,如图: + + +![递归空间复杂度分析](https://file1.kamacoder.com/i/algo/20210305093200104.png) + +从图中,可以看出f(5)是由f(4)和f(3)相加而来,那么f(4)是由f(3)和f(2)相加而来 以此类推。 + +在这棵二叉树中每一个节点都是一次递归,那么这棵树有多少个节点呢? + +我们之前也有说到,一棵深度(按根节点深度为1)为k的二叉树最多可以有 2^k - 1 个节点。 + +所以该递归算法的时间复杂度为O(2^n),这个复杂度是非常大的,随着n的增大,耗时是指数上升的。 + +来做一个实验,大家可以有一个直观的感受。 + +以下为C++代码,来测一下,让我们输入n的时候,这段递归求斐波那契代码的耗时。 + +```CPP +#include +#include +#include +using namespace std; +using namespace chrono; +int fibonacci(int i) { + if(i <= 0) return 0; + if(i == 1) return 1; + return fibonacci(i - 1) + fibonacci(i - 2); +} +void time_consumption() { + int n; + while (cin >> n) { + milliseconds start_time = duration_cast( + system_clock::now().time_since_epoch() + ); + + fibonacci(n); + + milliseconds end_time = duration_cast( + system_clock::now().time_since_epoch() + ); + cout << milliseconds(end_time).count() - milliseconds(start_time).count() + <<" ms"<< endl; + } +} +int main() +{ + time_consumption(); + return 0; +} +``` + +根据以上代码,给出几组实验数据: + +测试电脑以2015版MacPro为例,CPU配置:`2.7 GHz Dual-Core Intel Core i5` + +测试数据如下: + +* n = 40,耗时:837 ms +* n = 50,耗时:110306 ms + +可以看出,O(2^n)这种指数级别的复杂度是非常大的。 + +所以这种求斐波那契数的算法看似简洁,其实时间复杂度非常高,一般不推荐这样来实现斐波那契。 + +其实罪魁祸首就是这里的两次递归,导致了时间复杂度以指数上升。 + +```CPP +return fibonacci(i-1) + fibonacci(i-2); +``` + +可不可以优化一下这个递归算法呢。 主要是减少递归的调用次数。 + +来看一下如下代码: + +```CPP +// 版本二 +int fibonacci(int first, int second, int n) { + if (n <= 0) { + return 0; + } + if (n < 3) { + return 1; + } + else if (n == 3) { + return first + second; + } + else { + return fibonacci(second, first + second, n - 1); + } +} +``` + +这里相当于用first和second来记录当前相加的两个数值,此时就不用两次递归了。 + +因为每次递归的时候n减1,即只是递归了n次,所以时间复杂度是 O(n)。 + +同理递归的深度依然是n,每次递归所需的空间也是常数,所以空间复杂度依然是O(n)。 + +代码(版本二)的复杂度如下: + +* 时间复杂度:O(n) +* 空间复杂度:O(n) + +此时再来测一下耗时情况验证一下: + +```CPP +#include +#include +#include +using namespace std; +using namespace chrono; +int fibonacci_3(int first, int second, int n) { + if (n <= 0) { + return 0; + } + if (n < 3) { + return 1; + } + else if (n == 3) { + return first + second; + } + else { + return fibonacci_3(second, first + second, n - 1); + } +} + +void time_consumption() { + int n; + while (cin >> n) { + milliseconds start_time = duration_cast( + system_clock::now().time_since_epoch() + ); + + fibonacci_3(1, 1, n); + + milliseconds end_time = duration_cast( + system_clock::now().time_since_epoch() + ); + cout << milliseconds(end_time).count() - milliseconds(start_time).count() + <<" ms"<< endl; + } +} +int main() +{ + time_consumption(); + return 0; +} + +``` + +测试数据如下: + +* n = 40,耗时:0 ms +* n = 50,耗时:0 ms + +大家此时应该可以看出差距了!! + +### 空间复杂度分析 + +说完了这段递归代码的时间复杂度,再看看如何求其空间复杂度呢,这里给大家提供一个公式:**递归算法的空间复杂度 = 每次递归的空间复杂度 * 递归深度** + +为什么要求递归的深度呢? + +因为每次递归所需的空间都被压到调用栈里(这是内存管理里面的数据结构,和算法里的栈原理是一样的),一次递归结束,这个栈就是就是把本次递归的数据弹出去。所以这个栈最大的长度就是递归的深度。 + +此时可以分析这段递归的空间复杂度,从代码中可以看出每次递归所需要的空间大小都是一样的,所以每次递归中需要的空间是一个常量,并不会随着n的变化而变化,每次递归的空间复杂度就是$O(1)$。 + +在看递归的深度是多少呢?如图所示: + + +![递归空间复杂度分析](https://file1.kamacoder.com/i/algo/20210305094749554.png) + +递归第n个斐波那契数的话,递归调用栈的深度就是n。 + +那么每次递归的空间复杂度是O(1), 调用栈深度为n,所以这段递归代码的空间复杂度就是O(n)。 + +```CPP +int fibonacci(int i) { + if(i <= 0) return 0; + if(i == 1) return 1; + return fibonacci(i-1) + fibonacci(i-2); +} +``` + + +最后对各种求斐波那契数列方法的性能做一下分析,如题: + + +![递归的空间复杂度分析](https://file1.kamacoder.com/i/algo/20210305095227356.png) + +可以看出,求斐波那契数的时候,使用递归算法并不一定是在性能上是最优的,但递归确实简化的代码层面的复杂度。 + +### 二分法(递归实现)的性能分析 + +带大家再分析一段二分查找的递归实现。 + +```CPP +int binary_search( int arr[], int l, int r, int x) { + if (r >= l) { + int mid = l + (r - l) / 2; + if (arr[mid] == x) + return mid; + if (arr[mid] > x) + return binary_search(arr, l, mid - 1, x); + return binary_search(arr, mid + 1, r, x); + } + return -1; +} +``` + +都知道二分查找的时间复杂度是O(logn),那么递归二分查找的空间复杂度是多少呢? + +我们依然看 **每次递归的空间复杂度和递归的深度** + +每次递归的空间复杂度可以看出主要就是参数里传入的这个arr数组,但需要注意的是在C/C++中函数传递数组参数,不是整个数组拷贝一份传入函数而是传入的数组首元素地址。 + +**也就是说每一层递归都是公用一块数组地址空间的**,所以 每次递归的空间复杂度是常数即:O(1)。 + +再来看递归的深度,二分查找的递归深度是logn ,递归深度就是调用栈的长度,那么这段代码的空间复杂度为 1 * logn = O(logn)。 + +大家要注意自己所用的语言在传递函数参数的时,是拷贝整个数值还是拷贝地址,如果是拷贝整个数值那么该二分法的空间复杂度就是O(nlogn)。 + + +## 总结 + +本章我们详细分析了递归实现的求斐波那契和二分法的空间复杂度,同时也对时间复杂度做了分析。 + +特别是两种递归实现的求斐波那契数列,其时间复杂度截然不容,我们还做了实验,验证了时间复杂度为O(2^n)是非常耗时的。 + +通过本篇大家应该对递归算法的时间复杂度和空间复杂度有更加深刻的理解了。 + + + + + + + diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" old mode 100644 new mode 100755 index 99bb2abcd0..df1747e26a --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -75,7 +75,7 @@ ## 总结 -![](https://file.kamacoder.com/pics/链表总结.png) +![](https://file1.kamacoder.com/i/algo/链表总结.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。 diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" old mode 100644 new mode 100755 index 5305c9a9dc..c465818739 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -12,7 +12,7 @@ 链表的入口节点称为链表的头结点也就是head。 如图所示: -![链表1](https://file.kamacoder.com/pics/20200806194529815.png) +![链表1](https://file1.kamacoder.com/i/algo/20200806194529815.png) ## 链表的类型 @@ -31,7 +31,7 @@ 双链表 既可以向前查询也可以向后查询。 如图所示: -![链表2](https://file.kamacoder.com/pics/20200806194559317.png) +![链表2](https://file1.kamacoder.com/i/algo/20200806194559317.png) ### 循环链表 @@ -39,7 +39,7 @@ 循环链表可以用来解决约瑟夫环问题。 -![链表4](https://file.kamacoder.com/pics/20200806194629603.png) +![链表4](https://file1.kamacoder.com/i/algo/20200806194629603.png) ## 链表的存储方式 @@ -54,7 +54,7 @@ 如图所示: -![链表3](https://file.kamacoder.com/pics/20200806194613920.png) +![链表3](https://file1.kamacoder.com/i/algo/20200806194613920.png) 这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。 @@ -104,7 +104,7 @@ head->val = 5; 删除D节点,如图所示: -![链表-删除节点](https://file.kamacoder.com/pics/20200806195114541-20230310121459257.png) +![链表-删除节点](https://file1.kamacoder.com/i/algo/20200806195114541-20230310121459257.png) 只要将C节点的next指针 指向E节点就可以了。 @@ -118,7 +118,7 @@ head->val = 5; 如图所示: -![链表-添加节点](https://file.kamacoder.com/pics/20200806195134331-20230310121503147.png) +![链表-添加节点](https://file1.kamacoder.com/i/algo/20200806195134331-20230310121503147.png) 可以看出链表的增添和删除都是O(1)操作,也不会影响到其他节点。 @@ -128,7 +128,7 @@ head->val = 5; 再把链表的特性和数组的特性进行一个对比,如图所示: -![链表-链表与数据性能对比](https://file.kamacoder.com/pics/20200806195200276.png) +![链表-链表与数据性能对比](https://file1.kamacoder.com/i/algo/20200806195200276.png) 数组在定义的时候,长度就是固定的,如果想改动数组的长度,就需要重新定义一个新的数组。 diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" old mode 100644 new mode 100755 index 0207b71eac..7e23172093 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -13,7 +13,7 @@ 图示两个链表在节点 c1 开始相交: -![](https://file.kamacoder.com/pics/20211219221657.png) +![](https://file1.kamacoder.com/i/algo/20211219221657.png) 题目数据 保证 整个链式结构中不存在环。 @@ -21,15 +21,15 @@ 示例 1: -![](https://file.kamacoder.com/pics/20211219221723.png) +![](https://file1.kamacoder.com/i/algo/20211219221723.png) 示例 2: -![](https://file.kamacoder.com/pics/20211219221749.png) +![](https://file1.kamacoder.com/i/algo/20211219221749.png) 示例 3: -![](https://file.kamacoder.com/pics/20211219221812.png) +![](https://file1.kamacoder.com/i/algo/20211219221812.png) @@ -42,11 +42,11 @@ 看如下两个链表,目前curA指向链表A的头结点,curB指向链表B的头结点: -![面试题02.07.链表相交_1](https://code-thinking.cdn.bcebos.com/pics/面试题02.07.链表相交_1.png) +![面试题02.07.链表相交_1](https://file1.kamacoder.com/i/algo/面试题02.07.链表相交_1.png) 我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置,如图: -![面试题02.07.链表相交_2](https://code-thinking.cdn.bcebos.com/pics/面试题02.07.链表相交_2.png) +![面试题02.07.链表相交_2](https://file1.kamacoder.com/i/algo/面试题02.07.链表相交_2.png) 此时我们就可以比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到交点。 From 6cc4034fff4f6fcfb870f4f1cfabc47a0fe2deb1 Mon Sep 17 00:00:00 2001 From: tuhacrt Date: Thu, 22 May 2025 22:43:10 +0800 Subject: [PATCH 1523/1533] =?UTF-8?q?fix:=200093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=20python=20=E8=8C=83=E4=BE=8B=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BA=8C=20codeblock=20=E7=BC=BA=E5=B0=91=20'```'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 6fa732d0c1..b9a3e08f63 100755 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -463,7 +463,7 @@ class Solution: return False num = int(s[start:end+1]) return 0 <= num <= 255 - +``` 回溯(版本三) ```python From de5b18603aeabe2f1214b468b5c3818727e938c7 Mon Sep 17 00:00:00 2001 From: chenzhiw <32631622+chenzhiw@users.noreply.github.com> Date: Fri, 23 May 2025 17:24:01 +0800 Subject: [PATCH 1524/1533] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 段 --> 端 --- ...\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index 0d3cc3a0c7..a33c2a880f 100755 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -63,7 +63,7 @@ C++标准库是有多个版本的,要知道我们使用的STL是哪个版本 **我们常用的SGI STL,如果没有指定底层实现的话,默认是以deque为缺省情况下栈的底层结构。** -deque是一个双向队列,只要封住一段,只开通另一端就可以实现栈的逻辑了。 +deque是一个双向队列,只要封住一端,只开通另一端就可以实现栈的逻辑了。 **SGI STL中 队列底层实现缺省情况下一样使用deque实现的。** From 226f6c0354ca41108e0fcae07d0afeef16695616 Mon Sep 17 00:00:00 2001 From: cheems Date: Sat, 28 Jun 2025 23:04:13 +0800 Subject: [PATCH 1525/1533] =?UTF-8?q?fix(docs):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=8A=E5=A6=82=E4=BD=95=E5=9C=A8Github=E4=B8=8A=E6=8F=90?= =?UTF-8?q?=E4=BA=A4PR=E3=80=8B=E4=B8=AD=E7=9A=84=E6=96=87=E5=AD=97?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/qita/join.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/qita/join.md b/problems/qita/join.md index 232d84ca2f..a4bd5baff4 100644 --- a/problems/qita/join.md +++ b/problems/qita/join.md @@ -45,7 +45,7 @@
-然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 +然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一致。 然后点击 create pull request 就可以了
From 7aa973b561943cf30ee53b162250f0e37ad4fb0e Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Sun, 6 Jul 2025 11:26:06 +0800 Subject: [PATCH 1526/1533] Update --- ...2.\346\216\245\351\233\250\346\260\264.md" | 3 +- ...55\344\271\260\345\234\237\345\234\260.md" | 1 - ...17\202\344\274\232dijkstra\345\240\206.md" | 1 - ...74\232dijkstra\346\234\264\347\264\240.md" | 1 - .../0053.\345\257\273\345\256\235-Kruskal.md" | 1 - .../0053.\345\257\273\345\256\235-prim.md" | 1 - ...77\346\215\242\346\225\260\345\255\227.md" | 1 - ...13\345\255\227\347\254\246\344\270\262.md" | 1 - ...8.\345\214\272\351\227\264\345\222\214.md" | 1 - ...\211\251\350\277\220\350\276\223I-SPFA.md" | 1 - ...7\347\211\251\350\277\220\350\276\223I.md" | 1 - ...\347\211\251\350\277\220\350\276\223II.md" | 101 +++++++++++++++++- ...347\211\251\350\277\220\350\276\223III.md" | 1 - ...16\351\200\233\345\205\254\345\233\255.md" | 1 - ...57\350\276\276\350\267\257\345\276\204.md" | 2 +- ...60\351\207\217\345\271\277\346\220\234.md" | 3 +- ...60\351\207\217\346\267\261\346\220\234.md" | 3 +- ...00\345\244\247\351\235\242\347\247\257.md" | 3 +- ...04\346\200\273\351\235\242\347\247\257.md" | 3 +- ...11\346\262\241\345\255\244\345\262\233.md" | 3 +- ...64\346\265\201\351\227\256\351\242\230.md" | 3 +- ...00\345\244\247\345\262\233\345\261\277.md" | 3 +- ...50\345\217\257\350\276\276\346\200\247.md" | 5 +- ...77\347\232\204\345\221\250\351\225\277.md" | 3 +- ...50\347\232\204\350\267\257\345\276\204.md" | 1 - ...27\344\275\231\350\277\236\346\216\245.md" | 1 - ...\344\275\231\350\277\236\346\216\245II.md" | 1 - ...46\344\270\262\346\216\245\351\276\231.md" | 3 +- ...57\344\273\266\346\236\204\345\273\272.md" | 1 - ...7\232\204\346\224\273\345\207\273astar.md" | 1 - ...347\224\250ACM\346\250\241\345\274\217.md" | 1 - ...06\350\256\272\345\237\272\347\241\200.md" | 1 - ...06\350\256\272\345\237\272\347\241\200.md" | 3 +- ...72\346\200\273\347\273\223\347\257\207.md" | 1 - ...06\350\256\272\345\237\272\347\241\200.md" | 3 +- ...06\350\256\272\345\237\272\347\241\200.md" | 3 +- ...30\346\200\273\347\273\223\347\257\207.md" | 1 - problems/qita/shejimoshi.md | 57 ---------- ...22\346\200\273\347\273\223\347\257\207.md" | 2 +- 39 files changed, 127 insertions(+), 100 deletions(-) delete mode 100644 problems/qita/shejimoshi.md diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index c208637b2f..62fbe270b7 100755 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -14,8 +14,7 @@ 示例 1: -![](https://code-thinking-1253855093.cos.ap-guangzhou.myqcloud.com/pics/20210713205038.png) - +![image](https://file1.kamacoder.com/i/algo/2025-07-04_12-06-59.jpg) * 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1] * 输出:6 * 解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 diff --git "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" index cb5fbb7468..64804842f2 100644 --- "a/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" +++ "b/problems/kamacoder/0044.\345\274\200\345\217\221\345\225\206\350\264\255\344\271\260\345\234\237\345\234\260.md" @@ -613,4 +613,3 @@ func main() { } ``` -
diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index 80d7851eaf..fceef23926 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -927,4 +927,3 @@ func main() { ### C -
diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 42099df92a..2498643a36 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -942,4 +942,3 @@ main() ### C -
diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index 53da7af9ee..a7022c5d08 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -760,4 +760,3 @@ int main() } ``` -
diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index df0129ee2a..d3f0aeb373 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -757,4 +757,3 @@ main() ### C -
diff --git "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" index 67d31a5564..6679f68bb6 100644 --- "a/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" +++ "b/problems/kamacoder/0054.\346\233\277\346\215\242\346\225\260\345\255\227.md" @@ -432,4 +432,3 @@ echo $s; ### Rust: -
diff --git "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" index be998390c5..b0918f4eea 100644 --- "a/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/kamacoder/0055.\345\217\263\346\227\213\345\255\227\347\254\246\344\270\262.md" @@ -409,4 +409,3 @@ echo $s; ### Rust: -
diff --git "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" index 894e0383d5..b62adcfc62 100644 --- "a/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" +++ "b/problems/kamacoder/0058.\345\214\272\351\227\264\345\222\214.md" @@ -408,4 +408,3 @@ func main() { } ``` -
diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" index 9d3fbe839e..bebc2f39c5 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I-SPFA.md" @@ -536,4 +536,3 @@ main() -
diff --git "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" index 63d1be2a30..fc79bcde1a 100644 --- "a/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" +++ "b/problems/kamacoder/0094.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223I.md" @@ -538,4 +538,3 @@ main() ### C -
diff --git "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" index 957b8a80bc..f9cf8151b0 100644 --- "a/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" +++ "b/problems/kamacoder/0095.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223II.md" @@ -183,7 +183,6 @@ struct Edge { //邻接表 Edge(int t, int w): to(t), val(w) {} // 构造函数 }; - int main() { int n, m, p1, p2, val; cin >> n >> m; @@ -229,16 +228,111 @@ int main() { } } } - if (flag) cout << "circle" << endl; else if (minDist[end] == INT_MAX) { cout << "unconnected" << endl; } else { cout << minDist[end] << endl; } - } +``` + +以上代码看上去没问题,但提交之后会发现报错了,为什么呢? + +例如这组数据: + +``` +4 6 +1 4 3 +1 2 1 +1 3 1 +3 2 -2 +2 4 1 +3 4 0 +``` + +如图: + +![](https://file1.kamacoder.com/i/algo/2025-06-03_16-12-41.jpg) + +上面代码在执行的过程中,节点4 已经出队列了,但 计算入度会反复计算 节点4的入度,导致 误判 该图有环。 + +我们需要记录哪些节点已经出队列了,哪些节点在队列里面,对于已经出队列的节点不用统计入度,以下为优化后的代码: + +```CPP +#include +#include +#include +#include +#include +using namespace std; + +struct Edge { //邻接表 + int to; // 链接的节点 + int val; // 边的权重 + + Edge(int t, int w): to(t), val(w) {} // 构造函数 +}; + + +int main() { + int n, m, p1, p2, val; + cin >> n >> m; + + vector> grid(n + 1); // 邻接表 + + // 将所有边保存起来 + for(int i = 0; i < m; i++){ + cin >> p1 >> p2 >> val; + // p1 指向 p2,权值为 val + grid[p1].push_back(Edge(p2, val)); + } + int start = 1; // 起点 + int end = n; // 终点 + + vector minDist(n + 1 , INT_MAX); + minDist[start] = 0; + + queue que; + que.push(start); // 队列里放入起点 + + vector count(n+1, 0); // 记录节点加入队列几次 + count[start]++; + vector inQueue(n+1, false); // 记录节点是否在队列中 + + bool flag = false; + while (!que.empty()) { + + int node = que.front(); que.pop(); + inQueue[node] = false; // 节点出队 + + for (Edge edge : grid[node]) { + int from = node; + int to = edge.to; + int value = edge.val; + if (minDist[to] > minDist[from] + value) { // 开始松弛 + minDist[to] = minDist[from] + value; + if (!inQueue[to]) { // 避免重复入队 + que.push(to); + inQueue[to] = true; + count[to]++; + if (count[to] == n) {// 如果加入队列次数超过 n-1次 就说明该图与负权回路 + flag = true; + while (!que.empty()) que.pop(); + break; + } + } + } + } + } + if (flag) cout << "circle" << endl; + else if (minDist[end] == INT_MAX) { + cout << "unconnected" << endl; + } else { + cout << minDist[end] << endl; + } +} ``` ## 其他语言版本 @@ -456,4 +550,3 @@ if __name__ == "__main__": ### C -
diff --git "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" index 0c00ccb668..7d85435eff 100644 --- "a/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" +++ "b/problems/kamacoder/0096.\345\237\216\345\270\202\351\227\264\350\264\247\347\211\251\350\277\220\350\276\223III.md" @@ -922,4 +922,3 @@ if __name__ == "__main__": ### C -
diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index 53e66ee46f..a2b673b373 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -574,4 +574,3 @@ if __name__ == '__main__': ### C -
diff --git "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" index c71981996b..8853f78f40 100644 --- "a/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0098.\346\211\200\346\234\211\345\217\257\350\276\276\350\267\257\345\276\204.md" @@ -72,6 +72,7 @@ * 1 <= N <= 100 * 1 <= M <= 500 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[所有可达路径](https://www.bilibili.com/video/BV1VePeepEpP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 插曲 @@ -887,4 +888,3 @@ async function dfs(graph, x, n) { -
diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 93c1fe41fa..cfa6af18e4 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -46,6 +46,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:广搜有陷阱啊!! | 广度优先搜索 | 卡码网:99.岛屿数量](https://www.bilibili.com/video/BV18PRGYcEiB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: @@ -557,4 +559,3 @@ object Solution { ### C -
diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" index 3c54278af0..fb824aeff2 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\346\267\261\346\220\234.md" @@ -46,6 +46,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:来用深搜解决一道题目,两种深搜写法,你掉坑了吗? | 卡码网:99.岛屿数量](https://www.bilibili.com/video/BV18PRGYcEiB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: @@ -459,4 +461,3 @@ object Solution { ### C -
diff --git "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 2ae1f452e0..fc024f6120 100644 --- "a/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0100.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -44,6 +44,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:深搜也有陷阱啊!! | 深搜优先搜索 | 卡码网:100.岛屿的最大面积](https://www.bilibili.com/video/BV1FzoyY5EXH),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图: @@ -890,4 +892,3 @@ main(); ### C -
diff --git "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" index c883100724..b3f66b96d8 100644 --- "a/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" +++ "b/problems/kamacoder/0101.\345\255\244\345\262\233\347\232\204\346\200\273\351\235\242\347\247\257.md" @@ -48,6 +48,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:岛屿问题再出新花样 | 深搜优先搜索 | 卡码网:101.孤岛总面积](https://www.bilibili.com/video/BV1mmZJYRESc),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 本题使用dfs,bfs,并查集都是可以的。 本题要求找到不靠边的陆地面积,那么我们只要从周边找到陆地然后 通过 dfs或者bfs 将周边靠陆地且相邻的陆地都变成海洋,然后再去重新遍历地图 统计此时还剩下的陆地就可以了。 @@ -700,4 +702,3 @@ const bfs = (graph, x, y) => { ### C -
diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index 1b31676277..e335a018f5 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -55,6 +55,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:岛屿问题也需要逆向思考 | 深搜优先搜索DFS | 卡码网:102.沉没孤岛](https://www.bilibili.com/video/BV1fjdWYyEGu),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 这道题目和[0101.孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173)正好反过来了,[0101.孤岛的总面积](https://kamacoder.com/problempage.php?pid=1173)是求 地图中间的空格数,而本题是要把地图中间的 1 都改成 0 。 那么两题在思路上也是差不多的。 @@ -503,4 +505,3 @@ const bfs = (graph, x, y) => { ### C -
diff --git "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" index bf6cd40f43..9ae8f745fb 100644 --- "a/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" +++ "b/problems/kamacoder/0103.\346\260\264\346\265\201\351\227\256\351\242\230.md" @@ -59,6 +59,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:水流向何方? | 深搜优先搜索DFS| 广度优先搜索BFS | 卡码网:103.水流问题](https://www.bilibili.com/video/BV1WNoEYrEio),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 一个比较直白的想法,其实就是 遍历每个点,然后看这个点 能不能同时到达第一组边界和第二组边界。 至于遍历方式,可以用dfs,也可以用bfs,以下用dfs来举例。 @@ -854,4 +856,3 @@ const isResult = (x, y) => { -
diff --git "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" index 8c4964a9e4..619c5bccb8 100644 --- "a/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" +++ "b/problems/kamacoder/0104.\345\273\272\351\200\240\346\234\200\345\244\247\345\262\233\345\261\277.md" @@ -50,6 +50,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:岛屿问题上难度了! |深搜优先搜索DFS | 广度优先搜索BFS | 卡码网:104.建造最大岛屿](https://www.bilibili.com/video/BV1Dn5CzZEw1/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 本题的一个暴力想法,应该是遍历地图尝试 将每一个 0 改成1,然后去搜索地图中的最大的岛屿面积。 计算地图的最大面积:遍历地图 + 深搜岛屿,时间复杂度为 n * n。 @@ -663,4 +665,3 @@ const dfs = (graph, visited, x, y, mark) => { ### C -
diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 1358f673d5..014050fd40 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -1,7 +1,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!

-# 105.有向图的完全可达性 +# 105.有向图的完全联通 [卡码网题目链接(ACM模式)](https://kamacoder.com/problempage.php?pid=1177) @@ -44,6 +44,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:这次我们能到达有向图的所有节点吗?| 深度优先搜索 | 卡码网:105. 有向图的完全联通](https://www.bilibili.com/video/BV1QRJ6ziEvJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 本题给我们是一个有向图, 意识到这是有向图很重要! 接下来我们再画一个图,从图里可以直观看出来,节点6 是 不能到达节点1 的 @@ -553,4 +555,3 @@ rl.on('close',()=>{ ### C -
diff --git "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 4492d5cd6d..3708715a4b 100644 --- "a/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/kamacoder/0106.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -47,6 +47,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:这次的岛屿问题有点简单了。。 卡码网:106.岛屿的周长](https://www.bilibili.com/video/BV13fjczSEyV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 岛屿问题最容易让人想到BFS或者DFS,但本题确实还用不上。 为了避免大家惯性思维,所以给大家安排了这道题目。 @@ -359,4 +361,3 @@ func parseLine(line string, count int) []int { ### C -
diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 9ab3388f17..572d4712e6 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -422,4 +422,3 @@ const isSame = (u, v) => { ### C -
diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index de2435073c..a5076468df 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -376,4 +376,3 @@ const isSame = (u, v) => { ### C -
diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 6ad59c4188..fb8edbee77 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -596,4 +596,3 @@ const getRemoveEdge = (edges) => { ### C -
diff --git "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" index 6cb5886d20..facb5f21d8 100644 --- "a/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" +++ "b/problems/kamacoder/0110.\345\255\227\347\254\246\344\270\262\346\216\245\351\276\231.md" @@ -63,6 +63,8 @@ yhn ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:朴实无华的广搜这么难? | 广度优先搜索 | 卡码网:110.字符串接龙](https://www.bilibili.com/video/BV1QEEizDEC4),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 以示例1为例,从这个图中可以看出 abc 到 def的路线 不止一条,但最短的一条路径上是4个节点。 ![](https://file1.kamacoder.com/i/algo/20250317105155.png) @@ -360,4 +362,3 @@ const init = async () => { ### C -
diff --git "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" index c5650d9708..d92eede505 100644 --- "a/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" +++ "b/problems/kamacoder/0117.\350\275\257\344\273\266\346\236\204\345\273\272.md" @@ -539,4 +539,3 @@ const init = async () => { ### C -
diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 6669ce7ba1..8fd331e3cd 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -684,4 +684,3 @@ int main() { -
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" "b/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" index c5122b1760..362ea9714a 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\344\270\272\344\273\200\344\271\210\347\224\250ACM\346\250\241\345\274\217.md" @@ -91,4 +91,3 @@ cout << result[result.size() - 1]; 等大家将图论刷完,就会感受到我的良苦用心。加油 -
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 61d49b5297..9566a7b7ec 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -454,4 +454,3 @@ void join(int u, int v) { 敬请期待 并查集题目精讲系列。 -
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 718f5484a1..c76917b742 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\277\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,5 +1,7 @@ # 广度优先搜索理论基础 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[广度优先搜索理论基础](https://www.bilibili.com/video/BV1M19iY4EL9),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 在[深度优先搜索](./图论深搜理论基础.md)的讲解中,我们就讲过深度优先搜索和广度优先搜索的区别。 广搜(bfs)是一圈一圈的搜索过程,和深搜(dfs)是一条路跑到黑然后再回溯。 @@ -102,4 +104,3 @@ void bfs(vector>& grid, vector>& visited, int x, int y 相信看完本篇,大家会对广搜有一个基础性的认识,后面再来做对应的题目就会得心应手一些。 -
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" index d7b8da94cb..d89d6411e9 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\200\273\347\273\223\347\257\207.md" @@ -143,4 +143,3 @@ kruscal的主要思路: 图论也是我 《代码随想录》所有章节里 所费精力最大的一个章节。 只为了不负录友们的期待。 大家加油💪🏻 -
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 50df8aa6df..29256de466 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,6 +1,8 @@ # 深度优先搜索理论基础 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[深度优先搜索理论基础](https://www.bilibili.com/video/BV1hFA8eKE6C),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 录友们期待图论内容已久了,为什么鸽了这么久,主要是最近半年开始更新[代码随想录算法公开课](https://www.bilibili.com/video/BV1fA4y1o715/),是开源在B站的算法视频,已经帮助非常多基础不好的录友学习算法。 录视频其实是非常累的,也要花很多时间,所以图论这边就没抽出时间来。 @@ -194,4 +196,3 @@ for (选择:本节点所连接的其他节点) { 后面我也会给大家安排具体练习的题目,依旧是代码随想录的风格,循序渐进由浅入深! -
diff --git "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" index fb52c83921..13c59374fa 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,6 +1,8 @@ # 图论理论基础 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论理论基础](https://www.bilibili.com/video/BV1hYNBeYEvb/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 这一篇我们正式开始图论! 代码随想录图论中的算法题目将统一使用ACM模式,[为什么要使用ACM模式](./图论为什么用ACM模式.md) @@ -246,4 +248,3 @@ dfs 和 bfs 一种搜索算法,可以在不同的数据结构上进行搜索 敬请期待! -
diff --git "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index 194f1f5ee2..041d41b1ac 100644 --- "a/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/kamacoder/\346\234\200\347\237\255\350\267\257\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -51,4 +51,3 @@ -
diff --git a/problems/qita/shejimoshi.md b/problems/qita/shejimoshi.md deleted file mode 100644 index 959a3fa90a..0000000000 --- a/problems/qita/shejimoshi.md +++ /dev/null @@ -1,57 +0,0 @@ - -# 23种设计模式精讲 | 配套练习题 | 卡码网 - -关于设计模式的学习,大家应该还是看书或者看博客,但却没有一个边学边练的学习环境。 - -学完了一种设计模式 是不是应该去练一练? - -所以卡码网 针对 23种设计,**推出了 23道编程题目,来帮助大家练习设计模式**。 - -
- -这里的23到编程题目对应了 23种这几模式。 例如第一题,小明的购物车,就是单例模式: - -
- -区别于网上其他教程,本教程的特点是: - -* **23种设计模式全覆盖**,涵盖了所有Gang of Four设计模式,包括创建型、结构型和行为型设计模式。 -* 通过23道简单而实用的例子,**以刷算法题的形式了解每种设计模式的概念、结构和应用场景**。 -* **为每个设计模式提供清晰的文字解释、结构图和代码演示**,帮助你更好地理解和实践。 -* **难度安排循序渐进**,从基础的、常用的设计模式逐步深入。 - -这样的一个学习体验,要收费吗? - -**免费的**! - -相信录友们可能还没有这种学习设计模式的体验,快去卡码网(kamacoder.com)上体验吧。 - -23道 设计模式的题目给大家出了,那么是不是得安排上对应的讲解? - -**当然安排**! - -针对每道题目,还给大家编写了一套 23种设计模式精讲,已经开源到Github上: - -> https://github.com/youngyangyang04/kama-DesignPattern - -支持Java,Python,Go,C++ 版本,也欢迎大家去Github上提交PR,补充其他语言版本。 - -所以题解也免费开放给录友! - -同时还给全部整理到PDF上,这份PDF,我们写的很用心了,来个大家截个图: - -
- -
- -
- -
- -关于设计模式的题目,大家现在就可以去 卡码网(kamacoder)去做了。 - -关于这23道题目对应 设计模式精讲 PDF,也免费分享给录友们,大家可以加我的企业微信获取: -
- -已经有我企业微信的录友,直接发:设计模式,这四个字就好,我会直接发你。 - diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index 32df8af41c..83d88cf448 100755 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -115,7 +115,7 @@ 能把本篇中列举的题目都研究通透的话,你的动规水平就已经非常高了。 对付面试已经足够! -![](https://kstar-1253855093.cos.ap-nanjing.myqcloud.com/baguwenpdf/_%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE_%E9%9D%92.png) +![image](https://file1.kamacoder.com/i/algo/_动态规划思维导图_青.png) 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 From 0afc3f7d7af076545c9a58758c147cdee6c50726 Mon Sep 17 00:00:00 2001 From: zkws <15201650319@163.com> Date: Wed, 16 Jul 2025 17:21:14 +0800 Subject: [PATCH 1527/1533] =?UTF-8?q?=E4=BF=AE=E5=A4=8D0494.=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=92=8C=E9=A2=98=E7=9B=AEPython=E4=BA=8C=E7=BB=B4DP?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E4=B8=AD=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在遍历过程中,只有j >= 本次遍历元素才可以选取当前元素,原代码中错写为j>=nums[i-1],修复后变为j>=nums[i],提交才可以通过leetcode所有用例。 --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index b161bc57a8..2e464394b4 100755 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -684,7 +684,7 @@ class Solution: for i in range(1, len(nums)): for j in range(target_sum + 1): dp[i][j] = dp[i - 1][j] # 不选取当前元素 - if j >= nums[i - 1]: + if j >= nums[i]: #只有j >= 本次遍历元素才可以选取当前元素 dp[i][j] += dp[i - 1][j - nums[i]] # 选取当前元素 return dp[len(nums)-1][target_sum] # 返回达到目标和的方案数 From 95cb535731759b2a6e2bd97a5a2dd7f72e24e03b Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 30 Sep 2025 15:19:22 +0800 Subject: [PATCH 1528/1533] update readme --- README.md | 723 ++++++++++++++++++------------------------------------ 1 file changed, 243 insertions(+), 480 deletions(-) diff --git a/README.md b/README.md index 92336a19fc..3484edb83d 100644 --- a/README.md +++ b/README.md @@ -1,507 +1,270 @@ -👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定) -👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master) - -> 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) -> 2. **正式出版** :[《代码随想录》](https://programmercarl.com/qita/publish.html) 。 -> 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/qita/algo_pdf.html) 。 -> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 -> 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 -> 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 -> 7. **学习社区** :一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 -> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://www.programmercarl.com/qita/join.html)了解提交代码的方式。 -> 9. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境! - +# 代码随想录 · LeetCode-Master

- - - + 🌍 海外英文版 · + 🌍🇸 英文仓库 · + 🇨🇳 国内在线阅读 · + 🇨 Gitee 同步

-# LeetCode 刷题攻略 - -## 刷题攻略的背景 - -很多刚开始刷题的同学都有一个困惑:面对leetcode上近两千道题目,从何刷起。 - -大家平时刷题感觉效率低,浪费的时间主要在三点: - -* 找题 -* 找到了不应该现阶段做的题 -* 没有全套的优质题解可以参考 - -其实我之前在知乎上回答过这个问题,回答内容大概是按照如下类型来刷数组-> 链表-> 哈希表->字符串->栈与队列->树->回溯->贪心->动态规划->图论->高级数据结构,再从简单刷起,做了几个类型题目之后,再慢慢做中等题目、困难题目。 - -但我能设身处地的感受到:即使有这样一个整体规划,对于一位初学者甚至算法老手寻找合适自己的题目也是很困难,时间成本很高,而且题目还不一定就是经典题目。 - -对于刷题,我们都是想用最短的时间**按照循序渐进的难度顺序把经典题目都做一遍**,这样效率才是最高的! - -所以我整理了leetcode刷题攻略:一个超级详细的刷题顺序,**每道题目都是我精心筛选,都是经典题目高频面试题**,大家只要按照这个顺序刷就可以了,**你没看错,README已经把题目顺序都排好了,文章顺序就是刷题顺序!挨个刷就可以,不用自己再去题海里选题了!** - -而且每道题目我都写了的详细题解(图文并茂,难点配有视频),力扣上我的题解都是排在对应题目的首页,质量是有目共睹的。 - -**那么现在我把刷题顺序都整理出来,是为了帮助更多的学习算法的同学少走弯路!** - -如果你在刷leetcode,强烈建议先按照本攻略刷题顺序来刷,刷完了你会发现对整个知识体系有一个质的飞跃,不用在题海茫然的寻找方向。 - -
最新文章会首发在公众号「代码随想录」,扫码看看吧,你会发现相见恨晚!
- -
- -## 如何使用该刷题攻略 - -按照先面的排列顺序,从数组开始刷起就可以了,顺序都安排好了,按顺序刷就好。 - -在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,是这个专题的归纳总结。 - -如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 - -**这里每一篇题解,都是精品,值得仔细琢磨**。 - -我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。 - -**所以也欢迎大家参与进来,完善题解的各个语言版本,拥抱开源,让更多小伙伴们受益**。 - -准备好了么,刷题攻略开始咯,go go go! - ---------------------------------------------- - -## 前序 - -* [做项目(多个C++、Java、Go、前端、测开项目)](https://programmercarl.com/other/kstar.html) - - -* 编程语言 - * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) - * [编程语言基础课](https://kamacoder.com/courseshop.php) - * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) - * [大厂算法笔试题](https://kamacoder.com/company.php) - -* 工具 - * [一站式vim配置](https://github.com/youngyangyang04/PowerVim) - * [保姆级Git入门教程,万字详解](https://mp.weixin.qq.com/s/Q_O0ey4C9tryPZaZeJocbA) - * [程序员应该用什么用具来写文档?](./problems/前序/程序员写文档工具.md) - -* 求职 - * [ACM模式练习网站,卡码网](https://kamacoder.com/) - * [程序员的简历应该这么写!!(附简历模板)](./problems/前序/程序员简历.md) - * [【专业技能】应该这样写!](https://programmercarl.com/other/jianlizhuanye.html) - * [【项目经历】应该这样写!](https://programmercarl.com/other/jianlixiangmu.html) - * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) - -* 算法性能分析 - * [关于时间复杂度,你不知道的都在这里!](./problems/前序/时间复杂度.md) - * [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/算法超时.md) - * [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/递归算法的时间复杂度.md) - * [关于空间复杂度,可能有几个疑问?](./problems/前序/空间复杂度.md) - * [递归算法的时间与空间复杂度分析!](./problems/前序/递归算法的时间与空间复杂度分析.md) - * [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/内存消耗.md) - - -## 数组 - -1. [数组过于简单,但你该了解这些!](./problems/数组理论基础.md) -2. [数组:704.二分查找](./problems/0704.二分查找.md) -3. [数组:27.移除元素](./problems/0027.移除元素.md) -4. [数组:977.有序数组的平方](./problems/0977.有序数组的平方.md) -5. [数组:209.长度最小的子数组](./problems/0209.长度最小的子数组.md) -6. [数组:区间和](./problems/kamacoder/0058.区间和.md) -7. [数组:开发商购买土地](./problems/kamacoder/0044.开发商购买土地.md) -8. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md) -9. [数组:总结篇](./problems/数组总结篇.md) - -## 链表 - -1. [关于链表,你该了解这些!](./problems/链表理论基础.md) -2. [链表:203.移除链表元素](./problems/0203.移除链表元素.md) -3. [链表:707.设计链表](./problems/0707.设计链表.md) -4. [链表:206.翻转链表](./problems/0206.翻转链表.md) -5. [链表:24.两两交换链表中的节点](./problems/0024.两两交换链表中的节点.md) -6. [链表:19.删除链表的倒数第 N 个结点](./problems/0019.删除链表的倒数第N个节点.md) -7. [链表:链表相交](./problems/面试题02.07.链表相交.md) -8. [链表:142.环形链表](./problems/0142.环形链表II.md) -9. [链表:总结篇!](./problems/链表总结篇.md) - -## 哈希表 - -1. [关于哈希表,你该了解这些!](./problems/哈希表理论基础.md) -2. [哈希表:242.有效的字母异位词](./problems/0242.有效的字母异位词.md) -3. [哈希表:1002.查找常用字符](./problems/1002.查找常用字符.md) -4. [哈希表:349.两个数组的交集](./problems/0349.两个数组的交集.md) -5. [哈希表:202.快乐数](./problems/0202.快乐数.md) -6. [哈希表:1.两数之和](./problems/0001.两数之和.md) -7. [哈希表:454.四数相加II](./problems/0454.四数相加II.md) -8. [哈希表:383.赎金信](./problems/0383.赎金信.md) -9. [哈希表:15.三数之和](./problems/0015.三数之和.md) -10. [双指针法:18.四数之和](./problems/0018.四数之和.md) -11. [哈希表:总结篇!](./problems/哈希表总结.md) - - -## 字符串 - -1. [字符串:344.反转字符串](./problems/0344.反转字符串.md) -2. [字符串:541.反转字符串II](./problems/0541.反转字符串II.md) -3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md) -4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) -5. [字符串:右旋字符串](./problems/kamacoder/0055.右旋字符串.md) -6. [帮你把KMP算法学个通透](./problems/0028.实现strStr.md) -8. [字符串:459.重复的子字符串](./problems/0459.重复的子字符串.md) -9. [字符串:总结篇!](./problems/字符串总结.md) - -## 双指针法 - -双指针法基本都是应用在数组,字符串与链表的题目上 - -1. [数组:27.移除元素](./problems/0027.移除元素.md) -2. [字符串:344.反转字符串](./problems/0344.反转字符串.md) -3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md) -4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) -5. [链表:206.翻转链表](./problems/0206.翻转链表.md) -6. [链表:19.删除链表的倒数第 N 个结点](./problems/0019.删除链表的倒数第N个节点.md) -7. [链表:链表相交](./problems/面试题02.07.链表相交.md) -8. [链表:142.环形链表](./problems/0142.环形链表II.md) -9. [双指针:15.三数之和](./problems/0015.三数之和.md) -10. [双指针:18.四数之和](./problems/0018.四数之和.md) -11. [双指针:总结篇!](./problems/双指针总结.md) - -## 栈与队列 - -1. [栈与队列:理论基础](./problems/栈与队列理论基础.md) -2. [栈与队列:232.用栈实现队列](./problems/0232.用栈实现队列.md) -3. [栈与队列:225.用队列实现栈](./problems/0225.用队列实现栈.md) -4. [栈与队列:20.有效的括号](./problems/0020.有效的括号.md) -5. [栈与队列:1047.删除字符串中的所有相邻重复项](./problems/1047.删除字符串中的所有相邻重复项.md) -6. [栈与队列:150.逆波兰表达式求值](./problems/0150.逆波兰表达式求值.md) -7. [栈与队列:239.滑动窗口最大值](./problems/0239.滑动窗口最大值.md) -8. [栈与队列:347.前K个高频元素](./problems/0347.前K个高频元素.md) -9. [栈与队列:总结篇!](./problems/栈与队列总结.md) - -## 二叉树 - - -题目分类大纲如下: -二叉树大纲 - -1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md) -2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md) -3. [二叉树:二叉树的迭代遍历](./problems/二叉树的迭代遍历.md) -4. [二叉树:二叉树的统一迭代法](./problems/二叉树的统一迭代法.md) -5. [二叉树:二叉树的层序遍历](./problems/0102.二叉树的层序遍历.md) -6. [二叉树:226.翻转二叉树](./problems/0226.翻转二叉树.md) -7. [本周小结!(二叉树)](./problems/周总结/20200927二叉树周末总结.md) -8. [二叉树:101.对称二叉树](./problems/0101.对称二叉树.md) -9. [二叉树:104.二叉树的最大深度](./problems/0104.二叉树的最大深度.md) -10. [二叉树:111.二叉树的最小深度](./problems/0111.二叉树的最小深度.md) -11. [二叉树:222.完全二叉树的节点个数](./problems/0222.完全二叉树的节点个数.md) -12. [二叉树:110.平衡二叉树](./problems/0110.平衡二叉树.md) -13. [二叉树:257.二叉树的所有路径](./problems/0257.二叉树的所有路径.md) -14. [本周总结!(二叉树)](./problems/周总结/20201003二叉树周末总结.md) -16. [二叉树:404.左叶子之和](./problems/0404.左叶子之和.md) -17. [二叉树:513.找树左下角的值](./problems/0513.找树左下角的值.md) -18. [二叉树:112.路径总和](./problems/0112.路径总和.md) -19. [二叉树:106.构造二叉树](./problems/0106.从中序与后序遍历序列构造二叉树.md) -20. [二叉树:654.最大二叉树](./problems/0654.最大二叉树.md) -21. [本周小结!(二叉树)](./problems/周总结/20201010二叉树周末总结.md) -22. [二叉树:617.合并两个二叉树](./problems/0617.合并二叉树.md) -23. [二叉树:700.二叉搜索树登场!](./problems/0700.二叉搜索树中的搜索.md) -24. [二叉树:98.验证二叉搜索树](./problems/0098.验证二叉搜索树.md) -25. [二叉树:530.搜索树的最小绝对差](./problems/0530.二叉搜索树的最小绝对差.md) -26. [二叉树:501.二叉搜索树中的众数](./problems/0501.二叉搜索树中的众数.md) -27. [二叉树:236.公共祖先问题](./problems/0236.二叉树的最近公共祖先.md) -28. [本周小结!(二叉树)](./problems/周总结/20201017二叉树周末总结.md) -29. [二叉树:235.搜索树的最近公共祖先](./problems/0235.二叉搜索树的最近公共祖先.md) -30. [二叉树:701.搜索树中的插入操作](./problems/0701.二叉搜索树中的插入操作.md) -31. [二叉树:450.搜索树中的删除操作](./problems/0450.删除二叉搜索树中的节点.md) -32. [二叉树:669.修剪二叉搜索树](./problems/0669.修剪二叉搜索树.md) -33. [二叉树:108.将有序数组转换为二叉搜索树](./problems/0108.将有序数组转换为二叉搜索树.md) -34. [二叉树:538.把二叉搜索树转换为累加树](./problems/0538.把二叉搜索树转换为累加树.md) -35. [二叉树:总结篇!(需要掌握的二叉树技能都在这里了)](./problems/二叉树总结篇.md) - -## 回溯算法 - -题目分类大纲如下: - -回溯算法大纲 - -1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md) -2. [回溯算法:77.组合](./problems/0077.组合.md) -3. [回溯算法:77.组合优化](./problems/0077.组合优化.md) -4. [回溯算法:216.组合总和III](./problems/0216.组合总和III.md) -5. [回溯算法:17.电话号码的字母组合](./problems/0017.电话号码的字母组合.md) -6. [本周小结!(回溯算法系列一)](./problems/周总结/20201030回溯周末总结.md) -7. [回溯算法:39.组合总和](./problems/0039.组合总和.md) -8. [回溯算法:40.组合总和II](./problems/0040.组合总和II.md) -9. [回溯算法:131.分割回文串](./problems/0131.分割回文串.md) -10. [回溯算法:93.复原IP地址](./problems/0093.复原IP地址.md) -11. [回溯算法:78.子集](./problems/0078.子集.md) -12. [本周小结!(回溯算法系列二)](./problems/周总结/20201107回溯周末总结.md) -13. [回溯算法:90.子集II](./problems/0090.子集II.md) -14. [回溯算法:491.递增子序列](./problems/0491.递增子序列.md) -15. [回溯算法:46.全排列](./problems/0046.全排列.md) -16. [回溯算法:47.全排列II](./problems/0047.全排列II.md) -17. [本周小结!(回溯算法系列三)](./problems/周总结/20201112回溯周末总结.md) -18. [回溯算法去重问题的另一种写法](./problems/回溯算法去重问题的另一种写法.md) -19. [回溯算法:332.重新安排行程](./problems/0332.重新安排行程.md) -20. [回溯算法:51.N皇后](./problems/0051.N皇后.md) -21. [回溯算法:37.解数独](./problems/0037.解数独.md) -22. [回溯算法总结篇](./problems/回溯总结.md) - -## 贪心算法 - -题目分类大纲如下: - - -贪心算法大纲 - -1. [关于贪心算法,你该了解这些!](./problems/贪心算法理论基础.md) -2. [贪心算法:455.分发饼干](./problems/0455.分发饼干.md) -3. [贪心算法:376.摆动序列](./problems/0376.摆动序列.md) -4. [贪心算法:53.最大子序和](./problems/0053.最大子序和.md) -5. [本周小结!(贪心算法系列一)](./problems/周总结/20201126贪心周末总结.md) -6. [贪心算法:122.买卖股票的最佳时机II](./problems/0122.买卖股票的最佳时机II.md) -7. [贪心算法:55.跳跃游戏](./problems/0055.跳跃游戏.md) -8. [贪心算法:45.跳跃游戏II](./problems/0045.跳跃游戏II.md) -9. [贪心算法:1005.K次取反后最大化的数组和](./problems/1005.K次取反后最大化的数组和.md) -10. [本周小结!(贪心算法系列二)](./problems/周总结/20201203贪心周末总结.md) -11. [贪心算法:134.加油站](./problems/0134.加油站.md) -12. [贪心算法:135.分发糖果](./problems/0135.分发糖果.md) -13. [贪心算法:860.柠檬水找零](./problems/0860.柠檬水找零.md) -14. [贪心算法:406.根据身高重建队列](./problems/0406.根据身高重建队列.md) -15. [本周小结!(贪心算法系列三)](./problems/周总结/20201217贪心周末总结.md) -16. [贪心算法:406.根据身高重建队列(续集)](./problems/根据身高重建队列(vector原理讲解).md) -17. [贪心算法:452.用最少数量的箭引爆气球](./problems/0452.用最少数量的箭引爆气球.md) -18. [贪心算法:435.无重叠区间](./problems/0435.无重叠区间.md) -19. [贪心算法:763.划分字母区间](./problems/0763.划分字母区间.md) -20. [贪心算法:56.合并区间](./problems/0056.合并区间.md) -21. [本周小结!(贪心算法系列四)](./problems/周总结/20201224贪心周末总结.md) -22. [贪心算法:738.单调递增的数字](./problems/0738.单调递增的数字.md) -23. [贪心算法:968.监控二叉树](./problems/0968.监控二叉树.md) -24. [贪心算法:总结篇!(每逢总结必经典)](./problems/贪心算法总结篇.md) - -## 动态规划 - -动态规划专题已经开始啦,来不及解释了,小伙伴们上车别掉队! - - -1. [关于动态规划,你该了解这些!](./problems/动态规划理论基础.md) -2. [动态规划:509.斐波那契数](./problems/0509.斐波那契数.md) -3. [动态规划:70.爬楼梯](./problems/0070.爬楼梯.md) -4. [动态规划:746.使用最小花费爬楼梯](./problems/0746.使用最小花费爬楼梯.md) -5. [本周小结!(动态规划系列一)](./problems/周总结/20210107动规周末总结.md) -6. [动态规划:62.不同路径](./problems/0062.不同路径.md) -7. [动态规划:63.不同路径II](./problems/0063.不同路径II.md) -8. [动态规划:343.整数拆分](./problems/0343.整数拆分.md) -9. [动态规划:96.不同的二叉搜索树](./problems/0096.不同的二叉搜索树.md) -10. [本周小结!(动态规划系列二)](./problems/周总结/20210114动规周末总结.md) - -背包问题系列: - -背包问题大纲 - - -11. [动态规划:01背包理论基础(二维dp数组)](./problems/背包理论基础01背包-1.md) -12. [动态规划:01背包理论基础(一维dp数组)](./problems/背包理论基础01背包-2.md) -13. [动态规划:416.分割等和子集](./problems/0416.分割等和子集.md) -14. [动态规划:1049.最后一块石头的重量II](./problems/1049.最后一块石头的重量II.md) -15. [本周小结!(动态规划系列三)](./problems/周总结/20210121动规周末总结.md) -16. [动态规划:494.目标和](./problems/0494.目标和.md) -17. [动态规划:474.一和零](./problems/0474.一和零.md) -18. [动态规划:完全背包理论基础(二维dp数组)](./problems/背包问题理论基础完全背包.md) -19. [动态规划:完全背包理论基础(一维dp数组)](./problems/背包问题完全背包一维.md) -20. [动态规划:518.零钱兑换II](./problems/0518.零钱兑换II.md) -21. [本周小结!(动态规划系列四)](./problems/周总结/20210128动规周末总结.md) -22. [动态规划:377.组合总和Ⅳ](./problems/0377.组合总和Ⅳ.md) -23. [动态规划:70.爬楼梯(完全背包版本)](./problems/0070.爬楼梯完全背包版本.md) -24. [动态规划:322.零钱兑换](./problems/0322.零钱兑换.md) -25. [动态规划:279.完全平方数](./problems/0279.完全平方数.md) -26. [本周小结!(动态规划系列五)](./problems/周总结/20210204动规周末总结.md) -27. [动态规划:139.单词拆分](./problems/0139.单词拆分.md) -28. [动态规划:多重背包理论基础](./problems/背包问题理论基础多重背包.md) -29. [背包问题总结篇](./problems/背包总结篇.md) - -打家劫舍系列: - -29. [动态规划:198.打家劫舍](./problems/0198.打家劫舍.md) -30. [动态规划:213.打家劫舍II](./problems/0213.打家劫舍II.md) -31. [动态规划:337.打家劫舍III](./problems/0337.打家劫舍III.md) - -股票系列: - -股票问题总结 - - -32. [动态规划:121.买卖股票的最佳时机](./problems/0121.买卖股票的最佳时机.md) -33. [动态规划:本周小结(系列六)](./problems/周总结/20210225动规周末总结.md) -34. [动态规划:122.买卖股票的最佳时机II](./problems/0122.买卖股票的最佳时机II(动态规划).md) -35. [动态规划:123.买卖股票的最佳时机III](./problems/0123.买卖股票的最佳时机III.md) -36. [动态规划:188.买卖股票的最佳时机IV](./problems/0188.买卖股票的最佳时机IV.md) -37. [动态规划:309.最佳买卖股票时机含冷冻期](./problems/0309.最佳买卖股票时机含冷冻期.md) -38. [动态规划:本周小结(系列七)](./problems/周总结/20210304动规周末总结.md) -39. [动态规划:714.买卖股票的最佳时机含手续费](./problems/0714.买卖股票的最佳时机含手续费(动态规划).md) -40. [动态规划:股票系列总结篇](./problems/动态规划-股票问题总结篇.md) - -子序列系列: - - - - -41. [动态规划:300.最长递增子序列](./problems/0300.最长上升子序列.md) -42. [动态规划:674.最长连续递增序列](./problems/0674.最长连续递增序列.md) -43. [动态规划:718.最长重复子数组](./problems/0718.最长重复子数组.md) -44. [动态规划:1143.最长公共子序列](./problems/1143.最长公共子序列.md) -45. [动态规划:1035.不相交的线](./problems/1035.不相交的线.md) -46. [动态规划:53.最大子序和](./problems/0053.最大子序和(动态规划).md) -47. [动态规划:392.判断子序列](./problems/0392.判断子序列.md) -48. [动态规划:115.不同的子序列](./problems/0115.不同的子序列.md) -49. [动态规划:583.两个字符串的删除操作](./problems/0583.两个字符串的删除操作.md) -50. [动态规划:72.编辑距离](./problems/0072.编辑距离.md) -51. [编辑距离总结篇](./problems/为了绝杀编辑距离,卡尔做了三步铺垫.md) -52. [动态规划:647.回文子串](./problems/0647.回文子串.md) -53. [动态规划:516.最长回文子序列](./problems/0516.最长回文子序列.md) -54. [动态规划总结篇](./problems/动态规划总结篇.md) - - -## 单调栈 - -1. [单调栈:739.每日温度](./problems/0739.每日温度.md) -2. [单调栈:496.下一个更大元素I](./problems/0496.下一个更大元素I.md) -3. [单调栈:503.下一个更大元素II](./problems/0503.下一个更大元素II.md) -4. [单调栈:42.接雨水](./problems/0042.接雨水.md) -5. [单调栈:84.柱状图中最大的矩形](./problems/0084.柱状图中最大的矩形.md) - - -## 图论 - -**[图论正式发布](./problems/qita/tulunfabu.md)** - -1. [图论:理论基础](./problems/kamacoder/图论理论基础.md) -2. [图论:深度优先搜索理论基础](./problems/kamacoder/图论深搜理论基础.md) -3. [图论:所有可达路径](./problems/kamacoder/0098.所有可达路径.md) -4. [图论:广度优先搜索理论基础](./problems/kamacoder/图论广搜理论基础.md) -5. [图论:岛屿数量.深搜版](./problems/kamacoder/0099.岛屿的数量深搜.md) -6. [图论:岛屿数量.广搜版](./problems/kamacoder/0099.岛屿的数量广搜.md) -7. [图论:岛屿的最大面积](./problems/kamacoder/0100.岛屿的最大面积.md) -8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md) -9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md) -10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md) -11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) -12. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) -13. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) -14. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) -15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md) -16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md) -17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md) -18. [图论:冗余连接II](./problems/kamacoder/0109.冗余连接II.md) -19. [图论:最小生成树之prim](./problems/kamacoder/0053.寻宝-prim.md) -20. [图论:最小生成树之kruskal](./problems/kamacoder/0053.寻宝-Kruskal.md) -21. [图论:拓扑排序](./problems/kamacoder/0117.软件构建.md) -22. [图论:dijkstra(朴素版)](./problems/kamacoder/0047.参会dijkstra朴素.md) -23. [图论:dijkstra(堆优化版)](./problems/kamacoder/0047.参会dijkstra堆.md) -24. [图论:Bellman_ford 算法](./problems/kamacoder/0094.城市间货物运输I.md) -25. [图论:Bellman_ford 队列优化算法(又名SPFA)](./problems/kamacoder/0094.城市间货物运输I-SPFA.md) -26. [图论:Bellman_ford之判断负权回路](./problems/kamacoder/0095.城市间货物运输II.md) -27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0096.城市间货物运输III.md) -28. [图论:Floyd 算法](./problems/kamacoder/0097.小明逛公园.md) -29. [图论:A * 算法](./problems/kamacoder/0126.骑士的攻击astar.md) -30. [图论:最短路算法总结篇](./problems/kamacoder/最短路问题总结篇.md) -31. [图论:图论总结篇](./problems/kamacoder/图论总结篇.md) - - -(持续更新中....) - -# 补充题目 - -以上题目是重中之重,大家至少要刷两遍以上才能彻底理解,如果熟练以上题目之后还在找其他题目练手,可以再刷以下题目: - -这些题目很不错,但有的题目是和刷题攻略类似的,有的题解后面还会适当补充,所以我还没有将其纳入到刷题攻略。一些题解等日后我完善一下,再纳入到刷题攻略。 - - -## 数组 - -* [1365.有多少小于当前数字的数字](./problems/1365.有多少小于当前数字的数字.md) -* [941.有效的山脉数组](./problems/0941.有效的山脉数组.md) (双指针) -* [1207.独一无二的出现次数](./problems/1207.独一无二的出现次数.md) 数组在哈希法中的经典应用 -* [283.移动零](./problems/0283.移动零.md) 【数组】【双指针】 -* [189.旋转数组](./problems/0189.旋转数组.md) -* [724.寻找数组的中心索引](./problems/0724.寻找数组的中心索引.md) -* [34.在排序数组中查找元素的第一个和最后一个位置](./problems/0034.在排序数组中查找元素的第一个和最后一个位置.md) (二分法) -* [922.按奇偶排序数组II](./problems/0922.按奇偶排序数组II.md) -* [35.搜索插入位置](./problems/0035.搜索插入位置.md) - -## 链表 - -* [24.两两交换链表中的节点](./problems/0024.两两交换链表中的节点.md) -* [234.回文链表](./problems/0234.回文链表.md) -* [143.重排链表](./problems/0143.重排链表.md)【数组】【双向队列】【直接操作链表】 -* [141.环形链表](./problems/0141.环形链表.md) -* [160.相交链表](./problems/面试题02.07.链表相交.md) - -## 哈希表 -* [205.同构字符串](./problems/0205.同构字符串.md):【哈希表的应用】 - -## 字符串 -* [925.长按键入](./problems/0925.长按键入.md) 模拟匹配 -* [0844.比较含退格的字符串](./problems/0844.比较含退格的字符串.md)【栈模拟】【空间更优的双指针】 - -## 二叉树 -* [129.求根到叶子节点数字之和](./problems/0129.求根到叶子节点数字之和.md) -* [1382.将二叉搜索树变平衡](./problems/1382.将二叉搜索树变平衡.md) 构造平衡二叉搜索树 -* [100.相同的树](./problems/0100.相同的树.md) 同101.对称二叉树 一个思路 -* [116.填充每个节点的下一个右侧节点指针](./problems/0116.填充每个节点的下一个右侧节点指针.md) +

+ stars + forks + issues + contributors +

-## 回溯算法 +> 一套 **循序渐进**、**少走弯路** 的刷题计划。 +> 题目已按知识脉络与难度 **排好顺序**,每题配 **图文题解 + 视频讲解**。 +> 适合从零到进阶、系统化掌握数据结构与算法。 -* [52.N皇后II](./problems/0052.N皇后II.md) - +--- -## 贪心 -* [649.Dota2参议院](./problems/0649.Dota2参议院.md) 有难度 -* [1221.分割平衡字符](./problems/1221.分割平衡字符串.md) 简单贪心 +## 🔗 快速入口 -## 动态规划 -* [5.最长回文子串](./problems/0005.最长回文子串.md) 和[647.回文子串](https://mp.weixin.qq.com/s/2WetyP6IYQ6VotegepVpEw) 差不多是一样的 -* [132.分割回文串II](./problems/0132.分割回文串II.md) 与647.回文子串和 5.最长回文子串 很像 -* [673.最长递增子序列的个数](./problems/0673.最长递增子序列的个数.md) +- 📘 **出版书籍**:[《代码随想录》](https://programmercarl.com/qita/publish.html) +- 🧾 **PDF 精讲**:[算法精讲 PDF](https://programmercarl.com/qita/algo_pdf.html) +- 🎬 **算法公开课**:[B 站公开课](https://www.bilibili.com/video/BV1fA4y1o715) +- 🧠 **卡码笔记**:[最强八股文](https://notes.kamacoder.com/) +- 👥 **学习社区**:项目 / 面经 / 学习方法 / 面试技巧 → 加入 [「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) +- 🤝 **参与贡献**:本仓讲解以 C++ 为主,含 Java / Python / Go / JS 多语言实现。想点亮头像 👉 [如何提交代码](https://www.programmercarl.com/qita/join.html) · [致谢贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors) +- 📢 **转载须知**:全部为原创,引用请标注来源;恶意搬运将依法维权。 -## 图论 -* [463.岛屿的周长](./problems/0463.岛屿的周长.md) (模拟) -* [841.钥匙和房间](./problems/0841.钥匙和房间.md) 【有向图】dfs,bfs都可以 -* [127.单词接龙](./problems/0127.单词接龙.md) 广搜 +--- -## 并查集 -* [684.冗余连接](./problems/0684.冗余连接.md) 【并查集基础题目】 -* [685.冗余连接II](./problems/0685.冗余连接II.md)【并查集的应用】 +## 📚 为什么选这套刷题路线? -## 模拟 -* [657.机器人能否返回原点](./problems/0657.机器人能否返回原点.md) -* [31.下一个排列](./problems/0031.下一个排列.md) +- **不再海选题目**:README 就是刷题路线,**按顺序刷**即可。 +- **全链路学习体验**:每个专题含「理论基础 → 实战题目 → 总结复盘」。 +- **经典高频必会**:题目均为**高频面试题**与**典型考点**。 +- **多语言覆盖**:除 C++ 主线,还有社区贡献的多语言实现。 -## 位运算 -* [1356.根据数字二进制下1的数目排序](./problems/1356.根据数字二进制下1的数目排序.md) +

+ + + +

+--- + +## 🚀 如何使用本攻略 + +1. **从头开始**:按模块顺序「数组 → 链表 → 哈希表 → … → 图论」。 +2. **带着问题学**:每个模块先看「理论基础」,再刷对应题单。 +3. **及时复盘**:刷完一个模块,阅读「总结篇」,形成**知识闭环**。 +4. **语言不设限**:题解以 C++ 讲解为主,配多语言代码,思路通用。 + +> **建议**:新手先刷「数组/链表/哈希/字符串」,再进阶到「二叉树/回溯/贪心/动态规划/图论」。 + +--- + +## 🧭 刷题总目录(可折叠) + +> 已根据学习曲线优化排序;下方仅展示每章前若干题目,完整清单请展开查看。 + +
+前序 · 打基础 + +- [做项目(C++ / Java / Go / 前端 / 测开)](https://programmercarl.com/other/kstar.html) +- 编程语言 + - [C++ 面试与学习指南](https://github.com/youngyangyang04/TechCPP) + - [语言基础课](https://programmercarl.com/qita/language.html) + - [23 种设计模式](https://programmercarl.com/ke/shejimoshi.html) + - [大厂算法笔试题](https://programmercarl.com/ke/bishi.html) +- 工具 + - [一站式 Vim 配置](https://github.com/youngyangyang04/PowerVim) + - [万字 Git 入门](https://mp.weixin.qq.com/s/Q_O0ey4C9tryPZaZeJocbA) + - [程序员写文档用什么?](./problems/前序/程序员写文档工具.md) +- 求职 + - [卡码网(ACM 模式练习)](https://kamacoder.com/) + - [简历怎么写(含模板)](./problems/前序/程序员简历.md) + - [专业技能怎么写](https://programmercarl.com/other/jianlizhuanye.html) + - [项目经历怎么写](https://programmercarl.com/other/jianlixiangmu.html) + - [BAT 面试流程与注意事项](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) +- 算法性能分析 + - [时间复杂度全解](./problems/前序/时间复杂度.md) + - [O(n) 也会超时?](./problems/前序/算法超时.md) + - [递归的时空复杂度](./problems/前序/递归算法的时间复杂度.md) + - [空间复杂度常见疑问](./problems/前序/空间复杂度.md) + - [内存消耗那些事](./problems/前序/内存消耗.md) +
+ +
+数组 + +- [数组理论基础](./problems/数组理论基础.md) +- [704. 二分查找](./problems/0704.二分查找.md) +- [27. 移除元素](./problems/0027.移除元素.md) +- [977. 有序数组的平方](./problems/0977.有序数组的平方.md) +- [209. 长度最小的子数组](./problems/0209.长度最小的子数组.md) +- [59. 螺旋矩阵 II](./problems/0059.螺旋矩阵II.md) +- [数组总结篇](./problems/数组总结篇.md) +
+ +
+链表 + +- [链表理论基础](./problems/链表理论基础.md) +- [203. 移除链表元素](./problems/0203.移除链表元素.md) +- [707. 设计链表](./problems/0707.设计链表.md) +- [206. 反转链表](./problems/0206.翻转链表.md) +- [142. 环形链表 II](./problems/0142.环形链表II.md) +- [链表总结篇](./problems/链表总结篇.md) +
+ +
+哈希表 + +- [哈希表理论基础](./problems/哈希表理论基础.md) +- [242. 有效的字母异位词](./problems/0242.有效的字母异位词.md) +- [1. 两数之和](./problems/0001.两数之和.md) +- [15. 三数之和](./problems/0015.三数之和.md) +- [哈希表总结篇](./problems/哈希表总结.md) +
+ +
+字符串 + +- [344. 反转字符串](./problems/0344.反转字符串.md) +- [151. 翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) +- [KMP 通关](./problems/0028.实现strStr.md) +- [459. 重复的子字符串](./problems/0459.重复的子字符串.md) +- [字符串总结篇](./problems/字符串总结.md) +
+ +
+双指针法 + +- [数组 · 27. 移除元素](./problems/0027.移除元素.md) +- [链表 · 19. 删除倒数第 N 个](./problems/0019.删除链表的倒数第N个节点.md) +- [15. 三数之和](./problems/0015.三数之和.md) +- [18. 四数之和](./problems/0018.四数之和.md) +- [双指针总结篇](./problems/双指针总结.md) +
+ +
+栈与队列 + +- [理论基础](./problems/栈与队列理论基础.md) +- [232. 用栈实现队列](./problems/0232.用栈实现队列.md) +- [20. 有效的括号](./problems/0020.有效的括号.md) +- [239. 滑动窗口最大值](./problems/0239.滑动窗口最大值.md) +- [347. 前 K 个高频元素](./problems/0347.前K个高频元素.md) +- [总结篇](./problems/栈与队列总结.md) +
+ +
+二叉树 + +- [二叉树理论基础](./problems/二叉树理论基础.md) +- [递归遍历 / 迭代遍历 / 统一迭代](./problems/二叉树的递归遍历.md) +- [102. 层序遍历](./problems/0102.二叉树的层序遍历.md) +- [226. 翻转二叉树](./problems/0226.翻转二叉树.md) +- [98. 验证二叉搜索树](./problems/0098.验证二叉搜索树.md) +- [236. 最近公共祖先](./problems/0236.二叉树的最近公共祖先.md) +- [二叉树总结篇](./problems/二叉树总结篇.md) +
+ +
+回溯算法 + +- [回溯理论基础](./problems/回溯算法理论基础.md) +- [77. 组合 / 优化](./problems/0077.组合.md) +- [39/40. 组合总和(I/II)](./problems/0039.组合总和.md) +- [46/47. 全排列(I/II)](./problems/0046.全排列.md) +- [51. N 皇后 · 37. 解数独](./problems/0051.N皇后.md) +- [回溯总结篇](./problems/回溯总结.md) +
+ +
+贪心算法 + +- [贪心理论基础](./problems/贪心算法理论基础.md) +- [455. 分发饼干](./problems/0455.分发饼干.md) +- [53. 最大子序和](./problems/0053.最大子序和.md) +- [55/45. 跳跃游戏(I/II)](./problems/0055.跳跃游戏.md) +- [134. 加油站 / 135. 分发糖果](./problems/0134.加油站.md) +- [贪心总结篇](./problems/贪心算法总结篇.md) +
+ +
+动态规划 + +- [动规总览与思维方式](./problems/动态规划理论基础.md) +- 入门:509. 斐波那契 · 70. 爬楼梯 · 746. 使用最小花费爬楼梯 +- 经典:62/63. 不同路径 · 322. 零钱兑换 · 279. 完全平方数 +- 背包:01/完全/多重背包与模板、子集和、组合数 +- 股票:121/122/123/188/309/714 系列 +- 子序列:300 LIS、1143 LCS、72 编辑距离、516 最长回文子序列 +- [背包总结篇](./problems/背包总结篇.md) · [股票总结篇](./problems/动态规划-股票问题总结篇.md) · [动规总结篇](./problems/动态规划总结篇.md) +
+ +
+单调栈 + +- [739. 每日温度](./problems/0739.每日温度.md) +- [496/503. 下一个更大元素 I/II](./problems/0496.下一个更大元素I.md) +- [42. 接雨水 · 84. 柱状图最大矩形](./problems/0042.接雨水.md) +
+ +
+图论 + +- [图论发布说明](./problems/qita/tulunfabu.md) +- 搜索:DFS / BFS、岛屿问题系列、可达性 +- 并查集:冗余连接 I/II、连通性判定 +- 最小生成树:Prim / Kruskal +- 最短路:Dijkstra(朴素/堆)· Bellman-Ford · SPFA · Floyd · A* +- [最短路总结篇](./problems/kamacoder/最短路问题总结篇.md) · [图论总结篇](./problems/kamacoder/图论总结篇.md) +
+ +--- + +## 🧩 算法模板 + +- [各类基础算法模板(持续更新)](https://github.com/youngyangyang04/leetcode/blob/master/problems/算法模板.md) + +--- + +## 🙌 参与贡献 + +- 欢迎提交 **题解修订 / 多语言实现 / 文档勘误 / 新增练习** +- 请先阅读:[如何提交与协作](https://www.programmercarl.com/qita/join.html) +- 致谢所有贡献者 → [Contributors](https://github.com/youngyangyang04/leetcode-master/graphs/contributors) + +--- + +## ⭐ Star 趋势 -# 算法模板 +[![Star History Chart](https://api.star-history.com/svg?repos=youngyangyang04/leetcode-master&type=Date)](https://star-history.com/#youngyangyang04/leetcode-master&Date) -[各类基础算法模板](https://github.com/youngyangyang04/leetcode/blob/master/problems/算法模板.md) +--- -# 贡献者 +## 👨‍💻 关于作者 -[点此这里](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)查看LeetCode-Master的所有贡献者。感谢他们补充了LeetCode-Master的其他语言版本,让更多的读者受益于此项目。 +大家好,我是 **程序员 Carl**,哈工大师兄,先后在腾讯、百度从事后端与底层技术研发。 +著有《代码随想录》,长期输出 **高质量算法题解、编程实战项目与求职指导**。 -# Star 趋势 +--- -[![Star History Chart](https://api.star-history.com/svg?repos=youngyangyang04/leetcode-master&type=Date)](https://star-history.com/#youngyangyang04/leetcode-master&Date) +## 📥 PDF 下载与学习群 -# 关于作者 +添加下方企业微信,自动获取 **PDF 精讲**,并可选择加入刷题群: +> 备注格式 +> - **在职**:姓名-城市-岗位 +> - **学生**:姓名-学校-年级(**无备注不通过**) -大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术底层技术研发。 +

+ +

-# PDF下载 +--- -添加如下企业微信,会自动发送给大家PDF版本,顺便可以选择是否加入刷题群。 +## 📜 版权说明 -添加微信记得备注,如果是已工作,备注:姓名-城市-岗位。如果学生,备注:姓名-学校-年级。**备注没有自我介绍不通过哦** +- 本仓库所有内容均为原创,引用需 **注明出处与链接**。 +- 严禁恶意搬运与洗稿,侵权必究。 -
+--- From a6c33f859f91e5bcc9ffe5dfa59199a49e69cbfe Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 30 Sep 2025 15:20:46 +0800 Subject: [PATCH 1529/1533] Update --- ...5\220\210\346\200\273\345\222\214\342\205\243.md" | 2 +- ....\345\217\202\344\274\232dijkstra\345\240\206.md" | 6 +++--- .../0053.\345\257\273\345\256\235-Kruskal.md" | 2 ++ .../kamacoder/0053.\345\257\273\345\256\235-prim.md" | 2 ++ ...6\230\216\351\200\233\345\205\254\345\233\255.md" | 4 ++++ ...5\205\250\345\217\257\350\276\276\346\200\247.md" | 10 +++++----- ...5\234\250\347\232\204\350\267\257\345\276\204.md" | 2 ++ ...5\206\227\344\275\231\350\277\236\346\216\245.md" | 2 ++ ...206\227\344\275\231\350\277\236\346\216\245II.md" | 2 ++ ...\253\347\232\204\346\224\273\345\207\273astar.md" | 12 +++++------- ...7\220\206\350\256\272\345\237\272\347\241\200.md" | 2 ++ problems/qita/language.md | 1 + ...6\226\207\346\241\243\345\267\245\345\205\267.md" | 3 --- 13 files changed, 31 insertions(+), 19 deletions(-) diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index ab92f24aef..dacc9a7e98 100755 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -117,7 +117,7 @@ public: dp[0] = 1; for (int i = 0; i <= target; i++) { // 遍历背包 for (int j = 0; j < nums.size(); j++) { // 遍历物品 - if (i - nums[j] >= 0 && dp[i] < INT_MAX - dp[i - nums[j]]) { + if (i - nums[j] >= 0 && dp[i] <= INT_MAX - dp[i - nums[j]]) { dp[i] += dp[i - nums[j]]; } } diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" index fceef23926..a0a0bf8bf7 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\345\240\206.md" @@ -614,12 +614,12 @@ int main() { ``` -* 时间复杂度:O(E * (N + logE)) E为边的数量,N为节点数量 +* 时间复杂度:O(E * N * logE) E为边的数量,N为节点数量 * 空间复杂度:O(log(N^2)) -`while (!pq.empty())` 时间复杂度为 E ,while 里面 每次取元素 时间复杂度 为 logE,和 一个for循环 时间复杂度 为 N 。 +`while (!pq.empty())` 时间复杂度为 E ,优先级队列每次插入元素 时间复杂度 为 logE,和 一个for循环 时间复杂度 为 N 。 -所以整体是 E * (N + logE) +所以整体是 E * N * logE ## 总结 diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" index a7022c5d08..9da7ae5399 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-Kruskal.md" @@ -46,6 +46,8 @@ ## 解题思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:最小生成树之kruskal算法](https://www.bilibili.com/video/BV1GD3uz8EY4),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 在上一篇 我们讲解了 prim算法求解 最小生成树,本篇我们来讲解另一个算法:Kruskal,同样可以求最小生成树。 **prim 算法是维护节点的集合,而 Kruskal 是维护边的集合**。 diff --git "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" index d3f0aeb373..1339957d67 100644 --- "a/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" +++ "b/problems/kamacoder/0053.\345\257\273\345\256\235-prim.md" @@ -47,6 +47,8 @@ ## 解题思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:最小生成树之prim算法](https://www.bilibili.com/video/BV1gFKVzpExq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 本题是最小生成树的模板题,那么我们来讲一讲最小生成树。 最小生成树可以使用prim算法也可以使用kruskal算法计算出来。 diff --git "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" index a2b673b373..abe20d5130 100644 --- "a/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" +++ "b/problems/kamacoder/0097.\345\260\217\346\230\216\351\200\233\345\205\254\345\233\255.md" @@ -31,6 +31,7 @@ 【输入示例】 +``` 7 3 1 2 4 2 5 6 @@ -38,11 +39,14 @@ 2 1 2 2 3 +``` 【输出示例】 +``` 4 -1 +``` 【提示信息】 diff --git "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" index 014050fd40..7bf0a4f350 100644 --- "a/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" +++ "b/problems/kamacoder/0105.\346\234\211\345\220\221\345\233\276\347\232\204\345\256\214\345\205\250\345\217\257\350\276\276\346\200\247.md" @@ -66,15 +66,15 @@ 1. 确认递归函数,参数 -需要传入地图,需要知道当前我们拿到的key,以至于去下一个房间。 +需要传入地图,需要知道当前我们拿到的key,以至于去下一个节点。 -同时还需要一个数组,用来记录我们都走过了哪些房间,这样好知道最后有没有把所有房间都遍历的,可以定义一个一维数组。 +同时还需要一个数组,用来记录我们都走过了哪些节点,这样好知道最后有没有把所有节点都遍历的,可以定义一个一维数组。 所以 递归函数参数如下: ```C++ // key 当前得到的可以 -// visited 记录访问过的房间 +// visited 记录访问过的节点 void dfs(const vector>& graph, int key, vector& visited) { ``` @@ -259,9 +259,9 @@ int main() { } vector visited(n + 1, false); - visited[1] = true; // 1 号房间开始 + visited[1] = true; // 节点1开始 queue que; - que.push(1); // 1 号房间开始 + que.push(1); // 节点1开始 // 广度优先搜索的过程 while (!que.empty()) { diff --git "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" index 572d4712e6..12388d83b0 100644 --- "a/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/kamacoder/0107.\345\257\273\346\211\276\345\255\230\345\234\250\347\232\204\350\267\257\345\276\204.md" @@ -48,6 +48,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:没想到并查集这么简单 !](https://www.bilibili.com/video/BV1k3T7zWEVj),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 本题是并查集基础题目。 如果还不了解并查集,可以看这里:[并查集理论基础](https://programmercarl.com/kamacoder/图论并查集理论基础.html) 并查集可以解决什么问题呢? diff --git "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" index a5076468df..46b06b75b0 100644 --- "a/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/kamacoder/0108.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -52,6 +52,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:并查集有点不简单了。。](https://www.bilibili.com/video/BV1gRM3z9EwZ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 这道题目也是并查集基础题目。 这里我依然降调一下,并查集可以解决什么问题:两个节点是否在一个集合,也可以将两个节点添加到一个集合中。 diff --git "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index fb8edbee77..fb8640e3b3 100644 --- "a/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/kamacoder/0109.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -52,6 +52,8 @@ ## 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:并查集这次要上难度了](https://www.bilibili.com/video/BV1t2NEzaEMR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 本题与 [108.冗余连接](./0108.冗余连接.md) 类似,但本题是一个有向图,有向图相对要复杂一些。 本题的本质是 :有一个有向图,是由一颗有向树 + 一条有向边组成的 (所以此时这个图就不能称之为有向树),现在让我们找到那条边 把这条边删了,让这个图恢复为有向树。 diff --git "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" index 4e131480d8..17d7ba7530 100644 --- "a/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" +++ "b/problems/kamacoder/0126.\351\252\221\345\243\253\347\232\204\346\224\273\345\207\273astar.md" @@ -279,17 +279,15 @@ int main() ## 复杂度分析 -A * 算法的时间复杂度 其实是不好去量化的,因为他取决于 启发式函数怎么写。 +A*算法的时间复杂度其实是不容易量化的,这取决于怎么写启发式函数。 -最坏情况下,A * 退化成广搜,算法的时间复杂度 是 O(n * 2),n 为节点数量。 +最坏情况下,A*算法退化成BFS,时间复杂度是O(n^2),n为节点的数量。 -最佳情况,是从起点直接到终点,时间复杂度为 O(dlogd),d 为起点到终点的深度。 +一般情况下,搜索路径是从起点直接到终点,while(!que.empty()) 需要执行d次,d为起点到终点的深度,而优先级队列每次添加元素都要进行堆排序,时间复杂度是O(logk),k为队列中元素的数量。所以时间复杂度为O(dlogk)。 -因为在搜索的过程中也需要堆排序,所以是 O(dlogd)。 +也可以非常粗略地认为A*算法的时间复杂度是O(nlogn),n为节点的数量。 -实际上 A * 的时间复杂度是介于 最优 和最坏 情况之间, 可以 非常粗略的认为 A * 算法的时间复杂度是 O(nlogn) ,n 为节点数量。 - -A * 算法的空间复杂度 O(b ^ d) ,d 为起点到终点的深度,b 是 图中节点间的连接数量,本题因为是无权网格图,所以 节点间连接数量为 4。 +本题中A*算法的空间复杂度是O(k),k为队列中元素的数量,空间消耗主要是队列里需要存放遍历的节点。 ## 拓展 diff --git "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" index 9566a7b7ec..4db52c92bd 100644 --- "a/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/kamacoder/\345\233\276\350\256\272\345\271\266\346\237\245\351\233\206\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -1,5 +1,7 @@ # 并查集理论基础 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[图论:并查集理论基础!](https://www.bilibili.com/video/BV1pFjUzvES2/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + 接下来我们来讲一下并查集,首先当然是并查集理论基础。 ## 背景 diff --git a/problems/qita/language.md b/problems/qita/language.md index 625154e0a5..371bef9096 100755 --- a/problems/qita/language.md +++ b/problems/qita/language.md @@ -7,6 +7,7 @@ 如果你是编程零基础,又想快速达到刷算法题(或者说刷代码随想录)所需编程语言的水平,推荐 +* [设计模式编程课](../ke/shejimoshi.md) * [C++基础课](../ke/cplus.md) * [Java基础课](../ke/java.md) * [Python基础课](../ke/python.md) diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" index c991be1542..17b25aa1eb 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" @@ -1,8 +1,6 @@ # 程序员应该用什么用具来写文档? -

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- Carl平时写东西,都是统一使用markdown,包括题解啊,笔记啊,所以这里给大家安利一波markdown对程序员的重要性! @@ -130,4 +128,3 @@ Markdown支持部分html,例如这样 ----------------------- -
From 32736f2615400953b059e7519ce63f293def523d Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 30 Sep 2025 15:29:42 +0800 Subject: [PATCH 1530/1533] update readme --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3484edb83d..46700c6254 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ 🌍 海外英文版 · 🌍🇸 英文仓库 · 🇨🇳 国内在线阅读 · - 🇨 Gitee 同步 + 🇨 Gitee 同步

@@ -23,9 +23,9 @@ ## 🔗 快速入口 -- 📘 **出版书籍**:[《代码随想录》](https://programmercarl.com/qita/publish.html) +- 📘 **出版书籍**:[《代码随想录》](https://union-click.jd.com/jdc?e=618%7Cpc%7C&p=JF8BASMJK1olXwABU1pUCU0SCl8IGV8WVAICU24ZVxNJXF9RXh5UHw0cSgYYXBcIWDoXSQVJQwYAUF1UDEsQHDZNRwYlVEBGPAIccE51dQ1cfjpVCnsHUjYbTkcbM244GFIXWQYAUV5VOHsXBF9adYOj696n5UKJosTCi_g4GmsVWwILVFhZCUIXBWgMK1wVVDZfHAIVXwAnM18LK1wVVBIEJh8PHE1lM18IK1glXQcCVVpYDU8RB2YUG18QXA4BSF5bDEIXBWsJHlgVXAEyVl9cDEInM7GFqyYQWHkHVBY1TUxoBmZtXT7L0LYTKClfCkMWEl8BGCMVCkFGBg01Dg5zSgcJUCxeD2AKNRwzChFKfGx3HQtCDnN3XV0aDB1KM2o4G10VXzY) - 🧾 **PDF 精讲**:[算法精讲 PDF](https://programmercarl.com/qita/algo_pdf.html) -- 🎬 **算法公开课**:[B 站公开课](https://www.bilibili.com/video/BV1fA4y1o715) +- 🎬 **算法公开课**:[170期硬核视频](https://www.bilibili.com/video/BV1fA4y1o715) - 🧠 **卡码笔记**:[最强八股文](https://notes.kamacoder.com/) - 👥 **学习社区**:项目 / 面经 / 学习方法 / 面试技巧 → 加入 [「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) - 🤝 **参与贡献**:本仓讲解以 C++ 为主,含 Java / Python / Go / JS 多语言实现。想点亮头像 👉 [如何提交代码](https://www.programmercarl.com/qita/join.html) · [致谢贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors) @@ -243,8 +243,7 @@ ## 👨‍💻 关于作者 -大家好,我是 **程序员 Carl**,哈工大师兄,先后在腾讯、百度从事后端与底层技术研发。 -著有《代码随想录》,长期输出 **高质量算法题解、编程实战项目与求职指导**。 +大家好,我是 **程序员 Carl**,哈工大师兄,先后在腾讯、百度从事后端与底层技术研发,著有《代码随想录》。 --- From 60b1ce636e60572dc7479eca691bfa9cb3fab299 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 30 Sep 2025 15:35:54 +0800 Subject: [PATCH 1531/1533] Update readme --- README.md | 391 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 300 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 46700c6254..d9f060b7c2 100644 --- a/README.md +++ b/README.md @@ -66,157 +66,366 @@

前序 · 打基础 -- [做项目(C++ / Java / Go / 前端 / 测开)](https://programmercarl.com/other/kstar.html) -- 编程语言 - - [C++ 面试与学习指南](https://github.com/youngyangyang04/TechCPP) - - [语言基础课](https://programmercarl.com/qita/language.html) - - [23 种设计模式](https://programmercarl.com/ke/shejimoshi.html) - - [大厂算法笔试题](https://programmercarl.com/ke/bishi.html) -- 工具 - - [一站式 Vim 配置](https://github.com/youngyangyang04/PowerVim) - - [万字 Git 入门](https://mp.weixin.qq.com/s/Q_O0ey4C9tryPZaZeJocbA) - - [程序员写文档用什么?](./problems/前序/程序员写文档工具.md) -- 求职 - - [卡码网(ACM 模式练习)](https://kamacoder.com/) - - [简历怎么写(含模板)](./problems/前序/程序员简历.md) - - [专业技能怎么写](https://programmercarl.com/other/jianlizhuanye.html) - - [项目经历怎么写](https://programmercarl.com/other/jianlixiangmu.html) - - [BAT 面试流程与注意事项](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) -- 算法性能分析 - - [时间复杂度全解](./problems/前序/时间复杂度.md) - - [O(n) 也会超时?](./problems/前序/算法超时.md) - - [递归的时空复杂度](./problems/前序/递归算法的时间复杂度.md) - - [空间复杂度常见疑问](./problems/前序/空间复杂度.md) - - [内存消耗那些事](./problems/前序/内存消耗.md) +* [做项目(多个C++、Java、Go、前端、测开项目)](https://programmercarl.com/other/kstar.html) + + +* 编程语言 + * [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP) + * [编程语言基础课](https://kamacoder.com/courseshop.php) + * [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern) + * [大厂算法笔试题](https://kamacoder.com/company.php) + +* 工具 + * [一站式vim配置](https://github.com/youngyangyang04/PowerVim) + * [保姆级Git入门教程,万字详解](https://mp.weixin.qq.com/s/Q_O0ey4C9tryPZaZeJocbA) + * [程序员应该用什么用具来写文档?](./problems/前序/程序员写文档工具.md) + +* 求职 + * [ACM模式练习网站,卡码网](https://kamacoder.com/) + * [程序员的简历应该这么写!!(附简历模板)](./problems/前序/程序员简历.md) + * [【专业技能】应该这样写!](https://programmercarl.com/other/jianlizhuanye.html) + * [【项目经历】应该这样写!](https://programmercarl.com/other/jianlixiangmu.html) + * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) + +* 算法性能分析 + * [关于时间复杂度,你不知道的都在这里!](./problems/前序/时间复杂度.md) + * [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/算法超时.md) + * [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/递归算法的时间复杂度.md) + * [关于空间复杂度,可能有几个疑问?](./problems/前序/空间复杂度.md) + * [递归算法的时间与空间复杂度分析!](./problems/前序/递归算法的时间与空间复杂度分析.md) + * [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/内存消耗.md)
数组 -- [数组理论基础](./problems/数组理论基础.md) -- [704. 二分查找](./problems/0704.二分查找.md) -- [27. 移除元素](./problems/0027.移除元素.md) -- [977. 有序数组的平方](./problems/0977.有序数组的平方.md) -- [209. 长度最小的子数组](./problems/0209.长度最小的子数组.md) -- [59. 螺旋矩阵 II](./problems/0059.螺旋矩阵II.md) -- [数组总结篇](./problems/数组总结篇.md) + +1. [数组过于简单,但你该了解这些!](./problems/数组理论基础.md) +2. [数组:704.二分查找](./problems/0704.二分查找.md) +3. [数组:27.移除元素](./problems/0027.移除元素.md) +4. [数组:977.有序数组的平方](./problems/0977.有序数组的平方.md) +5. [数组:209.长度最小的子数组](./problems/0209.长度最小的子数组.md) +6. [数组:区间和](./problems/kamacoder/0058.区间和.md) +7. [数组:开发商购买土地](./problems/kamacoder/0044.开发商购买土地.md) +8. [数组:59.螺旋矩阵II](./problems/0059.螺旋矩阵II.md) +9. [数组:总结篇](./problems/数组总结篇.md)
链表 -- [链表理论基础](./problems/链表理论基础.md) -- [203. 移除链表元素](./problems/0203.移除链表元素.md) -- [707. 设计链表](./problems/0707.设计链表.md) -- [206. 反转链表](./problems/0206.翻转链表.md) -- [142. 环形链表 II](./problems/0142.环形链表II.md) -- [链表总结篇](./problems/链表总结篇.md) +1. [关于链表,你该了解这些!](./problems/链表理论基础.md) +2. [链表:203.移除链表元素](./problems/0203.移除链表元素.md) +3. [链表:707.设计链表](./problems/0707.设计链表.md) +4. [链表:206.翻转链表](./problems/0206.翻转链表.md) +5. [链表:24.两两交换链表中的节点](./problems/0024.两两交换链表中的节点.md) +6. [链表:19.删除链表的倒数第 N 个结点](./problems/0019.删除链表的倒数第N个节点.md) +7. [链表:链表相交](./problems/面试题02.07.链表相交.md) +8. [链表:142.环形链表](./problems/0142.环形链表II.md) +9. [链表:总结篇!](./problems/链表总结篇.md) +
哈希表 -- [哈希表理论基础](./problems/哈希表理论基础.md) -- [242. 有效的字母异位词](./problems/0242.有效的字母异位词.md) -- [1. 两数之和](./problems/0001.两数之和.md) -- [15. 三数之和](./problems/0015.三数之和.md) -- [哈希表总结篇](./problems/哈希表总结.md) + +1. [关于哈希表,你该了解这些!](./problems/哈希表理论基础.md) +2. [哈希表:242.有效的字母异位词](./problems/0242.有效的字母异位词.md) +3. [哈希表:1002.查找常用字符](./problems/1002.查找常用字符.md) +4. [哈希表:349.两个数组的交集](./problems/0349.两个数组的交集.md) +5. [哈希表:202.快乐数](./problems/0202.快乐数.md) +6. [哈希表:1.两数之和](./problems/0001.两数之和.md) +7. [哈希表:454.四数相加II](./problems/0454.四数相加II.md) +8. [哈希表:383.赎金信](./problems/0383.赎金信.md) +9. [哈希表:15.三数之和](./problems/0015.三数之和.md) +10. [双指针法:18.四数之和](./problems/0018.四数之和.md) +11. [哈希表:总结篇!](./problems/哈希表总结.md)
字符串 -- [344. 反转字符串](./problems/0344.反转字符串.md) -- [151. 翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) -- [KMP 通关](./problems/0028.实现strStr.md) -- [459. 重复的子字符串](./problems/0459.重复的子字符串.md) -- [字符串总结篇](./problems/字符串总结.md) + +1. [字符串:344.反转字符串](./problems/0344.反转字符串.md) +2. [字符串:541.反转字符串II](./problems/0541.反转字符串II.md) +3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md) +4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) +5. [字符串:右旋字符串](./problems/kamacoder/0055.右旋字符串.md) +6. [帮你把KMP算法学个通透](./problems/0028.实现strStr.md) +8. [字符串:459.重复的子字符串](./problems/0459.重复的子字符串.md) +9. [字符串:总结篇!](./problems/字符串总结.md)
双指针法 -- [数组 · 27. 移除元素](./problems/0027.移除元素.md) -- [链表 · 19. 删除倒数第 N 个](./problems/0019.删除链表的倒数第N个节点.md) -- [15. 三数之和](./problems/0015.三数之和.md) -- [18. 四数之和](./problems/0018.四数之和.md) -- [双指针总结篇](./problems/双指针总结.md) +双指针法基本都是应用在数组,字符串与链表的题目上 + +1. [数组:27.移除元素](./problems/0027.移除元素.md) +2. [字符串:344.反转字符串](./problems/0344.反转字符串.md) +3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md) +4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md) +5. [链表:206.翻转链表](./problems/0206.翻转链表.md) +6. [链表:19.删除链表的倒数第 N 个结点](./problems/0019.删除链表的倒数第N个节点.md) +7. [链表:链表相交](./problems/面试题02.07.链表相交.md) +8. [链表:142.环形链表](./problems/0142.环形链表II.md) +9. [双指针:15.三数之和](./problems/0015.三数之和.md) +10. [双指针:18.四数之和](./problems/0018.四数之和.md) +11. [双指针:总结篇!](./problems/双指针总结.md)
栈与队列 -- [理论基础](./problems/栈与队列理论基础.md) -- [232. 用栈实现队列](./problems/0232.用栈实现队列.md) -- [20. 有效的括号](./problems/0020.有效的括号.md) -- [239. 滑动窗口最大值](./problems/0239.滑动窗口最大值.md) -- [347. 前 K 个高频元素](./problems/0347.前K个高频元素.md) -- [总结篇](./problems/栈与队列总结.md) +1. [栈与队列:理论基础](./problems/栈与队列理论基础.md) +2. [栈与队列:232.用栈实现队列](./problems/0232.用栈实现队列.md) +3. [栈与队列:225.用队列实现栈](./problems/0225.用队列实现栈.md) +4. [栈与队列:20.有效的括号](./problems/0020.有效的括号.md) +5. [栈与队列:1047.删除字符串中的所有相邻重复项](./problems/1047.删除字符串中的所有相邻重复项.md) +6. [栈与队列:150.逆波兰表达式求值](./problems/0150.逆波兰表达式求值.md) +7. [栈与队列:239.滑动窗口最大值](./problems/0239.滑动窗口最大值.md) +8. [栈与队列:347.前K个高频元素](./problems/0347.前K个高频元素.md) +9. [栈与队列:总结篇!](./problems/栈与队列总结.md)
二叉树 -- [二叉树理论基础](./problems/二叉树理论基础.md) -- [递归遍历 / 迭代遍历 / 统一迭代](./problems/二叉树的递归遍历.md) -- [102. 层序遍历](./problems/0102.二叉树的层序遍历.md) -- [226. 翻转二叉树](./problems/0226.翻转二叉树.md) -- [98. 验证二叉搜索树](./problems/0098.验证二叉搜索树.md) -- [236. 最近公共祖先](./problems/0236.二叉树的最近公共祖先.md) -- [二叉树总结篇](./problems/二叉树总结篇.md) + +题目分类大纲如下: +二叉树大纲 + +1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md) +2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md) +3. [二叉树:二叉树的迭代遍历](./problems/二叉树的迭代遍历.md) +4. [二叉树:二叉树的统一迭代法](./problems/二叉树的统一迭代法.md) +5. [二叉树:二叉树的层序遍历](./problems/0102.二叉树的层序遍历.md) +6. [二叉树:226.翻转二叉树](./problems/0226.翻转二叉树.md) +7. [本周小结!(二叉树)](./problems/周总结/20200927二叉树周末总结.md) +8. [二叉树:101.对称二叉树](./problems/0101.对称二叉树.md) +9. [二叉树:104.二叉树的最大深度](./problems/0104.二叉树的最大深度.md) +10. [二叉树:111.二叉树的最小深度](./problems/0111.二叉树的最小深度.md) +11. [二叉树:222.完全二叉树的节点个数](./problems/0222.完全二叉树的节点个数.md) +12. [二叉树:110.平衡二叉树](./problems/0110.平衡二叉树.md) +13. [二叉树:257.二叉树的所有路径](./problems/0257.二叉树的所有路径.md) +14. [本周总结!(二叉树)](./problems/周总结/20201003二叉树周末总结.md) +16. [二叉树:404.左叶子之和](./problems/0404.左叶子之和.md) +17. [二叉树:513.找树左下角的值](./problems/0513.找树左下角的值.md) +18. [二叉树:112.路径总和](./problems/0112.路径总和.md) +19. [二叉树:106.构造二叉树](./problems/0106.从中序与后序遍历序列构造二叉树.md) +20. [二叉树:654.最大二叉树](./problems/0654.最大二叉树.md) +21. [本周小结!(二叉树)](./problems/周总结/20201010二叉树周末总结.md) +22. [二叉树:617.合并两个二叉树](./problems/0617.合并二叉树.md) +23. [二叉树:700.二叉搜索树登场!](./problems/0700.二叉搜索树中的搜索.md) +24. [二叉树:98.验证二叉搜索树](./problems/0098.验证二叉搜索树.md) +25. [二叉树:530.搜索树的最小绝对差](./problems/0530.二叉搜索树的最小绝对差.md) +26. [二叉树:501.二叉搜索树中的众数](./problems/0501.二叉搜索树中的众数.md) +27. [二叉树:236.公共祖先问题](./problems/0236.二叉树的最近公共祖先.md) +28. [本周小结!(二叉树)](./problems/周总结/20201017二叉树周末总结.md) +29. [二叉树:235.搜索树的最近公共祖先](./problems/0235.二叉搜索树的最近公共祖先.md) +30. [二叉树:701.搜索树中的插入操作](./problems/0701.二叉搜索树中的插入操作.md) +31. [二叉树:450.搜索树中的删除操作](./problems/0450.删除二叉搜索树中的节点.md) +32. [二叉树:669.修剪二叉搜索树](./problems/0669.修剪二叉搜索树.md) +33. [二叉树:108.将有序数组转换为二叉搜索树](./problems/0108.将有序数组转换为二叉搜索树.md) +34. [二叉树:538.把二叉搜索树转换为累加树](./problems/0538.把二叉搜索树转换为累加树.md) +35. [二叉树:总结篇!(需要掌握的二叉树技能都在这里了)](./problems/二叉树总结篇.md)
回溯算法 -- [回溯理论基础](./problems/回溯算法理论基础.md) -- [77. 组合 / 优化](./problems/0077.组合.md) -- [39/40. 组合总和(I/II)](./problems/0039.组合总和.md) -- [46/47. 全排列(I/II)](./problems/0046.全排列.md) -- [51. N 皇后 · 37. 解数独](./problems/0051.N皇后.md) -- [回溯总结篇](./problems/回溯总结.md) + +回溯算法大纲 + +1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md) +2. [回溯算法:77.组合](./problems/0077.组合.md) +3. [回溯算法:77.组合优化](./problems/0077.组合优化.md) +4. [回溯算法:216.组合总和III](./problems/0216.组合总和III.md) +5. [回溯算法:17.电话号码的字母组合](./problems/0017.电话号码的字母组合.md) +6. [本周小结!(回溯算法系列一)](./problems/周总结/20201030回溯周末总结.md) +7. [回溯算法:39.组合总和](./problems/0039.组合总和.md) +8. [回溯算法:40.组合总和II](./problems/0040.组合总和II.md) +9. [回溯算法:131.分割回文串](./problems/0131.分割回文串.md) +10. [回溯算法:93.复原IP地址](./problems/0093.复原IP地址.md) +11. [回溯算法:78.子集](./problems/0078.子集.md) +12. [本周小结!(回溯算法系列二)](./problems/周总结/20201107回溯周末总结.md) +13. [回溯算法:90.子集II](./problems/0090.子集II.md) +14. [回溯算法:491.递增子序列](./problems/0491.递增子序列.md) +15. [回溯算法:46.全排列](./problems/0046.全排列.md) +16. [回溯算法:47.全排列II](./problems/0047.全排列II.md) +17. [本周小结!(回溯算法系列三)](./problems/周总结/20201112回溯周末总结.md) +18. [回溯算法去重问题的另一种写法](./problems/回溯算法去重问题的另一种写法.md) +19. [回溯算法:332.重新安排行程](./problems/0332.重新安排行程.md) +20. [回溯算法:51.N皇后](./problems/0051.N皇后.md) +21. [回溯算法:37.解数独](./problems/0037.解数独.md) +22. [回溯算法总结篇](./problems/回溯总结.md)
贪心算法 -- [贪心理论基础](./problems/贪心算法理论基础.md) -- [455. 分发饼干](./problems/0455.分发饼干.md) -- [53. 最大子序和](./problems/0053.最大子序和.md) -- [55/45. 跳跃游戏(I/II)](./problems/0055.跳跃游戏.md) -- [134. 加油站 / 135. 分发糖果](./problems/0134.加油站.md) -- [贪心总结篇](./problems/贪心算法总结篇.md) + +贪心算法大纲 + +1. [关于贪心算法,你该了解这些!](./problems/贪心算法理论基础.md) +2. [贪心算法:455.分发饼干](./problems/0455.分发饼干.md) +3. [贪心算法:376.摆动序列](./problems/0376.摆动序列.md) +4. [贪心算法:53.最大子序和](./problems/0053.最大子序和.md) +5. [本周小结!(贪心算法系列一)](./problems/周总结/20201126贪心周末总结.md) +6. [贪心算法:122.买卖股票的最佳时机II](./problems/0122.买卖股票的最佳时机II.md) +7. [贪心算法:55.跳跃游戏](./problems/0055.跳跃游戏.md) +8. [贪心算法:45.跳跃游戏II](./problems/0045.跳跃游戏II.md) +9. [贪心算法:1005.K次取反后最大化的数组和](./problems/1005.K次取反后最大化的数组和.md) +10. [本周小结!(贪心算法系列二)](./problems/周总结/20201203贪心周末总结.md) +11. [贪心算法:134.加油站](./problems/0134.加油站.md) +12. [贪心算法:135.分发糖果](./problems/0135.分发糖果.md) +13. [贪心算法:860.柠檬水找零](./problems/0860.柠檬水找零.md) +14. [贪心算法:406.根据身高重建队列](./problems/0406.根据身高重建队列.md) +15. [本周小结!(贪心算法系列三)](./problems/周总结/20201217贪心周末总结.md) +16. [贪心算法:406.根据身高重建队列(续集)](./problems/根据身高重建队列(vector原理讲解).md) +17. [贪心算法:452.用最少数量的箭引爆气球](./problems/0452.用最少数量的箭引爆气球.md) +18. [贪心算法:435.无重叠区间](./problems/0435.无重叠区间.md) +19. [贪心算法:763.划分字母区间](./problems/0763.划分字母区间.md) +20. [贪心算法:56.合并区间](./problems/0056.合并区间.md) +21. [本周小结!(贪心算法系列四)](./problems/周总结/20201224贪心周末总结.md) +22. [贪心算法:738.单调递增的数字](./problems/0738.单调递增的数字.md) +23. [贪心算法:968.监控二叉树](./problems/0968.监控二叉树.md) +24. [贪心算法:总结篇!(每逢总结必经典)](./problems/贪心算法总结篇.md)
动态规划 -- [动规总览与思维方式](./problems/动态规划理论基础.md) -- 入门:509. 斐波那契 · 70. 爬楼梯 · 746. 使用最小花费爬楼梯 -- 经典:62/63. 不同路径 · 322. 零钱兑换 · 279. 完全平方数 -- 背包:01/完全/多重背包与模板、子集和、组合数 -- 股票:121/122/123/188/309/714 系列 -- 子序列:300 LIS、1143 LCS、72 编辑距离、516 最长回文子序列 -- [背包总结篇](./problems/背包总结篇.md) · [股票总结篇](./problems/动态规划-股票问题总结篇.md) · [动规总结篇](./problems/动态规划总结篇.md) +动态规划专题已经开始啦,来不及解释了,小伙伴们上车别掉队! + + +1. [关于动态规划,你该了解这些!](./problems/动态规划理论基础.md) +2. [动态规划:509.斐波那契数](./problems/0509.斐波那契数.md) +3. [动态规划:70.爬楼梯](./problems/0070.爬楼梯.md) +4. [动态规划:746.使用最小花费爬楼梯](./problems/0746.使用最小花费爬楼梯.md) +5. [本周小结!(动态规划系列一)](./problems/周总结/20210107动规周末总结.md) +6. [动态规划:62.不同路径](./problems/0062.不同路径.md) +7. [动态规划:63.不同路径II](./problems/0063.不同路径II.md) +8. [动态规划:343.整数拆分](./problems/0343.整数拆分.md) +9. [动态规划:96.不同的二叉搜索树](./problems/0096.不同的二叉搜索树.md) +10. [本周小结!(动态规划系列二)](./problems/周总结/20210114动规周末总结.md) + +背包问题系列: + +背包问题大纲 + + +11. [动态规划:01背包理论基础(二维dp数组)](./problems/背包理论基础01背包-1.md) +12. [动态规划:01背包理论基础(一维dp数组)](./problems/背包理论基础01背包-2.md) +13. [动态规划:416.分割等和子集](./problems/0416.分割等和子集.md) +14. [动态规划:1049.最后一块石头的重量II](./problems/1049.最后一块石头的重量II.md) +15. [本周小结!(动态规划系列三)](./problems/周总结/20210121动规周末总结.md) +16. [动态规划:494.目标和](./problems/0494.目标和.md) +17. [动态规划:474.一和零](./problems/0474.一和零.md) +18. [动态规划:完全背包理论基础(二维dp数组)](./problems/背包问题理论基础完全背包.md) +19. [动态规划:完全背包理论基础(一维dp数组)](./problems/背包问题完全背包一维.md) +20. [动态规划:518.零钱兑换II](./problems/0518.零钱兑换II.md) +21. [本周小结!(动态规划系列四)](./problems/周总结/20210128动规周末总结.md) +22. [动态规划:377.组合总和Ⅳ](./problems/0377.组合总和Ⅳ.md) +23. [动态规划:70.爬楼梯(完全背包版本)](./problems/0070.爬楼梯完全背包版本.md) +24. [动态规划:322.零钱兑换](./problems/0322.零钱兑换.md) +25. [动态规划:279.完全平方数](./problems/0279.完全平方数.md) +26. [本周小结!(动态规划系列五)](./problems/周总结/20210204动规周末总结.md) +27. [动态规划:139.单词拆分](./problems/0139.单词拆分.md) +28. [动态规划:多重背包理论基础](./problems/背包问题理论基础多重背包.md) +29. [背包问题总结篇](./problems/背包总结篇.md) + +打家劫舍系列: + +29. [动态规划:198.打家劫舍](./problems/0198.打家劫舍.md) +30. [动态规划:213.打家劫舍II](./problems/0213.打家劫舍II.md) +31. [动态规划:337.打家劫舍III](./problems/0337.打家劫舍III.md) + +股票系列: + +股票问题总结 + + +32. [动态规划:121.买卖股票的最佳时机](./problems/0121.买卖股票的最佳时机.md) +33. [动态规划:本周小结(系列六)](./problems/周总结/20210225动规周末总结.md) +34. [动态规划:122.买卖股票的最佳时机II](./problems/0122.买卖股票的最佳时机II(动态规划).md) +35. [动态规划:123.买卖股票的最佳时机III](./problems/0123.买卖股票的最佳时机III.md) +36. [动态规划:188.买卖股票的最佳时机IV](./problems/0188.买卖股票的最佳时机IV.md) +37. [动态规划:309.最佳买卖股票时机含冷冻期](./problems/0309.最佳买卖股票时机含冷冻期.md) +38. [动态规划:本周小结(系列七)](./problems/周总结/20210304动规周末总结.md) +39. [动态规划:714.买卖股票的最佳时机含手续费](./problems/0714.买卖股票的最佳时机含手续费(动态规划).md) +40. [动态规划:股票系列总结篇](./problems/动态规划-股票问题总结篇.md) + +子序列系列: + + + + +41. [动态规划:300.最长递增子序列](./problems/0300.最长上升子序列.md) +42. [动态规划:674.最长连续递增序列](./problems/0674.最长连续递增序列.md) +43. [动态规划:718.最长重复子数组](./problems/0718.最长重复子数组.md) +44. [动态规划:1143.最长公共子序列](./problems/1143.最长公共子序列.md) +45. [动态规划:1035.不相交的线](./problems/1035.不相交的线.md) +46. [动态规划:53.最大子序和](./problems/0053.最大子序和(动态规划).md) +47. [动态规划:392.判断子序列](./problems/0392.判断子序列.md) +48. [动态规划:115.不同的子序列](./problems/0115.不同的子序列.md) +49. [动态规划:583.两个字符串的删除操作](./problems/0583.两个字符串的删除操作.md) +50. [动态规划:72.编辑距离](./problems/0072.编辑距离.md) +51. [编辑距离总结篇](./problems/为了绝杀编辑距离,卡尔做了三步铺垫.md) +52. [动态规划:647.回文子串](./problems/0647.回文子串.md) +53. [动态规划:516.最长回文子序列](./problems/0516.最长回文子序列.md) +54. [动态规划总结篇](./problems/动态规划总结篇.md)
单调栈 -- [739. 每日温度](./problems/0739.每日温度.md) -- [496/503. 下一个更大元素 I/II](./problems/0496.下一个更大元素I.md) -- [42. 接雨水 · 84. 柱状图最大矩形](./problems/0042.接雨水.md) + +1. [单调栈:739.每日温度](./problems/0739.每日温度.md) +2. [单调栈:496.下一个更大元素I](./problems/0496.下一个更大元素I.md) +3. [单调栈:503.下一个更大元素II](./problems/0503.下一个更大元素II.md) +4. [单调栈:42.接雨水](./problems/0042.接雨水.md) +5. [单调栈:84.柱状图中最大的矩形](./problems/0084.柱状图中最大的矩形.md)
图论 -- [图论发布说明](./problems/qita/tulunfabu.md) -- 搜索:DFS / BFS、岛屿问题系列、可达性 -- 并查集:冗余连接 I/II、连通性判定 -- 最小生成树:Prim / Kruskal -- 最短路:Dijkstra(朴素/堆)· Bellman-Ford · SPFA · Floyd · A* -- [最短路总结篇](./problems/kamacoder/最短路问题总结篇.md) · [图论总结篇](./problems/kamacoder/图论总结篇.md) +**[图论正式发布](./problems/qita/tulunfabu.md)** + +1. [图论:理论基础](./problems/kamacoder/图论理论基础.md) +2. [图论:深度优先搜索理论基础](./problems/kamacoder/图论深搜理论基础.md) +3. [图论:所有可达路径](./problems/kamacoder/0098.所有可达路径.md) +4. [图论:广度优先搜索理论基础](./problems/kamacoder/图论广搜理论基础.md) +5. [图论:岛屿数量.深搜版](./problems/kamacoder/0099.岛屿的数量深搜.md) +6. [图论:岛屿数量.广搜版](./problems/kamacoder/0099.岛屿的数量广搜.md) +7. [图论:岛屿的最大面积](./problems/kamacoder/0100.岛屿的最大面积.md) +8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md) +9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md) +10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md) +11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md) +12. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md) +13. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md) +14. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md) +15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md) +16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md) +17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md) +18. [图论:冗余连接II](./problems/kamacoder/0109.冗余连接II.md) +19. [图论:最小生成树之prim](./problems/kamacoder/0053.寻宝-prim.md) +20. [图论:最小生成树之kruskal](./problems/kamacoder/0053.寻宝-Kruskal.md) +21. [图论:拓扑排序](./problems/kamacoder/0117.软件构建.md) +22. [图论:dijkstra(朴素版)](./problems/kamacoder/0047.参会dijkstra朴素.md) +23. [图论:dijkstra(堆优化版)](./problems/kamacoder/0047.参会dijkstra堆.md) +24. [图论:Bellman_ford 算法](./problems/kamacoder/0094.城市间货物运输I.md) +25. [图论:Bellman_ford 队列优化算法(又名SPFA)](./problems/kamacoder/0094.城市间货物运输I-SPFA.md) +26. [图论:Bellman_ford之判断负权回路](./problems/kamacoder/0095.城市间货物运输II.md) +27. [图论:Bellman_ford之单源有限最短路](./problems/kamacoder/0096.城市间货物运输III.md) +28. [图论:Floyd 算法](./problems/kamacoder/0097.小明逛公园.md) +29. [图论:A * 算法](./problems/kamacoder/0126.骑士的攻击astar.md) +30. [图论:最短路算法总结篇](./problems/kamacoder/最短路问题总结篇.md) +31. [图论:图论总结篇](./problems/kamacoder/图论总结篇.md) +
--- From 2e8268338b0f9925d7632a36339b5164ee82055c Mon Sep 17 00:00:00 2001 From: ch4r1ty <81227726+ch4r1ty@users.noreply.github.com> Date: Tue, 30 Sep 2025 21:01:52 -0400 Subject: [PATCH 1532/1533] =?UTF-8?q?nit.=20=E6=9B=B4=E6=96=B0=E4=BA=86pyt?= =?UTF-8?q?hon=E7=9A=84=E4=BB=A3=E7=A2=BC=E6=B2=92=E6=9C=89=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E7=9A=84=E5=95=8F=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前的python代碼沒有顔色,現在好了 --- ...\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" index 47ef1625bd..191d453fbc 100644 --- "a/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" +++ "b/problems/kamacoder/0047.\345\217\202\344\274\232dijkstra\346\234\264\347\264\240.md" @@ -806,7 +806,7 @@ public class Main { ### Python -``` +```Python import sys def dijkstra(n, m, edges, start, end): From ba343d21cdd66fee2f66917c74aabaad82afcc48 Mon Sep 17 00:00:00 2001 From: ch4r1ty <81227726+ch4r1ty@users.noreply.github.com> Date: Sun, 2 Nov 2025 13:04:02 -0500 Subject: [PATCH 1533/1533] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E2=80=9C?= =?UTF-8?q?=E9=87=8D=E8=B4=B4=E2=80=9D->=E2=80=9C=E9=87=8D=E5=8F=A0?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 4231a8ee90..4d089341e0 100755 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -48,7 +48,7 @@ 区间,1,2,3,4,5,6都按照右边界排好序。 -当确定区间 1 和 区间2 重叠后,如何确定是否与 区间3 也重贴呢? +当确定区间 1 和 区间2 重叠后,如何确定是否与 区间3 也重叠呢? 就是取 区间1 和 区间2 右边界的最小值,因为这个最小值之前的部分一定是 区间1 和区间2 的重合部分,如果这个最小值也触达到区间3,那么说明 区间 1,2,3都是重合的。

- +