Merge pull request #2430 from qiuzidian/patch-1

添加 0383.赎金信 c语言版本解法
This commit is contained in:
程序员Carl
2024-02-10 14:45:58 +08:00
committed by GitHub

View File

@ -445,6 +445,25 @@ public bool CanConstruct(string ransomNote, string magazine) {
return true;
}
```
### C
```c
bool canConstruct(char* ransomNote, char* magazine) {
// 定义哈希映射数组
int hashmap[26] = {0};
// 对magazine中字符计数
while (*magazine != '\0') hashmap[*magazine++ % 26]++;
// 遍历ransomNote对应的字符自减小于0说明该字符magazine没有或不足够表示
while (*ransomNote != '\0') hashmap[*ransomNote++ % 26]--;
// 如果数组中存在负数说明ransomNote不能由magazine里面的字符构成
for (int i = 0; i < 26; i++) {
if (hashmap[i] < 0) return false;
}
return true;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">