获取一个字符串中的第一个不反复字符
获取一个字符串中的第一个不重复字符
这是我的代码,时间复杂度为O(n),请各位看看有没有问题,或者可以改进的。谢谢了
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),请各位看看有没有问题,或者可以改进的。谢谢了