From 8d11b5bcf78c13fb386056bd1811a74cd9f5c745 Mon Sep 17 00:00:00 2001 From: qiuzidian <115708838+qiuzidian@users.noreply.github.com> Date: Mon, 29 Jan 2024 22:18:50 +0800 Subject: [PATCH] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91=E4=BF=A1.md?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0c=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index b800c232..1dbf5de0 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; +} + ```