Leetcode(每日一题)

最长重复子数组

题目描述:

在这里插入图片描述

思路分析:动态规划直接秒杀(思想见上一篇博客华为机试(二)

直接上代码:

class Solution {
    
    
public:
    int findLength(vector<int>& A, vector<int>& B) {
    
    
           int n = A.size(), m = B.size();
           vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
            int ans = 0;
            for (int i = n - 1; i >= 0; i--) {
    
    
                for (int j = m - 1; j >= 0; j--) {
    
    
                    dp[i][j] = A[i] == B[j] ? dp[i + 1][j + 1] + 1 : 0;
                    ans = max(ans, dp[i][j]);
                }
            }
            return ans;
        }
};

猜你喜欢

转载自blog.csdn.net/qq_41606378/article/details/108599658