【面试题35】第一个只出现一次的字符

【题目描述】

在字符串中找出第一个只出现一次的字符,如输入"abaccdeff",则输出"b"。

【解决方案】

利用哈希表解决。

 我的代码实现,仅供参考:

 1         public static char FirstNotRepeatChar(char[] str)
 2         {
 3             if (str == null || str.Length < 1)
 4                 return ' ';
 5 
 6             int[] hash = new int[256];
 7             char result = ' ';
 8 
 9             foreach (char ch in str)
10                 hash[ch]++;
11 
12             for (int i = 0; i < hash.Length; i++)
13             {
14                 if (hash[i] == 1)
15                 {
16                     result = (char)i;
17                     break;
18                 }
19             }
20 
21             return result;
22         }
原文地址:https://www.cnblogs.com/HuoAA/p/4825528.html