[leetcode] 718. Maximum Length of Repeated Subarray

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.

Example 1:

Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1].

Note:

  1. 1 <= len(A), len(B) <= 1000
  2. 0 <= A[i], B[i] < 100


从A串的结尾开始向前遍历,记当前pos为i。

对每一个i,从B串的开始开始遍历,记当前pos为j,用dp[j]表示以i位置开头的A子串和以j位置开头的B子串最长重复串的长度。

则在对B遍历时,若当前pos为j,则当前的dp[j+1]是上一次的结果,即以i+1位置开头的A子串和以j+1位置开头的B子串最长重复串的长度; 这时,若A[i]与B[j]相等,则dp[j] = dp[j+1] + 1,否则为0。

上述也是表示了为什么要从B串的开头开始循环:为了在计算pos j 时, dp[j+1]记录的时pos i 的数据。也即若A[i]与B[j]相等, dp[i][j] = dp[i+1][j+1] + 1。

考虑到边界情况,dp数组申请长度为B长度+1。

我的代码:

class Solution {
public:
    int findLength(vector<int>& A, vector<int>& B) {
        int len1= A.size(), len2 = B.size();
        if (!len1 || !len2) return 0;
        vector<int> dp(len2+1);
        int re = 0;
        for (int i = len1 - 1; i >= 0; i--) {
            for (int j = 0; j < len2; j++) {
                dp[j] = (A[i] == B[j])?(1 + dp[j+1]):0;
                re = max(re, dp[j]);
            }
        }
        return re;
    }
};
时间: 2024-08-02 10:47:51

[leetcode] 718. Maximum Length of Repeated Subarray的相关文章

718. Maximum Length of Repeated Subarray

#week9 Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= l

LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. LeetCode718. Maximum Length of Repeated Subarray 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连

[LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= len(A),

[Swift]LeetCode718. 最长重复子数组 | Maximum Length of Repeated Subarray

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1].  Note: 1 <= len(A),

[LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1]

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

Leetcode 动态规划 Maximum Subarray

正整数或一位小数或者俩位小数的正则表达式的写法 ^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$ Leetcode 动态规划 Maximum Subarray,布布扣,bubuko.com

LeetCode之Maximum Subarray

Maximum Subarray的通过率居然这么高,我提交了几次都是Wrong Answer,郁闷! Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has t

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]