mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
@ -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">
|
||||
|
Reference in New Issue
Block a user