【leetcode】最长公共子数组
类似于最长公共子序列
- 动态规划
思路
Java
1、求解最长公共子数组的长度
class Solution {
public int findLength(int[] A, int[] B) {
return subLength(A,B,A.length-1,B.length-1);
}
private int subLength(int[] A, int[] B, int lenA, int lenB)
{
int temp1=0,temp2=0;
if(lenA ==-1 || lenB==-1) return 0;
if(A[lenA] == B[lenB])
{
return subLength(A,B,lenA-1,lenB-1)+1;
}
temp1 = subLength(A,B,lenA-1,lenB);
temp2 = subLength(A,B,lenA,lenB-1);
return (temp1>temp2)?temp1:temp2;
}
}