Skip to content

Commit 0d5d25a

Browse files
committed
添加0115.不同的子序列.md C语言版本
1 parent 4745c7d commit 0d5d25a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

problems/0115.不同的子序列.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,45 @@ impl Solution {
371371
}
372372
}
373373
```
374+
### C:
375+
```c
376+
#define MOD 1000000007
377+
int numDistinct(char *s, char *t)
378+
{
379+
//dp[i][j]: t前i个字符,s前j个字符,s子序列中t出现的个数
380+
int **dp = malloc(sizeof(int *) * (strlen(t) + 1));
381+
for (int i = 0; i <= strlen(t); i++)
382+
{
383+
dp[i] = malloc(sizeof(int) * (strlen(s) + 1));
384+
}
385+
386+
// 初始化
387+
for (int i = 0; i <= strlen(t); i++)
388+
{
389+
dp[i][0] = 0;
390+
}
391+
for (int j = 0; j <= strlen(s); j++)
392+
{
393+
dp[0][j] = 1;
394+
}
374395

396+
// 遍历
397+
for (int i = 1; i <= strlen(t); i++)
398+
{
399+
for (int j = 1; j <= strlen(s); j++)
400+
{
401+
if (s[j - 1] == t[i - 1])
402+
{
403+
dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
404+
}
405+
else
406+
{
407+
dp[i][j] = dp[i][j - 1];
408+
}
409+
}
410+
}
411+
return dp[strlen(t)][strlen(s)] % MOD;
412+
}
413+
```
375414

376415

0 commit comments

Comments
 (0)