leetcode387

public class Solution {
    public int FirstUniqChar(string s) {
        Dictionary<char, int> dic = new Dictionary<char, int>();
            foreach (char c in s)
            {
                if (!dic.ContainsKey(c))
                {
                    dic.Add(c, 1);
                }
                else
                {
                    dic[c]++;
                }
            }

            var list = dic.Where(x => x.Value == 1).ToList();

            var index = -1;

            for (int i = 0; i < s.Length; i++)
            {
                var c = s[i];
                if (list.Any(x => x.Key == c))
                {
                    index = i;
                    break;
                }
            }
            Console.WriteLine(index);
            return index;
    }
}

https://leetcode.com/problems/first-unique-character-in-a-string/#/description

原文地址:https://www.cnblogs.com/asenyang/p/6732393.html