Update 1143.最长公共子序列.md

1143.最长公共子序列新增C语言实现
This commit is contained in:
a12bb
2024-03-13 22:08:15 +08:00
parent 00a5515bad
commit fac689939f

View File

@ -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];
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>