Topic links:
Idea: using the ASCII code mapping, the number of times each character appears and the location of storage into the last occurrence of two-dimensional array, and then re-iterate two-dimensional array, and identify the number 1 position appear minimal, return to this location is ok, complex of O (n)
Implementation source code;
Package Penalty for niuke; / ** * az: 97-122 * AZ: 65-90 * / public class first appears only one character { public int FirstNotRepeatingChar (String str) { IF (str.length () == 0 | | STR == null ) return 0 ; int [] [] = statistic new new int [53 is] [2 ]; // Mapping the ASCII, Times First IS, IS SECOND LOCATION char [] TEMP = str.toCharArray (); for ( int I = 0; I <temp.length; ++ I) { IF (TEMP [I]> = 97) { int trans = temp[i] - 96; statistic[trans][0] += 1; statistic[trans][1] = i; }else{ int trans = temp[i] - 38; statistic[trans][0] += 1; statistic[trans][1] = i; } }//statistic is over int min = 54; // char res = '#'; for(int i = 1;i<53;++i){ if(statistic[i][0]==1&&statistic[i][1]<min){ min = statistic[i][1]; // res = (char)i; } } if(min==54) return -1; else return min; } }
Code is AC
I hope to be helpful
the above