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

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

 1 char firstNotRepeatingChar(char *str)
 2 {
 3     /* hash表存储 每个可能出现的字符作为索引值 数组内容即为出现的次数 */
 4     char ret = '\0';
 5     // 1.建立hashtable
 6     const int size = 256;
 7     // 全部初始化为0
 8     int hashtable[size] = {0};
 9     char *hashKey = str;
10     // 2.记录出现次数
11     while(*hashKey != '\0')
12     {
13         hashtable[*hashKey]++;
14         hashKey++;
15     }
16     // 3.查找返回
17     hashKey = str;
18     while(*hashKey != '\0')
19     {
20         if (hashtable[*hashKey] == 1)
21         {
22             ret = *hashKey;
23             return ret;
24         }
25         hashKey++;
26     }
27     return ret;
28 
29 }

 

posted @ 2015-05-11 16:57  HugoJiang  阅读(307)  评论(0编辑  收藏  举报