【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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值