给两个整数数组 AB ,返回两个数组中公共的、长度最长的子数组的长度。

    示例:

    1. 输入:
    2. A: [1,2,3,2,1]
    3. B: [3,2,1,4,7]
    4. 输出:3
    5. 解释:
    6. 长度最长的公共子数组是 [3, 2, 1]


    提示:

    • 1 <= len(A), len(B) <= 1000
    • 0 <= A[i], B[i] < 100

      class Solution {
      public:
        int findLength(vector<int>& A, vector<int>& B) {
            vector<vector<int>> dp(A.size()+1, vector<int>(B.size()+1, 0));
            int res = 0;
            for(int i = 0; i<=A.size(); i++){
                for(int j = 0; j<=B.size();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;
                        res = max(res, dp[i][j]);
                    }
      
                }
            }
      
            return res;
        }
      };