Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加 最大子序和(动态规划).md c语言版本 #2923

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions problems/0053.最大子序和(动态规划).md
Original file line number Diff line number Diff line change
@@ -239,6 +239,35 @@ function maxSubArray(nums: number[]): number {
return resMax
}
```
### C:
```c
bool isSubsequence(char *s, char *t)
{
bool **dp = malloc(sizeof(bool *) * (strlen(s) + 1));
for (int i = 0; i <= strlen(s); i++)
{
dp[i] = malloc(sizeof(bool) * (strlen(t) + 1));
}
// 初始化
for (int i = 0; i <= strlen(s); i++)
{
dp[i][0] = false;
}
for (int j = 0; j <= strlen(t); j++)
{
dp[0][j] = true;
}
// 遍历
for (int i = 1; i <= strlen(s); i++)
{
for (int j = 1; j <= strlen(t); j++)
{
dp[i][j] = s[i - 1] == t[j - 1] ? dp[i - 1][j - 1] : dp[i][j - 1];
}
}
return dp[strlen(s)][strlen(t)];
}
```



39 changes: 39 additions & 0 deletions problems/0115.不同的子序列.md
Original file line number Diff line number Diff line change
@@ -371,6 +371,45 @@ impl Solution {
}
}
```
### C:
```c
#define MOD 1000000007
int numDistinct(char *s, char *t)
{
//dp[i][j]: t前i个字符,s前j个字符,s子序列中t出现的个数
int **dp = malloc(sizeof(int *) * (strlen(t) + 1));
for (int i = 0; i <= strlen(t); i++)
{
dp[i] = malloc(sizeof(int) * (strlen(s) + 1));
}

// 初始化
for (int i = 0; i <= strlen(t); i++)
{
dp[i][0] = 0;
}
for (int j = 0; j <= strlen(s); j++)
{
dp[0][j] = 1;
}

// 遍历
for (int i = 1; i <= strlen(t); i++)
{
for (int j = 1; j <= strlen(s); j++)
{
if (s[j - 1] == t[i - 1])
{
dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
}
else
{
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[strlen(t)][strlen(s)] % MOD;
}
```