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 4eefc54

Browse files
添加(0115.不同的子序列.md):增加typescript版本
1 parent aafc18e commit 4eefc54

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎problems/0115.不同的子序列.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,36 @@ const numDistinct = (s, t) => {
267267
};
268268
```
269269

270+
TypeScript:
271+
272+
```typescript
273+
function numDistinct(s: string, t: string): number {
274+
/**
275+
dp[i][j]: s前i个字符,t前j个字符,s子序列中t出现的个数
276+
dp[0][0]=1, 表示s前0个字符为'',t前0个字符为''
277+
*/
278+
const sLen: number = s.length,
279+
tLen: number = t.length;
280+
const dp: number[][] = new Array(sLen + 1).fill(0)
281+
.map(_ => new Array(tLen + 1).fill(0));
282+
for (let m = 0; m < sLen; m++) {
283+
dp[m][0] = 1;
284+
}
285+
for (let i = 1; i <= sLen; i++) {
286+
for (let j = 1; j <= tLen; j++) {
287+
if (s[i - 1] === t[j - 1]) {
288+
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
289+
} else {
290+
dp[i][j] = dp[i - 1][j];
291+
}
292+
}
293+
}
294+
return dp[sLen][tLen];
295+
};
296+
```
297+
298+
299+
270300

271301
-----------------------
272302
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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