mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0718.最长重复子数组.md
This commit is contained in:
@ -158,6 +158,36 @@ Java:
|
||||
|
||||
Python:
|
||||
|
||||
> 动态规划:
|
||||
```python
|
||||
class Solution:
|
||||
def findLength(self, A: List[int], B: List[int]) -> int:
|
||||
dp = [[0] * (len(B)+1) for _ in range(len(A)+1)]
|
||||
result = 0
|
||||
for i in range(1, len(A)+1):
|
||||
for j in range(1, len(B)+1):
|
||||
if A[i-1] == B[j-1]:
|
||||
dp[i][j] = dp[i-1][j-1] + 1
|
||||
result = max(result, dp[i][j])
|
||||
return result
|
||||
```
|
||||
|
||||
> 动态规划:滚动数组
|
||||
```python
|
||||
class Solution:
|
||||
def findLength(self, A: List[int], B: List[int]) -> int:
|
||||
dp = [0] * (len(B) + 1)
|
||||
result = 0
|
||||
for i in range(1, len(A)+1):
|
||||
for j in range(len(B), 0, -1):
|
||||
if A[i-1] == B[j-1]:
|
||||
dp[j] = dp[j-1] + 1
|
||||
else:
|
||||
dp[j] = 0 #注意这里不相等的时候要有赋0的操作
|
||||
result = max(result, dp[j])
|
||||
return result
|
||||
```
|
||||
|
||||
|
||||
Go:
|
||||
```Go
|
||||
|
Reference in New Issue
Block a user