第一个只出现一次的字符--java实现

 1     /**
 2      * 主要思想是通过数组来保存每个字符的出现次数,数组访问O(1),所以总时间复杂度可以保持O(n),通过两次遍历可以解决问题
 3      * @param ch
 4      * @return
 5      */
 6     
 7     public static char getFirstNotRepeatChar(char[] charArray){
 8         if(charArray == null)
 9             return 0;
10         
11         char result = 0;
12         int[] table = new int[256];
13         for(int i = 0; i < table.length ; i++)
14             table[i] = 0;
15         for(char temp : charArray){
16             table[temp]++;
17         }
18         for(char temp : charArray){
19             if(table[temp] == 1){
20                 result = (char) temp;
21                 break;
22             }
23         }
24         return result;
25     }

 

posted @ 2015-11-05 14:15  music180  阅读(245)  评论(0编辑  收藏  举报