diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index f33391c3..12bd90f8 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -376,10 +376,32 @@ impl Solution { } ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int longestCommonSubsequence(char* text1, char* text2) { + int text1Len = strlen(text1); + int text2Len = strlen(text2); + int dp[text1Len + 1][text2Len + 1]; + memset(dp, 0, sizeof (dp)); + for (int i = 1; i <= text1Len; ++i) { + for (int j = 1; j <= text2Len; ++j) { + if(text1[i - 1] == text2[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[text1Len][text2Len]; +} +``` +

-