天天看點

Detect Pangram(C語言CodeWars)

解題思路:

(1)建立一個hash table,其中key為字元的ascii值

(2)通路一個未通路的,計數加1,并設定其為已通路

(3)計數達到26則傳回true退出,否則最後傳回fasle

#include <ctype.h>
#include <stdlib.h>

#include <stdbool.h>

bool is_pangram(const char *str_in) {

    int* hash = (int*)calloc(256,sizeof(int));
    int count = 0;
    while(*str_in!='\0') {
    if(isalpha(*str_in)) {
      if(hash[tolower(*str_in)]==0) {
        hash[tolower(*str_in)]=1;
        count++;
        //printf("%c,%d\n",*str_in,count);
        if(count==26) return true;
      }
    }
    str_in++;
  }
  free(hash);hash=NULL;
  return false;
}
           

繼續閱讀