[LeetCode] 718. Maximum Length of Repeated Subarray
来源:互联网 发布:python 列表变元组 编辑:程序博客网 时间:2024/05/21 09:22
[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 <= len(A), len(B) <= 1000
0 <= A[i], B[i] < 100
分析
当a[i] = b[j],如果i = 0 或者 j = 0, dp[i][j] = 1。如果i, j都不为0,那么dp[i][j] = dp[i-1][j-1]。返回最大的dp[i][j]。
class Solution {public: int findLength(vector<int>& A, vector<int>& B) { int sizeA = A.size(); int sizeB = B.size(); vector<vector<int> > dp(sizeA + 1, vector<int>(sizeB + 1, 0)); int res = 0; for (int i = 0; i < sizeA; i++) { for (int j = 0; j < sizeB; j++) { if (A[i] == B[j]) { if (i == 0 || j == 0) { dp[i][j] = 1; } else { dp[i][j] = dp[i - 1][j - 1] + 1; res = max(res, dp[i][j]); } } } } return res; }};
阅读全文
0 0
- Leetcode 718. Maximum Length of Repeated Subarray
- Leetcode 718. Maximum Length of Repeated Subarray
- leetcode 718. Maximum Length of Repeated Subarray
- [LeetCode]718. Maximum Length of Repeated Subarray
- [LeetCode] 718. Maximum Length of Repeated Subarray
- [leetcode] 718. Maximum Length of Repeated Subarray
- Leetcode:718. Maximum Length of Repeated Subarray
- leetcode 718( Maximum Length of Repeated Subarray)
- Leetcode | Maximum Length of Repeated Subarray
- LeetCode #718 Maximum Length of Repeated Subarray
- Leetcode | Maximum Length of Repeated Subarray
- LeetCode:Maximum Length of Repeated Subarray
- [leetcode]Maximum Length of Repeated Subarray
- [LeetCode]Maximum Length of Repeated Subarray
- Leetcode 718 Maximum Length of Repeated Subarray
- LeetCode:Maximum Length of Repeated Subarray
- leetCode-Maximum Length of Repeated Subarray
- LeetCode题解系列--718. Maximum Length of Repeated Subarray
- 日期类型转换
- 为什么不能和程序媛谈恋爱?
- Spring5学习(二)-spring projects之Spring Cloud
- Java语言学习(四)面向对象初步(2)
- 工作中一点点小小总结
- [LeetCode] 718. Maximum Length of Repeated Subarray
- 高经理写的qiezzi
- 很莫名的textarea最前面的空格问题
- vue路由\导航刷新后:ative\localStorage\url截取参数
- 正则%00截断
- android 获取ListView内部Item高度
- [LeetCode] 646. Maximum Length of Pair Chain
- java获取excel的.xlsx格式的具体某一列
- [LeetCode] 688. Knight Probability in Chessboard