From 4d6b8252a1d51b0cef64e5b4739ef21329bdd41c Mon Sep 17 00:00:00 2001 From: chenzhg <2216468566@qq.com> Date: Thu, 29 Sep 2022 17:31:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=20C++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0583.两个字符串的删除操作.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index fd80853e..c33f7f58 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -47,6 +47,8 @@ dp[i][j]:以i-1为结尾的字符串word1,和以j-1位结尾的字符串word 那最后当然是取最小值,所以当word1[i - 1] 与 word2[j - 1]不相同的时候,递推公式:dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1}); +因为dp[i - 1][j - 1] + 1等于 dp[i - 1][j] 或 dp[i][j - 1],所以递推公式可简化为:dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1); + 3. dp数组如何初始化 @@ -90,7 +92,7 @@ public: if (word1[i - 1] == word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { - dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1}); + dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1); } } } From e4ca3482fbb5202172c6268a10565dfbf116f6fc Mon Sep 17 00:00:00 2001 From: chenzhg <2216468566@qq.com> Date: Sat, 1 Oct 2022 16:25:03 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB=20=E6=B7=BB=E5=8A=A0C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0072.编辑距离.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index e40461de..a15012d7 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -362,6 +362,33 @@ function minDistance(word1: string, word2: string): number { }; ``` +C: + +```c +int min(int num1, int num2, int num3) { + return num1 > num2 ? (num2 > num3 ? num3 : num2) : (num1 > num3 ? num3 : num1); +} + +int minDistance(char * word1, char * word2){ + int dp[strlen(word1)+1][strlen(word2)+1]; + dp[0][0] = 0; + for (int i = 1; i <= strlen(word1); i++) dp[i][0] = i; + for (int i = 1; i <= strlen(word2); i++) dp[0][i] = i; + + for (int i = 1; i <= strlen(word1); i++) { + for (int j = 1; j <=strlen(word2); j++) { + if (word1[i-1] == word2[j-1]) { + dp[i][j] = dp[i-1][j-1]; + } + else { + dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1; + } + } + } + return dp[strlen(word1)][strlen(word2)]; +} +``` + -----------------------