Time limit: 1 seconds space limit: 32768K heat index: 106210 The title describes the first occurrence of a character in a string (1<= string length <=10000, all letters), and returns its position
ImportJava.util.HashMap; Public classSolution { Public intFirstnotrepeatingchar (String str) {HashMap<Character,Integer> hashmap=NewHashmap<>(); if(str==NULL){ return-1; } for(intI=0;i<str.length (); i++){ if(Hashmap.containskey (Str.charat (i))) {intCount=Hashmap.get (Str.charat (i)); Hashmap.remove (Str.charat (i)); Hashmap.put (Str.charat (i), Count+1); }Else{hashmap.put (Str.charat (i),1); } } for(intJ=0;j<str.length (); j + +){ if(Hashmap.get (Str.charat (j)) ==1){ returnJ; } } return-1; }}
It is easy to understand that the javahashmap,key-values mapping relationship is applied, and the first character with a value of 1 is found.
Java---The first occurrence of only one character--hashmap application