From 2bd9b0280b9618236d37b74c3b7dd71884ba15b7 Mon Sep 17 00:00:00 2001 From: Jerry-306 <82520819+Jerry-306@users.noreply.github.com> Date: Fri, 17 Sep 2021 20:01:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200718=20=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=95=B0=E7=BB=84=20JavaScript=20=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0718.最长重复子数组.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index 9e3da663..712b5eeb 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -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; +} +``` -----------------------