剑指offer二十四:第一个只出现一次的字符
#include<iostream>
using namespace std;
char firstNotrepeatChar(char* string)
{
if (string == NULL)
return NULL;
const int tableSize = 256;
unsigned int hashTable[tableSize];
for (int i = 0; i < tableSize; i++)
hashTable[i] = 0;
//第一次扫描时,每扫描到一个字符就在哈希表对应项中把次数加1
char* temp = string;
while (*(temp) != '\0')
{
hashTable[*(temp)]++;
temp++;
}
//第二次扫描时,每扫描到一个字符就能从哈希表中得到该字符出现的次数。这样第一个出行依次的字符就是符合要求的输出
temp = string;
while (*(temp) != '\0')
{
if (hashTable[*(temp)] == 1)
return *temp;
temp++;
}
return NULL;
}
int main()
{
char* string = "abaccdeff";
char result = firstNotrepeatChar(string);
cout << result << endl;
}