Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8fb1697

Browse files
Update 0718.最长重复子数组.md
1 parent c652187 commit 8fb1697

File tree

1 file changed

+79
-17
lines changed

1 file changed

+79
-17
lines changed

‎problems/0718.最长重复子数组.md‎

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,37 +247,99 @@ class Solution {
247247

248248
Python:
249249

250-
> 动态规划:
250+
2维DP
251251
```python
252252
class Solution:
253-
def findLength(self, A: List[int], B: List[int]) -> int:
254-
dp = [[0] * (len(B)+1) for _ in range(len(A)+1)]
253+
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
254+
# 创建一个二维数组 dp,用于存储最长公共子数组的长度
255+
dp = [[0] * (len(nums2) + 1) for _ in range(len(nums1) + 1)]
256+
# 记录最长公共子数组的长度
255257
result = 0
256-
for i in range(1, len(A)+1):
257-
for j in range(1, len(B)+1):
258-
if A[i-1] == B[j-1]:
259-
dp[i][j] = dp[i-1][j-1] + 1
260-
result = max(result, dp[i][j])
258+
259+
# 遍历数组 nums1
260+
for i in range(1, len(nums1) + 1):
261+
# 遍历数组 nums2
262+
for j in range(1, len(nums2) + 1):
263+
# 如果 nums1[i-1] 和 nums2[j-1] 相等
264+
if nums1[i - 1] == nums2[j - 1]:
265+
# 在当前位置上的最长公共子数组长度为前一个位置上的长度加一
266+
dp[i][j] = dp[i - 1][j - 1] + 1
267+
# 更新最长公共子数组的长度
268+
if dp[i][j] > result:
269+
result = dp[i][j]
270+
271+
# 返回最长公共子数组的长度
261272
return result
273+
262274
```
263275

264-
> 动态规划:滚动数组
276+
1维DP
265277
```python
266278
class Solution:
267-
def findLength(self, A: List[int], B: List[int]) -> int:
268-
dp = [0] * (len(B) + 1)
279+
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
280+
# 创建一个一维数组 dp,用于存储最长公共子数组的长度
281+
dp = [0] * (len(nums2) + 1)
282+
# 记录最长公共子数组的长度
269283
result = 0
270-
for i in range(1, len(A)+1):
271-
for j in range(len(B), 0, -1):
272-
if A[i-1] == B[j-1]:
273-
dp[j] = dp[j-1] + 1
284+
285+
# 遍历数组 nums1
286+
for i in range(1, len(nums1) + 1):
287+
# 用于保存上一个位置的值
288+
prev = 0
289+
# 遍历数组 nums2
290+
for j in range(1, len(nums2) + 1):
291+
# 保存当前位置的值,因为会在后面被更新
292+
current = dp[j]
293+
# 如果 nums1[i-1] 和 nums2[j-1] 相等
294+
if nums1[i - 1] == nums2[j - 1]:
295+
# 在当前位置上的最长公共子数组长度为上一个位置的长度加一
296+
dp[j] = prev + 1
297+
# 更新最长公共子数组的长度
298+
if dp[j] > result:
299+
result = dp[j]
274300
else:
275-
dp[j] = 0 #注意这里不相等的时候要有赋0的操作
276-
result = max(result, dp[j])
301+
# 如果不相等,将当前位置的值置为零
302+
dp[j] = 0
303+
# 更新 prev 变量为当前位置的值,供下一次迭代使用
304+
prev = current
305+
306+
# 返回最长公共子数组的长度
277307
return result
308+
278309
```
279310

311+
2维DP 扩展
312+
```python
313+
class Solution:
314+
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
315+
# 创建一个二维数组 dp,用于存储最长公共子数组的长度
316+
dp = [[0] * (len(nums2) + 1) for _ in range(len(nums1) + 1)]
317+
# 记录最长公共子数组的长度
318+
result = 0
319+
320+
# 对第一行和第一列进行初始化
321+
for i in range(len(nums1)):
322+
if nums1[i] == nums2[0]:
323+
dp[i + 1][1] = 1
324+
for j in range(len(nums2)):
325+
if nums1[0] == nums2[j]:
326+
dp[1][j + 1] = 1
327+
328+
# 填充dp数组
329+
for i in range(1, len(nums1) + 1):
330+
for j in range(1, len(nums2) + 1):
331+
if nums1[i - 1] == nums2[j - 1]:
332+
# 如果 nums1[i-1] 和 nums2[j-1] 相等,则当前位置的最长公共子数组长度为左上角位置的值加一
333+
dp[i][j] = dp[i - 1][j - 1] + 1
334+
if dp[i][j] > result:
335+
# 更新最长公共子数组的长度
336+
result = dp[i][j]
337+
338+
# 返回最长公共子数组的长度
339+
return result
340+
280341

342+
```
281343
Go:
282344
```Go
283345
func findLength(A []int, B []int) int {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /