From 0dad9a5f7cd13b68b9a727983490bea2a5eaa992 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Tue, 13 Sep 2022 22:21:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200139.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8B=86=E5=88=86.md=20Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用Set容器加速两层for循环中的查询函数,原代码在List列表中查询,本质上为三层循环,使用Set容器后为两层循环 --- problems/0139.单词拆分.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 7ff13f72..5b38a7a5 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -234,11 +234,13 @@ Java: ```java class Solution { public boolean wordBreak(String s, List wordDict) { + HashSet set = new HashSet<>(wordDict); boolean[] valid = new boolean[s.length() + 1]; valid[0] = true; + for (int i = 1; i <= s.length(); i++) { - for (int j = 0; j < i; j++) { - if (wordDict.contains(s.substring(j,i)) && valid[j]) { + for (int j = 0; j < i && !valid[i]; j++) { + if (set.contains(s.substring(j, i)) && valid[j]) { valid[i] = true; } } From 7e3823d619a5ec36beb29e6d2a1c573bc0c7cd00 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Tue, 13 Sep 2022 22:24:58 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200139.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8B=86=E5=88=86.md=20Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加另一种思路的背包算法,比题解中的背包算法速度更快 --- problems/0139.单词拆分.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 5b38a7a5..3d4cdb31 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -250,6 +250,26 @@ class Solution { } } +// 另一种思路的背包算法 +class Solution { + public boolean wordBreak(String s, List wordDict) { + boolean[] dp = new boolean[s.length() + 1]; + dp[0] = true; + + for (int i = 1; i <= s.length(); i++) { + for (String word : wordDict) { + int len = word.length(); + if (i >= len && dp[i - len] && word.equals(s.substring(i - len, i))) { + dp[i] = true; + break; + } + } + } + + return dp[s.length()]; + } +} + // 回溯法+记忆化 class Solution { private Set set; From cf51b7c0397e140e68ed4dd3d5f98f6feab98e47 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Tue, 13 Sep 2022 22:27:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200139.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8B=86=E5=88=86.md=20Python=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=AB=98=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0139.单词拆分.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 3d4cdb31..d60fd46c 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -307,7 +307,7 @@ class Solution { Python: -```python3 +```python class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: '''排列'''