Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8759349

Browse files
添加(0392.判断子序列.md):增加typescript版本
1 parent aafc18e commit 8759349

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/0392.判断子序列.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,32 @@ const isSubsequence = (s, t) => {
201201
};
202202
```
203203

204+
TypeScript:
205+
206+
```typescript
207+
function isSubsequence(s: string, t: string): boolean {
208+
/**
209+
dp[i][j]: s的前i-1个,t的前j-1个,最长公共子序列的长度
210+
*/
211+
const sLen: number = s.length,
212+
tLen: number = t.length;
213+
const dp: number[][] = new Array(sLen + 1).fill(0)
214+
.map(_ => new Array(tLen + 1).fill(0));
215+
for (let i = 1; i <= sLen; i++) {
216+
for (let j = 1; j <= tLen; j++) {
217+
if (s[i - 1] === t[j - 1]) {
218+
dp[i][j] = dp[i - 1][j - 1] + 1;
219+
} else {
220+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
221+
}
222+
}
223+
}
224+
return dp[sLen][tLen] === s.length;
225+
};
226+
```
227+
204228
Go:
229+
205230
```go
206231
func isSubsequence(s string, t string) bool {
207232
dp := make([][]int,len(s)+1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /