using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return int整型 */ public int FirstNotRepeatingChar (string str) { // write code here if (string.IsNullOrWhiteSpace(str)) return -1; if (str.Length == 1) return 0; Dictionary<char, List<int>> kpvs = new Dictionary<char, List<int>>(); for (int i = 0; i < str.Length; i++) { if (!kpvs.ContainsKey(str[i])) kpvs.Add(str[i], new List<int>()); kpvs[str[i]].Add(i); } foreach (var kpv in kpvs) { if (kpv.Value.Count > 1) continue; return str.IndexOf(kpv.Key); } return -1; } }