From 7a4057066a52a68b60e1c19670a5f21b5ffa716c Mon Sep 17 00:00:00 2001 From: StriveDD Date: Thu, 2 Mar 2023 11:15:07 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0841.=E9=92=A5=E5=8C=99?= =?UTF-8?q?=E5=92=8C=E6=88=BF=E9=97=B4=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84BFS=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0841.钥匙和房间.md | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0841.钥匙和房间.md b/problems/0841.钥匙和房间.md index a973ab56..00263cac 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.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 02/18] =?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 --- problems/0127.单词接龙.md | 67 ++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 7ffd6a21..b4078913 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.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 03/18] =?UTF-8?q?=E5=A2=9E=E5=8A=A00827.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=BA=E5=B7=A5=E5=B2=9B=E7=9A=84Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0827.最大人工岛.md | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/problems/0827.最大人工岛.md b/problems/0827.最大人工岛.md index 1b49e9ee..45b2ef5a 100644 --- a/problems/0827.最大人工岛.md +++ b/problems/0827.最大人工岛.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 04/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A00127.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8E=A5=E9=BE=99=E7=9A=84Java=E7=89=88=E6=9C=AC=E5=8F=8C?= =?UTF-8?q?=E5=90=91BFS=E4=BB=A3=E7=A0=81=EF=BC=8C0827.=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E4=BA=BA=E5=B7=A5=E5=B2=9B=E7=9A=84Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C0841.=E9=92=A5=E5=8C=99=E5=92=8C=E6=88=BF=E9=97=B4?= =?UTF-8?q?=E7=9A=84Java=E7=89=88=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 --- problems/0127.单词接龙.md | 3 +-- problems/0841.钥匙和房间.md | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index b4078913..20ad5182 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.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.钥匙和房间.md b/problems/0841.钥匙和房间.md index 00263cac..faf2b97f 100644 --- a/problems/0841.钥匙和房间.md +++ b/problems/0841.钥匙和房间.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 54847a181cfd59d8e938aa619daf72307a50ac39 Mon Sep 17 00:00:00 2001 From: GODVvVZzz <2662446324@qq.com> Date: Fri, 3 Mar 2023 10:57:00 +0800 Subject: [PATCH 05/18] =?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 --- problems/周总结/20210114动规周末总结.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/周总结/20210114动规周末总结.md b/problems/周总结/20210114动规周末总结.md index 039f3596..f47925fe 100644 --- a/problems/周总结/20210114动规周末总结.md +++ b/problems/周总结/20210114动规周末总结.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 06/18] =?UTF-8?q?update=200416.=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86=EF=BC=9A=E4=BF=AE=E6=94=B9=20python?= =?UTF-8?q?=20=E4=BB=A3=E7=A0=81=E4=B8=AD=E4=B8=80=E7=BB=B4=E8=83=8C?= =?UTF-8?q?=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 --- problems/0416.分割等和子集.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 45dd289a..dfb327ec 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.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 07/18] =?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 --- problems/0349.两个数组的交集.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index ed6a5d97..347d1094 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.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 08/18] =?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 --- ...溯算法去重问题的另一种写法.md | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index 73862156..2265a89b 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.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 09/18] =?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 --- ...溯算法去重问题的另一种写法.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index 2265a89b..08ba64dc 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.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 10/18] =?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 --- ...溯算法去重问题的另一种写法.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index 08ba64dc..e60bd44a 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.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 11/18] =?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 --- problems/0416.分割等和子集.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 45dd289a..dfb327ec 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.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 12/18] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN=E4=B8=AA?= =?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 修改python版本为:fast先走n+1步 (旧版本是fast走n步,然后判断fast.next!=None),这样与上面的讲解一致 --- problems/0019.删除链表的倒数第N个节点.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index b0641b5f..4e4474ca 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.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 13/18] =?UTF-8?q?modified=20ACM=E4=B8=8B=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=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 --- problems/前序/ACM模式如何构建二叉树.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md index 42bf7af0..b6446403 100644 --- a/problems/前序/ACM模式如何构建二叉树.md +++ b/problems/前序/ACM模式如何构建二叉树.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 14/18] =?UTF-8?q?Update=200455.=E5=88=86=E5=8F=91=E9=A5=BC?= =?UTF-8?q?=E5=B9=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 2437582c..63525b03 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.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 15/18] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index efb9c6b6..d4daccc5 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.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 16/18] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index d4daccc5..0199d83b 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.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 77eeed96bcfb0375e09d1a8f2d4e7a6afe9cd615 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 9 Mar 2023 00:13:05 +0800 Subject: [PATCH 17/18] =?UTF-8?q?update=200704.=E4=BA=8C=E5=88=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE:=20=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/0704.二分查找.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index a02bf7a2..2d5525ba 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.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 18/18] =?UTF-8?q?update=200059.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5II=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/0059.螺旋矩阵II.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index d9a656b9..b4dad9c3 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -117,6 +117,9 @@ public: }; ``` +* 时间复杂度 O(n^2): 模拟遍历二维矩阵的时间 +* 空间复杂度 O(1) + ## 类似题目 * 54.螺旋矩阵