`
cq062364
  • 浏览: 2154 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

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

阅读更多
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),请各位看看有没有问题,或者可以改进的。谢谢了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics