mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 09:36:28 +08:00
refactor(JS, TS): Improve code readability in edit_distance (#860)
* fix(JS、TS): chapter_sorting QuickSortMedian and QuickSortTailCall example * refactor(JS、TS): Improve code readability in edit_distance * Updated code
This commit is contained in:
parent
d27890d98d
commit
88a1e4aaac
2 changed files with 8 additions and 16 deletions
|
@ -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]
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue