在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
package offer.FirstNotRepeatingChar;
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int FirstNotRepeatingChar(String str) {
int len=str.length();
Map map=new HashMap<>();
for(int i=0;i<len;i++){
char item=str.charAt(i);
if(map.get(item)==null){
map.put(item,1);
}else{
int cnt= (int) map.get(item);
map.put(item,cnt+1);
}
}
int rs=-1;
for(int i=0;i<len;i++){
char item=str.charAt(i);
int cnt= (int) map.get(item);
if(cnt==1){
rs=i;
break;
}
}
return rs;
}
}