diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 93ade51e..ff5aafed 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -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; +} + ```