update: 优化 0392.判断子序列 typescript 代码逻辑

This commit is contained in:
xin
2023-10-29 17:40:58 +08:00
parent ddaba36a12
commit ef1c72a36b

View File

@ -221,21 +221,19 @@ function isSubsequence(s: string, t: string): boolean {
/**
dp[i][j]: s的前i-1个t的前j-1个最长公共子序列的长度
*/
const sLen: number = s.length,
tLen: number = t.length;
const dp: number[][] = new Array(sLen + 1).fill(0)
.map(_ => new Array(tLen + 1).fill(0));
const sLen = s.length
const tLen = t.length
const dp: number[][] = new Array(sLen + 1).fill(0).map(_ => new Array(tLen + 1).fill(0))
for (let i = 1; i <= sLen; i++) {
for (let j = 1; j <= tLen; j++) {
if (s[i - 1] === t[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]);
}
if (s[i - 1] === t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1
// 只需要取 j-2 的 dp 值即可,不用考虑 i-2
else dp[i][j] = dp[i][j - 1]
}
}
return dp[sLen][tLen] === s.length;
};
return dp[sLen][tLen] === s.length
}
```
### Go