import java.util.HashMap; import java.util.Map; public class Solution { public int FirstNotRepeatingChar(String str) { if(str == null) return -1; Map<Character, Integer> map = new HashMap<>(); for(int i = 0;i <str.length(); i++) { if(map.containsKey(str.charAt(i))) { map.put(str.charAt(i), map.get(str.charAt(i))+1); }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; } }
原文:https://www.cnblogs.com/yihangZhou/p/10479745.html