From 00aed2a72b6713b937c0379988a7e97d8383e329 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Tue, 20 Sep 2022 20:46:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=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=20Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加另一种动态规划思路 --- .../0583.两个字符串的删除操作.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index 36c175c3..fd80853e 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -128,6 +128,28 @@ public: Java: ```java +// dp数组中存储word1和word2最长相同子序列的长度 +class Solution { + 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 - 1][j], dp[i][j - 1]); + } + } + } + + return len1 + len2 - dp[len1][len2] * 2; + } +} + +// dp数组中存储需要删除的字符个数 class Solution { public int minDistance(String word1, String word2) { int[][] dp = new int[word1.length() + 1][word2.length() + 1];