题目描述
在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;
public class Solution {
public int FirstNotRepeatingChar(String str) {
HashMap<Character, Integer> hashMap = new HashMap<>();
int len = str.length();
if(len<1||len>1000)
return -1;
for(int i=0;i<len;i++){
if(hashMap.containsKey(str.charAt(i))){
int num = hashMap.get(str.charAt(i));
hashMap.put(str.charAt(i), ++num);
}else
hashMap.put(str.charAt(i), 1);
}
for(int i=0;i<len;i++){
char c= str.charAt(i);
if(hashMap.get(c)==1)
return i;
}
return -1;
}
}