// interview questions 50 (2): The first character stream of characters appear only once // Title: Please implement a function to find the first character stream of characters appear only once. For example, when the // time character stream reads only the first two characters "go", the first character appears only once is 'g'. When the characters from the // time character stream reads the first six characters "google", it appears only once in the first character is 'l'. #include <cstdio> #include <Vector> #include <Limits> using namespace std; class CharStatistics { public: CharStatistics (): index ( 0 ) // index = 0 when initializing the hash table, a bit like a constructor { for ( int I = 0 ; I < 256 ; ++ I) occurrence[i] = -1; } void the Insert ( char CH) // insertion position of the current character into the hash table { IF (occurrence [CH] == - . 1 ) // there have been no occurrence [CH] = index; the else IF (occurrence [CH]> = 0 ) // number of occurrences of 1 time or more occurrence [CH] = - 2 ; ++index; } char FirstAppearingOnce() { char CH = ' \ 0 ' ; int minIndex the numeric_limits = < int ;> :: max () // maximum value of type int for (int i = 0; i < 256; ++i) { // occur once and the most forward position of the character IF (occurrence [I]> = 0 && occurrence [I] < minIndex) { CH = ( char ) I; // convert character minIndex = occurrence [I]; // update the minimum position } } return ch; } private: // occurrence[i]: A character with ASCII value i; // occurrence[i] = -1: The character has not found; // occurrence[i] = -2: The character has been found for mutlple times // occurrence[i] >= 0: The character has been found only once int occurrence[256]; //hash表 int index; //当前字符流的位数 };
// ==================== test code ==================== void the Test ( const char * TestName, CharStatistics chars, char expected) { if (testName != nullptr) printf("%s begins: ", testName); if (chars.FirstAppearingOnce() == expected) printf("Passed.\n"); else printf("FAILED.\n"); } int main(int argc, char* argv[]) { CharStatistics chars; Test("Test1", chars, '\0'); chars.Insert('g'); Test("Test2", chars, 'g'); chars.Insert('o'); Test("Test3", chars, 'g'); chars.Insert('o'); Test("Test4", chars, 'g'); chars.Insert('g'); Test("Test5", chars, '\0'); chars.Insert('l'); Test("Test6", chars, 'l'); chars.Insert('e'); Test("Test7", chars, 'l'); return 0; }
Analysis: The hash table when you need multiple passes with a nice string.
class Solution { public: Solution() : index(0) { for (int i = 0; i < 256; ++i) occurrence[i] = -1; } //Insert one char from stringstream void Insert(char ch) { if (occurrence[ch] == -1) occurrence[ch] = index; else if (occurrence[ch] >= 0) occurrence[ch] = -2; ++index; } //return the first appearence once char in current stringstream char FirstAppearingOnce() { char ch = '#'; int minIndex = numeric_limits<int>::max(); for (int i = 0; i < 256; ++i) { if (occurrence[i] >= 0 && occurrence[i] < minIndex) { ch = (char)i; minIndex = occurrence[i]; } } return ch; } private: int occurrence[256]; int index; };