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.
输入一组小写字母,找到其中第一个不重复的字母的索引号,如果找不到则返回-1。
思路:为26个字母设置计数器数组,然后按照s的顺序找到第一个计数器值为1的字母。
int firstUniqChar(string s) {
int count[26],i;
for(i=0;i<26;i++)
count[i]=0;
for(i=0;i<s.size();i++)
count[int(s[i]-'a')]++;
for(i=0;i<s.size();i++)
{
if(count[s[i]-'a']==1)
return i;
}
return -1;
}