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 568a569

Browse files
添加(1143.最长公共子序列.md):增加typescript版本
1 parent aafc18e commit 568a569

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎problems/1143.最长公共子序列.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ const longestCommonSubsequence = (text1, text2) => {
236236
};
237237
```
238238

239+
TypeScript:
240+
241+
```typescript
242+
function longestCommonSubsequence(text1: string, text2: string): number {
243+
/**
244+
dp[i][j]: text1中前i-1个和text2中前j-1个,最长公共子序列的长度
245+
*/
246+
const length1: number = text1.length,
247+
length2: number = text2.length;
248+
const dp: number[][] = new Array(length1 + 1).fill(0)
249+
.map(_ => new Array(length2 + 1).fill(0));
250+
for (let i = 1; i <= length1; i++) {
251+
for (let j = 1; j <= length2; j++) {
252+
if (text1[i - 1] === text2[j - 1]) {
253+
dp[i][j] = dp[i - 1][j - 1] + 1;
254+
} else {
255+
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
256+
}
257+
}
258+
}
259+
return dp[length1][length2];
260+
};
261+
```
262+
263+
264+
239265

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

0 commit comments

Comments
(0)

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