【面试题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         }

 

posted @ 2015-09-21 11:00  叫我霍啊啊啊  阅读(173)  评论(0编辑  收藏  举报