718. 鏈€闀块噸澶嶅瓙鏁扮粍. dp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了718. 鏈€闀块噸澶嶅瓙鏁扮粍. dp相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/strong' title='strong'>strong
str problem http https bar mem 杈撳叆 for缁欎袱涓暣鏁版暟缁?A?鍜?B?锛岃繑鍥炰袱涓暟缁勪腑鍏叡鐨勩€侀暱搴︽渶闀跨殑瀛愭暟缁勭殑闀垮害銆?/p>
绀轰緥 1:
杈撳叆:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
杈撳嚭: 3
瑙i噴:
闀垮害鏈€闀跨殑鍏叡瀛愭暟缁勬槸 [3, 2, 1]銆?br>
璇存槑:
1 <= len(A), len(B) <= 1000
0 <= A[i], B[i] < 100
鏉ユ簮锛氬姏鎵o紙LeetCode锛?br>
閾炬帴锛?a href="https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray">https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray
钁椾綔鏉冨綊棰嗘墸缃戠粶鎵€鏈夈€傚晢涓氳浆杞借鑱旂郴瀹樻柟鎺堟潈锛岄潪鍟嗕笟杞浇璇锋敞鏄庡嚭澶勩€?/p>
O(n*m)
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int n = A.size(), m = B.size();
int dp[n + 1][m + 1];
int ans = 0;
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if (A[i - 1] == B[j - 1]){
dp[i][j] = dp[i - 1][j - 1] + 1;
ans = max(ans, dp[i][j]);
}
}
}
return ans;
}
};
以上是关于718. 鏈€闀块噸澶嶅瓙鏁扮粍. dp的主要内容,如果未能解决你的问题,请参考以下文章