mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1361 from xiaofei-2020/dp45
添加(1035.不相交的线.md):增加typescript版本
This commit is contained in:
@ -183,6 +183,30 @@ const maxUncrossedLines = (nums1, nums2) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function maxUncrossedLines(nums1: number[], nums2: number[]): number {
|
||||
/**
|
||||
dp[i][j]: nums1前i-1个,nums2前j-1个,最大连线数
|
||||
*/
|
||||
const length1: number = nums1.length,
|
||||
length2: number = nums2.length;
|
||||
const dp: number[][] = new Array(length1 + 1).fill(0)
|
||||
.map(_ => new Array(length2 + 1).fill(0));
|
||||
for (let i = 1; i <= length1; i++) {
|
||||
for (let j = 1; j <= length2; j++) {
|
||||
if (nums1[i - 1] === nums2[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[length1][length2];
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user