面试题35:第一个只出现一次的字符
用数组实现一个哈希表,存储元素次数
判断字符出现次数的类似问题可以考虑基于数组创建一个简单的哈希表
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 }