Wins the offer - 53 character stream for the first time a character appears only

Title Description

Please implement a function to find the character stream for the first time a character appears only. For example, when the character stream reads only the first two characters "go", the first character only occurs once a "g". When reading out the first six characters "google" from the character stream, first appears only one character is "l".

Output Description:

If the current character stream does not appear there is a character, returns the # character. 

Solution:
  the number of hash table records each letter appears, was elected the letter is repeated, as if not looking after repeated letters

 

. 1  class Solution
 2  {
 . 3  public :
 . 4      // the Insert One char from the stringstream 
. 5      void the Insert ( char CH)
 . 6      {
 . 7          STR + = CH;
 . 8          Word [CH] ++ ;
 . 9          IF (RES == ' # ' && Word [CH] == . 1 ) // new letter 
10              RES = CH;
 . 11          IF (Word [RES]> . 1 ) // repeat 
12          {
13 is              RES = ' # ' ;
 14              for ( int I = index; I <str.length (); I ++) // successively repeated for outstanding letters 
15              {
 16                  IF (Word [STR [I]] == . 1 )
 . 17                  {
 18 is                      RES = STR [I];
 . 19                      index = I;
 20 is                      BREAK ;
 21 is                  }
 22 is              }
 23 is          }
 24      }
 25      //return the first appearence once char in current stringstream
26     char FirstAppearingOnce()
27     {
28         return res;
29     }
30 private:
31     string str = "";
32     int word[256] = { 0 };
33     int index = 0;
34     char res = '#';
35 };

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11701124.html