mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Update 0474.一和零.md
0474.一和零新增C语言实现
This commit is contained in:
@ -533,7 +533,39 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
```c
|
||||
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
int findMaxForm(char** strs, int strsSize, int m, int n) {
|
||||
int dp[m + 1][n + 1];
|
||||
memset(dp, 0, sizeof (int ) * (m + 1) * (n + 1));
|
||||
for(int i = 0; i < strsSize; i++){
|
||||
// 统计0和1的数量
|
||||
int count0 = 0;
|
||||
int count1 = 0;
|
||||
char *str = strs[i];
|
||||
while (*str != '\0'){
|
||||
if(*str == '0'){
|
||||
count0++;
|
||||
} else{
|
||||
count1++;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
for(int j = m; j >= count0; j--){
|
||||
for(int k = n; k >= count1; k--){
|
||||
dp[j][k] = max(dp[j][k], dp[j - count0][k - count1] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[m][n];
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### C#
|
||||
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
@ -565,4 +597,3 @@ public class Solution
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user