From 0ee27307e8338569a2919f136c7f11a02a50a050 Mon Sep 17 00:00:00 2001 From: triangold <10292799+triangold@user.noreply.gitee.com> Date: Thu, 6 Jan 2022 16:24:10 +0000 Subject: [PATCH 1/3] =?UTF-8?q?update=20problems/0583.=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.=20=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=80=E6=AE=B5ja?= =?UTF-8?q?va=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=8F=90=E4=BE=9B=E4=BA=86?= =?UTF-8?q?=E5=8F=A6=E4=B8=80=E7=A7=8D=E8=A7=A3=E9=A2=98=E6=80=9D=E8=B7=AF?= =?UTF-8?q?=EF=BC=9A=E5=85=88=E6=B1=82=E5=87=BAword1=E5=92=8Cword2?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E9=95=BF=E5=BA=A6len=EF=BC=8C=E7=84=B6=E5=90=8E?= =?UTF-8?q?=EF=BC=88word1-len=EF=BC=89+=EF=BC=88word2-len=EF=BC=89?= =?UTF-8?q?=E5=B0=B1=E6=98=AF=E8=A6=81=E5=88=A0=E9=99=A4=E7=9A=84=E6=9C=80?= =?UTF-8?q?=E5=B0=91=E6=AC=A1=E6=95=B0=E4=BA=86?= 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, 26 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 d2f7d84b..2bf0cdc4 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" @@ -125,6 +125,32 @@ class Solution { } ``` +```java +class Solution { + + //换了一种思路:先求出word1和word2的最大公共子序列长度len,然后(word1-len)+(word2-len)就是要删除的最少次数了 + public int minDistance(String word1, String word2) { + + int len1 = word1.length(); + int len2 = word2.length(); + + int[][] dp = new int[len1+1][len2+1]; + + + for(int i = 1; i <= len1; i++) { + for(int j = 1; j < len2; j++) { + if(word1.charAt(i-1) == word2.charAt(j-1)) { + dp[i][j] = dp[i-1][j-1] + 1; + }else { + dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]); + } + } + } + + return len1 + len2 - dp[len1][len2] * 2; + } +} +``` Python: ```python -- Gitee From 5632e44f7dfc0ee77741a21b98cdf8f61948eaf8 Mon Sep 17 00:00:00 2001 From: triangold <10292799+triangold@user.noreply.gitee.com> Date: Sat, 8 Jan 2022 05:53:11 +0000 Subject: [PATCH 2/3] =?UTF-8?q?update=20problems/0028.=E5=AE=9E=E7=8E=B0st?= =?UTF-8?q?rStr.md.=20=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE=E6=94=B9?= 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 ef7008bc..65ed165d 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -199,7 +199,7 @@ next数组就是一个前缀表(prefix table)。 所以要看前一位的 前缀表的数值。 -前一个字符的前缀表的数值是2, 所有把下标移动到下标2的位置继续比配。 可以再反复看一下上面的动画。 +前一个字符的前缀表的数值是2, 所以把下标移动到下标2的位置继续匹配。 可以再反复看一下上面的动画。 最后就在文本串中找到了和模式串匹配的子串了。 -- Gitee From 7fce678d7df48217684451213192176183d3f86c Mon Sep 17 00:00:00 2001 From: triangold <10292799+triangold@user.noreply.gitee.com> Date: Sat, 8 Jan 2022 06:00:51 +0000 Subject: [PATCH 3/3] =?UTF-8?q?update=20problems/0028.=E5=AE=9E=E7=8E=B0st?= =?UTF-8?q?rStr.md.=20=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 --- "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 65ed165d..42cf9b21 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -205,7 +205,7 @@ next数组就是一个前缀表(prefix table)。 # 前缀表与next数组 -很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? +很多KMP算法的实现都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? next数组就可以是前缀表,但是很多实现都是把前缀表统一减一(右移一位,初始位置为-1)之后作为next数组。 -- Gitee