题目
给出两个整数数组A和B,找出A和B的最长子数组,输出最长子数组的长度。
Python题解
class Solution(object):
def findLength(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
m, n = len(A), len(B)
memo = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
ans = 0
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
if A[i] == B[j]:
memo[i][j] = memo[i + 1][j + 1] + 1
ans = max(ans, memo[i][j])
return ans