Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

最长重复子数组

Sunny-117 opened this issue · comments

commented
最长重复子数组
var findLength = function(nums1, nums2) {
    const n = nums1.length;
    const m = nums2.length;
    const dp = new Array(n+1).fill(0).map(() => new Array(m+1).fill(0));
    let res = 0;

    for(let i = 1; i <= n; i++){
        for(let j = 1; j <= m; j++){
            if(nums1[i-1] === nums2[j - 1]){
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            res = Math.max(dp[i][j],res)
        }
    }
    return res
};