mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
update: 优化 0392.判断子序列 typescript 代码逻辑
This commit is contained in:
@ -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:
|
||||
|
Reference in New Issue
Block a user