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 | 152. Maximum Product SubarrayLeetCode
- [LeetCode] 2419. Longest Subarray With Maximum Bitwise ANDLeetCode
- [LeetCode] 2841. Maximum Sum of Almost Unique SubarrayLeetCode
- [LeetCode] 3090. Maximum Length Substring With Two OccurrencesLeetCode
- 【leetcode】53. Maximum Subarray 連續子序列的最大和LeetCode
- 【Lintcode】191. Maximum Product Subarray
- 最大子陣列問題(Maximum subarray problem)陣列
- 53_Maximum Subarray-最大子陣列陣列
- 一道題把我氣笑了:) 力扣.53 最大子陣列和 leetcode maximum-subarray力扣陣列LeetCode
- [LeetCode] 523. Continuous Subarray SumLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [leetcode]length-of-last-wordLeetCodeAST
- oracle 匯入報錯:field in data file exceeds maximum lengthOracle
- [LeetCode] 560. Subarray Sum Equals KLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- LeetCode: 560. Subarray Sum Equals KLeetCode
- LeetCode 209. Minimum Size Subarray Sum ?LeetCode
- Leetcode 209. Minimum Size Subarray SumLeetCode
- LeetCode 974 Subarray Sums Divisible by K All In OneLeetCode
- LeetCode之N-Repeated Element in Size 2N Array(Kotlin)LeetCodeKotlin
- [LeetCode] 568. Maximum Vacation DaysLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- [LeetCode] 962. Maximum Width RampLeetCode
- LeetCode C++ 1464. Maximum Product of Two Elements in an Array【Array/Sort】簡單LeetCodeC++
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- LeetCode | 232 Implement Queue Using StacksLeetCode
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- [LeetCode] 2064. Minimized Maximum of Products Distributed to Any StoreLeetCodeZed
- [LeetCode] 1750. Minimum Length of String After Deleting Similar EndsLeetCodeMILA
- [LeetCode] 1953. Maximum Number of Weeks for Which You Can WorkLeetCode
- [LeetCode] 2825. Make String a Subsequence Using Cyclic IncrementsLeetCodeREM
- [LeetCode] 232. 225 Implement Queue/Stack using Stacks/QueuesLeetCode
- Increase Subarray Sums
- Subarray Distinct Values
- mysql char_length和lengthMySql
- Java中 length、length()、size()區別Java