首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

获取一个字符串中的第一个不反复字符

2012-12-19 
获取一个字符串中的第一个不重复字符import java.io.*public class Test{public static int search(Strin

获取一个字符串中的第一个不重复字符

import java.io.*;public class Test{public static int search(String str){int[][] a=new int[128][2];int length=str.length();for(int i=0;i<length;i++){int j=(int)str.charAt(i);a[j][0]++;a[j][1]=i;}int minIndex=128;for(int i=0;i<128;i++){if(a[i][0]==1){if(minIndex>a[i][1]){minIndex=a[i][1];}}}return minIndex;}public static void main(String[] args){BufferedReader br=null;String str=null;while(true){try{br=new BufferedReader(new InputStreamReader(System.in));str=br.readLine();}catch(Exception e){e.printStackTrace();}int result=search(str);if(result==128){System.out.println("没有不重复的字符");}else{System.out.println(str.charAt(result));}}}}


这是我的代码,时间复杂度为O(n),请各位看看有没有问题,或者可以改进的。谢谢了

热点排行