Topic: 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".
. 1 class Solution 2 { . 3 . 4 // Method a: . 5 #if to false . 6 // dictionary storing character keys and the number of occurrences . 7 Private the Dictionary < char , int > dict = new new the Dictionary < char , int > (); . 8 . 9 public char FirstAppearingOnce () 10 { . 11 // Write code here Wallpaper 12 is // traverse the dictionary, the first Key 1 Value is required for the 13 is the foreach ( var Itemin dict) 14 { 15 IF (item.Value == . 1 ) 16 { . 17 return ( char ) item.Key; 18 is } . 19 } 20 is 21 is return ' # ' ; 22 is } 23 is 24 public void the Insert ( char C) 25 { 26 // the Write code here Wallpaper 27 // dictionary is empty, adding the first key 28 IF (dict == null) 29 { 30 dict.Add (c, . 1 ); 31 is return ; 32 } 33 is 34 is // dictionary whether there is key c, defaults to false 35 BOOL In Flag = to false ; 36 37 [ IF (dict.ContainsKey (C)) 38 is { 39 dict [C] + = . 1 ; 40 } 41 is the else 42 is { 43 is dict.Add (C, . 1 ); 44 is } 45 } 46 is #endif 47 48 // Method Two: 49 / * apply an array of integers of size 256, int [] COUNT = new new int [256] {0}; 50 * initialize 0s 51 is * values in the input character as the array index, into a position corresponding to appear in frequency 52 * declare a list of characters, the recording order of the input character * 53 is * / 54 is Private int [] COUNT = new new int [ 256 ]; 55 56 is Private list < char > = List new new List < char > (); 57 is 58 public void the Insert ( char CH) 59 { 60 // the Write code here Wallpaper 61 is 62 is // if the current character is the first time, the recording order of appearance 63 is IF (COUNT [( int ) CH] == 0 ) 64 list.add (CH); 65 66 COUNT [( int ) CH] ++ ; 67 } 68 69 public char FirstAppearingOnce () 70 { 71 is // the Write code here Wallpaper 72 73 is the foreach ( var Item in List) 74 { 75 if (count[(int) item] == 1) 76 { 77 return (char) item; 78 } 79 } 80 81 return '#'; 82 } 83 }