Maximum Length of Repeated Subarray
来源:互联网 发布:小猪php在线解密 编辑:程序博客网 时间:2024/06/05 03:18
问题描述:
Given two integer arrays A
and B
, return the maximum length of an subarray that appears in both arrays.
给定两个数组A、B,返回两个数组中的最大相同子数组的长度
问题解决:
用二维数组a[i][j]储存
通过动态规划找到A数组到i为止,B数组到j为止的最大相同字数组的长度,那么a[i][j]的递推式可以表示为
若A[i]==B[j] a[i][j] = a[i-1][j-1] + 1
否则a[i][j] = 0
代码如下:
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int i, j;
int max = 0;
int len1 = A.size();
int len2 = B.size();
int a[1001][1001];
//初始化数组
for(i = 0; i < 1001; i++) {
for(j = 0; j < 1001; j++) {
a[i][j] = 0;
}
}
//根据递推式算出子问题的值
for(i = 1; i < len1+1; i++) {
for(j = 1; j < len2+1; j++) {
a[i][j] = A[i-1] == B[j-1] ? a[i-1][j-1] + 1 : 0;
//找到最大的长度
max = max > a[i][j] ? max : a[i][j];
}
}
return max;
}
};
- Maximum Length of Repeated Subarray
- Maximum Length of Repeated Subarray
- Maximum Length of Repeated Subarray
- leetcode 718( Maximum Length of Repeated Subarray)
- Leetcode 718. Maximum Length of Repeated Subarray
- 718. Maximum Length of Repeated Subarray
- 718. Maximum Length of Repeated Subarray
- LeetCode718. Maximum Length of Repeated Subarray(Medium)
- 718. Maximum Length of Repeated Subarray
- Leetcode | Maximum Length of Repeated Subarray
- LeetCode #718 Maximum Length of Repeated Subarray
- Maximum Length of Repeated Subarray解题报告
- 718.Maximum Length of Repeated Subarray(M)
- Leetcode | Maximum Length of Repeated Subarray
- LeetCode:Maximum Length of Repeated Subarray
- [leetcode]Maximum Length of Repeated Subarray
- 718. Maximum Length of Repeated Subarray
- 718. Maximum Length of Repeated Subarray
- 欢迎使用CSDN-markdown编辑器
- java基础3:io流对象之File对象(上)
- Fluent Python读书笔记(三)
- Java 并发工具包-BlockingQueue-DelayQueue
- 在EC2上创建root用户,并使用root用户登录
- Maximum Length of Repeated Subarray
- babyos2(9)——系统调用
- 一个架构师谈什么是架构以及怎么成为一个架构师
- javascript 32位以内进制转换
- 独自闯荡上海,面试总结1
- 软件架构师知识点1
- 谈谈MySQL隐式类型转换
- 二分起步---Can you find it?
- Ubuntu 建立subversion与redmine关联