【題解】LeetCode 1143. Longest Common Subsequence

【題目敘述】https://leetcode.com/problems/longest-common-subsequence/

class Solution {
public:
    int dp[1005][1005];
    int longestCommonSubsequence(string s, string t) {
        for (int i = 1; i <= s.length(); i++){
            for (int j = 1; j <= t.length(); j++){
                if (s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1]+1;
                else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            }
        }
        return dp[s.length()][t.length()];
    }
};
分享本文 Share with friends