思路:
遍历str字符串,使用hashmap进行保存字符value值为1,如果找到重复的字符就将其value变为2,完成之后遍历str字符串判断在map中的value值,结果为1则符合题意返回
实现:
import java.util.HashMap;
public class Solution {
public int FirstNotRepeatingChar(String str) {
HashMap<Character,Integer> map = new HashMap<>();
for(int i = 0;i < str.length() ; i++){
if(map.containsKey(str.charAt(i))){
map.put(str.charAt(i),2);
}
else map.put(str.charAt(i),1);
}
for(int i = 0;i < str.length() ; i++){
if(map.get(str.charAt(i)) == 1)
return i;
}
return -1;
}
}