leetcode 718. Maximum Length of Repeated Subarray
来源:互联网 发布:java教材 pdf 编辑:程序博客网 时间:2024/06/14 16:53
题目718. Maximum Length of Repeated Subarray
等级:medium
标签: 动态规划
思路
类似LCS(最长公共字串)的处理方法。使用动态规划
定义dp[i][j] 表示以X[i]和Y[j]作为匹配的最长子数组的最后一个元素的长度,注意,这里X,Y所以都是从1开始,也就是dp[0][*] 表示第一个子数组是空串。时间复杂度为O(n)
实现
class Solution {public: int findLength(vector<int>& A, vector<int>& B) { int l1 = A.size(), l2 = B.size(); int ret = -1; if(l1 == 0 || l2 == 0) return 0; vector< vector<int> > dp(l1 + 1, vector<int>(l2 + 1, 0)); for(int i = 0; i <= l1; i++) for(int j = 0; j <= l2; j++) { if(i == 0 || j == 0) dp[i][j] = 0; else if(A[i - 1] == B[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; if(ret < dp[i][j]) ret = dp[i][j]; } return ret; }};
阅读全文
1 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
- echarts.js的使用方法
- java基础9:线程,线程池,原理分析,案例
- Linux sftp 命令
- Java23种设计模式——组合模式
- linux实现HA有几种方法
- leetcode 718. Maximum Length of Repeated Subarray
- pyhton--零开发---1
- 商品SKU选择实现
- 获取微信小程序二维码/小程序码 PHP
- 3-5 学生成绩统计
- AS打包出现com.android.ide.common.process.ProcessException终极解决方案
- C++对象模型(二)多继承
- java.sql.SQLException: ResultSet is closed解决方法
- Paper Reading:Real-time human pose recognition in parts from single depth images