LeetCode Weekly Contest 56 第三题
来源:互联网 发布:139和445端口 编辑:程序博客网 时间:2024/06/07 05:37
题目
- Maximum Length of Repeated Subarray My SubmissionsBack to Contest
User Accepted: 778
User Tried: 1121
Total Accepted: 744
Total Submissions: 1920
Difficulty: Medium
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
分析
题目意思就是找最大的重叠项
暴力模拟吧,这题可以动态规划都是O(M*N)题解里的暴力和我的暴力不太一样
我们用类似滑动校验的思想
先从头匹配
接着固定一端右移一项匹配
知道一个的末端
这时的解是不完全的
我们需要再换一个固定右移
详细的看代码
动态规划的开个二维的数组可以ac
代码
class Solution {public: int findLength(vector<int>& A, vector<int>& B) { int ans =0 ; for( int i=0 ; i<A.size() ;i++){ int length =0 ; for( int j =0 , k = i ; j<B.size() && k < A.size() ; j++,k++){ if( B[j] == A[k ]){ length++; ans = max( ans ,length ); } else{ length = 0 ; } } } for( int i=0 ; i<B.size() ;i++){ int length =0 ; for( int j =0 , k = i ; j<A.size() && k < B.size() ; j++,k++){ if( A[j] == B[k ]){ length++; ans = max( ans ,length ); } else{ length = 0 ; } } } return ans ; }};
时间复杂度O(M∗N)
空间复杂度O(1)
阅读全文
0 0
- LeetCode Weekly Contest 56 第三题
- LeetCode Weekly Contest 56 解题报告
- LeetCode Weekly Contest 25
- leetcode weekly contest #26
- leetcode weekly contest #27
- LeetCode Weekly Contest 28
- LeetCode Weekly Contest 30
- LeetCode Weekly Contest 39
- LeetCode Weekly Contest 47
- LeetCode Weekly Contest 51
- LeetCode Weekly Contest 51
- LeetCode Weekly Contest 64
- weekly contest 55 第三题 Subarray Product Less Than K
- leetcode weekly contest 4,3
- LeetCode Weekly Contest 40(1)
- LeetCode Weekly Contest 40(2)
- LeetCode Weekly Contest 40(3)
- LeetCode Weekly Contest 40(4)
- 心得丨一文告诉你想学数据分析该读什么书、从哪本读起
- FFT(快速傅里叶变换)
- 框架篇-Spring+Mybatis整合Druid连接池,并配置SQL监控
- 关于条件筛选出现的查询问题
- 关于电脑安装多个版本JDK后使用时的切换
- LeetCode Weekly Contest 56 第三题
- 92. Reverse Linked List II
- python 升级 yum 不可用解决方法
- Status源码翻译
- 自定义JSP标签自动完成对页面按钮做权限拦截处理
- Akka(34): Http:Unmarshalling,from Json
- SSL协议详解【转】
- 深度学习Tensorflow相关书籍推荐和PDF下载
- 创投日报:10月30日收录投融资项目20起