char FirstNotRepeatingChar(char* pString) { // invalid input if(!pString) return 0; // get a hash table, and initialize it const int tableSize = 256; //字符1个字节8位 unsigned int hashTable[tableSize]; //unsigned?? for(unsigned int i = 0; i < tableSize; ++ i) hashTable[i] = 0; //为什么要置0? // get the how many times each char appears in the string char* pHashKey = pString; while(*(pHashKey) != ‘\0‘) hashTable[*(pHashKey++)] ++; // find the first char which appears only once in a string pHashKey = pString; while(pHashKey) { if(hashTable[*pHashKey] == 1) return *pHashKey; pHashKey++; } // if the string is empty // or every char in the string appears at least twice return 0; }
时间: 2024-11-05 13:31:28