387.求字符串中第一个独立存在的字符 First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode", return 2.
Note: You may assume the string contain only lowercase letters.
public class Solution {public int FirstUniqChar(string s) {int index = -1;char[] chars = s.ToCharArray();char c = ' ';int length = chars.Length;Dictionary<char,int> d = new Dictionary<char,int>();for(int i =0;i<length;i++){c = chars[i];int v = 0;if(d.TryGetValue(c,out v)){d[c] = v + 1;}else{d.Add(c,1);}}for(int i=0;i<length;i++){c = chars[i];if(d[c] == 1){index = i;break;}}return index;}}

浙公网安备 33010602011771号