第一个只出现一次的字符

来源:互联网 发布:地图数据采集gis 编辑:程序博客网 时间:2024/06/06 01:57
第一个只出现一次的字符


在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。

/*O(n) * 次数统计,hash表,ascii为下标,值为出现次数 * char 8bit, hash表长度为2^8=256 * */char firstNotRepeatingChar2(char *str) {if (str == NULL) {return NULL;}char * temp = str;int hash[256] = { 0 };int i, j, flag = 0;for (; *temp != '\0'; temp++) {hash[*temp]++;}temp = str;for (; *temp != '\0'; temp++) {if (hash[*temp] == 1) {return *temp;}}return NULL;}


1 0