首页 > 编程语言 > 详细

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

时间:2015-11-05 15:17:06      阅读:210      评论:0      收藏:0      [点我收藏+]
 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     }

 

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

原文:http://www.cnblogs.com/music180/p/4939373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!