mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(0392.判断子序列.md):增加typescript版本
This commit is contained in:
@ -201,7 +201,32 @@ const isSubsequence = (s, t) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
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));
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[sLen][tLen] === s.length;
|
||||
};
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
func isSubsequence(s string, t string) bool {
|
||||
dp := make([][]int,len(s)+1)
|
||||
|
Reference in New Issue
Block a user