用数组实现一个哈希表,存储元素次数

判断字符出现次数的类似问题可以考虑基于数组创建一个简单的哈希表

 1 char FirstNotRepeatingChar(char* pString)
 2 {
 3     if(pString == NULL)
 4         return '\0';
 5 
 6     const int tableSize = 256;
 7     unsigned int hashTable[tableSize];
 8     for(unsigned int i = 0; i<tableSize; ++ i)
 9         hashTable[i] = 0;
10 
11     char* pHashKey = pString;
12     while(*(pHashKey) != '\0')
13         hashTable[*(pHashKey++)] ++;
14 
15     pHashKey = pString;
16     while(*pHashKey != '\0')
17     {
18         if(hashTable[*pHashKey] == 1)
19             return *pHashKey;
20 
21         pHashKey++;
22     }
23 
24     return '\0';
25 } 

 

相关文章:

  • 2021-11-16
  • 2022-12-23
  • 2022-01-02
  • 2021-12-17
  • 2021-06-17
  • 2022-01-25
猜你喜欢
  • 2022-12-23
  • 2021-07-24
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案