最长重复子数组

方法:动态规划
作者:LeetCode 链接:https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/solution/zui-chang-zhong-fu-zi-shu-zu-by-leetcode/ 来源:力扣(LeetCode)
设 dp[i][j]
为 A[i:]
和 B[j:]
的最长公共前缀,那么答案为所有 dp[i][j]
中的最大值
max(dp[i][j])
。若 A[i] == B[j]
,状态转移方程为
dp[i][j] = dp[i + 1][j + 1] + 1
,否则为
dp[i][j] = 0
。
复杂度分析
时间复杂度:O(M*N),其中 M和 N是数组 A 和 B 的长度。
空间复杂度:O(M*N),即为数组 dp
使用的空间。
代码
1 | class Solution { |
