From 8ac7cdce22a6f350ec57627e474bd482f5d041b3 Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:01:36 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII.md=EF=BC=89=EF=BC=9A=E8=A1=A5?= =?UTF-8?q?=E5=85=85Java=E7=89=88=E6=9C=AC2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index a39c064a..7a3f048c 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -142,6 +142,7 @@ public: ### Java ```Java +// 版本一 class Solution { public int jump(int[] nums) { if (nums == null || nums.length == 0 || nums.length == 1) { @@ -172,7 +173,30 @@ class Solution { } ``` +```java +// 版本二 +class Solution { + public int jump(int[] nums) { + int result = 0; + // 当前覆盖的最远距离下标 + int end = 0; + // 下一步覆盖的最远距离下标 + int temp = 0; + for (int i = 0; i <= end && end < nums.length - 1; ++i) { + temp = Math.max(temp, i + nums[i]); + // 可达位置的改变次数就是跳跃次数 + if (i == end) { + end = temp; + result++; + } + } + return result; + } +} +``` + ### Python + ```python class Solution: def jump(self, nums: List[int]) -> int: From 40c06155f9e65e3525100ac6aff6f3ce6aeb8dac Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:04:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880139.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8B=86=E5=88=86.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Java=E5=9B=9E=E5=9B=9E=E6=BA=AF+=E8=AE=B0=E5=BF=86?= =?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/0139.单词拆分.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index e24ffc1a..1653a81a 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -248,6 +248,36 @@ class Solution { return valid[s.length()]; } } + +// 回溯法+记忆化 +class Solution { + public boolean wordBreak(String s, List wordDict) { + Set wordDictSet = new HashSet(wordDict); + int[] memory = new int[s.length()]; + return backTrack(s, wordDictSet, 0, memory); + } + + public boolean backTrack(String s, Set wordDictSet, int startIndex, int[] memory) { + // 结束条件 + if (startIndex >= s.length()) { + return true; + } + if (memory[startIndex] != 0) { + // 此处认为:memory[i] = 1 表示可以拼出i 及以后的字符子串, memory[i] = -1 表示不能 + return memory[startIndex] == 1 ? true : false; + } + for (int i = startIndex; i < s.length(); ++i) { + // 处理 递归 回溯 循环不变量:[startIndex, i + 1) + String word = s.substring(startIndex, i + 1); + if (wordDictSet.contains(word) && backTrack(s, wordDictSet, i + 1, memory)) { + memory[startIndex] = 1; + return true; + } + } + memory[startIndex] = -1; + return false; + } +} ``` Python: From 5a2ff02b94d417a7a51d44998e212913afeba48b Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:08:56 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=EF=BC=89=EF=BC=9A=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=86=E9=80=BB=E8=BE=91=E5=B0=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 2bc23182..d75311eb 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -174,7 +174,7 @@ public: ```Java class Solution { public int wiggleMaxLength(int[] nums) { - if (nums == null || nums.length <= 1) { + if (nums.length <= 1) { return nums.length; } //当前差值