【C语言刷力扣】1832.判断句子是否为全字母句

题目:

法一

复制代码
bool checkIfPangram(char* sentence) {
    int str[256];
    memset(str, 0, sizeof(int));
    for (int i = 0; i < strlen(sentence); ++i) {
        ++str[ sentence[i] ];
    }

    for (int j = 'a'; j <= 'z'; ++j) {
        if (!str[j]) return false;
    }
    return true;
}

法二 动态分配

复制代码
typedef struct {
    char word;
    int count;
    UT_hash_handle hh;
} hashEntry;

bool checkIfPangram(char* sentence) {
    hashEntry * cnt = NULL;
    for (int i = 0; i < strlen(sentence); i++) {
        hashEntry* pEntry = NULL;
        HASH_FIND(hh, cnt, &sentence[i], sizeof(char), pEntry);
        if (pEntry == NULL) {
            hashEntry * pEntry = (hashEntry*)malloc(sizeof(hashEntry));
            pEntry -> word = sentence[i];
            pEntry -> count = 1;
            HASH_ADD(hh, cnt, word, sizeof(char), pEntry);
        }
        else pEntry -> count++;
    }

    hashEntry *curr = NULL, *next = NULL;
    int num = 26;
    HASH_ITER(hh, cnt, curr, next)
    {
        if (curr -> count > 0) {
            num--;
        }
        if (num == 0) return true;
        free(curr);
    }
    return false;
}
相关推荐
风中的微尘2 小时前
数学基础2
算法
一只小小的芙厨3 小时前
树状数组总结
数据结构·算法
大彼方..4 小时前
CSDN-02-开发环境介绍与安装
c语言·开发语言·stm32
郝学胜-神的一滴6 小时前
Effective STL 条款 17:用 swap 技巧去掉多余容量
c语言·开发语言·数据结构·c++·算法·rpc
玩转单片机与嵌入式6 小时前
深入浅出TinyML 36:如何验证一个TinyML系统真正具备工程应用价值?
网络·人工智能·算法
Nazarick8 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
16月6日-晴8 小时前
斐波那契数列题解
算法·leetcode
烟烟Abandon8 小时前
FFmpeg开发基础之 C 语言编译器
c语言·开发语言·ffmpeg
郝学胜-神的一滴8 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
Zwawa10 小时前
断电后参数为什么全没了:把小车配置安全地存进 Flash
c语言·单片机·嵌入式