diff --git a/codes/javascript/chapter_dynamic_programming/edit_distance.js b/codes/javascript/chapter_dynamic_programming/edit_distance.js index a8dd36558..5af39b8d1 100644 --- a/codes/javascript/chapter_dynamic_programming/edit_distance.js +++ b/codes/javascript/chapter_dynamic_programming/edit_distance.js @@ -24,7 +24,7 @@ function editDistanceDFS(s, t, i, j) { const del = editDistanceDFS(s, t, i - 1, j); const replace = editDistanceDFS(s, t, i - 1, j - 1); // 返回最少编辑步数 - return Math.min(Math.min(insert, del), replace) + 1; + return Math.min(insert, del, replace) + 1; } /* 编辑距离:记忆化搜索 */ @@ -50,7 +50,7 @@ function editDistanceDFSMem(s, t, mem, i, j) { const del = editDistanceDFSMem(s, t, mem, i - 1, j); const replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1); // 记录并返回最少编辑步数 - mem[i][j] = Math.min(Math.min(insert, del), replace) + 1; + mem[i][j] = Math.min(insert, del, replace) + 1; return mem[i][j]; } @@ -74,11 +74,7 @@ function editDistanceDP(s, t) { dp[i][j] = dp[i - 1][j - 1]; } else { // 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 - dp[i][j] = - Math.min( - Math.min(dp[i][j - 1], dp[i - 1][j]), - dp[i - 1][j - 1] - ) + 1; + dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1 } } } @@ -107,7 +103,7 @@ function editDistanceDPComp(s, t) { dp[j] = leftup; } else { // 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 - dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1; + dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1; } leftup = temp; // 更新为下一轮的 dp[i-1, j-1] } diff --git a/codes/typescript/chapter_dynamic_programming/edit_distance.ts b/codes/typescript/chapter_dynamic_programming/edit_distance.ts index ef91b13a5..a5bdcb2ec 100644 --- a/codes/typescript/chapter_dynamic_programming/edit_distance.ts +++ b/codes/typescript/chapter_dynamic_programming/edit_distance.ts @@ -24,7 +24,7 @@ function editDistanceDFS(s: string, t: string, i: number, j: number): number { const del = editDistanceDFS(s, t, i - 1, j); const replace = editDistanceDFS(s, t, i - 1, j - 1); // 返回最少编辑步数 - return Math.min(Math.min(insert, del), replace) + 1; + return Math.min(insert, del, replace) + 1; } /* 编辑距离:记忆化搜索 */ @@ -56,7 +56,7 @@ function editDistanceDFSMem( const del = editDistanceDFSMem(s, t, mem, i - 1, j); const replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1); // 记录并返回最少编辑步数 - mem[i][j] = Math.min(Math.min(insert, del), replace) + 1; + mem[i][j] = Math.min(insert, del, replace) + 1; return mem[i][j]; } @@ -82,11 +82,7 @@ function editDistanceDP(s: string, t: string): number { dp[i][j] = dp[i - 1][j - 1]; } else { // 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 - dp[i][j] = - Math.min( - Math.min(dp[i][j - 1], dp[i - 1][j]), - dp[i - 1][j - 1] - ) + 1; + dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1 } } } @@ -115,7 +111,7 @@ function editDistanceDPComp(s: string, t: string): number { dp[j] = leftup; } else { // 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 - dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1; + dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1; } leftup = temp; // 更新为下一轮的 dp[i-1, j-1] }