mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
添加 0718 最长重复数组 JavaScript 滚动数组解法
This commit is contained in:
@ -278,7 +278,26 @@ const findLength = (A, B) => {
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
> 滚动数组
|
||||
```javascript
|
||||
const findLength = (nums1, nums2) => {
|
||||
let len1 = nums1.length, len2 = nums2.length;
|
||||
// dp[i][j]: 以nums1[i-1]、nums2[j-1]为结尾的最长公共子数组的长度
|
||||
let dp = new Array(len2+1).fill(0);
|
||||
let res = 0;
|
||||
for (let i = 1; i <= len1; i++) {
|
||||
for (let j = len2; j > 0; j--) {
|
||||
if (nums1[i-1] === nums2[j-1]) {
|
||||
dp[j] = dp[j-1] + 1;
|
||||
} else {
|
||||
dp[j] = 0;
|
||||
}
|
||||
res = Math.max(res, dp[j]);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user