///////////////////////////////////////////////////////////////////////
// Find the first char which appears only once in a string
// Input: pString - the string
// Output: the first not repeating char if the string has, otherwise 0
///////////////////////////////////////////////////////////////////////
char hash(char * str) //对 hash表的用法!非常经典!!!!
{
const int tablesize=256;
unsignedint hashtable [256];
if(!str)
return NULL;
for(int i=0;i<tablesize;i++)
{
hashtable[i]=0;
}
char * p=str;
while(*p!='\0')
hashtable[*(p++)]++; //此语句是核心!!!
p=str;
while(*p!='\0')
if(hashtable[*(p++)]==1)
{
return *(--p);
}
return 0;
}
本文讨论了如何在给定的字符串中找到首次仅出现一次的字符,并提供了使用哈希表实现的方法。通过核心代码片段展示了哈希表的应用,并强调了改进代码的必要性。

被折叠的 条评论
为什么被折叠?



