leetCode(Using C)——718. Maximum Length of Repeated Subarray
Description:
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
Link:
If you want to solve the problem, you can visite the web site.click me
Solution
採用動態規劃的思路
#ifndef MAX(X,Y)
#define MAX(X,Y) (((X)>(Y))?(X):(Y)) //定義三目運算子
#endif // MAX
int findLength(int *A, int ASize, int *B, int BSize){
int Count[ASize+1][BSize+1]; //設立而為Count陣列
int i,j,max=0;
memset(Count, 0, sizeof(Count)); //重置陣列空間
for(i=1;i<ASize+1;i++){
for(j=1;j<BSize+1;j++){
Count[i][j]=A[i-1]==B[j-1]?Count[i-1][j-1]+1:0; //依次根據上一行改變當前行的統計長度
max=MAX(Count[i][j],max); //記錄遇到的最大值
}
}
return max;
}
相關文章
- Leetcode Maximum SubarrayLeetCode
- Leetcode Maximum Product SubarrayLeetCode
- Leetcode-Maximum SubarrayLeetCode
- Maximum Subarray leetcode javaLeetCodeJava
- Leetcode-Maximum Product SubarrayLeetCode
- [leetCode][001] Maximum Product SubarrayLeetCode
- LeetCode | 152. Maximum Product SubarrayLeetCode
- LeetCode-Maximum Size Subarray Sum Equals kLeetCode
- [LeetCode] 2419. Longest Subarray With Maximum Bitwise ANDLeetCode
- 【leetcode】53. Maximum Subarray 連續子序列的最大和LeetCode
- LintCode-Maximum Subarray II
- LintCode-Maximum Subarray III
- LintCode-Maximum Subarray Difference
- [LeetCode] Maximum Product Subarray 求連續子陣列的最大乘積LeetCode陣列
- sqlldr Field in data file exceeds maximum lengthSQL
- 【Lintcode】191. Maximum Product Subarray
- 最大子陣列問題(Maximum subarray problem)陣列
- Maximum Subarray 連續子陣列最大和陣列
- 53_Maximum Subarray-最大子陣列陣列
- LeetCode-Repeated DNA SequenceLeetCode
- oracle 匯入報錯:field in data file exceeds maximum lengthOracle
- Leetcode Length of Last WordLeetCodeAST
- [LeetCode] Minimum Size Subarray SumLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- LeetCode-Maximum GapLeetCode
- [leetcode]length-of-last-wordLeetCodeAST
- 【leetcode】length of last word (easy)LeetCodeAST
- Leetcode-Length of Last WordLeetCodeAST
- sqlldr匯入資料包錯 Field in data file exceeds maximum lengthSQL
- leetCode(Using C)——657. Judge Route CircleLeetCode
- [LeetCode] 560. Subarray Sum Equals KLeetCode
- LeetCode: 560. Subarray Sum Equals KLeetCode
- LeetCode-Minimum Size Subarray SumLeetCode
- LeetCode-Create Maximum NumberLeetCode
- leetCode(Using C)——461. Hamming DistanceLeetCode
- [LeetCode] 523. Continuous Subarray SumLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- LeetCode-Maximum Product of Word LengthsLeetCode