LeetCode:Maximum Length of Repeated Subarray
来源:互联网 发布:linux 查看ssh 编辑:程序博客网 时间:2024/06/03 13:53
题目:
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
Examples:
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].
思路:
用一个二维数组 C[i][j] 来表示以A[i-1] 和 B[j-1] 结尾的最长子串的长度。显然,C[0][0]=0。当A[i-1] = B[j-1]时,C[i][j] = C[i-1][j-1] + 1,否则为0.(例如:如果A[0] = B[0],那么C[1][1] = 1;如果 A[0] != B[0],那么C[1][1] = 0)
代码:
class Solution {public: int findLength(vector<int>& A, vector<int>& B) { int len1 = A.size(); int len2 = B.size(); vector<vector<int>> C(len1+1, vector<int>(len2+1, 0)); int len = 0; for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (A[i-1] == B[j-1]) { C[i][j] = C[i-1][j-1] + 1; } len = len > C[i][j] ? len : C[i][j]; } } return len; }};
阅读全文
0 0
- 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 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] 718. 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
- Maximum Length of Repeated Subarray
- RPM包管理基础和源码编译基础
- 自定义view轮播
- NodeJS之域名解析器实现
- ZOJ
- javaScript 小结
- LeetCode:Maximum Length of Repeated Subarray
- 【Web前端学习笔记】EasyUI、JQuery表单验证
- Java模板方法模式中的HookMethod实现
- Android里面常用的Drawable的使用
- Linux进程地址空间管理
- HDU 5982Relic Discovery
- python中的os.listdir()方法
- java创建线程的方式
- yum 网络库服务器基础搭建