Topic link

Leetcode-cn.com/problems/ma…

reference

www.bilibili.com/video/BV1eC…

Their thinking

Dp matrix:

// nums1[I] === nums2[k], So dp[I][k] is its upper left hand value + 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 2 1 0 0 0 1 3 2 0 1 1 0 0 0 1 1 0 0 0 1 1 1 0 0 0 1Copy the code

The problem solving code

Var findLength = function(nums1, nums2) {var findLength = function(nums1, nums2) { Do not use const dp = Array(nums1.length).fill(() => Array(nums2.length).fill(0)), because the Array num2 is generated with the same reference. const dp = Array.from(Array(nums1.length), () => Array(nums2.length).fill(0)); let ret = 0; for (let i = 0; i < nums1.length; i++) { for (let k = 0; k < nums2.length; K++) {/ / if equal to deal with so can the if (nums1 [I] = = = nums2 [k]) {if (dp && dp [I - 1] [I - 1]] [k - 1) {dp [I] [k] = dp [I - 1] + [k - 1] 1; } else { dp[i][k] = 1; } ret = Math.max(dp[i][k], ret) } } } return ret; };Copy the code