笑着生活  
思想大于技术...... 惟仁者为能以大事小,惟智者为能以小事大: Java之路——专注,研究,思考,总结
日历
<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 46
  • 文章 - 4
  • 评论 - 4
  • 引用 - 0

导航

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
求第一个无重复字符,如"total"的第一个无重复字符是o,"teeter"的第一个无重复字符是r,效率要优于O(n的平方)
public static Character FirstNonRepeated(String)
 
 
下面是我把别人写的c程序翻译成java程序:
public class firstNonRepeatedChar {        
        private    static final int MAX_CHAR = 256;            
                
        public static void main(String[] args) {        
                String str = "total";        
                firstNoRepeatedChar(str);        
        }        
    
        public static int firstNoRepeatedChar(String str) {        
                 int i = 0;            
                 int j = 0;            
                 int[] p = new int[MAX_CHAR];        
                 char[] chars = str.toCharArray();        
                 //初始化数组p,p用于保存字符出现的次数                    
                 for (j = 0; j < MAX_CHAR; j++) {            
                    p[j] = 0;            
                 }        
                        
                 //统计字符出现的次数                
                 for(i=0;i<chars.length;i++){        
                         p[chars[i]]++;        
                 }        
    
                 //寻找第一个统计次数为1的字符        
                 for (i = 0; chars[i] != -1; i++) {            
                            if (p[chars[i]] == 1) {            
                             System.out.println(chars[i]);        
                             return 1;            
                            }            
                         }            
                return 0;        
                        
        }        
}    

文章来源:http://java999.blog.51cto.com/259217/162744
posted on 2009-06-09 13:41 999 阅读(200) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 999 Powered by: 博客园 模板提供:沪江博客