阅读 73

LeetCode第36天🐱‍🏍

718. 最长重复子数组

dp[i][j] = dp[i - 1][j - 1] + 1

const findLength = (nums1, nums2) => {
    const m = nums1.length;
    const n = nums2.length;
    let res = 0;
    const dp = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0))

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

160. 相交链表

var getIntersectionNode = function(headA, headB) {
    if (headA === null || headB === null) return null
    let pA = headA, pB = headB
    while (pA != pB) {
        pA = pA === null ? headB : pA.next
        pB = pB === null ? headA : pB.next
    }
    return pA
};
复制代码
文章分类
前端