面试题35:第一个只出现一次的字符
2016-07-12 16:12
267 查看
用数组实现一个哈希表,存储元素次数
判断字符出现次数的类似问题可以考虑基于数组创建一个简单的哈希表
判断字符出现次数的类似问题可以考虑基于数组创建一个简单的哈希表
char FirstNotRepeatingChar(char* pString) { if(pString == NULL) return '\0'; const int tableSize = 256; unsigned int hashTable[tableSize]; for(unsigned int i = 0; i<tableSize; ++ i) hashTable[i] = 0; char* pHashKey = pString; while(*(pHashKey) != '\0') hashTable[*(pHashKey++)] ++; pHashKey = pString; while(*pHashKey != '\0') { if(hashTable[*pHashKey] == 1) return *pHashKey; pHashKey++; } return '\0'; }