c# Leetcode 387. 字符串中的第一个唯一字符

击败了全国百分之29.9%。

总之这类题都是一个套路,刷到现在关于dictionary 我已经轻车熟路。

 知识点:indexOf

提供的c#答案:

public static int FirstUniqChar(string s)
		{
			if (string.IsNullOrEmpty(s))
				 return -1;
			var dic = new Dictionary<char, int>();
			for (int i = 0; i < s.Length; i++)
			{
				if (dic.ContainsKey(s[i]))
					dic[s[i]]++;
				else
					dic[s[i]] = 1;
			}
			int res = -1;
			foreach (var item in dic)
			{
				if (item.Value==1)
				{ 
					res= s.IndexOf(item.Key);
					break; 
				} 
			}
			return res;
			 
		}

猜你喜欢

转载自blog.csdn.net/us2019/article/details/86508522